Don't forget the ssl_verify_peer pref when migrating
[claws.git] / src / plugins / rssyl / old_feeds.c
1 /*
2  * Copyright (C) 2012 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 /* Expat parser for old feeds.xml */
21
22 #include <glib/gi18n.h>
23 #include <gtk/gtk.h>
24 #include <expat.h>
25
26 #include <alertpanel.h>
27 #include <common/utils.h>
28
29 #include "libfeed/parser.h"
30 #include "old_feeds.h"
31 #include "rssyl.h"
32
33 struct _oldrssyl_ctx {
34         GSList *oldfeeds;
35 };
36
37 static void _elparse_start_oldrssyl(void *data, const gchar *el,
38                 const gchar **attr)
39 {
40         struct _oldrssyl_ctx *ctx = data;
41         OldRFeed *of;
42         gchar *tmp;
43
44 #define GETVAL_STR(name) (g_strdup(feed_parser_get_attribute_value(attr, name)))
45 #define GETVAL_INT(name) \
46         ((tmp = feed_parser_get_attribute_value(attr, name)) == NULL ? 0 : \
47                 (gint)atoi(tmp))
48
49         if (!strcmp(el, "feed")) {
50                 of = g_new0(OldRFeed, 1);
51
52                 of->name = GETVAL_STR("name");
53                 of->official_name = GETVAL_STR("official_name");
54                 of->url = GETVAL_STR("url");
55                 of->default_refresh_interval = GETVAL_INT("default_refresh_interval");
56                 of->refresh_interval = GETVAL_INT("refresh_interval");
57                 of->expired_num = GETVAL_INT("expired_num");
58                 of->fetch_comments = GETVAL_INT("fetch_comments");
59                 of->fetch_comments_for = GETVAL_INT("fetch_comments_for");
60                 of->silent_update = GETVAL_INT("silent_update");
61                 of->ssl_verify_peer = GETVAL_INT("ssl_verify_peer");
62
63                 debug_print("RSSyl: old feeds.xml: Adding '%s' (%s).\n", of->name,
64                                 of->url);
65
66                 ctx->oldfeeds = g_slist_prepend(ctx->oldfeeds, of);
67         }
68
69         return;
70 }
71
72 static void _elparse_end_oldrssyl(void *data, const gchar *el)
73 {
74         return;
75 }
76
77 GSList *rssyl_old_feed_metadata_parse(gchar *filepath)
78 {
79         XML_Parser parser;
80         GSList *oldfeeds = NULL;
81         gchar *contents = NULL;
82         gsize length;
83         GError *error;
84         struct _oldrssyl_ctx *ctx;
85
86         debug_print("RSSyl: Starting to parse old feeds.xml\n");
87
88         /* Read contents of the file into memory */
89         if (!g_file_get_contents(filepath, &contents, &length, &error)) {
90                 alertpanel_error(_("Couldn't read contents of old feeds.xml file:\n%s"),
91                                 error->message);
92                 debug_print("RSSyl: Couldn't read contents of feeds.xml\n");
93                 g_error_free(error);
94                 return NULL;
95         }
96
97         /* Set up expat parser */
98         parser = XML_ParserCreate(NULL);
99
100         ctx = g_new0(struct _oldrssyl_ctx, 1);
101         ctx->oldfeeds = NULL;
102         XML_SetUserData(parser, ctx);
103         XML_SetElementHandler(parser,
104                         _elparse_start_oldrssyl,
105                         _elparse_end_oldrssyl);
106
107         /* Parse the XML, our output ending up in oldfeeds */
108         XML_Parse(parser, contents, length, 1);
109
110         /* And clean up */
111         XML_ParserFree(parser);
112         g_free(contents);
113         oldfeeds = ctx->oldfeeds;
114         g_free(ctx);
115
116         debug_print("RSSyl: old feeds.xml: added %d items in total\n",
117                         g_slist_length(oldfeeds));
118
119         return oldfeeds;
120 }
121
122 static void _free_old_feed_entry(gpointer d, gpointer user_data)
123 {
124         OldRFeed *of = (OldRFeed *)d;
125
126         if (of == NULL)
127                 return;
128
129         g_free(of->name);
130         g_free(of->official_name);
131         g_free(of->url);
132         g_free(of);
133 }
134
135 void rssyl_old_feed_metadata_free(GSList *oldfeeds)
136 {
137         if (oldfeeds != NULL) {
138                 debug_print("RSSyl: releasing parsed contents of old feeds.xml\n");
139                 g_slist_foreach(oldfeeds, _free_old_feed_entry, NULL);
140                 g_slist_free(oldfeeds);
141                 oldfeeds = NULL;
142         }
143 }
144
145 static gint _old_feed_find_by_url(gconstpointer a, gconstpointer b)
146 {
147         OldRFeed *of = (OldRFeed *)a;
148         gchar *name = (gchar *)b;
149
150         if (of == NULL || of->name == NULL || of->url == NULL || name == NULL)
151                 return 1;
152
153         return strcmp(of->name, name);
154 }
155
156 OldRFeed *rssyl_old_feed_get_by_name(GSList *oldfeeds, gchar *name)
157 {
158         GSList *needle;
159
160         g_return_val_if_fail(oldfeeds != NULL, NULL);
161         g_return_val_if_fail(name != NULL, NULL);
162
163         if ((needle = g_slist_find_custom(oldfeeds, name, _old_feed_find_by_url))
164                         != NULL)
165                 return (OldRFeed *)needle->data;
166         
167         return NULL;
168 }