Use accessor for common preferences in all plugins.
[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 FolderItem *rssyl_feed_subscribe_new(FolderItem *parent, const gchar *url,
45                 gboolean verbose)
46 {
47         gchar *myurl = NULL, *tmpname = NULL;
48         FolderItem *new_item = NULL;
49         RFolderItem *ritem = NULL;
50         gboolean success = FALSE;
51
52         g_return_val_if_fail(parent != NULL, FALSE);
53         g_return_val_if_fail(url != NULL, FALSE);
54
55         log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBING, url);
56
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);
61         else
62                 myurl = g_strdup(url);
63
64         myurl = g_strchomp(myurl);
65
66         gtk_cmclist_freeze(GTK_CMCLIST(mainwindow_get_mainwindow()->folderview->ctree));
67         folder_item_update_freeze();
68
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);
72         g_free(tmpname);
73         if( !new_item ) {
74                 if( verbose )
75                         alertpanel_error(_("Couldn't create folder for new feed '%s'."),
76                                                 myurl);
77                 g_free(myurl);
78                 return NULL;
79         }
80
81         /* Set it up as a RSSyl folder */
82         ritem = (RFolderItem *)new_item;
83         ritem->url = g_strdup(myurl);
84
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);
89         else {
90                 folder_item_scan(new_item);
91                 folder_write_list();
92         }
93
94         folder_item_update_thaw();
95         gtk_cmclist_thaw(GTK_CMCLIST(mainwindow_get_mainwindow()->folderview->ctree));
96
97         if( success )
98                 log_print(LOG_PROTOCOL, RSSYL_LOG_SUBSCRIBED, ritem->official_title,
99                                 ritem->url);
100         else {
101                 debug_print("RSSyl: Failed to add feed '%s'\n", myurl);
102                 g_free(myurl);
103                 return NULL;
104         }
105
106         return new_item;
107 }
108
109 MsgInfo *rssyl_feed_parse_item_to_msginfo(gchar *file, MsgFlags flags,
110                 gboolean a, gboolean b, FolderItem *item)
111 {
112         MsgInfo *msginfo;
113
114         g_return_val_if_fail(item != NULL, NULL);
115
116         msginfo = procheader_parse_file(file, flags, a, b);
117         if (msginfo)
118                 msginfo->folder = item;
119
120         return msginfo;
121 }
122
123 gboolean rssyl_refresh_timeout_cb(gpointer data)
124 {
125         RRefreshCtx *ctx = (RRefreshCtx *)data;
126         time_t tt = time(NULL);
127         gchar *tmpdate = NULL;
128
129         g_return_val_if_fail(ctx != NULL, FALSE);
130
131         if( prefs_common_get_prefs()->work_offline)
132                 return TRUE;
133
134         if( ctx->ritem == NULL || ctx->ritem->url == NULL ) {
135                 debug_print("RSSyl: refresh_timeout_cb - ritem or url NULL\n");
136                 g_free(ctx);
137                 return FALSE;
138         }
139
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);
144                 g_free(tmpdate);
145                 g_free(ctx);
146                 return FALSE;
147         }
148
149         tmpdate = createRFC822Date(&tt);
150         debug_print(" %s: refresh %s (%d)\n", tmpdate, ctx->ritem->url,
151                         ctx->ritem->refresh_id);
152         g_free(tmpdate);
153         rssyl_update_feed(ctx->ritem, FALSE);
154
155         return TRUE;
156 }
157
158 void rssyl_feed_start_refresh_timeout(RFolderItem *ritem)
159 {
160         RRefreshCtx *ctx;
161         guint source_id;
162         RPrefs *rsprefs = NULL;
163
164         g_return_if_fail(ritem != NULL);
165
166         if( ritem->default_refresh_interval ) {
167                 rsprefs = rssyl_prefs_get();
168                 if( !rsprefs->refresh_enabled )
169                         return;
170                 ritem->refresh_interval = rsprefs->refresh;
171         }
172
173         ctx = g_new0(RRefreshCtx, 1);
174         ctx->ritem = ritem;
175
176         source_id = g_timeout_add(ritem->refresh_interval * 60 * 1000,
177                         (GSourceFunc)rssyl_refresh_timeout_cb, ctx );
178         ritem->refresh_id = source_id;
179         ctx->id = source_id;
180
181         debug_print("RSSyl: start_refresh_timeout - %d min (id %d)\n",
182                         ritem->refresh_interval, ctx->id);
183 }