1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* evolution-mbox-importer.c
*
* Authors: Iain Holmes <iain@ximian.com>
* Michael Zucchi <notzed@ximian.com>
*
* Copyright (C) 2001 Ximian, Inc.
* Copyright (C) 2004 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkprogressbar.h>
#include <bonobo/bonobo-control.h>
#include <camel/camel-exception.h>
#include <importer/evolution-importer.h>
#include <importer/GNOME_Evolution_Importer.h>
#include "mail/em-folder-selection-button.h"
#include "mail/mail-component.h"
#include "mail/mail-mt.h"
#include "mail-importer.h"
/* #define IMPORTER_DEBUG */
#ifdef IMPORTER_DEBUG
#define IN g_print ("=====> %s (%d)\n", G_GNUC_FUNCTION, __LINE__)
#define OUT g_print ("<==== %s (%d)\n", G_GNUC_FUNCTION, __LINE__)
#else
#define IN
#define OUT
#endif
typedef struct {
EvolutionImporter *ii;
GMutex *status_lock;
char *status_what;
int status_pc;
int status_timeout_id;
CamelOperation *cancel; /* cancel/status port */
GtkWidget *selector;
GtkWidget *label;
GtkWidget *progressbar;
GtkWidget *dialog;
char *uri;
} MboxImporter;
static void
process_item_fn(EvolutionImporter *eimporter, CORBA_Object listener, void *data, CORBA_Environment *ev)
{
/*MboxImporter *importer = data;*/
GNOME_Evolution_ImporterListener_ImporterResult result;
/* This is essentially a NOOP, it merely returns ok/fail and is only called once */
#if 0
if (camel_exception_is_set(importer->ex))
result = GNOME_Evolution_ImporterListener_BAD_FILE;
else
#endif
result = GNOME_Evolution_ImporterListener_OK;
GNOME_Evolution_ImporterListener_notifyResult(listener, result, FALSE, ev);
bonobo_object_unref(BONOBO_OBJECT(eimporter));
}
static void
folder_selected(EMFolderSelectionButton *button, MboxImporter *importer)
{
g_free(importer->uri);
importer->uri = g_strdup(em_folder_selection_button_get_selection(button));
}
static void
create_control_fn(EvolutionImporter *importer, Bonobo_Control *control, void *data)
{
GtkWidget *hbox, *w;
hbox = gtk_hbox_new(FALSE, 0);
w = gtk_label_new(_("Destination folder:"));
gtk_box_pack_start((GtkBox *)hbox, w, FALSE, TRUE, 6);
w = em_folder_selection_button_new(_("Select folder"), _("Select folder to import into"));
em_folder_selection_button_set_selection((EMFolderSelectionButton *)w,
mail_component_get_folder_uri(NULL, MAIL_COMPONENT_FOLDER_INBOX));
g_signal_connect(w, "selected", G_CALLBACK(folder_selected), data);
gtk_box_pack_start((GtkBox *)hbox, w, FALSE, TRUE, 6);
gtk_widget_show_all(hbox);
/* Another weird-arsed shell api */
*control = BONOBO_OBJREF(bonobo_control_new(hbox));
}
static gboolean
support_format_fn(EvolutionImporter *importer, const char *filename, void *closure)
{
char signature[6];
gboolean ret = FALSE;
int fd, n;
fd = open(filename, O_RDONLY);
if (fd != -1) {
n = read(fd, signature, 5);
ret = n == 5 && memcmp(signature, "From ", 5) == 0;
close(fd);
}
return ret;
}
static void
importer_destroy_cb(void *data, GObject *object)
{
MboxImporter *importer = data;
if (importer->status_timeout_id)
g_source_remove(importer->status_timeout_id);
g_free(importer->status_what);
g_mutex_free(importer->status_lock);
if (importer->dialog)
gtk_widget_destroy(importer->dialog);
g_free(importer);
}
static void
mbox_status(CamelOperation *op, const char *what, int pc, void *data)
{
MboxImporter *importer = data;
if (pc == CAMEL_OPERATION_START)
pc = 0;
else if (pc == CAMEL_OPERATION_END)
pc = 100;
g_mutex_lock(importer->status_lock);
g_free(importer->status_what);
importer->status_what = g_strdup(what);
importer->status_pc = pc;
g_mutex_unlock(importer->status_lock);
}
static gboolean
mbox_status_timeout(void *data)
{
MboxImporter *importer = data;
int pc;
char *what;
if (!importer->status_what)
return TRUE;
g_mutex_lock(importer->status_lock);
what = importer->status_what;
importer->status_what = NULL;
pc = importer->status_pc;
g_mutex_unlock(importer->status_lock);
gtk_progress_bar_set_fraction((GtkProgressBar *)importer->progressbar, (gfloat)(pc/100.0));
gtk_progress_bar_set_text((GtkProgressBar *)importer->progressbar, what);
return TRUE;
}
static void
mbox_importer_response(GtkWidget *w, guint button, void *data)
{
MboxImporter *importer = data;
if (button == GTK_RESPONSE_CANCEL
&& importer->cancel)
camel_operation_cancel(importer->cancel);
}
static gboolean
load_file_fn(EvolutionImporter *eimporter, const char *filename, void *data)
{
MboxImporter *importer = data;
importer->dialog = gtk_message_dialog_new(NULL, 0/*GTK_DIALOG_NO_SEPARATOR*/,
GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL,
_("Importing `%s'"), filename);
gtk_window_set_title (GTK_WINDOW (importer->dialog), _("Importing..."));
importer->label = gtk_label_new (_("Please wait"));
importer->progressbar = gtk_progress_bar_new ();
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (importer->dialog)->vbox), importer->label, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (importer->dialog)->vbox), importer->progressbar, FALSE, FALSE, 0);
g_signal_connect(importer->dialog, "response", G_CALLBACK(mbox_importer_response), importer);
gtk_widget_show_all(importer->dialog);
importer->status_timeout_id = g_timeout_add(100, mbox_status_timeout, importer);
importer->cancel = camel_operation_new(mbox_status, importer);
mail_msg_wait(mail_importer_import_mbox(filename, importer->uri, importer->cancel));
camel_operation_unref(importer->cancel);
g_source_remove(importer->status_timeout_id);
importer->status_timeout_id = 0;
return TRUE;
}
BonoboObject *
mbox_importer_new(void)
{
MboxImporter *mbox;
mbox = g_new0 (MboxImporter, 1);
mbox->status_lock = g_mutex_new();
mbox->ii = evolution_importer_new(create_control_fn, support_format_fn, load_file_fn, process_item_fn, NULL, mbox);
g_object_weak_ref(G_OBJECT(mbox->ii), importer_destroy_cb, mbox);
return BONOBO_OBJECT (mbox->ii);
}
|