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