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>
28 /* Claws Mail includes */
35 #include "libfeed/feed.h"
36 #include "libfeed/feeditem.h"
37 #include "libfeed/date.h"
40 #include "rssyl_add_item.h"
41 #include "rssyl_deleted.h"
42 #include "rssyl_feed.h"
43 #include "rssyl_parse_feed.h"
44 #include "rssyl_prefs.h"
47 static void rssyl_foreach_parse_func(gpointer data, gpointer user_data)
49 FeedItem *feed_item = (FeedItem *)data;
50 RFolderItem *ritem = (RFolderItem *)user_data;
52 rssyl_add_item(ritem, feed_item);
55 struct _RSSylExpireItemsCtx {
60 typedef struct _RSSylExpireItemsCtx RSSylExpireItemsCtx;
62 static void expire_items_func(gpointer data, gpointer user_data)
64 RSSylExpireItemsCtx *ctx = (RSSylExpireItemsCtx *)user_data;
65 FeedItem *item = (FeedItem *)data;
66 gchar *id = NULL, *id2 = NULL;
68 if( (id = feed_item_get_id(item)) == NULL )
69 id = feed_item_get_url(item);
74 if( (id2 = feed_item_get_id(ctx->item)) == NULL )
75 id2 = feed_item_get_url(ctx->item);
80 /* Simply check ID, as we should have up-to-date items right now. */
81 if( !strcmp(id, id2) )
85 static void rssyl_expire_items(RFolderItem *ritem, Feed *feed)
87 FeedItem *item = NULL;
89 RSSylExpireItemsCtx *ctx = NULL;
92 debug_print("RSSyl: rssyl_expire_items()\n");
94 g_return_if_fail(ritem != NULL);
95 g_return_if_fail(ritem->items != NULL);
96 g_return_if_fail(feed != NULL);
98 ctx = malloc( sizeof(RSSylExpireItemsCtx) );
100 /* Check each locally stored item, if it is still in the upstream
101 * feed - xnay it if not. */
102 for( i = ritem->items; i != NULL; i = i->next ) {
103 item = (FeedItem *)i->data;
105 /* Do not expire comments, they expire with their parents */
106 if (feed_item_get_parent_id(item) != NULL)
111 feed_foreach_item(feed, expire_items_func, ctx);
114 fctx = (RFeedCtx *)item->data;
115 /* TODO: expire item's comments (items with our parent_id) */
116 g_remove(fctx->path);
123 /* -------------------------------------------------------------------------
124 * rssyl_parse_feed() */
126 gboolean rssyl_parse_feed(RFolderItem *ritem, Feed *feed)
128 gchar *tmp = NULL, *tmp2 = NULL;
131 g_return_val_if_fail(ritem != NULL, FALSE);
132 g_return_val_if_fail(feed != NULL, FALSE);
133 g_return_val_if_fail(feed->title != NULL, FALSE);
135 debug_print("RSSyl: parse_feed\n");
137 /* Set the last_update timestamp here, so it is the same for all items */
138 ritem->last_update = time(NULL);
140 /* If the upstream feed changed its title, change name of our folder
141 * accordingly even if user has renamed it before. This makes sure that
142 * user will be aware of the upstream title change. */
143 if( !ritem->ignore_title_rename &&
144 (ritem->official_title == NULL ||
145 strcmp(feed->title, ritem->official_title)) ) {
146 g_free(ritem->official_title);
147 ritem->official_title = g_strdup(feed->title);
149 tmp = rssyl_format_string(feed->title, TRUE, TRUE);
151 tmp2 = g_strdup(tmp);
152 while (folder_item_rename(&ritem->item, tmp2) != 0 && i < 20) {
154 tmp2 = g_strdup_printf("%s__%d", tmp, ++i);
155 debug_print("RSSyl: couldn't rename, trying '%s'\n", tmp2);
157 /* TODO: handle case when i reaches 20 */
162 /* FIXME: update name in properties */
163 /* FIXME: store feed properties */
166 folder_item_update_freeze();
168 /* Read contents of folder, so we can check for duplicates/updates */
169 rssyl_folder_read_existing(ritem);
171 if( claws_is_exiting() ) {
172 debug_print("RSSyl: Claws-Mail is exiting, bailing out\n");
173 log_print(LOG_PROTOCOL, RSSYL_LOG_ABORTED_EXITING, ritem->url);
174 folder_item_update_thaw();
178 /* Populate the ->deleted_items list so that we can check it when
179 * adding each item. */
180 ritem->deleted_items = rssyl_deleted_update(ritem);
182 /* Parse each item in the feed, adding or updating existing items if
184 if( feed_n_items(feed) > 0 )
185 feed_foreach_item(feed, rssyl_foreach_parse_func, (gpointer)ritem);
187 if( !ritem->keep_old && !ritem->fetching_comments ) {
188 rssyl_folder_read_existing(ritem);
189 rssyl_expire_items(ritem, feed);
192 rssyl_deleted_free(ritem->deleted_items);
194 folder_item_scan(&ritem->item);
195 folder_item_update_thaw();
197 if( !ritem->fetching_comments )
198 log_print(LOG_PROTOCOL, RSSYL_LOG_UPDATED, ritem->url);