filemanager/src/inc/fmutils_win.cpp
changeset 37 15bc28c9dd51
parent 16 ada7962b4308
child 46 d58987eac7e8
equal deleted inserted replaced
16:ada7962b4308 37:15bc28c9dd51
    14 * 
    14 * 
    15 * Description:
    15 * Description:
    16 *     The source file of the file manager utilities on windows
    16 *     The source file of the file manager utilities on windows
    17 */
    17 */
    18 
    18 
       
    19 #include "fmutils.h"
       
    20 
    19 #include <windows.h>
    21 #include <windows.h>
    20 
    22 
    21 #include "fmutils.h"
       
    22 #include <QDir>
    23 #include <QDir>
       
    24 #include <QUrl>
       
    25 #include <QVariant>
    23 #include <QFileInfo>
    26 #include <QFileInfo>
    24 #include <QDesktopServices>
    27 #include <QDesktopServices>
    25 #include <QUrl>
    28 
    26 #include <QVariant>
    29 #include <hbglobal.h>
    27 
    30 
    28 #define BURCONFIGFILE  "burconfig.xml"
    31 #define BURCONFIGFILE  "burconfig.xml"
    29 
    32 const int KMaxFileName=0x100;
    30 QString FmUtils::getDriveNameFromPath( const QString &path )
    33 const int KMaxPath=0x100;
    31 {
    34 
    32     if( path.length() <3 ) {
    35 FmDriverInfo FmUtils::queryDriverInfo( const QString &driveName )
    33         return QString();
       
    34     }
       
    35     return path.left( 3 );
       
    36 }
       
    37 
       
    38 QString FmUtils::getDriveLetterFromPath( const QString &path )
       
    39 {
       
    40 	if( path.length() <3 ) {
       
    41         return QString();
       
    42     }
       
    43     return path.left( 1 );
       
    44 }
       
    45 
       
    46 FmDriverInfo FmUtils::queryDriverInfo( const QString &driverName )
       
    47 {
    36 {
    48     quint64 size = 0;
    37     quint64 size = 0;
    49     quint64 freeSize = 0;
    38     quint64 freeSize = 0;
    50     QString driver = driverName;
    39     QString drive = driveName;
    51     
    40     
    52     driver.replace( '/', "\\" );
    41     drive.replace( '/', "\\" );
    53     if ( driver.right(1) != "\\" ) {
    42     if ( drive.right(1) != "\\" ) {
    54         driver.append( "\\" );
    43         drive.append( "\\" );
    55     }
    44     }
    56     GetDiskFreeSpaceEx( (LPCWSTR)driver.constData(),
    45     GetDiskFreeSpaceEx( (LPCWSTR)drive.constData(),
    57                         (PULARGE_INTEGER)&freeSize,
    46                         (PULARGE_INTEGER)&freeSize,
    58                         (PULARGE_INTEGER)&size,
    47                         (PULARGE_INTEGER)&size,
    59                         0 );
    48                         0 );
    60 
    49 
    61     TCHAR volumeName[MAX_PATH + 1] = { 0 };
    50     TCHAR volumeName[MAX_PATH + 1] = { 0 };
    62     GetVolumeInformation( (LPCWSTR)driver.constData(),
    51     GetVolumeInformation( (LPCWSTR)drive.constData(),
    63                           &volumeName[0],
    52                           &volumeName[0],
    64                           MAX_PATH + 1,
    53                           MAX_PATH + 1,
    65                           0,
    54                           0,
    66                           0,
    55                           0,
    67                           0,
    56                           0,
    68                           0,
    57                           0,
    69                           0 );
    58                           0 );
    70 
    59 
    71     quint32 state( 0 );
    60     quint32 state( 0 );
    72     quint32 drvStatus = GetDriveType( (LPCWSTR)driver.constData() );
    61     quint32 drvStatus = GetDriveType( (LPCWSTR)drive.constData() );
    73     if ( drvStatus == DRIVE_REMOVABLE  ) {
    62     if ( drvStatus == DRIVE_REMOVABLE  ) {
    74         state |= FmDriverInfo::EDriveRemovable;
    63         state |= FmDriverInfo::EDriveRemovable;
    75     }
    64     }
    76     return FmDriverInfo( size, freeSize, driverName, QString::fromWCharArray( &volumeName[0] ), state );
    65 
    77 }
    66 	if( !(state&FmDriverInfo::EDriveNotPresent) && !(state&FmDriverInfo::EDriveLocked) &&
    78 
    67 		!(state&FmDriverInfo::EDriveCorrupted) ) {
    79 QString FmUtils::formatStorageSize( quint64 size )
    68 		state |= FmDriverInfo::EDriveAvailable;
    80 {
       
    81 	if ( size < 1000 ) {
       
    82 		return QString::number( size ) + " B";
       
    83 	} else if ( size < 1000 * 1000 ) {
       
    84 		return QString::number( size / 1024.0, 'f', 2 ) + " KB";
       
    85 	} else if ( size < 1000 * 1000 * 1000 ) {
       
    86 		return QString::number( size / (1024.0 * 1024.0), 'f', 1 ) + " MB";
       
    87 	} else {
       
    88 	    return QString::number( size / ( 1024.0 * 1024.0 * 1024.0 ), 'f', 1 ) + " GB";	    
       
    89 	}
    69 	}
    90 }
    70     return FmDriverInfo( size, freeSize, driveName, QString::fromWCharArray( &volumeName[0] ), state );
    91 /*
    71 }
    92 quint32 FmUtils::getDriverState( const QString &driverName )
    72 
    93 {
    73 int FmUtils::removeDrivePwd( const QString &driveName,  const QString &Pwd )
    94     quint32 state( 0 );
    74 {
    95 
    75     Q_UNUSED( driveName );
    96     QString driver = driverName;
       
    97     
       
    98     driver.replace( '/', "\\" );
       
    99     if ( driver.right(1) != "\\" ) {
       
   100         driver.append( "\\" );
       
   101     }
       
   102 
       
   103     quint32 drvStatus = GetDriveType( (LPCWSTR)driver.constData() );
       
   104 
       
   105     if ( drvStatus == DRIVE_REMOVABLE  ) {
       
   106         state |= FmDriverInfo::EDriveRemovable;
       
   107     }
       
   108 
       
   109     return state;
       
   110 
       
   111 }
       
   112 */
       
   113 int FmUtils::removeDrivePwd( const QString &driverName,  const QString &Pwd )
       
   114 {
       
   115     Q_UNUSED( driverName );
       
   116     Q_UNUSED( Pwd );
    76     Q_UNUSED( Pwd );
   117     return 0;
    77     return 0;
   118 }
    78 }
   119 
    79 
   120 int FmUtils::unlockDrive( const QString &driverName,  const QString &Pwd )
    80 int FmUtils::unlockDrive( const QString &driveName,  const QString &Pwd )
   121 {
    81 {
   122     Q_UNUSED( driverName );
    82     Q_UNUSED( driveName );
   123     Q_UNUSED( Pwd );
    83     Q_UNUSED( Pwd );
   124     return 0;
    84     return 0;
   125 }
    85 }
   126 
    86 
   127 int FmUtils::checkDrivePwd( const QString &driverName, const QString &pwd)
    87 int FmUtils::checkDrivePwd( const QString &driveName, const QString &pwd)
   128 {
    88 {
   129     Q_UNUSED( driverName );
    89     Q_UNUSED( driveName );
   130     Q_UNUSED( pwd );
    90     Q_UNUSED( pwd );
   131     return 0;
    91     return 0;
   132 }
    92 }
   133 
    93 
   134 int FmUtils::setDrivePwd( const QString &driverName, const QString &oldPwd, const QString &newPwd)
    94 int FmUtils::setDrivePwd( const QString &driveName, const QString &oldPwd, const QString &newPwd)
   135 {
    95 {
   136     Q_UNUSED( driverName );
    96     Q_UNUSED( driveName );
   137     Q_UNUSED( oldPwd );
    97     Q_UNUSED( oldPwd );
   138     Q_UNUSED( newPwd );
    98     Q_UNUSED( newPwd );
   139     return 0;
    99     return 0;
   140 }
   100 }
   141 
   101 
   142 void FmUtils::emptyPwd( QString &pwd )
   102 void FmUtils::emptyPwd( QString &pwd )
   143 {
   103 {
   144     Q_UNUSED( pwd );
   104     Q_UNUSED( pwd );
   145 }
   105 }
   146 
   106 
   147 int FmUtils::renameDrive( const QString &driverName, const QString &newVolumeName)
   107 int FmUtils::renameDrive( const QString &driveName, const QString &newVolumeName)
   148 {
   108 {
   149     Q_UNUSED( driverName );
   109     Q_UNUSED( driveName );
   150     foreach( QChar ch, newVolumeName )
   110     foreach( const QChar &ch, newVolumeName )
   151     {
   111     {
   152         // If not alphadigit or space, return error
   112         // If not alphadigit or space, return error
   153         if( !ch.isLetterOrNumber() && !ch.isSpace() )
   113         if( !ch.isLetterOrNumber() && !ch.isSpace() )
   154         {
   114         {
   155             return FmErrBadName;
   115             return FmErrBadName;
   156         }   
   116         }   
   157     }
   117     }
   158     return 0;
   118     return 0;
   159 }
   119 }
   160 
   120 
   161 void FmUtils::ejectDrive( const QString &driverName )
   121 int FmUtils::ejectDrive( const QString &driveName )
   162 {
   122 {
   163     Q_UNUSED( driverName );
   123     Q_UNUSED( driveName );
   164 }
   124 	return FmErrNone; 
   165 
   125 }
   166 QString FmUtils::getFileType( const QString &filePath  )
   126 
   167 {
   127 bool FmUtils::checkDriveAccessFilter( const QString &driveName )
   168     Q_UNUSED( filePath );
   128 {
   169     return QString( "" );
   129     Q_UNUSED( driveName );
   170 }
   130     return true;
   171 
   131 }
   172 quint64 FmUtils::getDriveDetailsResult( const QString &folderPath, const QString &extension )
   132 
       
   133 QString FmUtils::checkDriveToFolderFilter( const QString &path )
       
   134 {
       
   135     Q_UNUSED( path );
       
   136     return path;
       
   137 
       
   138 }
       
   139 
       
   140 QString FmUtils::checkFolderToDriveFilter( const QString &path )
       
   141 {
       
   142     Q_UNUSED( path );
       
   143     return path;
       
   144 
       
   145 }
       
   146 
       
   147 int FmUtils::isPathAccessabel( const QString &path )
       
   148 {
       
   149     Q_UNUSED( path );
       
   150     return FmErrNone;
       
   151 }
       
   152 
       
   153 bool FmUtils::isDriveAvailable( const QString &path )
       
   154 {
       
   155     Q_UNUSED( path );
       
   156 	return true;
       
   157 }
       
   158 
       
   159 bool FmUtils::isDefaultFolder( const QString &folderPath  )
   173 {
   160 {
   174     Q_UNUSED( folderPath );
   161     Q_UNUSED( folderPath );
   175     Q_UNUSED( extension );
       
   176     return 0;
       
   177 }
       
   178 
       
   179 bool FmUtils::isDriveC( const QString &driverName )
       
   180 {
       
   181     Q_UNUSED( driverName );
       
   182     return false;
   162     return false;
   183 }
   163 }
   184 
   164 
   185 bool FmUtils::isDrive( const QString &path )
   165 void FmUtils::createDefaultFolders( const QString &driveName )
   186 {
   166 {
   187    bool ret( false );
   167     Q_UNUSED( driveName );
   188    if( path.length() <= 3 && path.length() >=2 ) {
   168 }
   189        ret = true;
   169 
   190    }
   170 /*!
   191    
   171     set the \a desFile attributes as the same with \a srcFile
   192    return ret;   
   172 */
   193 }
   173 int FmUtils::setFileAttributes( const QString &srcFile, const QString &desFile )
   194 
   174 {
   195 void FmUtils::createDefaultFolders( const QString &driverName )
   175     Q_UNUSED( srcFile );
   196 {
   176     Q_UNUSED( desFile );
   197     Q_UNUSED( driverName );
   177     return FmErrNone;
   198 }
   178 }
   199 
   179 
   200 QString FmUtils::fillPathWithSplash( const QString &filePath )
   180 /*!
   201 {
   181     judge whether there is enough space on \a targetDrive for \a size.
   202     QString newFilePath( filePath );
   182     return true if has, false if not.
   203     if( filePath.isEmpty() ) {
   183 */
   204         return newFilePath;
   184 bool FmUtils::hasEnoughSpace( const QString &targetDrive, qint64 size )
   205     }
   185 {
   206 
   186     Q_UNUSED( targetDrive );
   207     if( filePath.at( filePath.length()-1 ) != QChar( '/' ) ){
   187     Q_UNUSED( size );
   208         newFilePath.append( QChar( '/' ) );
       
   209     }
       
   210     return newFilePath;
       
   211 }
       
   212 
       
   213 QString FmUtils::removePathSplash( const QString &filePath )
       
   214 {
       
   215     QString newFilePath( filePath );
       
   216     if( filePath.right( 1 ) == QChar( '/' ) || filePath.right(1) == QString( "\\" ) ) {
       
   217         newFilePath = filePath.left( filePath.length() - 1 );
       
   218     }
       
   219     return newFilePath;
       
   220 }
       
   221 
       
   222 bool FmUtils::checkDriveFilter( const QString &driveName )
       
   223 {
       
   224 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
       
   225     if( driveName.contains( "D:" ) || driveName.contains( "Z:" ) ) {
       
   226         return false;
       
   227     }
       
   228 #endif
       
   229     return true;
   188     return true;
   230 }
   189 }
   231 
   190 
   232 QString FmUtils::checkDriveToFolderFilter( const QString &path )
   191 /*!
   233 {
   192     move one file insice the same drive, from \a source to \a target.
   234     QFileInfo fileInfo( path );
   193     return KErrNone if successful, otherwise one of the other system-wide error codes.
   235     if( !fileInfo.exists() ) {
   194 */
   236             return QString();
   195 int FmUtils::moveInsideDrive( const QString &source, const QString &target )
   237         }
   196 {
   238 
   197     Q_UNUSED( source );
   239 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   198     Q_UNUSED( target );
   240     QString checkedPath = fillPathWithSplash( path );
   199     return FmErrNone;
   241     if( checkedPath.compare( QString( "C:/"), Qt::CaseInsensitive ) == 0 ) {
       
   242         checkedPath += QString( "data/" );
       
   243         QFileInfo fileInfo( checkedPath );
       
   244         if( !fileInfo.exists() ) {
       
   245             return QString();
       
   246         }
       
   247         return checkedPath;
       
   248     }
       
   249 #endif
       
   250     return path;
       
   251 
       
   252 }
       
   253 
       
   254 QString FmUtils::checkFolderToDriveFilter( const QString &path )
       
   255 {
       
   256 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
       
   257     QString logString;
       
   258     logString = QString( "checkFolderToDriveFilter: " ) + path;
       
   259     FmLogger::log( logString );
       
   260     QString checkedPath = fillPathWithSplash( path );
       
   261 
       
   262     logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
       
   263     FmLogger::log( logString );
       
   264     
       
   265     if( checkedPath.compare( QString( "C:/data/"), Qt::CaseInsensitive ) == 0 ) {
       
   266         FmLogger::log( QString( " change from c:/data/ to C:/" ) );
       
   267         return QString( "C:/" );
       
   268     }
       
   269 #endif
       
   270     return path;
       
   271 
       
   272 }
       
   273 
       
   274 bool FmUtils::isPathAccessabel( const QString &path )
       
   275 {
       
   276 #ifdef _DEBUG_DISABLE_DRIVE_D_TEST_DRIVEHIDE_
       
   277     if(path.contains("D:"))
       
   278         return false;
       
   279 #endif
       
   280     QFileInfo fileInfo( path );
       
   281 
       
   282 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
       
   283     if( fileInfo.absoluteFilePath().contains( QString( Drive_C ), Qt::CaseInsensitive ) &&
       
   284         !fileInfo.absoluteFilePath().contains( QString( Folder_C_Data ), Qt::CaseInsensitive ) ) {
       
   285         return false;
       
   286     }
       
   287     if( fileInfo.absoluteFilePath().contains( QString( Drive_D ), Qt::CaseInsensitive ) ) {
       
   288         return false;
       
   289     }
       
   290     if( fileInfo.absoluteFilePath().contains( QString( Drive_Z ), Qt::CaseInsensitive ) ) {
       
   291         return false;
       
   292     }
       
   293 #endif
       
   294     if( !fileInfo.exists() ) {
       
   295         return false;
       
   296     }
       
   297     return true;
       
   298 }
       
   299 
       
   300 bool FmUtils::isDriveAvailable( const QString &path )
       
   301 {
       
   302     QFileInfo fileInfo( path );
       
   303     if( !fileInfo.exists() ) {
       
   304         return false;
       
   305     }
       
   306     return true;
       
   307 }
       
   308 
       
   309 void FmUtils::getDriveList( QStringList &driveList, bool isHideUnAvailableDrive )
       
   310 {
       
   311     QFileInfoList infoList = QDir::drives();
       
   312 
       
   313     foreach( QFileInfo fileInfo, infoList ) {
       
   314         QString driveName = fileInfo.absolutePath();
       
   315         if( checkDriveFilter( driveName ) ) {
       
   316             if( !isHideUnAvailableDrive ) {
       
   317                 driveList.append( driveName );
       
   318             }
       
   319             else if ( isDriveAvailable( driveName ) ) {
       
   320                 driveList.append( driveName );
       
   321             }
       
   322         }
       
   323     }
       
   324     return;
       
   325 }
       
   326 
       
   327 QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
       
   328 {
       
   329     QString ret;
       
   330     QString tempDriveName = fillPathWithSplash( driveName );
       
   331 
       
   332     ret = removePathSplash( driveName );
       
   333     
       
   334     FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
       
   335     QString volumeName = driverInfo.volumeName();
       
   336 
       
   337     if( volumeName.isEmpty() && isFillWithDefaultVolume ){
       
   338         FmDriverInfo::DriveState driveState = FmUtils::queryDriverInfo( tempDriveName ).FmDriverInfo::driveState();
       
   339         if( !( driveState & FmDriverInfo::EDriveNotPresent ) ){
       
   340             if( driveState & FmDriverInfo::EDriveRemovable ) {
       
   341                 if( driveState & FmDriverInfo::EDriveMassStorage ) {
       
   342                     volumeName.append( QObject::tr( "Mass Storage" ) );  
       
   343                 }
       
   344                 else{
       
   345                     volumeName.append( QObject::tr( "Memory Card" ) );
       
   346                 }
       
   347             }
       
   348             else{
       
   349                 volumeName.append( QObject::tr( "Phone Memory" ) );
       
   350             }
       
   351         }
       
   352     }
       
   353 
       
   354     ret += QString( " " ) + volumeName;
       
   355     return ret;
       
   356 }
   200 }
   357 
   201 
   358 int FmUtils::launchFile( const QString &filePath )
   202 int FmUtils::launchFile( const QString &filePath )
   359 {
   203 {
   360     if( QDesktopServices::openUrl( QUrl::fromLocalFile( filePath ) ) ) {
   204     if( QDesktopServices::openUrl( QUrl::fromLocalFile( filePath ) ) ) {
   364     }
   208     }
   365 }
   209 }
   366 
   210 
   367 void FmUtils::sendFiles( QStringList &filePathList )
   211 void FmUtils::sendFiles( QStringList &filePathList )
   368 {
   212 {
   369 
   213     Q_UNUSED( filePathList );
   370 }
   214 }
   371 
   215 
   372 QString FmUtils::getBurConfigPath( QString appPath )
   216 QString FmUtils::getBurConfigPath( QString appPath )
   373 {
   217 {
   374     QFileInfo fileInfo( appPath );
   218     QFileInfo fileInfo( appPath );
   377     path = path + QString( "src/filemanager/" );
   221     path = path + QString( "src/filemanager/" );
   378     path = path + QString( BURCONFIGFILE );
   222     path = path + QString( BURCONFIGFILE );
   379     return path;
   223     return path;
   380 }
   224 }
   381 
   225 
   382 bool FmUtils::isPathEqual( const QString &pathFst, const QString &pathLast )
   226 QString FmUtils::getFileType( const QString &filePath  )
   383 {
   227 {
   384     QString fst( fillPathWithSplash( pathFst ) );
   228     Q_UNUSED( filePath );
   385     QString last( fillPathWithSplash( pathLast ) );
   229     return QString( "" );
   386     if( fst.compare( last, Qt::CaseInsensitive ) == 0 ) {
   230 }
       
   231 
       
   232 bool FmUtils::isDriveC( const QString &driveName )
       
   233 {
       
   234 	if( driveName.contains(Drive_C,Qt::CaseInsensitive) ){
   387         return true;
   235         return true;
   388     }
   236     }
   389     return false;
   237     else{
   390 }
   238         return false;
   391 
   239     }
   392 bool FmUtils::isDefaultFolder( const QString &folderPath  )
   240 }
   393 {
   241 
   394     Q_UNUSED( folderPath );
   242 int FmUtils::getMaxFileNameLength()
   395     return false;
   243 {
   396 }
   244     return KMaxFileName;
   397 
   245 }
   398 QString FmUtils::formatPath( const QString &path  )
   246 
   399 {
   247 bool FmUtils::checkMaxPathLength( const QString& path )
   400     Q_UNUSED( path );
   248 {
   401     return false;
   249     if( path.length() > KMaxPath ) {
   402 }
   250         return false;
       
   251     }
       
   252     return true;
       
   253 }