01cb0da41b8758836fbcf2cdb1a40c995fcbef21
[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                 if (g_remove(deleted_file) != 0) {
175                         debug_print("RSSyl: Oops, couldn't delete '%s', bailing out\n",
176                                         deleted_file);
177                         return;
178                 }
179         }
180
181         if (g_slist_length(deleted_items) == 0)
182                 return;
183
184         if ((f = g_fopen(deleted_file, "w")) == NULL) {
185                 debug_print("RSSyl: Couldn't open '%s', bailing out.\n", deleted_file);
186                 return;
187         }
188
189         g_slist_foreach(deleted_items, (GFunc)_store_one_deleted_item,
190                         (gpointer)f);
191
192         fclose(f);
193         debug_print("RSSyl: written and closed deletion file\n");
194 }
195
196 void rssyl_deleted_store(RFolderItem *ritem)
197 {
198         gchar *path;
199
200         g_return_if_fail(ritem != NULL);
201
202         path = _deleted_file_path(ritem);
203         rssyl_deleted_store_internal(ritem->deleted_items, path);
204 }
205
206
207 /* Creates a FeedItem from a message file and uses the data to add a item
208  * to the list of deleted stuff. */
209 void rssyl_deleted_add(RFolderItem *ritem, gchar *path)
210 {
211         FeedItem *fitem = NULL;
212         RDeletedItem *ditem = NULL;
213         GSList *deleted_items = rssyl_deleted_update(ritem);
214         gchar *deleted_file = NULL;
215
216         if (!(fitem = rssyl_parse_folder_item_file(path)))
217                 return;
218
219         ditem = _new_deleted_item();
220         ditem->id = g_strdup(feed_item_get_id(fitem));
221         ditem->title = conv_unmime_header(feed_item_get_title(fitem),
222                         CS_UTF_8, FALSE);
223         ditem->date_published = feed_item_get_date_published(fitem);
224         ditem->date_modified = feed_item_get_date_modified(fitem);
225
226         deleted_items = g_slist_prepend(deleted_items, ditem);
227
228         deleted_file = _deleted_file_path(ritem);
229         rssyl_deleted_store_internal(deleted_items, deleted_file);
230         g_free(deleted_file);
231
232         rssyl_deleted_free(deleted_items);
233         feed_item_free(fitem);
234 }
235
236 static gint _rssyl_deleted_check_func(gconstpointer a, gconstpointer b)
237 {
238         RDeletedItem *ditem = (RDeletedItem *)a;
239         FeedItem *fitem = (FeedItem *)b;
240
241         g_return_val_if_fail(ditem != NULL, -10);
242         g_return_val_if_fail(fitem != NULL, -20);
243
244         /* Following must match:
245          * ID, ... */
246         if (!ditem->id || !feed_item_get_id(fitem) ||
247                         strcmp(ditem->id, feed_item_get_id(fitem)))
248                 return -1;
249
250         /* title, ... */
251         if (!ditem->title || !feed_item_get_title(fitem) ||
252                         strcmp(ditem->title, feed_item_get_title(fitem)))
253                 return -2;
254
255         /* time of publishing, ... */
256         if (ditem->date_published != -1 &&
257                         ditem->date_published != feed_item_get_date_published(fitem))
258                 return -3;
259
260         /* and the time of last modification must be greater
261          * (this means that the item was updated since deletion, and possibly
262          * contains new data, so we add it again) */
263         if (ditem->date_modified != -1 &&
264                         ditem->date_modified < feed_item_get_date_modified(fitem))
265                 return -4;
266
267         return 0;
268 }
269
270 /* Returns TRUE if fitem is found among the deleted stuff. */
271 gboolean rssyl_deleted_check(GSList *deleted_items, FeedItem *fitem)
272 {
273         if (g_slist_find_custom(deleted_items, (gconstpointer)fitem,
274                                 _rssyl_deleted_check_func) != NULL)
275                 return TRUE;
276
277         return FALSE;
278 }
279
280 /******** Expiring ********/
281 struct _RDelExpireCtx {
282         RDeletedItem *ditem;
283         gboolean delete;
284 };
285
286 typedef struct _RDelExpireCtx RDelExpireCtx;
287
288 static void _rssyl_deleted_expire_func_f(gpointer data, gpointer user_data)
289 {
290         FeedItem *fitem = (FeedItem *)data;
291         RDelExpireCtx *ctx = (RDelExpireCtx *)user_data;
292
293         /* Following must match:
294          * ID, ... */
295         if (!ctx->ditem->id || !feed_item_get_id(fitem) ||
296                         strcmp(ctx->ditem->id, feed_item_get_id(fitem)))
297                 return;
298
299         /* title, ... */
300         if (!ctx->ditem->title || !feed_item_get_title(fitem) ||
301                         strcmp(ctx->ditem->title, feed_item_get_title(fitem)))
302                 return;
303
304         /* time of publishing, ... */
305         if (ctx->ditem->date_published != feed_item_get_date_published(fitem))
306                 return;
307
308         /* and the time of last modification must be greater
309          * (this means that the item was updated since deletion, and possibly
310          * contains new data, so we add it again) */
311         if (ctx->ditem->date_modified != feed_item_get_date_modified(fitem))
312                 return;
313
314         ctx->delete = FALSE;
315 }
316
317 /* Checks each item in deleted items list against feed and removes it if
318  * it is not found there anymore. */
319 void rssyl_deleted_expire(RFolderItem *ritem, Feed *feed)
320 {
321         GSList *d = NULL, *d2;
322         RDelExpireCtx *ctx = NULL;
323         RDeletedItem *ditem;
324
325         g_return_if_fail(ritem != NULL);
326         g_return_if_fail(feed != NULL);
327
328         ritem->deleted_items = rssyl_deleted_update(ritem);
329
330         /* Iterate over all items in the list */
331         d = ritem->deleted_items;
332         while (d) {
333                 ditem = (RDeletedItem *)d->data;
334                 ctx = g_new0(RDelExpireCtx, 1);
335                 ctx->ditem = ditem;
336                 ctx->delete = TRUE;
337
338                 /* Adjust ctx->delete accordingly */
339                 feed_foreach_item(feed, _rssyl_deleted_expire_func_f, (gpointer)ctx);
340
341                 /* Remove the item if necessary */
342                 if (ctx->delete) {
343                         debug_print("RSSyl: (DELETED) removing '%s' from list\n", ditem->title);
344                         d2 = d->next;
345                         ritem->deleted_items = g_slist_remove_link(ritem->deleted_items, d);
346                         d = d2;
347                         continue;
348                 } else {
349                         d = d->next;
350                 }
351
352                 g_free(ctx);
353         }
354
355         /* Write the new list to disk */
356         rssyl_deleted_store(ritem);
357
358         /* And clean up after myself */
359         rssyl_deleted_free(ritem->deleted_items);
360 }