2 * Copyright (C) 2012 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.
20 /* Expat parser for old feeds.xml */
22 #include <glib/gi18n.h>
26 #include <alertpanel.h>
27 #include <common/utils.h>
29 #include "libfeed/parser.h"
30 #include "old_feeds.h"
33 struct _oldrssyl_ctx {
37 static void _elparse_start_oldrssyl(void *data, const gchar *el,
40 struct _oldrssyl_ctx *ctx = data;
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 : \
49 if (!strcmp(el, "feed")) {
50 of = g_new0(OldRFeed, 1);
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");
63 debug_print("RSSyl: old feeds.xml: Adding '%s' (%s).\n", of->name,
66 ctx->oldfeeds = g_slist_prepend(ctx->oldfeeds, of);
72 static void _elparse_end_oldrssyl(void *data, const gchar *el)
77 GSList *rssyl_old_feed_metadata_parse(gchar *filepath)
80 GSList *oldfeeds = NULL;
81 gchar *contents = NULL;
84 struct _oldrssyl_ctx *ctx;
86 debug_print("RSSyl: Starting to parse old feeds.xml\n");
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"),
92 debug_print("RSSyl: Couldn't read contents of feeds.xml\n");
97 /* Set up expat parser */
98 parser = XML_ParserCreate(NULL);
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);
107 /* Parse the XML, our output ending up in oldfeeds */
108 XML_Parse(parser, contents, length, 1);
111 XML_ParserFree(parser);
113 oldfeeds = ctx->oldfeeds;
116 debug_print("RSSyl: old feeds.xml: added %d items in total\n",
117 g_slist_length(oldfeeds));
122 static void _free_old_feed_entry(gpointer d, gpointer user_data)
124 OldRFeed *of = (OldRFeed *)d;
130 g_free(of->official_name);
135 void rssyl_old_feed_metadata_free(GSList *oldfeeds)
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);
145 static gint _old_feed_find_by_url(gconstpointer a, gconstpointer b)
147 OldRFeed *of = (OldRFeed *)a;
148 gchar *name = (gchar *)b;
150 if (of == NULL || of->name == NULL || of->url == NULL || name == NULL)
153 return strcmp(of->name, name);
156 OldRFeed *rssyl_old_feed_get_by_name(GSList *oldfeeds, gchar *name)
160 g_return_val_if_fail(oldfeeds != NULL, NULL);
161 g_return_val_if_fail(name != NULL, NULL);
163 if ((needle = g_slist_find_custom(oldfeeds, name, _old_feed_find_by_url))
165 return (OldRFeed *)needle->data;