2 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
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.
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.
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.
25 #include <glib/gi18n.h>
28 /* Claws Mail includes */
29 #include <alertpanel.h>
30 #include <folder_item_prefs.h>
32 #include <mainwindow.h>
37 #include "old_feeds.h"
39 #include "rssyl_feed.h"
42 struct _RUpdateFormatCtx {
50 gboolean reached_first_new;
53 typedef struct _RUpdateFormatCtx RUpdateFormatCtx;
55 extern FolderClass rssyl_class;
57 static void rssyl_update_format_move_contents(FolderItem *olditem,
59 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item);
60 static void _delete_old_roots_func(gpointer data, gpointer user_data);
62 static void rssyl_update_format_func(FolderItem *item, gpointer data)
65 RUpdateFormatCtx *ctx = (RUpdateFormatCtx *)data;
67 FolderItem *new_item = NULL;
71 if( !IS_RSSYL_FOLDER_ITEM(item) )
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 )
79 if( item->folder == ctx->n_first ) {
80 ctx->reached_first_new = TRUE;
81 debug_print("RSSyl: (FORMAT) reached first new folder\n");
85 debug_print("RSSyl: (FORMAT) item '%s'\n", item->name);
87 if( folder_item_parent(item) == NULL ) {
88 /* Root rssyl folder */
89 ctx->oldroots = g_slist_prepend(ctx->oldroots, item);
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);
96 g_return_if_fail(f != NULL);
101 new_item = FOLDER_ITEM(f->node->data);
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)
108 ctx->n_parent = new_item;
110 /* Non-root folder */
112 if (folder_item_parent(item) == ctx->o_prev) {
113 /* We went one step deeper in folder hierarchy, adjust pointers
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"));
132 /* We have remained in the same subfolder, nothing to do here */
135 debug_print("RSSyl: (FORMAT) adding folder '%s'\n", item->name);
136 new_item = folder_create_folder(ctx->n_parent, item->name);
138 if (new_item == NULL) {
139 debug_print("RSSyl: (FORMAT) couldn't add folder '%s', skipping it\n",
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);
150 ritem = (RFolderItem *)new_item;
151 ritem->url = g_strdup(of->url);
153 rssyl_feed_start_refresh_timeout(ritem);
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;
166 folder_item_prefs_copy_prefs(item, &ritem->item);
169 rssyl_update_format_move_contents(item, new_item);
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;
177 /* Store folderlist with the new folder */
178 folder_item_scan(new_item);
183 ctx->n_prev = new_item;
187 void rssyl_update_format()
189 RUpdateFormatCtx *ctx = NULL;
191 gchar *old_feeds_xml = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
192 RSSYL_DIR, G_DIR_SEPARATOR_S, "feeds.xml", NULL);
194 if (!g_file_test(old_feeds_xml,
195 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
196 g_free(old_feeds_xml);
200 debug_print("RSSyl: Old format found, updating.\n");
202 oldfeeds = rssyl_old_feed_metadata_parse(old_feeds_xml);
204 /* We find all rssyl root folders and perform magic on each */
205 ctx = g_new0(RUpdateFormatCtx, 1);
207 ctx->o_parent = NULL;
209 ctx->n_parent = NULL;
211 ctx->oldfeeds = oldfeeds;
212 ctx->oldroots = NULL;
213 ctx->reached_first_new = FALSE;
215 folder_item_update_freeze();
217 /* Go through all RSSyl folders, making new copies */
218 folder_func_to_all_folders((FolderItemFunc)rssyl_update_format_func, ctx);
220 g_slist_foreach(ctx->oldroots, _delete_old_roots_func, NULL);
221 g_slist_free(ctx->oldroots);
223 prefs_matcher_write_config();
226 folder_item_update_thaw();
230 g_remove(old_feeds_xml);
231 g_free(old_feeds_xml);
234 static void _delete_old_roots_func(gpointer data, gpointer user_data)
236 FolderItem *item = (FolderItem *)data;
238 folder_destroy(item->folder);
241 /* Copy each item in a feed to the new directory */
242 static void rssyl_update_format_move_contents(FolderItem *olditem,
245 gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
247 GError *error = NULL;
249 oldpath = _old_rssyl_item_get_path(NULL, olditem);
250 newpath = folder_item_get_path(newitem);
252 if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
253 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
259 debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
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);
281 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
285 if (folder_item_parent(item) == NULL)
286 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
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);