RSSyl: Free GError from memory where necessary.
[claws.git] / src / plugins / rssyl / rssyl_deleted.c
1 /*
2  * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
5  *
6  * - handling of info about user-deleted feed items
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib.h>
29 #include <string.h>
30
31 /* Claws Mail includes */
32 #include <codeconv.h>
33 #include <common/utils.h>
34
35 /* Local includes */
36 #include "rssyl.h"
37 #include "parse822.h"
38 #include "strutils.h"
39
40 static RDeletedItem *_new_deleted_item()
41 {
42         RDeletedItem *ditem = g_new0(RDeletedItem, 1);
43
44         ditem->id = NULL;
45         ditem->title = NULL;
46         ditem->date_published = -1;
47         ditem->date_modified = -1;
48
49         return ditem;
50 }
51
52 static void _free_deleted_item(gpointer d, gpointer user_data)
53 {
54         RDeletedItem *ditem = (RDeletedItem *)d;
55
56         if (ditem == NULL)
57                 return;
58
59         g_free(ditem->id);
60         g_free(ditem->title);
61         g_free(ditem);
62 }
63
64 void rssyl_deleted_free(GSList *deleted_items)
65 {
66         if (deleted_items != NULL) {
67                 debug_print("RSSyl: releasing list of deleted items\n");
68                 g_slist_foreach(deleted_items, _free_deleted_item, NULL);
69                 g_slist_free(deleted_items);
70                 deleted_items = NULL;
71         }
72 }
73
74 static gchar * _deleted_file_path(RFolderItem *ritem)
75 {
76         gchar *itempath, *deleted_file;
77
78         itempath = folder_item_get_path(&ritem->item);
79         deleted_file = g_strconcat(itempath, G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
80         g_free(itempath);
81
82         return deleted_file;
83 }
84
85 /***************************************************************/
86 GSList *rssyl_deleted_update(RFolderItem *ritem)
87 {
88         gchar *deleted_file, *contents, **lines, **line;
89         GError *error = NULL;
90         guint i = 0;
91         RDeletedItem *ditem = NULL;
92         GSList *deleted_items = NULL;
93
94         g_return_val_if_fail(ritem != NULL, NULL);
95
96         deleted_file = _deleted_file_path(ritem);
97
98         debug_print("RSSyl: getting list of deleted items from '%s'\n", deleted_file);
99
100         if (!g_file_test(deleted_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
101                 debug_print("RSSyl: '%s' doesn't exist, ignoring\n", deleted_file);
102                 return NULL;
103         }
104
105         g_file_get_contents(deleted_file, &contents, NULL, &error);
106
107         if (error) {
108                 g_warning("GError: '%s'\n", error->message);
109                 g_error_free(error);
110         }
111
112         if (contents != NULL) {
113                 lines = strsplit_no_copy(contents, '\n');
114         } else {
115                 g_warning("Couldn't read '%s', ignoring\n", deleted_file);
116                 g_free(deleted_file);
117                 return NULL;
118         }
119
120         g_free(deleted_file);
121
122         while (lines[i]) {
123                 line = g_strsplit(lines[i], ": ", 2);
124                 if (line[0] && line[1] && strlen(line[0]) && strlen(line[1])) {
125                         if (!strcmp(line[0], "ID")) {
126                                 ditem = _new_deleted_item();
127                                 ditem->id = g_strdup(line[1]);
128                         } else if (ditem != NULL && !strcmp(line[0], "TITLE")) {
129                                 ditem->title = g_strdup(line[1]);
130                         } else if (ditem != NULL && !strcmp(line[0], "DPUB")) {
131                                 ditem->date_published = atoi(line[1]);
132                         } else if (ditem != NULL && !strcmp(line[0], "DMOD")) {
133                                 ditem->date_modified = atoi(line[1]);
134                                 deleted_items = g_slist_prepend(deleted_items, ditem);
135                                 ditem = NULL;
136                         }
137                 }
138
139                 g_strfreev(line);
140                 i++;
141         }
142
143         g_free(lines);
144         g_free(contents);
145
146         debug_print("RSSyl: got %d deleted items\n", g_slist_length(deleted_items));
147         return deleted_items;
148 }
149
150 static void _store_one_deleted_item(gpointer data, gpointer user_data)
151 {
152         RDeletedItem *ditem = (RDeletedItem *)data;
153         FILE *f = (FILE *)user_data;
154         gboolean err = FALSE;
155
156         if (ditem == NULL || ditem->id == NULL)
157                 return;
158
159         err |= (fprintf(f,
160                         "ID: %s\n"
161                         "TITLE: %s\n"
162                         "DPUB: %ld\n"
163                         "DMOD: %ld\n",
164                         ditem->id, ditem->title,
165                         ditem->date_published, ditem->date_modified) < 0);
166
167         if (err)
168                 debug_print("RSSyl: Error during writing deletion file.\n");
169 }
170
171 static void rssyl_deleted_store_internal(GSList *deleted_items, const gchar *deleted_file)
172 {
173         FILE *f;
174
175         if (g_file_test(deleted_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
176                 if (g_remove(deleted_file) != 0) {
177                         debug_print("RSSyl: Oops, couldn't delete '%s', bailing out\n",
178                                         deleted_file);
179                         return;
180                 }
181         }
182
183         if (g_slist_length(deleted_items) == 0)
184                 return;
185
186         if ((f = g_fopen(deleted_file, "w")) == NULL) {
187                 debug_print("RSSyl: Couldn't open '%s', bailing out.\n", deleted_file);
188                 return;
189         }
190
191         g_slist_foreach(deleted_items, (GFunc)_store_one_deleted_item,
192                         (gpointer)f);
193
194         fclose(f);
195         debug_print("RSSyl: written and closed deletion file\n");
196 }
197
198 void rssyl_deleted_store(RFolderItem *ritem)
199 {
200         gchar *path;
201
202         g_return_if_fail(ritem != NULL);
203
204         path = _deleted_file_path(ritem);
205         rssyl_deleted_store_internal(ritem->deleted_items, path);
206 }
207
208
209 /* Creates a FeedItem from a message file and uses the data to add a item
210  * to the list of deleted stuff. */
211 void rssyl_deleted_add(RFolderItem *ritem, gchar *path)
212 {
213         FeedItem *fitem = NULL;
214         RDeletedItem *ditem = NULL;
215         GSList *deleted_items = rssyl_deleted_update(ritem);
216         gchar *deleted_file = NULL;
217
218         if (!(fitem = rssyl_parse_folder_item_file(path)))
219                 return;
220
221         ditem = _new_deleted_item();
222         ditem->id = g_strdup(feed_item_get_id(fitem));
223         ditem->title = conv_unmime_header(feed_item_get_title(fitem),
224                         CS_UTF_8, FALSE);
225         ditem->date_published = feed_item_get_date_published(fitem);
226         ditem->date_modified = feed_item_get_date_modified(fitem);
227
228         deleted_items = g_slist_prepend(deleted_items, ditem);
229
230         deleted_file = _deleted_file_path(ritem);
231         rssyl_deleted_store_internal(deleted_items, deleted_file);
232         g_free(deleted_file);
233
234         rssyl_deleted_free(deleted_items);
235         feed_item_free(fitem);
236 }
237
238 static gint _rssyl_deleted_check_func(gconstpointer a, gconstpointer b)
239 {
240         RDeletedItem *ditem = (RDeletedItem *)a;
241         FeedItem *fitem = (FeedItem *)b;
242
243         g_return_val_if_fail(ditem != NULL, -10);
244         g_return_val_if_fail(fitem != NULL, -20);
245
246         /* Following must match:
247          * ID, ... */
248         if (!ditem->id || !feed_item_get_id(fitem) ||
249                         strcmp(ditem->id, feed_item_get_id(fitem)))
250                 return -1;
251
252         /* title, ... */
253         if (!ditem->title || !feed_item_get_title(fitem) ||
254                         strcmp(ditem->title, feed_item_get_title(fitem)))
255                 return -2;
256
257         /* time of publishing, ... */
258         if (ditem->date_published != -1 &&
259                         ditem->date_published != feed_item_get_date_published(fitem))
260                 return -3;
261
262         /* and the time of last modification must be greater
263          * (this means that the item was updated since deletion, and possibly
264          * contains new data, so we add it again) */
265         if (ditem->date_modified != -1 &&
266                         ditem->date_modified < feed_item_get_date_modified(fitem))
267                 return -4;
268
269         return 0;
270 }
271
272 /* Returns TRUE if fitem is found among the deleted stuff. */
273 gboolean rssyl_deleted_check(GSList *deleted_items, FeedItem *fitem)
274 {
275         if (g_slist_find_custom(deleted_items, (gconstpointer)fitem,
276                                 _rssyl_deleted_check_func) != NULL)
277                 return TRUE;
278
279         return FALSE;
280 }
281
282 /******** Expiring ********/
283 struct _RDelExpireCtx {
284         RDeletedItem *ditem;
285         gboolean delete;
286 };
287
288 typedef struct _RDelExpireCtx RDelExpireCtx;
289
290 static void _rssyl_deleted_expire_func_f(gpointer data, gpointer user_data)
291 {
292         FeedItem *fitem = (FeedItem *)data;
293         RDelExpireCtx *ctx = (RDelExpireCtx *)user_data;
294
295         /* Following must match:
296          * ID, ... */
297         if (!ctx->ditem->id || !feed_item_get_id(fitem) ||
298                         strcmp(ctx->ditem->id, feed_item_get_id(fitem)))
299                 return;
300
301         /* title, ... */
302         if (!ctx->ditem->title || !feed_item_get_title(fitem) ||
303                         strcmp(ctx->ditem->title, feed_item_get_title(fitem)))
304                 return;
305
306         /* time of publishing, ... */
307         if (ctx->ditem->date_published != feed_item_get_date_published(fitem))
308                 return;
309
310         /* and the time of last modification must be greater
311          * (this means that the item was updated since deletion, and possibly
312          * contains new data, so we add it again) */
313         if (ctx->ditem->date_modified != feed_item_get_date_modified(fitem))
314                 return;
315
316         ctx->delete = FALSE;
317 }
318
319 /* Checks each item in deleted items list against feed and removes it if
320  * it is not found there anymore. */
321 void rssyl_deleted_expire(RFolderItem *ritem, Feed *feed)
322 {
323         GSList *d = NULL, *d2;
324         RDelExpireCtx *ctx = NULL;
325         RDeletedItem *ditem;
326
327         g_return_if_fail(ritem != NULL);
328         g_return_if_fail(feed != NULL);
329
330         ritem->deleted_items = rssyl_deleted_update(ritem);
331
332         /* Iterate over all items in the list */
333         d = ritem->deleted_items;
334         while (d) {
335                 ditem = (RDeletedItem *)d->data;
336                 ctx = g_new0(RDelExpireCtx, 1);
337                 ctx->ditem = ditem;
338                 ctx->delete = TRUE;
339
340                 /* Adjust ctx->delete accordingly */
341                 feed_foreach_item(feed, _rssyl_deleted_expire_func_f, (gpointer)ctx);
342
343                 /* Remove the item if necessary */
344                 if (ctx->delete) {
345                         debug_print("RSSyl: (DELETED) removing '%s' from list\n", ditem->title);
346                         d2 = d->next;
347                         ritem->deleted_items = g_slist_remove_link(ritem->deleted_items, d);
348                         d = d2;
349                         continue;
350                 } else {
351                         d = d->next;
352                 }
353
354                 g_free(ctx);
355         }
356
357         /* Write the new list to disk */
358         rssyl_deleted_store(ritem);
359
360         /* And clean up after myself */
361         rssyl_deleted_free(ritem->deleted_items);
362 }