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