New RSSyl replacing old one.
[claws.git] / src / plugins / rssyl / opml_import.c
1 /*
2  * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
5  *
6  * - OPML import handling
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib.h>
29 #include <glib/gi18n.h>
30
31 /* Claws Mail includes */
32 #include <folder.h>
33 #include <alertpanel.h>
34 #include <common/utils.h>
35
36 /* Local includes */
37 #include "rssyl_feed.h"
38 #include "opml_import.h"
39
40 gint rssyl_folder_depth(FolderItem *item)
41 {
42         gint i;
43
44         for( i = -1; item != NULL; item = folder_item_parent(item), i++ ) {}
45         return i;
46 }
47
48 /* This gets called from the libfeed's OPML parser as a user function for
49  * each <outline ...> from the .opml file.
50  * It creates a folder or subscribes a feed, while keeping track of the
51  * location inside folder hierarchy (using depth and linked list of parent
52  * folders up to the root). */
53 void rssyl_opml_import_func(gchar *title, gchar *url, gint depth, gpointer data)
54 {
55         OPMLImportCtx *ctx = (OPMLImportCtx *)data;
56         gchar *tmp = NULL;
57         FolderItem *new_item;
58         gboolean nulltitle = FALSE;
59         gint i = 1;
60
61         debug_print("depth %d, ctx->depth %d\n", depth, ctx->depth);
62         while (depth < ctx->depth) {
63                 /* We've gone up at least one level, need to find correct parent */
64                 ctx->current = g_slist_delete_link(ctx->current, ctx->current);
65                 ctx->depth--;
66         }
67
68         debug_print("OPML_IMPORT: %s %s (%s)\n",
69                         (url != NULL ? "feed": "folder"), title, url);
70
71         if( title == NULL ) {
72                 debug_print("NULL title received, substituting a placeholder title\n");
73                 title = g_strdup(_("Untitled"));
74                 nulltitle = TRUE;
75         }
76
77         /* If URL is not given, then it's a folder */
78         if( url == NULL ) {
79                 /* Find an unused name for new folder */
80                 tmp = g_strdup(title);
81                 while (folder_find_child_item_by_name((FolderItem *)ctx->current->data, tmp)) {
82                         debug_print("RSSyl: Folder '%s' already exists, trying another name\n",
83                                         title);
84                         g_free(tmp);
85                         tmp = g_strdup_printf("%s__%d", title, ++i);
86                 }
87
88                 /* Create the folder */
89                 new_item = folder_create_folder((FolderItem *)ctx->current->data, tmp);
90                 if (!new_item) {
91                         alertpanel_error(_("Can't create the folder '%s'."), tmp);
92                         g_free(tmp);
93                 }
94
95                 if (nulltitle) {
96                         g_free(title);
97                         title = NULL;
98                 }
99
100                 ctx->current = g_slist_prepend(ctx->current, new_item);
101                 ctx->depth++;
102         } else {
103                 /* We have URL, try to add new feed... */
104                 new_item = rssyl_feed_subscribe_new((FolderItem *)ctx->current->data,
105                                 url, TRUE);
106                 /* ...and rename it if needed */
107                 if (new_item != NULL && strcmp(title, new_item->name))
108                         folder_item_rename(new_item, title);
109         }
110 }