filemanager/src/inc/fmutils_win.cpp
changeset 35 060d0b1ab845
parent 30 6e96d2143d46
child 41 fc4654ce4fcb
equal deleted inserted replaced
30:6e96d2143d46 35:060d0b1ab845
   220 void FmUtils::createDefaultFolders( const QString &driverName )
   220 void FmUtils::createDefaultFolders( const QString &driverName )
   221 {
   221 {
   222     Q_UNUSED( driverName );
   222     Q_UNUSED( driverName );
   223 }
   223 }
   224 
   224 
       
   225 /*!
       
   226     fill splash in the end of \a filePath if the path is not a file
       
   227     All "/" and "\" will be changed to QDir::separator
       
   228     \sa formatPath only changed "/" and "\" to QDir::separator
       
   229 */
   225 QString FmUtils::fillPathWithSplash( const QString &filePath )
   230 QString FmUtils::fillPathWithSplash( const QString &filePath )
   226 {
   231 {
   227 	QString newFilePath;
   232     QString newFilePath;
   228     if( filePath.isEmpty() ) {
   233     if( filePath.isEmpty() ) {
   229         return newFilePath;
   234         return newFilePath;
   230     }
   235     }
   231 
   236 
   232     foreach( QChar ch, filePath ) {
   237     newFilePath = formatPath( filePath );
   233         if( ch == QChar('\\') || ch == QChar('/') ) {
       
   234 			newFilePath.append( QDir::separator() );
       
   235         } else {
       
   236             newFilePath.append( ch );
       
   237         }
       
   238     }
       
   239     
   238     
   240     if( newFilePath.right( 1 )!= QDir::separator() ){
   239     if( newFilePath.right( 1 )!= QDir::separator() ){
   241         newFilePath.append( QDir::separator() );
   240         newFilePath.append( QDir::separator() );
   242     }
   241     }
   243     
       
   244     return newFilePath;
   242     return newFilePath;
   245 }
   243 }
   246 
   244 
   247 QString FmUtils::removePathSplash( const QString &filePath )
   245 QString FmUtils::removePathSplash( const QString &filePath )
   248 {
   246 {
   288 QString FmUtils::checkFolderToDriveFilter( const QString &path )
   286 QString FmUtils::checkFolderToDriveFilter( const QString &path )
   289 {
   287 {
   290 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   288 #ifdef _DEBUG_HIDE_VIEWFOLDER_WINDOWS_
   291     QString logString;
   289     QString logString;
   292     logString = QString( "checkFolderToDriveFilter: " ) + path;
   290     logString = QString( "checkFolderToDriveFilter: " ) + path;
   293     FmLogger::log( logString );
   291     FM_LOG( logString );
   294     QString checkedPath = fillPathWithSplash( path );
   292     QString checkedPath = fillPathWithSplash( path );
   295 
   293 
   296     logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
   294     logString = QString( "checkFolderToDriveFilter_fillPathWithSplash: " ) + checkedPath;
   297     FmLogger::log( logString );
   295     FM_LOG( logString );
   298     
   296     
   299     if( checkedPath.compare( QString( "C:/data/"), Qt::CaseInsensitive ) == 0 ) {
   297     if( checkedPath.compare( QString( "C:/data/"), Qt::CaseInsensitive ) == 0 ) {
   300         FmLogger::log( QString( " change from c:/data/ to C:/" ) );
   298         FM_LOG( QString( " change from c:/data/ to C:/" ) );
   301         return QString( "C:/" );
   299         return QString( "C:/" );
   302     }
   300     }
   303 #endif
   301 #endif
   304     return path;
   302     return path;
   305 
   303 
   435 {
   433 {
   436     Q_UNUSED( folderPath );
   434     Q_UNUSED( folderPath );
   437     return false;
   435     return false;
   438 }
   436 }
   439 
   437 
       
   438 /*!
       
   439     All "/" and "\" in \a path will be changed to QDir::separator
       
   440     \sa fillPathWithSplash, fillPathWithSplash will append QDir::separator in the end if path is no a file
       
   441 */
   440 QString FmUtils::formatPath( const QString &path  )
   442 QString FmUtils::formatPath( const QString &path  )
   441 {
   443 {
   442     QString formatPath;
   444     QString formatPath;
   443 	foreach( QChar ch, path ) {
   445     if( path.isEmpty() ) {
   444 		if( ch == QChar('\\') || ch == QChar('/') ) {
   446         return formatPath;
   445 			formatPath.append( QDir::separator() );
   447     }
   446 		} else {
   448     
   447 			formatPath.append( ch );
   449     foreach( QChar ch, path ) {
   448 		}
   450         if( ch == QChar('\\') || ch == QChar('/') ) {
   449     }
   451             formatPath.append( QDir::separator() );
   450 
   452         } else {
   451     if( formatPath.right( 1 ) != QDir::separator() ){
   453             formatPath.append( ch );
   452         formatPath.append( QDir::separator() );
   454         }
   453     }
   455     }
       
   456 
   454     return formatPath;
   457     return formatPath;
   455 }
   458 }
   456 
   459 
   457 int FmUtils::getMaxFileNameLength()
   460 int FmUtils::getMaxFileNameLength()
   458 {
   461 {
   464 	if( path.length() > KMaxPath ) {
   467 	if( path.length() > KMaxPath ) {
   465 		return false;
   468 		return false;
   466 	}
   469 	}
   467 	return true;
   470 	return true;
   468 }
   471 }
       
   472 
   469 bool FmUtils::checkFolderFileName( const QString& name )
   473 bool FmUtils::checkFolderFileName( const QString& name )
   470 {
   474 {
   471     if( name.endsWith( QChar('.'),  Qt::CaseInsensitive ) ) {
   475     // trim space firest, because there may be some spaces after "." ,  it is also not valid
   472         return false;
   476     // or there may only have spaces in name
   473     }
   477     QString trimmedName( name.trimmed() );
   474     if( name.contains( QChar('\\'), Qt::CaseInsensitive ) ||
   478 	if( trimmedName.isEmpty() ) {
   475         name.contains( QChar('/'),  Qt::CaseInsensitive ) ||
   479 		return false;
   476         name.contains( QChar(':'),  Qt::CaseInsensitive ) ||
   480 	}
   477         name.contains( QChar('*'),  Qt::CaseInsensitive ) ||
   481     if( trimmedName.endsWith( QChar('.'),  Qt::CaseInsensitive ) ) {
   478         name.contains( QChar('?'),  Qt::CaseInsensitive ) ||
   482         return false;
   479         name.contains( QChar('\"'), Qt::CaseInsensitive ) ||
   483     }
   480         name.contains( QChar('<'),  Qt::CaseInsensitive ) ||
   484     if( trimmedName.contains( QChar('\\'), Qt::CaseInsensitive ) ||
   481         name.contains( QChar('>'),  Qt::CaseInsensitive ) ||
   485         trimmedName.contains( QChar('/'),  Qt::CaseInsensitive ) ||
   482         name.contains( QChar('|'),  Qt::CaseInsensitive ) ){
   486         trimmedName.contains( QChar(':'),  Qt::CaseInsensitive ) ||
   483         return false;
   487         trimmedName.contains( QChar('*'),  Qt::CaseInsensitive ) ||
   484     }
   488         trimmedName.contains( QChar('?'),  Qt::CaseInsensitive ) ||
       
   489         trimmedName.contains( QChar('\"'), Qt::CaseInsensitive ) ||
       
   490         trimmedName.contains( QChar('<'),  Qt::CaseInsensitive ) ||
       
   491         trimmedName.contains( QChar('>'),  Qt::CaseInsensitive ) ||
       
   492         trimmedName.contains( QChar('|'),  Qt::CaseInsensitive ) ){
       
   493         return false;
       
   494     }
       
   495     // use orignal name to exam max size of file name
   485     if( name.length() > KMaxFileName ) {
   496     if( name.length() > KMaxFileName ) {
   486         return false;
   497         return false;
   487     }
   498     }
   488     return true;
   499     return true;
   489 }
   500 }
   490 
   501 
   491 bool FmUtils::checkNewFolderOrFile( const QString &path, QString &errString )
   502 bool FmUtils::checkNewFolderOrFile( const QString &fileName, const QString &path, QString &errString )
   492 {
   503 {
       
   504     // first check if fileName is valid, then check if path length is valid, and check if file/foler is existed at last
   493     QFileInfo fileInfo( path );
   505     QFileInfo fileInfo( path );
   494     bool ret( true );   
   506     bool ret( true );   
   495     if (!FmUtils::checkFolderFileName( fileInfo.fileName() ) ) {
   507     if (!FmUtils::checkFolderFileName( fileName ) ) {
   496         errString = hbTrId( "Invalid file or folder name, try again!" );
   508         errString = hbTrId( "Invalid file or folder name, try again!" );
   497         ret = false;
   509         ret = false;
   498     } else if( !FmUtils::checkMaxPathLength( path ) ) {
   510     } else if( !FmUtils::checkMaxPathLength( path ) ) {
   499         errString = hbTrId( "the path you specified is too long, try again!" );
   511         errString = hbTrId( "the path you specified is too long, try again!" );
   500         ret = false;
   512         ret = false;
   502         errString = hbTrId( "%1 already exist!" ).arg( fileInfo.fileName() );
   514         errString = hbTrId( "%1 already exist!" ).arg( fileInfo.fileName() );
   503         ret = false;
   515         ret = false;
   504     }
   516     }
   505     return ret;
   517     return ret;
   506 }
   518 }
       
   519 
       
   520 QString FmUtils::getVolumeNameWithDefaultNameIfNull( const QString &diskName, bool &defaultName )
       
   521 {
       
   522     FmDriverInfo driverInfo = FmUtils::queryDriverInfo( diskName );
       
   523 
       
   524     // do not add default volume for win32 version as this is only the dummy implememnt for debug on windows
       
   525     return driverInfo.volumeName();
       
   526 }
       
   527 
       
   528 /*!
       
   529     Check if \a dest is sub level path of \a src
       
   530     Used to check True/False when copy a folder to itself or its subfolder
       
   531     For example, c:\data\test is sub path of c:\data.
       
   532     But c:\data123\test is not sub path of c:\data.
       
   533     So after got right part of path, the first char must be \ or /
       
   534 */
       
   535 bool FmUtils::isSubLevelPath( const QString &src, const QString &dest )
       
   536 {
       
   537     FM_LOG("FmUtils::isSubFolder: src=" + src + " dest=" + dest);
       
   538     QString checkedSrc( FmUtils::fillPathWithSplash( src ) );
       
   539     QString checkedDest( FmUtils::fillPathWithSplash( dest ) );
       
   540     
       
   541     if( checkedDest.contains( checkedSrc, Qt::CaseInsensitive) &&
       
   542             checkedDest.length() > checkedSrc.length() ) {
       
   543         // for example c:\data\ vs c:\data\123\ 
       
   544         FM_LOG("FmUtils::isSubFolder: true");
       
   545         return true;
       
   546     }
       
   547     // for example c:\data\ vs c:\data\ 
       
   548     // for example c:\data\ vs c:\data123\ 
       
   549 
       
   550     FM_LOG("FmUtils::isSubFolder: false");
       
   551     return false;
       
   552 }
       
   553 
       
   554 int FmUtils::setFileAttributes( const QString &srcFile, const QString &desFile )
       
   555 {
       
   556     return FmErrNone;
       
   557 }