filemanager/src/filemanager/src/operationservice/fmoperationcopy.cpp
changeset 37 15bc28c9dd51
parent 16 ada7962b4308
child 46 d58987eac7e8
equal deleted inserted replaced
16:ada7962b4308 37:15bc28c9dd51
     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 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "fmoperationcopy.h"
       
    19 #include "fmcommon.h"
       
    20 #include "fmoperationbase.h"
       
    21 #include "fmdrivedetailstype.h"
       
    22 
       
    23 #include <QDir>
       
    24 #include <QFileInfo>
       
    25 #include <QStringList>
       
    26 #include <QStack>
       
    27 
       
    28 
       
    29 FmOperationCopy::FmOperationCopy( QObject *parent, QStringList sourceList, QString targetPath ) :
       
    30         FmOperationBase( parent, FmOperationService::EOperationTypeCopy ), 
       
    31         mSourceList( sourceList ), mTargetPath( targetPath ),
       
    32         mStop( 0 ), mTotalSize( 0 ), mErrString( 0 ), mCopiedSize( 0 ), mTotalSteps( 100 ), mCurrentStep( 0 )
       
    33 {
       
    34 }
       
    35 
       
    36 FmOperationCopy::~FmOperationCopy()
       
    37 {
       
    38 }
       
    39 
       
    40 QStringList FmOperationCopy::sourceList()
       
    41 {
       
    42     return mSourceList;
       
    43 }
       
    44 
       
    45 QString FmOperationCopy::targetPath()
       
    46 {
       
    47     return mTargetPath;
       
    48 }
       
    49 
       
    50 int FmOperationCopy::start( volatile bool *isStopped, QString *errString )
       
    51 {
       
    52     mStop = isStopped;
       
    53     mErrString = errString;
       
    54     mTotalSize   = 0;
       
    55     mCopiedSize  = 0;
       
    56     mCurrentStep = 0;
       
    57 
       
    58     if( mSourceList.empty() ) {
       
    59         return FmErrWrongParam;
       
    60     }
       
    61 
       
    62     emit notifyPreparing( true );
       
    63 
       
    64     int numofFolders = 0;
       
    65     int numofFiles      = 0;
       
    66 
       
    67     int ret = FmFolderDetails::queryDetailOfContentList( mSourceList, numofFolders, 
       
    68         numofFiles, mTotalSize, mStop, true );
       
    69     if( ret != FmErrNone ) {
       
    70         return ret;
       
    71     }
       
    72 
       
    73     emit notifyStart( true, mTotalSteps );
       
    74 
       
    75     foreach( const QString& source, mSourceList ) {
       
    76         QFileInfo fi( source );
       
    77         if( !fi.exists() ) {
       
    78             *mErrString = source;
       
    79             ret = FmErrSrcPathDoNotExist;
       
    80             return ret;
       
    81         }
       
    82         QString newName;
       
    83         bool    isAcceptReplace = false;
       
    84         QFileInfo destFi( mTargetPath + fi.fileName() );
       
    85 
       
    86         // while for duplicated file/dir
       
    87         while( destFi.exists() ) {
       
    88             if( destFi.isFile() && destFi.absoluteFilePath().compare( fi.absoluteFilePath(), Qt::CaseInsensitive ) != 0 ) {
       
    89                 emit askForReplace( destFi.absoluteFilePath(), fi.absoluteFilePath(), &isAcceptReplace );
       
    90                 if( isAcceptReplace ) {
       
    91                     //delete src file
       
    92                     if( !QFile::remove( destFi.absoluteFilePath() ) ) {
       
    93                         *mErrString = destFi.absoluteFilePath();
       
    94                         ret = FmErrCannotRemove;
       
    95                         break;
       
    96                     }
       
    97                     destFi.setFile( destFi.absoluteFilePath() );
       
    98                 } else {
       
    99                     emit askForRename( destFi.absoluteFilePath(), &newName );
       
   100                     if( newName.isEmpty() ) {
       
   101                         ret = FmErrCancel;
       
   102                         break;
       
   103                     }
       
   104                     QString targetName = mTargetPath + newName;
       
   105                     destFi.setFile( targetName );
       
   106                 }
       
   107             } else{
       
   108                 // destination is dir
       
   109                 emit askForRename( destFi.absoluteFilePath(), &newName );
       
   110                 if( newName.isEmpty() ) {
       
   111                     ret = FmErrCancel;
       
   112                     break;
       
   113                 }
       
   114                 QString targetName = mTargetPath + newName;
       
   115                 destFi.setFile( targetName );
       
   116             }
       
   117         }
       
   118         if( ret != FmErrNone ) {
       
   119             return ret;
       
   120         }
       
   121         ret = copy( source, mTargetPath, newName );
       
   122         if( ret != FmErrNone ) {
       
   123             return ret;
       
   124         }
       
   125     }
       
   126     return FmErrNone;
       
   127 }
       
   128 
       
   129 
       
   130 
       
   131 int FmOperationCopy::copy( const QString &source, const QString &targetPath,
       
   132                           const QString &newTargetName )
       
   133 {        
       
   134     if( *mStop ) {
       
   135         return FmErrCancel;
       
   136     }
       
   137 
       
   138     QFileInfo fi( source );
       
   139     if( !fi.exists() ) {
       
   140         *mErrString = source;
       
   141         return FmErrSrcPathDoNotExist;
       
   142     }
       
   143     QString newName;
       
   144     if( !newTargetName.isEmpty() ) {
       
   145         newName = targetPath + newTargetName;
       
   146     } else {
       
   147         newName = targetPath + fi.fileName();
       
   148     }
       
   149 
       
   150     int ret = FmErrNone;
       
   151     
       
   152     if (fi.isFile()) {
       
   153         quint64 fileSize = fi.size();
       
   154         if ( !QFile::copy( source, newName )) {
       
   155             *mErrString = source;
       
   156             ret = FmErrCannotCopy;
       
   157         } else {
       
   158         IncreaseProgress( fileSize );
       
   159         }
       
   160     } else if (fi.isDir()) {
       
   161         ret = copyDirInsideContent( source, newName );
       
   162     } else {
       
   163         qWarning( "Things other than file and directory are not copied" );
       
   164         ret = FmErrIsNotFileOrFolder;
       
   165     }
       
   166 
       
   167     return ret;
       
   168 }
       
   169 
       
   170 
       
   171 
       
   172 int FmOperationCopy::copyDirInsideContent( const QString &srcPath, const QString &destPath )
       
   173 {
       
   174     if( destPath.contains( srcPath, Qt::CaseInsensitive ) ) {
       
   175         *mErrString = destPath;
       
   176         return FmErrCopyDestToSubFolderInSrc;
       
   177     }
       
   178 
       
   179     QFileInfo srcInfo( srcPath );
       
   180     if( !srcInfo.isDir() || !srcInfo.exists() ) {
       
   181         *mErrString = srcPath;
       
   182         return FmErrSrcPathDoNotExist;
       
   183     }
       
   184 
       
   185     QFileInfo destInfo( destPath );
       
   186     if( !destInfo.exists() ) {
       
   187         if( !destInfo.dir().mkdir( destInfo.absoluteFilePath() ) ) {
       
   188             *mErrString = destPath;
       
   189             return FmErrCannotMakeDir;
       
   190         }
       
   191     }    
       
   192 
       
   193     if( !srcInfo.isDir() ) {
       
   194         *mErrString = destPath;
       
   195         return FmErrCannotMakeDir;
       
   196     }
       
   197 
       
   198     //start to copy
       
   199     QFileInfoList infoList = QDir( srcPath ).entryInfoList( QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden | QDir::System );
       
   200     while( !infoList.isEmpty() ) {
       
   201         if( *mStop ) {
       
   202             return FmErrCancel;
       
   203         }
       
   204 
       
   205         QFileInfo fileInfo = infoList.takeFirst();
       
   206         if( fileInfo.isFile() ){
       
   207             //copy file
       
   208             QString newFilePath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
       
   209             if (!QFile::copy( fileInfo.absoluteFilePath(), newFilePath ) ) {
       
   210                 *mErrString = fileInfo.absoluteFilePath();
       
   211                 return FmErrCannotCopy;
       
   212             }
       
   213             IncreaseProgress( fileInfo.size() );
       
   214         } else if( fileInfo.isDir() ) {
       
   215             //makedir
       
   216             QString newDirPath = destPath + fileInfo.absoluteFilePath().mid( srcPath.length() );
       
   217             if( !QDir( newDirPath ).exists() && !QDir( destPath ).mkdir( newDirPath ) ) {
       
   218                 *mErrString = newDirPath;
       
   219                 return FmErrCannotMakeDir;
       
   220             }
       
   221             // add dir content to list.
       
   222             QFileInfoList infoListDir = QDir( fileInfo.absoluteFilePath() ).entryInfoList(
       
   223                 QDir::NoDotAndDotDot | QDir::AllEntries );
       
   224             while( !infoListDir.isEmpty() ) {
       
   225                 infoList.push_front( infoListDir.takeLast() );
       
   226             }
       
   227 
       
   228         } else {
       
   229             *mErrString = fileInfo.absoluteFilePath();
       
   230             return FmErrIsNotFileOrFolder;
       
   231         }
       
   232 
       
   233     }
       
   234 
       
   235     return FmErrNone;
       
   236 }
       
   237 
       
   238 void FmOperationCopy::IncreaseProgress( quint64 size )
       
   239 {
       
   240     if( mTotalSize <=0 ) {
       
   241         return;
       
   242     }
       
   243     mCopiedSize += size;
       
   244     int step = ( mCopiedSize * 100 ) / mTotalSize;
       
   245     if( step > mCurrentStep ) {
       
   246         mCurrentStep = step;
       
   247         emit notifyProgress( mCurrentStep );
       
   248     }
       
   249 }