filemanager/src/inc/fmutils_win.cpp
changeset 25 b7bfdea70ca2
parent 16 ada7962b4308
child 27 df183af6b92f
equal deleted inserted replaced
16:ada7962b4308 25:b7bfdea70ca2
    23 #include <QFileInfo>
    23 #include <QFileInfo>
    24 #include <QDesktopServices>
    24 #include <QDesktopServices>
    25 #include <QUrl>
    25 #include <QUrl>
    26 #include <QVariant>
    26 #include <QVariant>
    27 
    27 
       
    28 #include <hbglobal.h>
       
    29 
    28 #define BURCONFIGFILE  "burconfig.xml"
    30 #define BURCONFIGFILE  "burconfig.xml"
       
    31 const int KMaxFileName=0x100;
       
    32 const int KMaxPath=0x100;
    29 
    33 
    30 QString FmUtils::getDriveNameFromPath( const QString &path )
    34 QString FmUtils::getDriveNameFromPath( const QString &path )
    31 {
    35 {
    32     if( path.length() <3 ) {
    36     if( path.length() <3 ) {
    33         return QString();
    37         return QString();
    71     quint32 state( 0 );
    75     quint32 state( 0 );
    72     quint32 drvStatus = GetDriveType( (LPCWSTR)driver.constData() );
    76     quint32 drvStatus = GetDriveType( (LPCWSTR)driver.constData() );
    73     if ( drvStatus == DRIVE_REMOVABLE  ) {
    77     if ( drvStatus == DRIVE_REMOVABLE  ) {
    74         state |= FmDriverInfo::EDriveRemovable;
    78         state |= FmDriverInfo::EDriveRemovable;
    75     }
    79     }
       
    80 #ifdef _DEBUG_DISABLE_DRIVE_D_TEST_DRIVEHIDE_
       
    81 	if ( driverName.contains( Drive_D, Qt::CaseInsensitive )  ) {
       
    82 		state |= FmDriverInfo::EDriveNotPresent;
       
    83     }
       
    84 #endif
       
    85 
       
    86 #ifdef _DEBUG_LOCKED_DRIVE_Z
       
    87 	if ( driverName.contains( Drive_Z, Qt::CaseInsensitive )  ) {
       
    88 		state |= FmDriverInfo::EDriveLocked;
       
    89     }
       
    90 #endif
       
    91 	if( !(state&FmDriverInfo::EDriveNotPresent) && !(state&FmDriverInfo::EDriveLocked) &&
       
    92 		!(state&FmDriverInfo::EDriveCorrupted) ) {
       
    93 		state |= FmDriverInfo::EDriveAvailable;
       
    94 	}
    76     return FmDriverInfo( size, freeSize, driverName, QString::fromWCharArray( &volumeName[0] ), state );
    95     return FmDriverInfo( size, freeSize, driverName, QString::fromWCharArray( &volumeName[0] ), state );
    77 }
    96 }
    78 
    97 
    79 QString FmUtils::formatStorageSize( quint64 size )
    98 QString FmUtils::formatStorageSize( quint64 size )
    80 {
    99 {
    86 		return QString::number( size / (1024.0 * 1024.0), 'f', 1 ) + " MB";
   105 		return QString::number( size / (1024.0 * 1024.0), 'f', 1 ) + " MB";
    87 	} else {
   106 	} else {
    88 	    return QString::number( size / ( 1024.0 * 1024.0 * 1024.0 ), 'f', 1 ) + " GB";	    
   107 	    return QString::number( size / ( 1024.0 * 1024.0 * 1024.0 ), 'f', 1 ) + " GB";	    
    89 	}
   108 	}
    90 }
   109 }
       
   110 
    91 /*
   111 /*
    92 quint32 FmUtils::getDriverState( const QString &driverName )
   112 quint32 FmUtils::getDriverState( const QString &driverName )
    93 {
   113 {
    94     quint32 state( 0 );
   114     quint32 state( 0 );
    95 
   115 
   145 }
   165 }
   146 
   166 
   147 int FmUtils::renameDrive( const QString &driverName, const QString &newVolumeName)
   167 int FmUtils::renameDrive( const QString &driverName, const QString &newVolumeName)
   148 {
   168 {
   149     Q_UNUSED( driverName );
   169     Q_UNUSED( driverName );
   150     foreach( QChar ch, newVolumeName )
   170     foreach( const QChar &ch, newVolumeName )
   151     {
   171     {
   152         // If not alphadigit or space, return error
   172         // If not alphadigit or space, return error
   153         if( !ch.isLetterOrNumber() && !ch.isSpace() )
   173         if( !ch.isLetterOrNumber() && !ch.isSpace() )
   154         {
   174         {
   155             return FmErrBadName;
   175             return FmErrBadName;
   156         }   
   176         }   
   157     }
   177     }
   158     return 0;
   178     return 0;
   159 }
   179 }
   160 
   180 
   161 void FmUtils::ejectDrive( const QString &driverName )
   181 int FmUtils::ejectDrive( const QString &driverName )
   162 {
   182 {
   163     Q_UNUSED( driverName );
   183     Q_UNUSED( driverName );
       
   184 	return FmErrNone; 
   164 }
   185 }
   165 
   186 
   166 QString FmUtils::getFileType( const QString &filePath  )
   187 QString FmUtils::getFileType( const QString &filePath  )
   167 {
   188 {
   168     Q_UNUSED( filePath );
   189     Q_UNUSED( filePath );
   176     return 0;
   197     return 0;
   177 }
   198 }
   178 
   199 
   179 bool FmUtils::isDriveC( const QString &driverName )
   200 bool FmUtils::isDriveC( const QString &driverName )
   180 {
   201 {
   181     Q_UNUSED( driverName );
   202 	if( driverName.contains(Drive_C,Qt::CaseInsensitive) ){
   182     return false;
   203         return true;
       
   204     }
       
   205     else{
       
   206         return false;
       
   207     }
   183 }
   208 }
   184 
   209 
   185 bool FmUtils::isDrive( const QString &path )
   210 bool FmUtils::isDrive( const QString &path )
   186 {
   211 {
   187    bool ret( false );
   212    bool ret( false );
   197     Q_UNUSED( driverName );
   222     Q_UNUSED( driverName );
   198 }
   223 }
   199 
   224 
   200 QString FmUtils::fillPathWithSplash( const QString &filePath )
   225 QString FmUtils::fillPathWithSplash( const QString &filePath )
   201 {
   226 {
   202     QString newFilePath( filePath );
   227 	QString newFilePath;
   203     if( filePath.isEmpty() ) {
   228     if( filePath.isEmpty() ) {
   204         return newFilePath;
   229         return newFilePath;
   205     }
   230     }
   206 
   231 
   207     if( filePath.at( filePath.length()-1 ) != QChar( '/' ) ){
   232     foreach( QChar ch, filePath ) {
   208         newFilePath.append( QChar( '/' ) );
   233         if( ch == QChar('\\') || ch == QChar('/') ) {
   209     }
   234 			newFilePath.append( QDir::separator() );
       
   235         } else {
       
   236             newFilePath.append( ch );
       
   237         }
       
   238     }
       
   239     
       
   240     if( newFilePath.right( 1 )!= QDir::separator() ){
       
   241         newFilePath.append( QDir::separator() );
       
   242     }
       
   243     
   210     return newFilePath;
   244     return newFilePath;
   211 }
   245 }
   212 
   246 
   213 QString FmUtils::removePathSplash( const QString &filePath )
   247 QString FmUtils::removePathSplash( const QString &filePath )
   214 {
   248 {
   217         newFilePath = filePath.left( filePath.length() - 1 );
   251         newFilePath = filePath.left( filePath.length() - 1 );
   218     }
   252     }
   219     return newFilePath;
   253     return newFilePath;
   220 }
   254 }
   221 
   255 
   222 bool FmUtils::checkDriveFilter( const QString &driveName )
   256 bool FmUtils::checkDriveAccessFilter( const QString &driveName )
   223 {
   257 {
   224 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   258 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   225     if( driveName.contains( "D:" ) || driveName.contains( "Z:" ) ) {
   259     if( driveName.contains( Drive_D, Qt::CaseInsensitive ) || driveName.contains( Drive_Z, Qt::CaseInsensitive ) ) {
   226         return false;
   260         return false;
   227     }
   261     }
   228 #endif
   262 #endif
   229     return true;
   263     return true;
   230 }
   264 }
   269 #endif
   303 #endif
   270     return path;
   304     return path;
   271 
   305 
   272 }
   306 }
   273 
   307 
   274 bool FmUtils::isPathAccessabel( const QString &path )
   308 int FmUtils::isPathAccessabel( const QString &path )
   275 {
   309 {
   276 #ifdef _DEBUG_DISABLE_DRIVE_D_TEST_DRIVEHIDE_
   310     if(!isDriveAvailable( path ) ) { //used to filter locked drive
   277     if(path.contains("D:"))
   311         return FmErrDriveNotAvailable;
   278         return false;
   312     }
   279 #endif
   313 
   280     QFileInfo fileInfo( path );
   314     QFileInfo fileInfo( path );
   281 
   315 
   282 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   316 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   283     if( fileInfo.absoluteFilePath().contains( QString( Drive_C ), Qt::CaseInsensitive ) &&
   317     if( fileInfo.absoluteFilePath().contains( QString( Drive_C ), Qt::CaseInsensitive ) &&
   284         !fileInfo.absoluteFilePath().contains( QString( Folder_C_Data ), Qt::CaseInsensitive ) ) {
   318         !fileInfo.absoluteFilePath().contains( QString( Folder_C_Data ), Qt::CaseInsensitive ) ) {
   290     if( fileInfo.absoluteFilePath().contains( QString( Drive_Z ), Qt::CaseInsensitive ) ) {
   324     if( fileInfo.absoluteFilePath().contains( QString( Drive_Z ), Qt::CaseInsensitive ) ) {
   291         return false;
   325         return false;
   292     }
   326     }
   293 #endif
   327 #endif
   294     if( !fileInfo.exists() ) {
   328     if( !fileInfo.exists() ) {
   295         return false;
   329         return FmErrPathNotExist;
   296     }
   330     }
   297     return true;
   331     return FmErrNone;
   298 }
   332 }
   299 
   333 
   300 bool FmUtils::isDriveAvailable( const QString &path )
   334 bool FmUtils::isDriveAvailable( const QString &path )
   301 {
   335 {
   302     QFileInfo fileInfo( path );
   336 #ifdef _DEBUG_DISABLE_DRIVE_D_TEST_DRIVEHIDE_
   303     if( !fileInfo.exists() ) {
   337 	if(path.contains(Drive_D, Qt::CaseInsensitive))
   304         return false;
   338 		return false;
   305     }
   339 #endif
   306     return true;
   340 #ifdef _DEBUG_LOCKED_DRIVE_Z
       
   341 	if(path.contains(Drive_Z, Qt::CaseInsensitive))
       
   342 		return false;
       
   343 #endif
       
   344 	return true;
   307 }
   345 }
   308 
   346 
   309 void FmUtils::getDriveList( QStringList &driveList, bool isHideUnAvailableDrive )
   347 void FmUtils::getDriveList( QStringList &driveList, bool isHideUnAvailableDrive )
   310 {
   348 {
   311     QFileInfoList infoList = QDir::drives();
   349     QFileInfoList infoList = QDir::drives();
   312 
   350 
   313     foreach( QFileInfo fileInfo, infoList ) {
   351     foreach( QFileInfo fileInfo, infoList ) {
   314         QString driveName = fileInfo.absolutePath();
   352         QString driveName = fileInfo.absolutePath();
   315         if( checkDriveFilter( driveName ) ) {
   353         if( checkDriveAccessFilter( driveName ) ) {
   316             if( !isHideUnAvailableDrive ) {
   354             if( !isHideUnAvailableDrive ) {
   317                 driveList.append( driveName );
   355                 driveList.append( driveName );
   318             }
   356             }
   319             else if ( isDriveAvailable( driveName ) ) {
   357             else if ( isDriveAvailable( driveName ) ) {
   320                 driveList.append( driveName );
   358                 driveList.append( driveName );
   327 QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
   365 QString FmUtils::fillDriveVolume( QString driveName, bool isFillWithDefaultVolume )
   328 {
   366 {
   329     QString ret;
   367     QString ret;
   330     QString tempDriveName = fillPathWithSplash( driveName );
   368     QString tempDriveName = fillPathWithSplash( driveName );
   331 
   369 
   332     ret = removePathSplash( driveName );
   370     QString checkedDriveName( removePathSplash( driveName ) );
   333     
   371     
   334     FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
   372     FmDriverInfo driverInfo = FmUtils::queryDriverInfo( tempDriveName );
   335     QString volumeName = driverInfo.volumeName();
   373     QString volumeName = driverInfo.volumeName();
   336 
   374 
   337     if( volumeName.isEmpty() && isFillWithDefaultVolume ){
   375     if( volumeName.isEmpty() && isFillWithDefaultVolume ){
   338         FmDriverInfo::DriveState driveState = FmUtils::queryDriverInfo( tempDriveName ).FmDriverInfo::driveState();
   376     FmDriverInfo::DriveState driveState = queryDriverInfo( tempDriveName ).driveState();
   339         if( !( driveState & FmDriverInfo::EDriveNotPresent ) ){
   377         if( driveState & FmDriverInfo::EDriveAvailable ){
   340             if( driveState & FmDriverInfo::EDriveRemovable ) {
   378             if( driveState & FmDriverInfo::EDriveRemovable ) {
   341                 if( driveState & FmDriverInfo::EDriveMassStorage ) {
   379                 if( driveState & FmDriverInfo::EDriveMassStorage ) {
   342                     volumeName.append( QObject::tr( "Mass Storage" ) );  
   380                     // Mass Storage
       
   381                     ret = hbTrId( "txt_fmgr_dblist_1_mass_storage" ).arg( checkedDriveName );  
       
   382                 } else if( driveState & FmDriverInfo::EDriveUsbMemory ) {
       
   383                     // USB Memory
       
   384                     ret = hbTrId( "txt_fmgr_dblist_1_usb_memory" ).arg( checkedDriveName );
       
   385                 } else{
       
   386                     // Memory Card
       
   387                     ret = hbTrId( "txt_fmgr_dblist_1_memory_card" ).arg( checkedDriveName );
   343                 }
   388                 }
   344                 else{
   389             } else{
   345                     volumeName.append( QObject::tr( "Memory Card" ) );
   390 				// phone memory
   346                 }
   391 				ret = hbTrId( "txt_fmgr_dblist_1_device_memory" ).arg( checkedDriveName );
   347             }
       
   348             else{
       
   349                 volumeName.append( QObject::tr( "Phone Memory" ) );
       
   350             }
   392             }
   351         }
   393         }
   352     }
   394     } else {
   353 
   395         ret = hbTrId( "txt_fmgr_dblist_1_2" ).arg( checkedDriveName ).arg( volumeName );
   354     ret += QString( " " ) + volumeName;
   396     }
   355     return ret;
   397     return ret;
   356 }
   398 }
   357 
   399 
   358 int FmUtils::launchFile( const QString &filePath )
   400 int FmUtils::launchFile( const QString &filePath )
   359 {
   401 {
   395     return false;
   437     return false;
   396 }
   438 }
   397 
   439 
   398 QString FmUtils::formatPath( const QString &path  )
   440 QString FmUtils::formatPath( const QString &path  )
   399 {
   441 {
   400     Q_UNUSED( path );
   442     QString formatPath;
   401     return false;
   443 	foreach( QChar ch, path ) {
   402 }
   444 		if( ch == QChar('\\') || ch == QChar('/') ) {
       
   445 			formatPath.append( QDir::separator() );
       
   446 		} else {
       
   447 			formatPath.append( ch );
       
   448 		}
       
   449     }
       
   450 
       
   451     if( formatPath.right( 1 ) != QDir::separator() ){
       
   452         formatPath.append( QDir::separator() );
       
   453     }
       
   454     return formatPath;
       
   455 }
       
   456 
       
   457 int FmUtils::getMaxFileNameLength()
       
   458 {
       
   459 	return KMaxFileName;
       
   460 }
       
   461 
       
   462 bool FmUtils::checkMaxPathLength( const QString& path )
       
   463 {
       
   464 	if( path.length() > KMaxPath ) {
       
   465 		return false;
       
   466 	}
       
   467 	return true;
       
   468 }
       
   469 bool FmUtils::checkFolderFileName( const QString& name )
       
   470 {
       
   471     if( name.endsWith( QChar('.'),  Qt::CaseInsensitive ) ) {
       
   472         return false;
       
   473     }
       
   474     if( name.contains( QChar('\\'), Qt::CaseInsensitive ) ||
       
   475         name.contains( QChar('/'),  Qt::CaseInsensitive ) ||
       
   476         name.contains( QChar(':'),  Qt::CaseInsensitive ) ||
       
   477         name.contains( QChar('*'),  Qt::CaseInsensitive ) ||
       
   478         name.contains( QChar('?'),  Qt::CaseInsensitive ) ||
       
   479         name.contains( QChar('\"'), Qt::CaseInsensitive ) ||
       
   480         name.contains( QChar('<'),  Qt::CaseInsensitive ) ||
       
   481         name.contains( QChar('>'),  Qt::CaseInsensitive ) ||
       
   482         name.contains( QChar('|'),  Qt::CaseInsensitive ) ){
       
   483         return false;
       
   484     }
       
   485     if( name.length() > KMaxFileName ) {
       
   486         return false;
       
   487     }
       
   488     return true;
       
   489 }
       
   490 
       
   491 bool FmUtils::checkNewFolderOrFile( const QString &path, QString &errString )
       
   492 {
       
   493     QFileInfo fileInfo( path );
       
   494     bool ret( true );   
       
   495     if (!FmUtils::checkFolderFileName( fileInfo.fileName() ) ) {
       
   496         errString = hbTrId( "Invalid file or folder name, try again!" );
       
   497         ret = false;
       
   498     } else if( !FmUtils::checkMaxPathLength( path ) ) {
       
   499         errString = hbTrId( "the path you specified is too long, try again!" );
       
   500         ret = false;
       
   501     } else if (fileInfo.exists()) {
       
   502         errString = hbTrId( "%1 already exist!" ).arg( fileInfo.fileName() );
       
   503         ret = false;
       
   504     }
       
   505     return ret;
       
   506 }