Rename claws_io to file-utils, and move file-related functions
[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 #include "file-utils.h"
42
43 struct _RUpdateFormatCtx {
44         FolderItem *o_prev;
45         FolderItem *o_parent;
46         FolderItem *n_prev;
47         FolderItem *n_parent;
48         Folder *n_first;
49         GSList *oldfeeds;
50         GSList *oldroots;
51         gboolean reached_first_new;
52 };
53
54 typedef struct _RUpdateFormatCtx RUpdateFormatCtx;
55
56 extern FolderClass rssyl_class;
57
58 static void rssyl_update_format_move_contents(FolderItem *olditem,
59                 FolderItem *newitem);
60 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item);
61 static void _delete_old_roots_func(gpointer data, gpointer user_data);
62
63 static void rssyl_update_format_func(FolderItem *item, gpointer data)
64 {
65         RFolderItem *ritem;
66         RUpdateFormatCtx *ctx = (RUpdateFormatCtx *)data;
67         Folder *f = NULL;
68         FolderItem *new_item = NULL;
69         gchar *name;
70         OldRFeed *of;
71
72         if( !IS_RSSYL_FOLDER_ITEM(item) )
73                 return;
74
75         /* Do not do anything once we reached first new folder
76          * (which we created earlier in this process) */
77         if( ctx->reached_first_new )
78                 return;
79
80         if( item->folder == ctx->n_first ) {
81                 ctx->reached_first_new = TRUE;
82                 debug_print("RSSyl: (FORMAT) reached first new folder\n");
83                 return;
84         }
85
86         debug_print("RSSyl: (FORMAT) item '%s'\n", item->name);
87
88         if( folder_item_parent(item) == NULL ) {
89                 /* Root rssyl folder */
90                 ctx->oldroots = g_slist_prepend(ctx->oldroots, item);
91
92                 /* Create its counterpart */
93                 name = rssyl_strreplace(folder_item_get_name(item), " (RSSyl)", "");
94                 debug_print("RSSyl: (FORMAT) adding new root folder '%s'\n", name);
95                 f = folder_new(rssyl_folder_get_class(), name, NULL);
96                 g_free(name);
97                 g_return_if_fail(f != NULL);
98                 folder_add(f);
99
100                 folder_write_list();
101
102                 new_item = FOLDER_ITEM(f->node->data);
103
104                 /* If user has more than one old rssyl foldertrees, keep the n_first
105                  * pointer at the beginning of first one. */
106                 if (ctx->n_first == NULL)
107                         ctx->n_first = f;
108
109                 ctx->n_parent = new_item;
110         } else {
111                 /* Non-root folder */
112
113                 if (folder_item_parent(item) == ctx->o_prev) {
114                         /* We went one step deeper in folder hierarchy, adjust pointers
115                          * to parents */
116                         ctx->o_parent = ctx->o_prev;
117                         ctx->n_parent = ctx->n_prev;
118                 } else if (folder_item_parent(item) != ctx->o_parent) {
119                         /* We are not in same folder anymore, which can only mean we have
120                          * moved up in the hierarchy. Find a correct parent */
121                         while (folder_item_parent(item) != ctx->o_parent) {
122                                 ctx->o_parent = folder_item_parent(ctx->o_parent);
123                                 ctx->n_parent = folder_item_parent(ctx->n_parent);
124                                 if (ctx->o_parent == NULL) {
125                                         /* This shouldn't happen, unless we are magically moved to a
126                                          * completely different folder structure */
127                                         debug_print("RSSyl: MISHAP WHILE UPGRADING STORAGE FORMAT: couldn't find folder parent\n");
128                                         alertpanel_error(_("Internal problem while upgrading storage format. This should not happen. Please report this, with debug output attached.\n"));
129                                         return;
130                                 }
131                         }
132                 } else {
133                         /* We have remained in the same subfolder, nothing to do here */
134                 }
135
136                 debug_print("RSSyl: (FORMAT) adding folder '%s'\n", item->name);
137                 new_item = folder_create_folder(ctx->n_parent, item->name);
138
139                 if (new_item == NULL) {
140                         debug_print("RSSyl: (FORMAT) couldn't add folder '%s', skipping it\n",
141                                         item->name);
142                         return;
143                 }
144
145                 of = rssyl_old_feed_get_by_name(ctx->oldfeeds, item->name);
146                 if (of != NULL && of->url != NULL) {
147                         /* Folder with an actual subscribed feed */
148                         debug_print("RSSyl: (FORMAT) making '%s' a feed with URL '%s'\n",
149                                         item->name, of->url);
150
151                         ritem = (RFolderItem *)new_item;
152                         ritem->url = g_strdup(of->url);
153
154                         rssyl_feed_start_refresh_timeout(ritem);
155
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         if (g_remove(old_feeds_xml) != 0) {
232                 debug_print("RSSyl: Couldn't delete '%s'\n", old_feeds_xml);
233         }
234         g_free(old_feeds_xml);
235 }
236
237 static void _delete_old_roots_func(gpointer data, gpointer user_data)
238 {
239         FolderItem *item = (FolderItem *)data;
240
241         folder_destroy(item->folder);
242 }
243
244 /* Copy each item in a feed to the new directory */
245 static void rssyl_update_format_move_contents(FolderItem *olditem,
246                 FolderItem *newitem)
247 {
248         gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
249         GDir *d = NULL;
250         GError *error = NULL;
251
252         oldpath = _old_rssyl_item_get_path(NULL, olditem);
253         newpath = folder_item_get_path(newitem);
254
255         if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
256                 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
257                                 error->message);
258                 g_error_free(error);
259                 return;
260         }
261
262         debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
263                         oldpath, newpath);
264
265         while ((fname = (gchar *)g_dir_read_name(d)) != NULL) {
266                 gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname;
267                 fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL);
268                 if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) {
269                         nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL);
270                         move_file(fpath, nfpath, FALSE);
271                         g_free(nfpath);
272                 }
273                 if (g_remove(fpath) != 0) {
274                         debug_print("RSSyl: (FORMAT) couldn't delete '%s'\n", fpath);
275                 }
276                 g_free(fpath);
277         }
278
279         g_dir_close(d);
280         g_rmdir(oldpath);
281
282         g_free(oldpath);
283         g_free(newpath);
284 }
285
286 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
287 {
288         gchar *result, *tmp;
289
290         if (folder_item_parent(item) == NULL)
291                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
292
293         tmp = rssyl_strreplace(item->name, "/", "\\");
294         result = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
295                         G_DIR_SEPARATOR_S, tmp, NULL);
296         g_free(tmp);
297         return result;
298 }