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>
37 #include "libfeed/date.h"
39 #include "opml_import.h"
42 #define RSSYL_OPML_FILE "rssyl-feedlist.opml"
44 struct _RSSylOpmlCtx {
49 typedef struct _RSSylOpmlCtx RSSylOpmlCtx;
51 static void rssyl_opml_export_func(FolderItem *item, gpointer data)
53 RSSylOpmlCtx *ctx = (RSSylOpmlCtx *)data;
54 RFolderItem *ritem = (RFolderItem *)item;
55 gboolean isfolder = FALSE, err = FALSE;
56 gboolean haschildren = FALSE;
57 gchar *indent = NULL, *xmlurl = NULL;
58 gchar *tmpoffn = NULL, *tmpurl = NULL, *tmpname = NULL;
61 if( !IS_RSSYL_FOLDER_ITEM(item) )
64 if( folder_item_parent(item) == NULL )
67 /* Check for depth and adjust indentation */
68 depth = rssyl_folder_depth(item);
69 if( depth < ctx->depth ) {
70 for( ctx->depth--; depth <= ctx->depth; ctx->depth-- ) {
71 indent = g_strnfill(ctx->depth + 1, '\t');
72 err |= (fprintf(ctx->f, "%s</outline>\n", indent) < 0);
78 if( ritem->url == NULL ) {
81 tmpurl = rssyl_strreplace(ritem->url, "&", "&");
82 xmlurl = g_strdup_printf("xmlUrl=\"%s\"", tmpurl);
86 if( g_node_n_children(item->node) )
89 indent = g_strnfill(ctx->depth + 1, '\t');
91 tmpname = rssyl_strreplace(item->name, "&", "&");
92 if( ritem->official_title != NULL )
93 tmpoffn = rssyl_strreplace(ritem->official_title, "&", "&");
95 tmpoffn = g_strdup(tmpname);
97 err |= (fprintf(ctx->f,
98 "%s<outline title=\"%s\" text=\"%s\" description=\"%s\" type=\"%s\" %s%s>\n",
99 indent, tmpname, tmpoffn, tmpoffn,
100 (isfolder ? "folder" : "rss"),
101 (xmlurl ? xmlurl : ""), (haschildren ? "" : "/")) < 0);
109 log_warning(LOG_PROTOCOL,
110 _("RSSyl: Error while writing '%s' to feed export list.\n"),
112 debug_print("Error while writing '%s' to feed_export list.\n",
117 void rssyl_opml_export(void)
120 gchar *opmlfile, *tmp;
121 time_t tt = time(NULL);
122 RSSylOpmlCtx *ctx = NULL;
123 gboolean err = FALSE;
125 opmlfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
126 G_DIR_SEPARATOR_S, RSSYL_OPML_FILE, NULL);
128 if( g_file_test(opmlfile, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR ) ) {
129 if (g_remove(opmlfile) != 0) {
130 log_warning(LOG_PROTOCOL,
131 _("RSSyl: Couldn't delete old OPML file '%s': %s\n"),
132 opmlfile, g_strerror(errno));
133 debug_print("RSSyl: couldn't delete old file '%s'\n", opmlfile);
139 if( (f = g_fopen(opmlfile, "w")) == NULL ) {
140 log_warning(LOG_PROTOCOL,
141 _("RSSyl: Couldn't open file '%s' for feed list exporting: %s\n"),
142 opmlfile, g_strerror(errno));
143 debug_print("RSSyl: Couldn't open feed list export file, returning.\n");
148 tmp = createRFC822Date(&tt);
150 /* Write OPML header */
152 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
153 "<opml version=\"1.1\">\n"
155 "\t\t<title>RSSyl Feed List Export</title>\n"
156 "\t\t<dateCreated>%s</dateCreated>\n"
162 ctx = g_new0(RSSylOpmlCtx, 1);
166 folder_func_to_all_folders(
167 (FolderItemFunc)rssyl_opml_export_func, ctx);
169 /* Print close all open <outline> tags if needed. */
170 while( ctx->depth > 2 ) {
172 tmp = g_strnfill(ctx->depth, '\t');
173 err |= (fprintf(f, "%s</outline>\n", tmp) < 0);
182 log_warning(LOG_PROTOCOL, _("RSSyl: Error during writing feed export file.\n"));
183 debug_print("RSSyl: Error during writing feed export file.\n");
186 debug_print("RSSyl: Feed export finished.\n");