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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Evolution calendar - ToDo Conduit
*
* Copyright (C) 1998 Free Software Foundation
* Copyright (C) 2000 Helix Code, Inc.
*
* Authors: Eskil Heyn Olsen <deity@eskil.dk>
* JP Rosevear <jpr@helixcode.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#include <config.h>
#include <sys/stat.h>
#include <utime.h>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <liboaf/liboaf.h>
#include <bonobo.h>
#include <gnome-xml/parser.h>
#include <cal-client/cal-client.h>
#include <cal-util/timeutil.h>
#include <pi-source.h>
#include <pi-socket.h>
#include <pi-file.h>
#include <pi-dlp.h>
#include <pi-version.h>
#include <libical/src/libical/icaltypes.h>
#define TODO_CONFIG_LOAD 1
#define TODO_CONFIG_DESTROY 1
#include <todo-conduit-config.h>
#undef TODO_CONFIG_LOAD
#undef TODO_CONFIG_DESTROY
#include <todo-conduit.h>
GnomePilotConduit * conduit_get_gpilot_conduit (guint32);
void conduit_destroy_gpilot_conduit (GnomePilotConduit*);
#define CONDUIT_VERSION "0.1.0"
#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "etodoconduit"
#define DEBUG_CALCONDUIT 1
/* #undef DEBUG_CALCONDUIT */
#ifdef DEBUG_CALCONDUIT
#define LOG(e...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, e)
#else
#define LOG(e...)
#endif
#define WARN(e...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, e)
#define INFO(e...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, e)
/* debug spew DELETE ME */
static char *
print_local (EToDoLocalRecord *local)
{
static char buff[ 4096 ];
if (local == NULL) {
sprintf (buff, "[NULL]");
return buff;
}
if (local->todo && local->todo->description) {
sprintf (buff, "[%d %ld %d %d '%s' '%s']",
local->todo->indefinite,
mktime (& local->todo->due),
local->todo->priority,
local->todo->complete,
local->todo->description,
local->todo->note);
return buff;
}
return "";
}
/* debug spew DELETE ME */
static char *print_remote (GnomePilotRecord *remote)
{
static char buff[ 4096 ];
struct ToDo todo;
if (remote == NULL) {
sprintf (buff, "[NULL]");
return buff;
}
memset (&todo, 0, sizeof (struct ToDo));
unpack_ToDo (&todo, remote->record, remote->length);
sprintf (buff, "[%d %ld %d %d '%s' '%s']",
todo.indefinite,
mktime (& todo.due),
todo.priority,
todo.complete,
todo.description,
todo.note);
return buff;
}
/* Context Routines */
static void
e_todo_context_new (EToDoConduitContext **ctxt, guint32 pilot_id)
{
*ctxt = g_new0 (EToDoConduitContext,1);
g_assert (ctxt!=NULL);
todoconduit_load_configuration (&(*ctxt)->cfg, pilot_id);
}
static void
e_todo_context_destroy (EToDoConduitContext **ctxt)
{
g_return_if_fail (ctxt!=NULL);
g_return_if_fail (*ctxt!=NULL);
if ((*ctxt)->client != NULL)
gtk_object_unref (GTK_OBJECT ((*ctxt)->client));
if ((*ctxt)->cfg != NULL)
todoconduit_destroy_configuration (&(*ctxt)->cfg);
g_free (*ctxt);
*ctxt = NULL;
}
/* Calendar Server routines */
static void
start_calendar_server_cb (GtkWidget *cal_client,
CalClientLoadStatus status,
EToDoConduitContext *ctxt)
{
CalClient *client = CAL_CLIENT (cal_client);
LOG (" entering start_calendar_server_load_cb, tried=%d\n",
ctxt->calendar_load_tried);
if (status == CAL_CLIENT_LOAD_SUCCESS) {
ctxt->calendar_load_success = TRUE;
LOG (" success\n");
gtk_main_quit (); /* end the sub event loop */
} else {
if (ctxt->calendar_load_tried) {
LOG (" load and create of calendar failed\n");
gtk_main_quit (); /* end the sub event loop */
return;
}
cal_client_create_calendar (client, ctxt->calendar_file);
ctxt->calendar_load_tried = TRUE;
}
}
static int
start_calendar_server (EToDoConduitContext *ctxt)
{
g_return_val_if_fail (ctxt != NULL, -2);
ctxt->client = cal_client_new ();
/* FIX ME */
ctxt->calendar_file = g_concat_dir_and_file (g_get_home_dir (),
"evolution/local/Calendar/calendar.ics");
gtk_signal_connect (GTK_OBJECT (ctxt->client), "cal_loaded",
start_calendar_server_cb, ctxt);
LOG (" calling cal_client_load_calendar\n");
cal_client_load_calendar (ctxt->client, ctxt->calendar_file);
/* run a sub event loop to turn cal-client's async load
notification into a synchronous call */
gtk_main ();
if (ctxt->calendar_load_success)
return 0;
return -1;
}
/* Utility routines */
static char *
map_name (EToDoConduitContext *ctxt)
{
char *filename;
filename = g_strdup_printf ("%s/evolution/local/Calendar/pilot-map-%d.xml", g_get_home_dir (), ctxt->cfg->pilot_id);
return filename;
}
static void
compute_status (EToDoConduitContext *ctxt, EToDoLocalRecord *local, const char *uid)
{
local->local.archived = FALSE;
local->local.secret = FALSE;
if (g_hash_table_lookup (ctxt->added, uid))
local->local.attr = GnomePilotRecordNew;
else if (g_hash_table_lookup (ctxt->modified, uid))
local->local.attr = GnomePilotRecordModified;
else if (g_hash_table_lookup (ctxt->deleted, uid))
local->local.attr = GnomePilotRecordDeleted;
else
local->local.attr = GnomePilotRecordNothing;
}
static GnomePilotRecord *
local_record_to_pilot_record (EToDoLocalRecord *local,
EToDoConduitContext *ctxt)
{
GnomePilotRecord *p = NULL;
g_return_val_if_fail (local != NULL, NULL);
g_assert (local->comp != NULL);
g_assert (local->todo != NULL );
LOG ("local_record_to_remote_record\n");
p = g_new0 (GnomePilotRecord, 1);
p->ID = local->local.ID;
p->category = 0;
p->attr = local->local.attr;
p->archived = local->local.archived;
p->secret = local->local.secret;
/* Generate pilot record structure */
p->record = g_new0 (char,0xffff);
p->length = pack_ToDo (local->todo, p->record, 0xffff);
return p;
}
/*
* converts a CalComponent object to a EToDoLocalRecord
*/
static void
local_record_from_comp (EToDoLocalRecord *local, CalComponent *comp, EToDoConduitContext *ctxt)
{
const char *uid;
int *priority;
struct icaltimetype *completed;
CalComponentText summary;
GSList *d_list = NULL;
CalComponentText *description;
CalComponentDateTime due;
time_t due_time;
CalComponentClassification classif;
LOG ("local_record_from_comp\n");
g_return_if_fail (local != NULL);
g_return_if_fail (comp != NULL);
local->comp = comp;
cal_component_get_uid (local->comp, &uid);
local->local.ID = e_pilot_map_lookup_pid (ctxt->map, uid);
compute_status (ctxt, local, uid);
local->todo = g_new0 (struct ToDo,1);
/* STOP: don't replace these with g_strdup, since free_ToDo
uses free to deallocate */
cal_component_get_summary (comp, &summary);
if (summary.value)
local->todo->description = strdup ((char *) summary.value);
cal_component_get_description_list (comp, &d_list);
if (d_list) {
description = (CalComponentText *) d_list->data;
if (description && description->value)
local->todo->note = strdup (description->value);
else
local->todo->note = NULL;
} else {
local->todo->note = NULL;
}
cal_component_get_due (comp, &due);
if (due.value) {
due_time = icaltime_as_timet (*due.value);
local->todo->due = *localtime (&due_time);
local->todo->indefinite = 0;
} else {
local->todo->indefinite = 1;
}
cal_component_get_completed (comp, &completed);
if (completed) {
local->todo->complete = 1;
cal_component_free_icaltimetype (completed);
}
cal_component_get_priority (comp, &priority);
if (priority) {
local->todo->priority = *priority;
cal_component_free_priority (priority);
}
cal_component_get_classification (comp, &classif);
if (classif == CAL_COMPONENT_CLASS_PRIVATE)
local->local.secret = 1;
else
local->local.secret = 0;
local->local.archived = 0;
}
static void
local_record_from_uid (EToDoLocalRecord *local,
const char *uid,
EToDoConduitContext *ctxt)
{
CalComponent *comp;
CalClientGetStatus status;
g_assert(local!=NULL);
status = cal_client_get_object (ctxt->client, uid, &comp);
if (status == CAL_CLIENT_GET_SUCCESS) {
local_record_from_comp (local, comp, ctxt);
} else if (status == CAL_CLIENT_GET_NOT_FOUND) {
comp = cal_component_new ();
cal_component_set_new_vtype (comp, CAL_COMPONENT_TODO);
cal_component_set_uid (comp, uid);
local_record_from_comp (local, comp, ctxt);
} else {
INFO ("Object did not exist");
}
}
static CalComponent *
comp_from_remote_record (GnomePilotConduitSyncAbs *conduit,
GnomePilotRecord *remote,
CalComponent *in_comp)
{
CalComponent *comp;
struct ToDo todo;
struct icaltimetype now = icaltime_from_timet (time (NULL), FALSE, FALSE);
CalComponentText summary = {NULL, NULL};
CalComponentText description = {NULL, NULL};
CalComponentDateTime dt = {NULL, NULL};
struct icaltimetype due;
GSList *d_list;
g_return_val_if_fail (remote != NULL, NULL);
memset (&todo, 0, sizeof (struct ToDo));
unpack_ToDo (&todo, remote->record, remote->length);
if (in_comp == NULL) {
comp = cal_component_new ();
cal_component_set_new_vtype (comp, CAL_COMPONENT_TODO);
cal_component_set_created (comp, &now);
} else {
comp = cal_component_clone (in_comp);
}
LOG (" comp_from_remote_record: "
"creating from remote %s and comp %s\n",
print_remote (remote), cal_component_get_as_string (comp));
cal_component_set_last_modified (comp, &now);
summary.value = todo.description;
cal_component_set_summary (comp, &summary);
description.value = todo.note;
d_list = g_slist_append (NULL, &description);
cal_component_set_comment_list (comp, d_list);
g_slist_free (d_list);
if (todo.complete) {
int percent = 100;
cal_component_set_completed (comp, &now);
cal_component_set_percent (comp, &percent);
}
/* FIX ME This is a bit hackish, how else can we tell if there is
* no due date set?
*/
if (todo.due.tm_sec || todo.due.tm_min || todo.due.tm_hour
|| todo.due.tm_mday || todo.due.tm_mon || todo.due.tm_year) {
due = icaltime_from_timet (mktime (&todo.due), FALSE, FALSE);
dt.value = &due;
cal_component_set_due (comp, &dt);
}
cal_component_set_priority (comp, &todo.priority);
cal_component_set_transparency (comp, CAL_COMPONENT_TRANSP_NONE);
if (remote->attr & dlpRecAttrSecret)
cal_component_set_classification (comp, CAL_COMPONENT_CLASS_PRIVATE);
else
cal_component_set_classification (comp, CAL_COMPONENT_CLASS_PUBLIC);
cal_component_commit_sequence (comp);
free_ToDo(&todo);
return comp;
}
static void
update_comp (GnomePilotConduitSyncAbs *conduit, CalComponent *comp,
EToDoConduitContext *ctxt)
{
gboolean success;
g_return_if_fail (conduit != NULL);
g_return_if_fail (comp != NULL);
LOG ("update_comp: saving to desktop\n%s\n",
cal_component_get_as_string (comp));
success = cal_client_update_object (ctxt->client, comp);
if (!success)
WARN (_("Error while communicating with calendar server"));
}
static void
check_for_slow_setting (GnomePilotConduit *c, EToDoConduitContext *ctxt)
{
int count, map_count;
count = g_list_length (ctxt->uids);
map_count = g_hash_table_size (ctxt->map->pid_map);
/* If there are no objects or objects but no log */
if ((count == 0) || (count > 0 && map_count == 0)) {
GnomePilotConduitStandard *conduit;
LOG (" doing slow sync\n");
conduit = GNOME_PILOT_CONDUIT_STANDARD (c);
gnome_pilot_conduit_standard_set_slow (conduit);
} else {
LOG (" doing fast sync\n");
}
}
/* Pilot syncing callbacks */
static gint
pre_sync (GnomePilotConduit *conduit,
GnomePilotDBInfo *dbi,
EToDoConduitContext *ctxt)
{
GnomePilotConduitSyncAbs *abs_conduit;
GList *l;
int len;
unsigned char *buf;
char *filename;
gint num_records;
abs_conduit = GNOME_PILOT_CONDUIT_SYNC_ABS (conduit);
LOG ("---------------------------------------------------------\n");
LOG ("pre_sync: ToDo Conduit v.%s", CONDUIT_VERSION);
g_message ("ToDo Conduit v.%s", CONDUIT_VERSION);
ctxt->client = NULL;
if (start_calendar_server (ctxt) != 0) {
WARN(_("Could not start wombat server"));
gnome_pilot_conduit_error (conduit, _("Could not start wombat"));
return -1;
}
/* Get the local database */
ctxt->uids = cal_client_get_uids (ctxt->client, CALOBJ_TYPE_TODO);
/* Load the uid <--> pilot id map */
filename = map_name (ctxt);
e_pilot_map_read (filename, &ctxt->map);
g_free (filename);
/* Find the added, modified and deleted items */
ctxt->added = g_hash_table_new (g_str_hash, g_str_equal);
ctxt->modified = g_hash_table_new (g_str_hash, g_str_equal);
ctxt->deleted = g_hash_table_new (g_str_hash, g_str_equal);
ctxt->changed = cal_client_get_changed_uids (ctxt->client,
CALOBJ_TYPE_TODO,
ctxt->map->since + 1);
for (l = ctxt->changed; l != NULL; l = l->next) {
CalObjChange *coc = l->data;
switch (coc->type) {
case CALOBJ_UPDATED:
if (e_pilot_map_lookup_pid (ctxt->map, coc->uid) > 0)
g_hash_table_insert (ctxt->modified, coc->uid, coc);
else
g_hash_table_insert (ctxt->added, coc->uid, coc);
break;
case CALOBJ_REMOVED:
g_hash_table_insert (ctxt->deleted, coc->uid, coc);
break;
}
}
/* Set the count information */
num_records = cal_client_get_n_objects (ctxt->client, CALOBJ_TYPE_TODO);
gnome_pilot_conduit_sync_abs_set_num_local_records(abs_conduit, num_records);
num_records = g_hash_table_size (ctxt->added);
gnome_pilot_conduit_sync_abs_set_num_new_local_records (abs_conduit, num_records);
num_records = g_hash_table_size (ctxt->modified);
gnome_pilot_conduit_sync_abs_set_num_updated_local_records (abs_conduit, num_records);
num_records = g_hash_table_size (ctxt->deleted);
gnome_pilot_conduit_sync_abs_set_num_deleted_local_records(abs_conduit, num_records);
gtk_object_set_data (GTK_OBJECT (conduit), "dbinfo", dbi);
buf = (unsigned char*)g_malloc (0xffff);
len = dlp_ReadAppBlock (dbi->pilot_socket, dbi->db_handle, 0,
(unsigned char *)buf, 0xffff);
if (len < 0) {
WARN (_("Could not read pilot's ToDo application block"));
WARN ("dlp_ReadAppBlock(...) = %d", len);
gnome_pilot_conduit_error (conduit,
_("Could not read pilot's ToDo application block"));
return -1;
}
unpack_ToDoAppInfo (&(ctxt->ai), buf, len);
g_free (buf);
check_for_slow_setting (conduit, ctxt);
return 0;
}
static gint
post_sync (GnomePilotConduit *conduit,
GnomePilotDBInfo *dbi,
EToDoConduitContext *ctxt)
{
gchar *filename;
LOG ("post_sync: ToDo Conduit v.%s", CONDUIT_VERSION);
LOG ("---------------------------------------------------------\n");
filename = map_name (ctxt);
e_pilot_map_write (filename, ctxt->map);
e_pilot_map_destroy (ctxt->map);
g_free (filename);
return 0;
}
static gint
set_pilot_id (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
guint32 ID,
EToDoConduitContext *ctxt)
{
const char *uid;
LOG ("set_pilot_id: setting to %d\n", ID);
cal_component_get_uid (local->comp, &uid);
e_pilot_map_insert (ctxt->map, ID, uid, FALSE);
return 0;
}
static gint
for_each (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord **local,
EToDoConduitContext *ctxt)
{
static GList *uids, *iterator;
static int count;
g_return_val_if_fail (local != NULL, -1);
if (*local == NULL) {
LOG ("beginning for_each");
uids = ctxt->uids;
count = 0;
if (uids != NULL) {
LOG ("iterating over %d records", g_list_length (uids));
*local = g_new0 (EToDoLocalRecord, 1);
local_record_from_uid (*local, uids->data, ctxt);
iterator = uids;
} else {
LOG ("no events");
(*local) = NULL;
return 0;
}
} else {
count++;
if (g_list_next (iterator)) {
iterator = g_list_next (iterator);
*local = g_new0 (EToDoLocalRecord, 1);
local_record_from_uid (*local, iterator->data, ctxt);
} else {
LOG ("for_each ending");
/* Tell the pilot the iteration is over */
*local = NULL;
return 0;
}
}
return 0;
}
static gint
for_each_modified (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord **local,
EToDoConduitContext *ctxt)
{
static GList *changes, *iterator;
static int count;
g_return_val_if_fail (local != NULL, 0);
if (*local == NULL) {
LOG ("beginning for_each_modified: beginning\n");
changes = ctxt->changed;
count = 0;
if (changes != NULL) {
CalObjChange *coc = changes->data;
LOG ("iterating over %d records", g_list_length (changes));
*local = g_new0 (EToDoLocalRecord, 1);
local_record_from_uid (*local, coc->uid, ctxt);
iterator = changes;
} else {
LOG ("no events");
(*local) = NULL;
return 0;
}
} else {
count++;
if (g_list_next (iterator)) {
CalObjChange *coc;
iterator = g_list_next (iterator);
coc = iterator->data;
*local = g_new0 (EToDoLocalRecord, 1);
local_record_from_uid (*local, coc->uid, ctxt);
} else {
LOG ("for_each_modified ending");
/* Tell the pilot the iteration is over */
(*local) = NULL;
return 0;
}
}
return 0;
}
static gint
compare (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
GnomePilotRecord *remote,
EToDoConduitContext *ctxt)
{
/* used by the quick compare */
GnomePilotRecord *local_pilot;
int retval = 0;
LOG ("compare: local=%s remote=%s...\n",
print_local (local), print_remote (remote));
g_return_val_if_fail (local!=NULL,-1);
g_return_val_if_fail (remote!=NULL,-1);
local_pilot = local_record_to_pilot_record (local, ctxt);
if (!local_pilot)
return -1;
if (remote->length != local_pilot->length
|| memcmp (local_pilot->record, remote->record, remote->length))
retval = 1;
if (retval == 0)
LOG (" equal");
else
LOG (" not equal");
g_free (local_pilot);
return retval;
}
static gint
add_record (GnomePilotConduitSyncAbs *conduit,
GnomePilotRecord *remote,
EToDoConduitContext *ctxt)
{
CalComponent *comp;
const char *uid;
int retval = 0;
g_return_val_if_fail (remote != NULL, -1);
LOG ("add_record: adding %s to desktop\n", print_remote (remote));
comp = comp_from_remote_record (conduit, remote, NULL);
update_comp (conduit, comp, ctxt);
cal_component_get_uid (comp, &uid);
e_pilot_map_insert (ctxt->map, remote->ID, uid, FALSE);
return retval;
}
static gint
add_archive_record (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
GnomePilotRecord *remote,
EToDoConduitContext *ctxt)
{
int retval = 0;
g_return_val_if_fail (remote != NULL, -1);
g_return_val_if_fail (local != NULL, -1);
LOG ("add_archive_record: doing nothing with %s\n",
print_local (local));
return retval;
}
static gint
replace_record (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
GnomePilotRecord *remote,
EToDoConduitContext *ctxt)
{
CalComponent *new_comp;
int retval = 0;
g_return_val_if_fail (remote != NULL, -1);
LOG ("replace_record: replace %s with %s\n",
print_local (local), print_remote (remote));
new_comp = comp_from_remote_record (conduit, remote, local->comp);
gtk_object_unref (GTK_OBJECT (local->comp));
local->comp = new_comp;
update_comp (conduit, local->comp, ctxt);
gtk_object_unref (GTK_OBJECT (new_comp));
return retval;
}
static gint
delete_record (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
EToDoConduitContext *ctxt)
{
const char *uid;
g_return_val_if_fail (local != NULL, -1);
g_assert (local->comp != NULL);
cal_component_get_uid (local->comp, &uid);
LOG ("delete_record: deleting %s\n", uid);
cal_client_remove_object (ctxt->client, uid);
return 0;
}
static gint
delete_archive_record (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
EToDoConduitContext *ctxt)
{
int retval =0;
g_return_val_if_fail(local!=NULL,-1);
LOG ("delete_archive_record: doing nothing\n");
return retval;
}
static gint
match (GnomePilotConduitSyncAbs *conduit,
GnomePilotRecord *remote,
EToDoLocalRecord **local,
EToDoConduitContext *ctxt)
{
const char *uid;
LOG ("match: looking for local copy of %s\n",
print_remote (remote));
g_return_val_if_fail (local != NULL, -1);
g_return_val_if_fail (remote != NULL, -1);
*local = NULL;
uid = e_pilot_map_lookup_uid (ctxt->map, remote->ID);
if (!uid)
return 0;
LOG (" matched\n");
*local = g_new0 (EToDoLocalRecord, 1);
local_record_from_uid (*local, uid, ctxt);
return 0;
}
static gint
free_match (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
EToDoConduitContext *ctxt)
{
LOG ("free_match: freeing\n");
g_return_val_if_fail (local != NULL, -1);
gtk_object_unref (GTK_OBJECT (local->comp));
g_free (local);
return 0;
}
static gint
prepare (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
GnomePilotRecord **remote,
EToDoConduitContext *ctxt)
{
LOG ("prepare: encoding local %s\n", print_local (local));
*remote = local_record_to_pilot_record (local, ctxt);
if (!*remote)
return -1;
return 0;
}
static gint
free_prepare (GnomePilotConduitSyncAbs *conduit,
EToDoLocalRecord *local,
GnomePilotRecord **remote,
EToDoConduitContext *ctxt)
{
LOG ("free_prepare: freeing\n");
g_return_val_if_fail (local != NULL, -1);
g_return_val_if_fail (remote != NULL, -1);
g_free (*remote);
*remote = NULL;
return 0;
}
static ORBit_MessageValidationResult
accept_all_cookies (CORBA_unsigned_long request_id,
CORBA_Principal *principal,
CORBA_char *operation)
{
/* allow ALL cookies */
return ORBIT_MESSAGE_ALLOW_ALL;
}
GnomePilotConduit *
conduit_get_gpilot_conduit (guint32 pilot_id)
{
GtkObject *retval;
EToDoConduitContext *ctxt;
LOG ("in todo's conduit_get_gpilot_conduit\n");
/* we need to find wombat with oaf, so make sure oaf
is initialized here. once the desktop is converted
to oaf and gpilotd is built with oaf, this can go away */
if (!oaf_is_initialized ()) {
char *argv[ 1 ] = {"hi"};
oaf_init (1, argv);
if (bonobo_init (CORBA_OBJECT_NIL,
CORBA_OBJECT_NIL,
CORBA_OBJECT_NIL) == FALSE)
g_error (_("Could not initialize Bonobo"));
ORBit_set_request_validation_handler (accept_all_cookies);
}
retval = gnome_pilot_conduit_sync_abs_new ("ToDoDB", 0x746F646F);
g_assert (retval != NULL);
gnome_pilot_conduit_construct (GNOME_PILOT_CONDUIT (retval),
"e_todo_conduit");
e_todo_context_new (&ctxt, pilot_id);
gtk_object_set_data (GTK_OBJECT (retval), "todoconduit_context", ctxt);
gtk_signal_connect (retval, "pre_sync", (GtkSignalFunc) pre_sync, ctxt);
gtk_signal_connect (retval, "post_sync", (GtkSignalFunc) post_sync, ctxt);
gtk_signal_connect (retval, "set_pilot_id", (GtkSignalFunc) set_pilot_id, ctxt);
gtk_signal_connect (retval, "for_each", (GtkSignalFunc) for_each, ctxt);
gtk_signal_connect (retval, "for_each_modified", (GtkSignalFunc) for_each_modified, ctxt);
gtk_signal_connect (retval, "compare", (GtkSignalFunc) compare, ctxt);
gtk_signal_connect (retval, "add_record", (GtkSignalFunc) add_record, ctxt);
gtk_signal_connect (retval, "add_archive_record", (GtkSignalFunc) add_archive_record, ctxt);
gtk_signal_connect (retval, "replace_record", (GtkSignalFunc) replace_record, ctxt);
gtk_signal_connect (retval, "delete_record", (GtkSignalFunc) delete_record, ctxt);
gtk_signal_connect (retval, "delete_archive_record", (GtkSignalFunc) delete_archive_record, ctxt);
gtk_signal_connect (retval, "match", (GtkSignalFunc) match, ctxt);
gtk_signal_connect (retval, "free_match", (GtkSignalFunc) free_match, ctxt);
gtk_signal_connect (retval, "prepare", (GtkSignalFunc) prepare, ctxt);
gtk_signal_connect (retval, "free_prepare", (GtkSignalFunc) free_prepare, ctxt);
return GNOME_PILOT_CONDUIT (retval);
}
void
conduit_destroy_gpilot_conduit (GnomePilotConduit *conduit)
{
EToDoConduitContext *ctxt;
ctxt = gtk_object_get_data (GTK_OBJECT (conduit),
"todoconduit_context");
e_todo_context_destroy (&ctxt);
gtk_object_destroy (GTK_OBJECT (conduit));
}
|