323da2072e309f1adfbde41303cb02c57ff35814
[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                         /* TODO: copy feed preferences from old structure */
156                         ritem->official_title = g_strdup(of->official_name);
157                         ritem->default_refresh_interval =
158                                 (of->default_refresh_interval != 0 ? TRUE : FALSE);
159                         ritem->refresh_interval = of->refresh_interval;
160                         ritem->keep_old = (of->expired_num > -1 ? TRUE : FALSE);
161                         ritem->fetch_comments =
162                                 (of->fetch_comments != 0 ? TRUE : FALSE);
163                         ritem->fetch_comments_max_age = of->fetch_comments_for;
164                         ritem->silent_update = of->silent_update;
165                         ritem->ssl_verify_peer = of->ssl_verify_peer;
166
167                         folder_item_prefs_copy_prefs(item, &ritem->item);
168                 }
169
170                 rssyl_update_format_move_contents(item, new_item);
171
172                 /* destroy the new folder's cache so we'll re-read the migrated one */
173                 if (new_item->cache) {
174                         msgcache_destroy(new_item->cache);
175                         new_item->cache = NULL;
176                 }
177
178                 /* Store folderlist with the new folder */
179                 folder_item_scan(new_item);
180                 folder_write_list();
181         }
182
183         ctx->o_prev = item;
184         ctx->n_prev = new_item;
185 }
186
187
188 void rssyl_update_format()
189 {
190         RUpdateFormatCtx *ctx = NULL;
191         GSList *oldfeeds;
192         gchar *old_feeds_xml = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
193                         RSSYL_DIR, G_DIR_SEPARATOR_S, "feeds.xml", NULL);
194
195         if (!g_file_test(old_feeds_xml,
196                                 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
197                 g_free(old_feeds_xml);
198                 return;
199         }
200
201         debug_print("RSSyl: Old format found, updating.\n");
202
203         oldfeeds = rssyl_old_feed_metadata_parse(old_feeds_xml);
204
205         /* We find all rssyl root folders and perform magic on each */
206         ctx = g_new0(RUpdateFormatCtx, 1);
207         ctx->o_prev = NULL;
208         ctx->o_parent = NULL;
209         ctx->n_prev = NULL;
210         ctx->n_parent = NULL;
211         ctx->n_first = NULL;
212         ctx->oldfeeds = oldfeeds;
213         ctx->oldroots = NULL;
214         ctx->reached_first_new = FALSE;
215
216         folder_item_update_freeze();
217
218         /* Go through all RSSyl folders, making new copies */
219         folder_func_to_all_folders((FolderItemFunc)rssyl_update_format_func, ctx);
220
221         g_slist_foreach(ctx->oldroots, _delete_old_roots_func, NULL);
222         g_slist_free(ctx->oldroots);
223
224         prefs_matcher_write_config();
225         folder_write_list();
226
227         folder_item_update_thaw();
228
229         g_free(ctx);
230
231         g_remove(old_feeds_xml);
232         g_free(old_feeds_xml);
233 }
234
235 static void _delete_old_roots_func(gpointer data, gpointer user_data)
236 {
237         FolderItem *item = (FolderItem *)data;
238
239         folder_destroy(item->folder);
240 }
241
242 /* Copy each item in a feed to the new directory */
243 static void rssyl_update_format_move_contents(FolderItem *olditem,
244                 FolderItem *newitem)
245 {
246         gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
247         GDir *d = NULL;
248         GError *error = NULL;
249
250         oldpath = _old_rssyl_item_get_path(NULL, olditem);
251         newpath = folder_item_get_path(newitem);
252
253         if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
254                 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
255                                 error->message);
256                 g_error_free(error);
257                 return;
258         }
259
260         debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
261                         oldpath, newpath);
262
263         while ((fname = (gchar *)g_dir_read_name(d)) != NULL) {
264                 gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname;
265                 fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL);
266                 if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) {
267                         nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL);
268                         move_file(fpath, nfpath, FALSE);
269                         g_free(nfpath);
270                 }
271                 g_remove(fpath);
272                 g_free(fpath);
273         }
274
275         g_dir_close(d);
276         g_rmdir(oldpath);
277
278         g_free(oldpath);
279         g_free(newpath);
280 }
281
282 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
283 {
284         gchar *result, *tmp;
285
286         if (folder_item_parent(item) == NULL)
287                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
288
289         tmp = rssyl_strreplace(item->name, "/", "\\");
290         result = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
291                         G_DIR_SEPARATOR_S, tmp, NULL);
292         g_free(tmp);
293         return result;
294 }