inital gtk2 patch
[claws.git] / src / export.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtktable.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkhbbox.h>
36 #include <gtk/gtkbutton.h>
37 #include <gtk/gtkfilesel.h>
38 #include <gtk/gtksignal.h>
39
40 #include "intl.h"
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         else
78                 gtk_widget_show(window);
79
80         change_dir(sylpheed_get_startup_dir());
81
82         if (default_src && default_src->path)
83                 src_id = folder_item_get_identifier(default_src);
84
85         if (src_id) {
86                 gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
87                 g_free(src_id);
88         } else
89                 gtk_entry_set_text(GTK_ENTRY(src_entry), "");
90         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
91         gtk_widget_grab_focus(file_entry);
92
93         manage_window_set_transient(GTK_WINDOW(window));
94
95         gtk_main();
96
97         if (export_ack) {
98                 const gchar *srcdir, *utf8mbox;
99                 FolderItem *src;
100
101                 srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
102                 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
103                 if (utf8mbox && *utf8mbox) {
104                         const gchar *src_codeset = CS_UTF_8;
105                         const gchar *dest_codeset = conv_get_current_charset_str();
106                         gchar *mbox;
107
108 #warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
109                         mbox = conv_codeset_strdup(utf8mbox, src_codeset, dest_codeset);
110                         if (!mbox) {
111                                 g_warning("faild to convert character set\n");
112                                 mbox = g_strdup(utf8mbox);
113                         }
114
115                         src = folder_find_item_from_identifier(srcdir);
116                         if (!src)
117                                 g_warning("Can't find the folder.\n");
118                         else
119                                 ok = export_to_mbox(src, mbox);
120
121                         g_free(mbox);
122                 }
123         }
124
125         gtk_widget_hide(window);
126
127         return ok;
128 }
129
130 static void export_create(void)
131 {
132         GtkWidget *vbox;
133         GtkWidget *hbox;
134         GtkWidget *desc_label;
135         GtkWidget *table;
136         GtkWidget *file_label;
137         GtkWidget *src_label;
138         GtkWidget *confirm_area;
139
140         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
141         gtk_window_set_title(GTK_WINDOW(window), _("Export"));
142         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
143         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
144         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
145         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
146         g_signal_connect(G_OBJECT(window), "delete_event",
147                          G_CALLBACK(delete_event), NULL);
148         g_signal_connect(G_OBJECT(window), "key_press_event",
149                          G_CALLBACK(key_pressed), NULL);
150         MANAGE_WINDOW_SIGNALS_CONNECT(window);
151
152         vbox = gtk_vbox_new(FALSE, 4);
153         gtk_container_add(GTK_CONTAINER(window), vbox);
154
155         hbox = gtk_hbox_new(FALSE, 0);
156         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
157         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
158
159         desc_label = gtk_label_new
160                 (_("Specify target folder and mbox file."));
161         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
162
163         table = gtk_table_new(2, 3, FALSE);
164         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
165         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
166         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
167         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
168         gtk_widget_set_size_request(table, 420, -1);
169
170         src_label = gtk_label_new(_("Source dir:"));
171         gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
172                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
173         gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
174
175         file_label = gtk_label_new(_("Exporting file:"));
176         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 1, 2,
177                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
178         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
179
180         src_entry = gtk_entry_new();
181         gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
182                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
183
184         file_entry = gtk_entry_new();
185         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
186                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
187
188         src_button = gtk_button_new_with_label(_(" Select... "));
189         gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
190                          0, 0, 0, 0);
191         g_signal_connect(G_OBJECT(src_button), "clicked",
192                          G_CALLBACK(export_srcsel_cb), NULL);
193
194         file_button = gtk_button_new_with_label(_(" Select... "));
195         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
196                          0, 0, 0, 0);
197         g_signal_connect(G_OBJECT(file_button), "clicked",
198                          G_CALLBACK(export_filesel_cb), NULL);
199
200         gtkut_button_set_create(&confirm_area,
201                                 &ok_button,     _("OK"),
202                                 &cancel_button, _("Cancel"),
203                                 NULL, NULL);
204         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
205         gtk_widget_grab_default(ok_button);
206
207         g_signal_connect(G_OBJECT(ok_button), "clicked",
208                          G_CALLBACK(export_ok_cb), NULL);
209         g_signal_connect(G_OBJECT(cancel_button), "clicked",
210                          G_CALLBACK(export_cancel_cb), NULL);
211
212         gtk_widget_show_all(window);
213 }
214
215 static void export_ok_cb(GtkWidget *widget, gpointer data)
216 {
217         export_ack = TRUE;
218         if (gtk_main_level() > 1)
219                 gtk_main_quit();
220 }
221
222 static void export_cancel_cb(GtkWidget *widget, gpointer data)
223 {
224         export_ack = FALSE;
225         if (gtk_main_level() > 1)
226                 gtk_main_quit();
227 }
228
229 static void export_filesel_cb(GtkWidget *widget, gpointer data)
230 {
231         gchar *filename;
232
233         filename = filesel_select_file(_("Select exporting file"), NULL);
234         if (!filename) return;
235
236         if (g_getenv ("G_BROKEN_FILENAMES")) {
237                 const gchar *oldstr = filename;
238                 filename = conv_codeset_strdup (filename,
239                                                 conv_get_current_charset_str(),
240                                                 CS_UTF_8);
241                 if (!filename) {
242                         g_warning("export_filesel_cb(): faild to convert character set.");
243                         filename = g_strdup(oldstr);
244                 }
245                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
246                 g_free(filename);
247         } else
248                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
249 }
250
251 static void export_srcsel_cb(GtkWidget *widget, gpointer data)
252 {
253         FolderItem *src;
254
255         src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
256         if (src && src->path)
257                 gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
258 }
259
260 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
261 {
262         export_cancel_cb(NULL, NULL);
263         return TRUE;
264 }
265
266 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
267 {
268         if (event && event->keyval == GDK_Escape)
269                 export_cancel_cb(NULL, NULL);
270         return FALSE;
271 }