filemanager/src/filemanager/src/operationservice/fmoperationcopy.cpp
changeset 25 b7bfdea70ca2
parent 16 ada7962b4308
equal deleted inserted replaced
16:ada7962b4308 25:b7bfdea70ca2
    17 
    17 
    18 #include "fmoperationcopy.h"
    18 #include "fmoperationcopy.h"
    19 #include "fmcommon.h"
    19 #include "fmcommon.h"
    20 #include "fmoperationbase.h"
    20 #include "fmoperationbase.h"
    21 #include "fmdrivedetailstype.h"
    21 #include "fmdrivedetailstype.h"
       
    22 #include "fmutils.h"
    22 
    23 
    23 #include <QDir>
    24 #include <QDir>
    24 #include <QFileInfo>
    25 #include <QFileInfo>
    25 #include <QStringList>
    26 #include <QStringList>
    26 #include <QStack>
    27 #include <QStack>
    94                         ret = FmErrCannotRemove;
    95                         ret = FmErrCannotRemove;
    95                         break;
    96                         break;
    96                     }
    97                     }
    97                     destFi.setFile( destFi.absoluteFilePath() );
    98                     destFi.setFile( destFi.absoluteFilePath() );
    98                 } else {
    99                 } else {
    99                     emit askForRename( destFi.absoluteFilePath(), &newName );
   100                     queryForRename( destFi.absoluteFilePath(), &newName );
   100                     if( newName.isEmpty() ) {
   101                     if( newName.isEmpty() ) {
   101                         ret = FmErrCancel;
   102                         ret = FmErrCancel;
   102                         break;
   103                         break;
   103                     }
   104                     }
   104                     QString targetName = mTargetPath + newName;
   105                     QString targetName = mTargetPath + newName;
   105                     destFi.setFile( targetName );
   106                     destFi.setFile( targetName );
   106                 }
   107                 }
   107             } else{
   108             } else{
   108                 // destination is dir
   109                 // destination is dir
   109                 emit askForRename( destFi.absoluteFilePath(), &newName );
   110                 queryForRename( destFi.absoluteFilePath(), &newName );
   110                 if( newName.isEmpty() ) {
   111                 if( newName.isEmpty() ) {
   111                     ret = FmErrCancel;
   112                     ret = FmErrCancel;
   112                     break;
   113                     break;
   113                 }
   114                 }
   114                 QString targetName = mTargetPath + newName;
   115                 QString targetName = mTargetPath + newName;
   153         quint64 fileSize = fi.size();
   154         quint64 fileSize = fi.size();
   154         if ( !QFile::copy( source, newName )) {
   155         if ( !QFile::copy( source, newName )) {
   155             *mErrString = source;
   156             *mErrString = source;
   156             ret = FmErrCannotCopy;
   157             ret = FmErrCannotCopy;
   157         } else {
   158         } else {
   158         IncreaseProgress( fileSize );
   159         increaseProgress( fileSize );
   159         }
   160         }
   160     } else if (fi.isDir()) {
   161     } else if (fi.isDir()) {
   161         ret = copyDirInsideContent( source, newName );
   162         ret = copyDirInsideContent( source, newName );
   162     } else {
   163     } else {
   163         qWarning( "Things other than file and directory are not copied" );
   164         qWarning( "Things other than file and directory are not copied" );
   169 
   170 
   170 
   171 
   171 
   172 
   172 int FmOperationCopy::copyDirInsideContent( const QString &srcPath, const QString &destPath )
   173 int FmOperationCopy::copyDirInsideContent( const QString &srcPath, const QString &destPath )
   173 {
   174 {
   174     if( destPath.contains( srcPath, Qt::CaseInsensitive ) ) {
   175     QFileInfo srcInfo( srcPath );
       
   176     QFileInfo destInfo( destPath );
       
   177     
       
   178     QString destUpPath = FmUtils::fillPathWithSplash( destInfo.absolutePath() );
       
   179     if( destUpPath.contains( srcPath, Qt::CaseInsensitive ) ) {
   175         *mErrString = destPath;
   180         *mErrString = destPath;
   176         return FmErrCopyDestToSubFolderInSrc;
   181         return FmErrCopyDestToSubFolderInSrc;
   177     }
   182     }
   178 
   183 
   179     QFileInfo srcInfo( srcPath );
   184     
   180     if( !srcInfo.isDir() || !srcInfo.exists() ) {
   185     if( !srcInfo.isDir() || !srcInfo.exists() ) {
   181         *mErrString = srcPath;
   186         *mErrString = srcPath;
   182         return FmErrSrcPathDoNotExist;
   187         return FmErrSrcPathDoNotExist;
   183     }
   188     }
   184 
   189     
   185     QFileInfo destInfo( destPath );
       
   186     if( !destInfo.exists() ) {
   190     if( !destInfo.exists() ) {
   187         if( !destInfo.dir().mkdir( destInfo.absoluteFilePath() ) ) {
   191         if( !destInfo.dir().mkdir( destInfo.absoluteFilePath() ) ) {
   188             *mErrString = destPath;
   192             *mErrString = destPath;
   189             return FmErrCannotMakeDir;
   193             return FmErrCannotMakeDir;
   190         }
   194         }
   208             QString newFilePath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
   212             QString newFilePath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
   209             if (!QFile::copy( fileInfo.absoluteFilePath(), newFilePath ) ) {
   213             if (!QFile::copy( fileInfo.absoluteFilePath(), newFilePath ) ) {
   210                 *mErrString = fileInfo.absoluteFilePath();
   214                 *mErrString = fileInfo.absoluteFilePath();
   211                 return FmErrCannotCopy;
   215                 return FmErrCannotCopy;
   212             }
   216             }
   213             IncreaseProgress( fileInfo.size() );
   217             increaseProgress( fileInfo.size() );
   214         } else if( fileInfo.isDir() ) {
   218         } else if( fileInfo.isDir() ) {
   215             //makedir
   219             //makedir
   216             QString newDirPath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
   220             QString newDirPath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
   217             if( !QDir( newDirPath ).exists() && !QDir( destPath ).mkdir( newDirPath ) ) {
   221             if( !QDir( newDirPath ).exists() && !QDir( destPath ).mkdir( newDirPath ) ) {
   218                 *mErrString = newDirPath;
   222                 *mErrString = newDirPath;
   233     }
   237     }
   234 
   238 
   235     return FmErrNone;
   239     return FmErrNone;
   236 }
   240 }
   237 
   241 
   238 void FmOperationCopy::IncreaseProgress( quint64 size )
   242 void FmOperationCopy::increaseProgress( quint64 size )
   239 {
   243 {
   240     if( mTotalSize <=0 ) {
   244     if( mTotalSize <=0 ) {
   241         return;
   245         return;
   242     }
   246     }
   243     mCopiedSize += size;
   247     mCopiedSize += size;
   245     if( step > mCurrentStep ) {
   249     if( step > mCurrentStep ) {
   246         mCurrentStep = step;
   250         mCurrentStep = step;
   247         emit notifyProgress( mCurrentStep );
   251         emit notifyProgress( mCurrentStep );
   248     }
   252     }
   249 }
   253 }
       
   254 
       
   255 void FmOperationCopy::queryForRename( const QString &srcFile, QString *destFile )
       
   256 {
       
   257     emit askForRename( srcFile, destFile );
       
   258 }