filemanager/src/inc/fmutils_s60.cpp
branchRCL_3
changeset 38 491b3ed49290
equal deleted inserted replaced
36:95243422089a 38:491b3ed49290
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *     Zhiqiang Yang <zhiqiang.yang@nokia.com>
       
    14 * 
       
    15 * Description:
       
    16 *     The source file of the file manager utilities on Symbian
       
    17 */
       
    18 
       
    19 
       
    20 #include "fmutils.h"
       
    21 #include "fmcommon.h"
       
    22 #include "fms60utils.h"
       
    23 
       
    24 #include <apgcli.h>
       
    25 #include <sysutil.h>
       
    26 #include <f32file.h>
       
    27 #include <coemain.h>
       
    28 #include <pathinfo.h>
       
    29 #include <driveinfo.h>
       
    30 #include <e32property.h>
       
    31 #include <coreapplicationuisdomainpskeys.h>
       
    32 
       
    33 #include <QDir>
       
    34 #include <QFile>
       
    35 #include <QRegExp>
       
    36 #include <QStringList>
       
    37 #include <QFileInfoList>
       
    38 
       
    39 #include <xqappmgr.h>
       
    40 #include <XQConversions>
       
    41 #include <xqaiwrequest.h>
       
    42 
       
    43 #include <shareui.h>
       
    44 #include <hbglobal.h>
       
    45 #include <hbdirectorynamelocalizer.h>
       
    46 
       
    47 #define BURCONFIGFILE  "z:/private/2002BCC0/burconfig.xml"
       
    48 
       
    49 /*!
       
    50     query drive info and status for \a driveName
       
    51     return \a FmDriverInfo
       
    52 */
       
    53 FmDriverInfo FmUtils::queryDriverInfo( const QString &driveName )
       
    54 {
       
    55     if( driveName.isEmpty() ) {
       
    56         return FmDriverInfo( 0, 0, driveName, QString(), FmDriverInfo::EDriveNotPresent );
       
    57     }
       
    58     CCoeEnv *env = CCoeEnv::Static();
       
    59     RFs& fs = env->FsSession();
       
    60 
       
    61     TVolumeInfo volumeInfo;
       
    62     TInt drive = 0;
       
    63     drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
    64 
       
    65     quint32 state( 0 );
       
    66     
       
    67     int volumeInfoErr( KErrNone );
       
    68     int driveInfoErr( KErrNone );
       
    69     int errorCode( KErrNone );
       
    70     volumeInfoErr = fs.Volume( volumeInfo, drive );
       
    71     errorCode = volumeInfoErr;
       
    72     QString volumeName( (QChar*)( volumeInfo.iName.Des().Ptr() ), volumeInfo.iName.Length() );
       
    73     
       
    74     TDriveInfo driveInfo;
       
    75     if( volumeInfoErr == KErrNone ) {
       
    76         driveInfo = volumeInfo.iDrive;
       
    77     } else {
       
    78         driveInfoErr = fs.Drive( driveInfo, drive );
       
    79         if( driveInfoErr != KErrNone ) {
       
    80             errorCode = driveInfoErr;
       
    81         }
       
    82     }
       
    83     
       
    84     if( volumeInfoErr == KErrNone || driveInfoErr == KErrNone ) {
       
    85         //TDriveInfo driveInfo = volumeInfo.iDrive;
       
    86     
       
    87         quint32 drvStatus( 0 );
       
    88         int err = DriveInfo::GetDriveStatus( fs, drive, drvStatus );
       
    89         if( err == KErrNone ) {
       
    90             
       
    91             if ( ( drvStatus & DriveInfo::EDriveInternal ) &&
       
    92                  ( drvStatus & DriveInfo::EDriveExternallyMountable ) ){
       
    93                 // Handle mass storage bits here
       
    94                 state |= FmDriverInfo::EDriveMassStorage | FmDriverInfo::EDriveRemovable;
       
    95             }
       
    96         
       
    97             if ( drvStatus & DriveInfo::EDriveUsbMemory )
       
    98                 {
       
    99                 state |= FmDriverInfo::EDriveUsbMemory;
       
   100                 }
       
   101             
       
   102             if ( drvStatus & DriveInfo::EDriveRemote )
       
   103                 {
       
   104                 state |= FmDriverInfo::EDriveRemote;
       
   105                 }
       
   106             
       
   107             if ( drvStatus & DriveInfo::EDriveRom ){
       
   108                 state |= FmDriverInfo::EDriveRom;  
       
   109             }
       
   110             
       
   111             if ( drvStatus & DriveInfo::EDriveRam ){
       
   112                 state |= FmDriverInfo::EDriveRam;  
       
   113             }
       
   114         
       
   115             if ( driveInfo.iMediaAtt & KMediaAttFormattable ){
       
   116                 state |= FmDriverInfo::EDriveFormattable;
       
   117             }
       
   118             if ( driveInfo.iMediaAtt & KMediaAttWriteProtected ){
       
   119                 state |= FmDriverInfo::EDriveWriteProtected;
       
   120             }
       
   121             if ( driveInfo.iMediaAtt & KMediaAttHasPassword ){
       
   122                 state |= FmDriverInfo::EDrivePasswordProtected;
       
   123             }    
       
   124             if ( driveInfo.iMediaAtt & KMediaAttLocked ){
       
   125                 state |= FmDriverInfo::EDriveLocked;
       
   126             }
       
   127         
       
   128             if ( driveInfo.iDriveAtt & KDriveAttRemovable ){
       
   129                 state |= FmDriverInfo::EDriveRemovable;
       
   130         
       
   131                 if ( drvStatus & DriveInfo::EDriveSwEjectable ){
       
   132                     state |= FmDriverInfo::EDriveEjectable;
       
   133                 }
       
   134             }
       
   135             
       
   136             if( driveInfo.iType == EMediaNotPresent ){
       
   137                 state |= FmDriverInfo::EDriveNotPresent;    
       
   138             }
       
   139         }
       
   140         // If memory card is not ready but type is present,
       
   141         // then check if it is reserved.
       
   142         if( err == KErrNone && volumeInfoErr == KErrNotReady &&
       
   143             driveInfo.iType != EMediaNotPresent )
       
   144             {
       
   145             // Check USB file transfer state
       
   146             TInt prop( ECoreAppUIsUSBFileTransferUninitialized );
       
   147             RProperty::Get(
       
   148                 KPSUidCoreApplicationUIs,
       
   149                 KCoreAppUIsUSBFileTransfer, prop );
       
   150             if ( prop == ECoreAppUIsUSBFileTransferActive )
       
   151                 {
       
   152                 errorCode = KErrInUse; // Reserved for file transfer
       
   153                 }
       
   154             }
       
   155         if( err!= KErrNone )
       
   156             {
       
   157             errorCode = err;
       
   158             }
       
   159     }
       
   160 
       
   161     // handle error code
       
   162     // volumeInfoErr will occur while drive is lock,corrupted...
       
   163     // driveInfoErr can not be promoted for locked, corrupted drive.
       
   164     // so we can not use driveInfoErr to justify EDriveAvailable
       
   165     switch( errorCode )
       
   166     {
       
   167     case KErrNone:
       
   168         // this drive could be used as it is not be locked, or corrupt.
       
   169         state |= FmDriverInfo::EDriveAvailable; 
       
   170         break;
       
   171     case KErrLocked:
       
   172         state |= FmDriverInfo::EDriveLocked;
       
   173         break;
       
   174     case KErrCorrupt:
       
   175         state |= FmDriverInfo::EDriveCorrupted;
       
   176         break;
       
   177     case KErrInUse:
       
   178         state |= FmDriverInfo::EDriveInUse;
       
   179         break;
       
   180     default: // other errors
       
   181         state |= FmDriverInfo::EDriveNotPresent;
       
   182         break;
       
   183     }
       
   184     QString logString ( "FmUtils::queryDriverInfo_" + driveName + 
       
   185             "_volumeInfoErr:" + QString::number( volumeInfoErr ) +
       
   186             "_driveInfoErr:" + QString::number( driveInfoErr ) +
       
   187             "_errorCode:" + QString::number( errorCode ) + 
       
   188             "_driveSatus:" + QString::number( state ) );
       
   189     FM_LOG( logString );
       
   190     return FmDriverInfo( volumeInfo.iSize, volumeInfo.iFree, driveName, volumeName, state );
       
   191 }
       
   192 
       
   193 /*!
       
   194     remove drive password for \a driveName
       
   195     \a Pwd is original password.
       
   196 */
       
   197 int FmUtils::removeDrivePwd( const QString &driveName,  const QString &Pwd )
       
   198 {
       
   199     if( driveName.isEmpty() || Pwd.length() > FmMaxLengthofDrivePassword ) {
       
   200         return FmErrWrongParam;
       
   201     }
       
   202     QString logString = "Drive name:" + driveName;
       
   203     FM_LOG( logString );
       
   204     logString = "Password:" + Pwd;
       
   205     FM_LOG( logString );
       
   206 
       
   207     CCoeEnv *env = CCoeEnv::Static();
       
   208 	RFs& fs = env->FsSession();
       
   209 
       
   210     TInt drive = 0;
       
   211 
       
   212     drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   213 
       
   214     HBufC* password16 = XQConversions::qStringToS60Desc( Pwd );
       
   215     TMediaPassword password;   
       
   216     TPtr ptrPassword16( password16->Des() );  
       
   217     FmS60Utils::ConvertCharsToPwd( ptrPassword16, password );
       
   218 
       
   219     int err( fs.ClearPassword( drive, password ) );
       
   220 
       
   221     logString = "Drive:" + QString::number( drive );
       
   222     FM_LOG( logString );
       
   223 
       
   224     logString = "Clear password error:" + QString::number( err );
       
   225     FM_LOG( logString );
       
   226 
       
   227     delete password16;
       
   228 
       
   229     if( err == KErrNone ){
       
   230         return FmErrNone;   
       
   231     }
       
   232     else if( err == KErrAccessDenied ){
       
   233         return FmErrAccessDenied;
       
   234     }
       
   235     else{
       
   236         return FmErrUnKnown;
       
   237     }
       
   238 }
       
   239 
       
   240 /*!
       
   241     Unlock drive \a driveName with provided password \a Pwd
       
   242 */
       
   243 int FmUtils::unlockDrive( const QString &driveName,  const QString &Pwd )
       
   244 {
       
   245     if( driveName.isEmpty() || Pwd.length() > FmMaxLengthofDrivePassword ) {
       
   246         return FmErrWrongParam;
       
   247     }
       
   248     QString logString = "Drive name:" + driveName;
       
   249     FM_LOG( logString );
       
   250     logString = "Password:" + Pwd;
       
   251     FM_LOG( logString );
       
   252 
       
   253     CCoeEnv *env = CCoeEnv::Static();
       
   254 	RFs& fs = env->FsSession();
       
   255 
       
   256     TInt drive = 0;
       
   257 	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   258     
       
   259     HBufC* password16 = XQConversions::qStringToS60Desc( Pwd );
       
   260     TMediaPassword password;   
       
   261     TPtr ptrPassword16( password16->Des() );  
       
   262     FmS60Utils::ConvertCharsToPwd( ptrPassword16, password );
       
   263 
       
   264     int err( fs.UnlockDrive( drive, password, ETrue) );
       
   265 
       
   266     logString = "Drive:" + QString::number( drive );
       
   267     FM_LOG( logString );
       
   268     logString = "Unlock drive error:" + QString::number( err );
       
   269     FM_LOG( logString );
       
   270 
       
   271     delete password16;
       
   272 
       
   273     if( err == KErrNone ){
       
   274         return FmErrNone;   
       
   275     }
       
   276     else if( err == KErrAccessDenied ){
       
   277         return FmErrAccessDenied;
       
   278     }
       
   279     else if( err == KErrAlreadyExists ){
       
   280         return FmErrAlreadyExists;
       
   281     }
       
   282     else if( err == KErrNotSupported ){
       
   283         return FmErrNotSupported;
       
   284     }
       
   285     else{
       
   286         return FmErrUnKnown;
       
   287     }
       
   288 }
       
   289 
       
   290 /*!
       
   291     Check if \a pwd is the right password for drive \a driveName
       
   292 */
       
   293 int FmUtils::checkDrivePwd( const QString &driveName, const QString &pwd )
       
   294 {
       
   295     if( driveName.isEmpty() || pwd.length() > FmMaxLengthofDrivePassword ) {
       
   296         return FmErrWrongParam;
       
   297     }
       
   298     QString logString = "checkDrivePwd Drive name:" + driveName;
       
   299     logString += " password:" + pwd;
       
   300     FM_LOG( logString );
       
   301 
       
   302     return setDrivePwd( driveName, pwd, pwd );
       
   303 }
       
   304 
       
   305 /*!
       
   306     Set new password \a newPwd for drive \a driveName. \a oldPwd is old password
       
   307 */
       
   308 int FmUtils::setDrivePwd( const QString &driveName, const QString &oldPwd, const QString &newPwd)
       
   309 {
       
   310     if( driveName.isEmpty() || 
       
   311         oldPwd.length() > FmMaxLengthofDrivePassword || newPwd.length() > FmMaxLengthofDrivePassword  ) {
       
   312         return FmErrWrongParam;
       
   313     }
       
   314     QString logString = "setDrivePwd Drive name:" + driveName ;
       
   315     logString += " Old password:" + oldPwd;
       
   316     logString += " New password:" + newPwd;
       
   317     FM_LOG( logString );
       
   318 
       
   319     CCoeEnv *env = CCoeEnv::Static();
       
   320 	RFs& fs = env->FsSession();
       
   321 
       
   322     TInt drive = 0;
       
   323 	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   324 	
       
   325     HBufC* newPassword16 = XQConversions::qStringToS60Desc( newPwd);
       
   326     HBufC* oldPassword16 = XQConversions::qStringToS60Desc( oldPwd );
       
   327 
       
   328     TMediaPassword oldPassword;
       
   329     TMediaPassword newPassword;
       
   330     
       
   331     TPtr ptrNewPassword16( newPassword16->Des() );
       
   332     TPtr ptrOldPassword16( oldPassword16->Des() );
       
   333     
       
   334     FmS60Utils::ConvertCharsToPwd( ptrNewPassword16, newPassword );
       
   335     FmS60Utils::ConvertCharsToPwd( ptrOldPassword16, oldPassword );
       
   336 
       
   337     int err( fs.LockDrive( drive, oldPassword, newPassword, ETrue ) );
       
   338 
       
   339     logString = "Drive:" + QString::number( drive );
       
   340     FM_LOG( logString );
       
   341     logString = "Password set error:" + QString::number( err );
       
   342     FM_LOG( logString );
       
   343 
       
   344     delete newPassword16;
       
   345     delete oldPassword16;
       
   346     if( err == KErrNone ){
       
   347         return FmErrNone;   
       
   348     }
       
   349     else if( err == KErrNotSupported ){
       
   350         return FmErrNotSupported;
       
   351     }
       
   352     else{
       
   353         return FmErrUnKnown;
       
   354     }
       
   355 }
       
   356 
       
   357 /*!
       
   358     Set \a pwd as empty password
       
   359 */
       
   360 void FmUtils::emptyPwd( QString &pwd )
       
   361 {
       
   362     TBuf< FmMaxLengthofDrivePassword > nullPwd;
       
   363     nullPwd.FillZ( nullPwd.MaxLength() );
       
   364     nullPwd.Zero();
       
   365     pwd = XQConversions::s60DescToQString( nullPwd );
       
   366 }
       
   367 
       
   368 /*!
       
   369     Set drive volume for drive \a driveName
       
   370 */
       
   371 int FmUtils::renameDrive( const QString &driveName, const QString &newVolumeName)
       
   372 {
       
   373     if( driveName.isEmpty() ) {
       
   374         return FmErrWrongParam;
       
   375     }
       
   376     foreach( const QChar &ch, newVolumeName )
       
   377     {
       
   378         bool a = ch.isSpace();
       
   379         bool b = ch.isLetterOrNumber();
       
   380         // If not alphadigit or space, return error
       
   381         if( !ch.isLetterOrNumber() && !ch.isSpace() )
       
   382         {
       
   383             return FmErrBadName;
       
   384         }   
       
   385     }
       
   386         
       
   387     CCoeEnv *env = CCoeEnv::Static();
       
   388 	RFs& fs = env->FsSession();
       
   389 
       
   390     TInt drive = 0;
       
   391 	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   392 
       
   393     TPtr newName ( ( XQConversions::qStringToS60Desc( newVolumeName ) )->Des() );
       
   394 
       
   395     int err( fs.SetVolumeLabel( newName, drive ));
       
   396     
       
   397     QString logString = "Rename error:" + QString::number( err );
       
   398     FM_LOG( logString );
       
   399 
       
   400     if( err == KErrNone ){
       
   401         return FmErrNone;   
       
   402     }
       
   403     else if( err == KErrNotReady ){
       
   404         return FmErrNotReady;
       
   405     }
       
   406     else{
       
   407         return FmErrUnKnown;
       
   408     }
       
   409 }
       
   410 
       
   411 /*!
       
   412     Eject drive \a driveName
       
   413 */
       
   414 int FmUtils::ejectDrive( const QString &driveName )
       
   415 {
       
   416     if( driveName.isEmpty() ) {
       
   417         return FmErrWrongParam;
       
   418     }
       
   419     QString logString = "FmUtils::ejectDrive start";
       
   420     FM_LOG( logString );
       
   421 
       
   422     TInt drive = 0;
       
   423 	drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   424 
       
   425     const int KDriveShift = 16;
       
   426 
       
   427     // Let SysAp handle eject
       
   428     RProperty::Set(
       
   429         KPSUidCoreApplicationUIs,
       
   430         KCoreAppUIsMmcRemovedWithoutEject,
       
   431         ECoreAppUIsEjectCommandUsedToDrive | ( drive << KDriveShift )
       
   432         );
       
   433     return FmErrNone;
       
   434 }
       
   435 
       
   436 /*!
       
   437     Check if drive \a driveName is accessable for user
       
   438 */
       
   439 bool FmUtils::checkDriveAccessFilter( const QString &driveName )
       
   440 {
       
   441     if( driveName.isEmpty() ) {
       
   442         return false;
       
   443     }
       
   444     FmDriverInfo driveInfo = queryDriverInfo( driveName );
       
   445     if( ( driveInfo.driveState()& FmDriverInfo::EDriveRam ) ||
       
   446         ( driveInfo.driveState()& FmDriverInfo::EDriveRom ) ) {
       
   447         return false;
       
   448     }
       
   449     return true;
       
   450 }
       
   451 
       
   452 /*!
       
   453     This function should be called to adjust path if user goto a drive.
       
   454     data folder will be append to C:\ becuase user could only view C:\data instead C:\ 
       
   455 */
       
   456 QString FmUtils::checkDriveToFolderFilter( const QString &path )
       
   457 {
       
   458     QString checkedPath = fillPathWithSplash( path );
       
   459     if( checkedPath.compare( Drive_C, Qt::CaseInsensitive ) == 0 ) {
       
   460         checkedPath += QString( "data" ) + QDir::separator();
       
   461         return checkedPath;
       
   462     }
       
   463     return path;
       
   464 
       
   465 }
       
   466 
       
   467 /*!
       
   468     This function should be called to adjust path if user back to up level path.
       
   469     If user is at C:\data then path level should be returned as C:\
       
   470     Becuase C:\data is root path for C drive 
       
   471 */
       
   472 QString FmUtils::checkFolderToDriveFilter( const QString &path )
       
   473 {
       
   474     QString logString;
       
   475     logString = QString( "checkFolderToDriveFilter: " ) + path;
       
   476     FM_LOG( logString );
       
   477     QString checkedPath = fillPathWithSplash( path );
       
   478 
       
   479     logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
       
   480     FM_LOG( logString );
       
   481     
       
   482     if( checkedPath.compare( Folder_C_Data, Qt::CaseInsensitive ) == 0 ) {
       
   483         FM_LOG( QString( " change from c:/data/ to C:/" ) );
       
   484         return Drive_C;
       
   485     }
       
   486     return path;
       
   487 
       
   488 }
       
   489 
       
   490 /*!
       
   491     Check if \a path is accessable for user
       
   492 */
       
   493 int FmUtils::isPathAccessabel( const QString &path )
       
   494 {
       
   495     // Used to check if path is accessable, very important feature
       
   496     // and will return filemanager error.
       
   497     FM_LOG( QString( "isPathAccessabel:" ) + path );
       
   498     if( path.isEmpty() ) {
       
   499         return FmErrPathNotExist;
       
   500     }
       
   501 
       
   502     // used to filter locked/ejected/corrupted drive
       
   503     // check if drive is available, no matter if it is a drive, a folder, or a file.
       
   504     if( !isDriveAvailable( path ) ) {
       
   505         FM_LOG( QString( "isPathAccessabel false: path is drive and not available" ) );
       
   506         return FmErrDriveNotAvailable;
       
   507     }
       
   508 
       
   509     QFileInfo fileInfo( path );
       
   510 
       
   511     if( fileInfo.absoluteFilePath().contains( Drive_C, Qt::CaseInsensitive ) &&
       
   512         !fileInfo.absoluteFilePath().contains( Folder_C_Data, Qt::CaseInsensitive ) ) {
       
   513         FM_LOG( QString( "isPathAccessabel false: path contain C and not in data folder" ) );
       
   514         return FmErrPathDenied;
       
   515     }
       
   516     if( !checkDriveAccessFilter( FmUtils::getDriveNameFromPath( fileInfo.absoluteFilePath() ) ) ){
       
   517         return FmErrDriveDenied;
       
   518     }
       
   519     if( !fileInfo.exists() ) {
       
   520         FM_LOG( QString( "isPathAccessabel false: path not exist" ) );
       
   521         return FmErrPathNotExist;
       
   522     }
       
   523     FM_LOG( QString( "isPathAccessabel FmErrNone" ) );
       
   524     return FmErrNone;
       
   525 }
       
   526 
       
   527 /*!
       
   528     Check if drive related to \a path is available.
       
   529     This function should not check if path is available. Only responsible for checking drive  
       
   530     When MMC is not inserted, also return false
       
   531 */
       
   532 bool FmUtils::isDriveAvailable( const QString &path )
       
   533 {
       
   534     FM_LOG( QString( "isDriveAvailable:" ) + path );
       
   535     if( path.isEmpty() ) {
       
   536         return false;
       
   537     }
       
   538     FmDriverInfo::DriveState driveState = queryDriverInfo( path ).driveState();
       
   539     if( ( driveState & FmDriverInfo::EDriveAvailable ) ) {
       
   540         FM_LOG( QString( "isDriveAvailable true" ) );
       
   541         return true;
       
   542     }
       
   543     FM_LOG( QString( "isDriveAvailable false" ) );
       
   544     return false;
       
   545 }
       
   546 
       
   547 /*!
       
   548     Check if \a folderPath is default folder for system
       
   549 */
       
   550 bool FmUtils::isDefaultFolder( const QString &folderPath  )
       
   551 {
       
   552     HBufC *path = XQConversions::qStringToS60Desc( folderPath );
       
   553     TPtrC desFolderPath( path->Des() );
       
   554     
       
   555     bool ret( true );
       
   556     TInt pathType( PathInfo::PathType( desFolderPath ) );
       
   557     switch( pathType ){
       
   558        case PathInfo::ENotSystemPath:{
       
   559            QString locString( localize( folderPath ) );
       
   560             if ( locString.isEmpty() ){
       
   561                 ret = false;
       
   562                 break;
       
   563             }
       
   564             ret = true;
       
   565             break;
       
   566             }
       
   567         case PathInfo::EPhoneMemoryRootPath: // FALL THROUGH
       
   568         case PathInfo::EMemoryCardRootPath: // FALL THROUGH
       
   569         case PathInfo::ERomRootPath:{
       
   570             ret = false;
       
   571             break;
       
   572         }
       
   573         // Accept other folders
       
   574         default:{
       
   575             ret = true;
       
   576             break;
       
   577         }
       
   578     }
       
   579     delete path;
       
   580     return ret;
       
   581 }
       
   582 
       
   583 /*!
       
   584     Create system default folders for drive \a driveName.
       
   585     Default folders should be created after format a drive.
       
   586 */
       
   587 void FmUtils::createDefaultFolders( const QString &driveName )
       
   588 {
       
   589     if( driveName.isEmpty() ) {
       
   590         return;
       
   591     }
       
   592     int err;
       
   593     
       
   594     TInt drive = 0;
       
   595     drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   596     
       
   597     RFs fs;
       
   598     err = fs.Connect();
       
   599     
       
   600     if( err != KErrNone ){
       
   601         return;
       
   602     }
       
   603     
       
   604     quint32 drvStatus( 0 );
       
   605     err = DriveInfo::GetDriveStatus( fs, drive, drvStatus );
       
   606     if ( !( drvStatus & DriveInfo::EDriveUserVisible ) ||
       
   607         ( drvStatus & ( DriveInfo::EDriveRemote |
       
   608                         DriveInfo::EDriveReadOnly |
       
   609                         DriveInfo::EDriveUsbMemory ) ) ){
       
   610             return; 
       
   611         }
       
   612     
       
   613     TEntry entry;
       
   614     CDesCArray* array = PathInfo::GetListOfPathsLC( drive );
       
   615     
       
   616     TInt count( array->MdcaCount() );
       
   617     for ( TInt i( 0 ); i < count; ++i )
       
   618         {
       
   619         TPtrC fullPath( array->MdcaPoint( i ) );
       
   620         TBool allow( ETrue );
       
   621 
       
   622         if ( drvStatus & DriveInfo::EDriveRemovable )
       
   623             {
       
   624             // Filter few folder types from physically removable memory cards
       
   625             TInt pathType( PathInfo::PathType( fullPath ) );
       
   626             switch( pathType )
       
   627                 {
       
   628                 case PathInfo::EGamesPath: // FALL THROUGH
       
   629                 case PathInfo::EInstallsPath: // FALL THROUGH
       
   630                 case PathInfo::EGsmPicturesPath: // FALL THROUGH
       
   631                 case PathInfo::EMmsBackgroundImagesPath: // FALL THROUGH
       
   632                 case PathInfo::EPresenceLogosPath:
       
   633                     {
       
   634                     allow = EFalse;
       
   635                     }
       
   636                 default:
       
   637                     {
       
   638                     break;
       
   639                     }
       
   640                 }
       
   641             }
       
   642 
       
   643         if ( allow ){
       
   644             fs.MkDirAll( fullPath ); // Ignore error
       
   645 
       
   646             if ( fs.Entry( fullPath, entry ) == KErrNone ){
       
   647                 if( entry.IsHidden() ){
       
   648                 // If setting fails, File Manager can still go on
       
   649                     fs.SetEntry(
       
   650                          fullPath, entry.iModified, 0, KEntryAttHidden );
       
   651                 }
       
   652             }
       
   653         }
       
   654     }
       
   655     CleanupStack::PopAndDestroy( array );
       
   656 }
       
   657 
       
   658 /*!
       
   659     In Symbian system, default folders will be localized.
       
   660     So localize is used to check if a path is a default folder
       
   661     \sa isDefaultFolder
       
   662 */
       
   663 QString FmUtils::localize( const QString &path )
       
   664 {
       
   665     // HbDirectoryNameLocalizer can not recognize path with \ in the end
       
   666     QString locPath( removePathSplash( path ) );
       
   667     
       
   668     HbDirectoryNameLocalizer localizer;
       
   669     return localizer.translate( locPath );
       
   670 }
       
   671 
       
   672 /*!
       
   673     set the \a desFile attributes as the same with \a srcFile
       
   674 */
       
   675 int FmUtils::setFileAttributes( const QString &srcFile, const QString &desFile )
       
   676 {
       
   677     RFs fsSession;
       
   678     User::LeaveIfError( fsSession.Connect() ); 
       
   679     CleanupClosePushL( fsSession );
       
   680     RFile64 src;
       
   681     RFile64 des;
       
   682     HBufC *buf1 = XQConversions::qStringToS60Desc( removePathSplash( formatPath( srcFile ) ) );
       
   683     HBufC *buf2 = XQConversions::qStringToS60Desc( removePathSplash( formatPath( desFile ) ) );
       
   684     User::LeaveIfError( src.Open( fsSession, *buf1, EFileRead | EFileShareReadersOnly ) );
       
   685     User::LeaveIfError( des.Open( fsSession, *buf2, EFileWrite | EFileShareExclusive ) );
       
   686     TTime mod;
       
   687     int err = src.Modified( mod );;
       
   688     if ( err == FmErrNone ) {
       
   689         err = des.SetModified( mod );    
       
   690     }
       
   691     TUint att( 0 );
       
   692     if ( err == FmErrNone ) {
       
   693         err = src.Att( att );        
       
   694     }
       
   695     if ( err == FmErrNone ) {
       
   696         des.SetAtt( att, ( ~att ) & KEntryAttMaskSupported );
       
   697     }    
       
   698     src.Close();
       
   699     des.Close();
       
   700     fsSession.Close();
       
   701     CleanupStack::PopAndDestroy(); // fsSession
       
   702     return err;
       
   703 }
       
   704 
       
   705 /*!
       
   706     judge whether there is enough space on \a targetDrive for \a size.
       
   707     return true if has, false if not.
       
   708 */
       
   709 bool FmUtils::hasEnoughSpace( const QString &targetDrive, qint64 size )
       
   710 {
       
   711     RFs fsSession;
       
   712     QT_TRAP_THROWING( fsSession.Connect() ); 
       
   713     CleanupClosePushL( fsSession );
       
   714     TInt dstDrv(0);
       
   715     HBufC* hbuf = XQConversions::qStringToS60Desc( targetDrive );
       
   716     QT_TRAP_THROWING( RFs::CharToDrive( hbuf->operator [](0), dstDrv ) );
       
   717     bool ret = !SysUtil::DiskSpaceBelowCriticalLevelL( &fsSession,  size , dstDrv );
       
   718     CleanupStack::PopAndDestroy(); // fsSession
       
   719     return ret;
       
   720     
       
   721 }
       
   722 
       
   723 /*!
       
   724     move one file insice the same drive, from \a source to \a target.
       
   725     return KErrNone if successful, otherwise one of the other system-wide error codes.
       
   726 */
       
   727 int FmUtils::moveInsideDrive( const QString &source, const QString &target )
       
   728 {
       
   729     RFs fsSession;
       
   730     QT_TRAP_THROWING( fsSession.Connect() ); 
       
   731     CleanupClosePushL( fsSession );
       
   732     HBufC* oldName = XQConversions::qStringToS60Desc( source );
       
   733     HBufC* newName = XQConversions::qStringToS60Desc( target );
       
   734     int ret = fsSession.Rename( *oldName, *newName );
       
   735     CleanupStack::PopAndDestroy(); // fsSession
       
   736     return ret;    
       
   737 }
       
   738 
       
   739 /*!
       
   740    Launch a file with associated application.
       
   741 */
       
   742 int FmUtils::launchFile( const QString &filePath )
       
   743 
       
   744 {
       
   745     QFile file( filePath );
       
   746     if( !file.exists() ) {
       
   747         return false;
       
   748     }
       
   749         
       
   750     XQApplicationManager mAiwMgr;
       
   751     XQAiwRequest *request = mAiwMgr.create(file);
       
   752     if ( request == 0 ) {
       
   753         // No handlers for the URI
       
   754         return FmErrUnKnown;
       
   755     }
       
   756     
       
   757     // Set function parameters
       
   758     QList<QVariant> args;
       
   759     args << file.fileName();
       
   760     request->setArguments(args);
       
   761     
       
   762     // Send the request
       
   763     bool res = request->send();
       
   764     if  (!res) 
       
   765     {
       
   766        // Request failed. 
       
   767       int error = request->lastError();
       
   768       
       
   769       delete request;
       
   770       return FmErrUnKnown;
       
   771     }
       
   772     
       
   773     delete request;
       
   774     return FmErrNone;
       
   775 }
       
   776 
       
   777 /*!
       
   778     Send files( \a filePathList ) with share ui.
       
   779 */
       
   780 void FmUtils::sendFiles( QStringList &filePathList )
       
   781 {
       
   782     ShareUi shareui;
       
   783     shareui.send( filePathList, false );
       
   784 }
       
   785 
       
   786 /*!
       
   787     return path for backup restore config file.
       
   788     Currently \a appPath is not used.
       
   789 */
       
   790 QString FmUtils::getBurConfigPath( QString appPath )
       
   791 {
       
   792     Q_UNUSED( appPath );
       
   793     QString path( BURCONFIGFILE );
       
   794     return path;
       
   795 }
       
   796 
       
   797 /*!
       
   798     return MetaData string for \a filePath
       
   799 */
       
   800 QString FmUtils::getFileType( const QString &filePath  )
       
   801 {
       
   802     RApaLsSession apaSession;
       
   803     TDataType dataType;
       
   804     TUid appUid;
       
   805     
       
   806     TBuf<128> mimeTypeBuf;
       
   807         
       
   808     int err = apaSession.Connect();
       
   809     
       
   810     if ( err == KErrNone ){   
       
   811         err = apaSession.AppForDocument( XQConversions::qStringToS60Desc( filePath )->Des(), 
       
   812                                          appUid, dataType );
       
   813         
       
   814         if( err == KErrNone ){
       
   815             mimeTypeBuf.Copy(dataType.Des8());
       
   816         }  
       
   817     }
       
   818     
       
   819     apaSession.Close();
       
   820     return XQConversions::s60DescToQString( mimeTypeBuf );
       
   821 }
       
   822 
       
   823 /*!
       
   824     Check if drive \a driveName is drive C
       
   825 */
       
   826 bool FmUtils::isDriveC( const QString &driveName )
       
   827 {
       
   828     if( driveName.isEmpty() ) {
       
   829         return false;
       
   830     }
       
   831     TInt drive = 0;
       
   832     drive = driveName[0].toUpper().toAscii() - 'A' + EDriveA;
       
   833     if( drive == EDriveC ){
       
   834         return true;
       
   835     }
       
   836     else{
       
   837         return false;
       
   838     }
       
   839    
       
   840 }
       
   841 
       
   842 /*!
       
   843     return max file name length
       
   844 */
       
   845 int FmUtils::getMaxFileNameLength()
       
   846 {
       
   847     return KMaxFileName;
       
   848 }
       
   849 
       
   850 /*!
       
   851     Check if length of \a path is exceed max path length. 
       
   852 */
       
   853 bool FmUtils::checkMaxPathLength( const QString& path )
       
   854 {
       
   855     if( path.length() > KMaxPath ) {
       
   856         return false;
       
   857     }
       
   858     return true;
       
   859 }