RSSyl: handle expiring comments after their parent is gone
[claws.git] / src / plugins / rssyl / rssyl_update_format.c
1 /*
2  * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 /* Global includes */
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 /* Claws Mail includes */
29 #include <alertpanel.h>
30 #include <folder_item_prefs.h>
31 #include <log.h>
32 #include <mainwindow.h>
33 #include <matcher.h>
34 #include <msgcache.h>
35
36 /* Local includes */
37 #include "old_feeds.h"
38 #include "rssyl.h"
39 #include "rssyl_feed.h"
40 #include "strutils.h"
41
42 struct _RUpdateFormatCtx {
43         FolderItem *o_prev;
44         FolderItem *o_parent;
45         FolderItem *n_prev;
46         FolderItem *n_parent;
47         Folder *n_first;
48         GSList *oldfeeds;
49         GSList *oldroots;
50         gboolean reached_first_new;
51 };
52
53 typedef struct _RUpdateFormatCtx RUpdateFormatCtx;
54
55 extern FolderClass rssyl_class;
56
57 static void rssyl_update_format_move_contents(FolderItem *olditem,
58                 FolderItem *newitem);
59 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item);
60 static void _delete_old_roots_func(gpointer data, gpointer user_data);
61
62 static void rssyl_update_format_func(FolderItem *item, gpointer data)
63 {
64         RFolderItem *ritem;
65         RUpdateFormatCtx *ctx = (RUpdateFormatCtx *)data;
66         Folder *f = NULL;
67         FolderItem *new_item = NULL;
68         gchar *name;
69         OldRFeed *of;
70
71         if( !IS_RSSYL_FOLDER_ITEM(item) )
72                 return;
73
74         /* Do not do anything once we reached first new folder
75          * (which we created earlier in this process) */
76         if( ctx->reached_first_new )
77                 return;
78
79         if( item->folder == ctx->n_first ) {
80                 ctx->reached_first_new = TRUE;
81                 debug_print("RSSyl: (FORMAT) reached first new folder\n");
82                 return;
83         }
84
85         debug_print("RSSyl: (FORMAT) item '%s'\n", item->name);
86
87         if( folder_item_parent(item) == NULL ) {
88                 /* Root rssyl folder */
89                 ctx->oldroots = g_slist_prepend(ctx->oldroots, item);
90
91                 /* Create its counterpart */
92                 name = rssyl_strreplace(folder_item_get_name(item), " (RSSyl)", "");
93                 debug_print("RSSyl: (FORMAT) adding new root folder '%s'\n", name);
94                 f = folder_new(rssyl_folder_get_class(), name, NULL);
95                 g_free(name);
96                 g_return_if_fail(f != NULL);
97                 folder_add(f);
98
99                 folder_write_list();
100
101                 new_item = FOLDER_ITEM(f->node->data);
102
103                 /* If user has more than one old rssyl foldertrees, keep the n_first
104                  * pointer at the beginning of first one. */
105                 if (ctx->n_first == NULL)
106                         ctx->n_first = f;
107
108                 ctx->n_parent = new_item;
109         } else {
110                 /* Non-root folder */
111
112                 if (folder_item_parent(item) == ctx->o_prev) {
113                         /* We went one step deeper in folder hierarchy, adjust pointers
114                          * to parents */
115                         ctx->o_parent = ctx->o_prev;
116                         ctx->n_parent = ctx->n_prev;
117                 } else if (folder_item_parent(item) != ctx->o_parent) {
118                         /* We are not in same folder anymore, which can only mean we have
119                          * moved up in the hierarchy. Find a correct parent */
120                         while (folder_item_parent(item) != ctx->o_parent) {
121                                 ctx->o_parent = folder_item_parent(ctx->o_parent);
122                                 ctx->n_parent = folder_item_parent(ctx->n_parent);
123                                 if (ctx->o_parent == NULL) {
124                                         /* This shouldn't happen, unless we are magically moved to a
125                                          * completely different folder structure */
126                                         debug_print("RSSyl: MISHAP WHILE UPGRADING STORAGE FORMAT: couldn't find folder parent\n");
127                                         alertpanel_error(_("Internal problem while upgrading storage format. This should not happen. Please report this, with debug output attached.\n"));
128                                         return;
129                                 }
130                         }
131                 } else {
132                         /* We have remained in the same subfolder, nothing to do here */
133                 }
134
135                 debug_print("RSSyl: (FORMAT) adding folder '%s'\n", item->name);
136                 new_item = folder_create_folder(ctx->n_parent, item->name);
137
138                 if (new_item == NULL) {
139                         debug_print("RSSyl: (FORMAT) couldn't add folder '%s', skipping it\n",
140                                         item->name);
141                         return;
142                 }
143
144                 of = rssyl_old_feed_get_by_name(ctx->oldfeeds, item->name);
145                 if (of != NULL && of->url != NULL) {
146                         /* Folder with an actual subscribed feed */
147                         debug_print("RSSyl: (FORMAT) making '%s' a feed with URL '%s'\n",
148                                         item->name, of->url);
149
150                         ritem = (RFolderItem *)new_item;
151                         ritem->url = g_strdup(of->url);
152
153                         rssyl_feed_start_refresh_timeout(ritem);
154
155                         ritem->official_title = g_strdup(of->official_name);
156                         ritem->default_refresh_interval =
157                                 (of->default_refresh_interval != 0 ? TRUE : FALSE);
158                         ritem->refresh_interval = of->refresh_interval;
159                         ritem->keep_old = (of->expired_num > -1 ? TRUE : FALSE);
160                         ritem->fetch_comments =
161                                 (of->fetch_comments != 0 ? TRUE : FALSE);
162                         ritem->fetch_comments_max_age = of->fetch_comments_for;
163                         ritem->silent_update = of->silent_update;
164                         ritem->ssl_verify_peer = of->ssl_verify_peer;
165
166                         folder_item_prefs_copy_prefs(item, &ritem->item);
167                 }
168
169                 rssyl_update_format_move_contents(item, new_item);
170
171                 /* destroy the new folder's cache so we'll re-read the migrated one */
172                 if (new_item->cache) {
173                         msgcache_destroy(new_item->cache);
174                         new_item->cache = NULL;
175                 }
176
177                 /* Store folderlist with the new folder */
178                 folder_item_scan(new_item);
179                 folder_write_list();
180         }
181
182         ctx->o_prev = item;
183         ctx->n_prev = new_item;
184 }
185
186
187 void rssyl_update_format()
188 {
189         RUpdateFormatCtx *ctx = NULL;
190         GSList *oldfeeds;
191         gchar *old_feeds_xml = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
192                         RSSYL_DIR, G_DIR_SEPARATOR_S, "feeds.xml", NULL);
193
194         if (!g_file_test(old_feeds_xml,
195                                 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
196                 g_free(old_feeds_xml);
197                 return;
198         }
199
200         debug_print("RSSyl: Old format found, updating.\n");
201
202         oldfeeds = rssyl_old_feed_metadata_parse(old_feeds_xml);
203
204         /* We find all rssyl root folders and perform magic on each */
205         ctx = g_new0(RUpdateFormatCtx, 1);
206         ctx->o_prev = NULL;
207         ctx->o_parent = NULL;
208         ctx->n_prev = NULL;
209         ctx->n_parent = NULL;
210         ctx->n_first = NULL;
211         ctx->oldfeeds = oldfeeds;
212         ctx->oldroots = NULL;
213         ctx->reached_first_new = FALSE;
214
215         folder_item_update_freeze();
216
217         /* Go through all RSSyl folders, making new copies */
218         folder_func_to_all_folders((FolderItemFunc)rssyl_update_format_func, ctx);
219
220         g_slist_foreach(ctx->oldroots, _delete_old_roots_func, NULL);
221         g_slist_free(ctx->oldroots);
222
223         prefs_matcher_write_config();
224         folder_write_list();
225
226         folder_item_update_thaw();
227
228         g_free(ctx);
229
230         g_remove(old_feeds_xml);
231         g_free(old_feeds_xml);
232 }
233
234 static void _delete_old_roots_func(gpointer data, gpointer user_data)
235 {
236         FolderItem *item = (FolderItem *)data;
237
238         folder_destroy(item->folder);
239 }
240
241 /* Copy each item in a feed to the new directory */
242 static void rssyl_update_format_move_contents(FolderItem *olditem,
243                 FolderItem *newitem)
244 {
245         gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
246         GDir *d = NULL;
247         GError *error = NULL;
248
249         oldpath = _old_rssyl_item_get_path(NULL, olditem);
250         newpath = folder_item_get_path(newitem);
251
252         if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
253                 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
254                                 error->message);
255                 g_error_free(error);
256                 return;
257         }
258
259         debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
260                         oldpath, newpath);
261
262         while ((fname = (gchar *)g_dir_read_name(d)) != NULL) {
263                 gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname;
264                 fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL);
265                 if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) {
266                         nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL);
267                         move_file(fpath, nfpath, FALSE);
268                         g_free(nfpath);
269                 }
270                 g_remove(fpath);
271                 g_free(fpath);
272         }
273
274         g_dir_close(d);
275         g_rmdir(oldpath);
276
277         g_free(oldpath);
278         g_free(newpath);
279 }
280
281 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
282 {
283         gchar *result, *tmp;
284
285         if (folder_item_parent(item) == NULL)
286                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
287
288         tmp = rssyl_strreplace(item->name, "/", "\\");
289         result = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
290                         G_DIR_SEPARATOR_S, tmp, NULL);
291         g_free(tmp);
292         return result;
293 }