2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * This file (C) 2008 Andrej Kacian <andrej@kacian.sk>
5 * - Export feed list to OPML
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
31 /* Claws Mail includes */
34 #include <common/utils.h>
35 #include <file-utils.h>
38 #include "libfeed/date.h"
40 #include "opml_import.h"
43 #define RSSYL_OPML_FILE "rssyl-feedlist.opml"
45 struct _RSSylOpmlCtx {
50 typedef struct _RSSylOpmlCtx RSSylOpmlCtx;
52 static void rssyl_opml_export_func(FolderItem *item, gpointer data)
54 RSSylOpmlCtx *ctx = (RSSylOpmlCtx *)data;
55 RFolderItem *ritem = (RFolderItem *)item;
56 gboolean isfolder = FALSE, err = FALSE;
57 gboolean haschildren = FALSE;
58 gchar *indent = NULL, *xmlurl = NULL;
59 gchar *tmpoffn = NULL, *tmpurl = NULL, *tmpname = NULL;
62 if( !IS_RSSYL_FOLDER_ITEM(item) )
65 if( folder_item_parent(item) == NULL )
68 /* Check for depth and adjust indentation */
69 depth = rssyl_folder_depth(item);
70 if( depth < ctx->depth ) {
71 for( ctx->depth--; depth <= ctx->depth; ctx->depth-- ) {
72 indent = g_strnfill(ctx->depth + 1, '\t');
73 err |= (fprintf(ctx->f, "%s</outline>\n", indent) < 0);
79 if( ritem->url == NULL ) {
82 tmpurl = rssyl_strreplace(ritem->url, "&", "&");
83 xmlurl = g_strdup_printf("xmlUrl=\"%s\"", tmpurl);
87 if( g_node_n_children(item->node) )
90 indent = g_strnfill(ctx->depth + 1, '\t');
92 tmpname = rssyl_strreplace(item->name, "&", "&");
93 if( ritem->official_title != NULL )
94 tmpoffn = rssyl_strreplace(ritem->official_title, "&", "&");
96 tmpoffn = g_strdup(tmpname);
98 err |= (fprintf(ctx->f,
99 "%s<outline title=\"%s\" text=\"%s\" description=\"%s\" type=\"%s\" %s%s>\n",
100 indent, tmpname, tmpoffn, tmpoffn,
101 (isfolder ? "folder" : "rss"),
102 (xmlurl ? xmlurl : ""), (haschildren ? "" : "/")) < 0);
110 log_warning(LOG_PROTOCOL,
111 _("RSSyl: Error while writing '%s' to feed export list.\n"),
113 debug_print("Error while writing '%s' to feed_export list.\n",
118 void rssyl_opml_export(void)
121 gchar *opmlfile, *tmp;
122 time_t tt = time(NULL);
123 RSSylOpmlCtx *ctx = NULL;
124 gboolean err = FALSE;
126 opmlfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
127 G_DIR_SEPARATOR_S, RSSYL_OPML_FILE, NULL);
129 if( g_file_test(opmlfile, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR ) ) {
130 if (g_remove(opmlfile) != 0) {
131 log_warning(LOG_PROTOCOL,
132 _("RSSyl: Couldn't delete old OPML file '%s': %s\n"),
133 opmlfile, g_strerror(errno));
134 debug_print("RSSyl: Couldn't delete old file '%s'\n", opmlfile);
140 if( (f = claws_fopen(opmlfile, "w")) == NULL ) {
141 log_warning(LOG_PROTOCOL,
142 _("RSSyl: Couldn't open file '%s' for feed list exporting: %s\n"),
143 opmlfile, g_strerror(errno));
144 debug_print("RSSyl: Couldn't open feed list export file, returning.\n");
149 tmp = createRFC822Date(&tt);
151 /* Write OPML header */
153 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
154 "<opml version=\"1.1\">\n"
156 "\t\t<title>RSSyl Feed List Export</title>\n"
157 "\t\t<dateCreated>%s</dateCreated>\n"
163 ctx = g_new0(RSSylOpmlCtx, 1);
167 folder_func_to_all_folders(
168 (FolderItemFunc)rssyl_opml_export_func, ctx);
170 /* Print close all open <outline> tags if needed. */
171 while( ctx->depth > 2 ) {
173 tmp = g_strnfill(ctx->depth, '\t');
174 err |= (fprintf(f, "%s</outline>\n", tmp) < 0);
183 log_warning(LOG_PROTOCOL, _("RSSyl: Error during writing feed export file.\n"));
184 debug_print("RSSyl: Error during writing feed export file.\n");
187 debug_print("RSSyl: Feed export finished.\n");
189 claws_safe_fclose(f);