filemanager/src/inc/fmutils_s60.cpp
changeset 37 15bc28c9dd51
parent 16 ada7962b4308
child 46 d58987eac7e8
--- a/filemanager/src/inc/fmutils_s60.cpp	Mon May 03 12:24:39 2010 +0300
+++ b/filemanager/src/inc/fmutils_s60.cpp	Tue Aug 24 10:24:14 2010 +0800
@@ -13,82 +13,96 @@
 *     Zhiqiang Yang <zhiqiang.yang@nokia.com>
 * 
 * Description:
-*     The source file of the file manager utilities
+*     The source file of the file manager utilities on Symbian
 */
 
 
 #include "fmutils.h"
+#include "fmcommon.h"
 #include "fms60utils.h"
-#include "fmcommon.h"
 
-#include <QRegExp>
-
+#include <apgcli.h>
+#include <sysutil.h>
+#include <f32file.h>
 #include <coemain.h>
+#include <pathinfo.h>
 #include <driveinfo.h>
 #include <e32property.h>
 #include <coreapplicationuisdomainpskeys.h>
-#include <f32file.h>
-#include <apgcli.h>
-#include <pathinfo.h>
-#include <CDirectoryLocalizer.h>
-#include <XQConversions>
+
+#include <QDir>
+#include <QFile>
+#include <QRegExp>
 #include <QStringList>
 #include <QFileInfoList>
-#include <QDir>
-#include <QFile>
 
+#include <xqappmgr.h>
+#include <XQConversions>
 #include <xqaiwrequest.h>
-#include <xqappmgr.h>
 
-#include <shareuidialog.h>
+#include <shareui.h>
+#include <hbglobal.h>
+#include <hbdirectorynamelocalizer.h>
 
 #define BURCONFIGFILE  "z:/private/2002BCC0/burconfig.xml"
 
-QString FmUtils::getDriveNameFromPath( const QString &path )
+/*!
+    query drive info and status for \a driveName
+    return \a FmDriverInfo
+*/
+FmDriverInfo FmUtils::queryDriverInfo( const QString &driveName )
 {
-    if( path.length() <3 ) {
-        return QString();
+    if( driveName.isEmpty() ) {
+        return FmDriverInfo( 0, 0, driveName, QString(), FmDriverInfo::EDriveNotPresent );
     }
-    return path.left( 3 );
-}
-
-QString FmUtils::getDriveLetterFromPath( const QString &path )
-{
-	if( path.length() <2 ) {
-        return QString();
-    }
-    return path.left( 1 );
-}
-
-FmDriverInfo FmUtils::queryDriverInfo( const QString &driverName )
-{
     CCoeEnv *env = CCoeEnv::Static();
     RFs& fs = env->FsSession();
 
     TVolumeInfo volumeInfo;
     TInt drive = 0;
-    drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+    drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
 
     quint32 state( 0 );
-    int err = fs.Volume( volumeInfo, drive );
+    
+    int volumeInfoErr( KErrNone );
+    int driveInfoErr( KErrNone );
+    int errorCode( KErrNone );
+    volumeInfoErr = fs.Volume( volumeInfo, drive );
+    errorCode = volumeInfoErr;
     QString volumeName( (QChar*)( volumeInfo.iName.Des().Ptr() ), volumeInfo.iName.Length() );
-
-    if( err == KErrNone ) {
-        TDriveInfo driveInfo = volumeInfo.iDrive;
+    
+    TDriveInfo driveInfo;
+    if( volumeInfoErr == KErrNone ) {
+        driveInfo = volumeInfo.iDrive;
+    } else {
+        driveInfoErr = fs.Drive( driveInfo, drive );
+        if( driveInfoErr != KErrNone ) {
+            errorCode = driveInfoErr;
+        }
+    }
+    
+    if( volumeInfoErr == KErrNone || driveInfoErr == KErrNone ) {
+        //TDriveInfo driveInfo = volumeInfo.iDrive;
     
         quint32 drvStatus( 0 );
-        err = DriveInfo::GetDriveStatus( fs, drive, drvStatus );
+        int err = DriveInfo::GetDriveStatus( fs, drive, drvStatus );
         if( err == KErrNone ) {
-            QString logString = driverName +':'+ QString::number( drvStatus);
-            FmLogger::log(logString);
             
             if ( ( drvStatus & DriveInfo::EDriveInternal ) &&
                  ( drvStatus & DriveInfo::EDriveExternallyMountable ) ){
                 // Handle mass storage bits here
-        
                 state |= FmDriverInfo::EDriveMassStorage | FmDriverInfo::EDriveRemovable;
             }
         
+            if ( drvStatus & DriveInfo::EDriveUsbMemory )
+                {
+                state |= FmDriverInfo::EDriveUsbMemory;
+                }
+            
+            if ( drvStatus & DriveInfo::EDriveRemote )
+                {
+                state |= FmDriverInfo::EDriveRemote;
+                }
             
             if ( drvStatus & DriveInfo::EDriveRom ){
                 state |= FmDriverInfo::EDriveRom;  
@@ -123,12 +137,36 @@
                 state |= FmDriverInfo::EDriveNotPresent;    
             }
         }
+        // If memory card is not ready but type is present,
+        // then check if it is reserved.
+        if( err == KErrNone && volumeInfoErr == KErrNotReady &&
+            driveInfo.iType != EMediaNotPresent )
+            {
+            // Check USB file transfer state
+            TInt prop( ECoreAppUIsUSBFileTransferUninitialized );
+            RProperty::Get(
+                KPSUidCoreApplicationUIs,
+                KCoreAppUIsUSBFileTransfer, prop );
+            if ( prop == ECoreAppUIsUSBFileTransferActive )
+                {
+                errorCode = KErrInUse; // Reserved for file transfer
+                }
+            }
+        if( err!= KErrNone )
+            {
+            errorCode = err;
+            }
     }
-    //handle error code
-    switch( err )
+
+    // handle error code
+    // volumeInfoErr will occur while drive is lock,corrupted...
+    // driveInfoErr can not be promoted for locked, corrupted drive.
+    // so we can not use driveInfoErr to justify EDriveAvailable
+    switch( errorCode )
     {
     case KErrNone:
-        state |= FmDriverInfo::EDriveAvailable;
+        // this drive could be used as it is not be locked, or corrupt.
+        state |= FmDriverInfo::EDriveAvailable; 
         break;
     case KErrLocked:
         state |= FmDriverInfo::EDriveLocked;
@@ -136,114 +174,42 @@
     case KErrCorrupt:
         state |= FmDriverInfo::EDriveCorrupted;
         break;
+    case KErrInUse:
+        state |= FmDriverInfo::EDriveInUse;
+        break;
     default: // other errors
         state |= FmDriverInfo::EDriveNotPresent;
         break;
     }
-    return FmDriverInfo( volumeInfo.iSize, volumeInfo.iFree, driverName, volumeName, state );
+    QString logString ( "FmUtils::queryDriverInfo_" + driveName + 
+            "_volumeInfoErr:" + QString::number( volumeInfoErr ) +
+            "_driveInfoErr:" + QString::number( driveInfoErr ) +
+            "_errorCode:" + QString::number( errorCode ) + 
+            "_driveSatus:" + QString::number( state ) );
+    FM_LOG( logString );
+    return FmDriverInfo( volumeInfo.iSize, volumeInfo.iFree, driveName, volumeName, state );
 }
 
-QString FmUtils::formatStorageSize( quint64 size )
+/*!
+    remove drive password for \a driveName
+    \a Pwd is original password.
+*/
+int FmUtils::removeDrivePwd( const QString &driveName,  const QString &Pwd )
 {
-	if ( size < 1000 ) {
-		return QString::number( size ) + " B";
-	} else if ( size < 1000 * 1000 ) {
-		return QString::number( size / 1024.0, 'f', 2 ) + " KB";
-	} else if ( size < 1000 * 1000 * 1000 ) {
-		return QString::number( size / (1024.0 * 1024.0), 'f', 1 ) + " MB";
-	} else {
-	    return QString::number( size / ( 1024.0 * 1024.0 * 1024.0 ), 'f', 1 ) + " GB";	    
-	}
-}
-/*
-quint32 FmUtils::getDriverState( const QString &driverName )
-{    
-    CCoeEnv *env = CCoeEnv::Static();
-    RFs& fs = env->FsSession();
-
-    TVolumeInfo volumeInfo;
-    TInt drive = 0;
-    drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
-
-    quint32 state( 0 );
-    int err = fs.Volume( volumeInfo, drive );
-    QString volumeName( (QChar*)( volumeInfo.iName.Des().Ptr() ), volumeInfo.iName.Length() );
-
-    //handle error code
-	if( err != KErrNone ) {
-            state |= FmDriverInfo::EDriveNotPresent;
-	}
-	if ( err == KErrLocked ) {
-            state |= FmDriverInfo::EDriveLocked;
-	}
-	
-    TDriveInfo driveInfo = volumeInfo.iDrive;
-
-    quint32 drvStatus( 0 );
-    DriveInfo::GetDriveStatus( fs, drive, drvStatus );
-
-    QString logString = driverName +':'+ QString::number( drvStatus);
-    FmLogger::log(logString);
-    
-    if ( ( drvStatus & DriveInfo::EDriveInternal ) &&
-         ( drvStatus & DriveInfo::EDriveExternallyMountable ) ){
-        // Handle mass storage bits here
-
-        state |= FmDriverInfo::EDriveMassStorage | FmDriverInfo::EDriveRemovable;
+    if( driveName.isEmpty() || Pwd.length() > FmMaxLengthofDrivePassword ) {
+        return FmErrWrongParam;
     }
-
-    
-    if ( drvStatus & DriveInfo::EDriveRom ){
-        state |= FmDriverInfo::EDriveRom;  
-    }
-    
-    if ( drvStatus & DriveInfo::EDriveRam ){
-        state |= FmDriverInfo::EDriveRam;  
-    }
-
-    if ( driveInfo.iMediaAtt & KMediaAttFormattable ){
-        state |= FmDriverInfo::EDriveFormattable;
-    }
-    if ( driveInfo.iMediaAtt & KMediaAttWriteProtected ){
-        state |= FmDriverInfo::EDriveWriteProtected;
-    }
-    if ( driveInfo.iMediaAtt & KMediaAttHasPassword ){
-        state |= FmDriverInfo::EDrivePasswordProtected;
-    }    
-    if ( driveInfo.iMediaAtt & KMediaAttLocked ){
-        state |= FmDriverInfo::EDriveLocked;
-    }
-
-    if ( driveInfo.iDriveAtt & KDriveAttRemovable ){
-        state |= FmDriverInfo::EDriveRemovable;
-
-        if ( drvStatus & DriveInfo::EDriveSwEjectable ){
-            state |= FmDriverInfo::EDriveEjectable;
-        }
-    }
-    
-    if( driveInfo.iType == EMediaNotPresent ){
-        state |= FmDriverInfo::EDriveNotPresent;    
-    }
-    
-    return state;
-
-}
-*/
-
-int FmUtils::removeDrivePwd( const QString &driverName,  const QString &Pwd )
-{
-    QString logString = "Drive name:" + driverName;
-    FmLogger::log( logString );
+    QString logString = "Drive name:" + driveName;
+    FM_LOG( logString );
     logString = "Password:" + Pwd;
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     CCoeEnv *env = CCoeEnv::Static();
 	RFs& fs = env->FsSession();
 
     TInt drive = 0;
 
-	drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+    drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
 
     HBufC* password16 = XQConversions::qStringToS60Desc( Pwd );
     TMediaPassword password;   
@@ -253,10 +219,12 @@
     int err( fs.ClearPassword( drive, password ) );
 
     logString = "Drive:" + QString::number( drive );
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     logString = "Clear password error:" + QString::number( err );
-    FmLogger::log( logString );
+    FM_LOG( logString );
+
+    delete password16;
 
     if( err == KErrNone ){
         return FmErrNone;   
@@ -269,18 +237,24 @@
     }
 }
 
-int FmUtils::unlockDrive( const QString &driverName,  const QString &Pwd )
+/*!
+    Unlock drive \a driveName with provided password \a Pwd
+*/
+int FmUtils::unlockDrive( const QString &driveName,  const QString &Pwd )
 {
-    QString logString = "Drive name:" + driverName;
-    FmLogger::log( logString );
+    if( driveName.isEmpty() || Pwd.length() > FmMaxLengthofDrivePassword ) {
+        return FmErrWrongParam;
+    }
+    QString logString = "Drive name:" + driveName;
+    FM_LOG( logString );
     logString = "Password:" + Pwd;
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     CCoeEnv *env = CCoeEnv::Static();
 	RFs& fs = env->FsSession();
 
     TInt drive = 0;
-	drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
     
     HBufC* password16 = XQConversions::qStringToS60Desc( Pwd );
     TMediaPassword password;   
@@ -290,9 +264,11 @@
     int err( fs.UnlockDrive( drive, password, ETrue) );
 
     logString = "Drive:" + QString::number( drive );
-    FmLogger::log( logString );
+    FM_LOG( logString );
     logString = "Unlock drive error:" + QString::number( err );
-    FmLogger::log( logString );
+    FM_LOG( logString );
+
+    delete password16;
 
     if( err == KErrNone ){
         return FmErrNone;   
@@ -311,27 +287,40 @@
     }
 }
 
-int FmUtils::checkDrivePwd( const QString &driverName, const QString &pwd )
+/*!
+    Check if \a pwd is the right password for drive \a driveName
+*/
+int FmUtils::checkDrivePwd( const QString &driveName, const QString &pwd )
 {
-    QString logString = "checkDrivePwd Drive name:" + driverName;
+    if( driveName.isEmpty() || pwd.length() > FmMaxLengthofDrivePassword ) {
+        return FmErrWrongParam;
+    }
+    QString logString = "checkDrivePwd Drive name:" + driveName;
     logString += " password:" + pwd;
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
-    return setDrivePwd( driverName, pwd, pwd );
+    return setDrivePwd( driveName, pwd, pwd );
 }
 
-int FmUtils::setDrivePwd( const QString &driverName, const QString &oldPwd, const QString &newPwd)
+/*!
+    Set new password \a newPwd for drive \a driveName. \a oldPwd is old password
+*/
+int FmUtils::setDrivePwd( const QString &driveName, const QString &oldPwd, const QString &newPwd)
 {
-    QString logString = "setDrivePwd Drive name:" + driverName ;
+    if( driveName.isEmpty() || 
+        oldPwd.length() > FmMaxLengthofDrivePassword || newPwd.length() > FmMaxLengthofDrivePassword  ) {
+        return FmErrWrongParam;
+    }
+    QString logString = "setDrivePwd Drive name:" + driveName ;
     logString += " Old password:" + oldPwd;
     logString += " New password:" + newPwd;
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     CCoeEnv *env = CCoeEnv::Static();
 	RFs& fs = env->FsSession();
 
     TInt drive = 0;
-	drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
 	
     HBufC* newPassword16 = XQConversions::qStringToS60Desc( newPwd);
     HBufC* oldPassword16 = XQConversions::qStringToS60Desc( oldPwd );
@@ -348,10 +337,12 @@
     int err( fs.LockDrive( drive, oldPassword, newPassword, ETrue ) );
 
     logString = "Drive:" + QString::number( drive );
-    FmLogger::log( logString );
+    FM_LOG( logString );
     logString = "Password set error:" + QString::number( err );
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
+    delete newPassword16;
+    delete oldPassword16;
     if( err == KErrNone ){
         return FmErrNone;   
     }
@@ -363,17 +354,26 @@
     }
 }
 
+/*!
+    Set \a pwd as empty password
+*/
 void FmUtils::emptyPwd( QString &pwd )
 {
-    TPtr des ( ( XQConversions::qStringToS60Desc( pwd ) )->Des() );
-    des.FillZ( des.MaxLength() );
-    des.Zero();
-    pwd = XQConversions::s60DescToQString( des );
+    TBuf< FmMaxLengthofDrivePassword > nullPwd;
+    nullPwd.FillZ( nullPwd.MaxLength() );
+    nullPwd.Zero();
+    pwd = XQConversions::s60DescToQString( nullPwd );
 }
 
-int FmUtils::renameDrive( const QString &driverName, const QString &newVolumeName)
+/*!
+    Set drive volume for drive \a driveName
+*/
+int FmUtils::renameDrive( const QString &driveName, const QString &newVolumeName)
 {
-    foreach( QChar ch, newVolumeName )
+    if( driveName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
+    foreach( const QChar &ch, newVolumeName )
     {
         bool a = ch.isSpace();
         bool b = ch.isLetterOrNumber();
@@ -388,14 +388,14 @@
 	RFs& fs = env->FsSession();
 
     TInt drive = 0;
-	drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
 
     TPtr newName ( ( XQConversions::qStringToS60Desc( newVolumeName ) )->Des() );
 
     int err( fs.SetVolumeLabel( newName, drive ));
     
     QString logString = "Rename error:" + QString::number( err );
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     if( err == KErrNone ){
         return FmErrNone;   
@@ -408,13 +408,19 @@
     }
 }
 
-void FmUtils::ejectDrive( const QString &driverName )
+/*!
+    Eject drive \a driveName
+*/
+int FmUtils::ejectDrive( const QString &driveName )
 {
+    if( driveName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "FmUtils::ejectDrive start";
-    FmLogger::log( logString );
+    FM_LOG( logString );
 
     TInt drive = 0;
-	drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
 
     const int KDriveShift = 16;
 
@@ -424,115 +430,169 @@
         KCoreAppUIsMmcRemovedWithoutEject,
         ECoreAppUIsEjectCommandUsedToDrive | ( drive << KDriveShift )
         );
+    return FmErrNone;
+}
+
+/*!
+    Check if drive \a driveName is accessable for user
+*/
+bool FmUtils::checkDriveAccessFilter( const QString &driveName )
+{
+    if( driveName.isEmpty() ) {
+        return false;
+    }
+    FmDriverInfo driveInfo = queryDriverInfo( driveName );
+    if( ( driveInfo.driveState()& FmDriverInfo::EDriveRam ) ||
+        ( driveInfo.driveState()& FmDriverInfo::EDriveRom ) ) {
+        return false;
+    }
+    return true;
 }
 
-QString FmUtils::getFileType( const QString &filePath  )
+/*!
+    This function should be called to adjust path if user goto a drive.
+    data folder will be append to C:\ becuase user could only view C:\data instead C:\ 
+*/
+QString FmUtils::checkDriveToFolderFilter( const QString &path )
 {
-    RApaLsSession apaSession;
-    TDataType dataType;
-    TUid appUid;
-    
-    TBuf<128> mimeTypeBuf;
-        
-    int err = apaSession.Connect();
+    QString checkedPath = fillPathWithSplash( path );
+    if( checkedPath.compare( Drive_C, Qt::CaseInsensitive ) == 0 ) {
+        checkedPath += QString( "data" ) + QDir::separator();
+        return checkedPath;
+    }
+    return path;
+
+}
+
+/*!
+    This function should be called to adjust path if user back to up level path.
+    If user is at C:\data then path level should be returned as C:\
+    Becuase C:\data is root path for C drive 
+*/
+QString FmUtils::checkFolderToDriveFilter( const QString &path )
+{
+    QString logString;
+    logString = QString( "checkFolderToDriveFilter: " ) + path;
+    FM_LOG( logString );
+    QString checkedPath = fillPathWithSplash( path );
+
+    logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
+    FM_LOG( logString );
     
-    if ( err == KErrNone ){   
-        err = apaSession.AppForDocument( XQConversions::qStringToS60Desc( filePath )->Des(), 
-                                         appUid, dataType );
-        
-        if( err == KErrNone ){
-            mimeTypeBuf.Copy(dataType.Des8());
-        }  
+    if( checkedPath.compare( Folder_C_Data, Qt::CaseInsensitive ) == 0 ) {
+        FM_LOG( QString( " change from c:/data/ to C:/" ) );
+        return Drive_C;
     }
-    
-    apaSession.Close();
-    return XQConversions::s60DescToQString( mimeTypeBuf );
+    return path;
+
 }
 
-quint64 FmUtils::getDriveDetailsResult( const QString &folderPath, const QString &extension )
+/*!
+    Check if \a path is accessable for user
+*/
+int FmUtils::isPathAccessabel( const QString &path )
 {
-    int err;
-    
-    RFs fs;
-    err = fs.Connect();
-    
-    QString string( formatPath( folderPath ) );
+    // Used to check if path is accessable, very important feature
+    // and will return filemanager error.
+    FM_LOG( QString( "isPathAccessabel:" ) + path );
+    if( path.isEmpty() ) {
+        return FmErrPathNotExist;
+    }
 
-    TPtrC desFolderPath( XQConversions::qStringToS60Desc( string )->Des() );
-    TPtrC ptrExtension( XQConversions::qStringToS60Desc( extension )->Des() );
-    
-    CDir* results = 0;
-    TParse parse;
-    
-    quint64 size = 0;
-    
-    const TInt pathlength = ptrExtension.Length() + desFolderPath.Length();
-    
-    if ( pathlength > KMaxFileName ){
-        err = KErrNotFound;   
+    // used to filter locked/ejected/corrupted drive
+    // check if drive is available, no matter if it is a drive, a folder, or a file.
+    if( !isDriveAvailable( path ) ) {
+        FM_LOG( QString( "isPathAccessabel false: path is drive and not available" ) );
+        return FmErrDriveNotAvailable;
     }
-    else{
-        err = fs.Parse( ptrExtension, desFolderPath, parse );
-        err = fs.GetDir( parse.FullName(), KEntryAttMaskSupported|KEntryAttAllowUid, 
-            ESortNone, results );
-        
-        TDesC des = parse.FullName();
-        
-        if (err == KErrNotFound)
-            {
-            return 0;
-            }
-    }
-    
-    if ( results ){
-        CleanupStack::PushL(results);
+
+    QFileInfo fileInfo( path );
 
-        // Go through all files in the list and tell subclass
-        TFileName file;
-        const TInt count = results->Count();
-        for( TInt i=0; i<count; ++i ){
-            const TEntry& entry = (*results)[i];
-            file = desFolderPath;
-            file += entry.iName;
-            size += entry.iSize;          
-        }
-        CleanupStack::PopAndDestroy(results);
+    if( fileInfo.absoluteFilePath().contains( Drive_C, Qt::CaseInsensitive ) &&
+        !fileInfo.absoluteFilePath().contains( Folder_C_Data, Qt::CaseInsensitive ) ) {
+        FM_LOG( QString( "isPathAccessabel false: path contain C and not in data folder" ) );
+        return FmErrPathDenied;
     }
-    
-    fs.Close();
-    
-    return size;  
+    if( !checkDriveAccessFilter( FmUtils::getDriveNameFromPath( fileInfo.absoluteFilePath() ) ) ){
+        return FmErrDriveDenied;
+    }
+    if( !fileInfo.exists() ) {
+        FM_LOG( QString( "isPathAccessabel false: path not exist" ) );
+        return FmErrPathNotExist;
+    }
+    FM_LOG( QString( "isPathAccessabel FmErrNone" ) );
+    return FmErrNone;
 }
 
-bool FmUtils::isDriveC( const QString &driverName )
+/*!
+    Check if drive related to \a path is available.
+    This function should not check if path is available. Only responsible for checking drive  
+    When MMC is not inserted, also return false
+*/
+bool FmUtils::isDriveAvailable( const QString &path )
 {
-    TInt drive = 0;
-    drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
-    if( drive == EDriveC ){
+    FM_LOG( QString( "isDriveAvailable:" ) + path );
+    if( path.isEmpty() ) {
+        return false;
+    }
+    FmDriverInfo::DriveState driveState = queryDriverInfo( path ).driveState();
+    if( ( driveState & FmDriverInfo::EDriveAvailable ) ) {
+        FM_LOG( QString( "isDriveAvailable true" ) );
         return true;
     }
-    else{
-        return false;
-    }
-   
+    FM_LOG( QString( "isDriveAvailable false" ) );
+    return false;
 }
 
-bool FmUtils::isDrive( const QString &path )
+/*!
+    Check if \a folderPath is default folder for system
+*/
+bool FmUtils::isDefaultFolder( const QString &folderPath  )
 {
-   bool ret( false );
-   if( path.length() <= 3 && path.length() >=2 ) {
-       ret = true;
-   }
-   
-   return ret;   
+    HBufC *path = XQConversions::qStringToS60Desc( folderPath );
+    TPtrC desFolderPath( path->Des() );
+    
+    bool ret( true );
+    TInt pathType( PathInfo::PathType( desFolderPath ) );
+    switch( pathType ){
+       case PathInfo::ENotSystemPath:{
+           QString locString( localize( folderPath ) );
+            if ( locString.isEmpty() ){
+                ret = false;
+                break;
+            }
+            ret = true;
+            break;
+            }
+        case PathInfo::EPhoneMemoryRootPath: // FALL THROUGH
+        case PathInfo::EMemoryCardRootPath: // FALL THROUGH
+        case PathInfo::ERomRootPath:{
+            ret = false;
+            break;
+        }
+        // Accept other folders
+        default:{
+            ret = true;
+            break;
+        }
+    }
+    delete path;
+    return ret;
 }
 
-void FmUtils::createDefaultFolders( const QString &driverName )
+/*!
+    Create system default folders for drive \a driveName.
+    Default folders should be created after format a drive.
+*/
+void FmUtils::createDefaultFolders( const QString &driveName )
 {
+    if( driveName.isEmpty() ) {
+        return;
+    }
     int err;
     
     TInt drive = 0;
-    drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
+    drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
     
     RFs fs;
     err = fs.Connect();
@@ -595,171 +655,98 @@
     CleanupStack::PopAndDestroy( array );
 }
 
-QString FmUtils::fillPathWithSplash( const QString &filePath )
-{
-    QString newFilePath( filePath );
-    if( filePath.isEmpty() ) {
-        return newFilePath;
-    }
-
-    if( filePath.at( filePath.length()-1 ) != QChar( '/' ) ){
-        newFilePath.append( QChar( '/' ) );
-    }
-    return newFilePath;
-}
-
-QString FmUtils::removePathSplash( const QString &filePath )
+/*!
+    In Symbian system, default folders will be localized.
+    So localize is used to check if a path is a default folder
+    \sa isDefaultFolder
+*/
+QString FmUtils::localize( const QString &path )
 {
-    QString newFilePath( filePath );
-    if( filePath.right( 1 ) == QChar( '/' ) || filePath.right(1) == QString( "\\" ) ) {
-        newFilePath = filePath.left( filePath.length() - 1 );
-    }
-    return newFilePath;
-}
-
-// used to filter drive which need be hide.
-bool FmUtils::checkDriveFilter( const QString &driveName )
-{
-    if( driveName.contains( "D:" ) || driveName.contains( "Z:" ) ) {
-        return false;
-    }
-    return true;
+    // HbDirectoryNameLocalizer can not recognize path with \ in the end
+    QString locPath( removePathSplash( path ) );
+    
+    HbDirectoryNameLocalizer localizer;
+    return localizer.translate( locPath );
 }
 
-QString FmUtils::checkDriveToFolderFilter( const QString &path )
+/*!
+    set the \a desFile attributes as the same with \a srcFile
+*/
+int FmUtils::setFileAttributes( const QString &srcFile, const QString &desFile )
 {
-    /*
-    QFileInfo fileInfo( path );
-    if( !fileInfo.exists() ) {
-            return QString();
-        }
-    */
-    QString checkedPath = fillPathWithSplash( path );
-    if( checkedPath.compare( QString( "C:/"), Qt::CaseInsensitive ) == 0 ) {
-        checkedPath += QString( "data/" );
-        return checkedPath;
+    RFs fsSession;
+    User::LeaveIfError( fsSession.Connect() ); 
+    CleanupClosePushL( fsSession );
+    RFile64 src;
+    RFile64 des;
+    HBufC *buf1 = XQConversions::qStringToS60Desc( removePathSplash( formatPath( srcFile ) ) );
+    HBufC *buf2 = XQConversions::qStringToS60Desc( removePathSplash( formatPath( desFile ) ) );
+    User::LeaveIfError( src.Open( fsSession, *buf1, EFileRead | EFileShareReadersOnly ) );
+    User::LeaveIfError( des.Open( fsSession, *buf2, EFileWrite | EFileShareExclusive ) );
+    TTime mod;
+    int err = src.Modified( mod );;
+    if ( err == FmErrNone ) {
+        err = des.SetModified( mod );    
     }
-    return path;
-
+    TUint att( 0 );
+    if ( err == FmErrNone ) {
+        err = src.Att( att );        
+    }
+    if ( err == FmErrNone ) {
+        des.SetAtt( att, ( ~att ) & KEntryAttMaskSupported );
+    }    
+    src.Close();
+    des.Close();
+    fsSession.Close();
+    CleanupStack::PopAndDestroy(); // fsSession
+    return err;
 }
 
-QString FmUtils::checkFolderToDriveFilter( const QString &path )
+/*!
+    judge whether there is enough space on \a targetDrive for \a size.
+    return true if has, false if not.
+*/
+bool FmUtils::hasEnoughSpace( const QString &targetDrive, qint64 size )
 {
-    QString logString;
-    logString = QString( "checkFolderToDriveFilter: " ) + path;
-    FmLogger::log( logString );
-    QString checkedPath = fillPathWithSplash( path );
-
-    logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
-    FmLogger::log( logString );
+    RFs fsSession;
+    QT_TRAP_THROWING( fsSession.Connect() ); 
+    CleanupClosePushL( fsSession );
+    TInt dstDrv(0);
+    HBufC* hbuf = XQConversions::qStringToS60Desc( targetDrive );
+    QT_TRAP_THROWING( RFs::CharToDrive( hbuf->operator [](0), dstDrv ) );
+    bool ret = !SysUtil::DiskSpaceBelowCriticalLevelL( &fsSession,  size , dstDrv );
+    CleanupStack::PopAndDestroy(); // fsSession
+    return ret;
     
-    if( checkedPath.compare( QString( "C:/data/"), Qt::CaseInsensitive ) == 0 ) {
-        FmLogger::log( QString( " change from c:/data/ to C:/" ) );
-        return QString( "C:/" );
-    }
-    return path;
-
 }
 
-bool FmUtils::isPathAccessabel( const QString &path )
+/*!
+    move one file insice the same drive, from \a source to \a target.
+    return KErrNone if successful, otherwise one of the other system-wide error codes.
+*/
+int FmUtils::moveInsideDrive( const QString &source, const QString &target )
 {
-    FmLogger::log( QString( "isPathAccessabel:" ) + path );
-    if( path.length() <= 3 && !isDriveAvailable( path ) ) { //used to filter locked drive
-        FmLogger::log( QString( "isPathAccessabel false: path is drive and not available" ) );
-        return false;
-    }
-    QFileInfo fileInfo( path );
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_C ), Qt::CaseInsensitive ) &&
-        !fileInfo.absoluteFilePath().contains( QString( Folder_C_Data ), Qt::CaseInsensitive ) ) {
-        FmLogger::log( QString( "isPathAccessabel false: path contain C and not in data folder" ) );
-        return false;
-    }
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_D ), Qt::CaseInsensitive ) ) {
-        FmLogger::log( QString( "isPathAccessabel false: path contain D" ) );
+    RFs fsSession;
+    QT_TRAP_THROWING( fsSession.Connect() ); 
+    CleanupClosePushL( fsSession );
+    HBufC* oldName = XQConversions::qStringToS60Desc( source );
+    HBufC* newName = XQConversions::qStringToS60Desc( target );
+    int ret = fsSession.Rename( *oldName, *newName );
+    CleanupStack::PopAndDestroy(); // fsSession
+    return ret;    
+}
+
+/*!
+   Launch a file with associated application.
+*/
+int FmUtils::launchFile( const QString &filePath )
+
+{
+    QFile file( filePath );
+    if( !file.exists() ) {
         return false;
     }
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_Z ), Qt::CaseInsensitive ) ) {
-        FmLogger::log( QString( "isPathAccessabel false: path contain Z" ) );
-        return false;
-    }
-    if( !fileInfo.exists() ) {
-        FmLogger::log( QString( "isPathAccessabel false: path not exist" ) );
-        return false;
-    }
-    FmLogger::log( QString( "isPathAccessabel true" ) );
-    return true;
-}
-
-// only used to check drive, when MMC is not inserted, also return false
-bool FmUtils::isDriveAvailable( const QString &path )
-{
-    FmLogger::log( QString( "isDriveAvailable:" ) + path );
-    FmDriverInfo::DriveState driveState = queryDriverInfo( path ).driveState();
-    if( ( driveState & FmDriverInfo::EDriveAvailable ) ) {
-        FmLogger::log( QString( "isDriveAvailable true" ) );
-        return true;
-    }
-    FmLogger::log( QString( "isDriveAvailable false" ) );
-    return false;
-}
-
-void FmUtils::getDriveList( QStringList &driveList, bool isHideUnAvailableDrive )
-{
-    if( isHideUnAvailableDrive ) {
-        FmLogger::log( QString( "getDriveList HideUnAvailableDrive_true" ) );
-    } else {
-        FmLogger::log( QString( "getDriveList HideUnAvailableDrive_false" ) );
-    }
-    QFileInfoList infoList = QDir::drives();
-
-    foreach( QFileInfo fileInfo, infoList ) {
-        QString driveName = fileInfo.absolutePath();
-        if( checkDriveFilter( driveName ) ) {
-            if( !isHideUnAvailableDrive ) {
-                driveList.append( driveName );
-            }
-            else if ( isDriveAvailable( driveName ) ) {
-                driveList.append( driveName );
-            }
-        }
-    }
-    return;
-}
-
-QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
-{
-    QString ret;
-    QString tempDriveName = fillPathWithSplash( driveName );
-
-    ret = removePathSplash( driveName );
-    
-    FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
-    QString volumeName = driverInfo.volumeName();
-
-    if( volumeName.isEmpty() && isFillWithDefaultVolume ){
-    FmDriverInfo::DriveState driveState = queryDriverInfo( tempDriveName ).driveState();
-        if( driveState & FmDriverInfo::EDriveAvailable ){
-            if( driveState & FmDriverInfo::EDriveRemovable ) {
-                if( driveState & FmDriverInfo::EDriveMassStorage ) {
-                    volumeName.append( QObject::tr( "Mass Storage" ) );  
-                }
-                else{
-                    volumeName.append( QObject::tr( "Memory Card" ) );
-                }
-            }
-            else{
-                volumeName.append( QObject::tr( "Phone Memory" ) );
-            }
-        }
-    }
-
-    ret += QString( " " ) + volumeName;
-    return ret;
-}
-
-int FmUtils::launchFile( const QString &filePath )
-{
-    QFile file( filePath );
+        
     XQApplicationManager mAiwMgr;
     XQAiwRequest *request = mAiwMgr.create(file);
     if ( request == 0 ) {
@@ -787,12 +774,19 @@
     return FmErrNone;
 }
 
+/*!
+    Send files( \a filePathList ) with share ui.
+*/
 void FmUtils::sendFiles( QStringList &filePathList )
 {
     ShareUi shareui;
     shareui.send( filePathList, false );
 }
 
+/*!
+    return path for backup restore config file.
+    Currently \a appPath is not used.
+*/
 QString FmUtils::getBurConfigPath( QString appPath )
 {
     Q_UNUSED( appPath );
@@ -800,64 +794,66 @@
     return path;
 }
 
-bool FmUtils::isPathEqual( const QString &pathFst, const QString &pathLast )
+/*!
+    return MetaData string for \a filePath
+*/
+QString FmUtils::getFileType( const QString &filePath  )
 {
-    QString fst( fillPathWithSplash( pathFst ) );
-    QString last( fillPathWithSplash( pathLast ) );
-    if( fst.compare( last, Qt::CaseInsensitive ) == 0 ) {
-        return true;
+    RApaLsSession apaSession;
+    TDataType dataType;
+    TUid appUid;
+    
+    TBuf<128> mimeTypeBuf;
+        
+    int err = apaSession.Connect();
+    
+    if ( err == KErrNone ){   
+        err = apaSession.AppForDocument( XQConversions::qStringToS60Desc( filePath )->Des(), 
+                                         appUid, dataType );
+        
+        if( err == KErrNone ){
+            mimeTypeBuf.Copy(dataType.Des8());
+        }  
     }
-    return false;
+    
+    apaSession.Close();
+    return XQConversions::s60DescToQString( mimeTypeBuf );
 }
 
-bool FmUtils::isDefaultFolder( const QString &folderPath  )
+/*!
+    Check if drive \a driveName is drive C
+*/
+bool FmUtils::isDriveC( const QString &driveName )
 {
-    TPtrC desFolderPath( XQConversions::qStringToS60Desc( folderPath )->Des() );
-    
-    TInt pathType( PathInfo::PathType( desFolderPath ) );
-    switch( pathType ){
-       case PathInfo::ENotSystemPath:{
-           QString locString( Localize( folderPath ) );
-            if ( locString.isEmpty() ){
-                return false;
-            }
-            return true;
-            }
-        case PathInfo::EPhoneMemoryRootPath: // FALL THROUGH
-        case PathInfo::EMemoryCardRootPath: // FALL THROUGH
-        case PathInfo::ERomRootPath:{
-            return false;
-        }
-        // Accept other folders
-        default:{
-            return true;
-        }
+    if( driveName.isEmpty() ) {
+        return false;
     }
+    TInt drive = 0;
+    drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
+    if( drive == EDriveC ){
+        return true;
+    }
+    else{
+        return false;
+    }
+   
 }
 
-QString FmUtils::Localize( const QString &path )
+/*!
+    return max file name length
+*/
+int FmUtils::getMaxFileNameLength()
 {
-    QString locPath = formatPath( path );
-
-    TPtrC desPath( XQConversions::qStringToS60Desc( locPath )->Des() );
-    CDirectoryLocalizer *localizer = CDirectoryLocalizer::NewL();
-
-    localizer->SetFullPath( desPath );
-    if( localizer->IsLocalized() ){   
-        return XQConversions::s60DescToQString( localizer->LocalizedName() );
-    }
-    
-    return QString();
+    return KMaxFileName;
 }
 
-QString FmUtils::formatPath( const QString &path  )
+/*!
+    Check if length of \a path is exceed max path length. 
+*/
+bool FmUtils::checkMaxPathLength( const QString& path )
 {
-    QString formatPath = path;
-    QRegExp regExp( "/" );
-    formatPath.replace( regExp, "\\" );
-    
-    if( path.right( 1 )!= "\\"){
-        formatPath.append( "\\" );
+    if( path.length() > KMaxPath ) {
+        return false;
     }
-    return formatPath;
+    return true;
 }