From 23f5d4229caea32af49c007de85563158630b5d4 Mon Sep 17 00:00:00 2001
From: tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>
Date: Fri, 4 Jun 2010 11:48:18 +0000
Subject: Multiple stability and usability fixes for the caldav/carddav
 resources

git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1134465 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
---
 kresources/caldav/resource.cpp  | 44 +++++++++++++++-------
 kresources/carddav/resource.cpp | 83 +++++++++++++++++++++++++++--------------
 kresources/carddav/resource.h   |  5 +++
 3 files changed, 91 insertions(+), 41 deletions(-)

diff --git a/kresources/caldav/resource.cpp b/kresources/caldav/resource.cpp
index 2562a8376..e5571ab99 100644
--- a/kresources/caldav/resource.cpp
+++ b/kresources/caldav/resource.cpp
@@ -95,15 +95,19 @@ ResourceCalDav::~ResourceCalDav() {
         mWriteRetryTimer->stop();	// Unfortunately we cannot do anything at this point; if this timer is still running something is seriously wrong
     }
 
+    if (mLoader) {
+        readLockout = true;
+        mLoader->terminate();
+        mLoader->wait(TERMINATION_WAITING_TIME);
+        mLoadingQueueReady = true;
+    }
+
     while ((mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady) {
         readLockout = true;
         sleep(1);
         qApp->processEvents(QEventLoop::ExcludeUserInput);
     }
 
-    if (mLoader) {
-        mLoader->terminate();
-    }
     if (mWriter) {
         mWriter->terminate();
     }
@@ -128,7 +132,8 @@ ResourceCalDav::~ResourceCalDav() {
 }
 
 bool ResourceCalDav::isSaving() {
-    return (((mWriteRetryTimer != NULL) ? mWriteRetryTimer->isActive() : 0) || (mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady);
+    doSave();
+    return (((mWriteRetryTimer != NULL) ? 1 : 0) || (mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady || readLockout);
 }
 
 /*=========================================================================
@@ -138,7 +143,7 @@ bool ResourceCalDav::isSaving() {
 bool ResourceCalDav::doLoad() {
     bool syncCache = true;
 
-    if ((mLoadingQueueReady == false) || (mLoadingQueue.isEmpty() == false) || (mLoader->running() == true) || (readLockout == true)) {
+    if ((mLoadingQueueReady == false) || (mLoadingQueue.isEmpty() == false) || (mLoader->running() == true) || (isSaving() == true)) {
         return true;	// Silently fail; the user has obviously not responded to a dialog and we don't need to pop up more of them!
     }
 
@@ -189,6 +194,13 @@ bool ResourceCalDav::doSave() {
         // FIXME: Calling clearChanges() here is not the ideal way since the
         // upload might fail, but there is no other place to call it...
         clearChanges();
+        if (mWriteRetryTimer != NULL) {
+            if (mWriteRetryTimer->isActive() == false) {
+                disconnect( mWriteRetryTimer, SIGNAL(timeout()), this, SLOT(doSave()) );
+                delete mWriteRetryTimer;
+                mWriteRetryTimer = NULL;
+            }
+        }
         return true;
     }
     else return true;	// We do not need to alert the user to this transient failure; a timer has been started to retry the save
@@ -277,6 +289,8 @@ void ResourceCalDav::updateProgressBar(int direction) {
 
     // See if anything is in the queues
     current_queued_events = mWritingQueue.count() + mLoadingQueue.count();
+    if ((direction == 0) && (mLoader->running() == true)) current_queued_events++;
+    if ((direction == 1) && (mWriter->running() == true)) current_queued_events++;
     if (current_queued_events > original_queued_events) {
         original_queued_events = current_queued_events;
     }
@@ -304,13 +318,13 @@ void ResourceCalDav::updateProgressBar(int direction) {
 void ResourceCalDav::loadingQueuePush(const LoadingTask *task) {
    if ((mLoadingQueue.isEmpty() == true) && (mLoader->running() == false)) {
         mLoadingQueue.enqueue(task);
-        loadingQueuePop();
         updateProgressBar(0);
+        loadingQueuePop();
     }
 }
 
 void ResourceCalDav::loadingQueuePop() {
-    if (!mLoadingQueueReady || mLoadingQueue.isEmpty() || (mWritingQueue.isEmpty() == false) || (mWriter->running() == true) || !mWritingQueueReady || (readLockout == true)) {
+    if (!mLoadingQueueReady || mLoadingQueue.isEmpty() || (isSaving() == true)) {
         return;
     }
 
@@ -335,10 +349,10 @@ void ResourceCalDav::loadingQueuePop() {
 
     log("starting actual download job");
     mLoader->start(QThread::LowestPriority);
-    updateProgressBar(0);
 
     // if all ok, removing the task from the queue
     mLoadingQueue.dequeue();
+    updateProgressBar(0);
 
     delete t;
 }
@@ -354,8 +368,6 @@ void ResourceCalDav::loadFinished() {
 
     log("load finished");
 
-    updateProgressBar(0);
-
     if (!loader) {
         log("loader is NULL");
         return;
@@ -411,7 +423,10 @@ void ResourceCalDav::loadFinished() {
 
     // Loading queue and mLoadingQueueReady flag are not shared resources, i.e. only one thread has an access to them.
     // That's why no mutexes are required.
+    mLoader->terminate();
+    mLoader->wait(TERMINATION_WAITING_TIME);
     mLoadingQueueReady = true;
+    updateProgressBar(0);
     loadingQueuePop();
 }
 
@@ -515,8 +530,8 @@ void ResourceCalDav::writingQueuePush(const WritingTask *task) {
 //     printf("task->deleted: %s\n\r", task->deleted.ascii());
 //     printf("task->changed: %s\n\r", task->changed.ascii());
     mWritingQueue.enqueue(task);
-    writingQueuePop();
     updateProgressBar(1);
+    writingQueuePop();
 }
 
 void ResourceCalDav::writingQueuePop() {
@@ -563,10 +578,10 @@ void ResourceCalDav::writingQueuePop() {
 
     log("starting actual write job");
     mWriter->start(QThread::LowestPriority);
-    updateProgressBar(1);
 
     // if all ok, remove the task from the queue
     mWritingQueue.dequeue();
+    updateProgressBar(1);
 
     delete t;
 }
@@ -667,8 +682,6 @@ bool ResourceCalDav::startWriting(const QString& url) {
 void ResourceCalDav::writingFinished() {
     log("writing finished");
 
-    updateProgressBar(1);
-
     if (!mWriter) {
         log("mWriter is NULL");
         return;
@@ -707,7 +720,10 @@ void ResourceCalDav::writingFinished() {
 
     // Writing queue and mWritingQueueReady flag are not shared resources, i.e. only one thread has an access to them.
     // That's why no mutexes are required.
+    mWriter->terminate();
+    mWriter->wait(TERMINATION_WAITING_TIME);
     mWritingQueueReady = true;
+    updateProgressBar(1);
     writingQueuePop();
 }
 
diff --git a/kresources/carddav/resource.cpp b/kresources/carddav/resource.cpp
index 77f5077ab..77a0e6889 100644
--- a/kresources/carddav/resource.cpp
+++ b/kresources/carddav/resource.cpp
@@ -95,24 +95,25 @@ ResourceCardDav::~ResourceCardDav() {
         mWriteRetryTimer->stop();	// Unfortunately we cannot do anything at this point; if this timer is still running something is seriously wrong
     }
 
+    if (mLoader) {
+        readLockout = true;
+        mLoader->terminate();
+        mLoader->wait(TERMINATION_WAITING_TIME);
+        mLoadingQueueReady = true;
+    }
+
     while ((mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady) {
         readLockout = true;
         sleep(1);
         qApp->processEvents(QEventLoop::ExcludeUserInput);
     }
 
-    if (mLoader) {
-        mLoader->terminate();
-    }
     if (mWriter) {
         mWriter->terminate();
     }
 
     log("waiting for jobs terminated");
 
-    if (mLoader) {
-        mLoader->wait(TERMINATION_WAITING_TIME);
-    }
     if (mWriter) {
         mWriter->wait(TERMINATION_WAITING_TIME);
     }
@@ -128,7 +129,8 @@ ResourceCardDav::~ResourceCardDav() {
 }
 
 bool ResourceCardDav::isSaving() {
-    return (((mWriteRetryTimer != NULL) ? mWriteRetryTimer->isActive() : 0) || (mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady);
+    doSave();
+    return (((mWriteRetryTimer != NULL) ? 1 : 0) || (mWriter->running() == true) || (mWritingQueue.isEmpty() == false) || !mWritingQueueReady || readLockout);
 }
 
 /*=========================================================================
@@ -138,7 +140,7 @@ bool ResourceCardDav::isSaving() {
 bool ResourceCardDav::load() {
     bool syncCache = true;
 
-    if ((mLoadingQueueReady == false) || (mLoadingQueue.isEmpty() == false) || (mLoader->running() == true) || (readLockout == true)) {
+    if ((mLoadingQueueReady == false) || (mLoadingQueue.isEmpty() == false) || (mLoader->running() == true) || (isSaving() == true)) {
         return true;	// Silently fail; the user has obviously not responded to a dialog and we don't need to pop up more of them!
     }
 
@@ -189,6 +191,13 @@ bool ResourceCardDav::doSave() {
         // FIXME: Calling clearChanges() here is not the ideal way since the
         // upload might fail, but there is no other place to call it...
         clearChanges();
+        if (mWriteRetryTimer != NULL) {
+            if (mWriteRetryTimer->isActive() == false) {
+                disconnect( mWriteRetryTimer, SIGNAL(timeout()), this, SLOT(doSave()) );
+                delete mWriteRetryTimer;
+                mWriteRetryTimer = NULL;
+            }
+        }
         return true;
     }
     else return true;	// We do not need to alert the user to this transient failure; a timer has been started to retry the save
@@ -267,6 +276,34 @@ void ResourceCardDav::setReadOnly(bool v) {
     ensureReadOnlyFlagHonored();
 }
 
+void ResourceCardDav::updateProgressBar(int direction) {
+    int current_queued_events;
+    static int original_queued_events;
+
+    // See if anything is in the queues
+    current_queued_events = mWritingQueue.count() + mLoadingQueue.count();
+    if ((direction == 0) && (mLoader->running() == true)) current_queued_events++;
+    if ((direction == 1) && (mWriter->running() == true)) current_queued_events++;
+    if (current_queued_events > original_queued_events) {
+        original_queued_events = current_queued_events;
+    }
+
+    if (current_queued_events == 0) {
+        if ( mProgress != NULL) {
+            mProgress->setComplete();
+            mProgress = NULL;
+            original_queued_events = 0;
+        }
+    }
+    else {
+        if (mProgress == NULL) {
+            if (direction == 0) mProgress = KPIM::ProgressManager::createProgressItem(KPIM::ProgressManager::getUniqueID(), i18n("Downloading Contacts") );
+            if (direction == 1) mProgress = KPIM::ProgressManager::createProgressItem(KPIM::ProgressManager::getUniqueID(), i18n("Uploading Contacts") );
+        }
+        mProgress->setProgress( ((((float)original_queued_events-(float)current_queued_events)*100)/(float)original_queued_events) );
+    }
+}
+
 /*=========================================================================
 | READING METHODS
  ========================================================================*/
@@ -274,16 +311,13 @@ void ResourceCardDav::setReadOnly(bool v) {
 void ResourceCardDav::loadingQueuePush(const LoadingTask *task) {
    if ((mLoadingQueue.isEmpty() == true) && (mLoader->running() == false)) {
         mLoadingQueue.enqueue(task);
+        updateProgressBar(0);
         loadingQueuePop();
-        if (mProgress == NULL) {
-            mProgress = KPIM::ProgressManager::createProgressItem(KPIM::ProgressManager::getUniqueID(), i18n("Downloading Calendar") );
-            mProgress->setProgress( 0 );
-        }
     }
 }
 
 void ResourceCardDav::loadingQueuePop() {
-    if (!mLoadingQueueReady || mLoadingQueue.isEmpty() || (mWritingQueue.isEmpty() == false) || (mWriter->running() == true) || !mWritingQueueReady) {
+    if (!mLoadingQueueReady || mLoadingQueue.isEmpty() || (isSaving() == true)) {
         return;
     }
 
@@ -311,6 +345,7 @@ void ResourceCardDav::loadingQueuePop() {
 
     // if all ok, removing the task from the queue
     mLoadingQueue.dequeue();
+    updateProgressBar(0);
 
     delete t;
 }
@@ -326,11 +361,6 @@ void ResourceCardDav::loadFinished() {
 
     log("load finished");
 
-    if ( mProgress != NULL) {
-        mProgress->setComplete();
-        mProgress = NULL;
-    }
-
     if (!loader) {
         log("loader is NULL");
         return;
@@ -387,7 +417,10 @@ void ResourceCardDav::loadFinished() {
 
     // Loading queue and mLoadingQueueReady flag are not shared resources, i.e. only one thread has an access to them.
     // That's why no mutexes are required.
+    mLoader->terminate();
+    mLoader->wait(TERMINATION_WAITING_TIME);
     mLoadingQueueReady = true;
+    updateProgressBar(0);
     loadingQueuePop();
 }
 
@@ -494,11 +527,8 @@ void ResourceCardDav::writingQueuePush(const WritingTask *task) {
 //     printf("task->deleted: %s\n\r", task->deleted.ascii());
 //     printf("task->changed: %s\n\r", task->changed.ascii());
     mWritingQueue.enqueue(task);
+    updateProgressBar(1);
     writingQueuePop();
-    if (mProgress == NULL) {
-        mProgress = KPIM::ProgressManager::createProgressItem(KPIM::ProgressManager::getUniqueID(), i18n("Saving Calendar") );
-        mProgress->setProgress( 0 );
-    }
 }
 
 void ResourceCardDav::writingQueuePop() {
@@ -548,6 +578,7 @@ void ResourceCardDav::writingQueuePop() {
 
     // if all ok, remove the task from the queue
     mWritingQueue.dequeue();
+    updateProgressBar(1);
 
     delete t;
 }
@@ -602,11 +633,6 @@ bool ResourceCardDav::startWriting(const QString& url) {
 void ResourceCardDav::writingFinished() {
     log("writing finished");
 
-    if ( mProgress != NULL) {
-        mProgress->setComplete();
-        mProgress = NULL;
-    }
-
     if (!mWriter) {
         log("mWriter is NULL");
         return;
@@ -642,7 +668,10 @@ void ResourceCardDav::writingFinished() {
 
     // Writing queue and mWritingQueueReady flag are not shared resources, i.e. only one thread has an access to them.
     // That's why no mutexes are required.
+    mWriter->terminate();
+    mWriter->wait(TERMINATION_WAITING_TIME);
     mWritingQueueReady = true;
+    updateProgressBar(1);
     writingQueuePop();
 }
 
diff --git a/kresources/carddav/resource.h b/kresources/carddav/resource.h
index e117daf31..d855f6529 100644
--- a/kresources/carddav/resource.h
+++ b/kresources/carddav/resource.h
@@ -117,6 +117,11 @@ protected:
      */
     void init();
 
+    /**
+     * Updates the progress bar
+     */
+    void updateProgressBar(int direction);
+
     /**
      * Initiates calendar loading process.
      * @param url URL to load calendar data from.
-- 
cgit v1.2.3

