Fixes Coverity CIDs: 1220388 1372362 1220222 1358399 1223756 1223757 1220305 1220269...
[claws.git] / src / plugins / rssyl / opml_export.c
1 /*
2  * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3  * This file (C) 2008 Andrej Kacian <andrej@kacian.sk>
4  *
5  * - Export feed list to OPML
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 /* Global includes */
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <errno.h>
30
31 /* Claws Mail includes */
32 #include <log.h>
33 #include <folder.h>
34 #include <common/utils.h>
35
36 /* Local includes */
37 #include "libfeed/date.h"
38 #include "rssyl.h"
39 #include "opml_import.h"
40 #include "strutils.h"
41
42 #define RSSYL_OPML_FILE "rssyl-feedlist.opml"
43
44 struct _RSSylOpmlCtx {
45         FILE *f;
46         gint depth;
47 };
48
49 typedef struct _RSSylOpmlCtx RSSylOpmlCtx;
50
51 static void rssyl_opml_export_func(FolderItem *item, gpointer data)
52 {
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;
59         gint depth;
60
61         if( !IS_RSSYL_FOLDER_ITEM(item) )
62                 return;
63
64         if( folder_item_parent(item) == NULL )
65                 return;
66
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);
73                         g_free(indent);
74                 }
75         }
76         ctx->depth = depth;
77
78         if( ritem->url == NULL ) {
79                 isfolder = TRUE;
80         } else {
81                 tmpurl = rssyl_strreplace(ritem->url, "&", "&amp;");
82                 xmlurl = g_strdup_printf("xmlUrl=\"%s\"", tmpurl);
83                 g_free(tmpurl);
84         }
85
86         if( g_node_n_children(item->node) )
87                 haschildren = TRUE;
88
89         indent = g_strnfill(ctx->depth + 1, '\t');
90
91         tmpname = rssyl_strreplace(item->name, "&", "&amp;");
92          if( ritem->official_title != NULL )
93                  tmpoffn = rssyl_strreplace(ritem->official_title, "&", "&amp;");
94          else
95                  tmpoffn = g_strdup(tmpname);
96
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);
102
103         g_free(indent);
104         g_free(xmlurl);
105         g_free(tmpname);
106         g_free(tmpoffn);
107         
108         if( err ) {
109                 log_warning(LOG_PROTOCOL,
110                                 _("RSSyl: Error while writing '%s' to feed export list.\n"),
111                                 item->name);
112                 debug_print("Error while writing '%s' to feed_export list.\n",
113                                 item->name);
114         }
115 }
116
117 void rssyl_opml_export(void)
118 {
119         FILE *f;
120         gchar *opmlfile, *tmp;
121         time_t tt = time(NULL);
122         RSSylOpmlCtx *ctx = NULL;
123         gboolean err = FALSE;
124
125         opmlfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
126                         G_DIR_SEPARATOR_S, RSSYL_OPML_FILE, NULL);
127
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);
134                         g_free(opmlfile);
135                         return;
136                 }
137         }
138         
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");
144                 g_free(opmlfile);
145                 return;
146         }
147
148         tmp = createRFC822Date(&tt);
149
150         /* Write OPML header */
151         err |= (fprintf(f,
152                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
153                                 "<opml version=\"1.1\">\n"
154                                 "\t<head>\n"
155                                 "\t\t<title>RSSyl Feed List Export</title>\n"
156                                 "\t\t<dateCreated>%s</dateCreated>\n"
157                                 "\t</head>\n"
158                                 "\t<body>\n",
159                                 tmp) < 0);
160         g_free(tmp);
161
162         ctx = g_new0(RSSylOpmlCtx, 1);
163         ctx->f = f;
164         ctx->depth = 1;
165
166         folder_func_to_all_folders(
167                         (FolderItemFunc)rssyl_opml_export_func, ctx);
168
169         /* Print close all open <outline> tags if needed. */
170         while( ctx->depth > 2 ) {
171                 ctx->depth--;
172                 tmp = g_strnfill(ctx->depth, '\t');
173                 err |= (fprintf(f, "%s</outline>\n", tmp) < 0);
174                 g_free(tmp);
175         }
176
177         err |= (fprintf(f,
178                                 "\t</body>\n"
179                                 "</opml>\n") < 0);
180
181         if( err ) {
182                 log_warning(LOG_PROTOCOL, _("RSSyl: Error during writing feed export file.\n"));
183                 debug_print("RSSyl: Error during writing feed export file.\n");
184         }
185
186         debug_print("RSSyl: Feed export finished.\n");
187
188         fclose(f);
189         g_free(opmlfile);
190         g_free(ctx);
191 }