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.
26 #include <glib/gi18n.h>
29 /* Claws Mail includes */
30 #include <common/claws.h>
31 #include <mainwindow.h>
32 #include <statusbar.h>
33 #include <alertpanel.h>
35 #include <prefs_common.h>
40 #include "libfeed/feed.h"
42 #include "rssyl_deleted.h"
43 #include "rssyl_feed.h"
44 #include "rssyl_parse_feed.h"
45 #include "rssyl_prefs.h"
46 #include "rssyl_update_comments.h"
48 /* rssyl_fetch_feed_thr() */
50 static void *rssyl_fetch_feed_thr(void *arg)
52 RFetchCtx *ctx = (RFetchCtx *)arg;
54 /* Fetch and parse the feed. */
55 ctx->response_code = feed_update(ctx->feed, -1);
57 /* Signal main thread that we're done here. */
63 /* rssyl_fetch_feed() */
64 void rssyl_fetch_feed(RFetchCtx *ctx, gboolean verbose)
70 g_return_if_fail(ctx != NULL);
73 if( pthread_create(&pt, PTHREAD_CREATE_JOINABLE, rssyl_fetch_feed_thr,
75 /* Bummer, couldn't create thread. Continue non-threaded. */
76 rssyl_fetch_feed_thr(ctx);
78 /* Thread created, let's wait until it finishes. */
79 debug_print("RSSyl: waiting for thread to finish (timeout: %ds)\n",
80 feed_get_timeout(ctx->feed));
81 while( !ctx->ready ) {
85 debug_print("RSSyl: thread finished\n");
86 pthread_join(pt, NULL);
89 debug_print("RSSyl: no pthreads available, running non-threaded fetch\n");
90 rssyl_fetch_feed_thr(ctx);
93 if( ctx->response_code == FEED_ERR_INIT ) {
94 debug_print("RSSyl: libfeed reports init error from libcurl\n");
95 ctx->error = g_strdup("Internal error");
96 } else if( ctx->response_code == FEED_ERR_FETCH ) {
97 debug_print("RSSyl: libfeed reports some other error from libcurl\n");
98 ctx->error = g_strdup(ctx->feed->fetcherr);
99 } else if( ctx->response_code >= 400 && ctx->response_code < 500 ) {
100 switch( ctx->response_code ) {
102 ctx->error = g_strdup(_("401 (Authorisation required)"));
105 ctx->error = g_strdup(_("403 (Unauthorised)"));
108 ctx->error = g_strdup(_("404 (Not found)"));
111 ctx->error = g_strdup_printf(_("Error %d"), ctx->response_code);
116 /* Here we handle "imperfect" conditions. If verbose is TRUE, we also
117 * display error dialogs for user. We always log the error. */
118 if( ctx->error != NULL ) {
119 /* libcurl wasn't happy */
120 debug_print("RSSyl: Error: %s\n", ctx->error);
122 alertpanel_error(g_markup_printf_escaped(C_("First parameter is URL, second is error text",
123 "Error fetching feed at\n<b>%s</b>:\n\n%s"),
124 feed_get_url(ctx->feed), ctx->error));
126 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_FETCH, ctx->feed->url, ctx->error);
128 ctx->success = FALSE;
130 if( feed_get_title(ctx->feed) == NULL ) {
131 /* libcurl was happy, but libfeed wasn't */
132 debug_print("RSSyl: Error reading feed\n");
134 alertpanel_error(g_markup_printf_escaped(_("No valid feed found at\n<b>%s</b>"),
135 feed_get_url(ctx->feed)));
137 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_NOFEED,
138 feed_get_url(ctx->feed));
140 ctx->success = FALSE;
145 RFetchCtx *rssyl_prep_fetchctx_from_item(RFolderItem *ritem)
147 RFetchCtx *ctx = NULL;
149 g_return_val_if_fail(ritem != NULL, NULL);
151 ctx = g_new0(RFetchCtx, 1);
152 ctx->feed = feed_new(ritem->url);
157 feed_set_timeout(ctx->feed, prefs_common.io_timeout_secs);
158 feed_set_cookies_path(ctx->feed, rssyl_prefs_get()->cookies_path);
159 feed_set_ssl_verify_peer(ctx->feed, ritem->ssl_verify_peer);
164 RFetchCtx *rssyl_prep_fetchctx_from_url(gchar *url)
166 RFetchCtx *ctx = NULL;
168 g_return_val_if_fail(url != NULL, NULL);
170 ctx = g_new0(RFetchCtx, 1);
171 ctx->feed = feed_new(url);
176 feed_set_timeout(ctx->feed, prefs_common.io_timeout_secs);
177 feed_set_cookies_path(ctx->feed, rssyl_prefs_get()->cookies_path);
178 feed_set_ssl_verify_peer(ctx->feed, rssyl_prefs_get()->ssl_verify_peer);
183 /* rssyl_update_feed() */
185 gboolean rssyl_update_feed(RFolderItem *ritem, gboolean verbose)
187 RFetchCtx *ctx = NULL;
188 MainWindow *mainwin = mainwindow_get_mainwindow();
190 gboolean success = FALSE;
192 g_return_val_if_fail(ritem != NULL, FALSE);
193 g_return_val_if_fail(ritem->url != NULL, FALSE);
195 debug_print("RSSyl: starting to update '%s' (%s)\n",
196 ritem->item.name, ritem->url);
198 log_print(LOG_PROTOCOL, RSSYL_LOG_UPDATING, ritem->url);
200 msg = g_strdup_printf(_("Updating feed '%s'..."), ritem->item.name);
201 STATUSBAR_PUSH(mainwin, msg);
206 /* Prepare context for fetching the feed file */
207 ctx = rssyl_prep_fetchctx_from_item(ritem);
208 g_return_val_if_fail(ctx != NULL, FALSE);
210 /* Fetch the feed file */
211 rssyl_fetch_feed(ctx, verbose);
213 debug_print("RSSyl: fetch done; success == %s\n",
214 ctx->success ? "TRUE" : "FALSE");
216 debug_print("RSSyl: STARTING TO PARSE FEED\n");
217 if( ctx->success && !(ctx->success = rssyl_parse_feed(ritem, ctx->feed)) ) {
218 /* both libcurl and libfeed were happy, but we weren't */
219 debug_print("RSSyl: Error processing feed\n");
221 alertpanel_error(g_markup_printf_escaped(_("Couldn't process feed at\n<b>%s</b>\n\nPlease contact developers, this should not happen."),
222 feed_get_url(ctx->feed)));
224 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, ctx->feed->url);
227 debug_print("RSSyl: FEED PARSED\n");
229 STATUSBAR_POP(mainwin);
231 if( claws_is_exiting() ) {
232 feed_free(ctx->feed);
238 if( ritem->fetch_comments )
239 rssyl_update_comments(ritem);
241 /* Prune our deleted items list of items which are no longer in
243 rssyl_deleted_expire(ritem, ctx->feed);
246 success = ctx->success;
247 feed_free(ctx->feed);
254 static gboolean rssyl_update_recursively_func(GNode *node, gpointer data)
259 g_return_val_if_fail(node->data != NULL, FALSE);
261 item = FOLDER_ITEM(node->data);
262 ritem = (RFolderItem *)item;
264 if( ritem->url != NULL ) {
265 debug_print("RSSyl: Updating feed '%s'\n", item->name);
266 rssyl_update_feed(ritem, FALSE);
268 debug_print("RSSyl: Updating in folder '%s'\n", item->name);
273 void rssyl_update_recursively(FolderItem *item)
275 g_return_if_fail(item != NULL);
276 g_return_if_fail(item->folder != NULL);
278 if( item->folder->klass != rssyl_folder_get_class() )
281 debug_print("Recursively updating '%s'\n", item->name);
283 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
284 rssyl_update_recursively_func, NULL);
287 void rssyl_update_all_func(FolderItem *item, gpointer data)
289 /* Only try to refresh our feed folders */
290 if( !IS_RSSYL_FOLDER_ITEM(item) )
293 if( folder_item_parent(item) == NULL )
294 rssyl_update_recursively(item);
297 void rssyl_update_all_feeds(void)
299 if (prefs_common.work_offline &&
300 !inc_offline_should_override(TRUE,
301 _("Claws Mail needs network access in order to update your feeds.")) ) {
305 folder_func_to_all_folders((FolderItemFunc)rssyl_update_all_func, NULL);