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 */
30 #include <alertpanel.h>
31 #include <procheader.h>
32 #include <prefs_common.h>
33 #include <mainwindow.h>
36 #include "libfeed/feeditem.h"
37 #include "libfeed/date.h"
39 #include "rssyl_feed.h"
40 #include "rssyl_prefs.h"
41 #include "rssyl_update_feed.h"
44 FolderItem *rssyl_feed_subscribe_new(FolderItem *parent, const gchar *url,
47 gchar *myurl = NULL, *tmpname = NULL;
48 FolderItem *new_item = NULL;
49 RFolderItem *ritem = NULL;
50 gboolean success = FALSE;
52 g_return_val_if_fail(parent != NULL, FALSE);
53 g_return_val_if_fail(url != NULL, FALSE);
55 log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBING, url);
57 if( !strncmp(url, "feed://", 7) )
58 myurl = g_strdup(url+7);
59 else if( !strncmp(url, "feed:", 5) )
60 myurl = g_strdup(url+5);
62 myurl = g_strdup(url);
64 myurl = g_strchomp(myurl);
66 folderview_freeze(mainwindow_get_mainwindow()->folderview);
67 folder_item_update_freeze();
69 /* Create a feed folder with generic name. */
70 tmpname = g_strdup_printf("%s.%ld", RSSYL_NEW_FOLDER_NAME, (long int)time(NULL));
71 new_item = folder_create_folder(parent, tmpname);
75 alertpanel_error(_("Couldn't create folder for new feed '%s'."),
81 /* Set it up as a RSSyl folder */
82 ritem = (RFolderItem *)new_item;
83 ritem->url = g_strdup(myurl);
85 /* Try to update it, delete if failed.
86 * (it is renamed in rssyl_update_feed(). */
87 if( (success = rssyl_update_feed(ritem, verbose)) == FALSE )
88 new_item->folder->klass->remove_folder(new_item->folder, new_item);
90 folder_item_scan(new_item);
94 folder_item_update_thaw();
95 folderview_thaw(mainwindow_get_mainwindow()->folderview);
98 log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBED, ritem->official_title,
101 debug_print("RSSyl: Failed to add feed '%s'\n", myurl);
109 MsgInfo *rssyl_feed_parse_item_to_msginfo(gchar *file, MsgFlags flags,
110 gboolean a, gboolean b, FolderItem *item)
114 g_return_val_if_fail(item != NULL, NULL);
116 msginfo = procheader_parse_file(file, flags, a, b);
118 msginfo->folder = item;
123 gboolean rssyl_refresh_timeout_cb(gpointer data)
125 RRefreshCtx *ctx = (RRefreshCtx *)data;
126 time_t tt = time(NULL);
127 gchar *tmpdate = NULL;
129 g_return_val_if_fail(ctx != NULL, FALSE);
131 if( prefs_common_get_prefs()->work_offline)
134 if( ctx->ritem == NULL || ctx->ritem->url == NULL ) {
135 debug_print("RSSyl: refresh_timeout_cb - ritem or url NULL\n");
140 if( ctx->id != ctx->ritem->refresh_id ) {
141 tmpdate = createRFC822Date(&tt);
142 debug_print("RSSyl: %s: timeout id changed, stopping: %d != %d\n",
143 tmpdate, ctx->id, ctx->ritem->refresh_id);
149 tmpdate = createRFC822Date(&tt);
150 debug_print(" %s: refresh %s (%d)\n", tmpdate, ctx->ritem->url,
151 ctx->ritem->refresh_id);
153 rssyl_update_feed(ctx->ritem, FALSE);
158 void rssyl_feed_start_refresh_timeout(RFolderItem *ritem)
162 RPrefs *rsprefs = NULL;
164 g_return_if_fail(ritem != NULL);
166 if( ritem->default_refresh_interval ) {
167 rsprefs = rssyl_prefs_get();
168 if( !rsprefs->refresh_enabled )
170 ritem->refresh_interval = rsprefs->refresh;
173 ctx = g_new0(RRefreshCtx, 1);
176 source_id = g_timeout_add(ritem->refresh_interval * 60 * 1000,
177 (GSourceFunc)rssyl_refresh_timeout_cb, ctx );
178 ritem->refresh_id = source_id;
181 debug_print("RSSyl: start_refresh_timeout - %d min (id %d)\n",
182 ritem->refresh_interval, ctx->id);