emailuis/nmailuiengine/src/nmoperation.cpp
changeset 20 ecc8def7944a
parent 18 578830873419
child 23 2dc6caa42ec3
--- a/emailuis/nmailuiengine/src/nmoperation.cpp	Fri Apr 16 14:51:52 2010 +0300
+++ b/emailuis/nmailuiengine/src/nmoperation.cpp	Mon May 03 12:23:15 2010 +0300
@@ -30,18 +30,21 @@
 mIsRunning(true)
 {
     // operation is started immediately
-    mTimer = new QTimer(this);
-    mTimer->setSingleShot(TRUE);
+    mTimer = new QTimer(this); // QObject takes care of deleting
+    mTimer->setSingleShot(true);
     connect(mTimer, SIGNAL(timeout()), this, SLOT(runAsyncOperation()));
     mTimer->start(1);
 }
 
 /*!
     \brief Destructor
-    Does nothing
  */
 NmOperation::~NmOperation()
 {
+    // Delete items from the mPreliminaryOperations list.
+    qDeleteAll(mPreliminaryOperations.begin(), mPreliminaryOperations.end());
+    // Remove the items.
+    mPreliminaryOperations.clear();
 }
 
 /*!
@@ -53,6 +56,21 @@
 }
 
 /*!
+    \brief Adds a "preliminary operation" which needs to end until this operation can start.
+    Ownership of the operation is transferred to this.
+ */
+void NmOperation::addPreliminaryOperation(NmOperation *operation)
+{
+    connect(operation, SIGNAL(operationCompleted()), this,
+        SLOT(handlePreliminaryOperationFinished()));
+
+    connect(operation, SIGNAL(operationCancelled()), this,
+        SLOT(handlePreliminaryOperationFinished()));
+
+    mPreliminaryOperations.append(operation);
+}
+
+/*!
     \brief Slot, complete
     The performer of the asynchronous function call should use this slot when
     the operation is completed, this will emit the actionCompleted signal * 
@@ -92,6 +110,39 @@
 }
 
 /*!
+    \brief Slot, run the operation if no preliminary operations are running.
+    Calls the pure virtual doRunAsyncOperation() of the derived class if there are no preliminary
+    operations running.
+ */
+void NmOperation::runAsyncOperation()
+{
+    // cleanup the preliminary operations
+    int count = mPreliminaryOperations.count();
+    for (int i = 0; i < count; ++i) {
+        if (!mPreliminaryOperations[i] || !mPreliminaryOperations[i]->isRunning()) {
+            delete mPreliminaryOperations.takeAt(i);
+            --i;
+            --count;
+        }
+    }
+
+    if (mPreliminaryOperations.count() == 0) {
+        doRunAsyncOperation();
+    }
+}
+
+/*!
+    \brief Slot, update progress
+    This is signalled by a preliminary operation when its operation is completed or cancelled. 
+    Do not call runAsyncOperation immediately but let the signal be handled by other slots first. 
+ */
+void NmOperation::handlePreliminaryOperationFinished()
+{
+    mTimer->stop();
+    mTimer->start(1);
+}
+
+/*!
     \brief Virtual function to be implemented by subclasses
     This function is called from the completeAction before the signal is
     emitted. Derived class can override this is some special actions