Migrate the old feeds' caches if possible.
[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 <log.h>
31 #include <mainwindow.h>
32
33 /* Local includes */
34 #include "old_feeds.h"
35 #include "rssyl.h"
36 #include "rssyl_feed.h"
37 #include "strutils.h"
38
39 struct _RUpdateFormatCtx {
40         FolderItem *o_prev;
41         FolderItem *o_parent;
42         FolderItem *n_prev;
43         FolderItem *n_parent;
44         Folder *n_first;
45         GSList *oldfeeds;
46         GSList *oldroots;
47         gboolean reached_first_new;
48 };
49
50 typedef struct _RUpdateFormatCtx RUpdateFormatCtx;
51
52 extern FolderClass rssyl_class;
53
54 static void rssyl_update_format_move_contents(FolderItem *olditem,
55                 FolderItem *newitem);
56 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item);
57 static void _delete_old_roots_func(gpointer data, gpointer user_data);
58
59 static void rssyl_update_format_func(FolderItem *item, gpointer data)
60 {
61         RFolderItem *ritem;
62         RUpdateFormatCtx *ctx = (RUpdateFormatCtx *)data;
63         Folder *f = NULL;
64         FolderItem *new_item = NULL;
65         gchar *name;
66         OldRFeed *of;
67
68         if( !IS_RSSYL_FOLDER_ITEM(item) )
69                 return;
70
71         /* Do not do anything once we reached first new folder
72          * (which we created earlier in this process) */
73         if( ctx->reached_first_new )
74                 return;
75
76         if( item->folder == ctx->n_first ) {
77                 ctx->reached_first_new = TRUE;
78                 debug_print("RSSyl: (FORMAT) reached first new folder\n");
79                 return;
80         }
81
82         debug_print("RSSyl: (FORMAT) item '%s'\n", item->name);
83
84         if( folder_item_parent(item) == NULL ) {
85                 /* Root rssyl folder */
86                 ctx->oldroots = g_slist_prepend(ctx->oldroots, item);
87
88                 /* Create its counterpart */
89                 name = rssyl_strreplace(folder_item_get_name(item), " (RSSyl)", "");
90                 debug_print("RSSyl: (FORMAT) adding new root folder '%s'\n", name);
91                 f = folder_new(rssyl_folder_get_class(), name, NULL);
92                 g_free(name);
93                 g_return_if_fail(f != NULL);
94                 folder_add(f);
95
96                 folder_write_list();
97
98                 new_item = FOLDER_ITEM(f->node->data);
99
100                 /* If user has more than one old rssyl foldertrees, keep the n_first
101                  * pointer at the beginning of first one. */
102                 if (ctx->n_first == NULL)
103                         ctx->n_first = f;
104
105                 ctx->n_parent = new_item;
106         } else {
107                 /* Non-root folder */
108
109                 if (folder_item_parent(item) == ctx->o_prev) {
110                         /* We went one step deeper in folder hierarchy, adjust pointers
111                          * to parents */
112                         ctx->o_parent = ctx->o_prev;
113                         ctx->n_parent = ctx->n_prev;
114                 } else if (folder_item_parent(item) != ctx->o_parent) {
115                         /* We are not in same folder anymore, which can only mean we have
116                          * moved up in the hierarchy. Find a correct parent */
117                         while (folder_item_parent(item) != ctx->o_parent) {
118                                 ctx->o_parent = folder_item_parent(ctx->o_parent);
119                                 ctx->n_parent = folder_item_parent(ctx->n_parent);
120                                 if (ctx->o_parent == NULL) {
121                                         /* This shouldn't happen, unless we are magically moved to a
122                                          * completely different folder structure */
123                                         debug_print("RSSyl: MISHAP WHILE UPGRADING STORAGE FORMAT: couldn't find folder parent\n");
124                                         alertpanel_error(_("Internal problem while upgrading storage format. This should not happen. Please report this, with debug output attached.\n"));
125                                         return;
126                                 }
127                         }
128                 } else {
129                         /* We have remained in the same subfolder, nothing to do here */
130                 }
131
132                 debug_print("RSSyl: (FORMAT) adding folder '%s'\n", item->name);
133                 new_item = folder_create_folder(ctx->n_parent, item->name);
134
135                 if (new_item == NULL) {
136                         debug_print("RSSyl: (FORMAT) couldn't add folder '%s', skipping it\n",
137                                         item->name);
138                         return;
139                 }
140
141                 of = rssyl_old_feed_get_by_name(ctx->oldfeeds, item->name);
142                 if (of != NULL && of->url != NULL) {
143                         /* Folder with an actual subscribed feed */
144                         debug_print("RSSyl: (FORMAT) making '%s' a feed with URL '%s'\n",
145                                         item->name, of->url);
146
147                         ritem = (RFolderItem *)new_item;
148                         ritem->url = g_strdup(of->url);
149
150                         rssyl_feed_start_refresh_timeout(ritem);
151
152                         /* TODO: copy feed preferences from old structure */
153                         ritem->official_title = g_strdup(of->official_name);
154                         ritem->default_refresh_interval =
155                                 (of->default_refresh_interval != 0 ? TRUE : FALSE);
156                         ritem->refresh_interval = of->refresh_interval;
157                         ritem->keep_old = (of->expired_num > -1 ? TRUE : FALSE);
158                         ritem->fetch_comments =
159                                 (of->fetch_comments != 0 ? TRUE : FALSE);
160                         ritem->fetch_comments_max_age = of->fetch_comments_for;
161                         ritem->silent_update = of->silent_update;
162                 }
163
164                 rssyl_update_format_move_contents(item, new_item);
165
166                 /* destroy the new folder's cache so we'll re-read the migrated one */
167                 if (new_item->cache) {
168                         msgcache_destroy(new_item->cache);
169                         new_item->cache = NULL;
170                 }
171
172                 /* Store folderlist with the new folder */
173                 folder_item_scan(new_item);
174                 folder_write_list();
175         }
176
177         ctx->o_prev = item;
178         ctx->n_prev = new_item;
179 }
180
181
182 void rssyl_update_format()
183 {
184         RUpdateFormatCtx *ctx = NULL;
185         GSList *oldfeeds;
186         gchar *old_feeds_xml = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
187                         RSSYL_DIR, G_DIR_SEPARATOR_S, "feeds.xml", NULL);
188
189         if (!g_file_test(old_feeds_xml,
190                                 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
191                 g_free(old_feeds_xml);
192                 return;
193         }
194
195         debug_print("RSSyl: Old format found, updating.\n");
196
197         oldfeeds = rssyl_old_feed_metadata_parse(old_feeds_xml);
198
199         /* We find all rssyl root folders and perform magic on each */
200         ctx = g_new0(RUpdateFormatCtx, 1);
201         ctx->o_prev = NULL;
202         ctx->o_parent = NULL;
203         ctx->n_prev = NULL;
204         ctx->n_parent = NULL;
205         ctx->n_first = NULL;
206         ctx->oldfeeds = oldfeeds;
207         ctx->oldroots = NULL;
208         ctx->reached_first_new = FALSE;
209
210         folder_item_update_freeze();
211
212         /* Go through all RSSyl folders, making new copies */
213         folder_func_to_all_folders((FolderItemFunc)rssyl_update_format_func, ctx);
214
215         g_slist_foreach(ctx->oldroots, _delete_old_roots_func, NULL);
216         g_slist_free(ctx->oldroots);
217
218         folder_item_update_thaw();
219
220         g_free(ctx);
221
222         g_remove(old_feeds_xml);
223         g_free(old_feeds_xml);
224 }
225
226 static void _delete_old_roots_func(gpointer data, gpointer user_data)
227 {
228         FolderItem *item = (FolderItem *)data;
229
230         folder_destroy(item->folder);
231 }
232
233 /* Copy each item in a feed to the new directory */
234 static void rssyl_update_format_move_contents(FolderItem *olditem,
235                 FolderItem *newitem)
236 {
237         gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
238         GDir *d = NULL;
239         GError *error = NULL;
240
241         oldpath = _old_rssyl_item_get_path(NULL, olditem);
242         newpath = folder_item_get_path(newitem);
243
244         if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
245                 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
246                                 error->message);
247                 g_error_free(error);
248                 return;
249         }
250
251         debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
252                         oldpath, newpath);
253
254         while ((fname = (gchar *)g_dir_read_name(d)) != NULL) {
255                 gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname;
256                 fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL);
257                 if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) {
258                         nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL);
259                         move_file(fpath, nfpath, FALSE);
260                         g_free(nfpath);
261                 }
262                 g_remove(fpath);
263                 g_free(fpath);
264         }
265
266         g_dir_close(d);
267         g_rmdir(oldpath);
268
269         g_free(oldpath);
270         g_free(newpath);
271 }
272
273 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
274 {
275         gchar *result, *tmp;
276
277         if (folder_item_parent(item) == NULL)
278                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
279
280         tmp = rssyl_strreplace(item->name, "/", "\\");
281         result = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
282                         G_DIR_SEPARATOR_S, tmp, NULL);
283         g_free(tmp);
284         return result;
285 }