Statistics
| Revision:

root / src / export.c @ 1603

History | View | Annotate | Download (7.6 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2006 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 <glib/gi18n.h>
28
#include <gdk/gdkkeysyms.h>
29
#include <gtk/gtkmain.h>
30
#include <gtk/gtkwidget.h>
31
#include <gtk/gtkwindow.h>
32
#include <gtk/gtkvbox.h>
33
#include <gtk/gtktable.h>
34
#include <gtk/gtklabel.h>
35
#include <gtk/gtkentry.h>
36
#include <gtk/gtkhbbox.h>
37
#include <gtk/gtkbutton.h>
38
#include <gtk/gtksignal.h>
39
40
#include "main.h"
41
#include "inc.h"
42
#include "mbox.h"
43
#include "filesel.h"
44
#include "foldersel.h"
45
#include "gtkutils.h"
46
#include "manage_window.h"
47
#include "folder.h"
48
#include "utils.h"
49
50
static GtkWidget *window;
51
static GtkWidget *src_entry;
52
static GtkWidget *file_entry;
53
static GtkWidget *src_button;
54
static GtkWidget *file_button;
55
static GtkWidget *ok_button;
56
static GtkWidget *cancel_button;
57
static gboolean export_finished;
58
static gboolean export_ack;
59
60
static void export_create        (void);
61
static void export_ok_cb        (GtkWidget        *widget,
62
                                 gpointer         data);
63
static void export_cancel_cb        (GtkWidget        *widget,
64
                                 gpointer         data);
65
static void export_srcsel_cb        (GtkWidget        *widget,
66
                                 gpointer         data);
67
static void export_filesel_cb        (GtkWidget        *widget,
68
                                 gpointer         data);
69
static gint delete_event        (GtkWidget        *widget,
70
                                 GdkEventAny        *event,
71
                                 gpointer         data);
72
static gboolean key_pressed        (GtkWidget        *widget,
73
                                 GdkEventKey        *event,
74
                                 gpointer         data);
75
76
gint export_mbox(FolderItem *default_src)
77
{
78
        gint ok = 0;
79
        gchar *src_id = NULL;
80
81
        export_create();
82
83
        change_dir(get_startup_dir());
84
85
        if (default_src && default_src->path)
86
                src_id = folder_item_get_identifier(default_src);
87
88
        if (src_id) {
89
                gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
90
                g_free(src_id);
91
        }
92
        gtk_widget_grab_focus(file_entry);
93
94
        manage_window_set_transient(GTK_WINDOW(window));
95
96
        export_finished = FALSE;
97
        export_ack = FALSE;
98
99
        while (!export_finished)
100
                gtk_main_iteration();
101
102
        if (export_ack) {
103
                const gchar *srcdir, *utf8mbox;
104
                FolderItem *src;
105
106
                srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
107
                utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
108
109
                if (utf8mbox && *utf8mbox) {
110
                        gchar *mbox;
111
112
                        mbox = g_filename_from_utf8
113
                                (utf8mbox, -1, NULL, NULL, NULL);
114
                        if (!mbox) {
115
                                g_warning("faild to convert character set\n");
116
                                mbox = g_strdup(utf8mbox);
117
                        }
118
119
                        src = folder_find_item_from_identifier(srcdir);
120
                        if (!src)
121
                                g_warning("Can't find the folder.\n");
122
                        else
123
                                ok = export_to_mbox(src, mbox);
124
125
                        g_free(mbox);
126
                }
127
        }
128
129
        gtk_widget_destroy(window);
130
        window = NULL;
131
        src_entry = file_entry = NULL;
132
        src_button = file_button = ok_button = cancel_button = NULL;
133
134
        return ok;
135
}
136
137
static void export_create(void)
138
{
139
        GtkWidget *vbox;
140
        GtkWidget *hbox;
141
        GtkWidget *desc_label;
142
        GtkWidget *table;
143
        GtkWidget *file_label;
144
        GtkWidget *src_label;
145
        GtkWidget *confirm_area;
146
147
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
148
        gtk_window_set_title(GTK_WINDOW(window), _("Export"));
149
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
150
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
151
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
152
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
153
        g_signal_connect(G_OBJECT(window), "delete_event",
154
                         G_CALLBACK(delete_event), NULL);
155
        g_signal_connect(G_OBJECT(window), "key_press_event",
156
                         G_CALLBACK(key_pressed), NULL);
157
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
158
159
        vbox = gtk_vbox_new(FALSE, 4);
160
        gtk_container_add(GTK_CONTAINER(window), vbox);
161
162
        hbox = gtk_hbox_new(FALSE, 0);
163
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
164
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
165
166
        desc_label = gtk_label_new
167
                (_("Specify target folder and mbox file."));
168
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
169
170
        table = gtk_table_new(2, 3, FALSE);
171
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
172
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
173
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
174
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
175
        gtk_widget_set_size_request(table, 420, -1);
176
177
        src_label = gtk_label_new(_("Source dir:"));
178
        gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 0, 1,
179
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
180
        gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
181
182
        file_label = gtk_label_new(_("Exporting file:"));
183
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 1, 2,
184
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
185
        gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
186
187
        src_entry = gtk_entry_new();
188
        gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 0, 1,
189
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
190
191
        file_entry = gtk_entry_new();
192
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
193
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
194
195
        src_button = gtk_button_new_with_label(_(" Select... "));
196
        gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
197
                         0, 0, 0, 0);
198
        g_signal_connect(G_OBJECT(src_button), "clicked",
199
                         G_CALLBACK(export_srcsel_cb), NULL);
200
201
        file_button = gtk_button_new_with_label(_(" Select... "));
202
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
203
                         0, 0, 0, 0);
204
        g_signal_connect(G_OBJECT(file_button), "clicked",
205
                         G_CALLBACK(export_filesel_cb), NULL);
206
207
        gtkut_stock_button_set_create(&confirm_area,
208
                                      &ok_button, GTK_STOCK_OK,
209
                                      &cancel_button, GTK_STOCK_CANCEL,
210
                                      NULL, NULL);
211
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
212
        gtk_widget_grab_default(ok_button);
213
214
        g_signal_connect(G_OBJECT(ok_button), "clicked",
215
                         G_CALLBACK(export_ok_cb), NULL);
216
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
217
                         G_CALLBACK(export_cancel_cb), NULL);
218
219
        gtk_widget_show_all(window);
220
}
221
222
static void export_ok_cb(GtkWidget *widget, gpointer data)
223
{
224
        export_finished = TRUE;
225
        export_ack = TRUE;
226
}
227
228
static void export_cancel_cb(GtkWidget *widget, gpointer data)
229
{
230
        export_finished = TRUE;
231
        export_ack = FALSE;
232
}
233
234
static void export_filesel_cb(GtkWidget *widget, gpointer data)
235
{
236
        gchar *filename;
237
        gchar *utf8_filename;
238
239
        filename = filesel_select_file(_("Select exporting file"), NULL,
240
                                       GTK_FILE_CHOOSER_ACTION_SAVE);
241
        if (!filename) return;
242
243
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
244
        if (!utf8_filename) {
245
                g_warning("export_filesel_cb(): faild to convert character set.\n");
246
                utf8_filename = g_strdup(filename);
247
        }
248
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
249
        g_free(utf8_filename);
250
251
        g_free(filename);
252
}
253
254
static void export_srcsel_cb(GtkWidget *widget, gpointer data)
255
{
256
        FolderItem *src;
257
258
        src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
259
        if (src && src->path)
260
                gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
261
}
262
263
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
264
{
265
        export_cancel_cb(NULL, NULL);
266
        return TRUE;
267
}
268
269
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
270
{
271
        if (event && event->keyval == GDK_Escape)
272
                export_cancel_cb(NULL, NULL);
273
        return FALSE;
274
}