Statistics
| Revision:

root / src / export.c @ 3019

History | View | Annotate | Download (13.1 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2009 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/gtkmenu.h>
35
#include <gtk/gtkmenuitem.h>
36
#include <gtk/gtkoptionmenu.h>
37
#include <gtk/gtklabel.h>
38
#include <gtk/gtkentry.h>
39
#include <gtk/gtkhbbox.h>
40
#include <gtk/gtkbutton.h>
41
#include <gtk/gtkprogressbar.h>
42
43
#include "main.h"
44
#include "inc.h"
45
#include "mbox.h"
46
#include "folder.h"
47
#include "procmsg.h"
48
#include "menu.h"
49
#include "filesel.h"
50
#include "foldersel.h"
51
#include "gtkutils.h"
52
#include "manage_window.h"
53
#include "folder.h"
54
#include "utils.h"
55
#include "progressdialog.h"
56
#include "alertpanel.h"
57
58
enum
59
{
60
        EXPORT_MBOX,
61
        EXPORT_EML,
62
        EXPORT_MH
63
};
64
65
static GtkWidget *window;
66
static GtkWidget *format_optmenu;
67
static GtkWidget *desc_label;
68
static GtkWidget *file_label;
69
static GtkWidget *src_entry;
70
static GtkWidget *file_entry;
71
static GtkWidget *src_button;
72
static GtkWidget *file_button;
73
static GtkWidget *ok_button;
74
static GtkWidget *cancel_button;
75
static gboolean export_finished;
76
static gboolean export_ack;
77
static ProgressDialog *progress;
78
79
static void export_create        (void);
80
static gint export_do                (void);
81
static gint export_eml                (FolderItem        *src,
82
                                 const gchar        *path,
83
                                 gint                 type);
84
85
static void export_format_menu_cb        (GtkWidget        *widget,
86
                                         gpointer         data);
87
88
static void export_ok_cb        (GtkWidget        *widget,
89
                                 gpointer         data);
90
static void export_cancel_cb        (GtkWidget        *widget,
91
                                 gpointer         data);
92
static void export_srcsel_cb        (GtkWidget        *widget,
93
                                 gpointer         data);
94
static void export_filesel_cb        (GtkWidget        *widget,
95
                                 gpointer         data);
96
static gint delete_event        (GtkWidget        *widget,
97
                                 GdkEventAny        *event,
98
                                 gpointer         data);
99
static gboolean key_pressed        (GtkWidget        *widget,
100
                                 GdkEventKey        *event,
101
                                 gpointer         data);
102
103
104
static void export_mbox_func(Folder *folder, FolderItem *item, gpointer data)
105
{
106
        gchar str[64];
107
        gint count = GPOINTER_TO_INT(data);
108
        static GTimeVal tv_prev = {0, 0};
109
        GTimeVal tv_cur;
110
111
        g_get_current_time(&tv_cur);
112
        if (item->total > 0)
113
                g_snprintf(str, sizeof(str), "%d / %d", count, item->total);
114
        else
115
                g_snprintf(str, sizeof(str), "%d", count);
116
        gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->progressbar), str);
117
118
        if (tv_prev.tv_sec == 0 ||
119
            (tv_cur.tv_sec - tv_prev.tv_sec) * G_USEC_PER_SEC +
120
            tv_cur.tv_usec - tv_prev.tv_usec > 100 * 1000) {
121
                if (item->total > 0)
122
                        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->progressbar), (gdouble)count / item->total);
123
                else
124
                        gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progress->progressbar));
125
                ui_update();
126
                tv_prev = tv_cur;
127
        }
128
}
129
130
gint export_mail(FolderItem *default_src)
131
{
132
        gint ok = 0;
133
        gchar *src_id = NULL;
134
135
        export_create();
136
137
        change_dir(get_startup_dir());
138
139
        if (default_src && default_src->path)
140
                src_id = folder_item_get_identifier(default_src);
141
142
        if (src_id) {
143
                gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
144
                g_free(src_id);
145
        }
146
        gtk_widget_grab_focus(file_entry);
147
148
        manage_window_set_transient(GTK_WINDOW(window));
149
150
        export_finished = FALSE;
151
        export_ack = FALSE;
152
153
        inc_lock();
154
155
        while (!export_finished)
156
                gtk_main_iteration();
157
158
        if (export_ack)
159
                ok = export_do();
160
161
        gtk_widget_destroy(window);
162
        window = NULL;
163
        src_entry = file_entry = NULL;
164
        src_button = file_button = ok_button = cancel_button = NULL;
165
166
        inc_unlock();
167
168
        return ok;
169
}
170
171
static gint export_do(void)
172
{
173
        gint ok = 0;
174
        const gchar *srcdir, *utf8mbox;
175
        FolderItem *src;
176
        gchar *mbox;
177
        gchar *msg;
178
        gint type;
179
180
        type = menu_get_option_menu_active_index
181
                (GTK_OPTION_MENU(format_optmenu));
182
183
        srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
184
        utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
185
186
        if (!utf8mbox || !*utf8mbox)
187
                return -1;
188
189
        mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
190
        if (!mbox) {
191
                g_warning("failed to convert character set.");
192
                mbox = g_strdup(utf8mbox);
193
        }
194
195
        src = folder_find_item_from_identifier(srcdir);
196
        if (!src) {
197
                g_warning("Can't find the folder.");
198
                g_free(mbox);
199
                return -1;
200
        }
201
202
        msg = g_strdup_printf(_("Exporting %s ..."), g_basename(srcdir));
203
        progress = progress_dialog_simple_create();
204
        gtk_window_set_title(GTK_WINDOW(progress->window), _("Exporting"));
205
        progress_dialog_set_label(progress, msg);
206
        g_free(msg);
207
        gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
208
        manage_window_set_transient(GTK_WINDOW(progress->window));
209
        gtk_widget_hide(progress->cancel_btn);
210
        g_signal_connect(G_OBJECT(progress->window), "delete_event",
211
                         G_CALLBACK(gtk_true), NULL);
212
        gtk_widget_show(progress->window);
213
        ui_update();
214
215
        if (type == EXPORT_MBOX) {
216
                folder_set_ui_func(src->folder, export_mbox_func, NULL);
217
                ok = export_to_mbox(src, mbox);
218
                folder_set_ui_func(src->folder, NULL, NULL);
219
        } else if (type == EXPORT_EML || type == EXPORT_MH) {
220
                ok = export_eml(src, mbox, type);
221
        }
222
223
        progress_dialog_destroy(progress);
224
        progress = NULL;
225
226
        g_free(mbox);
227
228
        if (ok < 0)
229
                alertpanel_error(_("Error occurred on export."));
230
231
        return ok;
232
}
233
234
static gint export_eml(FolderItem *src, const gchar *path, gint type)
235
{
236
        const gchar *ext = "";
237
        GSList *mlist, *cur;
238
        MsgInfo *msginfo;
239
        gchar *file, *dest;
240
        gint count = 0;
241
        gint ok = 0;
242
243
        g_return_val_if_fail(src != NULL, -1);
244
        g_return_val_if_fail(path != NULL, -1);
245
246
        if (type == EXPORT_EML)
247
                ext = ".eml";
248
249
        if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
250
                if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
251
                        make_dir_hier(path);
252
                        if (!g_file_test(path, G_FILE_TEST_IS_DIR))
253
                                return -1;
254
                } else {
255
                        g_warning("export_eml(): directory %s already exists.",
256
                                  path);
257
                        return -1;
258
                }
259
        }
260
261
        mlist = folder_item_get_msg_list(src, TRUE);
262
        if (!mlist)
263
                return 0;
264
265
        for (cur = mlist; cur != NULL; cur = cur->next) {
266
                msginfo = (MsgInfo *)cur->data;
267
268
                count++;
269
                export_mbox_func(src->folder, src, GINT_TO_POINTER(count));
270
271
                file = folder_item_fetch_msg(src, msginfo->msgnum);
272
                if (!file) {
273
                        ok = -1;
274
                        break;
275
                }
276
                dest = g_strdup_printf("%s%c%d%s", path, G_DIR_SEPARATOR,
277
                                       count, ext);
278
                if (g_file_test(dest, G_FILE_TEST_EXISTS)) {
279
                        g_warning("export_eml(): %s already exists.", dest);
280
                        g_free(dest);
281
                        g_free(file);
282
                        ok = -1;
283
                        break;
284
                }
285
                if (copy_file(file, dest, FALSE) < 0) {
286
                        g_free(dest);
287
                        g_free(file);
288
                        ok = -1;
289
                        break;
290
                }
291
                g_free(dest);
292
                g_free(file);
293
        }
294
295
        procmsg_msg_list_free(mlist);
296
297
        return ok;
298
}
299
300
static void export_create(void)
301
{
302
        GtkWidget *vbox;
303
        GtkWidget *hbox;
304
        GtkWidget *table;
305
        GtkWidget *menu;
306
        GtkWidget *menuitem;
307
        GtkWidget *format_label;
308
        GtkWidget *src_label;
309
        GtkWidget *confirm_area;
310
311
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
312
        gtk_window_set_title(GTK_WINDOW(window), _("Export"));
313
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
314
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
315
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
316
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
317
        g_signal_connect(G_OBJECT(window), "delete_event",
318
                         G_CALLBACK(delete_event), NULL);
319
        g_signal_connect(G_OBJECT(window), "key_press_event",
320
                         G_CALLBACK(key_pressed), NULL);
321
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
322
323
        vbox = gtk_vbox_new(FALSE, 4);
324
        gtk_container_add(GTK_CONTAINER(window), vbox);
325
326
        hbox = gtk_hbox_new(FALSE, 0);
327
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
328
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
329
330
        desc_label = gtk_label_new
331
                (_("Specify source folder and destination file."));
332
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
333
334
        table = gtk_table_new(2, 3, FALSE);
335
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
336
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
337
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
338
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
339
        gtk_widget_set_size_request(table, 420, -1);
340
341
        format_label = gtk_label_new(_("File format:"));
342
        gtk_table_attach(GTK_TABLE(table), format_label, 0, 1, 0, 1,
343
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
344
        gtk_misc_set_alignment(GTK_MISC(format_label), 1, 0.5);
345
346
        src_label = gtk_label_new(_("Source folder:"));
347
        gtk_table_attach(GTK_TABLE(table), src_label, 0, 1, 1, 2,
348
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
349
        gtk_misc_set_alignment(GTK_MISC(src_label), 1, 0.5);
350
351
        file_label = gtk_label_new(_("Destination:"));
352
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 2, 3,
353
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
354
        gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
355
356
        format_optmenu = gtk_option_menu_new();
357
        gtk_table_attach(GTK_TABLE(table), format_optmenu, 1, 2, 0, 1,
358
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
359
360
        menu = gtk_menu_new();
361
        MENUITEM_ADD(menu, menuitem, _("UNIX mbox"), EXPORT_MBOX);
362
        g_signal_connect(G_OBJECT(menuitem), "activate",
363
                         G_CALLBACK(export_format_menu_cb), NULL);
364
        MENUITEM_ADD(menu, menuitem, _("eml (number + .eml)"), EXPORT_EML);
365
        g_signal_connect(G_OBJECT(menuitem), "activate",
366
                         G_CALLBACK(export_format_menu_cb), NULL);
367
        MENUITEM_ADD(menu, menuitem, _("MH (number only)"), EXPORT_MH);
368
        g_signal_connect(G_OBJECT(menuitem), "activate",
369
                         G_CALLBACK(export_format_menu_cb), NULL);
370
371
        gtk_option_menu_set_menu(GTK_OPTION_MENU(format_optmenu), menu);
372
373
        src_entry = gtk_entry_new();
374
        gtk_table_attach(GTK_TABLE(table), src_entry, 1, 2, 1, 2,
375
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
376
377
        file_entry = gtk_entry_new();
378
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 2, 3,
379
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
380
381
        src_button = gtk_button_new_with_label(_(" Select... "));
382
        gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 1, 2,
383
                         0, 0, 0, 0);
384
        g_signal_connect(G_OBJECT(src_button), "clicked",
385
                         G_CALLBACK(export_srcsel_cb), NULL);
386
387
        file_button = gtk_button_new_with_label(_(" Select... "));
388
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 2, 3,
389
                         0, 0, 0, 0);
390
        g_signal_connect(G_OBJECT(file_button), "clicked",
391
                         G_CALLBACK(export_filesel_cb), NULL);
392
393
        gtkut_stock_button_set_create(&confirm_area,
394
                                      &ok_button, GTK_STOCK_OK,
395
                                      &cancel_button, GTK_STOCK_CANCEL,
396
                                      NULL, NULL);
397
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
398
        gtk_widget_grab_default(ok_button);
399
400
        g_signal_connect(G_OBJECT(ok_button), "clicked",
401
                         G_CALLBACK(export_ok_cb), NULL);
402
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
403
                         G_CALLBACK(export_cancel_cb), NULL);
404
405
        gtk_widget_show_all(window);
406
}
407
408
static void export_format_menu_cb(GtkWidget *widget, gpointer data)
409
{
410
        gint type;
411
412
        type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget),
413
                               MENU_VAL_ID));
414
        if (type == EXPORT_MBOX) {
415
                gtk_label_set_text(GTK_LABEL(desc_label),
416
                                   _("Specify source folder and destination file."));
417
        } else {
418
                gtk_label_set_text(GTK_LABEL(desc_label),
419
                                   _("Specify source folder and destination folder."));
420
        }
421
}
422
423
static void export_ok_cb(GtkWidget *widget, gpointer data)
424
{
425
        export_finished = TRUE;
426
        export_ack = TRUE;
427
}
428
429
static void export_cancel_cb(GtkWidget *widget, gpointer data)
430
{
431
        export_finished = TRUE;
432
        export_ack = FALSE;
433
}
434
435
static void export_filesel_cb(GtkWidget *widget, gpointer data)
436
{
437
        gchar *filename;
438
        gchar *utf8_filename;
439
        gint type;
440
441
        type = menu_get_option_menu_active_index
442
                (GTK_OPTION_MENU(format_optmenu));
443
444
        if (type == EXPORT_MBOX)
445
                filename = filesel_select_file(_("Select destination file"),
446
                                               NULL,
447
                                               GTK_FILE_CHOOSER_ACTION_SAVE);
448
        else
449
                filename = filesel_select_file(_("Select destination folder"),
450
                                               NULL,
451
                                               GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
452
        if (!filename) return;
453
454
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
455
        if (!utf8_filename) {
456
                g_warning("export_filesel_cb(): failed to convert character set.");
457
                utf8_filename = g_strdup(filename);
458
        }
459
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
460
        g_free(utf8_filename);
461
462
        g_free(filename);
463
}
464
465
static void export_srcsel_cb(GtkWidget *widget, gpointer data)
466
{
467
        FolderItem *src;
468
        gchar *src_id;
469
470
        src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
471
        if (src && src->path) {
472
                src_id = folder_item_get_identifier(src);
473
                if (src_id) {
474
                        gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
475
                        g_free(src_id);
476
                }
477
        }
478
}
479
480
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
481
{
482
        export_cancel_cb(NULL, NULL);
483
        return TRUE;
484
}
485
486
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
487
{
488
        if (event && event->keyval == GDK_Escape)
489
                export_cancel_cb(NULL, NULL);
490
        return FALSE;
491
}