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