1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@novell.com>
5 * Copyright 2004 Novell, Inc. (www.novell.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 #include <sys/types.h>
38 #include <glib/gi18n-lib.h>
39 #include <glib/gstdio.h>
41 #include "camel-data-cache.h"
42 #include "camel-file-utils.h"
43 #include "camel-folder-summary.h"
44 #include "camel-folder.h"
45 #include "camel-offline-journal.h"
46 #include "camel-private.h"
50 static void camel_offline_journal_class_init (CamelOfflineJournalClass *klass);
51 static void camel_offline_journal_init (CamelOfflineJournal *journal, CamelOfflineJournalClass *klass);
52 static void camel_offline_journal_finalize (CamelObject *object);
55 static CamelObjectClass *parent_class = NULL;
59 camel_offline_journal_get_type (void)
61 static CamelType type = 0;
64 type = camel_type_register (camel_object_get_type (),
65 "CamelOfflineJournal",
66 sizeof (CamelOfflineJournal),
67 sizeof (CamelOfflineJournalClass),
68 (CamelObjectClassInitFunc) camel_offline_journal_class_init,
70 (CamelObjectInitFunc) camel_offline_journal_init,
71 (CamelObjectFinalizeFunc) camel_offline_journal_finalize);
78 camel_offline_journal_class_init (CamelOfflineJournalClass *klass)
80 parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
84 camel_offline_journal_init (CamelOfflineJournal *journal, CamelOfflineJournalClass *klass)
86 journal->folder = NULL;
87 journal->filename = NULL;
88 e_dlist_init (&journal->queue);
92 camel_offline_journal_finalize (CamelObject *object)
94 CamelOfflineJournal *journal = (CamelOfflineJournal *) object;
97 g_free (journal->filename);
99 while ((entry = e_dlist_remhead (&journal->queue)))
100 CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_free (journal, entry);
104 * camel_offline_journal_construct:
105 * @journal: a #CamelOfflineJournal object
106 * @folder: a #CamelFolder object
107 * @filename: a filename to save/load the journal
109 * Constructs a journal object.
112 camel_offline_journal_construct (CamelOfflineJournal *journal, CamelFolder *folder, const char *filename)
117 journal->filename = g_strdup (filename);
118 journal->folder = folder;
120 if ((fp = g_fopen (filename, "rb"))) {
121 while ((entry = CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_load (journal, fp)))
122 e_dlist_addtail (&journal->queue, entry);
130 * camel_offline_journal_set_filename:
131 * @journal: a #CamelOfflineJournal object
132 * @filename: a filename to load/save the journal to
134 * Set the filename where the journal should load/save from.
137 camel_offline_journal_set_filename (CamelOfflineJournal *journal, const char *filename)
139 g_return_if_fail (CAMEL_IS_OFFLINE_JOURNAL (journal));
141 g_free (journal->filename);
142 journal->filename = g_strdup (filename);
147 * camel_offline_journal_write:
148 * @journal: a #CamelOfflineJournal object
149 * @ex: a #CamelException
151 * Save the journal to disk.
153 * Returns %0 on success or %-1 on fail
156 camel_offline_journal_write (CamelOfflineJournal *journal, CamelException *ex)
162 if ((fd = g_open (journal->filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666)) == -1) {
163 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
164 _("Cannot write offline journal for folder `%s': %s"),
165 journal->folder->full_name, g_strerror (errno));
169 fp = fdopen (fd, "w");
170 entry = journal->queue.head;
171 while (entry->next) {
172 if (CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_write (journal, entry, fp) == -1)
177 if (fsync (fd) == -1)
186 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
187 _("Cannot write offline journal for folder `%s': %s"),
188 journal->folder->full_name, g_strerror (errno));
197 * camel_offline_journal_replay:
198 * @journal: a #CamelOfflineJournal object
199 * @ex: a #CamelException
201 * Replay all entries in the journal.
203 * Returns %0 on success (no entry failed to replay) or %-1 on fail
206 camel_offline_journal_replay (CamelOfflineJournal *journal, CamelException *ex)
208 EDListNode *entry, *next;
212 camel_exception_init (&lex);
214 entry = journal->queue.head;
215 while (entry->next) {
217 if (CAMEL_OFFLINE_JOURNAL_GET_CLASS (journal)->entry_play (journal, entry, &lex) == -1) {
219 camel_exception_xfer (ex, &lex);
220 camel_exception_clear (&lex);
223 e_dlist_remove (entry);