Remove rssyl_feed_subscribe_new(), it was redundant.
[claws.git] / src / plugins / rssyl / rssyl_feed.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
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
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
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         debug_print(" %s: refresh %s (%d)\n", tmpdate, ctx->ritem->url,
86                         ctx->ritem->refresh_id);
87         g_free(tmpdate);
88         rssyl_update_feed(ctx->ritem, FALSE);
89
90         return TRUE;
91 }
92
93 void rssyl_feed_start_refresh_timeout(RFolderItem *ritem)
94 {
95         RRefreshCtx *ctx;
96         guint source_id;
97         RPrefs *rsprefs = NULL;
98
99         g_return_if_fail(ritem != NULL);
100
101         if( ritem->default_refresh_interval ) {
102                 rsprefs = rssyl_prefs_get();
103                 if( !rsprefs->refresh_enabled )
104                         return;
105                 ritem->refresh_interval = rsprefs->refresh;
106         }
107
108         ctx = g_new0(RRefreshCtx, 1);
109         ctx->ritem = ritem;
110
111         source_id = g_timeout_add(ritem->refresh_interval * 60 * 1000,
112                         (GSourceFunc)rssyl_refresh_timeout_cb, ctx );
113         ritem->refresh_id = source_id;
114         ctx->id = source_id;
115
116         debug_print("RSSyl: start_refresh_timeout - %d min (id %d)\n",
117                         ritem->refresh_interval, ctx->id);
118 }