2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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"
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;
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);
70 gint import_mbox(FolderItem *default_dest)
73 gchar *dest_id = NULL;
79 gtk_widget_show(window);
82 change_dir(sylpheed_get_startup_dir());
84 if (default_dest && default_dest->path) {
85 dest_id = folder_item_get_identifier(default_dest);
89 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
92 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
94 gtk_entry_set_text(GTK_ENTRY(file_entry), "");
95 gtk_widget_grab_focus(file_entry);
97 manage_window_set_transient(GTK_WINDOW(window));
102 const gchar *utf8mbox, *destdir;
105 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
106 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
107 if (utf8mbox && *utf8mbox) {
110 mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
112 g_warning("Failed to convert character set.\n");
113 mbox = g_strdup(utf8mbox);
116 if (!destdir || !*destdir) {
117 dest = folder_find_item_from_path(INBOX_DIR);
119 dest = folder_find_item_from_identifier
124 g_warning("Can't find the folder.\n");
126 ok = proc_mbox(dest, mbox, FALSE);
133 gtk_widget_hide(window);
138 static void import_create(void)
142 GtkWidget *desc_label;
144 GtkWidget *file_label;
145 GtkWidget *dest_label;
146 GtkWidget *confirm_area;
148 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
149 gtk_window_set_title(GTK_WINDOW(window), _("Import mbox file"));
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);
160 vbox = gtk_vbox_new(FALSE, 4);
161 gtk_container_add(GTK_CONTAINER(window), vbox);
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);
167 desc_label = gtk_label_new
168 (_("Locate the mbox file and specify the destination folder."));
169 gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
170 gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
172 table = gtk_table_new(2, 3, FALSE);
173 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
174 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
175 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
176 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
177 gtk_widget_set_size_request(table, 420, -1);
179 file_label = gtk_label_new(_("Mbox file:"));
180 gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
181 (GtkAttachOptions) (GTK_FILL),
182 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
183 gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
185 dest_label = gtk_label_new(_("Destination folder:"));
186 gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
187 (GtkAttachOptions) (GTK_FILL),
188 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
189 gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
191 file_entry = gtk_entry_new();
192 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
193 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
194 (GtkAttachOptions) (0), 0, 0);
196 dest_entry = gtk_entry_new();
197 gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
198 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
200 file_button = gtkut_get_browse_file_btn(_("_Browse"));
201 gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
202 (GtkAttachOptions) (GTK_FILL),
203 (GtkAttachOptions) (0), 0, 0);
204 g_signal_connect(G_OBJECT(file_button), "clicked",
205 G_CALLBACK(import_filesel_cb), NULL);
207 dest_button = gtkut_get_browse_directory_btn(_("B_rowse"));
208 gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
209 (GtkAttachOptions) (GTK_FILL),
210 (GtkAttachOptions) (0), 0, 0);
211 g_signal_connect(G_OBJECT(dest_button), "clicked",
212 G_CALLBACK(import_destsel_cb), NULL);
214 gtkut_stock_button_set_create(&confirm_area,
215 &ok_button, GTK_STOCK_OK,
216 &cancel_button, GTK_STOCK_CANCEL,
218 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
219 gtk_widget_grab_default(ok_button);
221 g_signal_connect(G_OBJECT(ok_button), "clicked",
222 G_CALLBACK(import_ok_cb), NULL);
223 g_signal_connect(G_OBJECT(cancel_button), "clicked",
224 G_CALLBACK(import_cancel_cb), NULL);
226 gtk_widget_show_all(window);
229 static void import_ok_cb(GtkWidget *widget, gpointer data)
232 if (gtk_main_level() > 1)
236 static void import_cancel_cb(GtkWidget *widget, gpointer data)
239 if (gtk_main_level() > 1)
243 static void import_filesel_cb(GtkWidget *widget, gpointer data)
246 gchar *utf8_filename;
248 filename = filesel_select_file_open(_("Select importing file"), NULL);
249 if (!filename) return;
251 utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
252 if (!utf8_filename) {
253 g_warning("import_filesel_cb(): failed to convert character set.");
254 utf8_filename = g_strdup(filename);
256 gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
257 g_free(utf8_filename);
260 static void import_destsel_cb(GtkWidget *widget, gpointer data)
265 dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
268 path = folder_item_get_identifier(dest);
269 gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
273 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
275 import_cancel_cb(NULL, NULL);
279 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
281 if (event && event->keyval == GDK_Escape)
282 import_cancel_cb(NULL, NULL);