Fix a memory leak in rssyl_update_comments().
[claws.git] / src / plugins / rssyl / rssyl_update_comments.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 <log.h>
31 #include <mainwindow.h>
32 #include <statusbar.h>
33 #include <main.h>
34
35 /* Local includes */
36 #include "libfeed/feed.h"
37 #include "rssyl.h"
38 #include "rssyl_feed.h"
39 #include "rssyl_parse_feed.h"
40 #include "rssyl_update_feed.h"
41 #include "parse822.h"
42
43 static void rssyl_update_reference_func(gpointer data, gpointer user_data)
44 {
45         FeedItem *item = (FeedItem *)data;
46         gchar *parent_id = (gchar *)user_data;
47
48         g_return_if_fail(item != NULL);
49         g_return_if_fail(user_data != NULL);
50
51         feed_item_set_parent_id(item, parent_id);
52 }
53
54 void rssyl_update_comments(RFolderItem *ritem)
55 {
56         FolderItem *item = &ritem->item;
57         FeedItem *fi = NULL;
58         RFetchCtx *fetchctx = NULL;
59         RFeedCtx *feedctx = NULL;
60         GDir *dp;
61         const gchar *d;
62         GError *error = NULL;
63         gint num;
64         gchar *path, *msg, *fname;
65         MainWindow *mainwin = mainwindow_get_mainwindow();
66
67         g_return_if_fail(ritem != NULL);
68
69         if( ritem->fetch_comments == FALSE )
70                 return;
71
72         path = folder_item_get_path(item);
73         g_return_if_fail(path != NULL);
74
75         debug_print("RSSyl: starting to parse comments, path is '%s'\n", path);
76
77         if( (dp = g_dir_open(path, 0, &error)) == NULL ) {
78                 debug_print("g_dir_open on \"%s\" failed with error %d (%s)\n",
79                                 path, error->code, error->message);
80                 g_error_free(error);
81                 g_free(path);
82                 return;
83         }
84
85         ritem->fetching_comments = TRUE;
86
87         while( (d = g_dir_read_name(dp)) != NULL ) {
88                 if (claws_is_exiting()) {
89                         g_dir_close(dp);
90                         g_free(path);
91                         debug_print("RSSyl: bailing out, app is exiting\n");
92                         return;
93                 }
94
95                 if( (num = to_number(d)) > 0) {
96                         fname = g_strdup_printf("%s%c%s", path, G_DIR_SEPARATOR, d);
97                         if (!g_file_test(fname, G_FILE_TEST_IS_REGULAR))
98                                 continue;
99
100                         debug_print("RSSyl: starting to parse '%s'\n", d);
101
102                         if( (fi = rssyl_parse_folder_item_file(fname)) != NULL ) {
103                                 feedctx = (RFeedCtx *)fi->data;
104                                 if( feed_item_get_comments_url(fi) && feed_item_get_id(fi) &&
105                                                 (ritem->fetch_comments_max_age == -1 ||
106                                                  time(NULL) - feed_item_get_date_modified(fi) <= ritem->fetch_comments_max_age*86400)) {
107                                         msg = g_strdup_printf(_("Updating comments for '%s'..."),
108                                                         feed_item_get_title(fi));
109                                         debug_print("RSSyl: updating comments for '%s' (%s)\n",
110                                                         feed_item_get_title(fi), feed_item_get_comments_url(fi));
111                                         STATUSBAR_PUSH(mainwin, msg);
112
113                                         fetchctx = rssyl_prep_fetchctx_from_url(feed_item_get_comments_url(fi));
114                                         if (fetchctx != NULL) {
115                                                 feed_set_ssl_verify_peer(fetchctx->feed, ritem->ssl_verify_peer);
116
117                                                 rssyl_fetch_feed(fetchctx, FALSE);
118                                         
119                                                 if( fetchctx->success && feed_n_items(fetchctx->feed) > 0 ) {
120                                                         g_free(fetchctx->feed->title);
121                                                         fetchctx->feed->title = g_strdup(ritem->official_title);
122
123                                                         feed_foreach_item(fetchctx->feed, rssyl_update_reference_func,
124                                                                         feed_item_get_id(fi));
125
126                                                         if( !rssyl_parse_feed(ritem, fetchctx->feed) ) {
127                                                                 debug_print("RSSyl: Error processing comments feed\n");
128                                                                 log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, fetchctx->feed->url);
129                                                         }
130                                                 }
131                                         }
132                                         STATUSBAR_POP(mainwin);
133                                 }
134                         }
135
136                         g_free(feedctx->path);
137                         feed_item_free(fi);
138                         g_free(fname);
139                 }
140         }
141
142         g_dir_close(dp);
143         g_free(path);
144
145         ritem->fetching_comments = FALSE;
146
147         debug_print("RSSyl: rssyl_update_comments() is done\n");
148 }