Fix error creation and warning
[claws.git] / src / export.c
blobd4ef252807969408705a44fb17a5ae488e223fd0
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtk.h>
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"
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 */
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);
61 gint export_mbox(FolderItem *default_src)
62 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
64 gchar *src_id = NULL;
66 export_ok = -2; // skipped or cancelled
68 if (!window) {
69 export_create();
71 else {
72 gtk_widget_show(window);
75 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
76 change_dir(claws_get_startup_dir());
78 if (default_src && default_src->path) {
79 src_id = folder_item_get_identifier(default_src);
82 if (src_id) {
83 gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
84 g_free(src_id);
85 } else {
86 gtk_entry_set_text(GTK_ENTRY(src_entry), "");
88 gtk_entry_set_text(GTK_ENTRY(file_entry), "");
89 gtk_widget_grab_focus(file_entry);
91 manage_window_set_transient(GTK_WINDOW(window));
93 gtk_main();
95 gtk_widget_hide(window);
96 gtk_window_set_modal(GTK_WINDOW(window), FALSE);
98 return export_ok;
101 static void export_create(void)
103 GtkWidget *vbox;
104 GtkWidget *hbox;
105 GtkWidget *desc_label;
106 GtkWidget *table;
107 GtkWidget *file_label;
108 GtkWidget *src_label;
109 GtkWidget *confirm_area;
111 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "export");
112 gtk_window_set_title(GTK_WINDOW(window), _("Export to mbox file"));
113 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
114 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
115 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
116 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
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);
123 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
124 gtk_container_add(GTK_CONTAINER(window), vbox);
126 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
127 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
128 gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
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);
135 table = gtk_grid_new();
136 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
137 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
138 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
139 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
140 gtk_widget_set_size_request(table, 420, -1);
142 src_label = gtk_label_new(_("Source folder:"));
143 gtk_label_set_xalign(GTK_LABEL(src_label), 1.0);
144 gtk_grid_attach(GTK_GRID(table), src_label, 0, 0, 1, 1);
146 file_label = gtk_label_new(_("Mbox file:"));
147 gtk_label_set_xalign(GTK_LABEL(file_label), 1.0);
148 gtk_grid_attach(GTK_GRID(table), file_label, 0, 1, 1, 1);
150 src_entry = gtk_entry_new();
151 gtk_grid_attach(GTK_GRID(table), src_entry, 1, 0, 1, 1);
152 gtk_widget_set_hexpand(src_entry, TRUE);
153 gtk_widget_set_halign(src_entry, GTK_ALIGN_FILL);
155 file_entry = gtk_entry_new();
156 gtk_entry_set_activates_default(GTK_ENTRY(file_entry), TRUE);
157 gtk_grid_attach(GTK_GRID(table), file_entry, 1, 1, 1, 1);
158 gtk_widget_set_hexpand(file_entry, TRUE);
159 gtk_widget_set_halign(file_entry, GTK_ALIGN_FILL);
161 src_button = gtkut_get_browse_directory_btn(_("_Browse"));
162 gtk_grid_attach(GTK_GRID(table), src_button, 2, 0, 1, 1);
164 g_signal_connect(G_OBJECT(src_button), "clicked",
165 G_CALLBACK(export_srcsel_cb), NULL);
167 file_button = gtkut_get_browse_file_btn(_("B_rowse"));
168 gtk_grid_attach(GTK_GRID(table), file_button, 2, 1, 1, 1);
170 g_signal_connect(G_OBJECT(file_button), "clicked",
171 G_CALLBACK(export_filesel_cb), NULL);
173 gtkut_stock_button_set_create(&confirm_area,
174 &cancel_button, NULL, _("_Cancel"),
175 &ok_button, NULL, _("_OK"),
176 NULL, NULL, NULL);
177 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
178 gtk_widget_grab_default(ok_button);
180 g_signal_connect(G_OBJECT(ok_button), "clicked",
181 G_CALLBACK(export_ok_cb), NULL);
182 g_signal_connect(G_OBJECT(cancel_button), "clicked",
183 G_CALLBACK(export_cancel_cb), NULL);
185 gtk_widget_show_all(window);
188 static void export_ok_cb(GtkWidget *widget, gpointer data)
190 const gchar *srcdir, *utf8mbox;
191 FolderItem *src;
192 gchar *mbox;
194 srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
195 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
197 if (!utf8mbox || !*utf8mbox) {
198 alertpanel_error(_("Target mbox filename can't be left empty."));
199 gtk_widget_grab_focus(file_entry);
200 return;
202 if (!srcdir || !*srcdir) {
203 alertpanel_error(_("Source folder can't be left empty."));
204 gtk_widget_grab_focus(src_entry);
205 return;
208 mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
209 if (!mbox) {
210 g_warning("export_ok_cb(): failed to convert character set");
211 mbox = g_strdup(utf8mbox);
214 src = folder_find_item_from_identifier(srcdir);
215 if (!src) {
216 alertpanel_error(_("Couldn't find the source folder."));
217 gtk_widget_grab_focus(src_entry);
218 g_free(mbox);
219 return;
220 } else {
221 export_ok = export_to_mbox(src, mbox);
224 g_free(mbox);
226 if (gtk_main_level() > 1)
227 gtk_main_quit();
230 static void export_cancel_cb(GtkWidget *widget, gpointer data)
232 if (gtk_main_level() > 1)
233 gtk_main_quit();
236 static void export_filesel_cb(GtkWidget *widget, gpointer data)
238 gchar *filename;
240 filename = filesel_select_file_save(_("Select exporting file"), NULL);
241 if (!filename) return;
243 if (g_getenv ("G_BROKEN_FILENAMES")) {
244 const gchar *oldstr = filename;
245 filename = conv_codeset_strdup (filename,
246 conv_get_locale_charset_str(),
247 CS_UTF_8);
248 if (!filename) {
249 g_warning("export_filesel_cb(): failed to convert character set");
250 filename = g_strdup(oldstr);
252 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
253 g_free(filename);
254 } else
255 gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
258 static void export_srcsel_cb(GtkWidget *widget, gpointer data)
260 FolderItem *src;
262 src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE,
263 _("Select folder to export"));
264 if (src && src->path)
265 gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
268 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
270 export_cancel_cb(NULL, NULL);
271 return TRUE;
274 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
276 if (event && event->keyval == GDK_KEY_Escape)
277 export_cancel_cb(NULL, NULL);
278 return FALSE;