From 5572a114be163aeae75648f8e82ce692d4a77517 Mon Sep 17 00:00:00 2001
From: tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>
Date: Mon, 30 Aug 2010 04:23:41 +0000
Subject: Separated event and task reads into two separate jobs

git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1169762 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
---
 kresources/caldav/job.cpp      | 37 ++++++++++++++++++++++---
 kresources/caldav/job.h        | 61 +++++++++++++++++++++++++++++++++++++-----
 kresources/caldav/reader.cpp   | 38 +++++++++++++++-----------
 kresources/caldav/reader.h     |  2 ++
 kresources/caldav/resource.cpp | 31 +++++++++++++++++++--
 kresources/caldav/writer.cpp   |  9 +++++++
 kresources/caldav/writer.h     |  2 ++
 7 files changed, 153 insertions(+), 27 deletions(-)

diff --git a/kresources/caldav/job.cpp b/kresources/caldav/job.cpp
index 1f6c38c49..6edfbbfe8 100644
--- a/kresources/caldav/job.cpp
+++ b/kresources/caldav/job.cpp
@@ -62,6 +62,12 @@ void CalDavJob::setErrorString(const TQString& err, const long number) {
     mErrorNumber = number;
 }
 
+void CalDavJob::setTasksErrorString(const TQString& err, const long number) {
+    mTasksError = true;
+    mTasksErrorString = err;
+    mTasksErrorNumber = number;
+}
+
 void CalDavJob::processError(const caldav_error* err) {
     TQString error_string;
 
@@ -70,7 +76,7 @@ void CalDavJob::processError(const caldav_error* err) {
     if (-401 == code) { // unauthorized
         error_string = i18n("Unauthorized. Username or password incorrect.");
     } else if (-599 <= code && code <= -300) {
-        error_string = i18n("HTTP error %1. Maybe, URL is not a CalDAV resource.").arg(-code);
+        error_string = i18n("HTTP error %1. Please ensure that the URL is a valid CalDAV resource.").arg(-code);
     } else {
         error_string = err->str;
     }
@@ -78,12 +84,29 @@ void CalDavJob::processError(const caldav_error* err) {
     setErrorString(error_string, code);
 }
 
+void CalDavJob::processTasksError(const caldav_error* err) {
+    TQString error_string;
+
+    long code = err->code;
+
+    if (-401 == code) { // unauthorized
+        error_string = i18n("Unauthorized. Username or password incorrect.");
+    } else if (-599 <= code && code <= -300) {
+        error_string = i18n("HTTP error %1. Please ensure that the URL is a valid CalDAV resource.").arg(-code);
+    } else {
+        error_string = err->str;
+    }
+
+    setTasksErrorString(error_string, code);
+}
+
 
 void CalDavJob::run() {
     log("cleaning job");
     cleanJob();
 
     int res = OK;
+    int tasksres = OK;
 
     runtime_info* caldav_runtime = caldav_get_runtime_info();
 
@@ -92,14 +115,22 @@ void CalDavJob::run() {
     enableCaldavDebug(caldav_runtime);
 #endif // KCALDAV_DEBUG
 
-    log("running job");
+    log("running event job");
     res = runJob(caldav_runtime);
 
     if (OK != res) {
-        log("job failed");
+        log("event job failed");
         processError(caldav_runtime->error);
     }
 
+    log("running tasks job");
+    tasksres = runTasksJob(caldav_runtime);
+
+    if (OK != tasksres) {
+        log("tasks job failed");
+        processTasksError(caldav_runtime->error);
+    }
+
     caldav_free_runtime_info(&caldav_runtime);
 
     // Signal done
diff --git a/kresources/caldav/job.h b/kresources/caldav/job.h
index 53d4e0e18..bad00c98d 100644
--- a/kresources/caldav/job.h
+++ b/kresources/caldav/job.h
@@ -103,32 +103,53 @@ public:
     }
 
     /**
-     * @return true if downloading process failed.
+     * @return true if events downloading process failed.
      */
     virtual bool error() const {
         return mError;
     }
 
     /**
-     * @return an error string.
+     * @return true if tasks downloading process failed.
+     */
+    virtual bool tasksError() const {
+        return mTasksError;
+    }
+
+    /**
+     * @return an event error string.
      */
     virtual TQString errorString() const {
         return mErrorString;
     }
 
     /**
-     * @return an error number.
+     * @return a task error string.
+     */
+    virtual TQString tasksErrorString() const {
+        return mTasksErrorString;
+    }
+
+    /**
+     * @return an event error number.
      */
     virtual long errorNumber() const {
         return mErrorNumber;
     }
 
+    /**
+     * @return a task error number.
+     */
+    virtual long tasksErrorNumber() const {
+        return mTasksErrorNumber;
+    }
+
 protected:
 
     virtual void run();
 
     /**
-     * Main run method for jobs. Jobs should not override run() method.
+     * Main run method for event jobs. Jobs should not override run() method.
      * Instead of this they should override this one.
      * @param caldavRuntime specific libcaldav runtime information. This pointer should not be saved for the usage
      * outside of runJob.
@@ -136,6 +157,15 @@ protected:
      */
     virtual int runJob(runtime_info* caldavRuntime) = 0;
 
+    /**
+     * Main run method for task jobs. Jobs should not override run() method.
+     * Instead of this they should override this one.
+     * @param caldavRuntime specific libcaldav runtime information. This pointer should not be saved for the usage
+     * outside of runJob.
+     * @return libcaldav response code (see CALDAV_RESPONSE)
+     */
+    virtual int runTasksJob(runtime_info* caldavRuntime) = 0;
+
     /**
      * Some cleaning. Jobs may (and usually should) override this method.
      */
@@ -143,28 +173,47 @@ protected:
         mError = false;
         mErrorString = "";
         mErrorNumber = 0;
+        mTasksError = false;
+        mTasksErrorString = "";
+        mTasksErrorNumber = 0;
     }
 
     /**
-     * Sets an error string to @p err. Also sets an error flag.
+     * Sets an event error string to @p err. Also sets an error flag.
      */
     void setErrorString(const TQString& str, const long number);
 
     /**
-     * Process an error.
+     * Sets a task error string to @p err. Also sets an error flag.
+     */
+    void setTasksErrorString(const TQString& str, const long number);
+
+    /**
+     * Process an event error.
      * Subclasses can overwrite this method, if some special error message handling
      * should be done. Call setErrorString() to set the error after processing is done.
      * @param err error structure.
      */
     virtual void processError(const caldav_error* err);
 
+    /**
+     * Process a task error.
+     * Subclasses can overwrite this method, if some special error message handling
+     * should be done. Call setErrorString() to set the error after processing is done.
+     * @param err error structure.
+     */
+    virtual void processTasksError(const caldav_error* err);
+
 private:
 
     TQString mUrl;
     TQString mTasksUrl;
     bool mError;
+    bool mTasksError;
     TQString mErrorString;
+    TQString mTasksErrorString;
     long mErrorNumber;
+    long mTasksErrorNumber;
     TQObject *mParent;
     int mType;
 
diff --git a/kresources/caldav/reader.cpp b/kresources/caldav/reader.cpp
index 02358ba04..a7956fe62 100644
--- a/kresources/caldav/reader.cpp
+++ b/kresources/caldav/reader.cpp
@@ -31,6 +31,10 @@ using namespace KCal;
 void CalDavReader::cleanJob() {
     CalDavJob::cleanJob();
     mData = "";
+}
+
+void CalDavReader::cleanTasksJob() {
+    CalDavJob::cleanJob();
     mTasksData = "";
 }
 
@@ -48,28 +52,33 @@ int CalDavReader::runJob(runtime_info* RT) {
           kdDebug() << "getting object from the specified time range";
           res = caldav_get_object(result, mTimeStart.toTime_t(), mTimeEnd.toTime_t(), std::string(url().ascii()).c_str(), RT);
       }
-    }
 
-    if (OK == res) {
-        kdDebug() << "success";
-        if (result->msg) {
-            mData = result->msg;
-        } else {
-            kdDebug() << "empty collection";
-            // empty collection
-            mData = "";
-        }
+      if (OK == res) {
+          kdDebug() << "success";
+          if (result->msg) {
+              mData = result->msg;
+          } else {
+              kdDebug() << "empty collection";
+              // empty collection
+              mData = "";
+          }
+      }
     }
 
     caldav_free_response(&result);
 
+    return res;
+}
+
+int CalDavReader::runTasksJob(runtime_info* RT) {
+    kdDebug() << "reader::run, tasksUrl: " << tasksUrl();
+
+    response* result = caldav_get_response();
     CALDAV_RESPONSE tasksres = OK;
 
     if ((OK == tasksres) && (tasksUrl() != "")) {
       kdDebug() << "reader::run, url: " << tasksUrl();
 
-      result = caldav_get_response();
-
       if (mGetAll) {
           kdDebug() << "getting all objects";
           tasksres = caldav_tasks_getall_object(result, std::string(tasksUrl().ascii()).c_str(), RT);
@@ -92,10 +101,7 @@ int CalDavReader::runJob(runtime_info* RT) {
       caldav_free_response(&result);
     }
 
-    if (tasksres == OK)
-      return res;
-    else
-      return tasksres;
+    return tasksres;
 }
 
 // EOF ========================================================================
diff --git a/kresources/caldav/reader.h b/kresources/caldav/reader.h
index c34f2083b..b62a5931b 100644
--- a/kresources/caldav/reader.h
+++ b/kresources/caldav/reader.h
@@ -81,8 +81,10 @@ public:
 protected:
 
     virtual int runJob(runtime_info* caldavRuntime);
+    virtual int runTasksJob(runtime_info* caldavRuntime);
 
     virtual void cleanJob();
+    virtual void cleanTasksJob();
 
 private:
 
diff --git a/kresources/caldav/resource.cpp b/kresources/caldav/resource.cpp
index f3835b180..59865f41a 100644
--- a/kresources/caldav/resource.cpp
+++ b/kresources/caldav/resource.cpp
@@ -394,9 +394,8 @@ void ResourceCalDav::loadFinished() {
             loadError(TQString("[%1] ").arg(abs(loader->errorNumber())) + loader->errorString());
         }
     } else {
-        log("successful load");
+        log("successful event load");
         TQString data = loader->data();
-        TQString tasksData = loader->tasksData();
 
         if (!data.isNull() && !data.isEmpty()) {
             // TODO: I don't know why, but some schedules on http://caldav-test.ioda.net/ (I used it for testing)
@@ -417,6 +416,34 @@ void ResourceCalDav::loadFinished() {
                 emit resourceLoaded(this);
             }
         }
+    }
+
+    if (loader->tasksError()) {
+        if (loader->tasksErrorNumber() == -401) {
+            if (NULL != mPrefs) {
+//                 TQCString newpass;
+//                 if (KPasswordDialog::getPassword (newpass, TQString("<b>") + i18n("Remote authorization required") + TQString("</b><p>") + i18n("Please input the password for") + TQString(" ") + mPrefs->getusername(), NULL) != 1) {
+//                     log("load error: " + loader->tasksErrorString() );
+//                     loadError(TQString("[%1] ").arg(abs(loader->tasksErrorNumber())) + loader->tasksErrorString());
+//                 }
+//                 else {
+//                     // Set new password and try again
+//                     mPrefs->setPassword(TQString(newpass));
+//                     startLoading(mPrefs->getFullUrl(), mPrefs->getFullTasksUrl());
+//                 }
+            }
+            else {
+                log("load error: " + loader->tasksErrorString() );
+                loadError(TQString("[%1] ").arg(abs(loader->tasksErrorNumber())) + loader->tasksErrorString());
+            }
+        }
+        else {
+            log("load error: " + loader->tasksErrorString() );
+            loadError(TQString("[%1] ").arg(abs(loader->tasksErrorNumber())) + loader->tasksErrorString());
+        }
+    } else {
+        log("successful tasks load");
+        TQString tasksData = loader->tasksData();
 
         if (!tasksData.isNull() && !tasksData.isEmpty()) {
             // TODO: I don't know why, but some schedules on http://caldav-test.ioda.net/ (I used it for testing)
diff --git a/kresources/caldav/writer.cpp b/kresources/caldav/writer.cpp
index de168f363..e939f3d9c 100644
--- a/kresources/caldav/writer.cpp
+++ b/kresources/caldav/writer.cpp
@@ -113,4 +113,13 @@ int CalDavWriter::runJob(runtime_info* RT) {
       return tasksres;
 }
 
+int CalDavWriter::runTasksJob(runtime_info* RT) {
+    // Stub function as there is no reason to split the writing jobs like the reading jobs
+    return OK;
+}
+
+void CalDavWriter::cleanTasksJob() {
+    // Stub function as there is no reason to split the writing jobs like the reading jobs
+}
+
 // EOF ========================================================================
diff --git a/kresources/caldav/writer.h b/kresources/caldav/writer.h
index e9bfcd3b5..3b8f82a81 100644
--- a/kresources/caldav/writer.h
+++ b/kresources/caldav/writer.h
@@ -114,8 +114,10 @@ public:
 protected:
 
     virtual int runJob(runtime_info* caldavRuntime);
+    virtual int runTasksJob(runtime_info* caldavRuntime);
 
     virtual void cleanJob();
+    virtual void cleanTasksJob();
 
     /// Just a wrapper above libcaldav event writing functions.
     template<typename Operation>
-- 
cgit v1.2.1