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>
31 #include <mainwindow.h>
35 #include "old_feeds.h"
37 #include "rssyl_feed.h"
40 struct _RUpdateFormatCtx {
48 gboolean reached_first_new;
51 typedef struct _RUpdateFormatCtx RUpdateFormatCtx;
53 extern FolderClass rssyl_class;
55 static void rssyl_update_format_move_contents(FolderItem *olditem,
57 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item);
58 static void _delete_old_roots_func(gpointer data, gpointer user_data);
60 static void rssyl_update_format_func(FolderItem *item, gpointer data)
63 RUpdateFormatCtx *ctx = (RUpdateFormatCtx *)data;
65 FolderItem *new_item = NULL;
69 if( !IS_RSSYL_FOLDER_ITEM(item) )
72 /* Do not do anything once we reached first new folder
73 * (which we created earlier in this process) */
74 if( ctx->reached_first_new )
77 if( item->folder == ctx->n_first ) {
78 ctx->reached_first_new = TRUE;
79 debug_print("RSSyl: (FORMAT) reached first new folder\n");
83 debug_print("RSSyl: (FORMAT) item '%s'\n", item->name);
85 if( folder_item_parent(item) == NULL ) {
86 /* Root rssyl folder */
87 ctx->oldroots = g_slist_prepend(ctx->oldroots, item);
89 /* Create its counterpart */
90 name = rssyl_strreplace(folder_item_get_name(item), " (RSSyl)", "");
91 debug_print("RSSyl: (FORMAT) adding new root folder '%s'\n", name);
92 f = folder_new(rssyl_folder_get_class(), name, NULL);
94 g_return_if_fail(f != NULL);
99 new_item = FOLDER_ITEM(f->node->data);
101 /* If user has more than one old rssyl foldertrees, keep the n_first
102 * pointer at the beginning of first one. */
103 if (ctx->n_first == NULL)
106 ctx->n_parent = new_item;
108 /* Non-root folder */
110 if (folder_item_parent(item) == ctx->o_prev) {
111 /* We went one step deeper in folder hierarchy, adjust pointers
113 ctx->o_parent = ctx->o_prev;
114 ctx->n_parent = ctx->n_prev;
115 } else if (folder_item_parent(item) != ctx->o_parent) {
116 /* We are not in same folder anymore, which can only mean we have
117 * moved up in the hierarchy. Find a correct parent */
118 while (folder_item_parent(item) != ctx->o_parent) {
119 ctx->o_parent = folder_item_parent(ctx->o_parent);
120 ctx->n_parent = folder_item_parent(ctx->n_parent);
121 if (ctx->o_parent == NULL) {
122 /* This shouldn't happen, unless we are magically moved to a
123 * completely different folder structure */
124 debug_print("RSSyl: MISHAP WHILE UPGRADING STORAGE FORMAT: couldn't find folder parent\n");
125 alertpanel_error(_("Internal problem while upgrading storage format. This should not happen. Please report this, with debug output attached.\n"));
130 /* We have remained in the same subfolder, nothing to do here */
133 debug_print("RSSyl: (FORMAT) adding folder '%s'\n", item->name);
134 new_item = folder_create_folder(ctx->n_parent, item->name);
136 if (new_item == NULL) {
137 debug_print("RSSyl: (FORMAT) couldn't add folder '%s', skipping it\n",
142 of = rssyl_old_feed_get_by_name(ctx->oldfeeds, item->name);
143 if (of != NULL && of->url != NULL) {
144 /* Folder with an actual subscribed feed */
145 debug_print("RSSyl: (FORMAT) making '%s' a feed with URL '%s'\n",
146 item->name, of->url);
148 ritem = (RFolderItem *)new_item;
149 ritem->url = g_strdup(of->url);
151 rssyl_feed_start_refresh_timeout(ritem);
153 /* TODO: copy feed preferences from old structure */
154 ritem->official_title = g_strdup(of->official_name);
155 ritem->default_refresh_interval =
156 (of->default_refresh_interval != 0 ? TRUE : FALSE);
157 ritem->refresh_interval = of->refresh_interval;
158 ritem->keep_old = (of->expired_num > -1 ? TRUE : FALSE);
159 ritem->fetch_comments =
160 (of->fetch_comments != 0 ? TRUE : FALSE);
161 ritem->fetch_comments_max_age = of->fetch_comments_for;
162 ritem->silent_update = of->silent_update;
163 ritem->ssl_verify_peer = of->ssl_verify_peer;
166 rssyl_update_format_move_contents(item, new_item);
168 /* destroy the new folder's cache so we'll re-read the migrated one */
169 if (new_item->cache) {
170 msgcache_destroy(new_item->cache);
171 new_item->cache = NULL;
174 /* Store folderlist with the new folder */
175 folder_item_scan(new_item);
180 ctx->n_prev = new_item;
184 void rssyl_update_format()
186 RUpdateFormatCtx *ctx = NULL;
188 gchar *old_feeds_xml = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
189 RSSYL_DIR, G_DIR_SEPARATOR_S, "feeds.xml", NULL);
191 if (!g_file_test(old_feeds_xml,
192 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
193 g_free(old_feeds_xml);
197 debug_print("RSSyl: Old format found, updating.\n");
199 oldfeeds = rssyl_old_feed_metadata_parse(old_feeds_xml);
201 /* We find all rssyl root folders and perform magic on each */
202 ctx = g_new0(RUpdateFormatCtx, 1);
204 ctx->o_parent = NULL;
206 ctx->n_parent = NULL;
208 ctx->oldfeeds = oldfeeds;
209 ctx->oldroots = NULL;
210 ctx->reached_first_new = FALSE;
212 folder_item_update_freeze();
214 /* Go through all RSSyl folders, making new copies */
215 folder_func_to_all_folders((FolderItemFunc)rssyl_update_format_func, ctx);
217 g_slist_foreach(ctx->oldroots, _delete_old_roots_func, NULL);
218 g_slist_free(ctx->oldroots);
220 folder_item_update_thaw();
224 g_remove(old_feeds_xml);
225 g_free(old_feeds_xml);
228 static void _delete_old_roots_func(gpointer data, gpointer user_data)
230 FolderItem *item = (FolderItem *)data;
232 folder_destroy(item->folder);
235 /* Copy each item in a feed to the new directory */
236 static void rssyl_update_format_move_contents(FolderItem *olditem,
239 gchar *oldpath, *newpath, *fname, *fpath, *nfpath;
241 GError *error = NULL;
243 oldpath = _old_rssyl_item_get_path(NULL, olditem);
244 newpath = folder_item_get_path(newitem);
246 if ((d = g_dir_open(oldpath, 0, &error)) == NULL) {
247 debug_print("RSSyl: (FORMAT) couldn't open dir '%s': %s\n", oldpath,
253 debug_print("RSSyl: (FORMAT) moving contents of '%s' to '%s'\n",
256 while ((fname = (gchar *)g_dir_read_name(d)) != NULL) {
257 gboolean migrate_file = to_number(fname) > 0 || strstr(fname, ".claws_") == fname;
258 fpath = g_strconcat(oldpath, G_DIR_SEPARATOR_S, fname, NULL);
259 if (migrate_file && g_file_test(fpath, G_FILE_TEST_IS_REGULAR)) {
260 nfpath = g_strconcat(newpath, G_DIR_SEPARATOR_S, fname, NULL);
261 move_file(fpath, nfpath, FALSE);
275 static gchar *_old_rssyl_item_get_path(Folder *folder, FolderItem *item)
279 if (folder_item_parent(item) == NULL)
280 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR, NULL);
282 tmp = rssyl_strreplace(item->name, "/", "\\");
283 result = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
284 G_DIR_SEPARATOR_S, tmp, NULL);