RSSyl: add more missing checks for offline mode
[claws.git] / src / plugins / rssyl / rssyl_subscribe.c
1 /*
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>
5  *
6  * - DESCRIPTION HERE
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 /* Claws Mail includes */
32 #include <alertpanel.h>
33 #include <folder.h>
34 #include <log.h>
35 #include <common/utils.h>
36
37 /* Local includes */
38 #include "libfeed/feed.h"
39 #include "libfeed/feeditem.h"
40 #include "rssyl.h"
41 #include "rssyl_add_item.h"
42 #include "rssyl_feed.h"
43 #include "rssyl_gtk.h"
44 #include "rssyl_subscribe_gtk.h"
45 #include "strutils.h"
46
47 static void rssyl_subscribe_foreach_func(gpointer data, gpointer user_data)
48 {
49         RFolderItem *ritem = (RFolderItem *)user_data;
50         FeedItem *feed_item = (FeedItem *)data;
51
52         g_return_if_fail(ritem != NULL);
53         g_return_if_fail(feed_item != NULL);
54
55         rssyl_add_item(ritem, feed_item);
56 }
57
58 FolderItem *rssyl_subscribe(FolderItem *parent, const gchar *url,
59                 RSSylVerboseFlags verbose)
60 {
61         gchar *myurl = NULL, *tmpname = NULL, *tmpname2 = NULL;
62         RFetchCtx *ctx;
63         FolderItem *new_item;
64         RFolderItem *ritem;
65         gint i = 1;
66         RSubCtx *sctx;
67         gboolean edit_properties = FALSE;
68         gchar *official_title = NULL;
69
70         g_return_val_if_fail(parent != NULL, FALSE);
71         g_return_val_if_fail(url != NULL, FALSE);
72
73         log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBING, url);
74
75         myurl = my_normalize_url(url);
76
77         /* Fetch the feed. */
78         ctx = rssyl_prep_fetchctx_from_url(myurl);
79         g_free(myurl);
80         g_return_val_if_fail(ctx != NULL, FALSE);
81
82         rssyl_fetch_feed(ctx, verbose);
83
84         debug_print("RSSyl: fetch success == %s\n",
85                         ctx->success ? "TRUE" : "FALSE");
86
87         if (!ctx->success) {
88                 /* User notification was already handled inside rssyl_fetch_feed(),
89                  * let's just return quietly. */
90                 feed_free(ctx->feed);
91                 g_free(ctx->error);
92                 g_free(ctx);
93                 return NULL;
94         }
95
96         if (verbose & RSSYL_SHOW_RENAME_DIALOG) {
97                 sctx = g_new0(RSubCtx, 1);
98                 sctx->feed = ctx->feed;
99                 sctx->edit_properties = FALSE;
100
101                 debug_print("RSSyl: Calling subscribe dialog routine...\n");
102                 rssyl_subscribe_dialog(sctx);
103
104                 if (sctx->feed == NULL) {
105                         debug_print("RSSyl: User cancelled subscribe.\n");
106                         g_free(sctx);
107                         return NULL;
108                 }
109
110                 edit_properties = sctx->edit_properties;
111                 if (sctx->official_title != NULL) {
112                         debug_print("RSSyl: custom official title\n");
113                         official_title = g_strdup(sctx->official_title);
114                 }
115
116                 if (sctx->edit_properties)
117                         debug_print("RSSyl: User wants to edit properties of the new feed.\n");
118                 else
119                         debug_print("RSSyl: User does not want to edit properties of the new feed.\n");
120                 g_free(sctx->official_title);
121                 g_free(sctx);
122         }
123
124         /* OK, feed is successfully fetched and correct, let's add it to CM. */
125
126         /* Create a folder for it. */
127         tmpname = rssyl_format_string(ctx->feed->title, TRUE, TRUE);
128         tmpname2 = g_strdup(tmpname);
129
130 #ifdef G_OS_WIN32
131         /* Windows does not allow its filenames to start or end with a dot,
132          * or to end with a space. */
133         if (tmpname2[0] == '.')
134                 tmpname2[0] = '_';
135         if (tmpname2[strlen(tmpname2) - 1] == '.')
136                 tmpname2[strlen(tmpname2) - 1] = '_';
137         if (tmpname2[strlen(tmpname2) - 1] == ' ')
138                 tmpname2[strlen(tmpname2) - 1] = '_';
139 #endif
140
141         while (folder_find_child_item_by_name(parent, tmpname2) != 0 && i < 20) {
142                 debug_print("RSSyl: Folder '%s' already exists, trying another name\n",
143                                 tmpname2);
144                 g_free(tmpname2);
145                 tmpname2 = g_strdup_printf("%s__%d", tmpname, ++i);
146         }
147         /* TODO: handle cases where i reaches 20 */
148
149         folder_item_update_freeze();
150
151         new_item = folder_create_folder(parent, tmpname2);
152         g_free(tmpname);
153         g_free(tmpname2);
154
155         if (!new_item) {
156                 if (verbose & RSSYL_SHOW_ERRORS)
157                         alertpanel_error(_("Couldn't create folder for new feed '%s'."),
158                                         myurl);
159                 feed_free(ctx->feed);
160                 g_free(ctx->error);
161                 g_free(ctx); 
162                 g_free(myurl);
163                 return NULL;
164         }
165
166         debug_print("RSSyl: Adding '%s'\n", ctx->feed->url);
167
168         ritem = (RFolderItem *)new_item;
169         ritem->url = g_strdup(ctx->feed->url);
170
171         if (official_title != NULL) {
172                 debug_print("RSSyl: storing official feed title '%s'\n", official_title);
173                 ritem->official_title = official_title;
174         }
175
176         if (feed_n_items(ctx->feed) > 0)
177                 feed_foreach_item(ctx->feed, rssyl_subscribe_foreach_func, (gpointer)ritem);
178
179         folder_item_scan(new_item);
180         folder_write_list();
181
182         if (edit_properties)
183                 rssyl_gtk_prop(ritem);
184
185         folder_item_update_thaw();
186
187         return new_item;
188 }