Fix bug #3363 “preferences page composition seems weird”
[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 == FEED_ERR_UNAUTH ) {
100                 debug_print("RSSyl: URL authorization type is unknown\n");
101                 ctx->error = g_strdup("Unknown value for URL authorization type");
102         } else if( ctx->response_code >= 400 && ctx->response_code < 500 ) {
103                 switch( ctx->response_code ) {
104                         case 401:
105                                 ctx->error = g_strdup(_("401 (Authorisation required)"));
106                                 break;
107                         case 403:
108                                 ctx->error = g_strdup(_("403 (Unauthorised)"));
109                                 break;
110                         case 404:
111                                 ctx->error = g_strdup(_("404 (Not found)"));
112                                 break;
113                         default:
114                                 ctx->error = g_strdup_printf(_("Error %d"), ctx->response_code);
115                                 break;
116                 }
117         }
118
119         /* Here we handle "imperfect" conditions. If verbose is TRUE, we also
120          * display error dialogs for user. We always log the error. */
121         if( ctx->error != NULL ) {
122                 /* libcurl wasn't happy */
123                 debug_print("RSSyl: Error: %s\n", ctx->error);
124                 if( verbose ) {
125                         gchar *msg = g_markup_printf_escaped(
126                                         (const char *) C_("First parameter is URL, second is error text",
127                                                 "Error fetching feed at\n<b>%s</b>:\n\n%s"),
128                                         feed_get_url(ctx->feed), ctx->error);
129                         alertpanel_error("%s", msg);
130                         g_free(msg);
131                 }
132
133                 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_FETCH, ctx->feed->url, ctx->error);
134
135                 ctx->success = FALSE;
136         } else {
137                 if( feed_get_title(ctx->feed) == NULL ) {
138                         /* libcurl was happy, but libfeed wasn't */
139                         debug_print("RSSyl: Error reading feed\n");
140                         if( verbose ) {
141                                 gchar *msg = g_markup_printf_escaped(
142                                                 (const char *) _("No valid feed found at\n<b>%s</b>"),
143                                                 feed_get_url(ctx->feed));
144                                 alertpanel_error("%s", msg);
145                                 g_free(msg);
146                         }
147
148                         log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_NOFEED,
149                                         feed_get_url(ctx->feed));
150
151                         ctx->success = FALSE;
152                 }
153         }
154 }
155
156 RFetchCtx *rssyl_prep_fetchctx_from_item(RFolderItem *ritem)
157 {
158         RFetchCtx *ctx = NULL;
159
160         g_return_val_if_fail(ritem != NULL, NULL);
161
162         ctx = g_new0(RFetchCtx, 1);
163         ctx->feed = feed_new(ritem->url);
164         ctx->error = NULL;
165         ctx->success = TRUE;
166         ctx->ready = FALSE;
167
168         feed_set_timeout(ctx->feed, prefs_common.io_timeout_secs);
169         feed_set_cookies_path(ctx->feed, rssyl_prefs_get()->cookies_path);
170         feed_set_ssl_verify_peer(ctx->feed, ritem->ssl_verify_peer);
171         feed_set_auth(ctx->feed, ritem->auth);
172
173         return ctx;
174 }
175
176 RFetchCtx *rssyl_prep_fetchctx_from_url(gchar *url)
177 {
178         RFetchCtx *ctx = NULL;
179
180         g_return_val_if_fail(url != NULL, NULL);
181
182         ctx = g_new0(RFetchCtx, 1);
183         ctx->feed = feed_new(url);
184         ctx->error = NULL;
185         ctx->success = TRUE;
186         ctx->ready = FALSE;
187
188         feed_set_timeout(ctx->feed, prefs_common.io_timeout_secs);
189         feed_set_cookies_path(ctx->feed, rssyl_prefs_get()->cookies_path);
190         feed_set_ssl_verify_peer(ctx->feed, rssyl_prefs_get()->ssl_verify_peer);
191
192         return ctx;
193 }
194
195 /* rssyl_update_feed() */
196
197 gboolean rssyl_update_feed(RFolderItem *ritem, gboolean verbose)
198 {
199         RFetchCtx *ctx = NULL;
200         MainWindow *mainwin = mainwindow_get_mainwindow();
201         gchar *msg = NULL;
202         gboolean success = FALSE;
203
204         g_return_val_if_fail(ritem != NULL, FALSE);
205         g_return_val_if_fail(ritem->url != NULL, FALSE);
206
207         debug_print("RSSyl: starting to update '%s' (%s)\n",
208                         ritem->item.name, ritem->url);
209
210         log_print(LOG_PROTOCOL, RSSYL_LOG_UPDATING, ritem->url);
211
212         msg = g_strdup_printf(_("Updating feed '%s'..."), ritem->item.name);
213         STATUSBAR_PUSH(mainwin, msg);
214         g_free(msg);
215
216         GTK_EVENTS_FLUSH();
217
218         /* Prepare context for fetching the feed file */
219         ctx = rssyl_prep_fetchctx_from_item(ritem);
220         g_return_val_if_fail(ctx != NULL, FALSE);
221
222         /* Fetch the feed file */
223         rssyl_fetch_feed(ctx, verbose);
224
225         debug_print("RSSyl: fetch done; success == %s\n",
226                         ctx->success ? "TRUE" : "FALSE");
227
228         debug_print("RSSyl: STARTING TO PARSE FEED\n");
229   if( ctx->success && !(ctx->success = rssyl_parse_feed(ritem, ctx->feed)) ) {
230                 /* both libcurl and libfeed were happy, but we weren't */
231                 debug_print("RSSyl: Error processing feed\n");
232                 if( verbose ) {
233                         gchar *msg = g_markup_printf_escaped(
234                                         (const char *) _("Couldn't process feed at\n<b>%s</b>\n\n"
235                                                 "Please contact developers, this should not happen."),
236                                         feed_get_url(ctx->feed));
237                         alertpanel_error("%s", msg);
238                         g_free(msg);
239                 }
240
241                 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, ctx->feed->url);
242         }
243         
244         debug_print("RSSyl: FEED PARSED\n");
245
246         STATUSBAR_POP(mainwin);
247
248         if( claws_is_exiting() ) {
249                 feed_free(ctx->feed);
250                 g_free(ctx->error);
251                 g_free(ctx);
252                 return success;
253         }
254
255         if( ritem->fetch_comments )
256                 rssyl_update_comments(ritem);
257
258         /* Prune our deleted items list of items which are no longer in
259          * upstream feed. */
260         rssyl_deleted_expire(ritem, ctx->feed);
261
262         /* Clean up. */
263         success = ctx->success;
264         feed_free(ctx->feed);
265         g_free(ctx->error);
266         g_free(ctx);
267
268         return success;
269 }
270
271 static gboolean rssyl_update_recursively_func(GNode *node, gpointer data)
272 {
273         FolderItem *item;
274         RFolderItem *ritem;
275
276         g_return_val_if_fail(node->data != NULL, FALSE);
277
278         item = FOLDER_ITEM(node->data);
279         ritem = (RFolderItem *)item;
280
281         if( ritem->url != NULL ) {
282                 debug_print("RSSyl: Updating feed '%s'\n", item->name);
283                 rssyl_update_feed(ritem, FALSE);
284         } else
285                 debug_print("RSSyl: Updating in folder '%s'\n", item->name);
286
287         return FALSE;
288 }
289
290 void rssyl_update_recursively(FolderItem *item)
291 {
292         g_return_if_fail(item != NULL);
293         g_return_if_fail(item->folder != NULL);
294
295         if( item->folder->klass != rssyl_folder_get_class() )
296                 return;
297
298         debug_print("Recursively updating '%s'\n", item->name);
299
300         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
301                         rssyl_update_recursively_func, NULL);
302 }
303
304 void rssyl_update_all_func(FolderItem *item, gpointer data)
305 {
306         /* Only try to refresh our feed folders */
307         if( !IS_RSSYL_FOLDER_ITEM(item) )
308                 return;
309
310         if( folder_item_parent(item) == NULL )
311                 rssyl_update_recursively(item);
312 }
313
314 void rssyl_update_all_feeds(void)
315 {
316         if (prefs_common.work_offline &&
317                         !inc_offline_should_override(TRUE,
318                                 _("Claws Mail needs network access in order to update your feeds.")) ) {
319                 return;
320         }
321
322         folder_func_to_all_folders((FolderItemFunc)rssyl_update_all_func, NULL);
323 }