2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
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>
45 #include "folderview.h"
47 #include "foldersel.h"
49 #include "manage_window.h"
52 #include "alertpanel.h"
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 */
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);
71 gint import_mbox(FolderItem *default_dest)
72 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
74 gchar *dest_id = NULL;
76 import_ok = -2; // skipped or cancelled
82 gtk_widget_show(window);
85 change_dir(claws_get_startup_dir());
87 if (default_dest && default_dest->path) {
88 dest_id = folder_item_get_identifier(default_dest);
92 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
95 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
97 gtk_entry_set_text(GTK_ENTRY(file_entry), "");
98 gtk_widget_grab_focus(file_entry);
100 manage_window_set_transient(GTK_WINDOW(window));
104 gtk_widget_hide(window);
109 static void import_create(void)
113 GtkWidget *desc_label;
115 GtkWidget *file_label;
116 GtkWidget *dest_label;
117 GtkWidget *confirm_area;
119 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "import");
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);
131 vbox = gtk_vbox_new(FALSE, 4);
132 gtk_container_add(GTK_CONTAINER(window), vbox);
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);
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);
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);
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);
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);
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);
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);
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);
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);
185 gtkut_stock_button_set_create(&confirm_area,
186 &cancel_button, GTK_STOCK_CANCEL,
187 &ok_button, GTK_STOCK_OK,
189 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
190 gtk_widget_grab_default(ok_button);
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);
197 gtk_widget_show_all(window);
200 static void import_ok_cb(GtkWidget *widget, gpointer data)
202 const gchar *utf8mbox, *destdir;
206 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
207 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
209 if (utf8mbox && !*utf8mbox) {
210 alertpanel_error(_("Source mbox filename can't be left empty."));
211 gtk_widget_grab_focus(file_entry);
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);
223 mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
225 g_warning("import_ok_cb(): failed to convert character set.\n");
226 mbox = g_strdup(utf8mbox);
229 if (!destdir || !*destdir) {
230 dest = folder_find_item_from_path(INBOX_DIR);
232 dest = folder_find_item_from_identifier
237 alertpanel_error(_("Can't find the destination folder."));
238 gtk_widget_grab_focus(dest_entry);
241 import_ok = proc_mbox(dest, mbox, FALSE, NULL);
246 if (gtk_main_level() > 1)
250 static void import_cancel_cb(GtkWidget *widget, gpointer data)
252 if (gtk_main_level() > 1)
256 static void import_filesel_cb(GtkWidget *widget, gpointer data)
259 gchar *utf8_filename;
261 filename = filesel_select_file_open(_("Select importing file"), NULL);
262 if (!filename) return;
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);
269 gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
270 g_free(utf8_filename);
273 static void import_destsel_cb(GtkWidget *widget, gpointer data)
278 dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
281 path = folder_item_get_identifier(dest);
282 gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
286 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
288 import_cancel_cb(NULL, NULL);
292 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
294 if (event && event->keyval == GDK_Escape)
295 import_cancel_cb(NULL, NULL);