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
254
255
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
#include <config.h>
#include <stdio.h>
#include <liboaf/liboaf.h>
#include <bonobo/bonobo-context.h>
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-main.h>
#include <e-book.h>
#include <importer/evolution-importer.h>
#include <importer/GNOME_Evolution_Importer.h>
#define COMPONENT_FACTORY_IID "OAFIID:GNOME_Evolution_Addressbook_VCard_ImporterFactory"
static BonoboGenericFactory *factory = NULL;
typedef struct {
char *filename;
char *folderpath;
GList *cardlist;
GList *iterator;
EBook *book;
gboolean ready;
} VCardImporter;
static void
add_card_cb (EBook *book, EBookStatus status, const gchar *id, gpointer closure)
{
ECard *card = E_CARD(closure);
char *vcard = e_card_get_vcard(card);
g_free(vcard);
gtk_object_unref(GTK_OBJECT(card));
}
static void
book_open_cb (EBook *book, EBookStatus status, gpointer closure)
{
VCardImporter *gci = (VCardImporter *) closure;
gci->cardlist = e_card_load_cards_from_file(gci->filename);
gci->ready = TRUE;
}
static void
ebook_create (VCardImporter *gci)
{
gchar *path, *uri;
gchar *epath;
gci->book = e_book_new ();
if (!gci->book) {
printf ("%s: %s(): Couldn't create EBook, bailing.\n",
__FILE__,
__FUNCTION__);
return;
}
path = g_concat_dir_and_file (g_get_home_dir (), "evolution/local");
uri = g_strdup_printf ("file://%s", path);
g_free (path);
epath = e_path_to_physical (uri, gci->folderpath);
g_free (uri);
uri = g_strdup_printf ("%s/addressbook.db", epath);
g_free (epath);
if (! e_book_load_uri (gci->book, uri, book_open_cb, gci)) {
printf ("error calling load_uri!\n");
}
g_free(uri);
}
/* EvolutionImporter methods */
static void
process_item_fn (EvolutionImporter *importer,
CORBA_Object listener,
void *closure,
CORBA_Environment *ev)
{
VCardImporter *gci = (VCardImporter *) closure;
ECard *card;
if (gci->iterator == NULL)
gci->iterator = gci->cardlist;
if (gci->ready == FALSE) {
GNOME_Evolution_ImporterListener_notifyResult (listener,
GNOME_Evolution_ImporterListener_NOT_READY,
gci->iterator ? TRUE : FALSE,
ev);
return;
}
if (gci->iterator == NULL) {
GNOME_Evolution_ImporterListener_notifyResult (listener,
GNOME_Evolution_ImporterListener_UNSUPPORTED_OPERATION,
FALSE, ev);
return;
}
card = gci->iterator->data;
e_book_add_card (gci->book, card, add_card_cb, card);
gci->iterator = gci->iterator->next;
GNOME_Evolution_ImporterListener_notifyResult (listener,
GNOME_Evolution_ImporterListener_OK,
gci->iterator ? TRUE : FALSE,
ev);
if (ev->_major != CORBA_NO_EXCEPTION) {
g_warning ("Error notifying listeners.");
}
return;
}
static char *supported_extensions[3] = {
".vcf",
".gcrd",
NULL
};
/* Actually check the contents of this file */
static gboolean
check_file_is_vcard (const char *filename)
{
FILE *handle;
char line[4096];
gboolean result;
handle = fopen (filename, "r");
if (handle == NULL) {
return FALSE;
}
fgets (line, 4096, handle);
if (line == NULL) {
fclose (handle);
return FALSE;
}
if (strcmp (line, "BEGIN:VCARD") == 0) {
result = TRUE;
} else {
result = FALSE;
}
fclose (handle);
return result;
}
static gboolean
support_format_fn (EvolutionImporter *importer,
const char *filename,
void *closure)
{
char *ext;
int i;
ext = strrchr (filename, '.');
if (ext == NULL) {
return check_file_is_vcard (filename);
}
for (i = 0; supported_extensions[i] != NULL; i++) {
if (strcmp (supported_extensions[i], ext) == 0)
return check_file_is_vcard (filename);
}
return FALSE;
}
static void
importer_destroy_cb (GtkObject *object,
VCardImporter *gci)
{
gtk_main_quit ();
}
static gboolean
load_file_fn (EvolutionImporter *importer,
const char *filename,
const char *folderpath,
void *closure)
{
VCardImporter *gci;
gci = (VCardImporter *) closure;
gci->filename = g_strdup (filename);
gci->folderpath = g_strdup (folderpath);
gci->cardlist = NULL;
gci->iterator = NULL;
gci->ready = FALSE;
ebook_create (gci);
return TRUE;
}
static BonoboObject *
factory_fn (BonoboGenericFactory *_factory,
void *closure)
{
EvolutionImporter *importer;
VCardImporter *gci;
gci = g_new (VCardImporter, 1);
importer = evolution_importer_new (support_format_fn, load_file_fn,
process_item_fn, NULL, gci);
gtk_signal_connect (GTK_OBJECT (importer), "destroy",
GTK_SIGNAL_FUNC (importer_destroy_cb), gci);
return BONOBO_OBJECT (importer);
}
static void
importer_init (void)
{
if (factory != NULL)
return;
factory = bonobo_generic_factory_new (COMPONENT_FACTORY_IID,
factory_fn, NULL);
if (factory == NULL) {
g_error ("Unable to create factory");
}
bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory));
}
int
main (int argc,
char **argv)
{
CORBA_ORB orb;
gnome_init_with_popt_table ("Evolution-VCard-Importer",
PACKAGE, argc, argv, oaf_popt_options, 0,
NULL);
orb = oaf_init (argc, argv);
if (bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL) == FALSE) {
g_error ("Could not initialize Bonobo.");
}
importer_init ();
bonobo_main ();
return 0;
}
|