Statistics
| Revision:

root / src / export.c @ 1

History | View | Annotate | Download (7.43 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 "filesel.h"
45
#include "foldersel.h"
46
#include "gtkutils.h"
47
#include "manage_window.h"
48
#include "folder.h"
49
#include "utils.h"
50

    
51
static GtkWidget *window;
52
static GtkWidget *src_entry;
53
static GtkWidget *file_entry;
54
static GtkWidget *src_button;
55
static GtkWidget *file_button;
56
static GtkWidget *ok_button;
57
static GtkWidget *cancel_button;
58
static gboolean export_ack;
59

    
60
static void export_create(void);
61
static void export_ok_cb(GtkWidget *widget, gpointer data);
62
static void export_cancel_cb(GtkWidget *widget, gpointer data);
63
static void export_srcsel_cb(GtkWidget *widget, gpointer data);
64
static void export_filesel_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 export_mbox(FolderItem *default_src)
69
{
70
        gint ok = 0;
71
        gchar *src_id = NULL;
72

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

    
78
        change_dir(startup_dir);
79

    
80
        if (default_src && default_src->path)
81
                src_id = folder_item_get_identifier(default_src);
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
        gtk_entry_set_text(GTK_ENTRY(file_entry), "");
89
        gtk_widget_grab_focus(file_entry);
90

    
91
        manage_window_set_transient(GTK_WINDOW(window));
92

    
93
        gtk_main();
94

    
95
        if (export_ack) {
96
                const gchar *srcdir, *utf8mbox;
97
                FolderItem *src;
98

    
99
                srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
100
                utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
101

    
102
                if (utf8mbox && *utf8mbox) {
103
                        gchar *mbox;
104

    
105
                        mbox = g_filename_from_utf8
106
                                (utf8mbox, -1, NULL, NULL, NULL);
107
                        if (!mbox) {
108
                                g_warning("faild to convert character set\n");
109
                                mbox = g_strdup(utf8mbox);
110
                        }
111

    
112
                        src = folder_find_item_from_identifier(srcdir);
113
                        if (!src)
114
                                g_warning("Can't find the folder.\n");
115
                        else
116
                                ok = export_to_mbox(src, mbox);
117

    
118
                        g_free(mbox);
119
                }
120
        }
121

    
122
        gtk_widget_hide(window);
123

    
124
        return ok;
125
}
126

    
127
static void export_create(void)
128
{
129
        GtkWidget *vbox;
130
        GtkWidget *hbox;
131
        GtkWidget *desc_label;
132
        GtkWidget *table;
133
        GtkWidget *file_label;
134
        GtkWidget *src_label;
135
        GtkWidget *confirm_area;
136

    
137
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
138
        gtk_window_set_title(GTK_WINDOW(window), _("Export"));
139
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
140
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
141
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
142
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
143
        g_signal_connect(G_OBJECT(window), "delete_event",
144
                         G_CALLBACK(delete_event), NULL);
145
        g_signal_connect(G_OBJECT(window), "key_press_event",
146
                         G_CALLBACK(key_pressed), NULL);
147
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
148

    
149
        vbox = gtk_vbox_new(FALSE, 4);
150
        gtk_container_add(GTK_CONTAINER(window), vbox);
151

    
152
        hbox = gtk_hbox_new(FALSE, 0);
153
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
154
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
155

    
156
        desc_label = gtk_label_new
157
                (_("Specify target folder and mbox file."));
158
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
159

    
160
        table = gtk_table_new(2, 3, FALSE);
161
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
162
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
163
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
164
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
165
        gtk_widget_set_size_request(table, 420, -1);
166

    
167
        src_label = gtk_label_new(_("Source dir:"));
168
        gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
169
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
170
        gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
171

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

    
177
        src_entry = gtk_entry_new();
178
        gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
179
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
180

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

    
185
        src_button = gtk_button_new_with_label(_(" Select... "));
186
        gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
187
                         0, 0, 0, 0);
188
        g_signal_connect(G_OBJECT(src_button), "clicked",
189
                         G_CALLBACK(export_srcsel_cb), NULL);
190

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

    
197
        gtkut_button_set_create(&confirm_area,
198
                                &ok_button,        _("OK"),
199
                                &cancel_button, _("Cancel"),
200
                                NULL, NULL);
201
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
202
        gtk_widget_grab_default(ok_button);
203

    
204
        g_signal_connect(G_OBJECT(ok_button), "clicked",
205
                         G_CALLBACK(export_ok_cb), NULL);
206
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
207
                         G_CALLBACK(export_cancel_cb), NULL);
208

    
209
        gtk_widget_show_all(window);
210
}
211

    
212
static void export_ok_cb(GtkWidget *widget, gpointer data)
213
{
214
        export_ack = TRUE;
215
        if (gtk_main_level() > 1)
216
                gtk_main_quit();
217
}
218

    
219
static void export_cancel_cb(GtkWidget *widget, gpointer data)
220
{
221
        export_ack = FALSE;
222
        if (gtk_main_level() > 1)
223
                gtk_main_quit();
224
}
225

    
226
static void export_filesel_cb(GtkWidget *widget, gpointer data)
227
{
228
        const gchar *filename;
229
        gchar *utf8_filename;
230

    
231
        filename = filesel_select_file(_("Select exporting file"), NULL);
232
        if (!filename) return;
233

    
234
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
235
        if (!utf8_filename) {
236
                g_warning("export_filesel_cb(): faild to convert character set.\n");
237
                utf8_filename = g_strdup(filename);
238
        }
239
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
240
        g_free(utf8_filename);
241
}
242

    
243
static void export_srcsel_cb(GtkWidget *widget, gpointer data)
244
{
245
        FolderItem *src;
246

    
247
        src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
248
        if (src && src->path)
249
                gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
250
}
251

    
252
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
253
{
254
        export_cancel_cb(NULL, NULL);
255
        return TRUE;
256
}
257

    
258
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
259
{
260
        if (event && event->keyval == GDK_Escape)
261
                export_cancel_cb(NULL, NULL);
262
        return FALSE;
263
}