2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2008-2023 the Claws Mail Team and Andrej Kacian <andrej@kacian.sk>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
28 /* Claws Mail includes */
31 #include <common/utils.h>
32 #include <file-utils.h>
35 #include "libfeed/date.h"
37 #include "opml_import.h"
40 #define RSSYL_OPML_FILE "rssyl-feedlist.opml"
42 struct _RSSylOpmlCtx {
47 typedef struct _RSSylOpmlCtx RSSylOpmlCtx;
49 static void rssyl_opml_export_func(FolderItem *item, gpointer data)
51 RSSylOpmlCtx *ctx = (RSSylOpmlCtx *)data;
52 RFolderItem *ritem = (RFolderItem *)item;
53 gboolean isfolder = FALSE, err = FALSE;
54 gboolean haschildren = FALSE;
55 gchar *indent = NULL, *xmlurl = NULL;
56 gchar *tmpoffn = NULL, *tmpurl = NULL, *tmpname = NULL;
59 if( !IS_RSSYL_FOLDER_ITEM(item) )
62 if( folder_item_parent(item) == NULL )
65 /* Check for depth and adjust indentation */
66 depth = rssyl_folder_depth(item);
67 if( depth < ctx->depth ) {
68 for( ctx->depth--; depth <= ctx->depth; ctx->depth-- ) {
69 indent = g_strnfill(ctx->depth + 1, '\t');
70 err |= (fprintf(ctx->f, "%s</outline>\n", indent) < 0);
76 if( ritem->url == NULL ) {
79 tmpurl = rssyl_strreplace(ritem->url, "&", "&");
80 xmlurl = g_strdup_printf("xmlUrl=\"%s\"", tmpurl);
84 if( g_node_n_children(item->node) )
87 indent = g_strnfill(ctx->depth + 1, '\t');
89 tmpname = rssyl_strreplace(item->name, "&", "&");
90 if( ritem->official_title != NULL )
91 tmpoffn = rssyl_strreplace(ritem->official_title, "&", "&");
93 tmpoffn = g_strdup(tmpname);
95 err |= (fprintf(ctx->f,
96 "%s<outline title=\"%s\" text=\"%s\" description=\"%s\" type=\"%s\" %s%s>\n",
97 indent, tmpname, tmpoffn, tmpoffn,
98 (isfolder ? "folder" : "rss"),
99 (xmlurl ? xmlurl : ""), (haschildren ? "" : "/")) < 0);
107 log_warning(LOG_PROTOCOL,
108 _("RSSyl: Error while writing '%s' to feed export list.\n"),
110 debug_print("Error while writing '%s' to feed_export list.\n",
115 void rssyl_opml_export(void)
118 gchar *opmlfile, *tmp;
119 time_t tt = time(NULL);
120 RSSylOpmlCtx *ctx = NULL;
121 gboolean err = FALSE;
123 opmlfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
124 G_DIR_SEPARATOR_S, RSSYL_OPML_FILE, NULL);
126 if( g_file_test(opmlfile, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR ) ) {
127 if (g_remove(opmlfile) != 0) {
128 log_warning(LOG_PROTOCOL,
129 _("RSSyl: Couldn't delete old OPML file '%s': %s\n"),
130 opmlfile, g_strerror(errno));
131 debug_print("RSSyl: Couldn't delete old file '%s'\n", opmlfile);
137 if( (f = claws_fopen(opmlfile, "w")) == NULL ) {
138 log_warning(LOG_PROTOCOL,
139 _("RSSyl: Couldn't open file '%s' for feed list exporting: %s\n"),
140 opmlfile, g_strerror(errno));
141 debug_print("RSSyl: Couldn't open feed list export file, returning.\n");
146 tmp = createRFC822Date(&tt);
148 /* Write OPML header */
150 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
151 "<opml version=\"1.1\">\n"
153 "\t\t<title>RSSyl Feed List Export</title>\n"
154 "\t\t<dateCreated>%s</dateCreated>\n"
160 ctx = g_new0(RSSylOpmlCtx, 1);
164 folder_func_to_all_folders(
165 (FolderItemFunc)rssyl_opml_export_func, ctx);
167 /* Print close all open <outline> tags if needed. */
168 while( ctx->depth > 2 ) {
170 tmp = g_strnfill(ctx->depth, '\t');
171 err |= (fprintf(f, "%s</outline>\n", tmp) < 0);
180 log_warning(LOG_PROTOCOL, _("RSSyl: Error during writing feed export file.\n"));
181 debug_print("RSSyl: Error during writing feed export file.\n");
184 debug_print("RSSyl: Feed export finished.\n");
186 claws_safe_fclose(f);