[evolution-data-server] CamelOfflineJournal: Use GQueues instead of CamelDLists.



commit 0334c4d5283f9cf0c8228c0f834b215959df13ec
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sat Jan 28 18:34:01 2012 -0500

    CamelOfflineJournal: Use GQueues instead of CamelDLists.
    
    GQueue has a more intuitive API.

 camel/camel-offline-journal.c             |   41 +++++++++++++++++------------
 camel/camel-offline-journal.h             |   17 +++++-------
 camel/providers/imap/camel-imap-journal.c |   26 +++++++++---------
 camel/providers/imap/camel-imap-journal.h |    2 -
 4 files changed, 44 insertions(+), 42 deletions(-)
---
diff --git a/camel/camel-offline-journal.c b/camel/camel-offline-journal.c
index 8c55fa6..463e61c 100644
--- a/camel/camel-offline-journal.c
+++ b/camel/camel-offline-journal.c
@@ -51,11 +51,11 @@ static void
 offline_journal_finalize (GObject *object)
 {
 	CamelOfflineJournal *journal = CAMEL_OFFLINE_JOURNAL (object);
-	CamelDListNode *entry;
+	gpointer entry;
 
 	g_free (journal->filename);
 
-	while ((entry = camel_dlist_remhead (&journal->queue)))
+	while ((entry = g_queue_pop_head (&journal->queue)))
 		CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_free (journal, entry);
 
 	/* Chain up to parent's finalize() method. */
@@ -74,9 +74,7 @@ camel_offline_journal_class_init (CamelOfflineJournalClass *class)
 static void
 camel_offline_journal_init (CamelOfflineJournal *journal)
 {
-	journal->folder = NULL;
-	journal->filename = NULL;
-	camel_dlist_init (&journal->queue);
+	g_queue_init (&journal->queue);
 }
 
 /**
@@ -92,7 +90,7 @@ camel_offline_journal_construct (CamelOfflineJournal *journal,
                                  CamelFolder *folder,
                                  const gchar *filename)
 {
-	CamelDListNode *entry;
+	gpointer entry;
 	FILE *fp;
 
 	journal->filename = g_strdup (filename);
@@ -100,7 +98,7 @@ camel_offline_journal_construct (CamelOfflineJournal *journal,
 
 	if ((fp = g_fopen (filename, "rb"))) {
 		while ((entry = CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_load (journal, fp)))
-			camel_dlist_addtail (&journal->queue, entry);
+			g_queue_push_tail (&journal->queue, entry);
 
 		fclose (fp);
 	}
@@ -136,7 +134,7 @@ gint
 camel_offline_journal_write (CamelOfflineJournal *journal,
                              GError **error)
 {
-	CamelDListNode *entry;
+	GList *head, *link;
 	FILE *fp;
 	gint fd;
 
@@ -154,11 +152,13 @@ camel_offline_journal_write (CamelOfflineJournal *journal,
 	if (!fp)
 		goto exception;
 
-	entry = journal->queue.head;
-	while (entry->next) {
+	head = g_queue_peek_head_link (&journal->queue);
+
+	for (link = head; link != NULL; link = g_list_next (link)) {
+		gpointer entry = link->data;
+
 		if (CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_write (journal, entry, fp) == -1)
 			goto exception;
-		entry = entry->next;
 	}
 
 	if (fsync (fd) == -1)
@@ -199,13 +199,16 @@ camel_offline_journal_replay (CamelOfflineJournal *journal,
                               GCancellable *cancellable,
                               GError **error)
 {
-	CamelDListNode *entry, *next;
+	GList *head, *link;
+	GQueue trash = G_QUEUE_INIT;
 	GError *local_error = NULL;
 	gint failed = 0;
 
-	entry = journal->queue.head;
-	while (entry->next) {
-		next = entry->next;
+	head = g_queue_peek_head_link (&journal->queue);
+
+	for (link = head; link != NULL; link = g_list_next (link)) {
+		gpointer entry = link->data;
+
 		if (CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_play (
 			journal, entry, cancellable, &local_error) == -1) {
 			if (failed == 0) {
@@ -215,11 +218,15 @@ camel_offline_journal_replay (CamelOfflineJournal *journal,
 			g_clear_error (&local_error);
 			failed++;
 		} else {
-			camel_dlist_remove (entry);
+			g_queue_push_tail (&trash, link);
 		}
-		entry = next;
 	}
 
+	/* Remove successfully played entries.
+	 * XXX Are we leaking entries here? */
+	while ((link = g_queue_pop_head (&trash)) != NULL)
+		g_queue_delete_link (&journal->queue, link);
+
 	if (failed > 0)
 		return -1;
 
diff --git a/camel/camel-offline-journal.h b/camel/camel-offline-journal.h
index c8cdda0..d021b71 100644
--- a/camel/camel-offline-journal.h
+++ b/camel/camel-offline-journal.h
@@ -30,8 +30,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 
-#include <camel/camel-list-utils.h>
-#include <camel/camel-object.h>
+#include <camel/camel-folder.h>
 
 /* Standard GObject macros */
 #define CAMEL_TYPE_OFFLINE_JOURNAL \
@@ -58,28 +57,26 @@ typedef struct _CamelOfflineJournal CamelOfflineJournal;
 typedef struct _CamelOfflineJournalClass CamelOfflineJournalClass;
 typedef struct _CamelOfflineJournalEntry CamelOfflineJournalEntry;
 
-struct _CamelFolder;
-
 struct _CamelOfflineJournal {
 	CamelObject parent;
 
-	struct _CamelFolder *folder;
+	CamelFolder *folder;
 	gchar *filename;
-	CamelDList queue;
+	GQueue queue;
 };
 
 struct _CamelOfflineJournalClass {
 	CamelObjectClass parent_class;
 
 	void		(*entry_free)		(CamelOfflineJournal *journal,
-						 CamelDListNode *entry);
-	CamelDListNode *(*entry_load)		(CamelOfflineJournal *journal,
+						 gpointer entry);
+	gpointer	(*entry_load)		(CamelOfflineJournal *journal,
 						 FILE *in);
 	gint		(*entry_write)		(CamelOfflineJournal *journal,
-						 CamelDListNode *entry,
+						 gpointer entry,
 						 FILE *out);
 	gint		(*entry_play)		(CamelOfflineJournal *journal,
-						 CamelDListNode *entry,
+						 gpointer entry,
 						 GCancellable *cancellable,
 						 GError **error);
 };
diff --git a/camel/providers/imap/camel-imap-journal.c b/camel/providers/imap/camel-imap-journal.c
index 31bc483..3a34608 100644
--- a/camel/providers/imap/camel-imap-journal.c
+++ b/camel/providers/imap/camel-imap-journal.c
@@ -41,10 +41,10 @@
 
 #define d(x)
 
-static void imap_entry_free (CamelOfflineJournal *journal, CamelDListNode *entry);
-static CamelDListNode *imap_entry_load (CamelOfflineJournal *journal, FILE *in);
-static gint imap_entry_write (CamelOfflineJournal *journal, CamelDListNode *entry, FILE *out);
-static gint imap_entry_play (CamelOfflineJournal *journal, CamelDListNode *entry, GCancellable *cancellable, GError **error);
+static void imap_entry_free (CamelOfflineJournal *journal, gpointer entry);
+static gpointer imap_entry_load (CamelOfflineJournal *journal, FILE *in);
+static gint imap_entry_write (CamelOfflineJournal *journal, gpointer entry, FILE *out);
+static gint imap_entry_play (CamelOfflineJournal *journal, gpointer entry, GCancellable *cancellable, GError **error);
 static void unref_folder (gpointer key, gpointer value, gpointer data);
 static void free_uids (GPtrArray *array);
 
@@ -112,9 +112,9 @@ unref_folder (gpointer key,
 
 static void
 imap_entry_free (CamelOfflineJournal *journal,
-                 CamelDListNode *entry)
+                 gpointer entry)
 {
-	CamelIMAPJournalEntry *imap_entry = (CamelIMAPJournalEntry *) entry;
+	CamelIMAPJournalEntry *imap_entry = entry;
 
 	switch (imap_entry->type) {
 		case CAMEL_IMAP_JOURNAL_ENTRY_EXPUNGE:
@@ -179,7 +179,7 @@ decode_uids (FILE *file)
 	return uids;
 }
 
-static CamelDListNode *
+static gpointer
 imap_entry_load (CamelOfflineJournal *journal,
                  FILE *in)
 {
@@ -215,7 +215,7 @@ imap_entry_load (CamelOfflineJournal *journal,
 		goto exception;
 	}
 
-	return (CamelDListNode *) entry;
+	return entry;
 
  exception:
 	switch (entry->type) {
@@ -245,10 +245,10 @@ encode_uids (FILE *file,
 
 static gint
 imap_entry_write (CamelOfflineJournal *journal,
-                  CamelDListNode *entry,
+                  gpointer entry,
                   FILE *out)
 {
-	CamelIMAPJournalEntry *imap_entry = (CamelIMAPJournalEntry *) entry;
+	CamelIMAPJournalEntry *imap_entry = entry;
 	GPtrArray *uids = NULL;
 
 	if (camel_file_util_encode_uint32 (out, imap_entry->type) == -1)
@@ -322,11 +322,11 @@ journal_decode_folder (CamelIMAPJournal *journal,
 
 static gint
 imap_entry_play (CamelOfflineJournal *journal,
-                 CamelDListNode *entry,
+                 gpointer entry,
                  GCancellable *cancellable,
                  GError **error)
 {
-	CamelIMAPJournalEntry *imap_entry = (CamelIMAPJournalEntry *) entry;
+	CamelIMAPJournalEntry *imap_entry = entry;
 
 	d(g_print ("DEBUG: PLaying the journal \n"));
 
@@ -461,7 +461,7 @@ camel_imap_journal_log (CamelOfflineJournal *journal,
 
 	va_end (ap);
 
-	camel_dlist_addtail (&journal->queue, (CamelDListNode *) entry);
+	g_queue_push_tail (&journal->queue, entry);
 	camel_offline_journal_write (journal, NULL);
 }
 
diff --git a/camel/providers/imap/camel-imap-journal.h b/camel/providers/imap/camel-imap-journal.h
index b92c60c..70f2669 100644
--- a/camel/providers/imap/camel-imap-journal.h
+++ b/camel/providers/imap/camel-imap-journal.h
@@ -61,8 +61,6 @@ typedef enum {
 } CamelOfflineAction;
 
 struct _CamelIMAPJournalEntry {
-	CamelDListNode node;
-
 	CamelOfflineAction type;
 
 	GPtrArray *uids;



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