2008-12-21 [paul] 3.7.0cvs2
[claws.git] / src / export.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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/gtk.h>
30
31 #include "claws.h"
32 #include "main.h"
33 #include "inc.h"
34 #include "mbox.h"
35 #include "filesel.h"
36 #include "foldersel.h"
37 #include "gtkutils.h"
38 #include "manage_window.h"
39 #include "folder.h"
40 #include "utils.h"
41 #include "codeconv.h"
42 #include "alertpanel.h"
43
44 static GtkWidget *window;
45 static GtkWidget *src_entry;
46 static GtkWidget *file_entry;
47 static GtkWidget *src_button;
48 static GtkWidget *file_button;
49 static GtkWidget *ok_button;
50 static GtkWidget *cancel_button;
51 static gboolean export_ok; /* see export_mbox() return values */
52
53 static void export_create(void);
54 static void export_ok_cb(GtkWidget *widget, gpointer data);
55 static void export_cancel_cb(GtkWidget *widget, gpointer data);
56 static void export_srcsel_cb(GtkWidget *widget, gpointer data);
57 static void export_filesel_cb(GtkWidget *widget, gpointer data);
58 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
59 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
60
61 gint export_mbox(FolderItem *default_src)
62 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
63 {
64         gchar *src_id = NULL;
65
66         export_ok = -2; // skipped or cancelled
67
68         if (!window) {
69                 export_create();
70         }
71         else {
72                 gtk_widget_show(window);
73         }
74
75         change_dir(claws_get_startup_dir());
76
77         if (default_src && default_src->path) {
78                 src_id = folder_item_get_identifier(default_src);
79         }
80
81         if (src_id) {
82                 gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
83                 g_free(src_id);
84         } else {
85                 gtk_entry_set_text(GTK_ENTRY(src_entry), "");
86         }
87         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
88         gtk_widget_grab_focus(file_entry);
89
90         manage_window_set_transient(GTK_WINDOW(window));
91
92         gtk_main();
93
94         gtk_widget_hide(window);
95
96         return export_ok;
97 }
98
99 static void export_create(void)
100 {
101         GtkWidget *vbox;
102         GtkWidget *hbox;
103         GtkWidget *desc_label;
104         GtkWidget *table;
105         GtkWidget *file_label;
106         GtkWidget *src_label;
107         GtkWidget *confirm_area;
108
109         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "export");
110         gtk_window_set_title(GTK_WINDOW(window), _("Export to mbox file"));
111         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
112         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
113         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
114         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
115         g_signal_connect(G_OBJECT(window), "delete_event",
116                          G_CALLBACK(delete_event), NULL);
117         g_signal_connect(G_OBJECT(window), "key_press_event",
118                          G_CALLBACK(key_pressed), NULL);
119         MANAGE_WINDOW_SIGNALS_CONNECT(window);
120
121         vbox = gtk_vbox_new(FALSE, 4);
122         gtk_container_add(GTK_CONTAINER(window), vbox);
123
124         hbox = gtk_hbox_new(FALSE, 0);
125         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
126         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
127
128         desc_label = gtk_label_new
129                 (_("Locate the folder to export and specify the mbox file."));
130         gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
131         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
132
133         table = gtk_table_new(2, 3, FALSE);
134         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
135         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
136         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
137         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
138         gtk_widget_set_size_request(table, 420, -1);
139
140         src_label = gtk_label_new(_("Source folder:"));
141         gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
142                          (GtkAttachOptions) (GTK_FILL),
143                          (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
144         gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
145
146         file_label = gtk_label_new(_("Mbox file:"));
147         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 1, 2,
148                          (GtkAttachOptions) (GTK_FILL),
149                          (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
150         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
151
152         src_entry = gtk_entry_new();
153         gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
154                          (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
155                          (GtkAttachOptions) (0), 0, 0);
156
157         file_entry = gtk_entry_new();
158         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
159                          (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
160                          (GtkAttachOptions) (0), 0, 0);
161
162         src_button = gtkut_get_browse_directory_btn(_("_Browse"));
163         gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
164                          (GtkAttachOptions) (0),
165                          (GtkAttachOptions) (0), 0, 0);
166         g_signal_connect(G_OBJECT(src_button), "clicked",
167                          G_CALLBACK(export_srcsel_cb), NULL);
168
169         file_button = gtkut_get_browse_file_btn(_("B_rowse"));
170         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
171                          (GtkAttachOptions) (0),
172                          (GtkAttachOptions) (0), 0, 0);
173         g_signal_connect(G_OBJECT(file_button), "clicked",
174                          G_CALLBACK(export_filesel_cb), NULL);
175
176         gtkut_stock_button_set_create(&confirm_area,
177                                       &cancel_button, GTK_STOCK_CANCEL,
178                                       &ok_button, GTK_STOCK_OK,
179                                       NULL, NULL);
180         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
181         gtk_widget_grab_default(ok_button);
182
183         g_signal_connect(G_OBJECT(ok_button), "clicked",
184                          G_CALLBACK(export_ok_cb), NULL);
185         g_signal_connect(G_OBJECT(cancel_button), "clicked",
186                          G_CALLBACK(export_cancel_cb), NULL);
187
188         gtk_widget_show_all(window);
189 }
190
191 static void export_ok_cb(GtkWidget *widget, gpointer data)
192 {
193         const gchar *srcdir, *utf8mbox;
194         FolderItem *src;
195         gchar *mbox;
196
197         srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
198         utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
199
200         if (utf8mbox && !*utf8mbox) {
201                 alertpanel_error(_("Target mbox filename can't be left empty."));
202                 gtk_widget_grab_focus(file_entry);
203                 return;
204         }
205         if (srcdir && !*srcdir) {
206                 alertpanel_error(_("Source folder can't be left empty."));
207                 gtk_widget_grab_focus(src_entry);
208                 return;
209         }
210
211         mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
212         if (!mbox) {
213                 g_warning("export_ok_cb(): failed to convert character set.\n");
214                 mbox = g_strdup(utf8mbox);
215         }
216
217         src = folder_find_item_from_identifier(srcdir);
218         if (!src) {
219                 alertpanel_error(_("Couldn't find the source folder."));
220                 gtk_widget_grab_focus(src_entry);
221                 return;
222         } else {
223                 export_ok = export_to_mbox(src, mbox);
224         }
225
226         g_free(mbox);
227
228         if (gtk_main_level() > 1)
229                 gtk_main_quit();
230 }
231
232 static void export_cancel_cb(GtkWidget *widget, gpointer data)
233 {
234         if (gtk_main_level() > 1)
235                 gtk_main_quit();
236 }
237
238 static void export_filesel_cb(GtkWidget *widget, gpointer data)
239 {
240         gchar *filename;
241
242         filename = filesel_select_file_save(_("Select exporting file"), NULL);
243         if (!filename) return;
244
245         if (g_getenv ("G_BROKEN_FILENAMES")) {
246                 const gchar *oldstr = filename;
247                 filename = conv_codeset_strdup (filename,
248                                                 conv_get_locale_charset_str(),
249                                                 CS_UTF_8);
250                 if (!filename) {
251                         g_warning("export_filesel_cb(): failed to convert character set.");
252                         filename = g_strdup(oldstr);
253                 }
254                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
255                 g_free(filename);
256         } else
257                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
258 }
259
260 static void export_srcsel_cb(GtkWidget *widget, gpointer data)
261 {
262         FolderItem *src;
263
264         src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
265         if (src && src->path)
266                 gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
267 }
268
269 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
270 {
271         export_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                 export_cancel_cb(NULL, NULL);
279         return FALSE;
280 }