2031103baa9fd6e002e1903efb03233deb88968f
[claws.git] / src / export.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <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>
40
41 #include "sylpheed.h"
42 #include "main.h"
43 #include "inc.h"
44 #include "mbox.h"
45 #include "filesel.h"
46 #include "foldersel.h"
47 #include "gtkutils.h"
48 #include "manage_window.h"
49 #include "folder.h"
50 #include "utils.h"
51 #include "codeconv.h"
52
53 static GtkWidget *window;
54 static GtkWidget *src_entry;
55 static GtkWidget *file_entry;
56 static GtkWidget *src_button;
57 static GtkWidget *file_button;
58 static GtkWidget *ok_button;
59 static GtkWidget *cancel_button;
60 static gboolean export_ack;
61
62 static void export_create(void);
63 static void export_ok_cb(GtkWidget *widget, gpointer data);
64 static void export_cancel_cb(GtkWidget *widget, gpointer data);
65 static void export_srcsel_cb(GtkWidget *widget, gpointer data);
66 static void export_filesel_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);
69
70 gint export_mbox(FolderItem *default_src)
71 {
72         gint ok = 0;
73         gchar *src_id = NULL;
74
75         if (!window) {
76                 export_create();
77         }
78         else {
79                 gtk_widget_show(window);
80         }
81
82         change_dir(sylpheed_get_startup_dir());
83
84         if (default_src && default_src->path) {
85                 src_id = folder_item_get_identifier(default_src);
86         }
87
88         if (src_id) {
89                 gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
90                 g_free(src_id);
91         } else {
92                 gtk_entry_set_text(GTK_ENTRY(src_entry), "");
93         }
94         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
95         gtk_widget_grab_focus(file_entry);
96
97         manage_window_set_transient(GTK_WINDOW(window));
98
99         gtk_main();
100
101         if (export_ack) {
102                 const gchar *srcdir, *utf8mbox;
103                 FolderItem *src;
104
105                 srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
106                 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
107                 if (utf8mbox && *utf8mbox) {
108                         gchar *mbox;
109
110                         mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
111                         if (!mbox) {
112                                 g_warning("Failed to convert character set.\n");
113                                 mbox = g_strdup(utf8mbox);
114                         }
115
116                         src = folder_find_item_from_identifier(srcdir);
117                         if (!src) {
118                                 g_warning("Can't find the folder.\n");
119                         } else {
120                                 ok = export_to_mbox(src, mbox);
121                         }
122
123                         g_free(mbox);
124                 }
125         }
126
127         gtk_widget_hide(window);
128
129         return ok;
130 }
131
132 static void export_create(void)
133 {
134         GtkWidget *vbox;
135         GtkWidget *hbox;
136         GtkWidget *desc_label;
137         GtkWidget *table;
138         GtkWidget *file_label;
139         GtkWidget *src_label;
140         GtkWidget *confirm_area;
141
142         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
143         gtk_window_set_title(GTK_WINDOW(window), _("Export"));
144         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
145         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
146         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
147         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
148         g_signal_connect(G_OBJECT(window), "delete_event",
149                          G_CALLBACK(delete_event), NULL);
150         g_signal_connect(G_OBJECT(window), "key_press_event",
151                          G_CALLBACK(key_pressed), NULL);
152         MANAGE_WINDOW_SIGNALS_CONNECT(window);
153
154         vbox = gtk_vbox_new(FALSE, 4);
155         gtk_container_add(GTK_CONTAINER(window), vbox);
156
157         hbox = gtk_hbox_new(FALSE, 0);
158         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
159         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
160
161         desc_label = gtk_label_new
162                 (_("Specify target folder and mbox file."));
163         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
164
165         table = gtk_table_new(2, 3, FALSE);
166         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
167         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
168         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
169         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
170         gtk_widget_set_size_request(table, 420, -1);
171
172         src_label = gtk_label_new(_("Source dir:"));
173         gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
174                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
175         gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
176
177         file_label = gtk_label_new(_("Exporting file:"));
178         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 1, 2,
179                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
180         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
181
182         src_entry = gtk_entry_new();
183         gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
184                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
185
186         file_entry = gtk_entry_new();
187         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
188                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
189
190 #if GTK_CHECK_VERSION(2, 6, 0)
191         src_button = gtk_button_new_with_mnemonic(_("_Browse"));
192         gtk_button_set_image((GtkButton*)src_button,
193                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
194 #else
195         src_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
196 #endif
197         gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
198                          0, 0, 0, 0);
199         g_signal_connect(G_OBJECT(src_button), "clicked",
200                          G_CALLBACK(export_srcsel_cb), NULL);
201
202 #if GTK_CHECK_VERSION(2, 6, 0)
203         file_button = gtk_button_new_with_mnemonic(_("B_rowse"));
204         gtk_button_set_image((GtkButton*)file_button,
205                 gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
206 #else
207         file_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
208 #endif
209         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
210                          0, 0, 0, 0);
211         g_signal_connect(G_OBJECT(file_button), "clicked",
212                          G_CALLBACK(export_filesel_cb), NULL);
213
214         gtkut_stock_button_set_create(&confirm_area,
215                                       &ok_button, GTK_STOCK_OK,
216                                       &cancel_button, GTK_STOCK_CANCEL,
217                                       NULL, NULL);
218         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
219         gtk_widget_grab_default(ok_button);
220
221         g_signal_connect(G_OBJECT(ok_button), "clicked",
222                          G_CALLBACK(export_ok_cb), NULL);
223         g_signal_connect(G_OBJECT(cancel_button), "clicked",
224                          G_CALLBACK(export_cancel_cb), NULL);
225
226         gtk_widget_show_all(window);
227 }
228
229 static void export_ok_cb(GtkWidget *widget, gpointer data)
230 {
231         export_ack = TRUE;
232         if (gtk_main_level() > 1)
233                 gtk_main_quit();
234 }
235
236 static void export_cancel_cb(GtkWidget *widget, gpointer data)
237 {
238         export_ack = FALSE;
239         if (gtk_main_level() > 1)
240                 gtk_main_quit();
241 }
242
243 static void export_filesel_cb(GtkWidget *widget, gpointer data)
244 {
245         gchar *filename;
246
247         filename = filesel_select_file_save(_("Select exporting file"), NULL);
248         if (!filename) return;
249
250         if (g_getenv ("G_BROKEN_FILENAMES")) {
251                 const gchar *oldstr = filename;
252                 filename = conv_codeset_strdup (filename,
253                                                 conv_get_locale_charset_str(),
254                                                 CS_UTF_8);
255                 if (!filename) {
256                         g_warning("export_filesel_cb(): faild to convert character set.");
257                         filename = g_strdup(oldstr);
258                 }
259                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
260                 g_free(filename);
261         } else
262                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
263 }
264
265 static void export_srcsel_cb(GtkWidget *widget, gpointer data)
266 {
267         FolderItem *src;
268
269         src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
270         if (src && src->path)
271                 gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
272 }
273
274 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
275 {
276         export_cancel_cb(NULL, NULL);
277         return TRUE;
278 }
279
280 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
281 {
282         if (event && event->keyval == GDK_Escape)
283                 export_cancel_cb(NULL, NULL);
284         return FALSE;
285 }