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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* sync.c
*
* (C)2004 Justin Wake <jwake@iinet.net.au>
*
* Licensed under the GNU GPL v2. See COPYING.
*
*/
#include "config.h"
#include "evolution-ipod-sync.h"
#include <gnome.h>
#include <libebook/e-book.h>
#include <libebook/e-contact.h>
#include <libecal/e-cal.h>
#include <libical/ical.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define EBOOK_SOURCE_LIST "/apps/evolution/addressbook/sources"
#define ECAL_SOURCE_LIST "/apps/evolution/calendar/sources"
#define ETASK_SOURCE_LIST "/apps/evolution/tasks/sources"
#define EMEMO_SOURCE_LIST "/apps/evolution/memos/sources"
extern GtkWidget *progress_bar;
extern IPod ipod_info;
static void pulse (void)
{
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progress_bar));
g_main_context_iteration (NULL, FALSE);
}
/**
* Something bad happened.
*/
static void error_dialog (char *title, char *error)
{
GtkWidget *error_dlg =
gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
"<span weight=\"bold\" size=\"larger\">"
"%s</span>\n\n%s.", title, error);
gtk_dialog_set_has_separator (GTK_DIALOG (error_dlg), FALSE);
gtk_container_set_border_width (GTK_CONTAINER (error_dlg), 5);
gtk_label_set_use_markup (GTK_LABEL (GTK_MESSAGE_DIALOG (error_dlg)->label),
TRUE);
gtk_dialog_set_default_response (GTK_DIALOG (error_dlg),
GTK_RESPONSE_OK);
gtk_dialog_run (GTK_DIALOG (error_dlg));
gtk_widget_destroy (error_dlg);
}
/**
* Something really bad happened.
*/
static void critical_error (char *title, char *error)
{
error_dialog (title, error);
gtk_main_quit ();
exit (EXIT_FAILURE);
}
static GSList *
get_source_uris_for_type (char *key)
{
ESourceList *sources;
GSList *groups;
GSList *uris = NULL;
GSList *item, *source;
sources = e_source_list_new_for_gconf_default (key);
groups = e_source_list_peek_groups (sources);
for (item = groups; item != NULL; item = item->next)
{
ESourceGroup *group;
g_assert (item->data != NULL);
group = E_SOURCE_GROUP (item->data);
for (source = e_source_group_peek_sources(group);
source != NULL;
source = source->next)
{
gchar *uri;
g_assert (source->data != NULL);
uri = e_source_get_uri (E_SOURCE (source->data));
uris = g_slist_append (uris, uri);
}
}
g_object_unref (sources);
return uris;
}
static void
free_uri_list (GSList *uris)
{
g_slist_foreach (uris, (GFunc)g_free, NULL);
g_slist_free (uris);
}
/**
* Force the data into little-endian output.
*
* Note: data must be of even length.
*/
static void
force_little_endian (gunichar2 *data, int length)
{
int i;
/* We're big-endian?
(A little tidier than before) */
if (G_BYTE_ORDER == G_BIG_ENDIAN)
{
for (i = 0; i < length; i++)
{
gunichar2 c = data[i];
c = ((((guint16)(c) & 0xFF00) >> 8) |
(((guint16)(c) & 0x00FF) << 8));
data[i] = c;
}
}
}
/**
* Write a string of data to a file on the iPod.
*
* Will return if the write worked, otherwise will
* display an error dialog and end the program.
*/
static void
write_to_ipod (GString *str, char *path, char *filename)
{
char *output_path;
char *output_file;
FILE *f;
guchar *utf8;
gunichar2 *utf16;
guchar bom[2] = {0xFF, 0xFE};
int i, count;
output_path = g_build_path (G_DIR_SEPARATOR_S,
ipod_info.mount_point,
path, NULL);
if (!g_file_test (output_path, G_FILE_TEST_IS_DIR))
{
if (mkdir (output_path, 0777) != 0)
critical_error (_("No output directory!"),
_("The output directory was not found on "
"iPod! Please ensure that iPod has been correctly "
"set up and try again."));
}
output_file = g_build_filename (output_path, filename, NULL);
g_free (output_path);
f = fopen (output_file, "w");
g_free (output_file);
if (f == NULL)
{
critical_error (_("Could not export data!"), strerror (errno));
}
/* Convert the input string into UTF16 */
utf8 = str->str;
if (g_utf8_validate (utf8, -1, NULL))
{
utf16 = g_utf8_to_utf16 (utf8, -1, NULL, NULL, NULL);
/* Swap the bytes if we're big-endian so that the output
* remains little-endian according to the BOM. */
force_little_endian (utf16, g_utf8_strlen (utf8, -1));
}
count = 2 * g_utf8_strlen (utf8, -1);
/* Write the BOM
* 0xFF 0xFE
* UTF-16 Little Endian
*/
for (i = 0; i < 2; i++)
fwrite (&bom[i], 1, 1, f);
if ((fwrite(utf16, count, 1, f) != 1) &&
(count > 0))
{
g_free (utf16);
fclose (f);
critical_error (_("Could not export data!"),
_("Exporting data failed."));
}
g_free (utf16);
fclose (f);
}
static GString *
uri_list_to_vcard_string (GSList *uris)
{
GString *str = NULL;
EBook *book = NULL;
EBookQuery *qry = NULL;
GList *contacts = NULL, *c = NULL;
GSList *uri;
qry = e_book_query_field_exists (E_CONTACT_FILE_AS);
str = g_string_new (NULL);
for (uri = uris; uri != NULL; uri = uri->next)
{
g_assert (uri->data != NULL);
book = e_book_new_from_uri (uri->data, NULL);
if (e_book_open (book, TRUE, NULL) == FALSE)
{
error_dialog (_("Could not open addressbook!"),
_("Could not open the Evolution addressbook to export data."));
/* Maybe the next one will work. */
continue;
}
if (e_book_get_contacts (book, qry, &contacts, NULL) == FALSE)
{
/* Looks like this one is empty. */
g_object_unref (book);
continue;
}
/* Loop through the contacts, adding them to the string. */
for (c = contacts; c != NULL; c = c->next)
{
gchar *tmp;
EContact *contact = E_CONTACT (c->data);
tmp = e_vcard_to_string (E_VCARD (contact),
EVC_FORMAT_VCARD_30);
g_string_append (str, tmp);
g_string_append (str, "\r\n");
g_free (tmp);
g_object_unref (contact);
}
if (contacts != NULL)
g_list_free (contacts);
g_object_unref (book);
}
/* Okay, all done. */
e_book_query_unref (qry);
return (str);
}
static GString *
uri_list_to_vcal_string (GSList *uris, ECalSourceType type)
{
GString *str = NULL;
ECal *cal = NULL;
icalcomponent *obj = NULL;
GList *objects = NULL, *o = NULL;
GSList *uri;
str = g_string_new (NULL);
for (uri = uris; uri != NULL; uri = uri->next)
{
g_assert (uri->data != NULL);
cal = e_cal_new_from_uri (uri->data, type);
if (e_cal_open (cal, TRUE, NULL) == FALSE)
{
error_dialog (_("Could not open calendar/todo!"),
_("Could not open the Evolution calendar/todo list to export data."));
/* Maybe the next one will work. */
continue;
}
e_cal_get_object_list (cal, "#t", &objects, NULL);
for (o = objects; o != NULL; o = o->next)
{
gchar *tmp;
icalcomponent *comp;
g_assert (o->data != NULL);
comp = o->data;
tmp = e_cal_get_component_as_string (cal, comp);
g_string_append (str, tmp);
g_free (tmp);
}
g_object_unref (cal);
}
/* Okay, all done. */
return (str);
}
/* Attempt to export the addressbook. */
static void
export_addressbook (void)
{
GSList *uris;
GString *data;
pulse ();
uris = get_source_uris_for_type (EBOOK_SOURCE_LIST);
pulse ();
data = uri_list_to_vcard_string (uris);
write_to_ipod (data, "/Contacts/", "evolution.vcf");
g_string_free (data, TRUE);
pulse ();
free_uri_list (uris);
pulse ();
}
/* Attempt to export the calendar(s). */
static void
export_calendar (void)
{
GSList *uris;
GString *data;
pulse ();
uris = get_source_uris_for_type (ECAL_SOURCE_LIST);
pulse ();
data = uri_list_to_vcal_string (uris, E_CAL_SOURCE_TYPE_EVENT);
write_to_ipod (data, "/Calendars/", "evolution-cal.ics");
g_string_free (data, TRUE);
free_uri_list (uris);
pulse ();
}
/* Attempt to export the task list(s). */
static void
export_tasks (void)
{
GSList *uris;
GString *data;
pulse ();
uris = get_source_uris_for_type (ETASK_SOURCE_LIST);
pulse ();
data = uri_list_to_vcal_string (uris, E_CAL_SOURCE_TYPE_TODO);
write_to_ipod (data, "/Calendars/", "evolution-todo.ics");
g_string_free (data, TRUE);
free_uri_list (uris);
pulse ();
}
/* Attempt to export the memo list(s). */
static void
export_memos (void)
{
GSList *uris;
GString *data;
pulse ();
uris = get_source_uris_for_type (EMEMO_SOURCE_LIST);
pulse ();
data = uri_list_to_vcal_string (uris, E_CAL_SOURCE_TYPE_JOURNAL);
write_to_ipod (data, "/Calendars/", "evolution-memo.ics");
g_string_free (data, TRUE);
free_uri_list (uris);
pulse ();
}
void
export_to_ipod (void)
{
pulse ();
if (ipod_info.addressbook == TRUE)
export_addressbook ();
if (ipod_info.calendar == TRUE)
export_calendar ();
if (ipod_info.tasks == TRUE)
export_tasks ();
if (ipod_info.memos == TRUE)
export_memos ();
pulse ();
sync ();
pulse ();
return;
}
|