filemanager/src/filemanager/src/operationservice/fmoperationservice.cpp
branchRCL_3
changeset 20 491b3ed49290
equal deleted inserted replaced
19:95243422089a 20:491b3ed49290
       
     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  * 
       
    15  * Description:
       
    16  *     The source file of the operation service of file manager
       
    17  */
       
    18 
       
    19 #include "fmoperationservice.h"
       
    20 #include "fmoperationthread.h"
       
    21 #include "fmbackupconfigloader.h"
       
    22 #include "fmbkupengine.h"
       
    23 #include "fmbackupsettings.h"
       
    24 #include "fmviewdetailsdialog.h"
       
    25 #include "fmoperationresultprocesser.h"
       
    26 #include "fmoperationcopyormove.h"
       
    27 #include "fmoperationremove.h"
       
    28 #include "fmoperationformat.h"
       
    29 #include "fmoperationviewdetails.h"
       
    30 #include "fmbackuprestorehandler.h"
       
    31 #include <hbaction.h>
       
    32 
       
    33 /* \fn void driveSpaceChanged( FmOperationBase* operationBase )
       
    34  * This signal is emitted when disk size changed.
       
    35  */
       
    36 
       
    37 /* \fn void notifyWaiting( FmOperationBase* operationBase, bool cancelable )
       
    38  * This signal is emitted when the operation emits notifyWaiting.
       
    39  */
       
    40 
       
    41 /* \fn void notifyPreparing( FmOperationBase* operationBase, bool cancelable )
       
    42  * This signal is emitted when the operation emits notifyPreparing.
       
    43  */
       
    44 
       
    45 /* \fn void notifyStart( FmOperationBase* operationBase, bool cancelable, int maxSteps )
       
    46  * This signal is emitted when the operation emits notifyStart.
       
    47  */
       
    48 
       
    49 /* \fn void notifyProgress( FmOperationBase* operationBase, int currentStep )
       
    50  * This signal is emitted when the operation emits notifyProgress.
       
    51  */
       
    52     
       
    53 /* \fn void notifyFinish( FmOperationBase* operationBase )
       
    54  * This signal is emitted when the operation emits notifyFinish.
       
    55  */ 
       
    56     
       
    57 /* \fn void notifyError( FmOperationBase* operationBase, int error, QString errString )
       
    58  * This signal is emitted when the operation emits notifyError.
       
    59  */ 
       
    60  
       
    61 /* \fn void notifyCanceled( FmOperationBase* operationBase )
       
    62  * This signal is emitted when the operation emits notifyCanceled.
       
    63  */ 
       
    64 
       
    65 /*
       
    66  * Constructs one operation Service with \a parent.
       
    67  */
       
    68 FmOperationService::FmOperationService( QObject *parent ) : QObject( parent ),
       
    69         mCurrentOperation( 0 ), mBackupRestoreHandler( 0 )
       
    70 {
       
    71     mThread = new FmOperationThread( this );
       
    72     mThread->setObjectName( "operationThread" );
       
    73     mOperationResultProcesser = new FmOperationResultProcesser( this );
       
    74     
       
    75     QMetaObject::connectSlotsByName( this );
       
    76 }
       
    77 
       
    78 /*
       
    79  * Destructs the operation service.
       
    80  */
       
    81 FmOperationService::~FmOperationService()
       
    82 {
       
    83     delete mThread;    
       
    84     mThread = 0;
       
    85     delete mBackupRestoreHandler;
       
    86     mBackupRestoreHandler = 0;
       
    87 }
       
    88 
       
    89 /*
       
    90  * Returns true if the thread is running, false if not.
       
    91  */
       
    92 bool FmOperationService::isRunning()
       
    93 {
       
    94     return mThread->isRunning();    
       
    95 }
       
    96 
       
    97 /*
       
    98  * Copies the file or foler \a targetPath asynchronously. 
       
    99  */
       
   100 int FmOperationService::asyncCopy( const QStringList &sourceList, const QString &targetPath )
       
   101 {    
       
   102     Q_ASSERT( !mCurrentOperation ); 
       
   103     mCurrentOperation  = new FmOperationCopyOrMove( this, FmOperationService::EOperationTypeCopy, sourceList, targetPath );
       
   104     connectSignalsAndSlots( mCurrentOperation );
       
   105     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   106     if ( ret!= FmErrNone ) {
       
   107         resetOperation();
       
   108         return ret; 
       
   109     }        
       
   110     return FmErrNone;
       
   111 }
       
   112 
       
   113 /*
       
   114  * Moves the file or foler \a sourceList to \a targetPath asynchronously. 
       
   115  */
       
   116 int FmOperationService::asyncMove( const QStringList &sourceList, const QString &targetPath )
       
   117 {
       
   118     Q_ASSERT( !mCurrentOperation );
       
   119 
       
   120     mCurrentOperation  = new FmOperationCopyOrMove( this, FmOperationService::EOperationTypeMove, sourceList, targetPath );
       
   121     connectSignalsAndSlots( mCurrentOperation );
       
   122     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   123     if ( ret!= FmErrNone ) {
       
   124         resetOperation();
       
   125         return ret; 
       
   126     }        
       
   127     return FmErrNone;
       
   128 }
       
   129 
       
   130 /*
       
   131  * Removes the file or dir \a pathList asynchronously. 
       
   132  */
       
   133 int FmOperationService::asyncRemove( const QStringList &pathList )
       
   134 {
       
   135     Q_ASSERT( !mCurrentOperation ); 
       
   136 
       
   137     mCurrentOperation = new FmOperationRemove( this, pathList );
       
   138     connectSignalsAndSlots( mCurrentOperation );
       
   139     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   140     if ( ret!= FmErrNone ) {
       
   141         resetOperation();
       
   142         return ret; 
       
   143     }        
       
   144     return FmErrNone;
       
   145 }
       
   146 
       
   147 /*
       
   148  * Formats the drive \a driverName asynchronously. 
       
   149  */
       
   150 int FmOperationService::asyncFormat( const QString &driverName )
       
   151 {
       
   152     Q_ASSERT( !mCurrentOperation );
       
   153 
       
   154     mCurrentOperation = new FmOperationFormat( this, driverName );
       
   155     connectSignalsAndSlots( mCurrentOperation );
       
   156     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   157     if ( ret!= FmErrNone ) {
       
   158         resetOperation();
       
   159         return ret; 
       
   160     }        
       
   161     return FmErrNone;
       
   162 }
       
   163 
       
   164 /*
       
   165  * Views drive \a driverName details asynchronously.
       
   166  */
       
   167 int FmOperationService::asyncViewDriveDetails( const QString &driverName )
       
   168 {
       
   169     Q_ASSERT( !mCurrentOperation );
       
   170     
       
   171     mCurrentOperation = new FmOperationDriveDetails( this, driverName );
       
   172     connectSignalsAndSlots( mCurrentOperation );
       
   173     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   174     if ( ret!= FmErrNone ) {
       
   175         resetOperation();
       
   176         return ret; 
       
   177     }        
       
   178     return FmErrNone;
       
   179 }
       
   180 
       
   181 /*
       
   182  * Views folder \a folderPath details asynchronously. 
       
   183  */
       
   184 int FmOperationService::asyncViewFolderDetails( const QString &folderPath )
       
   185 {
       
   186     Q_ASSERT( !mCurrentOperation );
       
   187     
       
   188     mCurrentOperation = new FmOperationFolderDetails( this, folderPath );
       
   189     connectSignalsAndSlots( mCurrentOperation );
       
   190     int ret = mThread->prepareOperationAndStart( mCurrentOperation );    
       
   191     if ( ret!= FmErrNone ) {
       
   192         resetOperation();
       
   193         return ret; 
       
   194     }        
       
   195     return FmErrNone;
       
   196 }
       
   197 
       
   198 /*
       
   199  * Backups asynchronously. 
       
   200  */
       
   201 int FmOperationService::asyncBackup()
       
   202 {   
       
   203     if ( isRunning() )
       
   204         return FmErrAlreadyStarted;
       
   205     Q_ASSERT( !mCurrentOperation );
       
   206 
       
   207     // BackupSettingsL will not leave, coding convention will be improvied in another task.
       
   208     QString targetDrive( backupRestoreHandler()->bkupEngine()->BackupSettingsL()->availableTargetDrive() );
       
   209     quint32 content( backupRestoreHandler()->bkupEngine()->BackupSettingsL()->content() );
       
   210     FmOperationBackup *operationBackup = 
       
   211             new FmOperationBackup( backupRestoreHandler(), targetDrive, content );
       
   212     mCurrentOperation = operationBackup;
       
   213     int ret = backupRestoreHandler()->startBackup( operationBackup );
       
   214     if( ret ){
       
   215         return FmErrNone;
       
   216     } else {
       
   217         resetOperation();
       
   218         return backupRestoreHandler()->error();
       
   219     }
       
   220 }
       
   221 
       
   222 /*
       
   223  * Restores asynchronously. 
       
   224  * \a selection selected restore items
       
   225  */
       
   226 int FmOperationService::asyncRestore( quint64 selection )
       
   227 {
       
   228     if ( isRunning() )
       
   229         return FmErrAlreadyStarted;
       
   230     Q_ASSERT( !mCurrentOperation );
       
   231 
       
   232     FmOperationRestore* operationRestore = new FmOperationRestore( mBackupRestoreHandler, selection );
       
   233     mCurrentOperation = operationRestore;
       
   234     int ret = backupRestoreHandler()->startRestore( operationRestore );
       
   235     if( ret ){
       
   236         return FmErrNone;
       
   237     } else {
       
   238         resetOperation();
       
   239         return backupRestoreHandler()->error();
       
   240     }
       
   241 }
       
   242 
       
   243 /*
       
   244  * Delete backup synchronously. 
       
   245  * \a selection selected backup items
       
   246  */
       
   247 int FmOperationService::syncDeleteBackup( quint64 selection )
       
   248 {
       
   249     return backupRestoreHandler()->deleteBackup( selection );
       
   250 }
       
   251 
       
   252 /*
       
   253  * Cancels current operation.
       
   254  */
       
   255 void FmOperationService::cancelOperation()
       
   256 {
       
   257     switch( mCurrentOperation->operationType() )
       
   258     {
       
   259     case  EOperationTypeBackup:
       
   260         backupRestoreHandler()->cancelBackup();
       
   261         break;
       
   262     case EOperationTypeDriveDetails:
       
   263         mThread->stop();
       
   264         break;
       
   265     case EOperationTypeFolderDetails:
       
   266         mThread->stop();
       
   267         break;
       
   268     case EOperationTypeFormat:
       
   269         //can not cancel format
       
   270         break;
       
   271     case EOperationTypeCopy:
       
   272          mThread->stop();
       
   273         break;
       
   274     case EOperationTypeMove:
       
   275          mThread->stop();
       
   276         break;
       
   277     case EOperationTypeRemove:
       
   278          mThread->stop();
       
   279         break;
       
   280     default:
       
   281         Q_ASSERT( false );
       
   282     }    
       
   283 }
       
   284 
       
   285 /*
       
   286  * Set valume synchronously. not used.
       
   287  */
       
   288 int FmOperationService::syncSetVolume( const QString &driverName, const QString &volume )
       
   289 {
       
   290     Q_UNUSED( driverName );
       
   291     Q_UNUSED( volume );
       
   292     return FmErrNone;
       
   293 }
       
   294 
       
   295 /*
       
   296  * Set drive password synchronously. not used.
       
   297  */
       
   298 int FmOperationService::syncSetdDriverPassword( const QString &driverName,
       
   299                                                const QString &oldPassword, 
       
   300                                                const QString &newPassword )
       
   301 {
       
   302     Q_UNUSED( driverName );
       
   303     Q_UNUSED( oldPassword );
       
   304     Q_UNUSED( newPassword );
       
   305     return FmErrNone;
       
   306 }
       
   307 
       
   308 /*
       
   309  * Rename synchronously. not used.
       
   310  */
       
   311 int FmOperationService::syncRename( const QString &oldPath, const QString &newName )
       
   312 {
       
   313     Q_UNUSED( oldPath );
       
   314     Q_UNUSED( newName );
       
   315     return FmErrNone;
       
   316 }
       
   317 
       
   318 /*
       
   319  * Launches the file in synchronous way.
       
   320  */
       
   321 int FmOperationService::syncLaunchFileOpen( const QString &filePath )
       
   322 {
       
   323     return FmUtils::launchFile( filePath );
       
   324 }
       
   325 
       
   326 /*
       
   327  * Returns the backup handler.
       
   328  */
       
   329 FmBackupRestoreHandler *FmOperationService::backupRestoreHandler()
       
   330 {
       
   331     if( !mBackupRestoreHandler ) {
       
   332         mBackupRestoreHandler = new FmBackupRestoreHandler( this );        
       
   333         mBackupRestoreHandler->setObjectName( "backupRestore" ) ;
       
   334         QMetaObject::connectSlotsByName( this );
       
   335     }
       
   336     return mBackupRestoreHandler;
       
   337 }
       
   338 
       
   339 /*
       
   340  * Deletes the operation and set it to be 0.
       
   341  */
       
   342 void FmOperationService::resetOperation()
       
   343 {
       
   344    if( mCurrentOperation ) {
       
   345         delete mCurrentOperation;
       
   346         mCurrentOperation = 0;
       
   347     }
       
   348 }
       
   349 
       
   350 /*
       
   351  * Connects \a operation's sinals to slots
       
   352  */
       
   353 void FmOperationService::connectSignalsAndSlots( FmOperationBase *operation )
       
   354 {
       
   355     
       
   356     connect( operation, SIGNAL( showNote( QString ) ),
       
   357             this, SLOT( on_operation_showNote( QString )), Qt::BlockingQueuedConnection );
       
   358     connect( operation, SIGNAL( notifyError( int, QString ) ),
       
   359             this, SLOT( on_operation_notifyError( int, QString ) ) );
       
   360     connect( operation, SIGNAL( notifyStart( bool, int ) ),
       
   361             this, SLOT( on_operation_notifyStart( bool, int ) ) );
       
   362     connect( operation, SIGNAL( notifyProgress( int ) ),
       
   363             this, SLOT( on_operation_notifyProgress( int ) ) );
       
   364     connect( operation, SIGNAL( notifyFinish() ),
       
   365             this, SLOT( on_operation_notifyFinish()) );
       
   366     connect( operation, SIGNAL( notifyWaiting( bool ) ),
       
   367             this, SLOT( on_operation_notifyWaiting( bool )) );   
       
   368     
       
   369 }
       
   370 
       
   371 /*
       
   372  * Responds to mCurrentOperation's askForRename signal.
       
   373  * \a srcFile the source file.
       
   374  * \a destFile the new file name.
       
   375  */
       
   376 void FmOperationService::on_operation_askForRename( const QString &srcFile, QString *destFile )
       
   377 {
       
   378     mOperationResultProcesser->onAskForRename(
       
   379         mCurrentOperation, srcFile, destFile );
       
   380 }
       
   381 
       
   382 /*
       
   383  * Responds to mCurrentOperation's askForReplace signal.
       
   384  * \a srcFile the source file.
       
   385  * \a destFile the target file.
       
   386  * \a isAccepted whether to replace the target file.
       
   387  */
       
   388 void FmOperationService::on_operation_askForReplace( const QString &srcFile, const QString &destFile, bool *isAccepted )
       
   389 {
       
   390     mOperationResultProcesser->onAskForReplace(
       
   391         mCurrentOperation, srcFile, destFile, isAccepted );
       
   392 }
       
   393 
       
   394 /*
       
   395  * Responds to mCurrentOperation's showNote signal.
       
   396  * \a noteString the note content.
       
   397  */
       
   398 void FmOperationService::on_operation_showNote( const char *noteString )
       
   399 {
       
   400     mOperationResultProcesser->onShowNote( mCurrentOperation, noteString );
       
   401 }
       
   402 
       
   403 /*
       
   404  * Responds to mCurrentOperation's showNote signal.
       
   405  * \a noteString the note content.
       
   406  */
       
   407 void FmOperationService::on_operation_notifyWaiting( bool cancelable )
       
   408 {
       
   409     mOperationResultProcesser->onNotifyWaiting(
       
   410         mCurrentOperation, cancelable );
       
   411     emit notifyWaiting( mCurrentOperation, cancelable );
       
   412 }
       
   413 
       
   414 /*
       
   415  * Responds to mCurrentOperation's notifyPreparing signal.
       
   416  * \a cancelable indicates whether the progress bar could be cancelled.
       
   417  */
       
   418 void FmOperationService::on_operation_notifyPreparing( bool cancelable )
       
   419 {
       
   420     mOperationResultProcesser->onNotifyPreparing(
       
   421         mCurrentOperation, cancelable );
       
   422     emit notifyPreparing( mCurrentOperation, cancelable );
       
   423 }
       
   424 
       
   425 /*
       
   426  * Responds to mCurrentOperation's notifyPreparing signal.
       
   427  * \a cancelable indicates whether the progress bar could be cancelled.
       
   428  * \maxSteps the length of progress bar.
       
   429  */
       
   430 void FmOperationService::on_operation_notifyStart( bool cancelable, int maxSteps )
       
   431 {
       
   432     mOperationResultProcesser->onNotifyStart(
       
   433         mCurrentOperation, cancelable, maxSteps );
       
   434     emit notifyStart( mCurrentOperation, cancelable, maxSteps );
       
   435 }
       
   436 
       
   437 /*
       
   438  * Responds to mCurrentOperation's notifyPreparing signal.
       
   439  * \a currentStep indicates the current length of progress bar.
       
   440  */
       
   441 void FmOperationService::on_operation_notifyProgress( int currentStep )
       
   442 {
       
   443     mOperationResultProcesser->onNotifyProgress(
       
   444         mCurrentOperation, currentStep );
       
   445     emit notifyProgress( mCurrentOperation, currentStep );
       
   446 }
       
   447 
       
   448 /*
       
   449  * Responds to mCurrentOperation's notifyFinish signal, indicate the
       
   450  * progress is over.
       
   451  */
       
   452 void FmOperationService::on_operation_notifyFinish()
       
   453 {
       
   454     mOperationResultProcesser->onNotifyFinish( mCurrentOperation );
       
   455     emit notifyFinish( mCurrentOperation );
       
   456     resetOperation();
       
   457 }
       
   458 
       
   459 /*
       
   460  * Responds to mCurrentOperation's notifyError signal.
       
   461  * \a error error id.
       
   462  * \a errString the error string.
       
   463  */
       
   464 void FmOperationService::on_operation_notifyError(int error, QString errString )
       
   465 {
       
   466     mOperationResultProcesser->onNotifyError(
       
   467         mCurrentOperation, error, errString );
       
   468     emit notifyError( mCurrentOperation, error, errString );
       
   469     resetOperation();
       
   470 }
       
   471 
       
   472 /*
       
   473  * Responds to mCurrentOperation's driveSpaceChanged 
       
   474  */
       
   475 void FmOperationService::on_operation_driveSpaceChanged()
       
   476 {
       
   477     emit driveSpaceChanged( mCurrentOperation );
       
   478 }
       
   479 
       
   480 /*
       
   481  * Responds to mBackupRestoreHandler's notifyPreparing 
       
   482  * \a cancelable indicates whether it could be cancelled.
       
   483  */
       
   484 void FmOperationService::on_backupRestore_notifyPreparing( bool cancelable )
       
   485 {
       
   486     mOperationResultProcesser->onNotifyPreparing(
       
   487         mCurrentOperation, cancelable );
       
   488      emit notifyPreparing( mCurrentOperation, cancelable );
       
   489 }
       
   490 
       
   491 /*
       
   492  * Responds to mBackupRestoreHandler's notifyStart 
       
   493  * \a cancelable indicates whether it could be cancelled.
       
   494  * \a maxSteps the lenth of progress bar.
       
   495  */
       
   496 void FmOperationService::on_backupRestore_notifyStart( bool cancelable, int maxSteps )
       
   497 {
       
   498     mOperationResultProcesser->onNotifyStart(
       
   499         mCurrentOperation, cancelable, maxSteps );
       
   500     emit notifyStart( mCurrentOperation, cancelable, maxSteps );
       
   501 }
       
   502 
       
   503 /*
       
   504  * Responds to mBackupRestoreHandler's notifyProgress 
       
   505  * \a currentStep the current progress bar's step.
       
   506  */
       
   507 void FmOperationService::on_backupRestore_notifyProgress( int currentStep )
       
   508 {
       
   509     mOperationResultProcesser->onNotifyProgress(
       
   510         mCurrentOperation, currentStep );
       
   511     emit notifyProgress( mCurrentOperation, currentStep );
       
   512 }
       
   513 
       
   514 /*
       
   515  * Responds to mBackupRestoreHandler's notifyFinish 
       
   516  */
       
   517 void FmOperationService::on_backupRestore_notifyFinish()
       
   518 {
       
   519     mOperationResultProcesser->onNotifyFinish( mCurrentOperation );
       
   520     emit notifyFinish( mCurrentOperation );
       
   521     resetOperation();
       
   522 }
       
   523 
       
   524 /*
       
   525  * Responds to mBackupRestoreHandler's notifyError
       
   526  * \a error the error id.
       
   527  * \a errString the error string.
       
   528  */
       
   529 void FmOperationService::on_backupRestore_notifyError(int error, const QString &errString )
       
   530 {
       
   531     mOperationResultProcesser->onNotifyError(
       
   532         mCurrentOperation, error, errString );
       
   533     emit notifyError( mCurrentOperation, error, errString );
       
   534     resetOperation();
       
   535 }
       
   536 
       
   537 /*
       
   538  * Responds to mBackupRestoreHandler's notifyCanceled 
       
   539  */
       
   540 void FmOperationService::on_backupRestore_notifyCanceled()
       
   541 {
       
   542     mOperationResultProcesser->onNotifyError(
       
   543         mCurrentOperation, FmErrCancel, QString() );
       
   544     emit notifyError( mCurrentOperation, FmErrCancel, QString() );
       
   545     resetOperation();
       
   546 }
       
   547 
       
   548 ///
       
   549 /////////////////////////////////////////////////////