aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/conduit/address-conduit.c
blob: 3c6ccf573bea10d089079f8b5889186a1cc82402 (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
/* -*- 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 <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 <address-conduit.h>
#include <libversit/vcc.h>
#include "ebook/e-book-types.h"

GnomePilotConduit * conduit_get_gpilot_conduit (guint32);
void conduit_destroy_gpilot_conduit (GnomePilotConduit*);
void local_record_from_ecard (AddressbookLocalRecord *local, ECard *ecard);

#define CONDUIT_VERSION "0.1"
#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "addressconduit" 

#define DEBUG_CALCONDUIT 1
/* #undef DEBUG_CALCONDUIT */

#ifdef DEBUG_ADDRESSBOOKCONDUIT
#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;                                                                             \
  }


static void
status_cb (EBook *ebook, EBookStatus status, gpointer closure)
{
    (*(EBookStatus*)closure) = status;
    gtk_main_quit();
}


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


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


/* Destroys any data allocated by conduit_new_context
   and deallocates its data. */
static void
conduit_destroy_context(AddressbookConduitContext **ctxt)
{
    g_return_if_fail(ctxt!=NULL);
    g_return_if_fail(*ctxt!=NULL);

    if ((*ctxt)->cfg!=NULL)
        conduit_destroy_configuration(&((*ctxt)->cfg));

    g_free(*ctxt);
    *ctxt = NULL;
}


static void
cursor_cb (EBook *book, EBookStatus status, ECardCursor *cursor, gpointer closure)
{
    AddressbookConduitContext *ctxt = (AddressbookConduitContext*)closure;

    if (status == E_BOOK_STATUS_SUCCESS) {
        long length;
        int i;

        ctxt->cursor = cursor;
        ctxt->address_load_success = TRUE;

        length = e_card_cursor_get_length (cursor);
        ctxt->cards = NULL;
        for (i = 0; i < length; i ++)
            ctxt->cards = g_list_append (ctxt->cards, e_card_cursor_get_nth (cursor, i));
            
        gtk_main_quit(); /* end the sub event loop */
    }
    else {
        WARN (_("BLARG\n"));
        gtk_main_quit(); /* end the sub event loop */
    }
}

static void
book_open_cb (EBook *book, EBookStatus status, gpointer closure)
{
    AddressbookConduitContext *ctxt = (AddressbookConduitContext*)closure;

    if (status == E_BOOK_STATUS_SUCCESS) {
        e_book_get_cursor (book, "(contains \"full_name\" \"\")", cursor_cb, ctxt);
    }
    else {
        WARN (_("BLARG\n"));
        gtk_main_quit(); /* end the sub event loop */
    }
}

static int
start_address_server (GnomePilotConduitStandardAbs *conduit,
              AddressbookConduitContext *ctxt)
{
    
    g_return_val_if_fail(conduit!=NULL,-2);
    g_return_val_if_fail(ctxt!=NULL,-2);

    ctxt->ebook = e_book_new ();

    e_book_load_uri (ctxt->ebook, "file:/home/toshok/evolution/local/Contacts", book_open_cb, ctxt);

    /* run a sub event loop to turn ebook's async loading into a
           synchronous call */
    gtk_main ();

    if (ctxt->address_load_success)
        return 0;

    return -1;
}

/*
 * converts a ECard to a AddressbookLocalRecord
 */
void
local_record_from_ecard(AddressbookLocalRecord *local,
            ECard *ecard)
{
    g_return_if_fail(local!=NULL);
    g_return_if_fail(ecard!=NULL);

    local->ecard = ecard;
    local->local.ID = local->ecard->pilot_id;
#if 0
    LOG ("local->Id = %ld [%s], status = %d",
          local->local.ID,obj->summary,local->ical->pilot_status);

    switch(local->ical->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;
    }
#endif

    local->local.attr = GnomePilotRecordNothing;

    /* Records without a pilot_id are new */
    if(local->local.ID == 0) 
        local->local.attr = GnomePilotRecordNew; 
  
    local->local.secret = 0;
#if 0
    if(obj->class!=NULL) 
        if(strcmp(obj->class,"PRIVATE")==0)
            local->local.secret = 1;
#endif
 
    local->local.archived = 0;  
}

static ECard *
get_ecard_by_pilot_id (GList *card_list, recordid_t id)
{
    GList *l;

    for (l = card_list; l; l = l->next) {
        guint32 pilot_id;
        ECard *card = l->data;

        if (!card)
            continue;

        gtk_object_get (GTK_OBJECT(card), "pilot_id", &pilot_id);

        if (pilot_id == id)
            return card;
    }

    return NULL;
}

/*
 * Given a PilotRecord, find the matching record in
 * the addressbook. If no match, return NULL
 */
static AddressbookLocalRecord *
find_record_in_ebook(GnomePilotConduitStandardAbs *conduit,
             PilotRecord *remote,
             AddressbookConduitContext *ctxt) 
{
    AddressbookLocalRecord *loc;
    ECard *ecard;
  
    g_return_val_if_fail(conduit!=NULL,NULL);
    g_return_val_if_fail(remote!=NULL,NULL);
  
    LOG ("requesting %ld", remote->ID);

    ecard = get_ecard_by_pilot_id (ctxt->cards, remote->ID);

    if (NULL != ecard) {
        LOG ("Found");
        loc = g_new0(AddressbookLocalRecord,1);
        /* memory allocated in new_from_string is freed in free_match */
        local_record_from_ecard (loc, ecard);
        return loc;
    }

    INFO ("Object did not exist");
    return NULL;
}

static ECard *
ecard_from_remote_record(GnomePilotConduitStandardAbs *conduit,
             PilotRecord *remote)
{
    ECard *ecard;
    struct Address address;
    VObject *vobj;
    VObject *nameprop, *addressprop;
    char *temp;

    g_return_val_if_fail(remote!=NULL,NULL);
    memset (&address, 0, sizeof (struct Address));
    unpack_Address (&address, remote->record, remote->length);
    
    vobj = newVObject (VCCardProp);
    nameprop = addProp (vobj, VCNameProp);

#define ADD_PROP(v,pilotprop,vprop) \
    if (address.entry [(pilotprop)]) \
        addPropValue ((v), (vprop), address.entry [(pilotprop)])

    ADD_PROP (nameprop, entryFirstname, VCGivenNameProp);
    ADD_PROP (nameprop, entryLastname, VCFamilyNameProp);

    addressprop = addProp (vobj, VCAdrProp);

    ADD_PROP (addressprop, entryAddress, VCStreetAddressProp);
    ADD_PROP (addressprop, entryCity, VCCityProp);
    ADD_PROP (addressprop, entryState, VCRegionProp);
    ADD_PROP (addressprop, entryZip, VCPostalCodeProp);
    ADD_PROP (addressprop, entryCountry, VCCountryNameProp);

    ADD_PROP (vobj, entryTitle, VCTitleProp);

    if (address.entry [entryCompany]) {
        VObject *orgprop;
        orgprop = addProp (vobj, VCOrgProp);
        ADD_PROP (orgprop, entryCompany, VCOrgNameProp);
    }

    temp = writeMemVObject (NULL, NULL, vobj);
    ecard = e_card_new (temp);
    free (temp);
    cleanVObject (vobj);

    free_Address(&address);

    gtk_object_set (GTK_OBJECT(ecard), "pilot_id", remote->ID);

    return ecard;
}

static void
add_card_cb (EBook *ebook, EBookStatus status, const char *id, gpointer closure)
{
    (*(EBookStatus*)closure) = status;
    gtk_main_quit();
}

static gint
update_record (GnomePilotConduitStandardAbs *conduit,
           PilotRecord *remote,
           AddressbookConduitContext *ctxt)
{
    struct Address address;
    ECard *ecard;

    g_return_val_if_fail(remote!=NULL,-1);

    memset (&address, 0, sizeof (struct Address));
    unpack_Address (&address, remote->record, remote->length);

    LOG ("requesting %ld [%s %s]", remote->ID, address.entry[entryFirstname], address.entry[entryLastname]);
    printf ("requesting %ld [%s %s]\n", remote->ID, address.entry[entryFirstname], address.entry[entryLastname]);

    ecard = get_ecard_by_pilot_id (ctxt->cards, remote->ID);

    if (ecard == NULL) {
        EBookStatus ebook_status;

        LOG ("Object did not exist, creating a new one");
        printf ("Object did not exist, creating a new one\n");

        ecard = ecard_from_remote_record (conduit, remote);

        /* add the ecard to the server */
        e_book_add_card (ctxt->ebook, ecard, add_card_cb, &ebook_status);
        gtk_main(); /* enter sub mainloop */

        if (ebook_status == E_BOOK_STATUS_SUCCESS)
            ctxt->cards = g_list_append (ctxt->cards, ecard);
        else
            WARN ("update_record: failed to add card to ebook\n");
    } else {
#if 0
        /* XXX toshok: need to figure out exactly what should
                   be done here - default to the existing ecard and
                   overwrite with information from the pilot? */
        iCalObject *new_obj;
        LOG ("Found");
        printf ("Found\n");
        new_obj = ecard_from_remote_record (conduit, remote, obj);
        obj = new_obj;
#endif
    }

#if 0
    /* update record on server */
    update_ecard (conduit, ecard, ctxt);
#endif
    free_Address(&address);

    return 0;
}

static void
check_for_slow_setting (GnomePilotConduit *c, AddressbookConduitContext *ctxt)
{
#if 0
    GList *uids;
    unsigned long int entry_number;

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

    entry_number = g_list_length (uids);

    LOG (_("Address holds %ld address entries"), entry_number);
    /* If the local base is empty, do a slow sync */
    if (entry_number == 0) {
        GnomePilotConduitStandard *conduit;
        conduit = GNOME_PILOT_CONDUIT_STANDARD (c);
        gnome_pilot_conduit_standard_set_slow (conduit);
    }
#endif /* 0 */
}

static gint
pre_sync (GnomePilotConduit *c,
      GnomePilotDBInfo *dbi,
      AddressbookConduitContext *ctxt)
{
    int l;
    unsigned char *buf;
    GnomePilotConduitStandardAbs *conduit;

    conduit = GNOME_PILOT_CONDUIT_STANDARD_ABS(c);
  
    g_message ("Evolution Addressbook Conduit v.%s",CONDUIT_VERSION);

    ctxt->ebook = NULL;
    
    if (start_address_server (GNOME_PILOT_CONDUIT_STANDARD_ABS(c), ctxt) != 0) {
        WARN(_("Could not start addressbook server"));
        gnome_pilot_conduit_error(GNOME_PILOT_CONDUIT(c),
                      _("Could not start addressbook server"));
        return -1;
    }


    /* Set the counters for the progress bar crap */

    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 Address application block"));
        WARN("dlp_ReadAppBlock(...) = %d",l);
        gnome_pilot_conduit_error(GNOME_PILOT_CONDUIT(c),
                 _("Could not read pilot's Address application block"));
        return -1;
    }
    unpack_AddressAppInfo(&(ctxt->ai),buf,l);
    g_free(buf);

#if 0
    check_for_slow_setting(c,ctxt);
#else
    /* for now just always use the slow sync method */
    gnome_pilot_conduit_standard_set_slow (GNOME_PILOT_CONDUIT_STANDARD (c));
#endif

    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,
         AddressbookLocalRecord **local,
         PilotRecord *remote,
         AddressbookConduitContext *ctxt)
{
    LOG ("in match_record");

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

    *local = find_record_in_ebook(conduit,remote,ctxt);
  
    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,
         AddressbookLocalRecord **local,
         AddressbookConduitContext *ctxt)
{
    LOG ("entering free_match");

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

    gtk_object_unref (GTK_OBJECT((*local)->ecard));
    g_free(*local);
    
        *local = NULL;
    return 0;
}

/*
  Move to archive and set status to Nothing
 */
static gint
archive_local (GnomePilotConduitStandardAbs *conduit,
           AddressbookLocalRecord *local,
           AddressbookConduitContext *ctxt)
{
    LOG ("entering archive_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,
        AddressbookLocalRecord *local,
        PilotRecord *remote,
        AddressbookConduitContext *ctxt)
{
    LOG ("entering archive_remote");

        //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,
          AddressbookConduitContext *ctxt)
{
    LOG ("entering store_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,
                AddressbookLocalRecord *local,
                AddressbookConduitContext *ctxt)
{
    LOG ("entering clear_status_archive_local");

    g_return_val_if_fail(local!=NULL,-1);

        return -1;
}

static gint
iterate (GnomePilotConduitStandardAbs *conduit,
     AddressbookLocalRecord **local,
     AddressbookConduitContext *ctxt)
{
    static GList *iterator;
    static int num;

    g_return_val_if_fail(local!=NULL,-1);

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

        iterator = ctxt->cards;
        num = 0;
        
        LOG ("iterating over %d records", g_list_length (ctxt->cards));
        *local = g_new0(AddressbookLocalRecord, 1);
        local_record_from_ecard (*local, (ECard*)iterator->data);
    } else {
        /* printf ("continuing iteration\n"); */
        num++;
        if(g_list_next(iterator)==NULL) {
            LOG ("ending");
            /** free stuff allocated for iteration */
            g_free((*local));

            LOG ("iterated over %d records", num);

            /* ends iteration */
            (*local) = NULL;
            return 0;
        } else {
            iterator = g_list_next (iterator);
            local_record_from_ecard (*local,(ECard*)(iterator->data));
        }
    }
    return 1;
}


static gint
iterate_specific (GnomePilotConduitStandardAbs *conduit,
          AddressbookLocalRecord **local,
          gint flag,
          gint archived,
          AddressbookConduitContext *ctxt)
{
#ifdef DEBUG_CALCONDUIT
    {
        gchar *tmp;
        switch (flag) {
        case GnomePilotRecordNothing: tmp = g_strdup("RecordNothing"); break;
        case GnomePilotRecordModified: tmp = g_strdup("RecordModified"); break;
        case GnomePilotRecordNew: tmp = g_strdup("RecordNew"); break;
        default: tmp = g_strdup_printf("0x%x",flag); break;
        }
        printf ("entering iterate_specific(flag = %s)\n", tmp);
        g_free(tmp);
    }
#endif
    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;
    }

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

static gint
purge (GnomePilotConduitStandardAbs *conduit,
       AddressbookConduitContext *ctxt)
{
    LOG ("entering purge");

    /* HEST, gem posterne her */

    return -1;
}


static gint
set_status (GnomePilotConduitStandardAbs *conduit,
        AddressbookLocalRecord *local,
        gint status,
        AddressbookConduitContext *ctxt)
{
#if 0
    gboolean success;
    LOG ("entering set_status(status=%d)",status);

    g_return_val_if_fail(local!=NULL,-1);

    g_assert(local->ical!=NULL);
    
    local->local.attr = status;
    switch(status) {
    case GnomePilotRecordPending:
    case GnomePilotRecordNothing:
        local->ical->pilot_status = ICAL_PILOT_SYNC_NONE;
        break;
    case GnomePilotRecordDeleted:
        break;
    case GnomePilotRecordNew:
    case GnomePilotRecordModified:
        local->ical->pilot_status = ICAL_PILOT_SYNC_MOD;
        break;    
    }
    
    if (status == GnomePilotRecordDeleted) {
        success = cal_client_remove_object (ctxt->client, local->ical->uid);
    } else {
        success = cal_client_update_object (ctxt->client, local->ical);
        cal_client_update_pilot_id (ctxt->client, local->ical->uid,
                        local->local.ID,
                        local->ical->pilot_status);
    }

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

static gint
set_archived (GnomePilotConduitStandardAbs *conduit,
          AddressbookLocalRecord *local,
          gint archived,
          AddressbookConduitContext *ctxt)
{
#if 0
    LOG ("entering set_archived");

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

    local->local.archived = archived;
    update_address_entry_in_repository (conduit, local->ical, ctxt);
#endif
        return 0;
}

static gint
set_pilot_id (GnomePilotConduitStandardAbs *conduit,
          AddressbookLocalRecord *local,
          guint32 ID,
          AddressbookConduitContext *ctxt)
{
    EBookStatus commit_status;

    LOG ("entering set_pilot_id(id=%d)",ID);

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

    local->local.ID = ID;

    gtk_object_set (GTK_OBJECT(local->ecard), "pilot_id", local->local.ID);
    e_book_commit_card (ctxt->ebook, local->ecard, status_cb, &commit_status);

    if (commit_status == E_BOOK_STATUS_SUCCESS) {
        return 0;
    }
    else {
        WARN ("set_pilot_id failed.\n");
        return -1;
    }
}

static gint
transmit (GnomePilotConduitStandardAbs *conduit,
      AddressbookLocalRecord *local,
      PilotRecord **remote,
      AddressbookConduitContext *ctxt)
{
    PilotRecord *p;
    
    LOG ("entering transmit");

    g_return_val_if_fail(local!=NULL,-1);
    g_return_val_if_fail(remote!=NULL,-1);
    g_assert(local->ecard!=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->address = g_new0(struct Address,1);

#if 0
    printf ("transmitting address to pilot [%s] complete=%d/%ld\n",
        local->ical->summary==NULL?"NULL":local->ical->summary,
        local->address->complete, local->ical->completed);
#endif

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

    *remote = p;

    return 0;
}

static gint
free_transmit (GnomePilotConduitStandardAbs *conduit,
           AddressbookLocalRecord *local,
           PilotRecord **remote,
           AddressbookConduitContext *ctxt)
{
    LOG ("entering free_transmit");

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

    free_Address(local->address);
    g_free((*remote)->record);
    *remote = NULL;
        return 0;
}

static gint
compare (GnomePilotConduitStandardAbs *conduit,
     AddressbookLocalRecord *local,
     PilotRecord *remote,
     AddressbookConduitContext *ctxt)
{
#if 0
    /* used by the quick compare */
    PilotRecord *remoteOfLocal;
    int err;
    int retval;

    g_message ("entering compare");
    printf ("entering compare\n");

    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) {
            g_message("compare failed on contents");
            printf ("compare failed on contents\n");
            retval = 1;

            /* debug spew */
            {
                struct Address foolocal;
                struct Address fooremote;

                unpack_Address (&foolocal,
                         remoteOfLocal->record,
                         remoteOfLocal->length);
                unpack_Address (&fooremote,
                         remote->record,
                         remote->length);

                printf (" local:[%d %ld %d %d '%s' '%s']\n",
                    foolocal.indefinite,
                    mktime (& foolocal.due),
                    foolocal.priority,
                    foolocal.complete,
                    foolocal.description,
                    foolocal.note);

                printf ("remote:[%d %ld %d %d '%s' '%s']\n",
                    fooremote.indefinite,
                    mktime (& fooremote.due),
                    fooremote.priority,
                    fooremote.complete,
                    fooremote.description,
                    fooremote.note);
            }
        }
    } else {
        g_message("compare failed on length");
        printf("compare failed on length\n");
        retval = 1;
    }

    free_transmit(conduit,local,&remoteOfLocal,ctxt);   
    return retval;
#endif /* 0 */
    return 0;
}

static gint
compare_backup (GnomePilotConduitStandardAbs *conduit,
        AddressbookLocalRecord *local,
        PilotRecord *remote,
        AddressbookConduitContext *ctxt)
{
    LOG ("entering compare_backup");

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

        return -1;
}

static gint
delete_all (GnomePilotConduitStandardAbs *conduit,
        AddressbookConduitContext *ctxt)
{
    GList *it;

    for (it=ctxt->cards; it; it = g_list_next (it)) {
        EBookStatus remove_status;
        e_book_remove_card (ctxt->ebook, it->data, status_cb, &remove_status);
        gtk_main (); /* enter sub main loop */

        if (E_BOOK_STATUS_SUCCESS != remove_status)
            WARN ("Failed to remove ecard");

        gtk_object_unref (it->data);
    }

    g_list_free (ctxt->cards);
    ctxt->cards = NULL;
        return -1;
}


GnomePilotConduit *
conduit_get_gpilot_conduit (guint32 pilotId)
{
    GtkObject *retval;
    AddressbookConduitCfg *cfg;
    AddressbookConduitContext *ctxt;

    printf ("in address's conduit_get_gpilot_conduit\n");

    retval = gnome_pilot_conduit_standard_abs_new ("AddressDB", 0x746F646F);
    g_assert (retval != NULL);
    gnome_pilot_conduit_construct(GNOME_PILOT_CONDUIT(retval),"AddressConduit");

    conduit_load_configuration(&cfg,pilotId);
    gtk_object_set_data(retval,"addressconduit_cfg",cfg);

    conduit_new_context(&ctxt,cfg);
    gtk_object_set_data(GTK_OBJECT(retval),"addressconduit_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)
{ 
        AddressbookConduitCfg *cc;
    AddressbookConduitContext *ctxt;

        cc = GET_CONDUITCFG(conduit);
    ctxt = GET_CONDUITCONTEXT(conduit);

    if (ctxt->ebook != NULL) {
        gtk_object_unref (GTK_OBJECT (ctxt->ebook));
    }

        conduit_destroy_configuration (&cc);

    conduit_destroy_context (&ctxt);

    gtk_object_destroy (GTK_OBJECT (conduit));
}