[evolution-mapi/gnome-2-32] Bug #629507 - gal re-fetches itself



commit 2f24334b5256a8114a74664d598924451c121bc8
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 29 12:22:07 2010 +0200

    Bug #629507 - gal re-fetches itself

 src/addressbook/e-book-backend-mapi-gal.c |   28 ++++++++++++
 src/addressbook/e-book-backend-mapi.c     |   65 +++++++++++++++++++++++++---
 src/addressbook/e-book-backend-mapi.h     |    2 +
 3 files changed, 88 insertions(+), 7 deletions(-)
---
diff --git a/src/addressbook/e-book-backend-mapi-gal.c b/src/addressbook/e-book-backend-mapi-gal.c
index b85a439..58ea50a 100644
--- a/src/addressbook/e-book-backend-mapi-gal.c
+++ b/src/addressbook/e-book-backend-mapi-gal.c
@@ -138,6 +138,7 @@ ebbm_gal_fetch_contacts (EBookBackendMAPI *ebma, struct mapi_SRestriction *restr
 	GError *mapi_error = NULL;
 	struct FetchGalData fgd = { 0 };
 	ExchangeMapiConnection *conn;
+	gchar *last_fetch;
 
 	e_book_backend_mapi_lock_connection (ebma);
 
@@ -149,6 +150,23 @@ ebbm_gal_fetch_contacts (EBookBackendMAPI *ebma, struct mapi_SRestriction *restr
 		return;
 	}
 
+	/* GAL doesn't use restrictions yet, thus just fetches all items always */
+	last_fetch = e_book_backend_mapi_cache_get (ebma, "gal-last-update");
+	if (last_fetch) {
+		GTimeVal last_tv = { 0 }, now = { 0 };
+
+		g_get_current_time (&now);
+
+		/* refetch gal only once per week */
+		if (g_time_val_from_iso8601 (last_fetch, &last_tv) && now.tv_sec - last_tv.tv_sec <= 60 * 60 * 24 * 7) {
+			g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
+			g_free (last_fetch);
+			return;
+		}
+
+		g_free (last_fetch);
+	}
+
 	fgd.ebma = ebma;
 	fgd.book_view = book_view;
 	fgd.notify_contact_data = notify_contact_data;
@@ -161,6 +179,16 @@ ebbm_gal_fetch_contacts (EBookBackendMAPI *ebma, struct mapi_SRestriction *restr
 	if (mapi_error) {
 		mapi_error_to_edb_error (error, mapi_error, E_DATA_BOOK_STATUS_OTHER_ERROR, _("Failed to fetch GAL entries"));
 		g_error_free (mapi_error);
+	} else {
+		GTimeVal now = { 0 };
+
+		g_get_current_time (&now);
+
+		last_fetch = g_time_val_to_iso8601 (&now);
+		if (last_fetch && *last_fetch)
+			e_book_backend_mapi_cache_set (ebma, "gal-last-update", last_fetch);
+
+		g_free (last_fetch);
 	}
 
 	e_book_backend_mapi_unlock_connection (ebma);
diff --git a/src/addressbook/e-book-backend-mapi.c b/src/addressbook/e-book-backend-mapi.c
index 1067ac4..8c5a402 100644
--- a/src/addressbook/e-book-backend-mapi.c
+++ b/src/addressbook/e-book-backend-mapi.c
@@ -299,6 +299,7 @@ ebbm_update_cache_cb (gpointer data)
 	EBookBackendMAPIClass *ebmac;
 	glong last_modification_secs = 0;
 	EDataBookView *book_view;
+	GError *error = NULL;
 
 	g_return_val_if_fail (ebma != NULL, NULL);
 	g_return_val_if_fail (E_IS_BOOK_BACKEND_MAPI (ebma), NULL);
@@ -324,14 +325,13 @@ ebbm_update_cache_cb (gpointer data)
 		if (!ebbm_get_cache_time (ebma, &last_modification_secs))
 			last_modification_secs = 0;
 
-		ebbm_fetch_contacts (ebma, restriction, NULL, &last_modification_secs, NULL);
+		ebbm_fetch_contacts (ebma, restriction, NULL, &last_modification_secs, &error);
 
 		talloc_free (mem_ctx);
 	}
 
-	if (!g_cancellable_is_cancelled (priv->update_cache) && ebmac->op_fetch_known_uids) {
+	if (!error && !g_cancellable_is_cancelled (priv->update_cache) && ebmac->op_fetch_known_uids) {
 		GHashTable *uids = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
-		GError *error = NULL;
 
 		ebmac->op_fetch_known_uids (ebma, priv->update_cache, uids, &error);
 
@@ -347,7 +347,8 @@ ebbm_update_cache_cb (gpointer data)
 
 				if (!uid || g_hash_table_lookup (uids, uid)
 				    || g_str_equal (uid, "populated")
-				    || g_str_equal (uid, "last_update_time"))
+				    || g_str_equal (uid, "last_update_time")
+				    || g_str_has_prefix (uid, "key:"))
 					continue;
 
 				/* uid is hold by a cache, thus make a copy before remove */
@@ -366,10 +367,11 @@ ebbm_update_cache_cb (gpointer data)
 		}
 
 		g_hash_table_destroy (uids);
-		if (error)
-			g_error_free (error);
 	}
 
+	if (error)
+		g_error_free (error);
+
 	book_view = ebbm_pick_book_view (ebma);
 	if (book_view)
 		e_data_book_view_notify_complete (book_view, NULL);
@@ -701,9 +703,10 @@ ebbm_book_view_thread (gpointer data)
 		if (ebmac && ebmac->op_book_view_thread)
 			ebmac->op_book_view_thread (bvtd->ebma, bvtd->book_view, &error);
 
-		if (!error && !priv->marked_for_offline) {
+		if (!error && !priv->marked_for_offline && !e_book_backend_cache_is_populated (priv->cache)) {
 			/* todo: create restriction based on the book_view */
 			ebbm_fetch_contacts (bvtd->ebma, NULL, bvtd->book_view, NULL, &error);
+			e_book_backend_cache_set_populated (priv->cache);
 		}
 	}
 
@@ -1624,6 +1627,54 @@ e_book_backend_mapi_notify_contact_removed (EBookBackendMAPI *ebma, const gchar
 	e_book_backend_notify_remove (E_BOOK_BACKEND (ebma), uid);
 }
 
+static gchar *
+create_cache_key (const gchar *key)
+{
+	g_return_val_if_fail (key != NULL, NULL);
+
+	return g_strconcat ("key:", key, NULL);
+}
+
+void
+e_book_backend_mapi_cache_set (EBookBackendMAPI *ebma, const gchar *key, const gchar *value)
+{
+	gchar *real_key;
+
+	g_return_if_fail (ebma != NULL);
+	g_return_if_fail (E_IS_BOOK_BACKEND_MAPI (ebma));
+	g_return_if_fail (ebma->priv != NULL);
+	g_return_if_fail (ebma->priv->cache != NULL);
+	g_return_if_fail (key != NULL);
+
+	real_key = create_cache_key (key);
+	g_return_if_fail (real_key != NULL);
+
+	if (!e_file_cache_add_object (E_FILE_CACHE (ebma->priv->cache), real_key, value))
+		e_file_cache_replace_object (E_FILE_CACHE (ebma->priv->cache), real_key, value);
+
+	g_free (real_key);
+}
+
+gchar *
+e_book_backend_mapi_cache_get (EBookBackendMAPI *ebma, const gchar *key)
+{
+	gchar *real_key, *res;
+
+	g_return_val_if_fail (ebma != NULL, NULL);
+	g_return_val_if_fail (E_IS_BOOK_BACKEND_MAPI (ebma), NULL);
+	g_return_val_if_fail (ebma->priv != NULL, NULL);
+	g_return_val_if_fail (ebma->priv->cache != NULL, NULL);
+	g_return_val_if_fail (key != NULL, NULL);
+
+	real_key = create_cache_key (key);
+	g_return_val_if_fail (real_key != NULL, NULL);
+
+	res = g_strdup (e_file_cache_get_object (E_FILE_CACHE (ebma->priv->cache), real_key));
+	g_free (real_key);
+
+	return res;
+}
+
 /* utility functions/macros */
 
 /* 'data' is one of GET_ALL_KNOWN_IDS or GET_UIDS_ONLY */
diff --git a/src/addressbook/e-book-backend-mapi.h b/src/addressbook/e-book-backend-mapi.h
index ffba273..0ae2682 100644
--- a/src/addressbook/e-book-backend-mapi.h
+++ b/src/addressbook/e-book-backend-mapi.h
@@ -105,6 +105,8 @@ void e_book_backend_mapi_update_view_by_cache (EBookBackendMAPI *ebma, EDataBook
 gboolean e_book_backend_mapi_is_marked_for_offline (EBookBackendMAPI *ebma);
 gboolean e_book_backend_mapi_notify_contact_update (EBookBackendMAPI *ebma, EDataBookView *book_view, EContact *contact, const struct timeval *pr_last_modification_time, gint index, gint total, gpointer notify_contact_data);
 void e_book_backend_mapi_notify_contact_removed (EBookBackendMAPI *ebma, const gchar *uid);
+void   e_book_backend_mapi_cache_set (EBookBackendMAPI *ebma, const gchar *key, const gchar *value);
+gchar *e_book_backend_mapi_cache_get (EBookBackendMAPI *ebma, const gchar *key);
 
 /* utility functions/macros */
 



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