Merge branch 'master' of ssh://git.claws-mail.org/home/git/claws
[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 #include <file-utils.h>
36
37 /* Local includes */
38 #include "libfeed/date.h"
39 #include "rssyl.h"
40 #include "opml_import.h"
41 #include "strutils.h"
42
43 #define RSSYL_OPML_FILE "rssyl-feedlist.opml"
44
45 struct _RSSylOpmlCtx {
46         FILE *f;
47         gint depth;
48 };
49
50 typedef struct _RSSylOpmlCtx RSSylOpmlCtx;
51
52 static void rssyl_opml_export_func(FolderItem *item, gpointer data)
53 {
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;
60         gint depth;
61
62         if( !IS_RSSYL_FOLDER_ITEM(item) )
63                 return;
64
65         if( folder_item_parent(item) == NULL )
66                 return;
67
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);
74                         g_free(indent);
75                 }
76         }
77         ctx->depth = depth;
78
79         if( ritem->url == NULL ) {
80                 isfolder = TRUE;
81         } else {
82                 tmpurl = rssyl_strreplace(ritem->url, "&", "&amp;");
83                 xmlurl = g_strdup_printf("xmlUrl=\"%s\"", tmpurl);
84                 g_free(tmpurl);
85         }
86
87         if( g_node_n_children(item->node) )
88                 haschildren = TRUE;
89
90         indent = g_strnfill(ctx->depth + 1, '\t');
91
92         tmpname = rssyl_strreplace(item->name, "&", "&amp;");
93          if( ritem->official_title != NULL )
94                  tmpoffn = rssyl_strreplace(ritem->official_title, "&", "&amp;");
95          else
96                  tmpoffn = g_strdup(tmpname);
97
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);
103
104         g_free(indent);
105         g_free(xmlurl);
106         g_free(tmpname);
107         g_free(tmpoffn);
108         
109         if( err ) {
110                 log_warning(LOG_PROTOCOL,
111                                 _("RSSyl: Error while writing '%s' to feed export list.\n"),
112                                 item->name);
113                 debug_print("Error while writing '%s' to feed_export list.\n",
114                                 item->name);
115         }
116 }
117
118 void rssyl_opml_export(void)
119 {
120         FILE *f;
121         gchar *opmlfile, *tmp;
122         time_t tt = time(NULL);
123         RSSylOpmlCtx *ctx = NULL;
124         gboolean err = FALSE;
125
126         opmlfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
127                         G_DIR_SEPARATOR_S, RSSYL_OPML_FILE, NULL);
128
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);
135                         g_free(opmlfile);
136                         return;
137                 }
138         }
139         
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");
145                 g_free(opmlfile);
146                 return;
147         }
148
149         tmp = createRFC822Date(&tt);
150
151         /* Write OPML header */
152         err |= (fprintf(f,
153                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
154                                 "<opml version=\"1.1\">\n"
155                                 "\t<head>\n"
156                                 "\t\t<title>RSSyl Feed List Export</title>\n"
157                                 "\t\t<dateCreated>%s</dateCreated>\n"
158                                 "\t</head>\n"
159                                 "\t<body>\n",
160                                 tmp) < 0);
161         g_free(tmp);
162
163         ctx = g_new0(RSSylOpmlCtx, 1);
164         ctx->f = f;
165         ctx->depth = 1;
166
167         folder_func_to_all_folders(
168                         (FolderItemFunc)rssyl_opml_export_func, ctx);
169
170         /* Print close all open <outline> tags if needed. */
171         while( ctx->depth > 2 ) {
172                 ctx->depth--;
173                 tmp = g_strnfill(ctx->depth, '\t');
174                 err |= (fprintf(f, "%s</outline>\n", tmp) < 0);
175                 g_free(tmp);
176         }
177
178         err |= (fprintf(f,
179                                 "\t</body>\n"
180                                 "</opml>\n") < 0);
181
182         if( err ) {
183                 log_warning(LOG_PROTOCOL, _("RSSyl: Error during writing feed export file.\n"));
184                 debug_print("RSSyl: Error during writing feed export file.\n");
185         }
186
187         debug_print("RSSyl: Feed export finished.\n");
188
189         claws_safe_fclose(f);
190         g_free(opmlfile);
191         g_free(ctx);
192 }