filemanager/src/filemanager/src/operationservice/fmoperationremove.cpp
changeset 37 15bc28c9dd51
parent 16 ada7962b4308
child 46 d58987eac7e8
equal deleted inserted replaced
16:ada7962b4308 37:15bc28c9dd51
    24 #include <QDir>
    24 #include <QDir>
    25 #include <QFileInfo>
    25 #include <QFileInfo>
    26 #include <QStringList>
    26 #include <QStringList>
    27 #include <QStack>
    27 #include <QStack>
    28 
    28 
    29 FmOperationRemove::FmOperationRemove( QObject *parent, QStringList pathList ) :
    29 /* \fn  void driveSpaceChanged()
    30         FmOperationBase( parent, FmOperationService::EOperationTypeRemove ),
    30  * This signal is emitted when copy or move is completed, and used to update the drive size.
    31         mPathList( pathList ), mStop( 0 ), mTotalCount( 0 ),
    31  */
    32         mErrString( 0 ), mRemovedCount( 0 ), mTotalSteps( 100 ), mCurrentStep( 0 )
    32 
    33 {
    33 /*
    34 }
    34  * Constructs a remove operation with
    35 
    35  * \a parent parent
       
    36  * \a pathList the file or path to be removed.
       
    37  */
       
    38 FmOperationRemove::FmOperationRemove(QObject *parent, const QStringList &pathList ) :
       
    39         FmOperationBase( parent, FmOperationService::EOperationTypeRemove ),        
       
    40         mPathList( pathList ), 
       
    41         mStop( 0 ),
       
    42         mTotalCount( 0 ), mRemovedCount( 0 ), mTotalSteps( 100 ), mCurrentStep( 0 )
       
    43 {
       
    44     connect( this, SIGNAL( driveSpaceChanged() ),
       
    45                 parent, SLOT( on_operation_driveSpaceChanged() ) );
       
    46 }
       
    47 
       
    48 /*
       
    49  * Destructs the operation.
       
    50  */
    36 FmOperationRemove::~FmOperationRemove()
    51 FmOperationRemove::~FmOperationRemove()
    37 {
    52 {
    38 }
    53 }
    39 
    54 
       
    55 /*
       
    56  * Returns the path list
       
    57  */
    40 QStringList FmOperationRemove::pathList()
    58 QStringList FmOperationRemove::pathList()
    41 {
    59 {
    42     return mPathList;
    60     return mPathList;
    43 }
    61 }
    44 
    62 
    45 int FmOperationRemove::start( volatile bool *isStopped, QString *errString )
    63 /*
       
    64  * Starts the operation.
       
    65  * \a isStopped flag the outside stop operation
       
    66  */
       
    67 void FmOperationRemove::start( volatile bool *isStopped )
    46 {
    68 {
    47     mStop = isStopped;
    69     mStop = isStopped;
    48     mErrString = errString;
       
    49 
       
    50     mTotalCount   = 0;
    70     mTotalCount   = 0;
    51     mRemovedCount  = 0;
    71     mRemovedCount  = 0;
    52     mCurrentStep = 0;
    72     mCurrentStep = 0;
    53 
    73 
    54     if( mPathList.empty() ) {
    74     if( mPathList.empty() ) {
    55         return FmErrWrongParam;
    75         emit notifyError( FmErrWrongParam, mErrString );    
       
    76         return ;
    56     }
    77     }
    57 
    78 
    58     emit notifyPreparing( true );
    79     emit notifyPreparing( true );
    59 
    80 
    60     quint64 totalSize= 0; 
    81     quint64 totalSize= 0; 
    62     int numofFiles      = 0;
    83     int numofFiles      = 0;
    63 
    84 
    64     int ret = FmFolderDetails::queryDetailOfContentList( mPathList, numofFolders, 
    85     int ret = FmFolderDetails::queryDetailOfContentList( mPathList, numofFolders, 
    65         numofFiles, totalSize, mStop, true );
    86         numofFiles, totalSize, mStop, true );
    66     if( ret != FmErrNone ) {
    87     if( ret != FmErrNone ) {
    67         return ret;
    88         emit notifyError( ret, mErrString );
       
    89         return;
    68     }
    90     }
    69     mTotalCount = numofFolders + numofFiles;
    91     mTotalCount = numofFolders + numofFiles;
    70 
    92 
    71     emit notifyStart( true, mTotalSteps );
    93     emit notifyStart( true, mTotalSteps );
    72 
    94 
    73     foreach( const QString& srcPath, mPathList ) {
    95     foreach( const QString& srcPath, mPathList ) {
    74         int ret = remove( srcPath );
    96         int ret = remove( srcPath );
    75         if( ret != FmErrNone ) {
    97         if( ret != FmErrNone ) {
    76             return ret;
    98             emit notifyError( ret, mErrString );
       
    99             // refresh drive space no care if cancel, error or finished.
       
   100             // as filemanger cannot notify drive space changed
       
   101             // do not refresh path as QFileSystemModel will do auto-refresh
       
   102             emit driveSpaceChanged();
       
   103             return;
    77         }
   104         }
    78     }
   105     }
    79     return FmErrNone;
   106     emit notifyFinish();
    80 }
   107     emit driveSpaceChanged();
    81 
   108 }
       
   109 
       
   110 /*
       
   111  * Removes the file or dir with name \a fileName
       
   112  */
    82 int FmOperationRemove::remove( const QString &fileName )
   113 int FmOperationRemove::remove( const QString &fileName )
    83 {
   114 {
    84     if( *mStop ) {
   115     if( *mStop ) {
    85         return FmErrCancel;
   116         return FmErrCancel;
    86     }
   117     }
    87 
   118 
    88     int ret = FmErrNone;
   119     int ret = FmErrNone;
    89     QFileInfo fi( fileName );
   120     QFileInfo fi( fileName );
    90     if (fi.isFile()) {
   121     if (fi.isFile()) {
    91         if( !QFile::remove( fileName ) ) {
   122         if( !QFile::remove( fileName ) ) {
    92             *mErrString = fileName;
   123             mErrString = fileName;
    93             ret = FmErrCannotRemove;
   124             ret = FmErrCannotRemove;
    94         }
   125         }
    95         IncreaseProgressOnce();
   126         IncreaseProgressOnce();
    96     } else if (fi.isDir()) {
   127     } else if (fi.isDir()) {
    97        if( FmUtils::isDefaultFolder( fileName ) ){
   128        if( FmUtils::isDefaultFolder( fileName ) ){
   107         ret = FmErrIsNotFileOrFolder;
   138         ret = FmErrIsNotFileOrFolder;
   108     }
   139     }
   109     return ret;
   140     return ret;
   110 }
   141 }
   111 
   142 
       
   143 /*
       
   144  * Remove the dir with name \a pathName
       
   145  */
   112 int FmOperationRemove::recursiveRemoveDir( const QString &pathName )
   146 int FmOperationRemove::recursiveRemoveDir( const QString &pathName )
   113 {
   147 {
   114     QFileInfo fi( pathName );
   148     QFileInfo fi( pathName );
   115     if (!fi.exists() || !fi.isDir())
   149     if (!fi.exists() || !fi.isDir())
   116         return FmErrSrcPathDoNotExist;
   150         return FmErrSrcPathDoNotExist;
   121     while (!dirs.isEmpty()) {
   155     while (!dirs.isEmpty()) {
   122         QFileInfoList infoList = dirs.top().entryInfoList( QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden | QDir::System );
   156         QFileInfoList infoList = dirs.top().entryInfoList( QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden | QDir::System );
   123         if (infoList.size() == 0) {
   157         if (infoList.size() == 0) {
   124             QDir dirToRemove( dirs.pop() );
   158             QDir dirToRemove( dirs.pop() );
   125             if ( !dirToRemove.rmdir( dirToRemove.absolutePath() ) ) {
   159             if ( !dirToRemove.rmdir( dirToRemove.absolutePath() ) ) {
   126                 *mErrString = dirToRemove.absolutePath();
   160                 mErrString = dirToRemove.absolutePath();
   127                 return FmErrCannotRemove;
   161                 return FmErrCannotRemove;
   128             }
   162             }
   129             IncreaseProgressOnce();
   163             IncreaseProgressOnce();
   130 
   164 
   131         } else {
   165         } else {
   137 
   171 
   138                 if (it->isDir()) {
   172                 if (it->isDir()) {
   139                     dirList.push_front( QDir( it->absoluteFilePath() ) );
   173                     dirList.push_front( QDir( it->absoluteFilePath() ) );
   140                 } else {
   174                 } else {
   141                     if ( !QFile::remove( it->absoluteFilePath() ) ) {
   175                     if ( !QFile::remove( it->absoluteFilePath() ) ) {
   142                         *mErrString = it->absoluteFilePath();
   176                         mErrString = it->absoluteFilePath();
   143                         return FmErrCannotRemove;
   177                         return FmErrCannotRemove;
   144                     }
   178                     }
   145                     IncreaseProgressOnce();
   179                     IncreaseProgressOnce();
   146                 }
   180                 }
   147             }
   181             }
   151         }
   185         }
   152     }
   186     }
   153     return FmErrNone;
   187     return FmErrNone;
   154 }
   188 }
   155 
   189 
       
   190 /*
       
   191  * Increase the progress bar
       
   192  */
   156 void FmOperationRemove::IncreaseProgressOnce()
   193 void FmOperationRemove::IncreaseProgressOnce()
   157 {
   194 {
   158     if( mTotalCount <= 0 )
   195     if( mTotalCount <= 0 )
   159         return;
   196         return;
   160     mRemovedCount++;
   197     mRemovedCount++;