2011-10-22 [colin] 3.7.10cvs42
[claws.git] / src / import.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtk.h>
30
31 #include "claws.h"
32 #include "main.h"
33 #include "inc.h"
34 #include "mbox.h"
35 #include "folderview.h"
36 #include "filesel.h"
37 #include "foldersel.h"
38 #include "gtkutils.h"
39 #include "manage_window.h"
40 #include "folder.h"
41 #include "codeconv.h"
42 #include "alertpanel.h"
43
44 static GtkWidget *window;
45 static GtkWidget *file_entry;
46 static GtkWidget *dest_entry;
47 static GtkWidget *file_button;
48 static GtkWidget *dest_button;
49 static GtkWidget *ok_button;
50 static GtkWidget *cancel_button;
51 static gboolean import_ok; /* see import_mbox() return values */
52
53 static void import_create(void);
54 static void import_ok_cb(GtkWidget *widget, gpointer data);
55 static void import_cancel_cb(GtkWidget *widget, gpointer data);
56 static void import_filesel_cb(GtkWidget *widget, gpointer data);
57 static void import_destsel_cb(GtkWidget *widget, gpointer data);
58 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
59 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
60
61 gint import_mbox(FolderItem *default_dest)
62 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
63 {
64         gchar *dest_id = NULL;
65
66         import_ok = -2; // skipped or cancelled
67
68         if (!window) {
69                 import_create();
70         }
71         else {
72                 gtk_widget_show(window);
73         }
74
75         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
76         change_dir(claws_get_startup_dir());
77
78         if (default_dest && default_dest->path) {
79                 dest_id = folder_item_get_identifier(default_dest);
80         }
81
82         if (dest_id) {
83                 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
84                 g_free(dest_id);
85         } else {
86                 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
87         }
88         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
89         gtk_widget_grab_focus(file_entry);
90
91         manage_window_set_transient(GTK_WINDOW(window));
92
93         gtk_main();
94
95         gtk_widget_hide(window);
96         gtk_window_set_modal(GTK_WINDOW(window), FALSE);
97
98         return import_ok;
99 }
100
101 static void import_create(void)
102 {
103         GtkWidget *vbox;
104         GtkWidget *hbox;
105         GtkWidget *desc_label;
106         GtkWidget *table;
107         GtkWidget *file_label;
108         GtkWidget *dest_label;
109         GtkWidget *confirm_area;
110
111         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "import");
112         gtk_window_set_title(GTK_WINDOW(window), _("Import mbox file"));
113         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
114         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
115         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
116         g_signal_connect(G_OBJECT(window), "delete_event",
117                          G_CALLBACK(delete_event), NULL);
118         g_signal_connect(G_OBJECT(window), "key_press_event",
119                          G_CALLBACK(key_pressed), NULL);
120         MANAGE_WINDOW_SIGNALS_CONNECT(window);
121
122         vbox = gtk_vbox_new(FALSE, 4);
123         gtk_container_add(GTK_CONTAINER(window), vbox);
124
125         hbox = gtk_hbox_new(FALSE, 0);
126         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
127         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
128
129         desc_label = gtk_label_new
130                 (_("Locate the mbox file and specify the destination folder."));
131         gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
132         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
133
134         table = gtk_table_new(2, 3, FALSE);
135         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
136         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
137         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
138         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
139         gtk_widget_set_size_request(table, 420, -1);
140
141         file_label = gtk_label_new(_("Mbox file:"));
142         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
143                          (GtkAttachOptions) (GTK_FILL),
144                          (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
145         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
146
147         dest_label = gtk_label_new(_("Destination folder:"));
148         gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
149                          (GtkAttachOptions) (GTK_FILL),
150                          (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
151         gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
152
153         file_entry = gtk_entry_new();
154         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
155                          (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
156                          (GtkAttachOptions) (0), 0, 0);
157
158         dest_entry = gtk_entry_new();
159         gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
160                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
161
162         file_button = gtkut_get_browse_file_btn(_("_Browse"));
163         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
164                          (GtkAttachOptions) (GTK_FILL),
165                          (GtkAttachOptions) (0), 0, 0);
166         g_signal_connect(G_OBJECT(file_button), "clicked",
167                          G_CALLBACK(import_filesel_cb), NULL);
168
169         dest_button = gtkut_get_browse_directory_btn(_("B_rowse"));
170         gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
171                          (GtkAttachOptions) (GTK_FILL),
172                          (GtkAttachOptions) (0), 0, 0);
173         g_signal_connect(G_OBJECT(dest_button), "clicked",
174                          G_CALLBACK(import_destsel_cb), NULL);
175
176         gtkut_stock_button_set_create(&confirm_area,
177                                       &cancel_button, GTK_STOCK_CANCEL,
178                                       &ok_button, GTK_STOCK_OK,
179                                       NULL, NULL);
180         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
181         gtk_widget_grab_default(ok_button);
182
183         g_signal_connect(G_OBJECT(ok_button), "clicked",
184                          G_CALLBACK(import_ok_cb), NULL);
185         g_signal_connect(G_OBJECT(cancel_button), "clicked",
186                          G_CALLBACK(import_cancel_cb), NULL);
187
188         gtk_widget_show_all(window);
189 }
190
191 static void import_ok_cb(GtkWidget *widget, gpointer data)
192 {
193         const gchar *utf8mbox, *destdir;
194         FolderItem *dest;
195         gchar *mbox;
196
197         utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
198         destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
199
200         if (utf8mbox && !*utf8mbox) {
201                 alertpanel_error(_("Source mbox filename can't be left empty."));
202                 gtk_widget_grab_focus(file_entry);
203                 return;
204         }
205         if (destdir && !*destdir) {
206                 if (alertpanel(_("Import mbox file"), _("Destination folder is not set.\nImport mbox file to the Inbox folder?"),
207                                                 GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
208                         == G_ALERTALTERNATE) {
209                         gtk_widget_grab_focus(dest_entry);
210                         return;
211                 }
212         }
213
214         mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
215         if (!mbox) {
216                 g_warning("import_ok_cb(): failed to convert character set.\n");
217                 mbox = g_strdup(utf8mbox);
218         }
219
220         if (!destdir || !*destdir) {
221                 dest = folder_find_item_from_path(INBOX_DIR);
222         } else {
223                 dest = folder_find_item_from_identifier
224                         (destdir);
225         }
226
227         if (!dest) {
228                 alertpanel_error(_("Can't find the destination folder."));
229                 gtk_widget_grab_focus(dest_entry);
230                 g_free(mbox);
231                 return;
232         } else {
233                 import_ok = proc_mbox(dest, mbox, FALSE, NULL);
234         }
235
236         g_free(mbox);
237
238         if (gtk_main_level() > 1)
239                 gtk_main_quit();
240 }
241
242 static void import_cancel_cb(GtkWidget *widget, gpointer data)
243 {
244         if (gtk_main_level() > 1)
245                 gtk_main_quit();
246 }
247
248 static void import_filesel_cb(GtkWidget *widget, gpointer data)
249 {
250         gchar *filename;
251         gchar *utf8_filename;
252
253         filename = filesel_select_file_open(_("Select importing file"), NULL);
254         if (!filename) return;
255
256         utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
257         if (!utf8_filename) {
258                 g_warning("import_filesel_cb(): failed to convert character set.");
259                 utf8_filename = g_strdup(filename);
260         }
261         gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
262         g_free(utf8_filename);
263 }
264
265 static void import_destsel_cb(GtkWidget *widget, gpointer data)
266 {
267         FolderItem *dest;
268         gchar *path;
269
270         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL, FALSE);
271         if (!dest)
272                  return;
273         path = folder_item_get_identifier(dest);
274         gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
275         g_free(path);
276 }
277
278 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
279 {
280         import_cancel_cb(NULL, NULL);
281         return TRUE;
282 }
283
284 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
285 {
286         if (event && event->keyval == GDK_KEY_Escape)
287                 import_cancel_cb(NULL, NULL);
288         return FALSE;
289 }