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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Evolution addressbook - Address Conduit
*
* Copyright (C) 1998 Free Software Foundation
* Copyright (C) 2000 Ximian, Inc.
*
* Authors: Eskil Heyn Olsen <deity@eskil.dk>
* JP Rosevear <jpr@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define G_LOG_DOMAIN "eaddrconduit"
#include <bonobo.h>
#include <libxml/parser.h>
#include <pi-source.h>
#include <pi-socket.h>
#include <pi-dlp.h>
#include <pi-address.h>
#include <libedataserver/e-url.h>
#include <libedataserverui/e-passwords.h>
#include <libebook/e-book.h>
#include <gpilotd/gnome-pilot-conduit.h>
#include <gpilotd/gnome-pilot-conduit-sync-abs.h>
#include <libgpilotdCM/gnome-pilot-conduit-management.h>
#include <libgpilotdCM/gnome-pilot-conduit-config.h>
#include <e-dialog-widgets.h>
#include <e-pilot-map.h>
#include <e-pilot-settings.h>
#include <e-pilot-util.h>
GnomePilotConduit * conduit_get_gpilot_conduit (guint32);
void conduit_destroy_gpilot_conduit (GnomePilotConduit*);
#define CONDUIT_VERSION "0.1.2"
#define DEBUG_CONDUIT 1
/* #undef DEBUG_CONDUIT */
#ifdef DEBUG_CONDUIT
#define LOG(x) x
#else
#define LOG(x)
#endif
#define WARN g_warning
#define INFO g_message
enum {
LABEL_WORK,
LABEL_HOME,
LABEL_FAX,
LABEL_OTHER,
LABEL_EMAIL,
LABEL_MAIN,
LABEL_PAGER,
LABEL_MOBILE
};
static EContactField priority [] = {
E_CONTACT_PHONE_BUSINESS,
E_CONTACT_PHONE_HOME,
E_CONTACT_PHONE_BUSINESS_FAX,
E_CONTACT_EMAIL_1,
E_CONTACT_PHONE_PAGER,
E_CONTACT_PHONE_MOBILE,
E_CONTACT_PHONE_BUSINESS_2,
E_CONTACT_PHONE_HOME_2,
E_CONTACT_PHONE_HOME_FAX,
E_CONTACT_EMAIL_2,
E_CONTACT_PHONE_OTHER,
E_CONTACT_PHONE_PRIMARY,
E_CONTACT_PHONE_OTHER_FAX,
E_CONTACT_EMAIL_3,
E_CONTACT_FIELD_LAST
};
static int priority_label [] = {
LABEL_WORK,
LABEL_HOME,
LABEL_FAX,
LABEL_EMAIL,
LABEL_PAGER,
LABEL_MOBILE,
LABEL_WORK,
LABEL_HOME,
LABEL_FAX,
LABEL_EMAIL,
LABEL_OTHER,
LABEL_MAIN,
LABEL_FAX,
LABEL_EMAIL,
-1
};
typedef struct _EAddrLocalRecord EAddrLocalRecord;
typedef struct _EAddrConduitCfg EAddrConduitCfg;
typedef struct _EAddrConduitGui EAddrConduitGui;
typedef struct _EAddrConduitContext EAddrConduitContext;
/* Local Record */
struct _EAddrLocalRecord {
/* The stuff from gnome-pilot-conduit-standard-abs.h
Must be first in the structure, or instances of this
structure cannot be used by gnome-pilot-conduit-standard-abs.
*/
GnomePilotDesktopRecord local;
/* The corresponding ECard object */
EContact *contact;
/* pilot-link address structure, used for implementing Transmit. */
struct Address *addr;
};
static void
addrconduit_destroy_record (EAddrLocalRecord *local)
{
g_object_unref (local->contact);
free_Address (local->addr);
g_free (local->addr);
g_free (local);
}
/* Configuration */
struct _EAddrConduitCfg {
guint32 pilot_id;
GnomePilotConduitSyncType sync_type;
ESourceList *source_list;
ESource *source;
gboolean secret;
EContactField default_address;
gchar *last_uri;
};
static EAddrConduitCfg *
addrconduit_load_configuration (guint32 pilot_id)
{
EAddrConduitCfg *c;
GnomePilotConduitManagement *management;
GnomePilotConduitConfig *config;
gchar *address, prefix[256];
g_snprintf (prefix, 255, "/gnome-pilot.d/e-address-conduit/Pilot_%u/",
pilot_id);
c = g_new0 (EAddrConduitCfg,1);
g_assert (c != NULL);
c->pilot_id = pilot_id;
management = gnome_pilot_conduit_management_new ("e_address_conduit", GNOME_PILOT_CONDUIT_MGMT_ID);
gtk_object_ref (GTK_OBJECT (management));
gtk_object_sink (GTK_OBJECT (management));
config = gnome_pilot_conduit_config_new (management, pilot_id);
gtk_object_ref (GTK_OBJECT (config));
gtk_object_sink (GTK_OBJECT (config));
if (!gnome_pilot_conduit_config_is_enabled (config, &c->sync_type))
c->sync_type = GnomePilotConduitSyncTypeNotSet;
gtk_object_unref (GTK_OBJECT (config));
gtk_object_unref (GTK_OBJECT (management));
/* Custom settings */
gnome_config_push_prefix (prefix);
if (!e_book_get_addressbooks (&c->source_list, NULL))
c->source_list = NULL;
if (c->source_list) {
c->source = e_pilot_get_sync_source (c->source_list);
if (!c->source)
c->source = e_source_list_peek_source_any (c->source_list);
if (c->source) {
g_object_ref (c->source);
} else {
g_object_unref (c->source_list);
c->source_list = NULL;
}
}
c->secret = gnome_config_get_bool ("secret=FALSE");
address = gnome_config_get_string ("default_address=business");
if (!strcmp (address, "business"))
c->default_address = E_CONTACT_ADDRESS_WORK;
else if (!strcmp (address, "home"))
c->default_address = E_CONTACT_ADDRESS_HOME;
else if (!strcmp (address, "other"))
c->default_address = E_CONTACT_ADDRESS_OTHER;
g_free (address);
c->last_uri = gnome_config_get_string ("last_uri");
gnome_config_pop_prefix ();
return c;
}
static void
addrconduit_save_configuration (EAddrConduitCfg *c)
{
gchar prefix[256];
g_snprintf (prefix, 255, "/gnome-pilot.d/e-address-conduit/Pilot_%u/",
c->pilot_id);
gnome_config_push_prefix (prefix);
e_pilot_set_sync_source (c->source_list, c->source);
gnome_config_set_bool ("secret", c->secret);
switch (c->default_address) {
case E_CONTACT_ADDRESS_WORK:
gnome_config_set_string ("default_address", "business");
break;
case E_CONTACT_ADDRESS_HOME:
gnome_config_set_string ("default_address", "home");
break;
case E_CONTACT_ADDRESS_OTHER:
gnome_config_set_string ("default_address", "other");
break;
default:
g_warning ("Unknown default_address value");
}
gnome_config_set_string ("last_uri", c->last_uri);
gnome_config_pop_prefix ();
gnome_config_sync ();
gnome_config_drop_all ();
}
static EAddrConduitCfg*
addrconduit_dupe_configuration (EAddrConduitCfg *c)
{
EAddrConduitCfg *retval;
g_return_val_if_fail (c != NULL, NULL);
retval = g_new0 (EAddrConduitCfg, 1);
retval->sync_type = c->sync_type;
retval->pilot_id = c->pilot_id;
if (c->source_list)
retval->source_list = g_object_ref (c->source_list);
if (c->source)
retval->source = g_object_ref (c->source);
retval->secret = c->secret;
retval->default_address = c->default_address;
retval->last_uri = g_strdup (c->last_uri);
return retval;
}
static void
addrconduit_destroy_configuration (EAddrConduitCfg *c)
{
g_return_if_fail (c != NULL);
g_object_unref (c->source_list);
g_object_unref (c->source);
g_free (c->last_uri);
g_free (c);
}
/* Gui */
struct _EAddrConduitGui {
GtkWidget *default_address;
};
static EAddrConduitGui *
e_addr_gui_new (EPilotSettings *ps)
{
EAddrConduitGui *gui;
GtkWidget *lbl, *menu;
gint rows, i;
static const char *items[] = {"Business", "Home", "Other", NULL};
g_return_val_if_fail (ps != NULL, NULL);
g_return_val_if_fail (E_IS_PILOT_SETTINGS (ps), NULL);
gtk_table_resize (GTK_TABLE (ps), E_PILOT_SETTINGS_TABLE_ROWS + 1,
E_PILOT_SETTINGS_TABLE_COLS);
gui = g_new0 (EAddrConduitGui, 1);
rows = E_PILOT_SETTINGS_TABLE_ROWS;
lbl = gtk_label_new (_("Default Sync Address:"));
gtk_misc_set_alignment (GTK_MISC (lbl), 0.0, 0.5);
gui->default_address = gtk_option_menu_new ();
menu = gtk_menu_new ();
for (i = 0; items[i] != NULL; i++) {
GtkWidget *item;
item = gtk_menu_item_new_with_label (items[i]);
gtk_widget_show (item);
gtk_menu_append (GTK_MENU (menu), item);
}
gtk_widget_show (menu);
gtk_option_menu_set_menu (GTK_OPTION_MENU (gui->default_address), menu);
gtk_table_attach_defaults (GTK_TABLE (ps), lbl, 0, 1, rows, rows + 1);
gtk_table_attach_defaults (GTK_TABLE (ps), gui->default_address, 1, 2, rows, rows + 1);
gtk_widget_show (lbl);
gtk_widget_show (gui->default_address);
return gui;
}
static const int default_address_map[] = {
E_CONTACT_ADDRESS_WORK,
E_CONTACT_ADDRESS_HOME,
E_CONTACT_ADDRESS_OTHER,
-1
};
static void
e_addr_gui_fill_widgets (EAddrConduitGui *gui, EAddrConduitCfg *cfg)
{
g_return_if_fail (gui != NULL);
g_return_if_fail (cfg != NULL);
e_dialog_option_menu_set (gui->default_address,
cfg->default_address,
default_address_map);
}
static void
e_addr_gui_fill_config (EAddrConduitGui *gui, EAddrConduitCfg *cfg)
{
g_return_if_fail (gui != NULL);
g_return_if_fail (cfg != NULL);
cfg->default_address = e_dialog_option_menu_get (gui->default_address,
default_address_map);
}
static void
e_addr_gui_destroy (EAddrConduitGui *gui)
{
g_free (gui);
}
/* Context */
struct _EAddrConduitContext {
GnomePilotDBInfo *dbi;
EAddrConduitCfg *cfg;
EAddrConduitCfg *new_cfg;
EAddrConduitGui *gui;
GtkWidget *ps;
struct AddressAppInfo ai;
EBook *ebook;
GList *cards;
GList *changed;
GHashTable *changed_hash;
GList *locals;
EPilotMap *map;
};
static EAddrConduitContext *
e_addr_context_new (guint32 pilot_id)
{
EAddrConduitContext *ctxt = g_new0 (EAddrConduitContext, 1);
ctxt->cfg = addrconduit_load_configuration (pilot_id);
ctxt->new_cfg = addrconduit_dupe_configuration (ctxt->cfg);
ctxt->gui = NULL;
ctxt->ps = NULL;
ctxt->ebook = NULL;
ctxt->cards = NULL;
ctxt->changed_hash = NULL;
ctxt->changed = NULL;
ctxt->locals = NULL;
ctxt->map = NULL;
return ctxt;
}
static void
e_addr_context_destroy (EAddrConduitContext *ctxt)
{
GList *l;
g_return_if_fail (ctxt != NULL);
if (ctxt->cfg != NULL)
addrconduit_destroy_configuration (ctxt->cfg);
if (ctxt->new_cfg != NULL)
addrconduit_destroy_configuration (ctxt->new_cfg);
if (ctxt->gui != NULL)
e_addr_gui_destroy (ctxt->gui);
if (ctxt->ebook != NULL)
g_object_unref (ctxt->ebook);
if (ctxt->cards != NULL) {
for (l = ctxt->cards; l != NULL; l = l->next)
g_object_unref (l->data);
g_list_free (ctxt->cards);
}
if (ctxt->changed_hash != NULL)
g_hash_table_destroy (ctxt->changed_hash);
if (ctxt->changed != NULL)
e_book_free_change_list (ctxt->changed);
if (ctxt->locals != NULL) {
for (l = ctxt->locals; l != NULL; l = l->next)
addrconduit_destroy_record (l->data);
g_list_free (ctxt->locals);
}
if (ctxt->map != NULL)
e_pilot_map_destroy (ctxt->map);
g_free (ctxt);
}
/* Debug routines */
static char *
print_local (EAddrLocalRecord *local)
{
static char buff[ 4096 ];
if (local == NULL) {
sprintf (buff, "[NULL]");
return buff;
}
if (local->addr) {
g_snprintf (buff, 4096, "['%s' '%s' '%s']",
local->addr->entry[entryLastname] ?
local->addr->entry[entryLastname] : "",
local->addr->entry[entryFirstname] ?
local->addr->entry[entryFirstname] : "",
local->addr->entry[entryCompany] ?
local->addr->entry[entryCompany] : "");
return buff;
}
return "";
}
static char *print_remote (GnomePilotRecord *remote)
{
static char buff[ 4096 ];
struct Address addr;
if (remote == NULL) {
sprintf (buff, "[NULL]");
return buff;
}
memset (&addr, 0, sizeof (struct Address));
unpack_Address (&addr, remote->record, remote->length);
g_snprintf (buff, 4096, "['%s' '%s' '%s']",
addr.entry[entryLastname] ?
addr.entry[entryLastname] : "",
addr.entry[entryFirstname] ?
addr.entry[entryFirstname] : "",
addr.entry[entryCompany] ?
addr.entry[entryCompany] : "");
free_Address (&addr);
return buff;
}
/* Utility routines */
static char *
map_name (EAddrConduitContext *ctxt)
{
char *filename = NULL;
filename = g_strdup_printf ("%s/.evolution/addressbook/local/system/pilot-map-%d.xml", g_get_home_dir (), ctxt->cfg->pilot_id);
return filename;
}
static GList *
next_changed_item (EAddrConduitContext *ctxt, GList *changes)
{
EBookChange *ebc;
GList *l;
for (l = changes; l != NULL; l = l->next) {
ebc = l->data;
if (g_hash_table_lookup (ctxt->changed_hash, e_contact_get_const (ebc->contact, E_CONTACT_UID)))
return l;
}
return NULL;
}
static EContactField
get_next_mail (EContactField *field)
{
if (field == NULL)
return E_CONTACT_EMAIL_1;
switch (*field) {
case E_CONTACT_EMAIL_1:
return E_CONTACT_EMAIL_2;
case E_CONTACT_EMAIL_2:
return E_CONTACT_EMAIL_3;
default:
break;
}
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_home (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_HOME;
switch (*field) {
case E_CONTACT_PHONE_HOME:
return E_CONTACT_PHONE_HOME_2;
default:
break;
}
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_work (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_BUSINESS;
switch (*field) {
case E_CONTACT_PHONE_BUSINESS:
return E_CONTACT_PHONE_BUSINESS_2;
default:
break;
}
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_fax (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_BUSINESS_FAX;
switch (*field) {
case E_CONTACT_PHONE_BUSINESS_FAX:
return E_CONTACT_PHONE_HOME_FAX;
case E_CONTACT_PHONE_HOME_FAX:
return E_CONTACT_PHONE_OTHER_FAX;
default:
break;
}
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_other (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_OTHER;
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_main (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_PRIMARY;
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_pager (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_PAGER;
return E_CONTACT_FIELD_LAST;
}
static EContactField
get_next_mobile (EContactField *field)
{
if (field == NULL)
return E_CONTACT_PHONE_MOBILE;
return E_CONTACT_FIELD_LAST;
}
static void
get_next_init (EContactField *next_mail,
EContactField *next_home,
EContactField *next_work,
EContactField *next_fax,
EContactField *next_other,
EContactField *next_main,
EContactField *next_pager,
EContactField *next_mobile)
{
*next_mail = get_next_mail (NULL);
*next_home = get_next_home (NULL);
*next_work = get_next_work (NULL);
*next_fax = get_next_fax (NULL);
*next_other = get_next_other (NULL);
*next_main = get_next_main (NULL);
*next_pager = get_next_pager (NULL);
*next_mobile = get_next_mobile (NULL);
}
static gboolean
is_next_done (EContactField field)
{
if (field == E_CONTACT_FIELD_LAST)
return TRUE;
return FALSE;
}
static gboolean
is_syncable (EAddrConduitContext *ctxt, EAddrLocalRecord *local)
{
EContactField next_mail, next_home, next_work, next_fax;
EContactField next_other, next_main, next_pager, next_mobile;
gboolean syncable = TRUE;
int i, l = 0;
/* See if there are fields we can't sync or not in priority order */
get_next_init (&next_mail, &next_home, &next_work, &next_fax,
&next_other, &next_main, &next_pager, &next_mobile);
for (i = entryPhone1; i <= entryPhone5 && syncable; i++) {
int phonelabel = local->addr->phoneLabel[i - entryPhone1];
const char *phone_str = local->addr->entry[i];
gboolean empty = !(phone_str && *phone_str);
if (empty)
continue;
for ( ; priority_label[l] != -1; l++)
if (phonelabel == priority_label[l])
break;
if (priority_label[l] == -1) {
syncable = FALSE;
continue;
}
if (phonelabel == LABEL_EMAIL) {
if (is_next_done (next_mail) || next_mail != priority[l]) {
syncable = FALSE;
break;
}
next_mail = get_next_mail (&next_mail);
} else if (phonelabel == LABEL_HOME) {
if (is_next_done (next_home) || next_home != priority[l]) {
syncable = FALSE;
break;
}
next_home = get_next_home (&next_home);
} else if (phonelabel == LABEL_WORK) {
if (is_next_done (next_work) || next_work != priority[l]) {
syncable = FALSE;
break;
}
next_work = get_next_work (&next_work);
} else if (phonelabel == LABEL_FAX) {
if (is_next_done (next_fax) || next_fax != priority[l]) {
syncable = FALSE;
break;
}
next_fax = get_next_fax (&next_fax);
} else if (phonelabel == LABEL_OTHER) {
if (is_next_done (next_other) || next_other != priority[l]) {
syncable = FALSE;
break;
}
next_other = get_next_other (&next_other);
} else if (phonelabel == LABEL_MAIN) {
if (is_next_done (next_main) || next_main != priority[l]) {
syncable = FALSE;
break;
}
next_main = get_next_main (&next_main);
} else if (phonelabel == LABEL_PAGER) {
if (is_next_done (next_pager) || next_pager != priority[l]) {
syncable = FALSE;
break;
}
next_pager = get_next_pager (&next_pager);
} else if (phonelabel == LABEL_MOBILE) {
if (is_next_done (next_mobile) || next_mobile != priority[l]) {
syncable = FALSE;
break;
}
next_mobile = get_next_mobile (&next_mobile);
}
}
return syncable;
}
static void
set_contact_text (EContact *contact, EContactField field, struct Address address, int entry)
{
char *text = NULL;
if (address.entry[entry])
text = e_pilot_utf8_from_pchar (address.entry[entry]);
e_contact_set (contact, field, text);
g_free (text);
}
static char *
get_entry_text (struct Address address, int entry)
{
if (address.entry[entry])
return e_pilot_utf8_from_pchar (address.entry[entry]);
return NULL;
}
static void
clear_entry_text (struct Address address, int field)
{
if (address.entry[field]) {
free (address.entry[field]);
address.entry[field] = NULL;
}
}
static void
compute_status (EAddrConduitContext *ctxt, EAddrLocalRecord *local, const char *uid)
{
EBookChange *ebc;
local->local.archived = FALSE;
local->local.secret = FALSE;
ebc = g_hash_table_lookup (ctxt->changed_hash, uid);
if (ebc == NULL) {
local->local.attr = GnomePilotRecordNothing;
return;
}
switch (ebc->change_type) {
case E_BOOK_CHANGE_CARD_ADDED:
local->local.attr = GnomePilotRecordNew;
break;
case E_BOOK_CHANGE_CARD_MODIFIED:
local->local.attr = GnomePilotRecordModified;
break;
case E_BOOK_CHANGE_CARD_DELETED:
local->local.attr = GnomePilotRecordDeleted;
break;
}
}
static GnomePilotRecord
local_record_to_pilot_record (EAddrLocalRecord *local,
EAddrConduitContext *ctxt)
{
GnomePilotRecord p;
static char record[0xffff];
g_assert (local->addr != NULL );
LOG (g_message ( "local_record_to_pilot_record\n" ));
p.ID = local->local.ID;
p.category = local->local.category;
p.attr = local->local.attr;
p.archived = local->local.archived;
p.secret = local->local.secret;
/* Generate pilot record structure */
p.record = record;
p.length = pack_Address (local->addr, p.record, 0xffff);
return p;
}
static void
local_record_from_ecard (EAddrLocalRecord *local, EContact *contact, EAddrConduitContext *ctxt)
{
EContactAddress *address = NULL;
int phone = entryPhone1;
EContactField field;
gboolean syncable;
int i;
g_return_if_fail (local != NULL);
g_return_if_fail (contact != NULL);
local->contact = g_object_ref (contact);
local->local.ID = e_pilot_map_lookup_pid (ctxt->map, e_contact_get_const (contact, E_CONTACT_UID), TRUE);
compute_status (ctxt, local, e_contact_get_const (contact, E_CONTACT_UID));
local->addr = g_new0 (struct Address, 1);
/* Handle the fields and category we don't sync by making sure
* we don't overwrite them
*/
if (local->local.ID != 0) {
struct Address addr;
char record[0xffff];
int cat = 0;
if (dlp_ReadRecordById (ctxt->dbi->pilot_socket,
ctxt->dbi->db_handle,
local->local.ID, &record,
NULL, NULL, NULL, &cat) > 0) {
local->local.category = cat;
memset (&addr, 0, sizeof (struct Address));
unpack_Address (&addr, record, 0xffff);
for (i = 0; i < 5; i++) {
if (addr.entry[entryPhone1 + i])
local->addr->entry[entryPhone1 + i] =
strdup (addr.entry[entryPhone1 + i]);
local->addr->phoneLabel[i] = addr.phoneLabel[i];
}
local->addr->showPhone = addr.showPhone;
for (i = 0; i < 4; i++) {
if (addr.entry[entryCustom1 + i])
local->addr->entry[entryCustom1 + i] =
strdup (addr.entry[entryCustom1 + i]);
}
free_Address (&addr);
}
}
local->addr->entry[entryFirstname] = e_pilot_utf8_to_pchar (e_contact_get_const (contact, E_CONTACT_GIVEN_NAME));
local->addr->entry[entryLastname] = e_pilot_utf8_to_pchar (e_contact_get_const (contact, E_CONTACT_FAMILY_NAME));
local->addr->entry[entryCompany] = e_pilot_utf8_to_pchar (e_contact_get_const (contact, E_CONTACT_ORG));
local->addr->entry[entryTitle] = e_pilot_utf8_to_pchar (e_contact_get_const (contact, E_CONTACT_TITLE));
/* See if the default has something in it */
if ((address = e_contact_get (contact, ctxt->cfg->default_address))) {
field = ctxt->cfg->default_address;
} else {
/* Try to find a non-empty address field */
for (field = E_CONTACT_FIRST_ADDRESS_ID; field <= E_CONTACT_LAST_ADDRESS_ID; field++) {
if ((address = e_contact_get (contact, field)))
break;
}
}
if (address) {
char *add;
/* If the address has 2 lines, make sure both get added */
if (address->ext != NULL &&
strlen (address->ext) > 0) {
add = g_strconcat (address->street, "\n", address->ext, NULL);
LOG (g_warning ("Address has two lines: [%s]\n", add));
}
else {
add = g_strdup (address->street);
LOG (g_warning ("Address has only one line: [%s]\n", add));
}
local->addr->entry[entryAddress] = e_pilot_utf8_to_pchar (add);
g_free (add);
local->addr->entry[entryCity] = e_pilot_utf8_to_pchar (address->locality);
local->addr->entry[entryState] = e_pilot_utf8_to_pchar (address->region);
local->addr->entry[entryZip] = e_pilot_utf8_to_pchar (address->code);
local->addr->entry[entryCountry] = e_pilot_utf8_to_pchar (address->country);
e_contact_address_free (address);
}
/* Phone numbers */
/* See if everything is syncable */
syncable = is_syncable (ctxt, local);
if (syncable) {
INFO ("Syncable");
/* Sync by priority */
for (i = 0, phone = entryPhone1;
priority[i] != E_CONTACT_FIELD_LAST && phone <= entryPhone5; i++) {
const char *phone_str;
phone_str = e_contact_get_const (contact, priority[i]);
if (phone_str && *phone_str) {
clear_entry_text (*local->addr, phone);
local->addr->entry[phone] = e_pilot_utf8_to_pchar (phone_str);
local->addr->phoneLabel[phone - entryPhone1] = priority_label[i];
phone++;
}
}
for ( ; phone <= entryPhone5; phone++)
local->addr->phoneLabel[phone - entryPhone1] = phone - entryPhone1;
local->addr->showPhone = 0;
} else {
EContactField next_mail, next_home, next_work, next_fax;
EContactField next_other, next_main, next_pager, next_mobile;
INFO ("Not Syncable");
get_next_init (&next_mail, &next_home, &next_work, &next_fax,
&next_other, &next_main, &next_pager, &next_mobile);
/* Not completely syncable, so do the best we can */
for (i = entryPhone1; i <= entryPhone5; i++) {
int phonelabel = local->addr->phoneLabel[i - entryPhone1];
const char *phone_str = NULL;
if (phonelabel == LABEL_EMAIL && !is_next_done (next_mail)) {
phone_str = e_contact_get_const (contact, next_mail);
next_mail = get_next_mail (&next_mail);
} else if (phonelabel == LABEL_HOME && !is_next_done (next_home)) {
phone_str = e_contact_get_const (contact, next_home);
next_home = get_next_home (&next_home);
} else if (phonelabel == LABEL_WORK && !is_next_done (next_work)) {
phone_str = e_contact_get_const (contact, next_work);
next_work = get_next_work (&next_work);
} else if (phonelabel == LABEL_FAX && !is_next_done (next_fax)) {
phone_str = e_contact_get_const (contact, next_fax);
next_fax = get_next_fax (&next_fax);
} else if (phonelabel == LABEL_OTHER && !is_next_done (next_other)) {
phone_str = e_contact_get_const (contact, next_other);
next_other = get_next_other (&next_other);
} else if (phonelabel == LABEL_MAIN && !is_next_done (next_main)) {
phone_str = e_contact_get_const (contact, next_main);
next_main = get_next_main (&next_main);
} else if (phonelabel == LABEL_PAGER && !is_next_done (next_pager)) {
phone_str = e_contact_get_const (contact, next_pager);
next_pager = get_next_pager (&next_pager);
} else if (phonelabel == LABEL_MOBILE && !is_next_done (next_mobile)) {
phone_str = e_contact_get_const (contact, next_mobile);
next_mobile = get_next_mobile (&next_mobile);
}
if (phone_str && *phone_str) {
clear_entry_text (*local->addr, i);
local->addr->entry[i] = e_pilot_utf8_to_pchar (phone_str);
}
}
}
/* Note */
local->addr->entry[entryNote] = e_pilot_utf8_to_pchar (e_contact_get_const (contact, E_CONTACT_NOTE));
}
static void
local_record_from_uid (EAddrLocalRecord *local,
const char *uid,
EAddrConduitContext *ctxt)
{
EContact *contact = NULL;
const char *cuid;
GList *l;
g_assert (local != NULL);
for (l = ctxt->cards; l != NULL; l = l->next) {
contact = l->data;
/* FIXME Do we need to check for the empty string? */
if ((cuid = e_contact_get_const (contact, E_CONTACT_UID)) && !strcmp (cuid, uid))
break;
contact = NULL;
}
if (contact != NULL) {
local_record_from_ecard (local, contact, ctxt);
} else {
contact = e_contact_new ();
e_contact_set (contact, E_CONTACT_UID, (gpointer) uid);
local_record_from_ecard (local, contact, ctxt);
g_object_unref (contact);
}
}
static EContact *
ecard_from_remote_record(EAddrConduitContext *ctxt,
GnomePilotRecord *remote,
EContact *in_contact)
{
struct Address address;
EContact *contact;
EContactName *name;
EContactAddress *eaddress;
EContactField mailing_address;
char *txt, *find, *full_name;
EContactField next_mail, next_home, next_work, next_fax;
EContactField next_other, next_main, next_pager, next_mobile;
int i;
g_return_val_if_fail(remote!=NULL,NULL);
memset (&address, 0, sizeof (struct Address));
unpack_Address (&address, remote->record, remote->length);
if (in_contact == NULL)
contact = e_contact_new ();
else
contact = e_contact_duplicate (in_contact);
/* Name */
name = e_contact_name_new ();
name->given = get_entry_text (address, entryFirstname);
name->family = get_entry_text (address, entryLastname);
full_name = e_contact_name_to_string (name);
e_contact_set (contact, E_CONTACT_FULL_NAME, full_name);
e_contact_name_free (name);
/* File As */
if (!full_name || !*full_name)
set_contact_text (contact, E_CONTACT_FILE_AS, address, entryCompany);
g_free (full_name);
/* Title and Company */
set_contact_text (contact, E_CONTACT_TITLE, address, entryTitle);
set_contact_text (contact, E_CONTACT_ORG, address, entryCompany);
/* Address */
mailing_address = -1;
if ((eaddress = e_contact_get (contact, ctxt->cfg->default_address))) {
mailing_address = ctxt->cfg->default_address;
e_contact_address_free (eaddress);
} else {
for (i = E_CONTACT_FIRST_ADDRESS_ID; i <= E_CONTACT_LAST_ADDRESS_ID; i++) {
if ((eaddress = e_contact_get (contact, i))) {
e_contact_address_free (eaddress);
mailing_address = i;
break;
}
}
}
if (mailing_address == -1)
mailing_address = ctxt->cfg->default_address;
eaddress = g_new0 (EContactAddress, 1);
txt = get_entry_text (address, entryAddress);
if (txt && (find = strchr (txt, '\n')) != NULL) {
*find = '\0';
find++;
} else {
find = NULL;
}
eaddress->street = txt;
eaddress->ext = find != NULL ? g_strdup (find) : g_strdup ("");
eaddress->locality = get_entry_text (address, entryCity);
eaddress->region = get_entry_text (address, entryState);
eaddress->country = get_entry_text (address, entryCountry);
eaddress->code = get_entry_text (address, entryZip);
e_contact_set (contact, mailing_address, eaddress);
e_contact_address_free (eaddress);
/* Phone numbers */
get_next_init (&next_mail, &next_home, &next_work, &next_fax,
&next_other, &next_main, &next_pager, &next_mobile);
for (i = entryPhone1; i <= entryPhone5; i++) {
int phonelabel = address.phoneLabel[i - entryPhone1];
char *phonenum = get_entry_text (address, i);
if (phonelabel == LABEL_EMAIL && !is_next_done (next_mail)) {
e_contact_set (contact, next_mail, phonenum);
next_mail = get_next_mail (&next_mail);
} else if (phonelabel == LABEL_HOME && !is_next_done (next_home)) {
e_contact_set (contact, next_home, phonenum);
next_home = get_next_home (&next_home);
} else if (phonelabel == LABEL_WORK && !is_next_done (next_work)) {
e_contact_set (contact, next_work, phonenum);
next_work = get_next_work (&next_work);
} else if (phonelabel == LABEL_FAX && !is_next_done (next_fax)) {
e_contact_set (contact, next_fax, phonenum);
next_fax = get_next_fax (&next_fax);
} else if (phonelabel == LABEL_OTHER && !is_next_done (next_other)) {
e_contact_set (contact, next_other, phonenum);
next_other = get_next_other (&next_other);
} else if (phonelabel == LABEL_MAIN && !is_next_done (next_main)) {
e_contact_set (contact, next_main, phonenum);
next_main = get_next_main (&next_main);
} else if (phonelabel == LABEL_PAGER && !is_next_done (next_pager)) {
e_contact_set (contact, next_pager, phonenum);
next_pager = get_next_pager (&next_pager);
} else if (phonelabel == LABEL_MOBILE && !is_next_done (next_mobile)) {
e_contact_set (contact, next_mobile, phonenum);
next_mobile = get_next_mobile (&next_mobile);
}
g_free (phonenum);
}
/* Note */
set_contact_text (contact, E_CONTACT_NOTE, address, entryNote);
free_Address(&address);
return contact;
}
static void
check_for_slow_setting (GnomePilotConduit *c, EAddrConduitContext *ctxt)
{
GnomePilotConduitStandard *conduit = GNOME_PILOT_CONDUIT_STANDARD (c);
int map_count;
const char *uri;
map_count = g_hash_table_size (ctxt->map->pid_map);
if (map_count == 0)
gnome_pilot_conduit_standard_set_slow (conduit, TRUE);
/* Or if the URI's don't match */
uri = e_book_get_uri (ctxt->ebook);
LOG (g_message (" Current URI %s (%s)\n", uri, ctxt->cfg->last_uri ? ctxt->cfg->last_uri : "<NONE>"));
if (ctxt->cfg->last_uri != NULL && strcmp (ctxt->cfg->last_uri, uri)) {
gnome_pilot_conduit_standard_set_slow (conduit, TRUE);
e_pilot_map_clear (ctxt->map);
}
if (gnome_pilot_conduit_standard_get_slow (conduit)) {
ctxt->map->write_touched_only = TRUE;
LOG (g_message ( " doing slow sync\n" ));
} else {
LOG (g_message ( " doing fast sync\n" ));
}
}
static void
addressbook_authenticate (EBook *book,
gpointer data)
{
gchar *auth;
gchar *user;
gchar *passwd;
gchar *str_uri;
gchar *pass_key;
gchar *auth_domain;
gchar *component_name;
EUri *e_uri;
ESource *source = (ESource *)data;
auth = e_source_get_property (source, "auth");
auth_domain = e_source_get_property (source, "auth-domain");
component_name = auth_domain ? auth_domain : "Addressbook";
if (auth && !strcmp ("plain/password", auth))
user = e_source_get_property (source, "user");
else
user = e_source_get_property (source, "email_addr");
if (!user)
user = "";
str_uri = e_source_get_uri (source);
e_uri = e_uri_new (str_uri);
pass_key = e_uri_to_string (e_uri, FALSE);
e_uri_free (e_uri);
passwd = e_passwords_get_password (component_name, pass_key);
if (passwd)
passwd = "";
if (book)
if (!e_book_authenticate_user (book, user, passwd, auth, NULL))
LOG (g_warning ("Authentication failed"));
g_free (pass_key);
g_free (str_uri);
return;
}
/* Pilot syncing callbacks */
static gint
pre_sync (GnomePilotConduit *conduit,
GnomePilotDBInfo *dbi,
EAddrConduitContext *ctxt)
{
GnomePilotConduitSyncAbs *abs_conduit;
EBookQuery *query;
GList *l;
int len;
unsigned char *buf;
char *filename;
char *change_id;
char *auth;
gint num_records, add_records = 0, mod_records = 0, del_records = 0;
abs_conduit = GNOME_PILOT_CONDUIT_SYNC_ABS (conduit);
LOG (g_message ( "---------------------------------------------------------\n" ));
LOG (g_message ( "pre_sync: Addressbook Conduit v.%s", CONDUIT_VERSION ));
/* g_message ("Addressbook Conduit v.%s", CONDUIT_VERSION); */
ctxt->dbi = dbi;
if (ctxt->cfg->source) {
ctxt->ebook = e_book_new (ctxt->cfg->source, NULL);
} else {
ctxt->ebook = e_book_new_default_addressbook (NULL);
}
auth = e_source_get_property (ctxt->cfg->source, "auth");
if (auth) {
LOG (g_message ("contacts needs authentication\n"));
g_signal_connect (ctxt->ebook, "auth_required",
G_CALLBACK (addressbook_authenticate), ctxt->cfg->source);
}
if (!ctxt->ebook || !e_book_open (ctxt->ebook, TRUE, NULL)) {
WARN(_("Could not load addressbook"));
gnome_pilot_conduit_error (conduit, _("Could not load addressbook"));
return -1;
}
/* Load the uid <--> pilot id mappings */
filename = map_name (ctxt);
e_pilot_map_read (filename, &ctxt->map);
g_free (filename);
/* Get a list of all contacts */
if (!(query = e_book_query_any_field_contains (""))) {
LOG (g_warning ("Failed to get EBookQuery"));
return -1;
}
if (!e_book_get_contacts (ctxt->ebook, query, &ctxt->cards, NULL)) {
LOG (g_warning ("Failed to get Contacts"));
e_book_query_unref (query);
return -1;
}
e_book_query_unref (query);
/* Count and hash the changes */
change_id = g_strdup_printf ("pilot-sync-evolution-addressbook-%d", ctxt->cfg->pilot_id);
if (!e_book_get_changes (ctxt->ebook, change_id, &ctxt->changed, NULL))
return -1;
ctxt->changed_hash = g_hash_table_new (g_str_hash, g_str_equal);
g_free (change_id);
for (l = ctxt->changed; l != NULL; l = l->next) {
EBookChange *ebc = l->data;
const char *uid;
uid = e_contact_get_const (ebc->contact, E_CONTACT_UID);
if (!e_pilot_map_uid_is_archived (ctxt->map, uid)) {
g_hash_table_insert (ctxt->changed_hash, g_strdup (uid), ebc);
switch (ebc->change_type) {
case E_BOOK_CHANGE_CARD_ADDED:
add_records++;
break;
case E_BOOK_CHANGE_CARD_MODIFIED:
mod_records++;
break;
case E_BOOK_CHANGE_CARD_DELETED:
del_records++;
break;
}
} else if (ebc->change_type == E_BOOK_CHANGE_CARD_DELETED) {
e_pilot_map_remove_by_uid (ctxt->map, uid);
}
}
/* Set the count information */
num_records = g_list_length (ctxt->cards);
gnome_pilot_conduit_sync_abs_set_num_local_records(abs_conduit, num_records);
gnome_pilot_conduit_sync_abs_set_num_new_local_records (abs_conduit, add_records);
gnome_pilot_conduit_sync_abs_set_num_updated_local_records (abs_conduit, mod_records);
gnome_pilot_conduit_sync_abs_set_num_deleted_local_records(abs_conduit, del_records);
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 Address application block"));
WARN ("dlp_ReadAppBlock(...) = %d", len);
gnome_pilot_conduit_error (conduit,
_("Could not read pilot's Address application block"));
return -1;
}
unpack_AddressAppInfo (&(ctxt->ai), buf, len);
g_free (buf);
check_for_slow_setting (conduit, ctxt);
if (ctxt->cfg->sync_type == GnomePilotConduitSyncTypeCopyToPilot
|| ctxt->cfg->sync_type == GnomePilotConduitSyncTypeCopyFromPilot)
ctxt->map->write_touched_only = TRUE;
return 0;
}
static gint
post_sync (GnomePilotConduit *conduit,
GnomePilotDBInfo *dbi,
EAddrConduitContext *ctxt)
{
GList *changed;
gchar *filename, *change_id;
LOG (g_message ( "post_sync: Address Conduit v.%s", CONDUIT_VERSION ));
g_free (ctxt->cfg->last_uri);
ctxt->cfg->last_uri = g_strdup (e_book_get_uri (ctxt->ebook));
addrconduit_save_configuration (ctxt->cfg);
filename = map_name (ctxt);
e_pilot_map_write (filename, ctxt->map);
g_free (filename);
/* FIX ME ugly hack - our changes musn't count, this does introduce
* a race condition if anyone changes a record elsewhere during sycnc
*/
change_id = g_strdup_printf ("pilot-sync-evolution-addressbook-%d", ctxt->cfg->pilot_id);
if (e_book_get_changes (ctxt->ebook, change_id, &changed, NULL))
e_book_free_change_list (changed);
g_free (change_id);
LOG (g_message ( "---------------------------------------------------------\n" ));
return 0;
}
static gint
set_pilot_id (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
guint32 ID,
EAddrConduitContext *ctxt)
{
LOG (g_message ( "set_pilot_id: setting to %d\n", ID ));
e_pilot_map_insert (ctxt->map, ID, e_contact_get_const (local->contact, E_CONTACT_UID), FALSE);
return 0;
}
static gint
set_status_cleared (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
EAddrConduitContext *ctxt)
{
const char *uid;
LOG (g_message ( "set_status_cleared: clearing status\n" ));
if ((uid = e_contact_get_const (local->contact, E_CONTACT_UID)))
g_hash_table_remove (ctxt->changed_hash, uid);
return 0;
}
static gint
for_each (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord **local,
EAddrConduitContext *ctxt)
{
static GList *cards, *iterator;
static int count;
g_return_val_if_fail (local != NULL, -1);
if (*local == NULL) {
LOG (g_message ( "beginning for_each" ));
cards = ctxt->cards;
count = 0;
if (cards != NULL) {
LOG (g_message ( "iterating over %d records", g_list_length (cards) ));
*local = g_new0 (EAddrLocalRecord, 1);
local_record_from_ecard (*local, cards->data, ctxt);
g_list_prepend (ctxt->locals, *local);
iterator = cards;
} else {
LOG (g_message ( "no events" ));
(*local) = NULL;
return 0;
}
} else {
count++;
if (g_list_next (iterator)) {
iterator = g_list_next (iterator);
*local = g_new0 (EAddrLocalRecord, 1);
local_record_from_ecard (*local, iterator->data, ctxt);
g_list_prepend (ctxt->locals, *local);
} else {
LOG (g_message ( "for_each ending" ));
/* Tell the pilot the iteration is over */
*local = NULL;
return 0;
}
}
return 0;
}
static gint
for_each_modified (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord **local,
EAddrConduitContext *ctxt)
{
static GList *iterator;
static int count;
g_return_val_if_fail (local != NULL, 0);
if (*local == NULL) {
LOG (g_message ( "for_each_modified beginning\n" ));
iterator = ctxt->changed;
count = 0;
iterator = next_changed_item (ctxt, iterator);
if (iterator != NULL) {
EBookChange *ebc = iterator->data;
LOG (g_message ( "iterating over %d records", g_hash_table_size (ctxt->changed_hash)));
*local = g_new0 (EAddrLocalRecord, 1);
local_record_from_ecard (*local, ebc->contact, ctxt);
g_list_prepend (ctxt->locals, *local);
} else {
LOG (g_message ( "no events" ));
*local = NULL;
}
} else {
count++;
iterator = g_list_next (iterator);
if (iterator && (iterator = next_changed_item (ctxt, iterator))) {
EBookChange *ebc = iterator->data;
*local = g_new0 (EAddrLocalRecord, 1);
local_record_from_ecard (*local, ebc->contact, ctxt);
g_list_prepend (ctxt->locals, *local);
} else {
LOG (g_message ( "for_each_modified ending" ));
/* Signal the iteration is over */
*local = NULL;
return 0;
}
}
return 0;
}
static gint
compare (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
GnomePilotRecord *remote,
EAddrConduitContext *ctxt)
{
GnomePilotRecord local_pilot;
int retval = 0;
LOG (g_message ("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 (remote->length != local_pilot.length
|| memcmp (local_pilot.record, remote->record, remote->length))
retval = 1;
if (retval == 0)
LOG (g_message ( " equal" ));
else
LOG (g_message ( " not equal" ));
return retval;
}
static gint
add_record (GnomePilotConduitSyncAbs *conduit,
GnomePilotRecord *remote,
EAddrConduitContext *ctxt)
{
EContact *contact;
int retval = 0;
g_return_val_if_fail (remote != NULL, -1);
LOG (g_message ( "add_record: adding %s to desktop\n", print_remote (remote) ));
contact = ecard_from_remote_record (ctxt, remote, NULL);
/* add the ecard to the server */
if (!e_book_add_contact (ctxt->ebook, contact, NULL)) {
WARN ("add_record: failed to add card to ebook\n");
g_object_unref (contact);
return -1;
}
e_pilot_map_insert (ctxt->map, remote->ID, e_contact_get (contact, E_CONTACT_UID), FALSE);
g_object_unref (contact);
return retval;
}
static gint
replace_record (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
GnomePilotRecord *remote,
EAddrConduitContext *ctxt)
{
EContact *new_contact;
EBookChange *ebc;
char *old_id;
int retval = 0;
g_return_val_if_fail (remote != NULL, -1);
LOG (g_message ("replace_record: replace %s with %s\n",
print_local (local), print_remote (remote)));
old_id = e_contact_get (local->contact, E_CONTACT_UID);
ebc = g_hash_table_lookup (ctxt->changed_hash, old_id);
new_contact = ecard_from_remote_record (ctxt, remote, local->contact);
g_object_unref (local->contact);
local->contact = new_contact;
if (ebc && ebc->change_type == E_BOOK_CHANGE_CARD_DELETED) {
if (!e_book_add_contact (ctxt->ebook, local->contact, NULL)) {
WARN (G_STRLOC ": failed to add card\n");
return -1;
}
} else {
if (!e_book_commit_contact (ctxt->ebook, local->contact, NULL)) {
WARN (G_STRLOC ": failed to commit card\n");
return -1;
}
}
/* Adding a record causes wombat to assign a new uid so we must tidy */
if (ebc && ebc->change_type == E_BOOK_CHANGE_CARD_DELETED) {
const char *uid = e_contact_get_const (local->contact, E_CONTACT_UID);
gboolean arch;
arch = e_pilot_map_uid_is_archived (ctxt->map, uid);
e_pilot_map_insert (ctxt->map, remote->ID, uid, arch);
ebc = g_hash_table_lookup (ctxt->changed_hash, old_id);
if (ebc) {
g_hash_table_remove (ctxt->changed_hash, old_id);
g_object_unref (ebc->contact);
g_object_ref (local->contact);
ebc->contact = local->contact;
/* FIXME We should possibly be duplicating the uid */
g_hash_table_insert (ctxt->changed_hash, (gpointer) uid, ebc);
}
}
return retval;
}
static gint
delete_record (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
EAddrConduitContext *ctxt)
{
GError *error = NULL;
int retval = 0;
g_return_val_if_fail (local != NULL, -1);
g_return_val_if_fail (local->contact != NULL, -1);
LOG (g_message ( "delete_record: delete %s\n", print_local (local) ));
e_pilot_map_remove_by_uid (ctxt->map, e_contact_get_const (local->contact, E_CONTACT_UID));
if (!e_book_remove_contact (ctxt->ebook, e_contact_get_const (local->contact, E_CONTACT_UID), &error) && error->code != E_BOOK_ERROR_CONTACT_NOT_FOUND) {
WARN ("delete_record: failed to delete card in ebook\n");
g_error_free (error);
retval = -1;
}
return retval;
}
static gint
archive_record (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
gboolean archive,
EAddrConduitContext *ctxt)
{
int retval = 0;
g_return_val_if_fail (local != NULL, -1);
LOG (g_message ( "archive_record: %s\n", archive ? "yes" : "no" ));
e_pilot_map_insert (ctxt->map, local->local.ID, e_contact_get_const (local->contact, E_CONTACT_UID), archive);
return retval;
}
static gint
match (GnomePilotConduitSyncAbs *conduit,
GnomePilotRecord *remote,
EAddrLocalRecord **local,
EAddrConduitContext *ctxt)
{
const char *uid;
LOG (g_message ("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, TRUE);
if (!uid)
return 0;
LOG (g_message ( " matched\n" ));
*local = g_new0 (EAddrLocalRecord, 1);
local_record_from_uid (*local, uid, ctxt);
return 0;
}
static gint
free_match (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
EAddrConduitContext *ctxt)
{
LOG (g_message ( "free_match: freeing\n" ));
g_return_val_if_fail (local != NULL, -1);
addrconduit_destroy_record (local);
return 0;
}
static gint
prepare (GnomePilotConduitSyncAbs *conduit,
EAddrLocalRecord *local,
GnomePilotRecord *remote,
EAddrConduitContext *ctxt)
{
LOG (g_message ( "prepare: encoding local %s\n", print_local (local) ));
*remote = local_record_to_pilot_record (local, ctxt);
return 0;
}
/* Pilot Settings Callbacks */
static void
fill_widgets (EAddrConduitContext *ctxt)
{
if (ctxt->cfg->source)
e_pilot_settings_set_source (E_PILOT_SETTINGS (ctxt->ps),
ctxt->cfg->source);
e_pilot_settings_set_secret (E_PILOT_SETTINGS (ctxt->ps),
ctxt->cfg->secret);
e_addr_gui_fill_widgets (ctxt->gui, ctxt->cfg);
}
static gint
create_settings_window (GnomePilotConduit *conduit,
GtkWidget *parent,
EAddrConduitContext *ctxt)
{
LOG (g_message ( "create_settings_window" ));
if (!ctxt->cfg->source_list)
return -1;
ctxt->ps = e_pilot_settings_new (ctxt->cfg->source_list);
ctxt->gui = e_addr_gui_new (E_PILOT_SETTINGS (ctxt->ps));
gtk_container_add (GTK_CONTAINER (parent), ctxt->ps);
gtk_widget_show (ctxt->ps);
fill_widgets (ctxt);
return 0;
}
static void
display_settings (GnomePilotConduit *conduit, EAddrConduitContext *ctxt)
{
LOG (g_message ( "display_settings" ));
fill_widgets (ctxt);
}
static void
save_settings (GnomePilotConduit *conduit, EAddrConduitContext *ctxt)
{
LOG (g_message ( "save_settings" ));
if (ctxt->new_cfg->source)
g_object_unref (ctxt->new_cfg->source);
ctxt->new_cfg->source = e_pilot_settings_get_source (E_PILOT_SETTINGS (ctxt->ps));
g_object_ref (ctxt->new_cfg->source);
ctxt->new_cfg->secret =
e_pilot_settings_get_secret (E_PILOT_SETTINGS (ctxt->ps));
e_addr_gui_fill_config (ctxt->gui, ctxt->new_cfg);
addrconduit_save_configuration (ctxt->new_cfg);
}
static void
revert_settings (GnomePilotConduit *conduit, EAddrConduitContext *ctxt)
{
LOG (g_message ( "revert_settings" ));
addrconduit_save_configuration (ctxt->cfg);
addrconduit_destroy_configuration (ctxt->new_cfg);
ctxt->new_cfg = addrconduit_dupe_configuration (ctxt->cfg);
}
GnomePilotConduit *
conduit_get_gpilot_conduit (guint32 pilot_id)
{
GtkObject *retval;
EAddrConduitContext *ctxt;
LOG (g_message ( "in address's conduit_get_gpilot_conduit\n" ));
retval = gnome_pilot_conduit_sync_abs_new ("AddressDB", 0x61646472);
g_assert (retval != NULL);
ctxt = e_addr_context_new (pilot_id);
gtk_object_set_data (GTK_OBJECT (retval), "addrconduit_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, "set_status_cleared", (GtkSignalFunc) set_status_cleared, 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, "replace_record", (GtkSignalFunc) replace_record, ctxt);
gtk_signal_connect (retval, "delete_record", (GtkSignalFunc) delete_record, ctxt);
gtk_signal_connect (retval, "archive_record", (GtkSignalFunc) 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);
/* Gui Settings */
gtk_signal_connect (retval, "create_settings_window", (GtkSignalFunc) create_settings_window, ctxt);
gtk_signal_connect (retval, "display_settings", (GtkSignalFunc) display_settings, ctxt);
gtk_signal_connect (retval, "save_settings", (GtkSignalFunc) save_settings, ctxt);
gtk_signal_connect (retval, "revert_settings", (GtkSignalFunc) revert_settings, ctxt);
return GNOME_PILOT_CONDUIT (retval);
}
void
conduit_destroy_gpilot_conduit (GnomePilotConduit *conduit)
{
EAddrConduitContext *ctxt;
ctxt = gtk_object_get_data (GTK_OBJECT (conduit),
"addrconduit_context");
e_addr_context_destroy (ctxt);
gtk_object_destroy (GTK_OBJECT (conduit));
}
|