Statistics
| Revision:

root / src / export.c @ 1029

History | View | Annotate | Download (7.6 kB)

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