RSSyl: fix memory leak in rssyl_deleted
[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
110         if (contents != NULL) {
111                 lines = strsplit_no_copy(contents, '\n');
112         } else {
113                 g_warning("Couldn't read '%s', ignoring\n", deleted_file);
114                 g_free(deleted_file);
115                 return NULL;
116         }
117
118         g_free(deleted_file);
119
120         while (lines[i]) {
121                 line = g_strsplit(lines[i], ": ", 2);
122                 if (line[0] && line[1] && strlen(line[0]) && strlen(line[1])) {
123                         if (!strcmp(line[0], "ID")) {
124                                 ditem = _new_deleted_item();
125                                 ditem->id = g_strdup(line[1]);
126                         } else if (ditem != NULL && !strcmp(line[0], "TITLE")) {
127                                 ditem->title = g_strdup(line[1]);
128                         } else if (ditem != NULL && !strcmp(line[0], "DPUB")) {
129                                 ditem->date_published = atoi(line[1]);
130                         } else if (ditem != NULL && !strcmp(line[0], "DMOD")) {
131                                 ditem->date_modified = atoi(line[1]);
132                                 deleted_items = g_slist_prepend(deleted_items, ditem);
133                                 ditem = NULL;
134                         }
135                 }
136
137                 g_strfreev(line);
138                 i++;
139         }
140
141         g_free(lines);
142         g_free(contents);
143
144         debug_print("RSSyl: got %d deleted items\n", g_slist_length(deleted_items));
145         return deleted_items;
146 }
147
148 static void _store_one_deleted_item(gpointer data, gpointer user_data)
149 {
150         RDeletedItem *ditem = (RDeletedItem *)data;
151         FILE *f = (FILE *)user_data;
152         gboolean err = FALSE;
153
154         if (ditem == NULL || ditem->id == NULL)
155                 return;
156
157         err |= (fprintf(f,
158                         "ID: %s\n"
159                         "TITLE: %s\n"
160                         "DPUB: %ld\n"
161                         "DMOD: %ld\n",
162                         ditem->id, ditem->title,
163                         ditem->date_published, ditem->date_modified) < 0);
164
165         if (err)
166                 debug_print("RSSyl: Error during writing deletion file.\n");
167 }
168
169 static void rssyl_deleted_store_internal(GSList *deleted_items, const gchar *deleted_file)
170 {
171         FILE *f;
172
173         if (g_file_test(deleted_file, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
174                 g_remove(deleted_file);
175
176         if (g_slist_length(deleted_items) == 0)
177                 return;
178
179         if ((f = g_fopen(deleted_file, "w")) == NULL) {
180                 debug_print("RSSyl: Couldn't open '%s', ignoring.\n", deleted_file);
181                 return;
182         }
183
184         g_slist_foreach(deleted_items, (GFunc)_store_one_deleted_item,
185                         (gpointer)f);
186         
187         fclose(f);
188         debug_print("RSSyl: written and closed deletion file\n");
189 }
190
191 void rssyl_deleted_store(RFolderItem *ritem)
192 {
193         gchar *path;
194
195         g_return_if_fail(ritem != NULL);
196
197         path = _deleted_file_path(ritem);
198         rssyl_deleted_store_internal(ritem->deleted_items, path);
199 }
200
201
202 /* Creates a FeedItem from a message file and uses the data to add a item
203  * to the list of deleted stuff. */
204 void rssyl_deleted_add(RFolderItem *ritem, gchar *path)
205 {
206         FeedItem *fitem = NULL;
207         RDeletedItem *ditem = NULL;
208         GSList *deleted_items = rssyl_deleted_update(ritem);
209         gchar *deleted_file = NULL;
210
211         if (!(fitem = rssyl_parse_folder_item_file(path)))
212                 return;
213
214         ditem = _new_deleted_item();
215         ditem->id = g_strdup(feed_item_get_id(fitem));
216         ditem->title = conv_unmime_header(feed_item_get_title(fitem),
217                         CS_UTF_8, FALSE);
218         ditem->date_published = feed_item_get_date_published(fitem);
219         ditem->date_modified = feed_item_get_date_modified(fitem);
220
221         deleted_items = g_slist_prepend(deleted_items, ditem);
222
223         deleted_file = _deleted_file_path(ritem);
224         rssyl_deleted_store_internal(deleted_items, deleted_file);
225         g_free(deleted_file);
226
227         rssyl_deleted_free(deleted_items);
228         feed_item_free(fitem);
229 }
230
231 static gint _rssyl_deleted_check_func(gconstpointer a, gconstpointer b)
232 {
233         RDeletedItem *ditem = (RDeletedItem *)a;
234         FeedItem *fitem = (FeedItem *)b;
235
236         g_return_val_if_fail(ditem != NULL, -10);
237         g_return_val_if_fail(fitem != NULL, -20);
238
239         /* Following must match:
240          * ID, ... */
241         if (!ditem->id || !feed_item_get_id(fitem) ||
242                         strcmp(ditem->id, feed_item_get_id(fitem)))
243                 return -1;
244
245         /* title, ... */
246         if (!ditem->title || !feed_item_get_title(fitem) ||
247                         strcmp(ditem->title, feed_item_get_title(fitem)))
248                 return -2;
249
250         /* time of publishing, ... */
251         if (ditem->date_published != -1 &&
252                         ditem->date_published != feed_item_get_date_published(fitem))
253                 return -3;
254
255         /* and the time of last modification must be greater
256          * (this means that the item was updated since deletion, and possibly
257          * contains new data, so we add it again) */
258         if (ditem->date_modified != -1 &&
259                         ditem->date_modified < feed_item_get_date_modified(fitem))
260                 return -4;
261
262         return 0;
263 }
264
265 /* Returns TRUE if fitem is found among the deleted stuff. */
266 gboolean rssyl_deleted_check(GSList *deleted_items, FeedItem *fitem)
267 {
268         if (g_slist_find_custom(deleted_items, (gconstpointer)fitem,
269                                 _rssyl_deleted_check_func) != NULL)
270                 return TRUE;
271
272         return FALSE;
273 }
274
275 /******** Expiring ********/
276 struct _RDelExpireCtx {
277         RDeletedItem *ditem;
278         gboolean delete;
279 };
280
281 typedef struct _RDelExpireCtx RDelExpireCtx;
282
283 static void _rssyl_deleted_expire_func_f(gpointer data, gpointer user_data)
284 {
285         FeedItem *fitem = (FeedItem *)data;
286         RDelExpireCtx *ctx = (RDelExpireCtx *)user_data;
287
288         /* Following must match:
289          * ID, ... */
290         if (!ctx->ditem->id || !feed_item_get_id(fitem) ||
291                         strcmp(ctx->ditem->id, feed_item_get_id(fitem)))
292                 return;
293
294         /* title, ... */
295         if (!ctx->ditem->title || !feed_item_get_title(fitem) ||
296                         strcmp(ctx->ditem->title, feed_item_get_title(fitem)))
297                 return;
298
299         /* time of publishing, ... */
300         if (ctx->ditem->date_published != feed_item_get_date_published(fitem))
301                 return;
302
303         /* and the time of last modification must be greater
304          * (this means that the item was updated since deletion, and possibly
305          * contains new data, so we add it again) */
306         if (ctx->ditem->date_modified != feed_item_get_date_modified(fitem))
307                 return;
308
309         ctx->delete = FALSE;
310 }
311
312 /* Checks each item in deleted items list against feed and removes it if
313  * it is not found there anymore. */
314 void rssyl_deleted_expire(RFolderItem *ritem, Feed *feed)
315 {
316         GSList *d = NULL, *d2;
317         RDelExpireCtx *ctx = NULL;
318         RDeletedItem *ditem;
319
320         g_return_if_fail(ritem != NULL);
321         g_return_if_fail(feed != NULL);
322
323         ritem->deleted_items = rssyl_deleted_update(ritem);
324
325         /* Iterate over all items in the list */
326         d = ritem->deleted_items;
327         while (d) {
328                 ditem = (RDeletedItem *)d->data;
329                 ctx = g_new0(RDelExpireCtx, 1);
330                 ctx->ditem = ditem;
331                 ctx->delete = TRUE;
332
333                 /* Adjust ctx->delete accordingly */
334                 feed_foreach_item(feed, _rssyl_deleted_expire_func_f, (gpointer)ctx);
335
336                 /* Remove the item if necessary */
337                 if (ctx->delete) {
338                         debug_print("RSSyl: (DELETED) removing '%s' from list\n", ditem->title);
339                         d2 = d->next;
340                         ritem->deleted_items = g_slist_remove_link(ritem->deleted_items, d);
341                         d = d2;
342                         continue;
343                 } else {
344                         d = d->next;
345                 }
346
347                 g_free(ctx);
348         }
349
350         /* Write the new list to disk */
351         rssyl_deleted_store(ritem);
352
353         /* And clean up after myself */
354         rssyl_deleted_free(ritem->deleted_items);
355 }