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