2005-10-13 [paul] 1.9.15cvs34
[claws.git] / src / import.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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
53 static GtkWidget *window;
54 static GtkWidget *file_entry;
55 static GtkWidget *dest_entry;
56 static GtkWidget *file_button;
57 static GtkWidget *dest_button;
58 static GtkWidget *ok_button;
59 static GtkWidget *cancel_button;
60 static gboolean import_ack;
61
62 static void import_create(void);
63 static void import_ok_cb(GtkWidget *widget, gpointer data);
64 static void import_cancel_cb(GtkWidget *widget, gpointer data);
65 static void import_filesel_cb(GtkWidget *widget, gpointer data);
66 static void import_destsel_cb(GtkWidget *widget, gpointer data);
67 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
68 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
69
70 gint import_mbox(FolderItem *default_dest)
71 {
72         gint ok = 0;
73         gchar *dest_id = NULL;
74
75         if (!window) {
76                 import_create();
77         }
78         else {
79                 gtk_widget_show(window);
80         }
81
82         change_dir(sylpheed_get_startup_dir());
83
84         if (default_dest && default_dest->path) {
85                 dest_id = folder_item_get_identifier(default_dest);
86         }
87
88         if (dest_id) {
89                 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
90                 g_free(dest_id);
91         } else {
92                 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
93         }
94         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
95         gtk_widget_grab_focus(file_entry);
96
97         manage_window_set_transient(GTK_WINDOW(window));
98
99         gtk_main();
100
101         if (import_ack) {
102                 const gchar *utf8mbox, *destdir;
103                 FolderItem *dest;
104
105                 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
106                 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
107                 if (utf8mbox && *utf8mbox) {
108                         gchar *mbox;
109
110                         mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
111                         if (!mbox) {
112                                 g_warning("Failed to convert character set.\n");
113                                 mbox = g_strdup(utf8mbox);
114                         }
115
116                         if (!destdir || !*destdir) {
117                                 dest = folder_find_item_from_path(INBOX_DIR);
118                         } else {
119                                 dest = folder_find_item_from_identifier
120                                         (destdir);
121                         }
122
123                         if (!dest) {
124                                 g_warning("Can't find the folder.\n");
125                         } else {
126                                 ok = proc_mbox(dest, mbox, FALSE);
127                         }
128
129                         g_free(mbox);
130                 }
131         }
132
133         gtk_widget_hide(window);
134
135         return ok;
136 }
137
138 static void import_create(void)
139 {
140         GtkWidget *vbox;
141         GtkWidget *hbox;
142         GtkWidget *desc_label;
143         GtkWidget *table;
144         GtkWidget *file_label;
145         GtkWidget *dest_label;
146         GtkWidget *confirm_area;
147
148         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
149         gtk_window_set_title(GTK_WINDOW(window), _("Import"));
150         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
151         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
152         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
153         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
154         g_signal_connect(G_OBJECT(window), "delete_event",
155                          G_CALLBACK(delete_event), NULL);
156         g_signal_connect(G_OBJECT(window), "key_press_event",
157                          G_CALLBACK(key_pressed), NULL);
158         MANAGE_WINDOW_SIGNALS_CONNECT(window);
159
160         vbox = gtk_vbox_new(FALSE, 4);
161         gtk_container_add(GTK_CONTAINER(window), vbox);
162
163         hbox = gtk_hbox_new(FALSE, 0);
164         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
165         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
166
167         desc_label = gtk_label_new
168                 (_("Specify target mbox file and destination folder."));
169         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
170
171         table = gtk_table_new(2, 3, FALSE);
172         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
173         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
174         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
175         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
176         gtk_widget_set_size_request(table, 420, -1);
177
178         file_label = gtk_label_new(_("Importing file:"));
179         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
180                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
181         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
182
183         dest_label = gtk_label_new(_("Destination dir:"));
184         gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
185                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
186         gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
187
188         file_entry = gtk_entry_new();
189         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
190                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
191
192         dest_entry = gtk_entry_new();
193         gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
194                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
195
196 #if GTK_CHECK_VERSION(2, 6, 0)
197         file_button = gtk_button_new_with_mnemonic(_("_Browse"));
198         gtk_button_set_image((GtkButton*)file_button,
199                 gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
200 #else
201         file_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
202 #endif
203         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
204                          0, 0, 0, 0);
205         g_signal_connect(G_OBJECT(file_button), "clicked",
206                          G_CALLBACK(import_filesel_cb), NULL);
207
208 #if GTK_CHECK_VERSION(2, 6, 0)
209         dest_button = gtk_button_new_with_mnemonic(_("B_rowse"));
210         gtk_button_set_image((GtkButton*)dest_button,
211                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
212 #else
213         dest_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
214 #endif
215         gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
216                          0, 0, 0, 0);
217         g_signal_connect(G_OBJECT(dest_button), "clicked",
218                          G_CALLBACK(import_destsel_cb), NULL);
219
220         gtkut_stock_button_set_create(&confirm_area,
221                                       &ok_button, GTK_STOCK_OK,
222                                       &cancel_button, GTK_STOCK_CANCEL,
223                                       NULL, NULL);
224         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
225         gtk_widget_grab_default(ok_button);
226
227         g_signal_connect(G_OBJECT(ok_button), "clicked",
228                          G_CALLBACK(import_ok_cb), NULL);
229         g_signal_connect(G_OBJECT(cancel_button), "clicked",
230                          G_CALLBACK(import_cancel_cb), NULL);
231
232         gtk_widget_show_all(window);
233 }
234
235 static void import_ok_cb(GtkWidget *widget, gpointer data)
236 {
237         import_ack = TRUE;
238         if (gtk_main_level() > 1)
239                 gtk_main_quit();
240 }
241
242 static void import_cancel_cb(GtkWidget *widget, gpointer data)
243 {
244         import_ack = FALSE;
245         if (gtk_main_level() > 1)
246                 gtk_main_quit();
247 }
248
249 static void import_filesel_cb(GtkWidget *widget, gpointer data)
250 {
251         gchar *filename;
252         gchar *utf8_filename;
253
254         filename = filesel_select_file_open(_("Select importing file"), NULL);
255         if (!filename) return;
256
257         utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
258         if (!utf8_filename) {
259                 g_warning("import_filesel_cb(): failed to convert character set.");
260                 utf8_filename = g_strdup(filename);
261         }
262         gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
263         g_free(utf8_filename);
264 }
265
266 static void import_destsel_cb(GtkWidget *widget, gpointer data)
267 {
268         FolderItem *dest;
269         gchar *path;
270
271         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
272         if (!dest)
273                  return;
274         path = folder_item_get_identifier(dest);
275         gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
276         g_free(path);
277 }
278
279 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
280 {
281         import_cancel_cb(NULL, NULL);
282         return TRUE;
283 }
284
285 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
286 {
287         if (event && event->keyval == GDK_Escape)
288                 import_cancel_cb(NULL, NULL);
289         return FALSE;
290 }