Statistics
| Revision:

root / src / import.c @ 2006

History | View | Annotate | Download (7.9 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/gtksignal.h>
39 237 hiro
#include <gtk/gtkstock.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 "folderview.h"
45 1 hiro
#include "filesel.h"
46 1 hiro
#include "foldersel.h"
47 1 hiro
#include "gtkutils.h"
48 1 hiro
#include "manage_window.h"
49 1 hiro
#include "folder.h"
50 1 hiro
51 1 hiro
static GtkWidget *window;
52 1 hiro
static GtkWidget *file_entry;
53 1 hiro
static GtkWidget *dest_entry;
54 1 hiro
static GtkWidget *file_button;
55 1 hiro
static GtkWidget *dest_button;
56 1 hiro
static GtkWidget *ok_button;
57 1 hiro
static GtkWidget *cancel_button;
58 903 hiro
static gboolean import_finished;
59 1 hiro
static gboolean import_ack;
60 1 hiro
61 903 hiro
static void import_create        (void);
62 903 hiro
static void import_ok_cb        (GtkWidget        *widget,
63 903 hiro
                                 gpointer         data);
64 903 hiro
static void import_cancel_cb        (GtkWidget        *widget,
65 903 hiro
                                 gpointer         data);
66 903 hiro
static void import_filesel_cb        (GtkWidget        *widget,
67 903 hiro
                                 gpointer         data);
68 903 hiro
static void import_destsel_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 import_mbox(FolderItem *default_dest)
78 1 hiro
{
79 1 hiro
        gint ok = 0;
80 1 hiro
        gchar *dest_id = NULL;
81 1 hiro
82 903 hiro
        import_create();
83 1 hiro
84 1 hiro
        if (default_dest && default_dest->path)
85 1 hiro
                dest_id = folder_item_get_identifier(default_dest);
86 1 hiro
87 1 hiro
        if (dest_id) {
88 1 hiro
                gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
89 1 hiro
                g_free(dest_id);
90 903 hiro
        }
91 1 hiro
        gtk_widget_grab_focus(file_entry);
92 1 hiro
93 1 hiro
        manage_window_set_transient(GTK_WINDOW(window));
94 1 hiro
95 903 hiro
        import_finished = FALSE;
96 903 hiro
        import_ack = FALSE;
97 1 hiro
98 903 hiro
        while (!import_finished)
99 903 hiro
                gtk_main_iteration();
100 903 hiro
101 1 hiro
        if (import_ack) {
102 1 hiro
                const gchar *utf8filename, *destdir;
103 1 hiro
                FolderItem *dest;
104 1 hiro
105 1 hiro
                utf8filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
106 1 hiro
                destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
107 1 hiro
                if (utf8filename && *utf8filename) {
108 1 hiro
                        gchar *filename;
109 1 hiro
110 1 hiro
                        filename = g_filename_from_utf8
111 1 hiro
                                (utf8filename, -1, NULL, NULL, NULL);
112 1 hiro
                        if (!filename) {
113 1 hiro
                                g_warning("faild to convert character set\n");
114 1 hiro
                                filename = g_strdup(utf8filename);
115 1 hiro
                        }
116 1 hiro
117 1 hiro
                        if (!destdir || !*destdir) {
118 1 hiro
                                dest = folder_find_item_from_path(INBOX_DIR);
119 1 hiro
                        } else
120 1 hiro
                                dest = folder_find_item_from_identifier
121 1 hiro
                                        (destdir);
122 1 hiro
123 1 hiro
                        if (!dest) {
124 1 hiro
                                g_warning("Can't find the folder.\n");
125 1 hiro
                        } else {
126 1 hiro
                                ok = proc_mbox(dest, filename, NULL);
127 1 hiro
                                folder_item_scan(dest);
128 1 hiro
                                folderview_update_item(dest, TRUE);
129 1 hiro
                        }
130 1 hiro
131 1 hiro
                        g_free(filename);
132 1 hiro
                }
133 1 hiro
        }
134 1 hiro
135 903 hiro
        gtk_widget_destroy(window);
136 903 hiro
        window = NULL;
137 903 hiro
        file_entry = dest_entry = NULL;
138 903 hiro
        file_button = dest_button = ok_button = cancel_button = NULL;
139 1 hiro
140 1 hiro
        return ok;
141 1 hiro
}
142 1 hiro
143 1 hiro
static void import_create(void)
144 1 hiro
{
145 1 hiro
        GtkWidget *vbox;
146 1 hiro
        GtkWidget *hbox;
147 1 hiro
        GtkWidget *desc_label;
148 1 hiro
        GtkWidget *table;
149 1 hiro
        GtkWidget *file_label;
150 1 hiro
        GtkWidget *dest_label;
151 1 hiro
        GtkWidget *confirm_area;
152 1 hiro
153 1 hiro
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
154 1 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Import"));
155 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
156 1 hiro
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
157 1 hiro
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
158 1 hiro
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
159 1 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
160 1 hiro
                         G_CALLBACK(delete_event), NULL);
161 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
162 1 hiro
                         G_CALLBACK(key_pressed), NULL);
163 1 hiro
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
164 1 hiro
165 1 hiro
        vbox = gtk_vbox_new(FALSE, 4);
166 1 hiro
        gtk_container_add(GTK_CONTAINER(window), vbox);
167 1 hiro
168 1 hiro
        hbox = gtk_hbox_new(FALSE, 0);
169 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
170 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
171 1 hiro
172 1 hiro
        desc_label = gtk_label_new
173 1 hiro
                (_("Specify target mbox file and destination folder."));
174 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
175 1 hiro
176 1 hiro
        table = gtk_table_new(2, 3, FALSE);
177 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
178 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
179 1 hiro
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
180 1 hiro
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
181 1 hiro
        gtk_widget_set_size_request(table, 420, -1);
182 1 hiro
183 1 hiro
        file_label = gtk_label_new(_("Importing file:"));
184 1 hiro
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
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
        dest_label = gtk_label_new(_("Destination dir:"));
189 1 hiro
        gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
190 1 hiro
                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
191 1 hiro
        gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
192 1 hiro
193 1 hiro
        file_entry = gtk_entry_new();
194 1 hiro
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
195 1 hiro
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
196 1 hiro
197 1 hiro
        dest_entry = gtk_entry_new();
198 1 hiro
        gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
199 1 hiro
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
200 1 hiro
201 1 hiro
        file_button = gtk_button_new_with_label(_(" Select... "));
202 1 hiro
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
203 1 hiro
                         0, 0, 0, 0);
204 1 hiro
        g_signal_connect(G_OBJECT(file_button), "clicked",
205 1 hiro
                         G_CALLBACK(import_filesel_cb), NULL);
206 1 hiro
207 1 hiro
        dest_button = gtk_button_new_with_label(_(" Select... "));
208 1 hiro
        gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
209 1 hiro
                         0, 0, 0, 0);
210 1 hiro
        g_signal_connect(G_OBJECT(dest_button), "clicked",
211 1 hiro
                         G_CALLBACK(import_destsel_cb), NULL);
212 1 hiro
213 31 hiro
        gtkut_stock_button_set_create(&confirm_area,
214 31 hiro
                                      &ok_button, GTK_STOCK_OK,
215 31 hiro
                                      &cancel_button, GTK_STOCK_CANCEL,
216 31 hiro
                                      NULL, NULL);
217 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
218 1 hiro
        gtk_widget_grab_default(ok_button);
219 1 hiro
220 1 hiro
        g_signal_connect(G_OBJECT(ok_button), "clicked",
221 1 hiro
                         G_CALLBACK(import_ok_cb), NULL);
222 1 hiro
        g_signal_connect(G_OBJECT(cancel_button), "clicked",
223 1 hiro
                         G_CALLBACK(import_cancel_cb), NULL);
224 1 hiro
225 1 hiro
        gtk_widget_show_all(window);
226 1 hiro
}
227 1 hiro
228 1 hiro
static void import_ok_cb(GtkWidget *widget, gpointer data)
229 1 hiro
{
230 903 hiro
        import_finished = TRUE;
231 1 hiro
        import_ack = TRUE;
232 1 hiro
}
233 1 hiro
234 1 hiro
static void import_cancel_cb(GtkWidget *widget, gpointer data)
235 1 hiro
{
236 903 hiro
        import_finished = TRUE;
237 1 hiro
        import_ack = FALSE;
238 1 hiro
}
239 1 hiro
240 1 hiro
static void import_filesel_cb(GtkWidget *widget, gpointer data)
241 1 hiro
{
242 128 hiro
        gchar *filename;
243 1 hiro
        gchar *utf8_filename;
244 1 hiro
245 128 hiro
        filename = filesel_select_file(_("Select importing file"), NULL,
246 128 hiro
                                       GTK_FILE_CHOOSER_ACTION_OPEN);
247 1 hiro
        if (!filename) return;
248 1 hiro
249 1 hiro
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
250 1 hiro
        if (!utf8_filename) {
251 1 hiro
                g_warning("import_filesel_cb(): faild to convert characer set.");
252 1 hiro
                utf8_filename = g_strdup(filename);
253 1 hiro
        }
254 1 hiro
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);
255 1 hiro
        g_free(utf8_filename);
256 128 hiro
257 128 hiro
        g_free(filename);
258 1 hiro
}
259 1 hiro
260 1 hiro
static void import_destsel_cb(GtkWidget *widget, gpointer data)
261 1 hiro
{
262 1 hiro
        FolderItem *dest;
263 1 hiro
264 1 hiro
        dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
265 1 hiro
        if (dest && dest->path)
266 1 hiro
                gtk_entry_set_text(GTK_ENTRY(dest_entry), dest->path);
267 1 hiro
}
268 1 hiro
269 1 hiro
static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
270 1 hiro
{
271 1 hiro
        import_cancel_cb(NULL, NULL);
272 1 hiro
        return TRUE;
273 1 hiro
}
274 1 hiro
275 1 hiro
static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
276 1 hiro
{
277 1 hiro
        if (event && event->keyval == GDK_Escape)
278 1 hiro
                import_cancel_cb(NULL, NULL);
279 1 hiro
        return FALSE;
280 1 hiro
}