7f032fac072a657ec3f65cfef5a0f319c6a5e737
[claws.git] / src / plugins / rssyl / rssyl_update_feed.c
1 /*
2  * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 /* Global includes */
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <pthread.h>
28
29 /* Claws Mail includes */
30 #include <common/claws.h>
31 #include <mainwindow.h>
32 #include <statusbar.h>
33 #include <alertpanel.h>
34 #include <log.h>
35 #include <prefs_common.h>
36 #include <inc.h>
37 #include <main.h>
38
39 /* Local includes */
40 #include "libfeed/feed.h"
41 #include "rssyl.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"
47
48 /* rssyl_fetch_feed_thr() */
49
50 static void *rssyl_fetch_feed_thr(void *arg)
51 {
52         RFetchCtx *ctx = (RFetchCtx *)arg;
53
54         /* Fetch and parse the feed. */
55         ctx->response_code = feed_update(ctx->feed, -1);
56
57         /* Signal main thread that we're done here. */
58         ctx->ready = TRUE;
59
60         return NULL;
61 }
62
63 /* rssyl_fetch_feed() */
64 void rssyl_fetch_feed(RFetchCtx *ctx, gboolean verbose)
65 {
66 #ifdef USE_PTHREAD
67         pthread_t pt;
68 #endif
69
70         g_return_if_fail(ctx != NULL);
71
72 #ifdef USE_PTHREAD
73         if( pthread_create(&pt, PTHREAD_CREATE_JOINABLE, rssyl_fetch_feed_thr,
74                                 (void *)ctx) != 0 ) {
75                 /* Bummer, couldn't create thread. Continue non-threaded. */
76                 rssyl_fetch_feed_thr(ctx);
77         } else {
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 ) {
82                         claws_do_idle();
83                 }
84
85                 debug_print("RSSyl: thread finished\n");
86                 pthread_join(pt, NULL);
87         }
88 #else
89         debug_print("RSSyl: no pthreads available, running non-threaded fetch\n");
90         rssyl_fetch_feed_thr(ctx);
91 #endif
92
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 ) {
101                         case 401:
102                                 ctx->error = g_strdup(_("401 (Authorisation required)"));
103                                 break;
104                         case 403:
105                                 ctx->error = g_strdup(_("403 (Unauthorised)"));
106                                 break;
107                         case 404:
108                                 ctx->error = g_strdup(_("404 (Not found)"));
109                                 break;
110                         default:
111                                 ctx->error = g_strdup_printf(_("Error %d"), ctx->response_code);
112                                 break;
113                 }
114         }
115
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);
121                 if( verbose )
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));
125
126                 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_FETCH, ctx->feed->url, ctx->error);
127
128                 ctx->success = FALSE;
129         } else {
130                 if( feed_get_title(ctx->feed) == NULL ) {
131                         /* libcurl was happy, but libfeed wasn't */
132                         debug_print("RSSyl: Error reading feed\n");
133                         if( verbose )
134                                 alertpanel_error(g_markup_printf_escaped(_("No valid feed found at\n<b>%s</b>"),
135                                                         feed_get_url(ctx->feed)));
136
137                         log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_NOFEED,
138                                         feed_get_url(ctx->feed));
139
140                         ctx->success = FALSE;
141                 }
142         }
143 }
144
145 RFetchCtx *rssyl_prep_fetchctx_from_item(RFolderItem *ritem)
146 {
147         RFetchCtx *ctx = NULL;
148
149         g_return_val_if_fail(ritem != NULL, NULL);
150
151         ctx = g_new0(RFetchCtx, 1);
152         ctx->feed = feed_new(ritem->url);
153         ctx->error = NULL;
154         ctx->success = TRUE;
155         ctx->ready = FALSE;
156
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);
160
161         return ctx;
162 }
163
164 RFetchCtx *rssyl_prep_fetchctx_from_url(gchar *url)
165 {
166         RFetchCtx *ctx = NULL;
167
168         g_return_val_if_fail(url != NULL, NULL);
169
170         ctx = g_new0(RFetchCtx, 1);
171         ctx->feed = feed_new(url);
172         ctx->error = NULL;
173         ctx->success = TRUE;
174         ctx->ready = FALSE;
175
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);
179
180         return ctx;
181 }
182
183 /* rssyl_update_feed() */
184
185 gboolean rssyl_update_feed(RFolderItem *ritem, gboolean verbose)
186 {
187         RFetchCtx *ctx = NULL;
188         MainWindow *mainwin = mainwindow_get_mainwindow();
189         gchar *msg = NULL;
190         gboolean success = FALSE;
191
192         g_return_val_if_fail(ritem != NULL, FALSE);
193         g_return_val_if_fail(ritem->url != NULL, FALSE);
194
195         debug_print("RSSyl: starting to update '%s' (%s)\n",
196                         ritem->item.name, ritem->url);
197
198         log_print(LOG_PROTOCOL, RSSYL_LOG_UPDATING, ritem->url);
199
200         msg = g_strdup_printf(_("Updating feed '%s'..."), ritem->item.name);
201         STATUSBAR_PUSH(mainwin, msg);
202         g_free(msg);
203
204         GTK_EVENTS_FLUSH();
205
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);
209
210         /* Fetch the feed file */
211         rssyl_fetch_feed(ctx, verbose);
212
213         debug_print("RSSyl: fetch done; success == %s\n",
214                         ctx->success ? "TRUE" : "FALSE");
215
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");
220                 if( verbose )
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)));
223
224                 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, ctx->feed->url);
225         }
226         
227         debug_print("RSSyl: FEED PARSED\n");
228
229         STATUSBAR_POP(mainwin);
230
231         if( claws_is_exiting() ) {
232                 feed_free(ctx->feed);
233                 g_free(ctx->error);
234                 g_free(ctx);
235                 return success;
236         }
237
238         if( ritem->fetch_comments )
239                 rssyl_update_comments(ritem);
240
241         /* Prune our deleted items list of items which are no longer in
242          * upstream feed. */
243         rssyl_deleted_expire(ritem, ctx->feed);
244
245         /* Clean up. */
246         success = ctx->success;
247         feed_free(ctx->feed);
248         g_free(ctx->error);
249         g_free(ctx);
250
251         return success;
252 }
253
254 static gboolean rssyl_update_recursively_func(GNode *node, gpointer data)
255 {
256         FolderItem *item;
257         RFolderItem *ritem;
258
259         g_return_val_if_fail(node->data != NULL, FALSE);
260
261         item = FOLDER_ITEM(node->data);
262         ritem = (RFolderItem *)item;
263
264         if( ritem->url != NULL ) {
265                 debug_print("RSSyl: Updating feed '%s'\n", item->name);
266                 rssyl_update_feed(ritem, FALSE);
267         } else
268                 debug_print("RSSyl: Updating in folder '%s'\n", item->name);
269
270         return FALSE;
271 }
272
273 void rssyl_update_recursively(FolderItem *item)
274 {
275         g_return_if_fail(item != NULL);
276         g_return_if_fail(item->folder != NULL);
277
278         if( item->folder->klass != rssyl_folder_get_class() )
279                 return;
280
281         debug_print("Recursively updating '%s'\n", item->name);
282
283         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
284                         rssyl_update_recursively_func, NULL);
285 }
286
287 void rssyl_update_all_func(FolderItem *item, gpointer data)
288 {
289         /* Only try to refresh our feed folders */
290         if( !IS_RSSYL_FOLDER_ITEM(item) )
291                 return;
292
293         if( folder_item_parent(item) == NULL )
294                 rssyl_update_recursively(item);
295 }
296
297 void rssyl_update_all_feeds(void)
298 {
299         if (prefs_common.work_offline &&
300                         !inc_offline_should_override(TRUE,
301                                 _("Claws Mail needs network access in order to update your feeds.")) ) {
302                 return;
303         }
304
305         folder_func_to_all_folders((FolderItemFunc)rssyl_update_all_func, NULL);
306 }