filemanager/src/inc/fmutils_s60.cpp
changeset 25 b7bfdea70ca2
parent 16 ada7962b4308
child 27 df183af6b92f
--- a/filemanager/src/inc/fmutils_s60.cpp	Mon May 03 12:24:39 2010 +0300
+++ b/filemanager/src/inc/fmutils_s60.cpp	Fri Jun 25 17:08:34 2010 +0800
@@ -37,19 +37,23 @@
 #include <QDir>
 #include <QFile>
 
+#include <hbglobal.h>
+
 #include <xqaiwrequest.h>
 #include <xqappmgr.h>
 
-#include <shareuidialog.h>
+#include <shareui.h>
 
 #define BURCONFIGFILE  "z:/private/2002BCC0/burconfig.xml"
 
 QString FmUtils::getDriveNameFromPath( const QString &path )
 {
-    if( path.length() <3 ) {
+    // fillPathWithSplash make sure path length will be at least 3 if it is not empty.
+    QString checkedPath( fillPathWithSplash( path ) );
+    if( checkedPath.length() < 3 ) {
         return QString();
     }
-    return path.left( 3 );
+    return checkedPath.left( 3 );
 }
 
 QString FmUtils::getDriveLetterFromPath( const QString &path )
@@ -62,6 +66,9 @@
 
 FmDriverInfo FmUtils::queryDriverInfo( const QString &driverName )
 {
+    if( driverName.isEmpty() ) {
+        return FmDriverInfo( 0, 0, driverName, QString(), FmDriverInfo::EDriveNotPresent );
+    }
     CCoeEnv *env = CCoeEnv::Static();
     RFs& fs = env->FsSession();
 
@@ -70,17 +77,29 @@
     drive = driverName[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( volumeName == 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 ) ){
@@ -89,7 +108,11 @@
                 state |= FmDriverInfo::EDriveMassStorage | FmDriverInfo::EDriveRemovable;
             }
         
-            
+            if ( drvStatus & DriveInfo::EDriveUsbMemory )
+                {
+                state |= FmDriverInfo::EDriveUsbMemory;
+                }
+
             if ( drvStatus & DriveInfo::EDriveRom ){
                 state |= FmDriverInfo::EDriveRom;  
             }
@@ -123,12 +146,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,10 +183,19 @@
     case KErrCorrupt:
         state |= FmDriverInfo::EDriveCorrupted;
         break;
+    case KErrInUse:
+        state |= FmDriverInfo::EDriveInUse;
+        break;
     default: // other errors
         state |= FmDriverInfo::EDriveNotPresent;
         break;
     }
+    QString logString ( "FmUtils::queryDriverInfo_" + driverName + 
+            "_volumeInfoErr:" + QString::number( volumeInfoErr ) +
+            "_driveInfoErr:" + QString::number( driveInfoErr ) +
+            "_errorCode:" + QString::number( errorCode ) + 
+            "_driveSatus:" + QString::number( state ) );
+    FmLogger::log( logString );
     return FmDriverInfo( volumeInfo.iSize, volumeInfo.iFree, driverName, volumeName, state );
 }
 
@@ -155,84 +211,12 @@
 	    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 ( 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 )
 {
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "Drive name:" + driverName;
     FmLogger::log( logString );
     logString = "Password:" + Pwd;
@@ -271,6 +255,9 @@
 
 int FmUtils::unlockDrive( const QString &driverName,  const QString &Pwd )
 {
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "Drive name:" + driverName;
     FmLogger::log( logString );
     logString = "Password:" + Pwd;
@@ -313,6 +300,9 @@
 
 int FmUtils::checkDrivePwd( const QString &driverName, const QString &pwd )
 {
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "checkDrivePwd Drive name:" + driverName;
     logString += " password:" + pwd;
     FmLogger::log( logString );
@@ -322,6 +312,9 @@
 
 int FmUtils::setDrivePwd( const QString &driverName, const QString &oldPwd, const QString &newPwd)
 {
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "setDrivePwd Drive name:" + driverName ;
     logString += " Old password:" + oldPwd;
     logString += " New password:" + newPwd;
@@ -373,7 +366,10 @@
 
 int FmUtils::renameDrive( const QString &driverName, const QString &newVolumeName)
 {
-    foreach( QChar ch, newVolumeName )
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
+    foreach( const QChar &ch, newVolumeName )
     {
         bool a = ch.isSpace();
         bool b = ch.isLetterOrNumber();
@@ -408,8 +404,11 @@
     }
 }
 
-void FmUtils::ejectDrive( const QString &driverName )
+int FmUtils::ejectDrive( const QString &driverName )
 {
+    if( driverName.isEmpty() ) {
+        return FmErrWrongParam;
+    }
     QString logString = "FmUtils::ejectDrive start";
     FmLogger::log( logString );
 
@@ -424,6 +423,7 @@
         KCoreAppUIsMmcRemovedWithoutEject,
         ECoreAppUIsEjectCommandUsedToDrive | ( drive << KDriveShift )
         );
+    return FmErrNone;
 }
 
 QString FmUtils::getFileType( const QString &filePath  )
@@ -506,6 +506,9 @@
 
 bool FmUtils::isDriveC( const QString &driverName )
 {
+    if( driverName.isEmpty() ) {
+        return false;
+    }
     TInt drive = 0;
     drive = driverName[0].toUpper().toAscii() - 'A' + EDriveA;
     if( drive == EDriveC ){
@@ -529,6 +532,9 @@
 
 void FmUtils::createDefaultFolders( const QString &driverName )
 {
+    if( driverName.isEmpty() ) {
+        return;
+    }
     int err;
     
     TInt drive = 0;
@@ -597,14 +603,23 @@
 
 QString FmUtils::fillPathWithSplash( const QString &filePath )
 {
-    QString newFilePath( filePath );
+	QString newFilePath;
     if( filePath.isEmpty() ) {
         return newFilePath;
     }
 
-    if( filePath.at( filePath.length()-1 ) != QChar( '/' ) ){
-        newFilePath.append( QChar( '/' ) );
+    foreach( QChar ch, filePath ) {
+        if( ch == QChar('\\') || ch == QChar('/') ) {
+			newFilePath.append( QDir::separator() );
+        } else {
+            newFilePath.append( ch );
+        }
     }
+    
+    if( newFilePath.right( 1 )!= QDir::separator() ){
+        newFilePath.append( QDir::separator() );
+    }
+    
     return newFilePath;
 }
 
@@ -617,10 +632,15 @@
     return newFilePath;
 }
 
-// used to filter drive which need be hide.
-bool FmUtils::checkDriveFilter( const QString &driveName )
+// filter un-accessable drive
+bool FmUtils::checkDriveAccessFilter( const QString &driveName )
 {
-    if( driveName.contains( "D:" ) || driveName.contains( "Z:" ) ) {
+    if( driveName.isEmpty() ) {
+        return false;
+    }
+    FmDriverInfo driveInfo = queryDriverInfo( driveName );
+    if( ( driveInfo.driveState()& FmDriverInfo::EDriveRam ) ||
+        ( driveInfo.driveState()& FmDriverInfo::EDriveRom ) ) {
         return false;
     }
     return true;
@@ -635,8 +655,8 @@
         }
     */
     QString checkedPath = fillPathWithSplash( path );
-    if( checkedPath.compare( QString( "C:/"), Qt::CaseInsensitive ) == 0 ) {
-        checkedPath += QString( "data/" );
+    if( checkedPath.compare( Drive_C, Qt::CaseInsensitive ) == 0 ) {
+        checkedPath += QString( "data" ) + QDir::separator();
         return checkedPath;
     }
     return path;
@@ -653,47 +673,50 @@
     logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
     FmLogger::log( logString );
     
-    if( checkedPath.compare( QString( "C:/data/"), Qt::CaseInsensitive ) == 0 ) {
+    if( checkedPath.compare( Folder_C_Data, Qt::CaseInsensitive ) == 0 ) {
         FmLogger::log( QString( " change from c:/data/ to C:/" ) );
-        return QString( "C:/" );
+        return Drive_C;
     }
     return path;
 
 }
 
-bool FmUtils::isPathAccessabel( const QString &path )
+int FmUtils::isPathAccessabel( const QString &path )
 {
+    // Used to check if path is accessable, very important feature
+	// and will return filemanager error.
     FmLogger::log( QString( "isPathAccessabel:" ) + path );
+    if( path.isEmpty() ) {
+        return FmErrPathNotExist;
+    }
     if( path.length() <= 3 && !isDriveAvailable( path ) ) { //used to filter locked drive
         FmLogger::log( QString( "isPathAccessabel false: path is drive and not available" ) );
-        return false;
+        return FmErrDriveNotAvailable;
     }
     QFileInfo fileInfo( path );
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_C ), Qt::CaseInsensitive ) &&
-        !fileInfo.absoluteFilePath().contains( QString( Folder_C_Data ), Qt::CaseInsensitive ) ) {
+    if( fileInfo.absoluteFilePath().contains( Drive_C, Qt::CaseInsensitive ) &&
+        !fileInfo.absoluteFilePath().contains( Folder_C_Data, Qt::CaseInsensitive ) ) {
         FmLogger::log( QString( "isPathAccessabel false: path contain C and not in data folder" ) );
-        return false;
+        return FmErrPathDenied;
     }
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_D ), Qt::CaseInsensitive ) ) {
-        FmLogger::log( QString( "isPathAccessabel false: path contain D" ) );
-        return false;
-    }
-    if( fileInfo.absoluteFilePath().contains( QString( Drive_Z ), Qt::CaseInsensitive ) ) {
-        FmLogger::log( QString( "isPathAccessabel false: path contain Z" ) );
-        return false;
+    if( !checkDriveAccessFilter( FmUtils::getDriveNameFromPath( fileInfo.absoluteFilePath() ) ) ){
+        return FmErrDriveDenied;
     }
     if( !fileInfo.exists() ) {
         FmLogger::log( QString( "isPathAccessabel false: path not exist" ) );
-        return false;
+        return FmErrPathNotExist;
     }
-    FmLogger::log( QString( "isPathAccessabel true" ) );
-    return true;
+    FmLogger::log( QString( "isPathAccessabel FmErrNone" ) );
+    return FmErrNone;
 }
 
 // only used to check drive, when MMC is not inserted, also return false
 bool FmUtils::isDriveAvailable( const QString &path )
 {
     FmLogger::log( QString( "isDriveAvailable:" ) + path );
+    if( path.isEmpty() ) {
+        return false;
+    }
     FmDriverInfo::DriveState driveState = queryDriverInfo( path ).driveState();
     if( ( driveState & FmDriverInfo::EDriveAvailable ) ) {
         FmLogger::log( QString( "isDriveAvailable true" ) );
@@ -714,7 +737,7 @@
 
     foreach( QFileInfo fileInfo, infoList ) {
         QString driveName = fileInfo.absolutePath();
-        if( checkDriveFilter( driveName ) ) {
+        if( checkDriveAccessFilter( driveName ) ) {
             if( !isHideUnAvailableDrive ) {
                 driveList.append( driveName );
             }
@@ -729,9 +752,12 @@
 QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
 {
     QString ret;
+    if( driveName.isEmpty() ) {
+        return ret;
+    }
     QString tempDriveName = fillPathWithSplash( driveName );
 
-    ret = removePathSplash( driveName );
+    QString checkedDriveName( removePathSplash( driveName ) );
     
     FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
     QString volumeName = driverInfo.volumeName();
@@ -741,25 +767,37 @@
         if( driveState & FmDriverInfo::EDriveAvailable ){
             if( driveState & FmDriverInfo::EDriveRemovable ) {
                 if( driveState & FmDriverInfo::EDriveMassStorage ) {
-                    volumeName.append( QObject::tr( "Mass Storage" ) );  
+                    // Mass Storage
+                    ret = hbTrId( "txt_fmgr_dblist_1_mass_storage" ).arg( checkedDriveName );  
+                } else if( driveState & FmDriverInfo::EDriveUsbMemory ) {
+                    // USB Memory
+                    ret = hbTrId( "txt_fmgr_dblist_1_usb_memory" ).arg( checkedDriveName );
+                } else{
+                    // Memory Card
+                    ret = hbTrId( "txt_fmgr_dblist_1_memory_card" ).arg( checkedDriveName );
                 }
-                else{
-                    volumeName.append( QObject::tr( "Memory Card" ) );
-                }
+            } else{
+                // phone memory
+                ret = hbTrId( "txt_fmgr_dblist_1_device_memory" ).arg( checkedDriveName );
             }
-            else{
-                volumeName.append( QObject::tr( "Phone Memory" ) );
-            }
-        }
+        }  
     }
-
-    ret += QString( " " ) + volumeName;
+    
+    if( ret.isEmpty() ) {
+        // set ret as volumeName
+        // txt_fmgr_dblist_1_2 is not correct, can not use.
+        ret = hbTrId( "%1 %2" ).arg( checkedDriveName ).arg( volumeName );
+    }
     return ret;
 }
 
-int FmUtils::launchFile( const QString &filePath )
+int FmUtils::launchFile( const QString &filePath )
 {
     QFile file( filePath );
+    if( !file.exists() ) {
+        return false;
+    }
+        
     XQApplicationManager mAiwMgr;
     XQAiwRequest *request = mAiwMgr.create(file);
     if ( request == 0 ) {
@@ -852,12 +890,68 @@
 
 QString FmUtils::formatPath( const QString &path  )
 {
-    QString formatPath = path;
-    QRegExp regExp( "/" );
-    formatPath.replace( regExp, "\\" );
-    
-    if( path.right( 1 )!= "\\"){
-        formatPath.append( "\\" );
+    QString formatPath;
+	foreach( QChar ch, path ) {
+		if( ch == QChar('\\') || ch == QChar('/') ) {
+			formatPath.append( QDir::separator() );
+		} else {
+			formatPath.append( ch );
+		}
+    }
+
+    if( formatPath.right( 1 ) != QDir::separator() ){
+        formatPath.append( QDir::separator() );
     }
     return formatPath;
 }
+
+int FmUtils::getMaxFileNameLength()
+{
+    return KMaxFileName;
+}
+
+bool FmUtils::checkMaxPathLength( const QString& path )
+{
+    if( path.length() > KMaxPath ) {
+        return false;
+    }
+    return true;
+}
+bool FmUtils::checkFolderFileName( const QString& name )
+{
+    if( name.endsWith( QChar('.'),  Qt::CaseInsensitive ) ) {
+        return false;
+    }
+    if( name.contains( QChar('\\'), Qt::CaseInsensitive ) ||
+        name.contains( QChar('/'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar(':'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar('*'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar('?'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar('\"'), Qt::CaseInsensitive ) ||
+        name.contains( QChar('<'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar('>'),  Qt::CaseInsensitive ) ||
+        name.contains( QChar('|'),  Qt::CaseInsensitive ) ){
+        return false;
+    }
+    if( name.length() > KMaxFileName ) {
+        return false;
+    }
+    return true;
+}
+
+bool FmUtils::checkNewFolderOrFile( const QString &path, QString &errString )
+{
+    QFileInfo fileInfo( path );
+    bool ret( true );   
+    if (!FmUtils::checkFolderFileName( fileInfo.fileName() ) ) {
+        errString = hbTrId( "Invalid file or folder name!" );
+        ret = false;
+    } else if( !FmUtils::checkMaxPathLength( path ) ) {
+        errString = hbTrId( "the path you specified is too long!" );
+        ret = false;
+    } else if (fileInfo.exists()) {
+        errString = hbTrId( "%1 already exist!" ).arg( fileInfo.fileName() );
+        ret = false;
+    }
+    return ret;
+}