[evolution-ews] Claim sources even when in offline



commit ed4a49d1eb8a28285cee77dc9e758bc5876a04a9
Author: Milan Crha <mcrha redhat com>
Date:   Mon Feb 25 14:13:02 2013 +0100

    Claim sources even when in offline
    
    Partially addresses https://bugzilla.gnome.org/show_bug.cgi?id=678466

 src/addressbook/e-book-backend-ews.c |   67 ++++++++++++++++++++++----
 src/calendar/e-cal-backend-ews.c     |   86 +++++++++++++++++++++++++++++++---
 src/collection/e-ews-backend.c       |   21 +++++++-
 3 files changed, 154 insertions(+), 20 deletions(-)
---
diff --git a/src/addressbook/e-book-backend-ews.c b/src/addressbook/e-book-backend-ews.c
index 3649eaa..fce0352 100644
--- a/src/addressbook/e-book-backend-ews.c
+++ b/src/addressbook/e-book-backend-ews.c
@@ -191,6 +191,37 @@ convert_error_to_edb_error (GError **perror)
 }
 
 static gboolean
+book_backend_ews_ensure_connected (EBookBackendEws *bbews,
+                                  GCancellable *cancellable,
+                                  GError **perror)
+{
+       GError *local_error = NULL;
+
+       g_return_val_if_fail (E_IS_BOOK_BACKEND_EWS (bbews), FALSE);
+
+       PRIV_LOCK (bbews->priv);
+
+       if (bbews->priv->cnc) {
+               PRIV_UNLOCK (bbews->priv);
+               return TRUE;
+       }
+
+       PRIV_UNLOCK (bbews->priv);
+
+       e_backend_authenticate_sync (
+               E_BACKEND (bbews),
+               E_SOURCE_AUTHENTICATOR (bbews),
+               cancellable, &local_error);
+
+       if (!local_error)
+               return TRUE;
+
+       g_propagate_error (perror, local_error);
+
+       return FALSE;
+}
+
+static gboolean
 ews_remove_attachments (const gchar *attachment_dir)
 {
        GDir *dir;
@@ -961,6 +992,7 @@ e_book_backend_ews_create_contacts (EBookBackend *backend,
        EwsCreateContact *create_contact;
        EwsFolderId *fid;
        EBookBackendEwsPrivate *priv;
+       GError *error = NULL;
 
        if (vcards->next != NULL) {
                e_data_book_respond_create_contacts (
@@ -984,8 +1016,9 @@ e_book_backend_ews_create_contacts (EBookBackend *backend,
                return;
        }
 
-       if (ebews->priv->cnc == NULL) {
-               e_data_book_respond_create_contacts (book, opid, EDB_ERROR (AUTHENTICATION_REQUIRED), NULL);
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, &error)) {
+               convert_error_to_edb_error (&error);
+               e_data_book_respond_create_contacts (book, opid, error, NULL);
                return;
        }
 
@@ -1078,6 +1111,7 @@ e_book_backend_ews_remove_contacts (EBookBackend *backend,
        EwsRemoveContact *remove_contact;
        EBookBackendEwsPrivate *priv;
        GSList *l, *copy = NULL;
+       GError *error = NULL;
 
        ebews = E_BOOK_BACKEND_EWS (backend);
 
@@ -1093,8 +1127,9 @@ e_book_backend_ews_remove_contacts (EBookBackend *backend,
                return;
        }
 
-       if (ebews->priv->cnc == NULL) {
-               e_data_book_respond_remove_contacts (book, opid, EDB_ERROR (AUTHENTICATION_REQUIRED), NULL);
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, &error)) {
+               convert_error_to_edb_error (&error);
+               e_data_book_respond_remove_contacts (book, opid, error, NULL);
                return;
        }
 
@@ -1268,8 +1303,9 @@ e_book_backend_ews_modify_contacts (EBookBackend *backend,
                return;
        }
 
-       if (priv->cnc == NULL) {
-               e_data_book_respond_modify_contacts (book, opid, EDB_ERROR (AUTHENTICATION_REQUIRED), NULL);
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, &error)) {
+               convert_error_to_edb_error (&error);
+               e_data_book_respond_modify_contacts (book, opid, error, NULL);
                return;
        }
 
@@ -1328,18 +1364,21 @@ e_book_backend_ews_get_contact (EBookBackend *backend,
                                 const gchar *id)
 {
        EBookBackendEws *ebews;
+       GError *error = NULL;
 
        ebews =  E_BOOK_BACKEND_EWS (backend);
 
        if (!e_backend_get_online (E_BACKEND (backend))) {
-               e_data_book_respond_get_contact (book, opid, EDB_ERROR (CONTACT_NOT_FOUND), "");
+               e_data_book_respond_get_contact (book, opid, EDB_ERROR (REPOSITORY_OFFLINE), NULL);
                return;
        }
 
-       if (ebews->priv->cnc == NULL) {
-               e_data_book_respond_get_contact (book, opid, e_data_book_create_error_fmt 
(E_DATA_BOOK_STATUS_OTHER_ERROR, "Not connected"), NULL);
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, &error)) {
+               convert_error_to_edb_error (&error);
+               e_data_book_respond_get_contact (book, opid, error, NULL);
                return;
        }
+
        e_data_book_respond_get_contact (book, opid, EDB_ERROR (CONTACT_NOT_FOUND), "");
 }
 
@@ -1382,8 +1421,9 @@ e_book_backend_ews_get_contact_list (EBookBackend *backend,
                        return;
        }
 
-       if (priv->cnc == NULL) {
-               e_data_book_respond_get_contact_list (book, opid, EDB_ERROR (AUTHENTICATION_REQUIRED), NULL);
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, &error)) {
+               convert_error_to_edb_error (&error);
+               e_data_book_respond_get_contact_list (book, opid, error, NULL);
                return;
        }
 
@@ -2175,6 +2215,10 @@ ebews_fetch_items (EBookBackendEws *ebews,
        GSList *contact_item_ids = NULL, *dl_ids = NULL;
        GSList *new_items = NULL;
 
+       if (!book_backend_ews_ensure_connected (ebews, cancellable, error)) {
+               return FALSE;
+       }
+
        priv = ebews->priv;
        cnc = priv->cnc;
 
@@ -2777,6 +2821,7 @@ e_book_backend_ews_notify_online_cb (EBookBackend *backend,
                        }
                } else {
                        ebews->priv->cancellable = g_cancellable_new ();
+                       ebews->priv->is_writable = !ebews->priv->is_gal;
 
                        e_book_backend_set_writable (backend, ebews->priv->is_writable);
                }
diff --git a/src/calendar/e-cal-backend-ews.c b/src/calendar/e-cal-backend-ews.c
index ae27269..e8e72f8 100644
--- a/src/calendar/e-cal-backend-ews.c
+++ b/src/calendar/e-cal-backend-ews.c
@@ -217,6 +217,37 @@ switch_offline (ECalBackendEws *cbews)
        }
 }
 
+static gboolean
+cal_backend_ews_ensure_connected (ECalBackendEws *cbews,
+                                 GCancellable *cancellable,
+                                 GError **perror)
+{
+       GError *local_error = NULL;
+
+       g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
+
+       PRIV_LOCK (cbews->priv);
+
+       if (cbews->priv->cnc) {
+               PRIV_UNLOCK (cbews->priv);
+               return TRUE;
+       }
+
+       PRIV_UNLOCK (cbews->priv);
+
+       e_backend_authenticate_sync (
+               E_BACKEND (cbews),
+               E_SOURCE_AUTHENTICATOR (cbews),
+               cancellable, &local_error);
+
+       if (!local_error)
+               return TRUE;
+
+       g_propagate_error (perror, local_error);
+
+       return FALSE;
+}
+
 static void
 e_cal_backend_ews_add_timezone (ECalBackend *backend,
                                 EDataCal *cal,
@@ -325,6 +356,7 @@ e_cal_backend_ews_discard_alarm (ECalBackend *backend,
        ECalBackendEwsPrivate *priv;
        EwsDiscardAlarmData *edad;
        ECalComponent *comp;
+       GError *local_error = NULL;
 
        priv = cbews->priv;
 
@@ -341,6 +373,12 @@ e_cal_backend_ews_discard_alarm (ECalBackend *backend,
 
        PRIV_UNLOCK (priv);
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &local_error)) {
+               convert_error_to_edc_error (&local_error);
+               e_data_cal_respond_discard_alarm (cal, context, local_error);
+               return;
+       }
+
        /* FIXME: Can't there be multiple alarms for each event? Or does
         * Exchange not support that? */
        edad = g_new0 (EwsDiscardAlarmData, 1);
@@ -643,16 +681,18 @@ e_cal_backend_ews_get_object (ECalBackend *backend,
 
        PRIV_LOCK (priv);
 
-       /* make sure any pending refreshing is done */
-       while (priv->refreshing) {
-               PRIV_UNLOCK (priv);
-               e_flag_wait (priv->refreshing_done);
-               PRIV_LOCK (priv);
+       if (e_backend_get_online (E_BACKEND (backend))) {
+               /* make sure any pending refreshing is done */
+               while (priv->refreshing) {
+                       PRIV_UNLOCK (priv);
+                       e_flag_wait (priv->refreshing_done);
+                       PRIV_LOCK (priv);
+               }
        }
 
        /* search the object in the cache */
        comp = e_cal_backend_store_get_component (priv->store, uid, rid);
-       if (!comp) {
+       if (!comp && e_backend_get_online (E_BACKEND (backend))) {
                /* maybe a meeting invitation, for which the calendar item is not downloaded yet,
                 * thus synchronize local cache first */
                ews_start_sync (cbews);
@@ -907,6 +947,12 @@ e_cal_backend_ews_remove_object (ECalBackend *backend,
         */
        e_data_cal_error_if_fail (E_IS_CAL_BACKEND_EWS (cbews), InvalidArg);
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               convert_error_to_edc_error (&error);
+               e_data_cal_respond_remove_objects (cal, context, error, NULL, NULL, NULL);
+               return;
+       }
+
        priv = cbews->priv;
 
        PRIV_LOCK (priv);
@@ -1667,11 +1713,15 @@ e_cal_backend_ews_create_objects (ECalBackend *backend,
                goto exit;
        }
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               goto exit;
+       }
+
        /* parse ical data */
        comp =  e_cal_component_new_from_string (calobj);
        if (comp == NULL) {
                g_propagate_error (&error, EDC_ERROR (InvalidObject));
-               return;
+               goto exit;
        }
        icalcomp = e_cal_component_get_icalcomponent (comp);
 
@@ -2270,6 +2320,10 @@ e_cal_backend_ews_modify_object (ECalBackend *backend,
                goto exit;
        }
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               goto exit;
+       }
+
        icalcomp = icalparser_parse_string (calobj);
        if (!icalcomp) {
                g_propagate_error (&error, EDC_ERROR (InvalidObject));
@@ -2585,6 +2639,10 @@ e_cal_backend_ews_receive_objects (ECalBackend *backend,
                goto exit;
        }
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               goto exit;
+       }
+
        icalcomp = icalparser_parse_string (calobj);
 
        /* make sure data was parsed properly */
@@ -2893,6 +2951,10 @@ e_cal_backend_ews_send_objects (ECalBackend *backend,
                goto exit;
        }
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               goto exit;
+       }
+
        icalcomp = icalparser_parse_string (calobj);
 
        /* make sure data was parsed properly */
@@ -3750,6 +3812,12 @@ ews_start_sync (gpointer data)
        }
 
        ews_refreshing_inc (cbews);
+
+       if (!cbews->priv->cnc) {
+               ews_refreshing_dec (cbews);
+               PRIV_UNLOCK (cbews->priv);
+               return FALSE;
+       }
        PRIV_UNLOCK (cbews->priv);
 
        /* run the actual operation in thread,
@@ -3972,6 +4040,10 @@ e_cal_backend_ews_get_free_busy (ECalBackend *backend,
                goto exit;
        }
 
+       if (!cal_backend_ews_ensure_connected (cbews, cancellable, &error)) {
+               goto exit;
+       }
+
        /* EWS can support only 100 identities, which is the maximum number of identities that the Web 
service method can request
         see http://msdn.microsoft.com / en - us / library / aa564001 % 28v = EXCHG.140 % 29.aspx */
        if (g_slist_length ((GSList *) users) > 100)
diff --git a/src/collection/e-ews-backend.c b/src/collection/e-ews-backend.c
index 6f349c1..cc90a24 100644
--- a/src/collection/e-ews-backend.c
+++ b/src/collection/e-ews-backend.c
@@ -545,6 +545,8 @@ add_remote_sources (EEwsBackend *backend)
                        e_server_side_source_set_remote_deletable (
                                E_SERVER_SIDE_SOURCE (source), TRUE);
                        e_source_registry_server_add_source (registry, source);
+               } else {
+                       e_source_registry_server_add_source (registry, source);
                }
        }
 
@@ -644,8 +646,7 @@ ews_backend_populate (ECollectionBackend *backend)
        ews_backend->priv->need_update_folders = TRUE;
 
        /* do not do anything, if account is disabled */
-       if (!e_source_get_enabled (source) ||
-           !e_backend_get_online (E_BACKEND (backend)))
+       if (!e_source_get_enabled (source))
                return;
 
        /* For now at least, we don't need to know the
@@ -1219,6 +1220,22 @@ e_ews_backend_sync_folders_sync (EEwsBackend *backend,
 
        g_return_val_if_fail (E_IS_EWS_BACKEND (backend), FALSE);
 
+       if (!e_backend_get_online (E_BACKEND (backend))) {
+               SyncFoldersClosure *closure;
+
+               /* This takes ownership of the folder lists. */
+               closure = g_slice_new0 (SyncFoldersClosure);
+               closure->backend = g_object_ref (backend);
+
+               /* Process the results from an idle callback. */
+               g_idle_add_full (
+                       G_PRIORITY_DEFAULT_IDLE,
+                       ews_backend_sync_folders_idle_cb, closure,
+                       (GDestroyNotify) sync_folders_closure_free);
+
+               return TRUE;
+       }
+
        connection = e_ews_backend_ref_connection_sync (
                backend, cancellable, error);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]