RSSyl: struct dirent doesn't have d_type member on Windows.
[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 *ctx = NULL;
59         DIR *dp;
60         struct dirent *d;
61         gint num;
62         gchar *path, *msg, *fname;
63         MainWindow *mainwin = mainwindow_get_mainwindow();
64
65         g_return_if_fail(ritem != NULL);
66
67         if( ritem->fetch_comments == FALSE )
68                 return;
69
70         path = folder_item_get_path(item);
71         g_return_if_fail(path != NULL);
72
73         debug_print("RSSyl: starting to parse comments, path is '%s'\n", path);
74
75         if( (dp = opendir(path)) == NULL ) {
76                 FILE_OP_ERROR(item->path, "opendir");
77                 g_free(path);
78                 return;
79         }
80
81         ritem->fetching_comments = TRUE;
82
83         while( (d = readdir(dp)) != NULL ) {
84                 if (claws_is_exiting()) {
85                         closedir(dp);
86                         g_free(path);
87                         debug_print("RSSyl: bailing out, app is exiting\n");
88                         return;
89                 }
90
91 #ifdef G_OS_WIN32
92                 if( (num = to_number(d->d_name)) > 0) {
93 #else
94                 if( (num = to_number(d->d_name)) > 0 && d->d_type == DT_REG ) {
95 #endif
96                         debug_print("RSSyl: starting to parse '%s'\n", d->d_name);
97
98                         fname = g_strdup_printf("%s%c%s", path, G_DIR_SEPARATOR, d->d_name);
99                         if( (fi = rssyl_parse_folder_item_file(fname)) != NULL ) {
100                                 if( feed_item_get_comments_url(fi) && feed_item_get_id(fi) &&
101                                                 (ritem->fetch_comments_max_age == -1 ||
102                                                  time(NULL) - feed_item_get_date_modified(fi) <= ritem->fetch_comments_max_age*86400)) {
103                                         msg = g_strdup_printf(_("Updating comments for '%s'..."),
104                                                         feed_item_get_title(fi));
105                                         debug_print("RSSyl: updating comments for '%s' (%s)\n",
106                                                         feed_item_get_title(fi), feed_item_get_comments_url(fi));
107                                         STATUSBAR_PUSH(mainwin, msg);
108
109                                         ctx = rssyl_prep_fetchctx_from_url(feed_item_get_comments_url(fi));
110                                         g_return_if_fail(ctx != NULL);
111                                         feed_set_ssl_verify_peer(ctx->feed, ritem->ssl_verify_peer);
112
113                                         rssyl_fetch_feed(ctx, FALSE);
114                                         
115                                         if( ctx->success && feed_n_items(ctx->feed) > 0 ) {
116                                                 g_free(ctx->feed->title);
117                                                 ctx->feed->title = g_strdup(ritem->official_title);
118
119                                                 feed_foreach_item(ctx->feed, rssyl_update_reference_func,
120                                                                 feed_item_get_id(fi));
121
122                                                 if( !rssyl_parse_feed(ritem, ctx->feed) ) {
123                                                         debug_print("RSSyl: Error processing comments feed\n");
124                                                         log_error(LOG_PROTOCOL, RSSYL_LOG_ERROR_PROC, ctx->feed->url);
125                                                 }
126                                         }
127                                 }
128
129                                 STATUSBAR_POP(mainwin);
130
131                                 feed_item_free(fi);
132                         }
133
134                         g_free(fname);
135                 }
136         }
137
138         closedir(dp);
139         g_free(path);
140
141         ritem->fetching_comments = FALSE;
142
143         debug_print("RSSyl: rssyl_update_comments() is done\n");
144 }