emailuis/nmailuiengine/src/nmoperation.cpp
changeset 43 99bcbff212ad
parent 40 2c62ef3caffd
equal deleted inserted replaced
42:139d4b7b2938 43:99bcbff212ad
    23  */
    23  */
    24 NmOperation::NmOperation()
    24 NmOperation::NmOperation()
    25 : mProgress(0),
    25 : mProgress(0),
    26   mIsRunning(true)
    26   mIsRunning(true)
    27 {
    27 {
       
    28     NM_FUNCTION;
       
    29     
    28     QMetaObject::invokeMethod(this, "runAsyncOperation", Qt::QueuedConnection);
    30     QMetaObject::invokeMethod(this, "runAsyncOperation", Qt::QueuedConnection);
    29 }
    31 }
    30 
    32 
    31 /*!
    33 /*!
    32     \brief Destructor
    34     \brief Destructor
    33  */
    35  */
    34 NmOperation::~NmOperation()
    36 NmOperation::~NmOperation()
    35 {
    37 {
       
    38     NM_FUNCTION;
       
    39     
    36     while (!mPreliminaryOperations.isEmpty()) {
    40     while (!mPreliminaryOperations.isEmpty()) {
    37         QPointer<NmOperation> operation = mPreliminaryOperations.takeLast();
    41         QPointer<NmOperation> operation = mPreliminaryOperations.takeLast();
    38         if (operation && operation->isRunning()) {
    42         if (operation && operation->isRunning()) {
    39             operation->cancelOperation();
    43             operation->cancelOperation();
    40         }
    44         }
    44 /*!
    48 /*!
    45     \brief Tells whether the operation is running or not
    49     \brief Tells whether the operation is running or not
    46  */
    50  */
    47 bool NmOperation::isRunning() const
    51 bool NmOperation::isRunning() const
    48 {
    52 {
       
    53     NM_FUNCTION;
       
    54     
    49     return mIsRunning;
    55     return mIsRunning;
    50 }
    56 }
    51 
    57 
    52 /*!
    58 /*!
    53     \brief Adds a "preliminary operation" which needs to end until this operation can start.
    59     \brief Adds a "preliminary operation" which needs to end until this operation can start.
    54  */
    60  */
    55 void NmOperation::addPreliminaryOperation(NmOperation *operation)
    61 void NmOperation::addPreliminaryOperation(NmOperation *operation)
    56 {
    62 {
       
    63     NM_FUNCTION;
       
    64     
    57     // if the preliminary operation is already completed
    65     // if the preliminary operation is already completed
    58     // the input parameter can be null
    66     // the input parameter can be null
    59     if (operation && operation->isRunning()) {
    67     if (operation && operation->isRunning()) {
    60         connect(operation, SIGNAL(operationCompleted()), this,
    68         connect(operation, SIGNAL(operationCompleted()), this,
    61             SLOT(handlePreliminaryOperationFinished()));
    69             SLOT(handlePreliminaryOperationFinished()));
    73     the operation is completed, this will emit the actionCompleted signal * 
    81     the operation is completed, this will emit the actionCompleted signal * 
    74     \param result Result from operation
    82     \param result Result from operation
    75  */
    83  */
    76 void NmOperation::completeOperation(int result)
    84 void NmOperation::completeOperation(int result)
    77 {
    85 {
       
    86     NM_FUNCTION;
       
    87     
    78     mIsRunning = false;
    88     mIsRunning = false;
    79     // Operation is completed, emit the signal
    89     // Operation is completed, emit the signal
    80     doCompleteOperation();
    90     doCompleteOperation();
    81     emit this->operationCompleted(result);
    91     emit this->operationCompleted(result);
    82     QMetaObject::invokeMethod(this, "deleteOperation", Qt::QueuedConnection);
    92     QMetaObject::invokeMethod(this, "deleteOperation", Qt::QueuedConnection);
    87     The observer of the asynchronous function call should use this slot if it
    97     The observer of the asynchronous function call should use this slot if it
    88     wants to cancel the ongoing operation, it will emit the actionCancelled signal
    98     wants to cancel the ongoing operation, it will emit the actionCancelled signal
    89  */
    99  */
    90 void NmOperation::cancelOperation()
   100 void NmOperation::cancelOperation()
    91 {
   101 {
       
   102     NM_FUNCTION;
       
   103     
    92     mIsRunning = false;
   104     mIsRunning = false;
    93     // Operation is canceled, emit the signal
   105     // Operation is canceled, emit the signal
    94     this->doCancelOperation();
   106     this->doCancelOperation();
    95     emit this->operationCancelled();
   107     emit this->operationCancelled();
    96     QMetaObject::invokeMethod(this, "deleteOperation", Qt::QueuedConnection);
   108     QMetaObject::invokeMethod(this, "deleteOperation", Qt::QueuedConnection);
   100     The performer of the asynchronous function call should use this slot when
   112     The performer of the asynchronous function call should use this slot when
   101     updating the operation progress, this will emit the progressChanged signal
   113     updating the operation progress, this will emit the progressChanged signal
   102  */
   114  */
   103 void NmOperation::updateOperationProgress(int progress)
   115 void NmOperation::updateOperationProgress(int progress)
   104 {
   116 {
       
   117     NM_FUNCTION;
       
   118     
   105     mProgress = progress;
   119     mProgress = progress;
   106     this->doUpdateOperationProgress();
   120     this->doUpdateOperationProgress();
   107     emit this->operationProgressChanged(mProgress);
   121     emit this->operationProgressChanged(mProgress);
   108 }
   122 }
   109 
   123 
   113     operations running. Operation will be deleted after it has compeleted or cancelled and the control
   127     operations running. Operation will be deleted after it has compeleted or cancelled and the control
   114     returns to the event loop.
   128     returns to the event loop.
   115  */
   129  */
   116 void NmOperation::runAsyncOperation()
   130 void NmOperation::runAsyncOperation()
   117 {
   131 {
       
   132     NM_FUNCTION;
       
   133     
   118     int count = mPreliminaryOperations.count();
   134     int count = mPreliminaryOperations.count();
   119     int ready = 0;
   135     int ready = 0;
   120     // go through preliminary operations
   136     // go through preliminary operations
   121     for (int i = 0; i < count; ++i) {
   137     for (int i = 0; i < count; ++i) {
   122         if (!mPreliminaryOperations[i] || !mPreliminaryOperations[i]->isRunning()) {
   138         if (!mPreliminaryOperations[i] || !mPreliminaryOperations[i]->isRunning()) {
   135     This is signalled by a preliminary operation when its operation is completed or cancelled. 
   151     This is signalled by a preliminary operation when its operation is completed or cancelled. 
   136     Do not call runAsyncOperation immediately but let the signal be handled by other slots first. 
   152     Do not call runAsyncOperation immediately but let the signal be handled by other slots first. 
   137  */
   153  */
   138 void NmOperation::handlePreliminaryOperationFinished()
   154 void NmOperation::handlePreliminaryOperationFinished()
   139 {
   155 {
       
   156     NM_FUNCTION;
       
   157     
   140     QMetaObject::invokeMethod(this, "runAsyncOperation", Qt::QueuedConnection);
   158     QMetaObject::invokeMethod(this, "runAsyncOperation", Qt::QueuedConnection);
   141 }
   159 }
   142 
   160 
   143 /*!
   161 /*!
   144     \brief Virtual function to be implemented by subclasses
   162     \brief Virtual function to be implemented by subclasses
   146     emitted. Derived class can override this is some special actions
   164     emitted. Derived class can override this is some special actions
   147     are needed.
   165     are needed.
   148  */ 
   166  */ 
   149 void NmOperation::doCompleteOperation()
   167 void NmOperation::doCompleteOperation()
   150 {
   168 {
   151     
   169     NM_FUNCTION;
   152 }
   170 }
   153 
   171 
   154 /*!
   172 /*!
   155     \brief Virtual function to be implemented by subclasses
   173     \brief Virtual function to be implemented by subclasses
   156     This function is called from the cancelAction after the trigger timer
   174     This function is called from the cancelAction after the trigger timer
   157     and before the signal is emitted. Derived class can override this
   175     and before the signal is emitted. Derived class can override this
   158     and put its own cancellation operations here.
   176     and put its own cancellation operations here.
   159  */
   177  */
   160 void NmOperation::doCancelOperation()
   178 void NmOperation::doCancelOperation()
   161 {
   179 {
       
   180     NM_FUNCTION;
   162 }
   181 }
   163 
   182 
   164 /*!
   183 /*!
   165     \brief Virtual function to be implemented by subclasses
   184     \brief Virtual function to be implemented by subclasses
   166     This function is called from the updateProgress after the progress
   185     This function is called from the updateProgress after the progress
   167     value has been updated and before the signal is emitted. Derived class
   186     value has been updated and before the signal is emitted. Derived class
   168     can override this.
   187     can override this.
   169  */
   188  */
   170 void NmOperation::doUpdateOperationProgress()
   189 void NmOperation::doUpdateOperationProgress()
   171 {
   190 {
       
   191     NM_FUNCTION;
   172 }
   192 }
   173 
   193 
   174 /*!
   194 /*!
   175     \brief Private slot for operation deleting
   195     \brief Private slot for operation deleting
   176     This is signalled when operation is completed or cancelled. 
   196     This is signalled when operation is completed or cancelled. 
   177     Delete happens when the control returns to the event loop.
   197     Delete happens when the control returns to the event loop.
   178  */
   198  */
   179 void NmOperation::deleteOperation()
   199 void NmOperation::deleteOperation()
   180 {
   200 {
       
   201     NM_FUNCTION;
       
   202     
   181     this->deleteLater();   
   203     this->deleteLater();   
   182 }
   204 }
   183 
   205