0f9ac06379e9d3b8238013148cfe1cc9914a7f38
[claws.git] / src / plugins / rssyl / rssyl.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  * - s-c folderclass callback handler functions
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 <glib/gi18n.h>
30 #include <curl/curl.h>
31
32 /* Claws Mail includes */
33 #include <folder.h>
34 #include <procmsg.h>
35 #include <localfolder.h>
36 #include <common/utils.h>
37 #include <main.h>
38 #include <mh.h>
39 #include <xml.h>
40 #include <toolbar.h>
41 #include <prefs_toolbar.h>
42 #include <utils.h>
43
44 /* Local includes */
45 #include "libfeed/feeditem.h"
46 #include "rssyl.h"
47 #include "rssyl_deleted.h"
48 #include "rssyl_gtk.h"
49 #include "rssyl_feed.h"
50 #include "rssyl_prefs.h"
51 #include "rssyl_update_feed.h"
52 #include "rssyl_update_format.h"
53 #include "opml_import.h"
54 #include "opml_export.h"
55 #include "strutils.h"
56
57 FolderClass rssyl_class;
58
59 static gint rssyl_create_tree(Folder *folder);
60 static gint rssyl_scan_tree(Folder *folder);
61
62 static gboolean existing_tree_found = FALSE;
63
64 static void rssyl_init_read_func(FolderItem *item, gpointer data)
65 {
66         RFolderItem *ritem = (RFolderItem *)item;
67         RPrefs *rsprefs = NULL;
68
69         if( !IS_RSSYL_FOLDER_ITEM(item) )
70                 return;
71
72         existing_tree_found = TRUE;
73
74         /* Don't do anything if we're on root of our folder tree or on
75          * a regular folder (no feed) */
76         if( folder_item_parent(item) == NULL || ritem->url == NULL )
77                 return;
78
79         ritem->refresh_id = 0;
80
81         /* Start automatic refresh timer, if necessary */
82         if( ritem->default_refresh_interval ) {
83                 rsprefs = rssyl_prefs_get();
84                 if( !rsprefs->refresh_enabled )
85                         return;
86
87                 ritem->refresh_interval = rsprefs->refresh;
88         }
89
90         /* Start the timer, if determined interval is >0 */
91         if( ritem->refresh_interval > 0 )
92                 rssyl_feed_start_refresh_timeout(ritem);
93 }
94
95 static void rssyl_make_rc_dir(void)
96 {
97         gchar *rssyl_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
98                         NULL);
99
100         if( !is_dir_exist(rssyl_dir) ) {
101                 if( make_dir(rssyl_dir) < 0 ) {
102                         g_warning("couldn't create directory %s\n", rssyl_dir);
103                 }
104
105                 debug_print("RSSyl: created directory %s\n", rssyl_dir);
106         }
107
108         g_free(rssyl_dir);
109 }
110
111 static void rssyl_create_default_mailbox(void)
112 {
113         Folder *root = NULL;
114
115         rssyl_make_rc_dir();
116
117         root = folder_new(rssyl_folder_get_class(), RSSYL_DEFAULT_MAILBOX, NULL);
118
119         g_return_if_fail(root != NULL);
120         folder_add(root);
121
122         rssyl_scan_tree(root);
123
124         /* FIXME: subscribe default feed */
125 //      rssyl_subscribe_new_feed(item, RSSYL_DEFAULT_FEED, TRUE);
126 }
127
128 static gboolean rssyl_update_all_feeds_deferred(gpointer data)
129 {
130         rssyl_update_all_feeds();
131         return FALSE;
132 }
133
134 static void rssyl_toolbar_cb_refresh_all_feeds(gpointer parent, const gchar *item_name, gpointer data)
135 {
136         rssyl_update_all_feeds();
137 }
138
139 void rssyl_init(void)
140 {
141         folder_register_class(rssyl_folder_get_class());
142
143         rssyl_gtk_init();
144         rssyl_make_rc_dir();
145
146         rssyl_prefs_init();
147
148         folder_func_to_all_folders((FolderItemFunc)rssyl_init_read_func, NULL);
149
150         if( !existing_tree_found )
151                 rssyl_create_default_mailbox();
152         else
153                 rssyl_update_format();
154
155         prefs_toolbar_register_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"), rssyl_toolbar_cb_refresh_all_feeds, NULL);
156
157         if( rssyl_prefs_get()->refresh_on_startup &&
158                         claws_is_starting() )
159                 g_timeout_add(2000, rssyl_update_all_feeds_deferred, NULL);
160 }
161
162 void rssyl_done(void)
163 {
164         rssyl_opml_export();
165
166         prefs_toolbar_unregister_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"));
167
168         rssyl_prefs_done();
169         rssyl_gtk_done();
170
171         if( !claws_is_exiting() )
172                 folder_unregister_class(rssyl_folder_get_class());
173
174         debug_print("RSSyl is done\n");
175 }
176
177 static gchar *rssyl_get_new_msg_filename(FolderItem *dest)
178 {
179         gchar *destfile;
180         gchar *destpath;
181
182         destpath = folder_item_get_path(dest);
183         g_return_val_if_fail(destpath != NULL, NULL);
184
185         if( !is_dir_exist(destpath) )
186                 make_dir_hier(destpath);
187
188         for( ; ; ) {
189                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
190                                 dest->last_num + 1);
191                 if( is_file_entry_exist(destfile) ) {
192                         dest->last_num++;
193                         g_free(destfile);
194                 } else
195                         break;
196         }
197
198         g_free(destpath);
199
200         return destfile;
201 }
202
203 static void rssyl_get_last_num(Folder *folder, FolderItem *item)
204 {
205         gchar *path;
206         DIR *dp;
207         struct dirent *d;
208         gint max = 0;
209         gint num;
210
211         g_return_if_fail(item != NULL);
212
213         debug_print("rssyl_get_last_num(): Scanning %s ...\n", item->path);
214         path = folder_item_get_path(item);
215         g_return_if_fail(path != NULL);
216
217         if( (dp = opendir(path)) == NULL ) {
218                 FILE_OP_ERROR(item->path, "opendir");
219                 g_free(path);
220                 return;
221         }
222
223         g_free(path);
224
225         while( (d = readdir(dp)) != NULL ) {
226                 if( (num = to_number(d->d_name)) > 0 && dirent_is_regular_file(d) ) {
227                         if( max < num )
228                                 max = num;
229                 }
230         }
231         closedir(dp);
232
233         debug_print("Last number in dir %s = %d\n", item->path, max);
234         item->last_num = max;
235 }
236
237 static Folder *rssyl_new_folder(const gchar *name, const gchar *path)
238 {
239         Folder *folder;
240
241         debug_print("RSSyl: new_folder: %s (%s)\n", name, path);
242
243         rssyl_make_rc_dir();
244
245         folder = g_new0(Folder, 1);
246         FOLDER(folder)->klass = &rssyl_class;
247         folder_init(FOLDER(folder), name);
248
249         return FOLDER(folder);
250 }
251
252 static void rssyl_destroy_folder(Folder *folder)
253 {
254         folder_local_folder_destroy(LOCAL_FOLDER(folder));
255 }
256
257 static void rssyl_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
258 {
259         GList *cur;
260         RFolderItem *ritem = (RFolderItem *)item;
261
262         folder_item_set_xml(folder, item, tag);
263
264         for( cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
265                 XMLAttr *attr = (XMLAttr *) cur->data;
266
267                 if( !attr || !attr->name || !attr->value)
268                         continue;
269
270                 /* (str) URL */
271                 if( !strcmp(attr->name, "uri")) {
272                         g_free(ritem->url);
273                         ritem->url = g_strdup(attr->value);
274                 }
275                 /* (str) Official title */
276                 if( !strcmp(attr->name, "official_title")) {
277                         g_free(ritem->official_title);
278                         ritem->official_title = g_strdup(attr->value);
279                 }
280                 /* (bool) Keep old items */
281                 if( !strcmp(attr->name, "keep_old"))
282                         ritem->keep_old = (atoi(attr->value) == 0 ? FALSE : TRUE );
283                 /* (bool) Use default refresh_interval */
284                 if( !strcmp(attr->name, "default_refresh_interval"))
285                         ritem->default_refresh_interval = (atoi(attr->value) == 0 ? FALSE : TRUE );
286                 /* (int) Refresh interval */
287                 if( !strcmp(attr->name, "refresh_interval"))
288                         ritem->refresh_interval = atoi(attr->value);
289                 /* (bool) Fetch comments */
290                 if( !strcmp(attr->name, "fetch_comments"))
291                         ritem->fetch_comments = (atoi(attr->value) == 0 ? FALSE : TRUE );
292                 /* (int) Max age of posts to fetch comments for */
293                 if( !strcmp(attr->name, "fetch_comments_max_age"))
294                         ritem->fetch_comments_max_age = atoi(attr->value);
295                 /* (bool) Write heading */
296                 if( !strcmp(attr->name, "write_heading"))
297                         ritem->write_heading = (atoi(attr->value) == 0 ? FALSE : TRUE );
298                 /* (int) Silent update */
299                 if( !strcmp(attr->name, "silent_update"))
300                         ritem->silent_update = atoi(attr->value);
301                 /* (bool) Ignore title rename */
302                 if( !strcmp(attr->name, "ignore_title_rename"))
303                         ritem->ignore_title_rename = (atoi(attr->value) == 0 ? FALSE : TRUE );
304                 /* (bool) Verify SSL peer  */
305                 if( !strcmp(attr->name, "ssl_verify_peer"))
306                         ritem->ssl_verify_peer = (atoi(attr->value) == 0 ? FALSE : TRUE );
307         }
308 }
309
310 static XMLTag *rssyl_item_get_xml(Folder *folder, FolderItem *item)
311 {
312         XMLTag *tag;
313         RFolderItem *ri = (RFolderItem *)item;
314         gchar *tmp = NULL;
315
316         tag = folder_item_get_xml(folder, item);
317
318         /* (str) URL */
319         if( ri->url != NULL )
320                 xml_tag_add_attr(tag, xml_attr_new("uri", ri->url));
321         /* (str) Official title */
322         if( ri->official_title != NULL )
323                 xml_tag_add_attr(tag, xml_attr_new("official_title", ri->official_title));
324         /* (bool) Keep old items */
325         xml_tag_add_attr(tag, xml_attr_new("keep_old",
326                                 (ri->keep_old ? "1" : "0")) );
327         /* (bool) Use default refresh interval */
328         xml_tag_add_attr(tag, xml_attr_new("default_refresh_interval",
329                                 (ri->default_refresh_interval ? "1" : "0")) );
330         /* (int) Refresh interval */
331         tmp = g_strdup_printf("%d", ri->refresh_interval);
332         xml_tag_add_attr(tag, xml_attr_new("refresh_interval", tmp));
333         g_free(tmp);
334         /* (bool) Fetch comments */
335         xml_tag_add_attr(tag, xml_attr_new("fetch_comments",
336                                 (ri->fetch_comments ? "1" : "0")) );
337         /* (int) Max age of posts to fetch comments for */
338         tmp = g_strdup_printf("%d", ri->fetch_comments_max_age);
339         xml_tag_add_attr(tag, xml_attr_new("fetch_comments_max_age", tmp));
340         g_free(tmp);
341         /* (bool) Write heading */
342         xml_tag_add_attr(tag, xml_attr_new("write_heading",
343                                 (ri->write_heading ? "1" : "0")) );
344         /* (int) Silent update */
345         tmp = g_strdup_printf("%d", ri->silent_update);
346         xml_tag_add_attr(tag, xml_attr_new("silent_update", tmp));
347         g_free(tmp);
348         /* (bool) Ignore title rename */
349         xml_tag_add_attr(tag, xml_attr_new("ignore_title_rename",
350                                 (ri->ignore_title_rename ? "1" : "0")) );
351         /* (bool) Verify SSL peer */
352         xml_tag_add_attr(tag, xml_attr_new("ssl_verify_peer",
353                                 (ri->ssl_verify_peer ? "1" : "0")) );
354
355         return tag;
356 }
357
358 static gint rssyl_scan_tree(Folder *folder)
359 {
360         g_return_val_if_fail(folder != NULL, -1);
361
362         folder->outbox = NULL;
363         folder->draft = NULL;
364         folder->queue = NULL;
365         folder->trash = NULL;
366
367         debug_print("RSSyl: scanning tree\n");
368         rssyl_create_tree(folder);
369
370         return 0;
371 }
372
373 static gint rssyl_create_tree(Folder *folder)
374 {
375         FolderItem *rootitem;
376         GNode *rootnode;
377
378         g_return_val_if_fail(folder != NULL, -1);
379
380         rssyl_make_rc_dir();
381
382         if( !folder->node ) {
383                 rootitem = folder_item_new(folder, folder->name, NULL);
384                 rootitem->folder = folder;
385                 rootnode = g_node_new(rootitem);
386                 folder->node = rootnode;
387                 rootitem->node = rootnode;
388         }
389
390         debug_print("RSSyl: created new rssyl tree\n");
391         return 0;
392 }
393
394 static FolderItem *rssyl_item_new(Folder *folder)
395 {
396         RFolderItem *ritem = g_new0(RFolderItem, 1);
397
398         ritem->url = NULL;
399         ritem->official_title = NULL;
400         ritem->source_id = NULL;
401         ritem->items = NULL;
402         ritem->keep_old = FALSE;
403         ritem->default_refresh_interval = TRUE;
404         ritem->refresh_interval = atoi(PREF_DEFAULT_REFRESH);
405         ritem->fetch_comments = FALSE;
406         ritem->fetch_comments_max_age = -1;
407         ritem->write_heading = TRUE;
408         ritem->fetching_comments = FALSE;
409         ritem->silent_update = 0;
410         ritem->last_update = 0;
411         ritem->ignore_title_rename = FALSE;
412
413         return (FolderItem *)ritem;
414 }
415
416 static void rssyl_item_destroy(Folder *folder, FolderItem *item)
417 {
418         RFolderItem *ritem = (RFolderItem *)item;
419
420         g_return_if_fail(ritem != NULL);
421
422         g_free(ritem->url);
423         g_free(ritem->official_title);
424         g_slist_free(ritem->items);
425
426         /* Remove a scheduled refresh, if any */
427         if( ritem->refresh_id != 0)
428                 g_source_remove(ritem->refresh_id);
429
430         g_free(ritem);
431 }
432
433 static FolderItem *rssyl_create_folder(Folder *folder,
434                                                                 FolderItem *parent, const gchar *name)
435 {
436         gchar *path = NULL, *basepath = NULL, *itempath = NULL;
437         FolderItem *newitem = NULL;
438
439         g_return_val_if_fail(folder != NULL, NULL);
440         g_return_val_if_fail(parent != NULL, NULL);
441         g_return_val_if_fail(name != NULL, NULL);
442
443         path = folder_item_get_path(parent);
444         if( !is_dir_exist(path) ) {
445                 if( (make_dir_hier(path) != 0) ) {
446                         debug_print("RSSyl: Couldn't create directory (rec) '%s'\n", path);
447                         return NULL;
448                 }
449         }
450
451         basepath = g_strdelimit(g_strdup(name), G_DIR_SEPARATOR_S, '_');
452         path = g_strconcat(path, G_DIR_SEPARATOR_S, basepath, NULL);
453
454         if( make_dir(path) < 0 ) {
455                 debug_print("RSSyl: Couldn't create directory '%s'\n", path);
456                 g_free(path);
457                 g_free(basepath);
458                 return NULL;
459         }
460         g_free(path);
461
462         itempath = g_strconcat((parent->path ? parent->path : ""),
463                         G_DIR_SEPARATOR_S, basepath, NULL);
464         newitem = folder_item_new(folder, name, itempath);
465         g_free(itempath);
466         g_free(basepath);
467
468         folder_item_append(parent, newitem);
469
470         return newitem;
471 }
472
473 FolderItem *rssyl_get_root_folderitem(FolderItem *item)
474 {
475         FolderItem *i;
476
477         for( i = item; folder_item_parent(i) != NULL; i = folder_item_parent(i) ) { }
478         return i;
479 }
480
481 static gchar *rssyl_item_get_path(Folder *folder, FolderItem *item)
482 {
483         gchar *path, *name;
484
485         g_return_val_if_fail(folder != NULL, NULL);
486         g_return_val_if_fail(item != NULL, NULL);
487
488         debug_print("RSSyl: item_get_path\n");
489
490         name = folder_item_get_name(rssyl_get_root_folderitem(item));
491         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
492                         G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S, item->path, NULL);
493         g_free(name);
494
495         return path;
496 }
497
498 static gboolean rssyl_rename_folder_func(GNode *node, gpointer data)
499 {
500         FolderItem *item = node->data;
501         gchar **paths = data;
502         const gchar *oldpath = paths[0];
503         const gchar *newpath = paths[1];
504         gchar *base;
505         gchar *new_itempath;
506         gint oldpathlen;
507
508         oldpathlen = strlen(oldpath);
509         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
510                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
511                 return TRUE;
512         }
513
514         base = item->path + oldpathlen;
515         while (*base == G_DIR_SEPARATOR) base++;
516         if (*base == '\0')
517                 new_itempath = g_strdup(newpath);
518         else
519                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
520                                 NULL);
521         g_free(item->path);
522         item->path = new_itempath;
523
524         return FALSE;
525 }
526
527 static gint rssyl_rename_folder(Folder *folder, FolderItem *item,
528                                 const gchar *name)
529 {
530         gchar *oldpath;
531         gchar *dirname;
532         gchar *newpath, *utf8newpath;
533         gchar *basenewpath;
534         gchar *paths[2];
535
536         g_return_val_if_fail(folder != NULL, -1);
537         g_return_val_if_fail(item != NULL, -1);
538         g_return_val_if_fail(item->path != NULL, -1);
539         g_return_val_if_fail(name != NULL, -1);
540
541         debug_print("RSSyl: rssyl_rename_folder '%s' -> '%s'\n",
542                         item->name, name);
543
544         if (!strcmp(item->name, name))
545                         return 0;
546
547         oldpath = folder_item_get_path(item);
548         if( !is_dir_exist(oldpath) )
549                 make_dir_hier(oldpath);
550
551         dirname = g_path_get_dirname(oldpath);
552         basenewpath = g_strdelimit(g_strdup(name), G_DIR_SEPARATOR_S, '_');
553         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, basenewpath, NULL);
554         g_free(basenewpath);
555
556         if( g_rename(oldpath, newpath) < 0 ) {
557                 FILE_OP_ERROR(oldpath, "rename");
558                 g_free(oldpath);
559                 g_free(newpath);
560                 return -1;
561         }
562
563         g_free(oldpath);
564         g_free(newpath);
565
566         if( strchr(item->path, G_DIR_SEPARATOR) != NULL ) {
567                 dirname = g_path_get_dirname(item->path);
568                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
569                 g_free(dirname);
570         } else
571                 utf8newpath = g_strdup(name);
572
573         g_free(item->name);
574         item->name = g_strdup(name);
575
576         paths[0] = g_strdup(item->path);
577         paths[1] = utf8newpath;
578         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
579                         rssyl_rename_folder_func, paths);
580
581         g_free(paths[0]);
582         g_free(paths[1]);
583
584         return 0;
585 }
586
587 static gint rssyl_remove_folder(Folder *folder, FolderItem *item)
588 {
589         gchar *path = NULL;
590
591         g_return_val_if_fail(folder != NULL, -1);
592         g_return_val_if_fail(item != NULL, -1);
593         g_return_val_if_fail(item->path != NULL, -1);
594         g_return_val_if_fail(item->stype == F_NORMAL, -1);
595
596         debug_print("RSSyl: removing folder item %s\n", item->path);
597
598         path = folder_item_get_path(item);
599         if( remove_dir_recursive(path) < 0 ) {
600                 g_warning("can't remove directory '%s'\n", path);
601                 g_free(path);
602                 return -1;
603         }
604
605         g_free(path);
606         folder_item_remove(item);
607
608         return 0;
609 }
610
611 static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
612                 MsgNumberList **list, gboolean *old_uids_valid)
613 {
614         gchar *path;
615         DIR *dp;
616         struct dirent *d;
617         gint num, nummsgs = 0;
618
619         g_return_val_if_fail(item != NULL, -1);
620
621         debug_print("RSSyl: get_num_list: scanning '%s'\n", item->path);
622
623         *old_uids_valid = TRUE;
624         
625         path = folder_item_get_path(item);
626         g_return_val_if_fail(path != NULL, -1);
627
628         if( (dp = opendir(path)) == NULL ) {
629                 FILE_OP_ERROR(item->path, "opendir");
630                 g_free(path);
631                 return -1;
632         }
633
634         g_free(path);
635
636         while( (d = readdir(dp)) != NULL ) {
637                 if( (num = to_number(d->d_name)) > 0 ) {
638                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
639                         nummsgs++;
640                 }
641         }
642
643         closedir(dp);
644
645         debug_print("RSSyl: get_num_list: returning %d\n", nummsgs);
646
647         return nummsgs;
648 }
649
650 static gboolean rssyl_is_msg_changed(Folder *folder, FolderItem *item,
651                 MsgInfo *msginfo)
652 {
653         struct stat s;
654         gchar *path = NULL;
655
656         g_return_val_if_fail(folder != NULL, FALSE);
657         g_return_val_if_fail(item != NULL, FALSE);
658         g_return_val_if_fail(msginfo != NULL, FALSE);
659
660         path = g_strconcat(folder_item_get_path(item), G_DIR_SEPARATOR_S,
661                         itos(msginfo->msgnum), NULL);
662
663         if (g_stat(path, &s) < 0 ||
664                 msginfo->size != s.st_size || (
665                                 (msginfo->mtime - s.st_mtime != 0) &&
666                                 (msginfo->mtime - s.st_mtime != 3600) &&
667                                 (msginfo->mtime - s.st_mtime != -3600))) {
668                 g_free(path);
669                 return TRUE;
670         }
671
672         g_free(path);
673         return FALSE;
674 }
675
676 static gchar *rssyl_fetch_msg(Folder *folder, FolderItem *item, gint num)
677 {
678         gchar *path;
679         gchar *file;
680
681         g_return_val_if_fail(item != NULL, NULL);
682         g_return_val_if_fail(num > 0, NULL);
683
684         path = folder_item_get_path(item);
685         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
686         g_free(path);
687
688         debug_print("RSSyl: fetch_msg '%s'\n", file);
689
690         if( !is_file_exist(file)) {
691                 g_free(file);
692                 return NULL;
693         }
694
695         return file;
696 }
697
698 static MsgInfo *rssyl_get_msginfo(Folder *folder, FolderItem *item, gint num)
699 {
700         MsgInfo *msginfo = NULL;
701         gchar *file;
702         MsgFlags flags;
703
704         g_return_val_if_fail(folder != NULL, NULL);
705         g_return_val_if_fail(item != NULL, NULL);
706         g_return_val_if_fail(num > 0, NULL);
707
708         debug_print("RSSyl: get_msginfo: %d\n", num);
709
710         file = rssyl_fetch_msg(folder, item, num);
711         g_return_val_if_fail(file != NULL, NULL);
712
713         flags.perm_flags = 0;
714         flags.tmp_flags = 0;
715
716         msginfo = rssyl_feed_parse_item_to_msginfo(file, flags, TRUE, TRUE, item);
717         g_free(file);
718
719         if( msginfo )
720                 msginfo->msgnum = num;
721
722         return msginfo;
723 }
724
725 static gint rssyl_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
726                 GHashTable *relation)
727 {
728         gchar *destfile;
729         GSList *cur;
730         MsgFileInfo *fileinfo;
731
732         g_return_val_if_fail(dest != NULL, -1);
733         g_return_val_if_fail(file_list != NULL, -1);
734
735         if( dest->last_num < 0 ) {
736                 rssyl_get_last_num(folder, dest);
737                 if( dest->last_num < 0 ) return -1;
738         }
739
740         for( cur = file_list; cur != NULL; cur = cur->next ) {
741                 fileinfo = (MsgFileInfo *)cur->data;
742
743                 destfile = rssyl_get_new_msg_filename(dest);
744                 g_return_val_if_fail(destfile != NULL, -1);
745                 debug_print("RSSyl: add_msgs: new filename is '%s'\n", destfile);
746
747                 if( link(fileinfo->file, destfile) < 0 ) {
748                         if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
749                                 g_warning("can't copy message %s to %s\n", fileinfo->file, destfile);
750                                 g_free(destfile);
751                                 return -1;
752                         }
753                 }
754
755                 if( relation != NULL )
756                         g_hash_table_insert(relation, fileinfo->msginfo != NULL ?
757                                         (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
758                                         GINT_TO_POINTER(dest->last_num + 1));
759                 g_free(destfile);
760                 dest->last_num++;
761         }
762
763
764         return dest->last_num;
765 }
766
767 static gint rssyl_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
768                 MsgFlags *flags)
769 {
770         GSList file_list;
771         MsgFileInfo fileinfo;
772
773         g_return_val_if_fail(file != NULL, -1);
774
775         fileinfo.msginfo = NULL;
776         fileinfo.file = (gchar *)file;
777         fileinfo.flags = flags;
778         file_list.data = &fileinfo;
779         file_list.next = NULL;
780
781         return rssyl_add_msgs(folder, dest, &file_list, NULL);
782 }
783
784 static gint rssyl_remove_msg(Folder *folder, FolderItem *item, gint num)
785 {
786         gboolean need_scan = FALSE;
787         gchar *file, *tmp;
788
789         g_return_val_if_fail(item != NULL, -1);
790
791         file = rssyl_fetch_msg(folder, item, num);
792         g_return_val_if_fail(file != NULL, -1);
793
794         need_scan = mh_get_class()->scan_required(folder, item);
795
796         /* are we doing a folder move ? */
797         tmp = g_strdup_printf("%s.tmp", file);
798         if (is_file_exist(tmp)) {
799                 g_unlink(tmp);
800                 g_free(tmp);
801                 g_free(file);
802                 return 0;
803         }
804         g_free(tmp);
805
806         rssyl_deleted_add((RFolderItem *)item, file);
807
808         if( g_unlink(file) < 0 ) {
809                 FILE_OP_ERROR(file, "unlink");
810                 g_free(file);
811                 return -1;
812         }
813
814         if( !need_scan )
815                 item->mtime = time(NULL);
816
817         g_free(file);
818         return 0;
819 }
820
821 static gboolean rssyl_subscribe_uri(Folder *folder, const gchar *uri)
822 {
823         if (folder->klass != rssyl_folder_get_class())
824                 return FALSE;
825         return (rssyl_feed_subscribe_new(FOLDER_ITEM(folder->node->data), uri, FALSE) ?
826                         TRUE : FALSE);
827 }
828
829 static void rssyl_copy_private_data(Folder *folder, FolderItem *oldi,
830                 FolderItem *newi)
831 {
832         gchar *dpathold, *dpathnew;
833         RFolderItem *olditem = (RFolderItem *)oldi,
834                                                                         *newitem = (RFolderItem *)newi;
835
836         g_return_if_fail(folder != NULL);
837         g_return_if_fail(olditem != NULL);
838         g_return_if_fail(newitem != NULL);
839
840         if (olditem->url != NULL) {
841                 g_free(newitem->url);
842                 newitem->url = g_strdup(olditem->url);
843         }
844
845         if (olditem->official_title != NULL) {
846                 g_free(newitem->official_title);
847                 newitem->official_title = g_strdup(olditem->official_title);
848         }
849
850         if (olditem->source_id != NULL) {
851                 g_free(newitem->source_id);
852                 newitem->source_id = g_strdup(olditem->source_id);
853         }
854
855         newitem->keep_old = olditem->keep_old;
856         newitem->default_refresh_interval = olditem->default_refresh_interval;
857         newitem->refresh_interval = olditem->refresh_interval;
858         newitem->fetch_comments = olditem->fetch_comments;
859         newitem->fetch_comments_max_age = olditem->fetch_comments_max_age;
860         newitem->silent_update = olditem->silent_update;
861         newitem->write_heading = olditem->write_heading;
862         newitem->ignore_title_rename = olditem->ignore_title_rename;
863         newitem->ssl_verify_peer = olditem->ssl_verify_peer;
864         newitem->refresh_id = olditem->refresh_id;
865         newitem->fetching_comments = olditem->fetching_comments;
866         newitem->last_update = olditem->last_update;
867
868         dpathold = g_strconcat(rssyl_item_get_path(oldi->folder, oldi),
869                         G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
870         dpathnew = g_strconcat(rssyl_item_get_path(newi->folder, newi),
871                         G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
872         move_file(dpathold, dpathnew, TRUE);
873         g_free(dpathold);
874         g_free(dpathnew);
875
876 }
877
878 /************************************************************************/
879
880 FolderClass *rssyl_folder_get_class()
881 {
882         if( rssyl_class.idstr == NULL ) {
883                 rssyl_class.type = F_UNKNOWN;
884                 rssyl_class.idstr = "rssyl";
885                 rssyl_class.uistr = "RSSyl";
886
887                 /* Folder functions */
888                 rssyl_class.new_folder = rssyl_new_folder;
889                 rssyl_class.destroy_folder = rssyl_destroy_folder;
890                 rssyl_class.set_xml = folder_set_xml;
891                 rssyl_class.get_xml = folder_get_xml;
892                 rssyl_class.scan_tree = rssyl_scan_tree;
893                 rssyl_class.create_tree = rssyl_create_tree;
894
895                 /* FolderItem functions */
896                 rssyl_class.item_new = rssyl_item_new;
897                 rssyl_class.item_destroy = rssyl_item_destroy;
898                 rssyl_class.item_get_path = rssyl_item_get_path;
899                 rssyl_class.create_folder = rssyl_create_folder;
900                 rssyl_class.rename_folder = rssyl_rename_folder;
901                 rssyl_class.remove_folder = rssyl_remove_folder;
902                 rssyl_class.get_num_list = rssyl_get_num_list;
903                 rssyl_class.scan_required = mh_get_class()->scan_required;
904                 rssyl_class.item_set_xml = rssyl_item_set_xml;
905                 rssyl_class.item_get_xml = rssyl_item_get_xml;
906
907                 /* Message functions */
908                 rssyl_class.get_msginfo = rssyl_get_msginfo;
909                 rssyl_class.fetch_msg = rssyl_fetch_msg;
910                 rssyl_class.copy_msg = mh_get_class()->copy_msg;
911                 rssyl_class.copy_msgs = mh_get_class()->copy_msgs;
912                 rssyl_class.add_msg = rssyl_add_msg;
913                 rssyl_class.add_msgs = rssyl_add_msgs;
914                 rssyl_class.remove_msg = rssyl_remove_msg;
915                 rssyl_class.remove_msgs = NULL;
916                 rssyl_class.is_msg_changed = rssyl_is_msg_changed;
917 //              rssyl_class.change_flags = rssyl_change_flags;
918                 rssyl_class.change_flags = NULL;
919                 rssyl_class.subscribe = rssyl_subscribe_uri;
920                 rssyl_class.copy_private_data = rssyl_copy_private_data;
921                 rssyl_class.search_msgs = folder_item_search_msgs_local;
922         }
923
924         return &rssyl_class;
925 }