2 * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4 * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
31 /* Claws Mail includes */
32 #include <alertpanel.h>
35 #include <common/utils.h>
38 #include "libfeed/feed.h"
39 #include "libfeed/feeditem.h"
41 #include "rssyl_add_item.h"
42 #include "rssyl_feed.h"
43 #include "rssyl_gtk.h"
44 #include "rssyl_update_feed.h"
45 #include "rssyl_subscribe_gtk.h"
48 static void rssyl_subscribe_foreach_func(gpointer data, gpointer user_data)
50 RFolderItem *ritem = (RFolderItem *)user_data;
51 FeedItem *feed_item = (FeedItem *)data;
53 g_return_if_fail(ritem != NULL);
54 g_return_if_fail(feed_item != NULL);
56 rssyl_add_item(ritem, feed_item);
59 gboolean rssyl_subscribe(FolderItem *parent, const gchar *url,
62 gchar *myurl = NULL, *tmpname = NULL, *tmpname2 = NULL;
68 gboolean edit_properties = FALSE;
69 gchar *official_title = NULL;
71 g_return_val_if_fail(parent != NULL, FALSE);
72 g_return_val_if_fail(url != NULL, FALSE);
74 log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBING, url);
76 myurl = my_normalize_url(url);
79 ctx = rssyl_prep_fetchctx_from_url(myurl);
81 g_return_val_if_fail(ctx != NULL, FALSE);
83 rssyl_fetch_feed(ctx, verbose);
85 debug_print("RSSyl: fetch success == %s\n",
86 ctx->success ? "TRUE" : "FALSE");
89 /* User notification was already handled inside rssyl_fetch_feed(),
90 * let's just return quietly. */
98 sctx = g_new0(RSubCtx, 1);
99 sctx->feed = ctx->feed;
100 sctx->edit_properties = FALSE;
102 debug_print("RSSyl: Calling subscribe dialog routine...\n");
103 rssyl_subscribe_dialog(sctx);
105 if (sctx->feed == NULL) {
106 debug_print("RSSyl: User cancelled subscribe.\n");
111 edit_properties = sctx->edit_properties;
112 if (sctx->official_title != NULL) {
113 debug_print("RSSyl: custom official title\n");
114 official_title = g_strdup(sctx->official_title);
117 if (sctx->edit_properties)
118 debug_print("RSSyl: User wants to edit properties of the new feed.\n");
120 debug_print("RSSyl: User does not want to edit properties of the new feed.\n");
121 g_free(sctx->official_title);
125 /* OK, feed is succesfully fetched and correct, let's add it to CM. */
127 /* Create a folder for it. */
128 tmpname = rssyl_format_string(ctx->feed->title, TRUE, TRUE);
129 tmpname2 = g_strdup(tmpname);
132 /* Windows does not allow its filenames to start or end with a dot,
133 * or to end with a space. */
134 if (tmpname2[0] == '.')
136 if (tmpname2[strlen(tmpname2) - 1] == '.')
137 tmpname2[strlen(tmpname2) - 1] = '_';
138 if (tmpname2[strlen(tmpname2) - 1] == ' ')
139 tmpname2[strlen(tmpname2) - 1] == '_';
142 while (folder_find_child_item_by_name(parent, tmpname2) != 0 && i < 20) {
143 debug_print("RSSyl: Folder '%s' already exists, trying another name\n",
146 tmpname2 = g_strdup_printf("%s__%d", tmpname, ++i);
148 /* TODO: handle cases where i reaches 20 */
150 folder_item_update_freeze();
152 new_item = folder_create_folder(parent, tmpname2);
158 alertpanel_error(_("Couldn't create folder for new feed '%s'."),
160 feed_free(ctx->feed);
167 debug_print("RSSyl: Adding '%s'\n", ctx->feed->url);
169 ritem = (RFolderItem *)new_item;
170 ritem->url = g_strdup(ctx->feed->url);
172 if (official_title != NULL) {
173 debug_print("RSSyl: storing official feed title '%s'\n", official_title);
174 ritem->official_title = official_title;
177 if (feed_n_items(ctx->feed) > 0)
178 feed_foreach_item(ctx->feed, rssyl_subscribe_foreach_func, (gpointer)ritem);
180 folder_item_scan(new_item);
184 rssyl_gtk_prop(ritem);
186 folder_item_update_thaw();