63395b72b01f0d6f5bce96a236809a9146b25c1b
[claws.git] / src / import.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkhbbox.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkfilesel.h>
38 #include <gtk/gtksignal.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "inc.h"
43 #include "mbox.h"
44 #include "folderview.h"
45 #include "filesel.h"
46 #include "foldersel.h"
47 #include "gtkutils.h"
48 #include "manage_window.h"
49 #include "folder.h"
50 #include "codeconv.h"
51
52 static GtkWidget *window;
53 static GtkWidget *file_entry;
54 static GtkWidget *dest_entry;
55 static GtkWidget *file_button;
56 static GtkWidget *dest_button;
57 static GtkWidget *ok_button;
58 static GtkWidget *cancel_button;
59 static gboolean import_ack;
60
61 static void import_create(void);
62 static void import_ok_cb(GtkWidget *widget, gpointer data);
63 static void import_cancel_cb(GtkWidget *widget, gpointer data);
64 static void import_filesel_cb(GtkWidget *widget, gpointer data);
65 static void import_destsel_cb(GtkWidget *widget, gpointer data);
66 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
67 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
68
69 gint import_mbox(FolderItem *default_dest)
70 {
71         gint ok = 0;
72         gchar *dest_id = NULL;
73
74         if (!window)
75                 import_create();
76         else
77                 gtk_widget_show(window);
78
79         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
80         if (default_dest && default_dest->path)
81                 dest_id = folder_item_get_identifier(default_dest);
82
83         if (dest_id) {
84                 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
85                 g_free(dest_id);
86         } else
87                 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
88         gtk_widget_grab_focus(file_entry);
89
90         manage_window_set_transient(GTK_WINDOW(window));
91
92         gtk_main();
93
94         if (import_ack) {
95                 const gchar *utf8filename, *destdir;
96                 FolderItem *dest;
97
98                 utf8filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
99                 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
100                 if (utf8filename && *utf8filename) {
101                         const gchar *src_codeset = CS_UTF_8;
102                         const gchar *dest_codeset = conv_get_current_charset_str();
103                         gchar *filename;
104
105 #warning FIXME_GTK2 /* should we use g_filename_from_utf8? */
106                         filename = conv_codeset_strdup(utf8filename,
107                                                        src_codeset,
108                                                        dest_codeset);
109                         if (!filename) {
110                                 g_warning("faild to convert character set\n");
111                                 filename = g_strdup(utf8filename);
112                         }
113
114                         if (!destdir || !*destdir) {
115                                 dest = folder_find_item_from_path(INBOX_DIR);
116                         } else
117                                 dest = folder_find_item_from_identifier
118                                         (destdir);
119
120                         if (!dest) {
121                                 g_warning("Can't find the folder.\n");
122                         } else {
123                                 ok = proc_mbox(dest, filename, FALSE);
124                         }
125
126                         g_free(filename);
127                 }
128         }
129
130         gtk_widget_hide(window);
131
132         return ok;
133 }
134
135 static void import_create(void)
136 {
137         GtkWidget *vbox;
138         GtkWidget *hbox;
139         GtkWidget *desc_label;
140         GtkWidget *table;
141         GtkWidget *file_label;
142         GtkWidget *dest_label;
143         GtkWidget *confirm_area;
144
145         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
146         gtk_window_set_title(GTK_WINDOW(window), _("Import"));
147         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
148         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
149         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
150         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
151         g_signal_connect(G_OBJECT(window), "delete_event",
152                          G_CALLBACK(delete_event), NULL);
153         g_signal_connect(G_OBJECT(window), "key_press_event",
154                          G_CALLBACK(key_pressed), NULL);
155         MANAGE_WINDOW_SIGNALS_CONNECT(window);
156
157         vbox = gtk_vbox_new(FALSE, 4);
158         gtk_container_add(GTK_CONTAINER(window), vbox);
159
160         hbox = gtk_hbox_new(FALSE, 0);
161         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
162         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
163
164         desc_label = gtk_label_new
165                 (_("Specify target mbox file and destination folder."));
166         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
167
168         table = gtk_table_new(2, 3, FALSE);
169         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
170         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
171         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
172         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
173         gtk_widget_set_size_request(table, 420, -1);
174
175         file_label = gtk_label_new(_("Importing file:"));
176         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
177                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
178         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
179
180         dest_label = gtk_label_new(_("Destination dir:"));
181         gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
182                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
183         gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
184
185         file_entry = gtk_entry_new();
186         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
187                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
188
189         dest_entry = gtk_entry_new();
190         gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
191                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
192
193         file_button = gtk_button_new_with_label(_(" Select... "));
194         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
195                          0, 0, 0, 0);
196         g_signal_connect(G_OBJECT(file_button), "clicked",
197                          G_CALLBACK(import_filesel_cb), NULL);
198
199         dest_button = gtk_button_new_with_label(_(" Select... "));
200         gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
201                          0, 0, 0, 0);
202         g_signal_connect(G_OBJECT(dest_button), "clicked",
203                          G_CALLBACK(import_destsel_cb), NULL);
204
205         gtkut_button_set_create(&confirm_area,
206                                 &ok_button,     _("OK"),
207                                 &cancel_button, _("Cancel"),
208                                 NULL, NULL);
209         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
210         gtk_widget_grab_default(ok_button);
211
212         g_signal_connect(G_OBJECT(ok_button), "clicked",
213                          G_CALLBACK(import_ok_cb), NULL);
214         g_signal_connect(G_OBJECT(cancel_button), "clicked",
215                          G_CALLBACK(import_cancel_cb), NULL);
216
217         gtk_widget_show_all(window);
218 }
219
220 static void import_ok_cb(GtkWidget *widget, gpointer data)
221 {
222         import_ack = TRUE;
223         if (gtk_main_level() > 1)
224                 gtk_main_quit();
225 }
226
227 static void import_cancel_cb(GtkWidget *widget, gpointer data)
228 {
229         import_ack = FALSE;
230         if (gtk_main_level() > 1)
231                 gtk_main_quit();
232 }
233
234 static void import_filesel_cb(GtkWidget *widget, gpointer data)
235 {
236         gchar *filename;
237
238         filename = filesel_select_file_open(_("Select importing file"), NULL);
239         if (!filename) return;
240
241         if (g_getenv ("G_BROKEN_FILENAMES")) {
242                 const gchar *oldstr = filename;
243                 filename = conv_codeset_strdup (filename,
244                                                 conv_get_current_charset_str(),
245                                                 CS_UTF_8);
246                 if (!filename) {
247                         g_warning("import_filesel_cb(): faild to convert character set.");
248                         filename = g_strdup(oldstr);
249                 }
250                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
251                 g_free(filename);
252         } else
253                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
254 }
255
256 static void import_destsel_cb(GtkWidget *widget, gpointer data)
257 {
258         FolderItem *dest;
259         gchar *path;
260
261         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
262         if (!dest)
263                  return;
264         path = folder_item_get_identifier(dest);
265         gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
266         g_free(path);
267 }
268
269 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
270 {
271         import_cancel_cb(NULL, NULL);
272         return TRUE;
273 }
274
275 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
276 {
277         if (event && event->keyval == GDK_Escape)
278                 import_cancel_cb(NULL, NULL);
279         return FALSE;
280 }