Fix bug #3574: Template addressing
[claws.git] / src / import.c
bloba0e513f2a079ee02e10f3e1e9a08bc4941095978
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
32 #include "claws.h"
33 #include "main.h"
34 #include "inc.h"
35 #include "mbox.h"
36 #include "folderview.h"
37 #include "filesel.h"
38 #include "foldersel.h"
39 #include "gtkutils.h"
40 #include "manage_window.h"
41 #include "folder.h"
42 #include "codeconv.h"
43 #include "alertpanel.h"
45 static GtkWidget *window;
46 static GtkWidget *file_entry;
47 static GtkWidget *dest_entry;
48 static GtkWidget *file_button;
49 static GtkWidget *dest_button;
50 static GtkWidget *ok_button;
51 static GtkWidget *cancel_button;
52 static gboolean import_ok; /* see import_mbox() return values */
54 static void import_create(void);
55 static void import_ok_cb(GtkWidget *widget, gpointer data);
56 static void import_cancel_cb(GtkWidget *widget, gpointer data);
57 static void import_filesel_cb(GtkWidget *widget, gpointer data);
58 static void import_destsel_cb(GtkWidget *widget, gpointer data);
59 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
60 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
62 gint import_mbox(FolderItem *default_dest)
63 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
65 gchar *dest_id = NULL;
67 import_ok = -2; // skipped or cancelled
69 if (!window) {
70 import_create();
72 else {
73 gtk_widget_show(window);
76 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
77 change_dir(claws_get_startup_dir());
79 if (default_dest && default_dest->path) {
80 dest_id = folder_item_get_identifier(default_dest);
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), "");
89 gtk_entry_set_text(GTK_ENTRY(file_entry), "");
90 gtk_widget_grab_focus(file_entry);
92 manage_window_set_transient(GTK_WINDOW(window));
94 gtk_main();
96 gtk_widget_hide(window);
97 gtk_window_set_modal(GTK_WINDOW(window), FALSE);
99 return import_ok;
102 static void import_create(void)
104 GtkWidget *vbox;
105 GtkWidget *hbox;
106 GtkWidget *desc_label;
107 GtkWidget *table;
108 GtkWidget *file_label;
109 GtkWidget *dest_label;
110 GtkWidget *confirm_area;
112 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "import");
113 gtk_window_set_title(GTK_WINDOW(window), _("Import mbox file"));
114 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
115 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
116 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
117 g_signal_connect(G_OBJECT(window), "delete_event",
118 G_CALLBACK(delete_event), NULL);
119 g_signal_connect(G_OBJECT(window), "key_press_event",
120 G_CALLBACK(key_pressed), NULL);
121 MANAGE_WINDOW_SIGNALS_CONNECT(window);
123 vbox = gtk_vbox_new(FALSE, 4);
124 gtk_container_add(GTK_CONTAINER(window), vbox);
126 hbox = gtk_hbox_new(FALSE, 0);
127 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
128 gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
130 desc_label = gtk_label_new
131 (_("Locate the mbox file and specify the destination folder."));
132 gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
133 gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
135 table = gtk_table_new(2, 3, FALSE);
136 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
137 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
138 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
139 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
140 gtk_widget_set_size_request(table, 420, -1);
142 file_label = gtk_label_new(_("Mbox file:"));
143 gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
144 (GtkAttachOptions) (GTK_FILL),
145 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
146 gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
148 dest_label = gtk_label_new(_("Destination folder:"));
149 gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
150 (GtkAttachOptions) (GTK_FILL),
151 (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
152 gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
154 file_entry = gtk_entry_new();
155 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
156 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
157 (GtkAttachOptions) (0), 0, 0);
159 dest_entry = gtk_entry_new();
160 gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
161 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
163 file_button = gtkut_get_browse_file_btn(_("_Browse"));
164 gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
165 (GtkAttachOptions) (GTK_FILL),
166 (GtkAttachOptions) (0), 0, 0);
167 g_signal_connect(G_OBJECT(file_button), "clicked",
168 G_CALLBACK(import_filesel_cb), NULL);
170 dest_button = gtkut_get_browse_directory_btn(_("B_rowse"));
171 gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
172 (GtkAttachOptions) (GTK_FILL),
173 (GtkAttachOptions) (0), 0, 0);
174 g_signal_connect(G_OBJECT(dest_button), "clicked",
175 G_CALLBACK(import_destsel_cb), NULL);
177 gtkut_stock_button_set_create(&confirm_area,
178 &cancel_button, GTK_STOCK_CANCEL,
179 &ok_button, GTK_STOCK_OK,
180 NULL, NULL);
181 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
182 gtk_widget_grab_default(ok_button);
184 g_signal_connect(G_OBJECT(ok_button), "clicked",
185 G_CALLBACK(import_ok_cb), NULL);
186 g_signal_connect(G_OBJECT(cancel_button), "clicked",
187 G_CALLBACK(import_cancel_cb), NULL);
189 gtk_widget_show_all(window);
192 static void import_ok_cb(GtkWidget *widget, gpointer data)
194 const gchar *utf8mbox, *destdir;
195 FolderItem *dest;
196 gchar *mbox;
198 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
199 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
201 if (utf8mbox && !*utf8mbox) {
202 alertpanel_error(_("Source mbox filename can't be left empty."));
203 gtk_widget_grab_focus(file_entry);
204 return;
206 if (destdir && !*destdir) {
207 if (alertpanel(_("Import mbox file"), _("Destination folder is not set.\nImport mbox file to the Inbox folder?"),
208 GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
209 == G_ALERTALTERNATE) {
210 gtk_widget_grab_focus(dest_entry);
211 return;
215 mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
216 if (!mbox) {
217 g_warning("import_ok_cb(): failed to convert character set.");
218 mbox = g_strdup(utf8mbox);
221 if (!destdir || !*destdir) {
222 dest = folder_find_item_from_path(INBOX_DIR);
223 } else {
224 dest = folder_find_item_from_identifier
225 (destdir);
228 if (!dest) {
229 alertpanel_error(_("Can't find the destination folder."));
230 gtk_widget_grab_focus(dest_entry);
231 g_free(mbox);
232 return;
233 } else {
234 import_ok = proc_mbox(dest, mbox, FALSE, NULL);
237 g_free(mbox);
239 if (gtk_main_level() > 1)
240 gtk_main_quit();
243 static void import_cancel_cb(GtkWidget *widget, gpointer data)
245 if (gtk_main_level() > 1)
246 gtk_main_quit();
249 static void import_filesel_cb(GtkWidget *widget, gpointer data)
251 gchar *filename;
252 gchar *utf8_filename;
254 filename = filesel_select_file_open(_("Select importing file"), NULL);
255 if (!filename) return;
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);
262 gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
263 g_free(utf8_filename);
266 static void import_destsel_cb(GtkWidget *widget, gpointer data)
268 FolderItem *dest;
269 gchar *path;
271 dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL, FALSE);
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);
279 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
281 import_cancel_cb(NULL, NULL);
282 return TRUE;
285 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
287 if (event && event->keyval == GDK_KEY_Escape)
288 import_cancel_cb(NULL, NULL);
289 return FALSE;