sync with sylpheed 0.4.65cvs10
[claws.git] / src / export.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 "main.h"
42 #include "inc.h"
43 #include "mbox.h"
44 #include "filesel.h"
45 #include "foldersel.h"
46 #include "gtkutils.h"
47 #include "manage_window.h"
48 #include "folder.h"
49 #include "utils.h"
50
51 static GtkWidget *window;
52 static GtkWidget *src_entry;
53 static GtkWidget *file_entry;
54 static GtkWidget *src_button;
55 static GtkWidget *file_button;
56 static GtkWidget *ok_button;
57 static GtkWidget *cancel_button;
58 static gboolean export_ack;
59
60 static void export_create(void);
61 static void export_ok_cb(GtkWidget *widget, gpointer data);
62 static void export_cancel_cb(GtkWidget *widget, gpointer data);
63 static void export_srcsel_cb(GtkWidget *widget, gpointer data);
64 static void export_filesel_cb(GtkWidget *widget, gpointer data);
65 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
66
67 gint export_mbox(FolderItem *default_src)
68 {
69         gint ok = 0;
70
71         if (!window)
72                 export_create();
73         else
74                 gtk_widget_show(window);
75
76         change_dir(startup_dir);
77
78         if (default_src && default_src->path)
79                 gtk_entry_set_text(GTK_ENTRY(src_entry), default_src->path);
80         else
81                 gtk_entry_set_text(GTK_ENTRY(src_entry), "");
82         gtk_entry_set_text(GTK_ENTRY(file_entry), "");
83         gtk_widget_grab_focus(file_entry);
84
85         manage_window_set_transient(GTK_WINDOW(window));
86
87         gtk_main();
88
89         if (export_ack) {
90                 gchar *srcdir, *mbox;
91                 FolderItem *src;
92
93                 srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
94                 mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
95
96                 if (mbox && *mbox) {
97                         src = folder_find_item_from_path(srcdir);
98                         if (!src)
99                                 g_warning("Can't find the folder.\n");
100                         else
101                                 ok = export_to_mbox(src, mbox);
102                 }
103         }
104
105         gtk_widget_hide(window);
106
107         return ok;
108 }
109
110 static void export_create(void)
111 {
112         GtkWidget *vbox;
113         GtkWidget *hbox;
114         GtkWidget *desc_label;
115         GtkWidget *table;
116         GtkWidget *file_label;
117         GtkWidget *src_label;
118         GtkWidget *confirm_area;
119
120         window = gtk_window_new(GTK_WINDOW_DIALOG);
121         gtk_window_set_title(GTK_WINDOW(window), _("Export"));
122         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
123         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
124         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
125         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
126         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
127                            GTK_SIGNAL_FUNC(export_cancel_cb), NULL);
128         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
129                            GTK_SIGNAL_FUNC(key_pressed), NULL);
130         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
131                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
132         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
133                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
134
135         vbox = gtk_vbox_new(FALSE, 4);
136         gtk_container_add(GTK_CONTAINER(window), vbox);
137
138         hbox = gtk_hbox_new(FALSE, 0);
139         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
140         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
141
142         desc_label = gtk_label_new
143                 (_("Specify target folder and mbox file."));
144         gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
145
146         table = gtk_table_new(2, 3, FALSE);
147         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
148         gtk_container_set_border_width(GTK_CONTAINER(table), 8);
149         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
150         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
151         gtk_widget_set_usize(table, 420, -1);
152
153         src_label = gtk_label_new(_("Source dir:"));
154         gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
155                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
156         gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
157
158         file_label = gtk_label_new(_("Exporting file:"));
159         gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 1, 2,
160                          GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
161         gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
162
163         src_entry = gtk_entry_new();
164         gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
165                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
166
167         file_entry = gtk_entry_new();
168         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
169                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
170
171         src_button = gtk_button_new_with_label(_(" Select... "));
172         gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
173                          0, 0, 0, 0);
174         gtk_signal_connect(GTK_OBJECT(src_button), "clicked",
175                            GTK_SIGNAL_FUNC(export_srcsel_cb), NULL);
176
177         file_button = gtk_button_new_with_label(_(" Select... "));
178         gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
179                          0, 0, 0, 0);
180         gtk_signal_connect(GTK_OBJECT(file_button), "clicked",
181                            GTK_SIGNAL_FUNC(export_filesel_cb), NULL);
182
183         gtkut_button_set_create(&confirm_area,
184                                 &ok_button,     _("OK"),
185                                 &cancel_button, _("Cancel"),
186                                 NULL, NULL);
187         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
188         gtk_widget_grab_default(ok_button);
189
190         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
191                            GTK_SIGNAL_FUNC(export_ok_cb), NULL);
192         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
193                            GTK_SIGNAL_FUNC(export_cancel_cb), NULL);
194
195         gtk_widget_show_all(window);
196 }
197
198 static void export_ok_cb(GtkWidget *widget, gpointer data)
199 {
200         export_ack = TRUE;
201         if (gtk_main_level() > 1)
202                 gtk_main_quit();
203 }
204
205 static void export_cancel_cb(GtkWidget *widget, gpointer data)
206 {
207         export_ack = FALSE;
208         if (gtk_main_level() > 1)
209                 gtk_main_quit();
210 }
211
212 static void export_filesel_cb(GtkWidget *widget, gpointer data)
213 {
214         gchar *filename;
215
216         filename = filesel_select_file(_("Select exporting file"), NULL);
217         if (filename)
218                 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
219 }
220
221 static void export_srcsel_cb(GtkWidget *widget, gpointer data)
222 {
223         FolderItem *src;
224
225         src = foldersel_folder_sel(NULL);
226         if (src && src->path)
227                 gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
228 }
229
230 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
231 {
232         if (event && event->keyval == GDK_Escape)
233                 export_cancel_cb(NULL, NULL);
234 }