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