fix building RSSyl
[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 }
229
230 static gint _rssyl_deleted_check_func(gconstpointer a, gconstpointer b)
231 {
232         RDeletedItem *ditem = (RDeletedItem *)a;
233         FeedItem *fitem = (FeedItem *)b;
234
235         g_return_val_if_fail(ditem != NULL, -10);
236         g_return_val_if_fail(fitem != NULL, -20);
237
238         /* Following must match:
239          * ID, ... */
240         if (!ditem->id || !feed_item_get_id(fitem) ||
241                         strcmp(ditem->id, feed_item_get_id(fitem)))
242                 return -1;
243
244         /* title, ... */
245         if (!ditem->title || !feed_item_get_title(fitem) ||
246                         strcmp(ditem->title, feed_item_get_title(fitem)))
247                 return -2;
248
249         /* time of publishing, ... */
250         if (ditem->date_published != -1 &&
251                         ditem->date_published != feed_item_get_date_published(fitem))
252                 return -3;
253
254         /* and the time of last modification must be greater
255          * (this means that the item was updated since deletion, and possibly
256          * contains new data, so we add it again) */
257         if (ditem->date_modified != -1 &&
258                         ditem->date_modified < feed_item_get_date_modified(fitem))
259                 return -4;
260
261         return 0;
262 }
263
264 /* Returns TRUE if fitem is found among the deleted stuff. */
265 gboolean rssyl_deleted_check(GSList *deleted_items, FeedItem *fitem)
266 {
267         if (g_slist_find_custom(deleted_items, (gconstpointer)fitem,
268                                 _rssyl_deleted_check_func) != NULL)
269                 return TRUE;
270
271         return FALSE;
272 }
273
274 /******** Expiring ********/
275 struct _RDelExpireCtx {
276         RDeletedItem *ditem;
277         gboolean delete;
278 };
279
280 typedef struct _RDelExpireCtx RDelExpireCtx;
281
282 static void _rssyl_deleted_expire_func_f(gpointer data, gpointer user_data)
283 {
284         FeedItem *fitem = (FeedItem *)data;
285         RDelExpireCtx *ctx = (RDelExpireCtx *)user_data;
286
287         /* Following must match:
288          * ID, ... */
289         if (!ctx->ditem->id || !feed_item_get_id(fitem) ||
290                         strcmp(ctx->ditem->id, feed_item_get_id(fitem)))
291                 return;
292
293         /* title, ... */
294         if (!ctx->ditem->title || !feed_item_get_title(fitem) ||
295                         strcmp(ctx->ditem->title, feed_item_get_title(fitem)))
296                 return;
297
298         /* time of publishing, ... */
299         if (ctx->ditem->date_published != feed_item_get_date_published(fitem))
300                 return;
301
302         /* and the time of last modification must be greater
303          * (this means that the item was updated since deletion, and possibly
304          * contains new data, so we add it again) */
305         if (ctx->ditem->date_modified != feed_item_get_date_modified(fitem))
306                 return;
307
308         ctx->delete = FALSE;
309 }
310
311 /* Checks each item in deleted items list against feed and removes it if
312  * it is not found there anymore. */
313 void rssyl_deleted_expire(RFolderItem *ritem, Feed *feed)
314 {
315         GSList *d = NULL, *d2;
316         RDelExpireCtx *ctx = NULL;
317         RDeletedItem *ditem;
318
319         g_return_if_fail(ritem != NULL);
320         g_return_if_fail(feed != NULL);
321
322         ritem->deleted_items = rssyl_deleted_update(ritem);
323
324         /* Iterate over all items in the list */
325         d = ritem->deleted_items;
326         while (d) {
327                 ditem = (RDeletedItem *)d->data;
328                 ctx = g_new0(RDelExpireCtx, 1);
329                 ctx->ditem = ditem;
330                 ctx->delete = TRUE;
331
332                 /* Adjust ctx->delete accordingly */
333                 feed_foreach_item(feed, _rssyl_deleted_expire_func_f, (gpointer)ctx);
334
335                 /* Remove the item if necessary */
336                 if (ctx->delete) {
337                         debug_print("RSSyl: (DELETED) removing '%s' from list\n", ditem->title);
338                         d2 = d->next;
339                         ritem->deleted_items = g_slist_remove_link(ritem->deleted_items, d);
340                         d = d2;
341                         continue;
342                 } else {
343                         d = d->next;
344                 }
345
346                 g_free(ctx);
347         }
348
349         /* Write the new list to disk */
350         rssyl_deleted_store(ritem);
351
352         /* And clean up after myself */
353         rssyl_deleted_free(ritem->deleted_items);
354 }