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
|
/*
This file is part of KAddressbook.
Copyright (c) 2003-2006 Helge Deller <deller@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
/*
Description:
This filter allows you to import and export the KDE addressbook entries
to/from a mobile phone, which is accessible via gnokii.
Gnokii homepage: http://www.gnokii.org
TODO:
- create a log file and give user possibility to see it afterwards
- handle callergroup value (Friend, VIP, Family, ...) better
*/
#include "config.h"
#ifdef HAVE_GNOKII_H
extern "C" {
#include <gnokii.h>
}
#else
#ifdef __GNUC__
# warning "Please install gnokii (http://www.gnokii.org) development headers and libraries !"
# warning "Please use at least version 0.6.13 or later of gnokii."
#endif
#endif
#include <qcursor.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprogress.h>
#include <kguiitem.h>
#include "gnokii_xxport.h"
#define APP "GNOKII_XXPORT"
#if 1 // !defined(NDEBUG)
#define GNOKII_DEBUG(x) do { kdWarning() << (x); } while (0)
#else
#define GNOKII_DEBUG(x) do { } while (0)
#endif
#define GNOKII_CHECK_ERROR(error) \
do { \
if (error) \
kdError() << QString("ERROR %1: %2\n").arg(error).arg(gn_error_print(error));\
} while (0)
// Locale conversion routines:
// Gnokii uses the local 8 Bit encoding (based on LC_ALL), kaddressbook uses Unicode
#define GN_FROM(x) QString::fromLocal8Bit(x)
#define GN_TO(x) (x).local8Bit()
// static variables for GUI updates
static GNOKIIXXPort *this_filter;
static KProgressDialog *m_progressDlg;
K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_gnokii_xxport, GNOKIIXXPort )
GNOKIIXXPort::GNOKIIXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
: KAB::XXPort( ab, parent, name )
{
this_filter = this;
m_progressDlg = NULL;
createImportAction( i18n( "Import From Mobile Phone..." ) );
createExportAction( i18n( "Export to Mobile Phone..." ) );
}
#ifdef HAVE_GNOKII_H
static QString makeValidPhone( const QString &number )
{
// allowed chars: 0-9, *, #, p, w, +
QString num = number.simplifyWhiteSpace();
QString allowed("0123456789*+#pw");
for (unsigned int i=num.length(); i>=1; i--)
if (allowed.find(num[i-1])==-1)
num.remove(i-1,1);
if (num.isEmpty())
num = "0";
return num;
}
#endif
/******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************/
#if defined(HAVE_GNOKII_H) && defined(LIBGNOKII_VERSION_MAJOR) && (LIBGNOKII_VERSION_MAJOR >= 3)
/* NEW GNOKII LIBRARIES (>= 0.6.13) */
static const char *manufacturer, *model, *revision, *imei;
static struct gn_statemachine *state;
static void busterminate(void)
{
gn_lib_phone_close(state);
gn_lib_phoneprofile_free(&state);
gn_lib_library_free();
}
static QString businit(void)
{
GNOKII_DEBUG( "Using new gnokii version." );
GNOKII_DEBUG( QString("Compiled with libgnokii version 0x%1\n").arg(QString::number(LIBGNOKII_VERSION,16)) );
GNOKII_DEBUG( QString("Using libgnokii runtime version 0x%1\n").arg(QString::number(gn_lib_version(),16)) );
gn_error error = gn_lib_phoneprofile_load(NULL, &state);
if (error)
return i18n("Failed to initialize the gnokii library.");
error = gn_lib_phone_open( state );
GNOKII_CHECK_ERROR(error);
if (error != GN_ERR_NONE) {
busterminate();
return i18n("<qt><center>Mobile Phone interface initialization failed.<br><br>"
"The returned error message was:<br><b>%1</b><br><br>"
"You might try to run \"gnokii --identify\" on the command line to "
"check any cable/transport issues and to verify if your gnokii "
"configuration is correct.</center></qt>")
.arg(gn_error_print(error));
}
// identify phone
manufacturer = gn_lib_get_phone_manufacturer(state);
model = gn_lib_get_phone_model(state);
revision = gn_lib_get_phone_revision(state);
imei = gn_lib_get_phone_imei(state);
GNOKII_DEBUG( QString("Found mobile phone: %1 %2, Revision: %3, IMEI: %4\n")
.arg(manufacturer, model, revision, imei) );
return QString::null;
}
// get number of entries in this phone memory type (internal/SIM-card)
static gn_error read_phone_memstat( const gn_memory_type memtype, gn_memory_status *memstat )
{
gn_error error;
error = gn_lib_addressbook_memstat(state, memtype, &memstat->used, &memstat->free);
GNOKII_DEBUG( QString("\n\nMobile phone memory status: Type: %1, used=%2, free=%3, total=%4\n\n")
.arg(memtype).arg(memstat->used).arg(memstat->free).arg(memstat->used+memstat->free) );
return error;
}
static QString buildPhoneInfoString( const gn_memory_status &memstat )
{
QString format = QString::fromLatin1("<tr><td><b>%1</b></td><td>%2</td></tr>");
return QString::fromLatin1("<b>%1</b><br><table>%2%3%4%5%6</table><br>")
.arg(i18n("Mobile Phone information:"))
.arg(format.arg(i18n("Manufacturer")).arg(GN_FROM(manufacturer)))
.arg(format.arg(i18n("Phone model")).arg(GN_FROM(model)))
.arg(format.arg(i18n("Revision")).arg(GN_FROM(revision)))
.arg(format.arg(i18n("IMEI")).arg(GN_FROM(imei)))
.arg(format.arg(i18n("Phonebook status"))
.arg(i18n("%1 out of %2 contacts used").arg(memstat.used).arg(memstat.used+memstat.free)));
}
// read and evaluate all phone entries
static gn_error read_phone_entries( const char *memtypestr, gn_memory_type memtype,
KABC::AddresseeList *addrList )
{
gn_error error;
if (m_progressDlg->wasCancelled())
return GN_ERR_NONE;
KProgress* progress = (KProgress*)m_progressDlg->progressBar();
progress->setProgress(0);
this_filter->processEvents();
// get number of entries in this phone memory type (internal/SIM-card)
gn_memory_status memstat;
error = read_phone_memstat(memtype, &memstat);
QStringList addrlist;
KABC::Address *addr;
QString s, country;
progress->setTotalSteps(memstat.used);
m_progressDlg->setLabel(i18n("<qt>Importing <b>%1</b> contacts from <b>%2</b> of the Mobile Phone.<br><br>%3</qt>")
.arg(memstat.used)
.arg(gn_memory_type2str(memtype))
.arg(buildPhoneInfoString(memstat)) );
int num_read = 0;
for (int i = 1; !m_progressDlg->wasCancelled() && i <= memstat.used + memstat.free; i++) {
error = gn_lib_phonebook_read_entry(state, memtype, i);
GNOKII_CHECK_ERROR(error);
progress->setProgress(num_read);
this_filter->processEvents();
if (error == GN_ERR_EMPTYLOCATION)
continue;
if (error == GN_ERR_INVALIDLOCATION)
break;
if (error == GN_ERR_INVALIDMEMORYTYPE)
break;
if (error == GN_ERR_NONE) {
const int subentries_count = gn_lib_get_pb_num_subentries(state);
const char *name = gn_lib_get_pb_name(state);
const char *number = gn_lib_get_pb_number(state);
GNOKII_DEBUG(QString("%1: %2, num=%3, location=%4, group=%5, count=%6\n").arg(i)
.arg(GN_FROM(name)).arg(GN_FROM(number))
.arg(gn_lib_get_pb_location(state)).arg(gn_lib_get_pb_caller_group(state))
.arg(subentries_count));
KABC::Addressee *a = new KABC::Addressee();
// try to split Name into FamilyName and GivenName
s = GN_FROM(name).simplifyWhiteSpace();
a->setFormattedName(s); // set formatted name as in Phone
if (s.find(',') == -1) {
// assumed format: "givenname [... familyname]"
addrlist = QStringList::split(' ', s);
if (addrlist.count() == 1) {
// only one string -> put it in the GivenName
a->setGivenName(s);
} else {
// multiple strings -> split them.
a->setFamilyName(addrlist.last().simplifyWhiteSpace());
addrlist.remove(addrlist.last());
a->setGivenName(addrlist.join(" ").simplifyWhiteSpace());
}
} else {
// assumed format: "familyname, ... givenname"
addrlist = QStringList::split(',', s);
a->setFamilyName(addrlist.first().simplifyWhiteSpace());
addrlist.remove(addrlist.first());
a->setGivenName(addrlist.join(" ").simplifyWhiteSpace());
}
a->insertCustom(APP, "X_GSM_CALLERGROUP", s.setNum(gn_lib_get_pb_caller_group(state)));
a->insertCustom(APP, "X_GSM_STORE_AT", QString("%1%2").arg(memtypestr).arg(gn_lib_get_pb_location(state)));
// set ProductId
a->setProductId(QString("%1-%2-%3-%4").arg(APP).arg(model).arg(revision).arg(imei));
// evaluate timestamp (ignore timezone)
QDateTime datetime;
gn_timestamp ts = gn_lib_get_pb_date(state);
if (ts.year<1998)
datetime = QDateTime::currentDateTime();
else
datetime = QDateTime( QDate(ts.year, ts.month, ts.day),
QTime(ts.hour, ts.minute, ts.second) );
GNOKII_DEBUG(QString(" date=%1\n").arg(datetime.toString()));
a->setRevision(datetime);
if (!subentries_count)
a->insertPhoneNumber(KABC::PhoneNumber(number,
KABC::PhoneNumber::Work | KABC::PhoneNumber::Pref));
/* scan sub-entries */
if (subentries_count)
for (int n=0; n<subentries_count; n++) {
gn_phonebook_entry_type entry_type;
gn_phonebook_number_type number_type;
const char *number;
error = gn_lib_get_pb_subentry(state, n, &entry_type, &number_type, &number);
GNOKII_CHECK_ERROR(error);
QString s = GN_FROM(number).simplifyWhiteSpace();
GNOKII_DEBUG(QString(" Subentry#%1, entry_type=%2, number_type=%3, number=%4\n")
.arg(n).arg(entry_type).arg(number_type).arg(s));
if (s.isEmpty())
continue;
switch(entry_type) {
case GN_PHONEBOOK_ENTRY_Name:
a->setName(s);
break;
case GN_PHONEBOOK_ENTRY_Email:
a->insertEmail(s);
break;
case GN_PHONEBOOK_ENTRY_Postal:
addrlist = QStringList::split(';', s, true);
addr = new KABC::Address(KABC::Address::Work);
if (addrlist.count() <= 1) {
addrlist = QStringList::split(',', s, true);
if (addrlist.count() > 1 ) {
// assumed format: "Locality, ZIP, Country"
addr->setLocality(addrlist[0]);
addr->setPostalCode(addrlist[1]);
if (!addrlist[2].isEmpty())
addr->setCountry(i18n(GN_TO(addrlist[2])));
} else {
// no idea about the format, just store it.
addr->setLocality(s);
}
} else {
// assumed format: "POBox; Extended; Street; Locality; Region; ZIP [;Country]
addr->setPostOfficeBox(addrlist[0]);
addr->setExtended(addrlist[1]);
addr->setStreet(addrlist[2]);
addr->setLocality(addrlist[3]);
addr->setRegion(addrlist[4]);
addr->setPostalCode(addrlist[5]);
country = addrlist[6];
if (!country.isEmpty())
addr->setCountry(i18n(GN_TO(country)));
}
a->insertAddress(*addr);
delete addr;
break;
case GN_PHONEBOOK_ENTRY_Note:
if (!a->note().isEmpty())
s = "\n" + s;
a->setNote(a->note()+s);
break;
case GN_PHONEBOOK_ENTRY_Number:
enum KABC::PhoneNumber::Types phonetype;
switch (number_type) {
case GN_PHONEBOOK_NUMBER_Mobile: phonetype = KABC::PhoneNumber::Cell; break;
case GN_PHONEBOOK_NUMBER_Fax: phonetype = KABC::PhoneNumber::Fax; break;
case GN_PHONEBOOK_NUMBER_General:
case GN_PHONEBOOK_NUMBER_Work: phonetype = KABC::PhoneNumber::Work; break;
default:
case GN_PHONEBOOK_NUMBER_Home: phonetype = KABC::PhoneNumber::Home; break;
}
//if (s == entry.number)
// type = (KABC::PhoneNumber::Types) (phonetype | KABC::PhoneNumber::Pref);
a->insertPhoneNumber(KABC::PhoneNumber(s, phonetype));
break;
case GN_PHONEBOOK_ENTRY_URL:
a->setUrl(s);
break;
case GN_PHONEBOOK_ENTRY_Group:
a->insertCategory(s);
break;
default:
GNOKII_DEBUG(QString(" Not handled id=%1, entry=%2\n")
.arg(entry_type).arg(s));
break;
} // switch()
} // if(subentry)
// add only if entry was valid
if (strlen(name) || strlen(number) || subentries_count)
addrList->append(*a);
// did we read all valid phonebook-entries ?
num_read++;
delete a;
if (num_read >= memstat.used)
break; // yes, all were read
else
continue; // no, we are still missing some.
}
GNOKII_CHECK_ERROR(error);
}
return GN_ERR_NONE;
}
// export to phone
static gn_error xxport_phone_write_entry( int phone_location, gn_memory_type memtype,
const KABC::Addressee *addr)
{
QString s;
/* initialize the phonebook entry values to zero */
gn_lib_phonebook_prepare_write_entry(state);
gn_lib_set_pb_location(state, phone_location);
gn_lib_set_pb_name(state, GN_TO(addr->realName()));
s = addr->phoneNumber(KABC::PhoneNumber::Pref).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Work).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Home).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Cell).number();
if (s.isEmpty() && addr->phoneNumbers().count()>0)
s = (*addr->phoneNumbers().at(0)).number();
s = makeValidPhone(s);
gn_lib_set_pb_number(state, s.ascii());
gn_lib_set_pb_memtype(state, memtype);
QString cg = addr->custom(APP, "X_GSM_CALLERGROUP");
if (cg.isEmpty())
gn_lib_set_pb_caller_group(state, GN_PHONEBOOK_GROUP_None); // default group
else
gn_lib_set_pb_caller_group(state, (gn_phonebook_group_type) cg.toInt());
// set date/revision
QDateTime datetime = addr->revision();
QDate date(datetime.date());
QTime time(datetime.time());
gn_timestamp ts;
gn_timestamp_set( &ts, date.year(), date.month(), date.day(),
time.hour(), time.minute(), time.second(), 0 );
gn_lib_set_pb_date(state, ts);
GNOKII_DEBUG(QString("Write #%1: name=%2, number=%3\n").arg(phone_location)
.arg(GN_FROM(gn_lib_get_pb_name(state))).arg(GN_FROM(gn_lib_get_pb_number(state))));
const KABC::Address homeAddr = addr->address(KABC::Address::Home);
const KABC::Address workAddr = addr->address(KABC::Address::Work);
// add all phone numbers
const KABC::PhoneNumber::List phoneList = addr->phoneNumbers();
KABC::PhoneNumber::List::ConstIterator it;
for ( it = phoneList.begin(); it != phoneList.end(); ++it ) {
const KABC::PhoneNumber *phonenumber = &(*it);
s = phonenumber->number();
if (s.isEmpty()) continue;
gn_phonebook_number_type type;
int pn_type = phonenumber->type();
if ((pn_type & KABC::PhoneNumber::Cell))
type = GN_PHONEBOOK_NUMBER_Mobile;
else if ((pn_type & KABC::PhoneNumber::Fax))
type = GN_PHONEBOOK_NUMBER_Fax;
else if ((pn_type & KABC::PhoneNumber::Home))
type = GN_PHONEBOOK_NUMBER_Home;
else if ((pn_type & KABC::PhoneNumber::Work))
type = GN_PHONEBOOK_NUMBER_Work;
else type = GN_PHONEBOOK_NUMBER_General;
gn_lib_set_pb_subentry(state, -1 /* index to append entry */,
GN_PHONEBOOK_ENTRY_Number, type, makeValidPhone(s).ascii());
/*subentry->id = phone_location<<8+entry.subentries_count;*/
}
// add URL
s = addr->url().prettyURL();
if (!s.isEmpty()) {
gn_lib_set_pb_subentry(state, -1 /* index to append entry */,
GN_PHONEBOOK_ENTRY_URL, GN_PHONEBOOK_NUMBER_General, GN_TO(s));
}
// add E-Mails
QStringList emails = addr->emails();
for (unsigned int n=0; n<emails.count(); n++) {
s = emails[n].simplifyWhiteSpace();
if (s.isEmpty()) continue;
// only one email allowed if we have URLS, notes, addresses (to avoid phone limitations)
if (n && !addr->url().isEmpty() && !addr->note().isEmpty() && addr->addresses().count()) {
GNOKII_DEBUG(QString(" DROPPED email %1 in favor of URLs, notes and addresses.\n")
.arg(s));
continue;
}
gn_lib_set_pb_subentry(state, -1 /* index to append entry */,
GN_PHONEBOOK_ENTRY_Email, GN_PHONEBOOK_NUMBER_General, GN_TO(s));
}
// add Adresses
const KABC::Address::List addresses = addr->addresses();
KABC::Address::List::ConstIterator it2;
for ( it2 = addresses.begin(); it2 != addresses.end(); ++it2 ) {
const KABC::Address *Addr = &(*it2);
if (Addr->isEmpty()) continue;
QStringList a;
QChar sem(';');
QString sem_repl(QString::fromLatin1(","));
a.append( Addr->postOfficeBox().replace( sem, sem_repl ) );
a.append( Addr->extended() .replace( sem, sem_repl ) );
a.append( Addr->street() .replace( sem, sem_repl ) );
a.append( Addr->locality() .replace( sem, sem_repl ) );
a.append( Addr->region() .replace( sem, sem_repl ) );
a.append( Addr->postalCode() .replace( sem, sem_repl ) );
a.append( Addr->country() .replace( sem, sem_repl ) );
s = a.join(sem);
gn_lib_set_pb_subentry(state, -1 /* index to append entry */,
GN_PHONEBOOK_ENTRY_Postal, GN_PHONEBOOK_NUMBER_General, GN_TO(s));
}
// add Note
s = addr->note().simplifyWhiteSpace();
if (!s.isEmpty()) {
gn_lib_set_pb_subentry(state, -1 /* index to append entry */,
GN_PHONEBOOK_ENTRY_Note, GN_PHONEBOOK_NUMBER_General, GN_TO(s));
}
// debug output
for (int st=0; st<gn_lib_get_pb_num_subentries(state); st++) {
gn_phonebook_entry_type entry_type;
gn_phonebook_number_type number_type;
const char *number;
gn_lib_get_pb_subentry(state, st, &entry_type, &number_type, &number);
GNOKII_DEBUG(QString(" SubTel #%1: entry_type=%2, number_type=%3, number=%4\n")
.arg(st).arg(entry_type)
.arg(number_type).arg(GN_FROM(number)));
}
gn_error error = gn_lib_phonebook_write_entry(state, memtype, phone_location);
GNOKII_CHECK_ERROR(error);
return error;
}
static gn_error xxport_phone_delete_entry( int phone_location, gn_memory_type memtype )
{
return gn_lib_phonebook_entry_delete(state, memtype, phone_location);
}
KABC::AddresseeList GNOKIIXXPort::importContacts( const QString& ) const
{
KABC::AddresseeList addrList;
if (KMessageBox::Continue != KMessageBox::warningContinueCancel(parentWidget(),
i18n("<qt>Please connect your Mobile Phone to your computer and press "
"<b>Continue</b> to start importing the personal contacts.<br><br>"
"Please note that if your Mobile Phone is not properly connected "
"the following detection phase might take up to two minutes, during which "
"KAddressbook will behave unresponsively.</qt>") ))
return addrList;
m_progressDlg = new KProgressDialog( parentWidget(), "importwidget",
i18n("Mobile Phone Import"),
i18n("<qt><center>Establishing connection to the Mobile Phone.<br><br>"
"Please wait...</center></qt>") );
m_progressDlg->setAllowCancel(true);
m_progressDlg->progressBar()->setProgress(0);
m_progressDlg->progressBar()->setCenterIndicator(true);
m_progressDlg->setModal(true);
m_progressDlg->setInitialSize(QSize(450,350));
m_progressDlg->show();
processEvents();
#if (QT_VERSION >= 0x030300)
m_progressDlg->setCursor( Qt::BusyCursor );
#endif
QString errStr = businit();
m_progressDlg->unsetCursor();
if (!errStr.isEmpty()) {
KMessageBox::error(parentWidget(), errStr);
delete m_progressDlg;
return addrList;
}
GNOKII_DEBUG("GNOKII import filter started.\n");
m_progressDlg->setButtonText(i18n("&Stop Import"));
read_phone_entries("ME", GN_MT_ME, &addrList); // internal phone memory
read_phone_entries("SM", GN_MT_SM, &addrList); // SIM card
GNOKII_DEBUG("GNOKII import filter finished.\n");
busterminate();
delete m_progressDlg;
return addrList;
}
bool GNOKIIXXPort::exportContacts( const KABC::AddresseeList &list, const QString & )
{
if (KMessageBox::Continue != KMessageBox::warningContinueCancel(parentWidget(),
i18n("<qt>Please connect your Mobile Phone to your computer and press "
"<b>Continue</b> to start exporting the selected personal contacts.<br><br>"
"Please note that if your Mobile Phone is not properly connected "
"the following detection phase might take up to two minutes, during which "
"KAddressbook will behave unresponsively.</qt>") ))
return false;
m_progressDlg = new KProgressDialog( parentWidget(), "importwidget",
i18n("Mobile Phone Export"),
i18n("<qt><center>Establishing connection to the Mobile Phone.<br><br>"
"Please wait...</center></qt>") );
m_progressDlg->setAllowCancel(true);
m_progressDlg->progressBar()->setProgress(0);
m_progressDlg->progressBar()->setCenterIndicator(true);
m_progressDlg->setModal(true);
m_progressDlg->setInitialSize(QSize(450,350));
m_progressDlg->show();
processEvents();
KProgress* progress = (KProgress*)m_progressDlg->progressBar();
KABC::AddresseeList::ConstIterator it;
QStringList failedList;
gn_error error;
bool deleteLabelInitialized = false;
#if (QT_VERSION >= 0x030300)
m_progressDlg->setCursor( Qt::BusyCursor );
#endif
QString errStr = businit();
m_progressDlg->unsetCursor();
if (!errStr.isEmpty()) {
KMessageBox::error(parentWidget(), errStr);
delete m_progressDlg;
return false;
}
GNOKII_DEBUG("GNOKII export filter started.\n");
gn_memory_type memtype = GN_MT_ME; // internal phone memory
int phone_count; // num entries in phone
bool overwrite_phone_entries = false;
int phone_entry_no, entries_written;
bool entry_empty;
// get number of entries in this phone memory
gn_memory_status memstat;
error = read_phone_memstat(memtype, &memstat);
if (error == GN_ERR_NONE) {
GNOKII_DEBUG("Writing to internal phone memory.\n");
} else {
memtype = GN_MT_SM; // try SIM card instead
error = read_phone_memstat(memtype, &memstat);
if (error != GN_ERR_NONE)
goto finish;
GNOKII_DEBUG("Writing to SIM card memory.\n");
}
phone_count = memstat.used;
if (memstat.free >= (int) list.count()) {
if (KMessageBox::No == KMessageBox::questionYesNo(parentWidget(),
i18n("<qt>Do you want the selected contacts to be <b>appended</b> to "
"the current mobile phonebook or should they <b>replace</b> all "
"currently existing phonebook entries ?<br><br>"
"Please note, that in case you choose to replace the phonebook "
"entries, every contact in your phone will be deleted and only "
"the newly exported contacts will be available from inside your phone.</qt>"),
i18n("Export to Mobile Phone"),
KGuiItem(i18n("&Append to Current Phonebook")),
KGuiItem(i18n("&Replace Current Phonebook with New Contacts")) ) )
overwrite_phone_entries = true;
}
progress->setTotalSteps(list.count());
entries_written = 0;
progress->setProgress(entries_written);
m_progressDlg->setButtonText(i18n("&Stop Export"));
m_progressDlg->setLabel(i18n("<qt>Exporting <b>%1</b> contacts to the <b>%2</b> "
"of the Mobile Phone.<br><br>%3</qt>")
.arg(list.count())
.arg(gn_memory_type2str(memtype))
.arg(buildPhoneInfoString(memstat)) );
// Now run the loop...
phone_entry_no = 1;
for ( it = list.begin(); it != list.end(); ++it ) {
const KABC::Addressee *addr = &(*it);
if (addr->isEmpty())
continue;
// don't write back SIM-card entries !
if (addr->custom(APP, "X_GSM_STORE_AT").startsWith("SM"))
continue;
progress->setProgress(entries_written++);
try_next_phone_entry:
this_filter->processEvents();
if (m_progressDlg->wasCancelled())
break;
// End of phone memory reached ?
if (phone_entry_no > (memstat.used + memstat.free))
break;
GNOKII_DEBUG(QString("Try to write entry '%1' at phone_entry_no=%2, phone_count=%3\n")
.arg(addr->realName()).arg(phone_entry_no).arg(phone_count));
error = GN_ERR_NONE;
// is this phone entry empty ?
entry_empty = gn_lib_phonebook_entry_isempty(state, memtype, phone_entry_no);
if (overwrite_phone_entries) {
// overwrite this phonebook entry ...
if (!entry_empty)
phone_count--;
error = xxport_phone_write_entry( phone_entry_no, memtype, addr);
phone_entry_no++;
} else {
// add this phonebook entry if possible ...
if (entry_empty) {
error = xxport_phone_write_entry( phone_entry_no, memtype, addr);
phone_entry_no++;
} else {
phone_entry_no++;
goto try_next_phone_entry;
}
}
if (error != GN_ERR_NONE)
failedList.append(addr->realName());
// break if we got an error on the first entry
if (error != GN_ERR_NONE && it==list.begin())
break;
} // for()
// if we wanted to overwrite all entries, make sure, that we also
// delete all remaining entries in the mobile phone.
while (overwrite_phone_entries && error==GN_ERR_NONE && phone_count>0) {
if (m_progressDlg->wasCancelled())
break;
if (!deleteLabelInitialized) {
m_progressDlg->setLabel(
i18n("<qt><center>"
"All selected contacts have been sucessfully copied to "
"the Mobile Phone.<br><br>"
"Please wait until all remaining orphaned contacts from "
"the Mobile Phone have been deleted.</center></qt>") );
m_progressDlg->setButtonText(i18n("&Stop Delete"));
deleteLabelInitialized = true;
progress->setTotalSteps(phone_count);
entries_written = 0;
progress->setProgress(entries_written);
this_filter->processEvents();
}
if (phone_entry_no > (memstat.used + memstat.free))
break;
entry_empty = gn_lib_phonebook_entry_isempty(state, memtype, phone_entry_no);
if (!entry_empty) {
error = xxport_phone_delete_entry(phone_entry_no, memtype);
phone_count--;
progress->setProgress(++entries_written);
this_filter->processEvents();
}
phone_entry_no++;
}
finish:
m_progressDlg->setLabel(i18n("Export to phone finished."));
this_filter->processEvents();
GNOKII_DEBUG("GNOKII export filter finished.\n");
busterminate();
delete m_progressDlg;
if (!failedList.isEmpty()) {
GNOKII_DEBUG(QString("Failed to export: %1\n").arg(failedList.join(", ")));
KMessageBox::informationList(parentWidget(),
i18n("<qt>The following contacts could not be exported to the Mobile Phone. "
"Possible Reasons for this problem could be:<br><ul>"
"<li>The contacts contain more information per entry than the phone can store.</li>"
"<li>Your phone does not allow to store multiple addresses, emails, homepages, ...</li>"
"<li>other storage size related problems.</li>"
"</ul>"
"To avoid those kind of problems in the future please reduce the amount of different "
"fields in the above contacts.</qt>"),
failedList,
i18n("Mobile Phone Export") );
}
return true;
}
/******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************/
#elif defined(HAVE_GNOKII_H)
#ifdef __GNUC__
# warning "Please upgrade your gnokii installation to at least version 0.6.13"
# warning "Older gnokii versions below 0.6.13 are not binary compatible and"
# warning "prevents KDE users to upgrade gnokii to newer versions later."
#endif
/* OLD GNOKII LIBRARIES (< 0.6.13) */
/* import */
static char *lockfile = NULL;
static char manufacturer[64], model[GN_MODEL_MAX_LENGTH+1],
revision[GN_REVISION_MAX_LENGTH+1], imei[GN_IMEI_MAX_LENGTH+1];
static QString PhoneProductId;
static struct gn_statemachine state;
static gn_data data;
static void busterminate(void)
{
gn_sm_functions(GN_OP_Terminate, NULL, &state);
if (lockfile) gn_device_unlock(lockfile);
}
static QString businit(void)
{
gn_error error;
char *aux;
GNOKII_DEBUG( "Using old gnokii version." );
#if defined(LIBGNOKII_VERSION)
if (gn_cfg_read_default()<0)
#else
static char *BinDir;
if (gn_cfg_read(&BinDir)<0)
#endif
return i18n("Failed to initialize the gnokii library.");
if (!gn_cfg_phone_load("", &state))
return i18n("Gnokii is not yet configured.");
// uncomment to debug all gnokii communication on stderr.
// gn_log_debug_mask = GN_LOG_T_STDERR;
gn_data_clear(&data);
aux = gn_cfg_get(gn_cfg_info, "global", "use_locking");
// Defaults to 'no'
if (aux && !strcmp(aux, "yes")) {
lockfile = gn_device_lock(state.config.port_device);
if (lockfile == NULL) {
return i18n("Gnokii reports a 'Lock File Error'.\n "
"Please exit all other running instances of gnokii, check if you have "
"write permissions in the /var/lock directory and try again.");
}
}
// Initialise the code for the GSM interface.
int old_dcd = state.config.require_dcd; // work-around for older gnokii versions
state.config.require_dcd = false;
error = gn_gsm_initialise(&state);
GNOKII_CHECK_ERROR(error);
state.config.require_dcd = old_dcd;
if (error != GN_ERR_NONE) {
busterminate();
return i18n("<qt><center>Mobile Phone interface initialization failed.<br><br>"
"The returned error message was:<br><b>%1</b><br><br>"
"You might try to run \"gnokii --identify\" on the command line to "
"check any cable/transport issues and to verify if your gnokii "
"configuration is correct.</center></qt>")
.arg(gn_error_print(error));
}
// identify phone
gn_data_clear(&data);
data.manufacturer = manufacturer;
data.model = model;
data.revision = revision;
data.imei = imei;
QCString unknown(GN_TO(i18n("Unknown")));
qstrncpy(manufacturer, unknown, sizeof(manufacturer)-1);
qstrncpy(model, unknown, sizeof(model)-1);
qstrncpy(revision, unknown, sizeof(revision)-1);
qstrncpy(imei, unknown, sizeof(imei)-1);
if (m_progressDlg->wasCancelled())
return QString::null;
else
error = gn_sm_functions(GN_OP_Identify, &data, &state);
GNOKII_CHECK_ERROR(error);
GNOKII_DEBUG( QString("Found mobile phone: %1 %2, Revision: %3, IMEI: %4\n")
.arg(manufacturer, model, revision, imei) );
PhoneProductId = QString("%1-%2-%3-%4").arg(APP).arg(model).arg(revision).arg(imei);
return QString::null;
}
// get number of entries in this phone memory type (internal/SIM-card)
static gn_error read_phone_memstat( const gn_memory_type memtype, gn_memory_status *memstat )
{
gn_error error;
gn_data_clear(&data);
memset(memstat, 0, sizeof(*memstat));
memstat->memory_type = memtype;
data.memory_status = memstat;
error = gn_sm_functions(GN_OP_GetMemoryStatus, &data, &state);
GNOKII_CHECK_ERROR(error);
if (error != GN_ERR_NONE) {
switch (memtype) {
case GN_MT_SM:
// use at least 100 entries
memstat->used = 0;
memstat->free = 100;
break;
default:
case GN_MT_ME:
// Phone doesn't support ME (5110)
memstat->used = memstat->free = 0;
break;
}
}
GNOKII_DEBUG( QString("\n\nMobile phone memory status: Type: %1, used=%2, free=%3, total=%4\n\n")
.arg(memtype).arg(memstat->used).arg(memstat->free).arg(memstat->used+memstat->free) );
return error;
}
// read phone entry #index from memory #memtype
static gn_error read_phone_entry( const int index, const gn_memory_type memtype, gn_phonebook_entry *entry )
{
gn_error error;
entry->memory_type = memtype;
entry->location = index;
data.phonebook_entry = entry;
error = gn_sm_functions(GN_OP_ReadPhonebook, &data, &state);
GNOKII_CHECK_ERROR(error);
return error;
}
static bool phone_entry_empty( const int index, const gn_memory_type memtype )
{
gn_error error;
gn_phonebook_entry entry;
entry.memory_type = memtype;
entry.location = index;
data.phonebook_entry = &entry;
error = gn_sm_functions(GN_OP_ReadPhonebook, &data, &state);
if (error == GN_ERR_EMPTYLOCATION)
return true;
GNOKII_CHECK_ERROR(error);
if (error == GN_ERR_NONE && entry.empty)
return true;
return false;
}
static QString buildPhoneInfoString( const gn_memory_status &memstat )
{
QString format = QString::fromLatin1("<tr><td><b>%1</b></td><td>%2</td></tr>");
return QString::fromLatin1("<b>%1</b><br><table>%2%3%4%5%6</table><br>")
.arg(i18n("Mobile Phone information:"))
.arg(format.arg(i18n("Manufacturer")).arg(GN_FROM(manufacturer)))
.arg(format.arg(i18n("Phone model")).arg(GN_FROM(model)))
.arg(format.arg(i18n("Revision")).arg(GN_FROM(revision)))
.arg(format.arg(i18n("IMEI")).arg(GN_FROM(imei)))
.arg(format.arg(i18n("Phonebook status"))
.arg(i18n("%1 out of %2 contacts used").arg(memstat.used).arg(memstat.used+memstat.free)));
}
static QString buildMemoryTypeString( gn_memory_type memtype )
{
switch (memtype) {
case GN_MT_ME: return i18n("internal memory");
case GN_MT_SM: return i18n("SIM-card memory");
default: return i18n("unknown memory");
}
}
// read and evaluate all phone entries
static gn_error read_phone_entries( const char *memtypestr, gn_memory_type memtype,
KABC::AddresseeList *addrList )
{
gn_error error;
if (m_progressDlg->wasCancelled())
return GN_ERR_NONE;
KProgress* progress = (KProgress*)m_progressDlg->progressBar();
progress->setProgress(0);
this_filter->processEvents();
// get number of entries in this phone memory type (internal/SIM-card)
gn_memory_status memstat;
error = read_phone_memstat(memtype, &memstat);
gn_phonebook_entry entry;
QStringList addrlist;
KABC::Address *addr;
QString s, country;
progress->setTotalSteps(memstat.used);
m_progressDlg->setLabel(i18n("<qt>Importing <b>%1</b> contacts from <b>%2</b> of the Mobile Phone.<br><br>%3</qt>")
.arg(memstat.used)
.arg(buildMemoryTypeString(memtype))
.arg(buildPhoneInfoString(memstat)) );
int num_read = 0;
for (int i = 1; !m_progressDlg->wasCancelled() && i <= memstat.used + memstat.free; i++) {
error = read_phone_entry( i, memtype, &entry );
progress->setProgress(num_read);
this_filter->processEvents();
if (error == GN_ERR_EMPTYLOCATION)
continue;
if (error == GN_ERR_INVALIDLOCATION)
break;
if (error == GN_ERR_INVALIDMEMORYTYPE)
break;
if (error == GN_ERR_NONE) {
GNOKII_DEBUG(QString("%1: %2, num=%3, location=%4, group=%5, count=%6\n").arg(i).arg(GN_FROM(entry.name))
.arg(GN_FROM(entry.number)).arg(entry.location).arg(entry.caller_group).arg(entry.subentries_count));
KABC::Addressee *a = new KABC::Addressee();
// try to split Name into FamilyName and GivenName
s = GN_FROM(entry.name).simplifyWhiteSpace();
a->setFormattedName(s); // set formatted name as in Phone
if (s.find(',') == -1) {
// assumed format: "givenname [... familyname]"
addrlist = QStringList::split(' ', s);
if (addrlist.count() == 1) {
// only one string -> put it in the GivenName
a->setGivenName(s);
} else {
// multiple strings -> split them.
a->setFamilyName(addrlist.last().simplifyWhiteSpace());
addrlist.remove(addrlist.last());
a->setGivenName(addrlist.join(" ").simplifyWhiteSpace());
}
} else {
// assumed format: "familyname, ... givenname"
addrlist = QStringList::split(',', s);
a->setFamilyName(addrlist.first().simplifyWhiteSpace());
addrlist.remove(addrlist.first());
a->setGivenName(addrlist.join(" ").simplifyWhiteSpace());
}
a->insertCustom(APP, "X_GSM_CALLERGROUP", s.setNum(entry.caller_group));
a->insertCustom(APP, "X_GSM_STORE_AT", QString("%1%2").arg(memtypestr).arg(entry.location));
// set ProductId
a->setProductId(PhoneProductId);
// evaluate timestamp (ignore timezone)
QDateTime datetime;
if (entry.date.year<1998)
datetime = QDateTime::currentDateTime();
else
datetime = QDateTime( QDate(entry.date.year, entry.date.month, entry.date.day),
QTime(entry.date.hour, entry.date.minute, entry.date.second) );
GNOKII_DEBUG(QString(" date=%1\n").arg(datetime.toString()));
a->setRevision(datetime);
if (!entry.subentries_count)
a->insertPhoneNumber(KABC::PhoneNumber(entry.number, KABC::PhoneNumber::Work | KABC::PhoneNumber::Pref));
/* scan sub-entries */
if (entry.subentries_count)
for (int n=0; n<entry.subentries_count; n++) {
QString s = GN_FROM(entry.subentries[n].data.number).simplifyWhiteSpace();
GNOKII_DEBUG(QString(" Subentry#%1, entry_type=%2, number_type=%3, number=%4\n")
.arg(n).arg(entry.subentries[n].entry_type)
.arg(entry.subentries[n].number_type).arg(s));
if (s.isEmpty())
continue;
switch(entry.subentries[n].entry_type) {
case GN_PHONEBOOK_ENTRY_Name:
a->setName(s);
break;
case GN_PHONEBOOK_ENTRY_Email:
a->insertEmail(s);
break;
case GN_PHONEBOOK_ENTRY_Postal:
addrlist = QStringList::split(';', s, true);
addr = new KABC::Address(KABC::Address::Work);
if (addrlist.count() <= 1) {
addrlist = QStringList::split(',', s, true);
if (addrlist.count() > 1 ) {
// assumed format: "Locality, ZIP, Country"
addr->setLocality(addrlist[0]);
addr->setPostalCode(addrlist[1]);
if (!addrlist[2].isEmpty())
addr->setCountry(i18n(GN_TO(addrlist[2])));
} else {
// no idea about the format, just store it.
addr->setLocality(s);
}
} else {
// assumed format: "POBox; Extended; Street; Locality; Region; ZIP [;Country]
addr->setPostOfficeBox(addrlist[0]);
addr->setExtended(addrlist[1]);
addr->setStreet(addrlist[2]);
addr->setLocality(addrlist[3]);
addr->setRegion(addrlist[4]);
addr->setPostalCode(addrlist[5]);
country = addrlist[6];
if (!country.isEmpty())
addr->setCountry(i18n(GN_TO(country)));
}
a->insertAddress(*addr);
delete addr;
break;
case GN_PHONEBOOK_ENTRY_Note:
if (!a->note().isEmpty())
s = "\n" + s;
a->setNote(a->note()+s);
break;
case GN_PHONEBOOK_ENTRY_Number:
enum KABC::PhoneNumber::Types phonetype;
switch (entry.subentries[n].number_type) {
case GN_PHONEBOOK_NUMBER_Mobile: phonetype = KABC::PhoneNumber::Cell; break;
case GN_PHONEBOOK_NUMBER_Fax: phonetype = KABC::PhoneNumber::Fax; break;
case GN_PHONEBOOK_NUMBER_General:
case GN_PHONEBOOK_NUMBER_Work: phonetype = KABC::PhoneNumber::Work; break;
default:
case GN_PHONEBOOK_NUMBER_Home: phonetype = KABC::PhoneNumber::Home; break;
}
//if (s == entry.number)
// type = (KABC::PhoneNumber::Types) (phonetype | KABC::PhoneNumber::Pref);
a->insertPhoneNumber(KABC::PhoneNumber(s, phonetype));
break;
case GN_PHONEBOOK_ENTRY_URL:
a->setUrl(s);
break;
case GN_PHONEBOOK_ENTRY_Group:
a->insertCategory(s);
break;
default:
GNOKII_DEBUG(QString(" Not handled id=%1, entry=%2\n")
.arg(entry.subentries[n].entry_type).arg(s));
break;
} // switch()
} // if(subentry)
// add only if entry was valid
if (strlen(entry.name) || strlen(entry.number) || entry.subentries_count)
addrList->append(*a);
// did we read all valid phonebook-entries ?
num_read++;
delete a;
if (num_read >= memstat.used)
break; // yes, all were read
else
continue; // no, we are still missing some.
}
GNOKII_CHECK_ERROR(error);
}
return GN_ERR_NONE;
}
// export to phone
static gn_error xxport_phone_write_entry( int phone_location, gn_memory_type memtype,
const KABC::Addressee *addr)
{
gn_phonebook_entry entry;
QString s;
memset(&entry, 0, sizeof(entry));
strncpy(entry.name, GN_TO(addr->realName()), sizeof(entry.name)-1);
s = addr->phoneNumber(KABC::PhoneNumber::Pref).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Work).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Home).number();
if (s.isEmpty())
s = addr->phoneNumber(KABC::PhoneNumber::Cell).number();
if (s.isEmpty() && addr->phoneNumbers().count()>0)
s = (*addr->phoneNumbers().at(0)).number();
s = makeValidPhone(s);
strncpy(entry.number, s.ascii(), sizeof(entry.number)-1);
entry.memory_type = memtype;
QString cg = addr->custom(APP, "X_GSM_CALLERGROUP");
if (cg.isEmpty())
entry.caller_group = 5; // default group
else
entry.caller_group = cg.toInt();
entry.location = phone_location;
// set date/revision
QDateTime datetime = addr->revision();
QDate date(datetime.date());
QTime time(datetime.time());
entry.date.year = date.year();
entry.date.month = date.month();
entry.date.day = date.day();
entry.date.hour = time.hour();
entry.date.minute = time.minute();
entry.date.second = time.second();
GNOKII_DEBUG(QString("Write #%1: name=%2, number=%3\n").arg(phone_location)
.arg(GN_FROM(entry.name)).arg(GN_FROM(entry.number)));
const KABC::Address homeAddr = addr->address(KABC::Address::Home);
const KABC::Address workAddr = addr->address(KABC::Address::Work);
entry.subentries_count = 0;
gn_phonebook_subentry *subentry = &entry.subentries[0];
// add all phone numbers
const KABC::PhoneNumber::List phoneList = addr->phoneNumbers();
KABC::PhoneNumber::List::ConstIterator it;
for ( it = phoneList.begin(); it != phoneList.end(); ++it ) {
const KABC::PhoneNumber *phonenumber = &(*it);
s = phonenumber->number();
if (s.isEmpty()) continue;
subentry->entry_type = GN_PHONEBOOK_ENTRY_Number;
gn_phonebook_number_type type;
int pn_type = phonenumber->type();
if ((pn_type & KABC::PhoneNumber::Cell))
type = GN_PHONEBOOK_NUMBER_Mobile;
else if ((pn_type & KABC::PhoneNumber::Fax))
type = GN_PHONEBOOK_NUMBER_Fax;
else if ((pn_type & KABC::PhoneNumber::Home))
type = GN_PHONEBOOK_NUMBER_Home;
else if ((pn_type & KABC::PhoneNumber::Work))
type = GN_PHONEBOOK_NUMBER_Work;
else type = GN_PHONEBOOK_NUMBER_General;
subentry->number_type = type;
strncpy(subentry->data.number, makeValidPhone(s).ascii(), sizeof(subentry->data.number)-1);
subentry->id = phone_location<<8+entry.subentries_count;
entry.subentries_count++;
subentry++;
if (entry.subentries_count >= GN_PHONEBOOK_SUBENTRIES_MAX_NUMBER)
break; // Phonebook full
}
// add URL
s = addr->url().prettyURL();
if (!s.isEmpty() && (entry.subentries_count<GN_PHONEBOOK_SUBENTRIES_MAX_NUMBER)) {
subentry->entry_type = GN_PHONEBOOK_ENTRY_URL;
strncpy(subentry->data.number, GN_TO(s), sizeof(subentry->data.number)-1);
entry.subentries_count++;
subentry++;
}
// add E-Mails
QStringList emails = addr->emails();
for (unsigned int n=0; n<emails.count(); n++) {
if (entry.subentries_count >= GN_PHONEBOOK_SUBENTRIES_MAX_NUMBER)
break; // Phonebook full
s = emails[n].simplifyWhiteSpace();
if (s.isEmpty()) continue;
// only one email allowed if we have URLS, notes, addresses (to avoid phone limitations)
if (n && !addr->url().isEmpty() && !addr->note().isEmpty() && addr->addresses().count()) {
GNOKII_DEBUG(QString(" DROPPED email %1 in favor of URLs, notes and addresses.\n")
.arg(s));
continue;
}
subentry->entry_type = GN_PHONEBOOK_ENTRY_Email;
strncpy(subentry->data.number, GN_TO(s), sizeof(subentry->data.number)-1);
entry.subentries_count++;
subentry++;
}
// add Adresses
const KABC::Address::List addresses = addr->addresses();
KABC::Address::List::ConstIterator it2;
for ( it2 = addresses.begin(); it2 != addresses.end(); ++it2 ) {
if (entry.subentries_count >= GN_PHONEBOOK_SUBENTRIES_MAX_NUMBER)
break; // Phonebook full
const KABC::Address *Addr = &(*it2);
if (Addr->isEmpty()) continue;
subentry->entry_type = GN_PHONEBOOK_ENTRY_Postal;
QStringList a;
QChar sem(';');
QString sem_repl(QString::fromLatin1(","));
a.append( Addr->postOfficeBox().replace( sem, sem_repl ) );
a.append( Addr->extended() .replace( sem, sem_repl ) );
a.append( Addr->street() .replace( sem, sem_repl ) );
a.append( Addr->locality() .replace( sem, sem_repl ) );
a.append( Addr->region() .replace( sem, sem_repl ) );
a.append( Addr->postalCode() .replace( sem, sem_repl ) );
a.append( Addr->country() .replace( sem, sem_repl ) );
s = a.join(sem);
strncpy(subentry->data.number, GN_TO(s), sizeof(subentry->data.number)-1);
entry.subentries_count++;
subentry++;
}
// add Note
s = addr->note().simplifyWhiteSpace();
if (!s.isEmpty() && (entry.subentries_count<GN_PHONEBOOK_SUBENTRIES_MAX_NUMBER)) {
subentry->entry_type = GN_PHONEBOOK_ENTRY_Note;
strncpy(subentry->data.number, GN_TO(s), sizeof(subentry->data.number)-1);
entry.subentries_count++;
subentry++;
}
// debug output
for (int st=0; st<entry.subentries_count; st++) {
gn_phonebook_subentry *subentry = &entry.subentries[st];
GNOKII_DEBUG(QString(" SubTel #%1: entry_type=%2, number_type=%3, number=%4\n")
.arg(st).arg(subentry->entry_type)
.arg(subentry->number_type).arg(GN_FROM(subentry->data.number)));
}
data.phonebook_entry = &entry;
gn_error error = gn_sm_functions(GN_OP_WritePhonebook, &data, &state);
GNOKII_CHECK_ERROR(error);
return error;
}
static gn_error xxport_phone_delete_entry( int phone_location, gn_memory_type memtype )
{
gn_phonebook_entry entry;
memset(&entry, 0, sizeof(entry));
entry.empty = 1;
entry.memory_type = memtype;
entry.location = phone_location;
data.phonebook_entry = &entry;
GNOKII_DEBUG(QString("Deleting entry %1\n").arg(phone_location));
gn_error error = gn_sm_functions(GN_OP_WritePhonebook, &data, &state);
GNOKII_CHECK_ERROR(error);
return error;
}
KABC::AddresseeList GNOKIIXXPort::importContacts( const QString& ) const
{
KABC::AddresseeList addrList;
if (KMessageBox::Continue != KMessageBox::warningContinueCancel(parentWidget(),
i18n("<qt>Please connect your Mobile Phone to your computer and press "
"<b>Continue</b> to start importing the personal contacts.<br><br>"
"Please note that if your Mobile Phone is not properly connected "
"the following detection phase might take up to two minutes, during which "
"KAddressbook will behave unresponsively.</qt>") ))
return addrList;
m_progressDlg = new KProgressDialog( parentWidget(), "importwidget",
i18n("Mobile Phone Import"),
i18n("<qt><center>Establishing connection to the Mobile Phone.<br><br>"
"Please wait...</center></qt>") );
m_progressDlg->setAllowCancel(true);
m_progressDlg->progressBar()->setProgress(0);
m_progressDlg->progressBar()->setCenterIndicator(true);
m_progressDlg->setModal(true);
m_progressDlg->setInitialSize(QSize(450,350));
m_progressDlg->show();
processEvents();
#if (QT_VERSION >= 0x030300)
m_progressDlg->setCursor( Qt::BusyCursor );
#endif
QString errStr = businit();
m_progressDlg->unsetCursor();
if (!errStr.isEmpty()) {
KMessageBox::error(parentWidget(), errStr);
delete m_progressDlg;
return addrList;
}
GNOKII_DEBUG("GNOKII import filter started.\n");
m_progressDlg->setButtonText(i18n("&Stop Import"));
read_phone_entries("ME", GN_MT_ME, &addrList); // internal phone memory
read_phone_entries("SM", GN_MT_SM, &addrList); // SIM card
GNOKII_DEBUG("GNOKII import filter finished.\n");
busterminate();
delete m_progressDlg;
return addrList;
}
bool GNOKIIXXPort::exportContacts( const KABC::AddresseeList &list, const QString & )
{
if (KMessageBox::Continue != KMessageBox::warningContinueCancel(parentWidget(),
i18n("<qt>Please connect your Mobile Phone to your computer and press "
"<b>Continue</b> to start exporting the selected personal contacts.<br><br>"
"Please note that if your Mobile Phone is not properly connected "
"the following detection phase might take up to two minutes, during which "
"KAddressbook will behave unresponsively.</qt>") ))
return false;
m_progressDlg = new KProgressDialog( parentWidget(), "importwidget",
i18n("Mobile Phone Export"),
i18n("<qt><center>Establishing connection to the Mobile Phone.<br><br>"
"Please wait...</center></qt>") );
m_progressDlg->setAllowCancel(true);
m_progressDlg->progressBar()->setProgress(0);
m_progressDlg->progressBar()->setCenterIndicator(true);
m_progressDlg->setModal(true);
m_progressDlg->setInitialSize(QSize(450,350));
m_progressDlg->show();
processEvents();
KProgress* progress = (KProgress*)m_progressDlg->progressBar();
KABC::AddresseeList::ConstIterator it;
QStringList failedList;
gn_error error;
bool deleteLabelInitialized = false;
#if (QT_VERSION >= 0x030300)
m_progressDlg->setCursor( Qt::BusyCursor );
#endif
QString errStr = businit();
m_progressDlg->unsetCursor();
if (!errStr.isEmpty()) {
KMessageBox::error(parentWidget(), errStr);
delete m_progressDlg;
return false;
}
GNOKII_DEBUG("GNOKII export filter started.\n");
gn_memory_type memtype = GN_MT_ME; // internal phone memory
int phone_count; // num entries in phone
bool overwrite_phone_entries = false;
int phone_entry_no, entries_written;
bool entry_empty;
// get number of entries in this phone memory
gn_memory_status memstat;
error = read_phone_memstat(memtype, &memstat);
if (error == GN_ERR_NONE) {
GNOKII_DEBUG("Writing to internal phone memory.\n");
} else {
memtype = GN_MT_SM; // try SIM card instead
error = read_phone_memstat(memtype, &memstat);
if (error != GN_ERR_NONE)
goto finish;
GNOKII_DEBUG("Writing to SIM card memory.\n");
}
phone_count = memstat.used;
if (memstat.free >= (int) list.count()) {
if (KMessageBox::No == KMessageBox::questionYesNo(parentWidget(),
i18n("<qt>Do you want the selected contacts to be <b>appended</b> to "
"the current mobile phonebook or should they <b>replace</b> all "
"currently existing phonebook entries ?<br><br>"
"Please note, that in case you choose to replace the phonebook "
"entries, every contact in your phone will be deleted and only "
"the newly exported contacts will be available from inside your phone.</qt>"),
i18n("Export to Mobile Phone"),
KGuiItem(i18n("&Append to Current Phonebook")),
KGuiItem(i18n("&Replace Current Phonebook with New Contacts")) ) )
overwrite_phone_entries = true;
}
progress->setTotalSteps(list.count());
entries_written = 0;
progress->setProgress(entries_written);
m_progressDlg->setButtonText(i18n("&Stop Export"));
m_progressDlg->setLabel(i18n("<qt>Exporting <b>%1</b> contacts to the <b>%2</b> "
"of the Mobile Phone.<br><br>%3</qt>")
.arg(list.count())
.arg(buildMemoryTypeString(memtype))
.arg(buildPhoneInfoString(memstat)) );
// Now run the loop...
phone_entry_no = 1;
for ( it = list.begin(); it != list.end(); ++it ) {
const KABC::Addressee *addr = &(*it);
if (addr->isEmpty())
continue;
// don't write back SIM-card entries !
if (addr->custom(APP, "X_GSM_STORE_AT").startsWith("SM"))
continue;
progress->setProgress(entries_written++);
try_next_phone_entry:
this_filter->processEvents();
if (m_progressDlg->wasCancelled())
break;
// End of phone memory reached ?
if (phone_entry_no > (memstat.used + memstat.free))
break;
GNOKII_DEBUG(QString("Try to write entry '%1' at phone_entry_no=%2, phone_count=%3\n")
.arg(addr->realName()).arg(phone_entry_no).arg(phone_count));
error = GN_ERR_NONE;
// is this phone entry empty ?
entry_empty = phone_entry_empty(phone_entry_no, memtype);
if (overwrite_phone_entries) {
// overwrite this phonebook entry ...
if (!entry_empty)
phone_count--;
error = xxport_phone_write_entry( phone_entry_no, memtype, addr);
phone_entry_no++;
} else {
// add this phonebook entry if possible ...
if (entry_empty) {
error = xxport_phone_write_entry( phone_entry_no, memtype, addr);
phone_entry_no++;
} else {
phone_entry_no++;
goto try_next_phone_entry;
}
}
if (error != GN_ERR_NONE)
failedList.append(addr->realName());
// break if we got an error on the first entry
if (error != GN_ERR_NONE && it==list.begin())
break;
} // for()
// if we wanted to overwrite all entries, make sure, that we also
// delete all remaining entries in the mobile phone.
while (overwrite_phone_entries && error==GN_ERR_NONE && phone_count>0) {
if (m_progressDlg->wasCancelled())
break;
if (!deleteLabelInitialized) {
m_progressDlg->setLabel(
i18n("<qt><center>"
"All selected contacts have been sucessfully copied to "
"the Mobile Phone.<br><br>"
"Please wait until all remaining orphaned contacts from "
"the Mobile Phone have been deleted.</center></qt>") );
m_progressDlg->setButtonText(i18n("&Stop Delete"));
deleteLabelInitialized = true;
progress->setTotalSteps(phone_count);
entries_written = 0;
progress->setProgress(entries_written);
this_filter->processEvents();
}
if (phone_entry_no > (memstat.used + memstat.free))
break;
entry_empty = phone_entry_empty(phone_entry_no, memtype);
if (!entry_empty) {
error = xxport_phone_delete_entry(phone_entry_no, memtype);
phone_count--;
progress->setProgress(++entries_written);
this_filter->processEvents();
}
phone_entry_no++;
}
finish:
m_progressDlg->setLabel(i18n("Export to phone finished."));
this_filter->processEvents();
GNOKII_DEBUG("GNOKII export filter finished.\n");
busterminate();
delete m_progressDlg;
if (!failedList.isEmpty()) {
GNOKII_DEBUG(QString("Failed to export: %1\n").arg(failedList.join(", ")));
KMessageBox::informationList(parentWidget(),
i18n("<qt>The following contacts could not be exported to the Mobile Phone. "
"Possible Reasons for this problem could be:<br><ul>"
"<li>The contacts contain more information per entry than the phone can store.</li>"
"<li>Your phone does not allow to store multiple addresses, emails, homepages, ...</li>"
"<li>other storage size related problems.</li>"
"</ul>"
"To avoid those kind of problems in the future please reduce the amount of different "
"fields in the above contacts.</qt>"),
failedList,
i18n("Mobile Phone Export") );
}
return true;
}
/******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************/
#else /* no gnokii installed */
KABC::AddresseeList GNOKIIXXPort::importContacts( const QString& ) const
{
KABC::AddresseeList addrList;
KMessageBox::error(parentWidget(), i18n("Gnokii interface is not available.\n"
"Please ask your distributor to add gnokii at compile time."));
return addrList;
}
bool GNOKIIXXPort::exportContacts( const KABC::AddresseeList &list, const QString & )
{
Q_UNUSED(list);
KMessageBox::error(parentWidget(), i18n("Gnokii interface is not available.\n"
"Please ask your distributor to add gnokii at compile time."));
return true;
}
#endif /* END OF GNOKII LIB SWITCH */
/******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************/
#include "gnokii_xxport.moc"
/* vim: set sts=4 ts=4 sw=4: */
|