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 
28 /* Claws Mail includes */
29 #include <folder.h>
30 #include <alertpanel.h>
31 #include <procheader.h>
32 #include <prefs_common.h>
33 #include <mainwindow.h>
34 
35 /* Local includes */
36 #include "libfeed/feeditem.h"
37 #include "libfeed/date.h"
38 #include "rssyl.h"
39 #include "rssyl_feed.h"
40 #include "rssyl_prefs.h"
41 #include "rssyl_update_feed.h"
42 #include "strutils.h"
43 
rssyl_feed_parse_item_to_msginfo(gchar * file,MsgFlags flags,gboolean a,gboolean b,FolderItem * item)44 MsgInfo *rssyl_feed_parse_item_to_msginfo(gchar *file, MsgFlags flags,
45 		gboolean a, gboolean b, FolderItem *item)
46 {
47 	MsgInfo *msginfo;
48 
49 	g_return_val_if_fail(item != NULL, NULL);
50 
51 	msginfo = procheader_parse_file(file, flags, a, b);
52 	if (msginfo)
53 		msginfo->folder = item;
54 
55 	return msginfo;
56 }
57 
rssyl_refresh_timeout_cb(gpointer data)58 gboolean rssyl_refresh_timeout_cb(gpointer data)
59 {
60 	RRefreshCtx *ctx = (RRefreshCtx *)data;
61 	time_t tt = time(NULL);
62 	gchar *tmpdate = NULL;
63 
64 	g_return_val_if_fail(ctx != NULL, FALSE);
65 
66 	if( prefs_common_get_prefs()->work_offline)
67 		return TRUE;
68 
69 	if( ctx->ritem == NULL || ctx->ritem->url == NULL ) {
70 		debug_print("RSSyl: refresh_timeout_cb - ritem or url NULL\n");
71 		g_free(ctx);
72 		return FALSE;
73 	}
74 
75 	if( ctx->id != ctx->ritem->refresh_id ) {
76 		tmpdate = createRFC822Date(&tt);
77 		debug_print("RSSyl: %s: timeout id changed, stopping: %d != %d\n",
78 				tmpdate, ctx->id, ctx->ritem->refresh_id);
79 		g_free(tmpdate);
80 		g_free(ctx);
81 		return FALSE;
82 	}
83 
84 	tmpdate = createRFC822Date(&tt);
85 
86 	if (prefs_common_get_prefs()->work_offline) {
87 		debug_print("RSSyl: %s: skipping update of %s (%d), we are offline\n",
88 				tmpdate, ctx->ritem->url, ctx->ritem->refresh_id);
89 	} else {
90 		debug_print("RSSyl: %s: updating %s (%d)\n",
91 				tmpdate, ctx->ritem->url, ctx->ritem->refresh_id);
92 		rssyl_update_feed(ctx->ritem, 0);
93 	}
94 
95 	g_free(tmpdate);
96 
97 	return TRUE;
98 }
99 
rssyl_feed_start_refresh_timeout(RFolderItem * ritem)100 void rssyl_feed_start_refresh_timeout(RFolderItem *ritem)
101 {
102 	RRefreshCtx *ctx;
103 	guint source_id;
104 	RPrefs *rsprefs = NULL;
105 
106 	g_return_if_fail(ritem != NULL);
107 
108 	if( ritem->default_refresh_interval ) {
109 		rsprefs = rssyl_prefs_get();
110 		if( !rsprefs->refresh_enabled )
111 			return;
112 		ritem->refresh_interval = rsprefs->refresh;
113 	}
114 
115 	ctx = g_new0(RRefreshCtx, 1);
116 	ctx->ritem = ritem;
117 
118 	source_id = g_timeout_add(ritem->refresh_interval * 60 * 1000,
119 			(GSourceFunc)rssyl_refresh_timeout_cb, ctx );
120 	ritem->refresh_id = source_id;
121 	ctx->id = source_id;
122 
123 	debug_print("RSSyl: start_refresh_timeout - %d min (id %d)\n",
124 			ritem->refresh_interval, ctx->id);
125 }
126