Statistics
| Revision:

root / src / import.c @ 1

History | View | Annotate | Download (7.68 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2002 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 gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
66
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
67

    
68
gint import_mbox(FolderItem *default_dest)
69
{
70
        gint ok = 0;
71
        gchar *dest_id = NULL;
72

    
73
        if (!window)
74
                import_create();
75
        else
76
                gtk_widget_show(window);
77

    
78
        gtk_entry_set_text(GTK_ENTRY(file_entry), "");
79
        if (default_dest && default_dest->path)
80
                dest_id = folder_item_get_identifier(default_dest);
81

    
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), "");
87
        gtk_widget_grab_focus(file_entry);
88

    
89
        manage_window_set_transient(GTK_WINDOW(window));
90

    
91
        gtk_main();
92

    
93
        if (import_ack) {
94
                const gchar *utf8filename, *destdir;
95
                FolderItem *dest;
96

    
97
                utf8filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
98
                destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
99
                if (utf8filename && *utf8filename) {
100
                        gchar *filename;
101

    
102
                        filename = g_filename_from_utf8
103
                                (utf8filename, -1, NULL, NULL, NULL);
104
                        if (!filename) {
105
                                g_warning("faild to convert character set\n");
106
                                filename = g_strdup(utf8filename);
107
                        }
108

    
109
                        if (!destdir || !*destdir) {
110
                                dest = folder_find_item_from_path(INBOX_DIR);
111
                        } else
112
                                dest = folder_find_item_from_identifier
113
                                        (destdir);
114

    
115
                        if (!dest) {
116
                                g_warning("Can't find the folder.\n");
117
                        } else {
118
                                ok = proc_mbox(dest, filename, NULL);
119
                                folder_item_scan(dest);
120
                                folderview_update_item(dest, TRUE);
121
                        }
122

    
123
                        g_free(filename);
124
                }
125
        }
126

    
127
        gtk_widget_hide(window);
128

    
129
        return ok;
130
}
131

    
132
static void import_create(void)
133
{
134
        GtkWidget *vbox;
135
        GtkWidget *hbox;
136
        GtkWidget *desc_label;
137
        GtkWidget *table;
138
        GtkWidget *file_label;
139
        GtkWidget *dest_label;
140
        GtkWidget *confirm_area;
141

    
142
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
143
        gtk_window_set_title(GTK_WINDOW(window), _("Import"));
144
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
145
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
146
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
147
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
148
        g_signal_connect(G_OBJECT(window), "delete_event",
149
                         G_CALLBACK(delete_event), NULL);
150
        g_signal_connect(G_OBJECT(window), "key_press_event",
151
                         G_CALLBACK(key_pressed), NULL);
152
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
153

    
154
        vbox = gtk_vbox_new(FALSE, 4);
155
        gtk_container_add(GTK_CONTAINER(window), vbox);
156

    
157
        hbox = gtk_hbox_new(FALSE, 0);
158
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
159
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
160

    
161
        desc_label = gtk_label_new
162
                (_("Specify target mbox file and destination folder."));
163
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
164

    
165
        table = gtk_table_new(2, 3, FALSE);
166
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
167
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
168
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
169
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
170
        gtk_widget_set_size_request(table, 420, -1);
171

    
172
        file_label = gtk_label_new(_("Importing file:"));
173
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
174
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
175
        gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
176

    
177
        dest_label = gtk_label_new(_("Destination dir:"));
178
        gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
179
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
180
        gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
181

    
182
        file_entry = gtk_entry_new();
183
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
184
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
185

    
186
        dest_entry = gtk_entry_new();
187
        gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
188
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
189

    
190
        file_button = gtk_button_new_with_label(_(" Select... "));
191
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
192
                         0, 0, 0, 0);
193
        g_signal_connect(G_OBJECT(file_button), "clicked",
194
                         G_CALLBACK(import_filesel_cb), NULL);
195

    
196
        dest_button = gtk_button_new_with_label(_(" Select... "));
197
        gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
198
                         0, 0, 0, 0);
199
        g_signal_connect(G_OBJECT(dest_button), "clicked",
200
                         G_CALLBACK(import_destsel_cb), NULL);
201

    
202
        gtkut_button_set_create(&confirm_area,
203
                                &ok_button,        _("OK"),
204
                                &cancel_button, _("Cancel"),
205
                                NULL, NULL);
206
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
207
        gtk_widget_grab_default(ok_button);
208

    
209
        g_signal_connect(G_OBJECT(ok_button), "clicked",
210
                         G_CALLBACK(import_ok_cb), NULL);
211
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
212
                         G_CALLBACK(import_cancel_cb), NULL);
213

    
214
        gtk_widget_show_all(window);
215
}
216

    
217
static void import_ok_cb(GtkWidget *widget, gpointer data)
218
{
219
        import_ack = TRUE;
220
        if (gtk_main_level() > 1)
221
                gtk_main_quit();
222
}
223

    
224
static void import_cancel_cb(GtkWidget *widget, gpointer data)
225
{
226
        import_ack = FALSE;
227
        if (gtk_main_level() > 1)
228
                gtk_main_quit();
229
}
230

    
231
static void import_filesel_cb(GtkWidget *widget, gpointer data)
232
{
233
        const gchar *filename;
234
        gchar *utf8_filename;
235

    
236
        filename = filesel_select_file(_("Select importing file"), NULL);
237
        if (!filename) return;
238

    
239
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
240
        if (!utf8_filename) {
241
                g_warning("import_filesel_cb(): faild to convert characer set.");
242
                utf8_filename = g_strdup(filename);
243
        }
244
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
245
        g_free(utf8_filename);
246
}
247

    
248
static void import_destsel_cb(GtkWidget *widget, gpointer data)
249
{
250
        FolderItem *dest;
251

    
252
        dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
253
        if (dest && dest->path)
254
                gtk_entry_set_text(GTK_ENTRY(dest_entry), dest->path);
255
}
256

    
257
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
258
{
259
        import_cancel_cb(NULL, NULL);
260
        return TRUE;
261
}
262

    
263
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
264
{
265
        if (event && event->keyval == GDK_Escape)
266
                import_cancel_cb(NULL, NULL);
267
        return FALSE;
268
}