aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/conduits/todo/todo-conduit.c
blob: 3ba9506854eef4a8fc87679eba25994c26c549f5 (plain) (blame)
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include <config.h>
#include <sys/stat.h>
#include <utime.h>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>

#include <cal-client/cal-client.h>
// #include <cal-util/calobj.h>
#include <cal-util/timeutil.h>
#include <pi-source.h>
#include <pi-socket.h>
#include <pi-file.h>
#include <pi-dlp.h>
#include <libgnorba/gnorba.h>
#include <libgnorba/gnome-factory.h>
#include <pi-version.h>
#include <gpilotd/gnome-pilot-conduit.h>
#include <gpilotd/gnome-pilot-conduit-standard-abs.h>
#include <todo-conduit.h>
#include <libical/src/libical/icaltypes.h>

#include <bonobo.h>

//#include "GnomeCal.h"

GnomePilotConduit * conduit_get_gpilot_conduit (guint32);
void conduit_destroy_gpilot_conduit (GnomePilotConduit*);
void local_record_from_icalobject (GCalLocalRecord *local, CalComponent *obj);

#define CONDUIT_VERSION "0.8.11"
#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "todoconduit" 

#define DEBUG_CALCONDUIT 1
/* #undef DEBUG_CALCONDUIT */

#ifdef DEBUG_CALCONDUIT
#define show_exception(e) g_warning ("Exception: %s\n", CORBA_exception_id (e))
#define LOG(e...) g_log(G_LOG_DOMAIN,G_LOG_LEVEL_MESSAGE, e)
#else
#define show_exception(e)
#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)

#define catch_ret_val(_env,ret)                                                                 \
  if (_env._major != CORBA_NO_EXCEPTION) {                                                      \
        g_log(G_LOG_DOMAIN,G_LOG_LEVEL_MESSAGE,"%s:%d: Caught exception",__FILE__,__LINE__);    \
        g_warning ("Exception: %s\n", CORBA_exception_id (&(_env)));                               \
    CORBA_exception_free(&(_env));                                                             \
    return ret;                                                                             \
  }



/* debug spew DELETE ME */
static char *print_ical (CalComponent *obj
             /*iCalObject *obj*/)
{
    static char buff[ 4096 ];

    int indefinite;
    CalComponentDateTime dtend;
    int priority;
    struct icaltimetype *complete;
    CalComponentText summary;
    GSList *comments;
    CalComponentText *first_comment = NULL;

    if (obj == NULL) {
        sprintf (buff, "[NULL]");
        return buff;
    }

    indefinite = 0; /* FIX ME how do i get this */
    cal_component_get_dtend (obj, &dtend);
    priority = 1; /* FIX ME how do i get this */
    cal_component_get_completed (obj, &complete);

    cal_component_get_summary (obj, &summary);
    cal_component_get_comment_list (obj, &comments);
    if (comments)
        first_comment = (CalComponentText *) comments->data;

    sprintf (buff, "[%d %d-%d-%d %d %d-%d-%d '%s' '%s']",
         indefinite,
         dtend.value->year, dtend.value->month, dtend.value->day,
         priority,
         complete->year, complete->month, complete->day,
         summary.value,  /* description */
         first_comment ? first_comment->value : "" /* note */
         );
    return buff;
}


/* debug spew DELETE ME */
static char *print_local (GCalLocalRecord *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 print_ical (local->ical);
}


/* debug spew DELETE ME */
static char *print_remote (PilotRecord *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;
}




/* Destroys any data allocated by gcalconduit_load_configuration
   and deallocates the given configuration. */
static void 
gcalconduit_destroy_configuration(GCalConduitCfg **c) 
{
    g_return_if_fail(c!=NULL);
    g_return_if_fail(*c!=NULL);
    g_free(*c);
    *c = NULL;
}


/* Given a GCalConduitContxt*, allocates the structure */
static void
gcalconduit_new_context(GCalConduitContext **ctxt,
            GCalConduitCfg *c) 
{
    *ctxt = g_new0(GCalConduitContext,1);
    g_assert(ctxt!=NULL);
    (*ctxt)->cfg = c;
    CORBA_exception_init (&((*ctxt)->ev));
}


/* Destroys any data allocated by gcalconduit_new_context
   and deallocates its data. */
static void
gcalconduit_destroy_context(GCalConduitContext **ctxt)
{
    g_return_if_fail(ctxt!=NULL);
    g_return_if_fail(*ctxt!=NULL);
/*
    if ((*ctxt)->cfg!=NULL)
        gcalconduit_destroy_configuration(&((*ctxt)->cfg));
*/
    g_free(*ctxt);
    *ctxt = NULL;
}


static void
gnome_calendar_load_cb (GtkWidget *cal_client,
            CalClientLoadStatus status,
            GCalConduitContext *ctxt)
{
    CalClient *client = CAL_CLIENT (cal_client);

    LOG ("    todo-conduit entering gnome_calendar_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 = 1;
    }
}





static int
start_calendar_server (GnomePilotConduitStandardAbs *conduit,
               GCalConduitContext *ctxt)
{
    
    g_return_val_if_fail(conduit!=NULL,-2);
    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.vcf");

    gtk_signal_connect (GTK_OBJECT (ctxt->client), "cal_loaded",
                gnome_calendar_load_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;
}


#if 0
/* Just a stub to link with */
void calendar_notify (time_t time, CalendarAlarm *which, void *data);
void calendar_notify (time_t time, CalendarAlarm *which, void *data) { }
#endif /* 0 */


static GSList * 
get_calendar_objects(GnomePilotConduitStandardAbs *conduit,
             gboolean *status,
             GCalConduitContext *ctxt) 
{
    GList *uids;
    GSList *result = NULL;

    g_return_val_if_fail (conduit != NULL, NULL);
    g_return_val_if_fail (ctxt != NULL, NULL);

    uids = cal_client_get_uids (ctxt->client, CALOBJ_TYPE_TODO);

    // LOG ("got %d todo entries from cal server\n", g_list_length (uids));

    if (status != NULL)
        (*status) = TRUE;

    if (! uids)
        INFO ("No entries found");
    else {
        GList *c;
        for (c=uids; c; c=c->next)
            result = g_slist_prepend (result, (gchar *) c->data);
        /* FIX ME free uids */
    }

    return result;
}


static void 
local_record_from_ical_uid (GCalLocalRecord *local,
                char *uid,
                GCalConduitContext *ctxt)
{
    //iCalObject *obj;
    CalComponent *obj;
    CalClientGetStatus status;

    g_assert(local!=NULL);

    status = cal_client_get_object (ctxt->client, uid, &obj);

    if (status == CAL_CLIENT_GET_SUCCESS)
        local_record_from_icalobject (local, obj);
    else
        INFO ("Object did not exist");
}



static char *gnome_pilot_status_to_string (gint status)
{
    switch(status) {
    case GnomePilotRecordPending: return "GnomePilotRecordPending";
    case GnomePilotRecordNothing: return "GnomePilotRecordNothing";
    case GnomePilotRecordDeleted: return "GnomePilotRecordDeleted";
    case GnomePilotRecordNew: return "GnomePilotRecordNew";
    case GnomePilotRecordModified: return "GnomePilotRecordModified";
    }

    return "Unknown";
}



/*
 * converts a iCalObject to a GCalLocalRecord
 */

void
local_record_from_icalobject(GCalLocalRecord *local,
                 CalComponent *obj) 
{
    //iCalPilotState pilot_status;
    unsigned long int pilot_status;

    g_return_if_fail(local!=NULL);
    g_return_if_fail(obj!=NULL);

    local->ical = obj;
    local->todo = NULL; /* ??? */
    cal_component_get_pilot_id (obj, &local->local.ID);
    cal_component_get_pilot_status (obj, &pilot_status);

    switch (pilot_status) {
    case ICAL_PILOT_SYNC_NONE: 
        local->local.attr = GnomePilotRecordNothing;
        break;
    case ICAL_PILOT_SYNC_MOD: 
        local->local.attr = GnomePilotRecordModified;
        break;
    case ICAL_PILOT_SYNC_DEL: 
        local->local.attr = GnomePilotRecordDeleted; 
        break;
    default:
        g_warning ("unhandled pilot status: %ld\n", pilot_status);
    }

    /* Records without a pilot_id are new */
    if(local->local.ID == 0)
        local->local.attr = GnomePilotRecordNew;

    /*
    local->local.secret = 0;
    if (obj->class!=NULL)
        if (strcmp(obj->class,"PRIVATE")==0)
            local->local.secret = 1;
    */

    local->local.archived = 0;  
}


/*
 * Given a PilotRecord, find the matching record in
 * the calendar repository. If no match, return NULL
 */
static GCalLocalRecord *
find_record_in_repository(GnomePilotConduitStandardAbs *conduit,
              PilotRecord *remote,
              GCalConduitContext *ctxt) 
{
    char *uid = NULL;
    GCalLocalRecord *loc;
    CalClientGetStatus status;
    //iCalObject *obj;
    CalComponent *obj;
  
    g_return_val_if_fail(conduit!=NULL,NULL);
    g_return_val_if_fail(remote!=NULL,NULL);

    LOG ("find_record_in_repository: remote=%s... ",
        print_remote (remote));

    // LOG ("requesting %ld", remote->ID);

    status = cal_client_get_uid_by_pilot_id (ctxt->client, remote->ID, &uid);

    if (status == CAL_CLIENT_GET_SUCCESS) {
        status = cal_client_get_object (ctxt->client, uid, &obj);
        if (status == CAL_CLIENT_GET_SUCCESS) {
            LOG ("found %s\n", print_ical (obj));
            loc = g_new0(GCalLocalRecord,1);
            /* memory allocated in new_from_string is freed in free_match */
            local_record_from_icalobject (loc, obj);
            return loc;
        }
    }

    // INFO ("Object did not exist");
    LOG ("not found\n");
    return NULL;
}


/* 
 * updates an given iCalObject in the repository
 */
static void
update_calendar_entry_in_repository(GnomePilotConduitStandardAbs *conduit,
                    CalComponent *obj,
                    GCalConduitContext *ctxt) 
{
    gboolean success;

    g_return_if_fail (conduit!=NULL);
    g_return_if_fail (obj!=NULL);

    LOG ("        update_calendar_entry_in_repository "
        "saving %s to desktop\n",
        print_ical (obj));

    success = cal_client_update_object (ctxt->client, obj);

    if (! success) {
        WARN (_("Error while communicating with calendar server"));
    }
}


static CalComponent *
ical_from_remote_record (GnomePilotConduitStandardAbs *conduit,
             PilotRecord *remote,
             CalComponent *in_obj)
{
    CalComponent *obj;
    struct ToDo todo;
    struct icaltimetype now = icaltime_from_timet (time (NULL), FALSE, FALSE);

    CalComponentText summary = {NULL, NULL};
    CalComponentText comment = {NULL, NULL};
    GSList *comment_list;

    g_return_val_if_fail(remote!=NULL,NULL);
    memset (&todo, 0, sizeof (struct ToDo));
    unpack_ToDo (&todo, remote->record, remote->length);

    LOG ("        ical_from_remote_record: "
        "merging remote %s into local %s\n",
        print_remote (remote), print_ical (in_obj));
    
    if (in_obj == NULL) {
        obj = cal_component_new ();
    } else {
        obj = in_obj;
    }

    summary.value = todo.description;
    cal_component_set_summary (obj, &summary);

    comment.value = todo.note;
    comment_list = g_slist_append (NULL, &comment);
    cal_component_set_comment_list (obj, comment_list);
    g_slist_free (comment_list);

    cal_component_set_new_vtype (obj, CAL_COMPONENT_TODO);
    // obj->new = TRUE;
    // obj->created = now;
    cal_component_set_created (obj, &now);
    // obj->last_mod = now;
    cal_component_set_last_modified (obj, &now);
    /* obj->priority = 0; */
    // obj->transp = 0;
    cal_component_set_transparency (obj,
                    /*CalComponentTransparency transp*/
                    CAL_COMPONENT_TRANSP_NONE);
    /* obj->related = NULL; */

    // cal_component_set_pilot_status (obj, ICAL_PILOT_SYNC_NONE);

    /*
     * Begin and end
     */

    // obj->dtend = mktime (& todo.due);    
    {
        /* do i need to malloc these?  FIX ME */
        struct icaltimetype dtend_ictt;
        CalComponentDateTime dtend;

        dtend_ictt = icaltime_from_timet (mktime (& todo.due), FALSE, FALSE);
        dtend.value = &dtend_ictt;
        dtend.tzid = NULL;
        cal_component_set_dtend (obj, &dtend);
    }


    /*
    if (todo.complete) {
        obj->completed = now-5;
        obj->percent = 100;
    }
    */
    {
        cal_component_set_completed (obj, &now);
    }

    /*
    LOG ("[%s] from pilot, complete=%d/%ld\n",
        todo.description,
        todo.complete,
        obj->completed);
    */


    //obj->priority = todo.priority; FIX ME

    /* g_free (obj->class); */

    /*
    if (remote->attr & dlpRecAttrSecret)
        obj->class = g_strdup ("PRIVATE");
    else
        obj->class = g_strdup ("PUBLIC");
    */

    if (remote->attr & dlpRecAttrSecret)
        cal_component_set_classification (obj, CAL_COMPONENT_CLASS_PRIVATE);
    else
        cal_component_set_classification (obj, CAL_COMPONENT_CLASS_PUBLIC);

    free_ToDo(&todo);

    return obj;
}


/* Code blatantly stolen from
 * calendar-pilot-sync.c:
 *   
 * (C) 1999 International GNOME Support
 *
 * Author:
 *   Miguel de Icaza (miguel@gnome-support.com)
 *
 * store a copy of a pilot record in the desktop database
 *
 */
static gint
update_record (GnomePilotConduitStandardAbs *conduit,
           PilotRecord *remote,
           GCalConduitContext *ctxt)
{
    //iCalObject *obj;
    CalComponent *obj;
    struct ToDo todo;
    CalClientGetStatus status;
    char *uid;

    CalComponentText summary = {NULL, NULL};
    CalComponentText comment = {NULL, NULL};
    GSList *comment_list;

    g_return_val_if_fail(remote!=NULL,-1);

    memset (&todo, 0, sizeof (struct ToDo));
    unpack_ToDo (&todo, remote->record, remote->length);

    LOG ("    cal_client_get_uid_by_pilot_id... ");

    status = cal_client_get_uid_by_pilot_id (ctxt->client,
                         remote->ID, &uid);
    if (status == CAL_CLIENT_GET_SUCCESS) {
        LOG (" succeeded with '%s'\n", uid);
        LOG ("    cal_client_get_object... ");
        status = cal_client_get_object (ctxt->client, uid, &obj);
    }

    if (status != CAL_CLIENT_GET_SUCCESS) {
        struct icaltimetype now = icaltime_from_timet (time (NULL), FALSE, FALSE);

        LOG ("failed, making a new one.\n");

        obj = cal_component_new ();

        summary.value = todo.description;
        cal_component_set_summary (obj, &summary);

        comment.value = todo.note;
        comment_list = g_slist_append (NULL, &comment);
        cal_component_set_comment_list (obj, comment_list);
        g_slist_free (comment_list);

        // obj->type = ICAL_TODO;
        cal_component_set_new_vtype (obj, CAL_COMPONENT_TODO);
        // obj->new = TRUE;
        // obj->created = now;
        cal_component_set_created (obj, &now);
        // obj->last_mod = now;
        cal_component_set_last_modified (obj, &now);
        // obj->priority = 0;
        // obj->transp = 0;
        cal_component_set_transparency (obj,
                        /*CalComponentTransparency transp*/
                        CAL_COMPONENT_TRANSP_NONE);
        // obj->related = NULL;
        //obj->pilot_id = remote->ID;
        //obj->pilot_status = ICAL_PILOT_SYNC_NONE;
        cal_component_set_pilot_id (obj, remote->ID);
        cal_component_set_pilot_status (obj, ICAL_PILOT_SYNC_NONE);
    } else {
        CalComponent *new_obj;

        LOG ("succeeded %s\n", print_ical (obj));

        new_obj = ical_from_remote_record (conduit, remote, obj);
        obj = new_obj;
    }

    /* update record on server */
    {
        const char *uid;
        unsigned long pilot_id;

        cal_component_get_uid (obj, &uid);
        cal_component_get_pilot_id (obj, &pilot_id);

        update_calendar_entry_in_repository (conduit, obj, ctxt);
        cal_client_update_pilot_id (ctxt->client, (char *) uid, pilot_id,
                        ICAL_PILOT_SYNC_NONE);
    }

    /*
     * Shutdown
     */
    //ical_object_unref (obj);
    free_ToDo(&todo);

    return 0;
}

static void
check_for_slow_setting (GnomePilotConduit *c, GCalConduitContext *ctxt)
{
    GList *uids;
    unsigned long int entry_number;

    uids = cal_client_get_uids (ctxt->client, CALOBJ_TYPE_TODO);

    entry_number = g_list_length (uids);

    /* If the local base is empty, do a slow sync */
    if (entry_number == 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");
    }
}

static gint
pre_sync (GnomePilotConduit *c,
      GnomePilotDBInfo *dbi,
      GCalConduitContext *ctxt)
{
    int l;
    unsigned char *buf;
    GnomePilotConduitStandardAbs *conduit;
    /* gint num_records; */
    //GList *uids;

    /*
    g_log_set_always_fatal (G_LOG_LEVEL_ERROR |
                G_LOG_LEVEL_CRITICAL |
                G_LOG_LEVEL_WARNING);
    */


    conduit = GNOME_PILOT_CONDUIT_STANDARD_ABS(c);

    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 (GNOME_PILOT_CONDUIT_STANDARD_ABS(c), ctxt) != 0) {
        WARN(_("Could not start gnomecal server"));
        gnome_pilot_conduit_error(GNOME_PILOT_CONDUIT(c),
                      _("Could not start gnomecal server"));
        return -1;
    }


#if 0  
    /* Set the counters for the progress bar crap */
    num_records = GNOME_Calendar_Repository_get_number_of_objects (ctxt->calendar, GNOME_Calendar_Repository_ANY, &(ctxt->ev));

    catch_ret_val (ctxt->ev, -1);
    gnome_pilot_conduit_standard_abs_set_num_local_records(GNOME_PILOT_CONDUIT_STANDARD_ABS(c), num_records);
    num_records = GNOME_Calendar_Repository_get_number_of_objects (ctxt->calendar, GNOME_Calendar_Repository_MODIFIED, &(ctxt->ev));
    catch_ret_val (ctxt->ev, -1);
    gnome_pilot_conduit_standard_abs_set_num_updated_local_records(GNOME_PILOT_CONDUIT_STANDARD_ABS(c), num_records);
    num_records = GNOME_Calendar_Repository_get_number_of_objects (ctxt->calendar, GNOME_Calendar_Repository_NEW, &(ctxt->ev));
    catch_ret_val (ctxt->ev, -1);
    gnome_pilot_conduit_standard_abs_set_num_new_local_records(GNOME_PILOT_CONDUIT_STANDARD_ABS(c), num_records);
    num_records = GNOME_Calendar_Repository_get_number_of_objects (ctxt->calendar, GNOME_Calendar_Repository_DELETED, &(ctxt->ev));
    catch_ret_val (ctxt->ev, -1);
    gnome_pilot_conduit_standard_abs_set_num_deleted_local_records(GNOME_PILOT_CONDUIT_STANDARD_ABS(c), num_records);
#endif /* 0 */

    gtk_object_set_data(GTK_OBJECT(c),"dbinfo",dbi);
  
    /* load_records(c); */

    buf = (unsigned char*)g_malloc(0xffff);
    if((l=dlp_ReadAppBlock(dbi->pilot_socket,dbi->db_handle,0,(unsigned char *)buf,0xffff)) < 0) {
        WARN(_("Could not read pilot's ToDo application block"));
        WARN("dlp_ReadAppBlock(...) = %d",l);
        gnome_pilot_conduit_error(GNOME_PILOT_CONDUIT(c),
                 _("Could not read pilot's ToDo application block"));
        return -1;
    }
    unpack_ToDoAppInfo(&(ctxt->ai),buf,l);
    g_free(buf);

    check_for_slow_setting(c,ctxt);

    return 0;
}

/**
 * Find (if possible) the local record which matches
 * the given PilotRecord.
 * if successfull, return non-zero and set *local to
 * a non-null value (the located local record),
 * otherwise return 0 and set *local = NULL;
 */

static gint
match_record    (GnomePilotConduitStandardAbs *conduit,
         GCalLocalRecord **local,
         PilotRecord *remote,
         GCalConduitContext *ctxt)
{
    LOG ("match_record: 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 = find_record_in_repository(conduit,remote,ctxt);

    if (*local == NULL)
        LOG ("    match_record: not found.\n");
    else
        LOG ("    match_record: found, %s\n", print_local (*local));
  
    if (*local==NULL) return -1;
    return 0;
}

/**
 * Free the data allocated by a previous match_record call.
 * If successfull, return non-zero and ser *local=NULL, otherwise
 * return 0.
 */
static gint
free_match  (GnomePilotConduitStandardAbs *conduit,
         GCalLocalRecord **local,
         GCalConduitContext *ctxt)
{
    LOG ("free_match: %s\n", print_local (*local));

    g_return_val_if_fail (local!=NULL, -1);
    g_return_val_if_fail (*local!=NULL, -1);

    // ical_object_unref (GCAL_LOCALRECORD(*local)->ical); 
    g_free (*local);

        *local = NULL;
    return 0;
}

/*
  Move to archive and set status to Nothing
 */
static gint
archive_local (GnomePilotConduitStandardAbs *conduit,
           GCalLocalRecord *local,
           GCalConduitContext *ctxt)
{
    LOG ("archive_local: doing nothing with %s\n", print_local (local));

    g_return_val_if_fail(local!=NULL,-1);

    return -1;
}

/*
  Store in archive and set status to Nothing
 */
static gint
archive_remote (GnomePilotConduitStandardAbs *conduit,
        GCalLocalRecord *local,
        PilotRecord *remote,
        GCalConduitContext *ctxt)
{
    LOG ("archive_remote: doing nothing with %s\n",
        print_local (local));

        //g_return_val_if_fail(remote!=NULL,-1);
    //g_return_val_if_fail(local!=NULL,-1);

    return -1;
}

/*
  Store and set status to Nothing
 */
static gint
store_remote (GnomePilotConduitStandardAbs *conduit,
          PilotRecord *remote,
          GCalConduitContext *ctxt)
{
    LOG ("store_remote: copying pilot record %s to desktop\n",
        print_remote (remote));

    g_return_val_if_fail(remote!=NULL,-1);
    remote->attr = GnomePilotRecordNothing;

    return update_record(conduit,remote,ctxt);
}

static gint
clear_status_archive_local (GnomePilotConduitStandardAbs *conduit,
                GCalLocalRecord *local,
                GCalConduitContext *ctxt)
{
    LOG ("clear_status_archive_local: doing nothing\n");

    g_return_val_if_fail(local!=NULL,-1);

        return -1;
}

static gint
iterate (GnomePilotConduitStandardAbs *conduit,
     GCalLocalRecord **local,
     GCalConduitContext *ctxt)
{
    static GSList *events,*iterator;
    static int hest;

    g_return_val_if_fail(local!=NULL,-1);

    if(*local==NULL) {
        // LOG ("beginning iteration");

        events = get_calendar_objects(conduit,NULL,ctxt);
        hest = 0;
        
        if(events!=NULL) {
            // LOG ("iterating over %d records", g_slist_length (events));
            *local = g_new0(GCalLocalRecord,1);

            local_record_from_ical_uid(*local,(gchar*)events->data,ctxt);
            iterator = events;
        } else {
            // LOG ("no events");
            (*local) = NULL;
        }
    } else {
        /* LOG ("continuing iteration\n"); */
        hest++;
        if(g_slist_next(iterator)==NULL) {
            GSList *l;

            // LOG ("ending");
            /** free stuff allocated for iteration */
            g_free((*local));

            // LOG ("iterated over %d records", hest);
            for(l=events;l;l=l->next)
                g_free(l->data);

            g_slist_free(events);

            /* ends iteration */
            (*local) = NULL;
            return 0;
        } else {
            iterator = g_slist_next(iterator);
            local_record_from_ical_uid(*local,(gchar*)(iterator->data),ctxt);
        }
    }
    return 1;
}


static gint
iterate_specific (GnomePilotConduitStandardAbs *conduit,
          GCalLocalRecord **local,
          gint flag,
          gint archived,
          GCalConduitContext *ctxt)
{
    (*local) = NULL; /* ??? */

    /* debugging */
    {
        gchar *tmp;
        switch (flag) {
        case GnomePilotRecordNothing:
            tmp = g_strdup("RecordNothing"); break;
        case GnomePilotRecordModified:
            tmp = g_strdup("RecordModified"); break;
        case GnomePilotRecordDeleted:
            tmp = g_strdup("RecordDeleted"); break;
        case GnomePilotRecordNew:
            tmp = g_strdup("RecordNew"); break;
        default: tmp = g_strdup_printf("0x%x",flag); break;
        }
        LOG ("\niterate_specific: (flag = %s)... ", tmp);
        g_free(tmp);
    }

    g_return_val_if_fail(local!=NULL,-1);

    /* iterate until a record meets the criteria */
    while (gnome_pilot_conduit_standard_abs_iterate (conduit,
                              (LocalRecord**)local)) {
        if((*local)==NULL) break;
        if(archived && ((*local)->local.archived==archived)) break;
        if(((*local)->local.attr == flag)) break;
    }

    if ((*local)) {
        LOG (" found %s\n", print_local (*local));
    } else {
        LOG (" no more found.\n");
    }

    return (*local)==NULL?0:1;
}

static gint
purge (GnomePilotConduitStandardAbs *conduit,
       GCalConduitContext *ctxt)
{
    LOG ("purge: doing nothing\n");

    /* HEST, gem posterne her */

    return -1;
}


static gint
set_status (GnomePilotConduitStandardAbs *conduit,
        GCalLocalRecord *local,
        gint status,
        GCalConduitContext *ctxt)
{
    gboolean success;
    iCalPilotState new_state = ICAL_PILOT_SYNC_NONE;

    LOG ("set_status: %s status is now '%s'\n",
        print_local (local),
        gnome_pilot_status_to_string (status));

    g_return_val_if_fail(local!=NULL,-1);
    g_assert(local->ical!=NULL);
    
    local->local.attr = status;
    switch(status) {
    case GnomePilotRecordPending:
    case GnomePilotRecordNothing:
        new_state = ICAL_PILOT_SYNC_NONE;
        break;
    case GnomePilotRecordDeleted:
        break;
    case GnomePilotRecordNew:
    case GnomePilotRecordModified:
        new_state = ICAL_PILOT_SYNC_MOD;
        break;    
    }
    
    if (status == GnomePilotRecordDeleted) {
        const char *uid;
        cal_component_get_uid (local->ical, &uid);
        success = cal_client_remove_object (ctxt->client, uid);
    } else {
        const char *uid;
        unsigned long pilot_id;

        cal_component_get_uid (local->ical, &uid);
        cal_component_get_pilot_id (local->ical, &pilot_id);

        success = cal_client_update_object (ctxt->client, local->ical);
        cal_client_update_pilot_id (ctxt->client, (char *) uid,
                        pilot_id, new_state);
    }

    if (! success) {
        WARN (_("Error while communicating with calendar server"));
    }
    
        return 0;
}

static gint
set_archived (GnomePilotConduitStandardAbs *conduit,
          GCalLocalRecord *local,
          gint archived,
          GCalConduitContext *ctxt)
{
    LOG ("set_archived: %s archived flag is now '%d'\n",
        print_local (local), archived);

    g_return_val_if_fail(local!=NULL,-1);
    g_assert(local->ical!=NULL);

    local->local.archived = archived;
    update_calendar_entry_in_repository(conduit,local->ical,ctxt);
    /* FIXME: This should move the entry into a speciel
       calendar file, eg. Archive, or (by config option), simply
       delete it */
        return 0;
}

static gint
set_pilot_id (GnomePilotConduitStandardAbs *conduit,
          GCalLocalRecord *local,
          guint32 ID,
          GCalConduitContext *ctxt)
{
    const char *uid;
    unsigned long int pilot_status;

    LOG ("set_pilot_id: %s pilot ID is now '%d'\n",
        print_local (local), ID);

    g_return_val_if_fail(local!=NULL,-1);
    g_assert(local->ical!=NULL);

    local->local.ID = ID;
    cal_component_set_pilot_id (local->ical, ID);

    cal_component_get_uid (local->ical, &uid);
    cal_component_get_pilot_status (local->ical, &pilot_status);

    cal_client_update_pilot_id (ctxt->client,
                    (char *) uid,
                    local->local.ID,
                    pilot_status);

        return 0;
}

static gint
transmit (GnomePilotConduitStandardAbs *conduit,
      GCalLocalRecord *local,
      PilotRecord **remote,
      GCalConduitContext *ctxt)
{
    PilotRecord *p;
    /* priority; FIX ME */
    struct icaltimetype *completed;
    CalComponentText summary;
    GSList *comment_list = NULL;
    CalComponentText *comment;

    LOG ("transmit: encoding local %s\n", print_local (local));

    g_return_val_if_fail(local!=NULL,-1);
    g_return_val_if_fail(remote!=NULL,-1);
    g_assert(local->ical!=NULL);

    p = g_new0(PilotRecord,1);

    p->ID = local->local.ID;
    p->attr = local->local.attr;
    p->archived = local->local.archived;
    p->secret = local->local.secret;

    local->todo = g_new0(struct ToDo,1);

    {
        CalComponentDateTime dtend;
        time_t dtend_time_t;

        cal_component_get_dtend (local->ical, &dtend);
        dtend_time_t = icaltime_as_timet (*dtend.value);

        local->todo->due = *localtime (&dtend_time_t);
        local->todo->indefinite = (dtend.value->year == 0);
    }

    //local->todo->priority = local->ical->priority;
    local->todo->priority = 1; /* FIX ME */

    cal_component_get_completed (local->ical, &completed);
    if (completed->year > 0)
        local->todo->complete = 1; /* FIX ME */

    /* STOP: don't replace these with g_strdup, since free_ToDo
       uses free to deallocte */

    cal_component_get_summary (local->ical, &summary);
    local->todo->description = 
        //local->ical->summary == NULL ? NULL : strdup (summary.value);
        strdup ((char *) summary.value);


    cal_component_get_comment_list (local->ical, &comment_list);
    if (comment_list) {
        comment = (CalComponentText *) comment_list->data;
        if (comment && comment->value)
            local->todo->note = strdup (comment->value);
        else
            local->todo->note = NULL;
    } else {
        local->todo->note = NULL;
    }

    /*
    LOG ("transmitting todo to pilot [%s] complete=%d/%ld\n",
        local->ical->summary==NULL?"NULL":local->ical->summary,
        local->todo->complete, local->ical->completed);
    */

    /* Generate pilot record structure */
    p->record = g_new0(char,0xffff);
    p->length = pack_ToDo(local->todo,p->record,0xffff);

    *remote = p;

    return 0;
}

static gint
free_transmit (GnomePilotConduitStandardAbs *conduit,
           GCalLocalRecord *local,
           PilotRecord **remote,
           GCalConduitContext *ctxt)
{
    LOG ("free_transmit: freeing %s\n",
        print_local (local));

    g_return_val_if_fail(local!=NULL,-1);
    g_return_val_if_fail(remote!=NULL,-1);

    /* free_ToDo(local->todo); */ /* FIX ME is this needed? */
    g_free((*remote)->record);
    *remote = NULL;
        return 0;
}


static gint
compare (GnomePilotConduitStandardAbs *conduit,
        GCalLocalRecord *local,
        PilotRecord *remote,
        GCalConduitContext *ctxt)
{
    /* used by the quick compare */
    PilotRecord *remoteOfLocal;
    int err;
    int retval;

    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);

    err = transmit(conduit,local,&remoteOfLocal,ctxt);
    if (err != 0) return err;

    retval = 0;
    if (remote->length == remoteOfLocal->length) {
        if  (memcmp (remoteOfLocal->record,
                 remote->record, remote->length)!=0) {
            LOG ("    compare failed on contents\n");
            retval = 1;
        }
    } else {
        LOG("    compare failed on length\n");
        retval = 1;
    }


    if (retval == 0) {
        LOG ("    match.\n");
    } else {
        /* debug spew */
        LOG ("        local:%s\n", print_remote (remoteOfLocal));
        LOG ("        remote:%s\n", print_remote (remote));
    }

    free_transmit(conduit,local,&remoteOfLocal,ctxt);   
    return retval;
}


static gint
compare_backup (GnomePilotConduitStandardAbs *conduit,
        GCalLocalRecord *local,
        PilotRecord *remote,
        GCalConduitContext *ctxt)
{
    LOG ("compare_backup: doing nothing\n");

    g_return_val_if_fail(local!=NULL,-1);
    g_return_val_if_fail(remote!=NULL,-1);

        return -1;
}


static gint
delete_all (GnomePilotConduitStandardAbs *conduit,
        GCalConduitContext *ctxt)
{
    GSList *events,*it;
    gboolean error;
    gboolean success;

    events = get_calendar_objects(conduit,&error,ctxt);

    LOG ("delete_all: deleting all objects from desktop\n");

    if (error == FALSE) return -1;
    for (it=events; it; it = g_slist_next (it)) {
        success = cal_client_remove_object (ctxt->client, it->data);

        if (!success)
            INFO ("Object did not exist");

        g_free (it->data);
    }

    g_slist_free (events);
        return -1;
}


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 pilotId)
{
    GtkObject *retval;
    GCalConduitCfg *cfg;
    GCalConduitContext *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_standard_abs_new ("ToDoDB", 0x746F646F);
    g_assert (retval != NULL);
    gnome_pilot_conduit_construct(GNOME_PILOT_CONDUIT(retval),"ToDoConduit");

    gcalconduit_load_configuration(&cfg,pilotId);
    gtk_object_set_data(retval,"todoconduit_cfg",cfg);

    gcalconduit_new_context(&ctxt,cfg);
    gtk_object_set_data(GTK_OBJECT(retval),"todoconduit_context",ctxt);

    gtk_signal_connect (retval, "match_record", (GtkSignalFunc) match_record, ctxt);
    gtk_signal_connect (retval, "free_match", (GtkSignalFunc) free_match, ctxt);
    gtk_signal_connect (retval, "archive_local", (GtkSignalFunc) archive_local, ctxt);
    gtk_signal_connect (retval, "archive_remote", (GtkSignalFunc) archive_remote, ctxt);
    gtk_signal_connect (retval, "store_remote", (GtkSignalFunc) store_remote, ctxt);
    gtk_signal_connect (retval, "clear_status_archive_local", (GtkSignalFunc) clear_status_archive_local, ctxt);
    gtk_signal_connect (retval, "iterate", (GtkSignalFunc) iterate, ctxt);
    gtk_signal_connect (retval, "iterate_specific", (GtkSignalFunc) iterate_specific, ctxt);
    gtk_signal_connect (retval, "purge", (GtkSignalFunc) purge, ctxt);
    gtk_signal_connect (retval, "set_status", (GtkSignalFunc) set_status, ctxt);
    gtk_signal_connect (retval, "set_archived", (GtkSignalFunc) set_archived, ctxt);
    gtk_signal_connect (retval, "set_pilot_id", (GtkSignalFunc) set_pilot_id, ctxt);
    gtk_signal_connect (retval, "compare", (GtkSignalFunc) compare, ctxt);
    gtk_signal_connect (retval, "compare_backup", (GtkSignalFunc) compare_backup, ctxt);
    gtk_signal_connect (retval, "free_transmit", (GtkSignalFunc) free_transmit, ctxt);
    gtk_signal_connect (retval, "delete_all", (GtkSignalFunc) delete_all, ctxt);
    gtk_signal_connect (retval, "transmit", (GtkSignalFunc) transmit, ctxt);
    gtk_signal_connect (retval, "pre_sync", (GtkSignalFunc) pre_sync, ctxt);

    return GNOME_PILOT_CONDUIT (retval);
}

void
conduit_destroy_gpilot_conduit (GnomePilotConduit *conduit)
{ 
        GCalConduitCfg *cc;
    GCalConduitContext *ctxt;

        cc = GET_GCALCONFIG(conduit);
    ctxt = GET_GCALCONTEXT(conduit);

    if (ctxt->client != NULL) {
        gtk_object_unref (GTK_OBJECT (ctxt->client));
        //pi_close (ctxt->link);
        //GNOME_Calendar_Repository_done (ctxt->calendar, &(ctxt->ev));
    }

        gcalconduit_destroy_configuration (&cc);

    gcalconduit_destroy_context (&ctxt);

    gtk_object_destroy (GTK_OBJECT (conduit));
}