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