Fix CID 1491093: attrib leaked if attvalue is null
[claws.git] / src / import.c
blob4cdb60db3563f61bf12468a2436568805b0e5df9
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 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 "folderview.h"
36 #include "filesel.h"
37 #include "foldersel.h"
38 #include "gtkutils.h"
39 #include "manage_window.h"
40 #include "folder.h"
41 #include "codeconv.h"
42 #include "alertpanel.h"
44 static GtkWidget *window;
45 static GtkWidget *file_entry;
46 static GtkWidget *dest_entry;
47 static GtkWidget *file_button;
48 static GtkWidget *dest_button;
49 static GtkWidget *ok_button;
50 static GtkWidget *cancel_button;
51 static gboolean import_ok; /* see import_mbox() return values */
53 static void import_create(void);
54 static void import_ok_cb(GtkWidget *widget, gpointer data);
55 static void import_cancel_cb(GtkWidget *widget, gpointer data);
56 static void import_filesel_cb(GtkWidget *widget, gpointer data);
57 static void import_destsel_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 import_mbox(FolderItem *default_dest, const gchar* mbox_file)
62 /* return values: -2 skipped/cancelled, -1 error, 0 OK */
64 gchar *dest_id = NULL;
66 import_ok = -2; // skipped or cancelled
68 if (!window) {
69 import_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_dest && default_dest->path) {
79 dest_id = folder_item_get_identifier(default_dest);
82 if (dest_id) {
83 gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
84 g_free(dest_id);
85 } else {
86 gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
88 if (mbox_file)
89 gtk_entry_set_text(GTK_ENTRY(file_entry), mbox_file);
90 else
91 gtk_entry_set_text(GTK_ENTRY(file_entry), "");
92 gtk_widget_grab_focus(file_entry);
94 manage_window_set_transient(GTK_WINDOW(window));
96 gtk_main();
98 gtk_widget_hide(window);
99 gtk_window_set_modal(GTK_WINDOW(window), FALSE);
101 return import_ok;
104 static void import_create(void)
106 GtkWidget *vbox;
107 GtkWidget *hbox;
108 GtkWidget *desc_label;
109 GtkWidget *table;
110 GtkWidget *file_label;
111 GtkWidget *dest_label;
112 GtkWidget *confirm_area;
114 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "import");
115 gtk_window_set_title(GTK_WINDOW(window), _("Import mbox file"));
116 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
117 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
118 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
119 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
120 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
121 g_signal_connect(G_OBJECT(window), "delete_event",
122 G_CALLBACK(delete_event), NULL);
123 g_signal_connect(G_OBJECT(window), "key_press_event",
124 G_CALLBACK(key_pressed), NULL);
125 MANAGE_WINDOW_SIGNALS_CONNECT(window);
127 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
128 gtk_container_add(GTK_CONTAINER(window), vbox);
130 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
131 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
132 gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
134 desc_label = gtk_label_new
135 (_("Locate the mbox file and specify the destination folder."));
136 gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
137 gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
139 table = gtk_grid_new();
140 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
141 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
142 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
143 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
144 gtk_widget_set_size_request(table, 420, -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, 0, 1, 1);
150 dest_label = gtk_label_new(_("Destination folder:"));
151 gtk_label_set_xalign(GTK_LABEL(dest_label), 1.0);
152 gtk_grid_attach(GTK_GRID(table), dest_label, 0, 1, 1, 1);
154 file_entry = gtk_entry_new();
155 gtk_grid_attach(GTK_GRID(table), file_entry, 1, 0, 1, 1);
156 gtk_widget_set_hexpand(file_entry, TRUE);
157 gtk_widget_set_halign(file_entry, GTK_ALIGN_FILL);
159 dest_entry = gtk_entry_new();
160 gtk_grid_attach(GTK_GRID(table), dest_entry, 1, 1, 1, 1);
161 gtk_widget_set_hexpand(dest_entry, TRUE);
162 gtk_widget_set_halign(dest_entry, GTK_ALIGN_FILL);
164 file_button = gtkut_get_browse_file_btn(_("_Browse"));
165 gtk_grid_attach(GTK_GRID(table), file_button, 2, 0, 1, 1);
167 g_signal_connect(G_OBJECT(file_button), "clicked",
168 G_CALLBACK(import_filesel_cb), NULL);
170 dest_button = gtkut_get_browse_directory_btn(_("B_rowse"));
171 gtk_grid_attach(GTK_GRID(table), dest_button, 2, 1, 1, 1);
173 g_signal_connect(G_OBJECT(dest_button), "clicked",
174 G_CALLBACK(import_destsel_cb), NULL);
176 gtkut_stock_button_set_create(&confirm_area,
177 &cancel_button, NULL, _("_Cancel"),
178 &ok_button, NULL, _("_OK"),
179 NULL, NULL, NULL);
180 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
181 gtk_widget_grab_default(ok_button);
183 g_signal_connect(G_OBJECT(ok_button), "clicked",
184 G_CALLBACK(import_ok_cb), NULL);
185 g_signal_connect(G_OBJECT(cancel_button), "clicked",
186 G_CALLBACK(import_cancel_cb), NULL);
188 gtk_widget_show_all(window);
191 static void import_ok_cb(GtkWidget *widget, gpointer data)
193 GtkWidget *window;
194 const gchar *utf8mbox, *destdir;
195 FolderItem *dest;
196 gchar *mbox;
198 utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
199 destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
201 cm_return_if_fail(utf8mbox);
202 cm_return_if_fail(destdir);
204 if (!*utf8mbox) {
205 alertpanel_error(_("Mbox file can't be left empty."));
206 gtk_widget_grab_focus(file_entry);
207 return;
209 if (!*destdir) {
210 alertpanel_error(_("Destination folder can't be left empty."));
211 gtk_widget_grab_focus(dest_entry);
212 return;
215 mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
216 if (!mbox) {
217 g_warning("import_ok_cb(): failed to convert character set");
218 mbox = g_strdup(utf8mbox);
221 dest = folder_find_item_from_identifier(destdir);
223 if (!dest) {
224 alertpanel_error(_("Can't find the destination folder."));
225 gtk_widget_grab_focus(dest_entry);
226 g_free(mbox);
227 return;
228 } else {
229 window = label_window_create(_("Importing mbox file..."));
230 import_ok = proc_mbox(dest, mbox, FALSE, NULL);
231 label_window_destroy(window);
234 g_free(mbox);
236 if (gtk_main_level() > 0)
237 gtk_main_quit();
240 static void import_cancel_cb(GtkWidget *widget, gpointer data)
242 if (gtk_main_level() > 0)
243 gtk_main_quit();
246 static void import_filesel_cb(GtkWidget *widget, gpointer data)
248 gchar *filename;
249 gchar *utf8_filename;
251 filename = filesel_select_file_open(_("Select importing file"), NULL);
252 if (!filename) return;
254 utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
255 if (!utf8_filename) {
256 g_warning("import_filesel_cb(): failed to convert character set");
257 utf8_filename = g_strdup(filename);
259 gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
260 g_free(utf8_filename);
263 static void import_destsel_cb(GtkWidget *widget, gpointer data)
265 FolderItem *dest;
266 gchar *path;
268 dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL, FALSE,
269 _("Select folder to import to"));
270 if (!dest)
271 return;
272 path = folder_item_get_identifier(dest);
273 gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
274 g_free(path);
277 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
279 import_cancel_cb(NULL, NULL);
280 return TRUE;
283 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
285 if (event && event->keyval == GDK_KEY_Escape)
286 import_cancel_cb(NULL, NULL);
287 return FALSE;