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
|
/*
This file is part of libkcal.
Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <qdatetime.h>
#include <qstring.h>
#include <qapplication.h>
#include <qptrlist.h>
#include <qregexp.h>
#include <qmessagebox.h>
#include <qclipboard.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qtextcodec.h>
#include <qxml.h>
#include <qlabel.h>
#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include "calendar.h"
#include "alarm.h"
#include "recurrence.h"
#include "calendarlocal.h"
#include "phoneformat.h"
#include "syncdefines.h"
using namespace KCal;
class PhoneParser : public QObject
{
public:
PhoneParser( Calendar *calendar, QString profileName ) : mCalendar( calendar ), mProfileName ( profileName ) {
;
}
bool readTodo( Calendar *existingCalendar,GSM_ToDoEntry *ToDo, GSM_StateMachine* s)
{
int id = ToDo->Location;
Todo *todo;
todo = existingCalendar->todo( mProfileName ,QString::number( id ) );
if (todo )
todo = (Todo *)todo->clone();
else
todo = new Todo;
todo->setID( mProfileName,QString::number( id ) );
todo->setTempSyncStat(SYNC_TEMPSTATE_NEW_EXTERNAL );
int priority;
switch (ToDo->Priority) {
case GSM_Priority_Low : priority = 5; break;
case GSM_Priority_Medium : priority = 3; break;
case GSM_Priority_High : priority = 1; break;
default :priority = 3 ; break;
}
todo->setPriority( priority );
GSM_Phone_Functions *Phone;
Phone=s->Phone.Functions;
int j;
GSM_DateTime* dtp;
bool alarm = false;
QDateTime alarmDt;
GSM_Category Category;
int error;
for (j=0;j<ToDo->EntriesNum;j++) {
//qDebug(" for todo %d",ToDo->Location );
switch (ToDo->Entries[j].EntryType) {
case TODO_END_DATETIME:
dtp = &ToDo->Entries[j].Date ;
todo->setDtDue (fromGSM ( dtp ));
break;
case TODO_COMPLETED:
if ( ToDo->Entries[j].Number == 1 ) {
todo->setCompleted( true );
}
else {
todo->setCompleted( false );
}
break;
case TODO_ALARM_DATETIME:
dtp = &ToDo->Entries[j].Date ;
alarm = true;
alarmDt = fromGSM ( dtp );
break;
case TODO_SILENT_ALARM_DATETIME:
dtp = &ToDo->Entries[j].Date ;
alarm = true;
alarmDt = fromGSM ( dtp );
break;
case TODO_TEXT:
//qDebug(" text *%s* ", (const char*) DecodeUnicodeConsole(ToDo->Entries[j].Text ));
todo->setSummary( QString::fromUtf8 ( (const char*)DecodeUnicodeConsole(ToDo->Entries[j].Text )));
break;
case TODO_PRIVATE:
if ( ToDo->Entries[j].Number == 1 )
todo->setSecrecy( Incidence::SecrecyPrivate );
else
todo->setSecrecy( Incidence::SecrecyPublic );
break;
case TODO_CATEGORY:
Category.Location = ToDo->Entries[j].Number;
Category.Type = Category_ToDo;
error=Phone->GetCategory(s, &Category);
if (error == ERR_NONE) {
QStringList cat = todo->categories();
QString nCat = QString ( (const char*)Category.Name );
if ( !nCat.isEmpty() )
if ( !cat.contains( nCat )) {
cat << nCat;
todo->setCategories( cat );
}
}
break;
case TODO_CONTACTID:
#if 0
// not supported
entry.Location = ToDo->Entries[j].Number;
entry.MemoryType = MEM_ME;
error=Phone->GetMemory(s, &entry);
if (error == ERR_NONE) {
name = GSM_PhonebookGetEntryName(&entry);
if (name != NULL) {
printmsg("Contact ID : \"%s\" (%d)\n", DecodeUnicodeConsole(name), ToDo->Entries[j].Number);
} else {
printmsg("Contact ID : %d\n",ToDo->Entries[j].Number);
}
} else {
printmsg("Contact : %d\n",ToDo->Entries[j].Number);
}
#endif
break;
case TODO_PHONE:
#if 0
// not supported
printmsg("Phone : \"%s\"\n",DecodeUnicodeConsole(ToDo->Entries[j].Text));
#endif
break;
}
}
QString alarmString = "na";
if ( alarm ) {
Alarm *alarm;
if ( todo->alarms().count() > 0 )
alarm = todo->alarms().first();
else {
alarm = new Alarm( todo );
todo->addAlarm( alarm );
}
alarm->setType( Alarm::Audio );
alarm->setEnabled( true );
int alarmOffset = alarmDt.secsTo( todo->dtStart() );
alarm->setStartOffset( -alarmOffset );
alarmString = QString::number( alarmOffset );
} else {
Alarm *alarm;
if ( todo->alarms().count() > 0 ) {
alarm = todo->alarms().first();
alarm->setType( Alarm::Audio );
alarm->setStartOffset( -60*15 );
alarm->setEnabled( false );
}
}
// csum *****************************************
uint cSum;
cSum = PhoneFormat::getCsumTodo( todo );
todo->setCsum( mProfileName, QString::number( cSum ));
todo->setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
mCalendar->addTodo( todo);
return true;
}
bool readEvent( Calendar *existingCalendar, GSM_CalendarEntry* Note)
{
int id = Note->Location;
Event *event;
event = existingCalendar->event( mProfileName ,QString::number( id ) );
if ( event )
event = (Event*)event->clone();
else
event = new Event;
event->setID( mProfileName,QString::number( id ) );
event->setTempSyncStat(SYNC_TEMPSTATE_NEW_EXTERNAL );
int i = 0;
bool repeating = false;
int repeat_dayofweek = -1;
int repeat_day = -1;
int repeat_weekofmonth = -1;
int repeat_month = -1;
int repeat_frequency = -1;
int rec_type = -1;
GSM_DateTime repeat_startdate = {0,0,0,0,0,0,0};
GSM_DateTime repeat_stopdate = {0,0,0,0,0,0,0};
GSM_DateTime* dtp;
bool alarm = false;
QDateTime alarmDt;
repeat_startdate.Day = 0;
repeat_stopdate.Day = 0;
for (i=0;i<Note->EntriesNum;i++) {
//qDebug(" for ev");
switch (Note->Entries[i].EntryType) {
case CAL_START_DATETIME:
dtp = &Note->Entries[i].Date ;
if ( dtp->Hour > 24 ) {
event->setFloats( true );
event->setDtStart( QDateTime (datefromGSM ( dtp ), QTime(0,0,0 )));
} else {
event->setDtStart (fromGSM ( dtp ));
}
//Note->Entries[i].Date.Hour = 5;
break;
case CAL_END_DATETIME:
dtp = &Note->Entries[i].Date ;
if ( dtp->Hour > 24 ) {
event->setFloats( true );
event->setDtEnd( QDateTime (datefromGSM ( dtp ), QTime(0,0,0 )));
} else {
event->setDtEnd (fromGSM ( dtp ));
}
break;
case CAL_ALARM_DATETIME:
dtp = &Note->Entries[i].Date ;
alarm = true;
alarmDt = fromGSM ( dtp );
break;
case CAL_SILENT_ALARM_DATETIME:
dtp = &Note->Entries[i].Date ;
alarm = true;
alarmDt = fromGSM ( dtp );
break;
case CAL_RECURRANCE:
rec_type = Note->Entries[i].Number;
//printmsg("Repeat : %d day%s\n",Note->Entries[i].Number/24,((Note->Entries[i].Number/24)>1) ? "s":"" );
break;
case CAL_TEXT:
//qDebug(" ev text %s", DecodeUnicodeConsole(Note->Entries[i].Text) );
event->setSummary( QString::fromUtf8 ( (const char*)DecodeUnicodeConsole( Note->Entries[i].Text )));
break;
case CAL_LOCATION:
event->setLocation(QString::fromUtf8 ((const char*) DecodeUnicodeConsole(Note->Entries[i].Text) ));
break;
case CAL_PHONE:
//printmsg("Phone : \"%s\"\n",DecodeUnicodeConsole(Note->Entries[i].Text));
break;
case CAL_PRIVATE:
if ( Note->Entries[i].Number == 1 )
event->setSecrecy( Incidence::SecrecyPrivate );
else
event->setSecrecy( Incidence::SecrecyPublic );
break;
case CAL_CONTACTID:
#if 0
entry.Location = Note->Entries[i].Number;
entry.MemoryType = MEM_ME;
error=Phone->GetMemory(&s, &entry);
if (error == ERR_NONE) {
name = GSM_PhonebookGetEntryName(&entry);
if (name != NULL) {
//printmsg("Contact ID : \"%s\" (%d)\n", DecodeUnicodeConsole(name), Note->Entries[i].Number);
} else {
//printmsg("Contact ID : %d\n",Note->Entries[i].Number);
}
} else {
//printmsg("Contact ID : %d\n",Note->Entries[i].Number);
}
#endif
break;
case CAL_REPEAT_DAYOFWEEK:
repeat_dayofweek = Note->Entries[i].Number;
repeating = true;
break;
case CAL_REPEAT_DAY:
repeat_day = Note->Entries[i].Number;
repeating = true;
break;
case CAL_REPEAT_WEEKOFMONTH:
repeat_weekofmonth = Note->Entries[i].Number;
repeating = true;
break;
case CAL_REPEAT_MONTH:
repeat_month = Note->Entries[i].Number;
repeating = true;
break;
case CAL_REPEAT_FREQUENCY:
repeat_frequency = Note->Entries[i].Number;
repeating = true;
break;
case CAL_REPEAT_STARTDATE:
repeat_startdate = Note->Entries[i].Date;
repeating = true;
break;
case CAL_REPEAT_STOPDATE:
repeat_stopdate = Note->Entries[i].Date;
repeating = true;
break;
}
}
#if 0
event->setDescription( attList[4] );
bool repeating = false;
int repeat_dayofweek = -1;
int repeat_day = -1;
int repeat_weekofmonth = -1;
int repeat_month = -1;
int repeat_frequency = -1;
GSM_DateTime repeat_startdate = {0,0,0,0,0,0,0};
GSM_DateTime repeat_stopdate = {0,0,0,0,0,0,0};
#endif
QString recurString = "no";
if ( repeating && repeat_frequency != -1) {
recurString = "y";
if ( repeat_dayofweek >= 0 )
recurString += "dow" + QString::number (repeat_dayofweek);
if ( repeat_day >= 0 )
recurString += "d" + QString::number (repeat_day);
if ( repeat_weekofmonth >= 0 )
recurString += "w" + QString::number (repeat_weekofmonth);
if ( repeat_month >= 0 )
recurString += "m" + QString::number ( repeat_month );
if ( repeat_frequency >= 0 )
recurString += "f" + QString::number (repeat_frequency );
int rtype = 0;
// qDebug("recurs ");
QDate startDate, endDate;
if ( repeat_startdate.Day > 0 ) {
startDate = datefromGSM ( &repeat_startdate );
event->setDtStart(QDateTime ( startDate, event->dtStart().time()));
} else {
startDate = event->dtStart().date();
}
int freq = repeat_frequency;
bool hasEndDate = false;
if ( repeat_stopdate.Day > 0 ) {
endDate = datefromGSM ( &repeat_stopdate );
hasEndDate = true;
}
uint weekDaysNum = repeat_dayofweek ;
// 1 == monday, 7 == sunday
QBitArray weekDays( 7 );
int i;
int bb = 1;
for( i = 1; i <= 7; ++i ) {
weekDays.setBit( i - 1, ( bb & weekDaysNum ));
bb = 2 << (i-1);
//qDebug(" %d bit %d ",i-1,weekDays.at(i-1) );
}
// qDebug("next ");
int pos = 0;
Recurrence *r = event->recurrence();
/*
0 daily;
1 weekly;x
2 monthpos;x
3 monthlyday;
4 rYearlyMont
bool repeating = false;
int repeat_dayofweek = -1;
int repeat_day = -1;
int repeat_weekofmonth = -1;
int repeat_month = -1;
int repeat_frequency = -1;
*/
int dayOfWeek = startDate.dayOfWeek();
if ( repeat_weekofmonth >= 0 ) {
rtype = 2; // ************************ 2 MonthlyPos
pos = repeat_weekofmonth;
if ( repeat_dayofweek >= 0 )
dayOfWeek = repeat_dayofweek;
if (repeat_month > 0) {
if ( repeat_month != event->dtStart().date().month() ) {
QDate date (event->dtStart().date().year(),repeat_month,event->dtStart().date().day() );
event->setDtStart(QDateTime ( date , event->dtStart().time()) );
}
if ( freq == 1 )
freq = 12;
}
} else if ( repeat_dayofweek >= 0 ) {
rtype = 1;// ************************ 1 Weekly
} else if ( repeat_day >= 0 ) {
if ( repeat_month > 0) {
rtype = 4;
} else {
rtype = 3;
}
} else {
rtype = 0 ;
}
if ( rtype == 0 ) {
if ( hasEndDate ) r->setDaily( freq, endDate );
else r->setDaily( freq, -1 );
} else if ( rtype == 1 ) {
if ( hasEndDate ) r->setWeekly( freq, weekDays, endDate );
else r->setWeekly( freq, weekDays, -1 );
} else if ( rtype == 3 ) {
if ( hasEndDate )
r->setMonthly( Recurrence::rMonthlyDay, freq, endDate );
else
r->setMonthly( Recurrence::rMonthlyDay, freq, -1 );
r->addMonthlyDay( startDate.day() );
} else if ( rtype == 2 ) {
if ( hasEndDate )
r->setMonthly( Recurrence::rMonthlyPos, freq, endDate );
else
r->setMonthly( Recurrence::rMonthlyPos, freq, -1 );
QBitArray days( 7 );
days.fill( false );
days.setBit( dayOfWeek - 1 );
r->addMonthlyPos( pos, days );
} else if ( rtype == 4 ) {
if ( hasEndDate )
r->setYearly( Recurrence::rYearlyMonth, freq, endDate );
else
r->setYearly( Recurrence::rYearlyMonth, freq, -1 );
r->addYearlyNum( startDate.month() );
}
} else {
event->recurrence()->unsetRecurs();
}
QStringList categoryList;
categoryList << getCategory( Note );
event->setCategories( categoryList );
// strange 0 semms to mean: alarm enabled
if ( alarm ) {
Alarm *alarm;
if ( event->alarms().count() > 0 )
alarm = event->alarms().first();
else {
alarm = new Alarm( event );
event->addAlarm( alarm );
}
alarm->setType( Alarm::Audio );
alarm->setEnabled( true );
int alarmOffset = alarmDt.secsTo( event->dtStart() );
alarm->setStartOffset( -alarmOffset );
} else {
Alarm *alarm;
if ( event->alarms().count() > 0 ) {
alarm = event->alarms().first();
alarm->setType( Alarm::Audio );
alarm->setStartOffset( -60*15 );
alarm->setEnabled( false );
}
}
// csum *****************************************
uint cSum;
cSum = PhoneFormat::getCsumEvent( event );
event->setCsum( mProfileName, QString::number( cSum ));
event->setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
mCalendar->addEvent( event);
return true;
}
QDateTime fromGSM ( GSM_DateTime* dtp, bool useTz = false ) {
QDateTime dt;
int y,m,t,h,min,sec;
y = dtp->Year;
m = dtp->Month;
t = dtp->Day;
h = dtp->Hour;
min = dtp->Minute;
sec = dtp->Second;
dt = QDateTime(QDate(y,m,t), QTime(h,min,sec));
// dtp->Timezone: offset in hours
int offset = KGlobal::locale()->localTimeOffset( dt );
if ( useTz )
dt = dt.addSecs ( offset*60);
return dt;
}
static QString dtToString( const QDateTime& dti, bool useTZ = false )
{
QString datestr;
QString timestr;
int offset = KGlobal::locale()->localTimeOffset( dti );
QDateTime dt;
if (useTZ)
dt = dti.addSecs ( -(offset*60));
else
dt = dti;
if(dt.date().isValid()){
const QDate& date = dt.date();
datestr.sprintf("%04d%02d%02d",
date.year(), date.month(), date.day());
}
if(dt.time().isValid()){
const QTime& time = dt.time();
timestr.sprintf("T%02d%02d%02d",
time.hour(), time.minute(), time.second());
}
return datestr + timestr;
}
QDate datefromGSM ( GSM_DateTime* dtp ) {
return QDate ( dtp->Year, dtp->Month, dtp->Day );
}
QString getCategory( GSM_CalendarEntry* Note)
{
QString CATEGORY;
switch (Note->Type) {
case GSM_CAL_REMINDER : CATEGORY = QString("Reminder"); break;
case GSM_CAL_CALL : CATEGORY = QString("Call"); break;
case GSM_CAL_MEETING : CATEGORY = QString("Meeting"); break;
case GSM_CAL_BIRTHDAY : CATEGORY = QString("Birthday"); break;
case GSM_CAL_MEMO : CATEGORY = QString("Memo"); break;
case GSM_CAL_TRAVEL : CATEGORY = QString("Travel"); break;
case GSM_CAL_VACATION : CATEGORY = QString("Vacation"); break;
case GSM_CAL_ALARM : CATEGORY = QString("Alarm"); break;
case GSM_CAL_DAILY_ALARM : CATEGORY = QString("Daily alarm"); break;
case GSM_CAL_T_ATHL : CATEGORY = QString("Training/Athletism"); break;
case GSM_CAL_T_BALL : CATEGORY = QString("Training/Ball Games"); break;
case GSM_CAL_T_CYCL : CATEGORY = QString("Training/Cycling"); break;
case GSM_CAL_T_BUDO : CATEGORY = QString("Training/Budo"); break;
case GSM_CAL_T_DANC : CATEGORY = QString("Training/Dance"); break;
case GSM_CAL_T_EXTR : CATEGORY = QString("Training/Extreme Sports"); break;
case GSM_CAL_T_FOOT : CATEGORY = QString("Training/Football"); break;
case GSM_CAL_T_GOLF : CATEGORY = QString("Training/Golf"); break;
case GSM_CAL_T_GYM : CATEGORY = QString("Training/Gym"); break;
case GSM_CAL_T_HORS : CATEGORY = QString("Training/Horse Races"); break;
case GSM_CAL_T_HOCK : CATEGORY = QString("Training/Hockey"); break;
case GSM_CAL_T_RACE : CATEGORY = QString("Training/Races"); break;
case GSM_CAL_T_RUGB : CATEGORY = QString("Training/Rugby"); break;
case GSM_CAL_T_SAIL : CATEGORY = QString("Training/Sailing"); break;
case GSM_CAL_T_STRE : CATEGORY = QString("Training/Street Games"); break;
case GSM_CAL_T_SWIM : CATEGORY = QString("Training/Swimming"); break;
case GSM_CAL_T_TENN : CATEGORY = QString("Training/Tennis"); break;
case GSM_CAL_T_TRAV : CATEGORY = QString("Training/Travels"); break;
case GSM_CAL_T_WINT : CATEGORY = QString("Training/Winter Games"); break;
default : CATEGORY = QString("");
}
return CATEGORY;
}
protected:
private:
Calendar *mCalendar;
QString mProfileName ;
};
PhoneFormat::PhoneFormat(QString profileName, QString device,QString connection, QString model )
{
mProfileName = profileName;
mDevice = device;
mConnection = connection;
mModel = model;
}
PhoneFormat::~PhoneFormat()
{
}
int PhoneFormat::initDevice(GSM_StateMachine *s)
{
GSM_ReadConfig(NULL, &s->Config[0], 0);
s->ConfigNum = 1;
GSM_Config *cfg = &s->Config[0];
if ( ! mConnection.isEmpty() ) {
cfg->Connection = strdup(mConnection.latin1());
cfg->DefaultConnection = false;
qDebug("Connection set %s ", cfg->Connection );
}
if ( ! mDevice.isEmpty() ) {
cfg->Device = strdup(mDevice.latin1());
cfg->DefaultDevice = false;
qDebug("Device set %s ", cfg->Device);
}
if ( ! mModel.isEmpty() ) {
strcpy(cfg->Model,mModel.latin1() );
cfg->DefaultModel = false;
qDebug("Model set %s ",cfg->Model );
}
int error=GSM_InitConnection(s,3);
return error;
}
ulong PhoneFormat::getCsumTodo( Todo* todo )
{
QStringList attList;
if ( todo->hasDueDate() )
attList << PhoneParser::dtToString ( todo->dtDue() );
attList << todo->summary();
QString completedString = "no";
if ( todo->isCompleted() )
completedString = "yes";
attList << completedString;
attList << QString::number( todo->priority() );
QString alarmString = "na";
Alarm *alarm;
if ( todo->alarms().count() > 0 ) {
alarm = todo->alarms().first();
if ( alarm->enabled() ) {
alarmString = QString::number(alarm->startOffset().asSeconds() );
}
}
attList << alarmString;
attList << todo->categoriesStr();
attList << todo->secrecyStr();
return PhoneFormat::getCsum(attList );
}
ulong PhoneFormat::getCsumEvent( Event* event )
{
QStringList attList;
attList << PhoneParser::dtToString ( event->dtStart() );
attList << PhoneParser::dtToString ( event->dtEnd() );
attList << event->summary();
attList << event->location();
QString alarmString = "na";
Alarm *alarm;
if ( event->alarms().count() > 0 ) {
alarm = event->alarms().first();
if ( alarm->enabled() ) {
alarmString = QString::number( alarm->startOffset().asSeconds() );
}
}
attList << alarmString;
Recurrence* rec = event->recurrence();
QStringList list;
bool writeEndDate = false;
switch ( rec->doesRecur() )
{
case Recurrence::rDaily: // 0
list.append( "0" );
list.append( QString::number( rec->frequency() ));//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
case Recurrence::rWeekly:// 1
list.append( "1" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
{
int days = 0;
QBitArray weekDays = rec->days();
int i;
for( i = 1; i <= 7; ++i ) {
if ( weekDays[i-1] ) {
days += 1 << (i-1);
}
}
list.append( QString::number( days ) );
}
//pending weekdays
writeEndDate = true;
break;
case Recurrence::rMonthlyPos:// 2
list.append( "2" );
list.append( QString::number( rec->frequency()) );//12
writeEndDate = true;
{
int count = 1;
QPtrList<Recurrence::rMonthPos> rmp;
rmp = rec->monthPositions();
if ( rmp.first()->negative )
count = 5 - rmp.first()->rPos - 1;
else
count = rmp.first()->rPos - 1;
list.append( QString::number( count ) );
}
list.append( "0" );
break;
case Recurrence::rMonthlyDay:// 3
list.append( "3" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
case Recurrence::rYearlyMonth://4
list.append( "4" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
default:
list.append( "255" );
list.append( QString() );
list.append( "0" );
list.append( QString() );
list.append( "0" );
list.append( "20991231T000000" );
break;
}
if ( writeEndDate ) {
if ( rec->endDate().isValid() ) { // 15 + 16
list.append( "1" );
list.append( PhoneParser::dtToString( rec->endDate()) );
} else {
list.append( "0" );
list.append( "20991231T000000" );
}
}
attList << list.join("");
attList << event->categoriesStr();
attList << event->secrecyStr();
return PhoneFormat::getCsum(attList );
}
ulong PhoneFormat::getCsum( const QStringList & attList)
{
int max = attList.count() -1;
ulong cSum = 0;
int j,k,i;
int add;
for ( i = 1; i < max ; ++i ) {
QString s = attList[i];
if ( ! s.isEmpty() ){
j = s.length();
for ( k = 0; k < j; ++k ) {
int mul = k +1;
add = s[k].unicode ();
if ( k < 16 )
mul = mul * mul;
add = add * mul *i*i*i;
cSum += add;
}
}
}
return cSum;
}
//extern "C" GSM_Error GSM_InitConnection(GSM_StateMachine *s, int ReplyNum);
#include <stdlib.h>
#define DEBUGMODE false
bool PhoneFormat::load( Calendar *calendar, Calendar *existingCal)
{
GSM_StateMachine s;
qDebug(" load ");
s.opened = false;
s.msg = NULL;
s.ConfigNum = 0;
QLabel status ( i18n("Opening device ..."), 0 );
int w = status.sizeHint().width()+20 ;
if ( w < 200 ) w = 230;
int h = status.sizeHint().height()+20 ;
int dw = QApplication::desktop()->width();
int dh = QApplication::desktop()->height();
status.setCaption(i18n("Reading phone...") );
status.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
status.show();
status.raise();
qApp->processEvents();
int error=initDevice(&s);
qDebug("GSM Init %d (no error is %d)", error, ERR_NONE);
if ( error != ERR_NONE )
return false;
GSM_Phone_Functions *Phone;
GSM_CalendarEntry note;
bool start = true;
Phone=s.Phone.Functions;
bool gshutdown = false;
PhoneParser handler( calendar, mProfileName );
int ccc = 0;
QString message = i18n(" Reading event # ");
int procCount = 0;
qDebug("Debug: only 10 calender items are downloaded ");
while (!gshutdown && ccc++ < 10) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("readEvent %d ", ccc);
error=Phone->GetNextCalendar(&s,¬e,start);
if (error == ERR_EMPTY) break;
start = false;
handler.readEvent( existingCal, ¬e );
qDebug("Org loc %d ",note.Location);
//note.Location = 0;
error=Phone->SetCalendar(&s,¬e);
qDebug("new loc %d ",note.Location);
}
start = true;
GSM_ToDoEntry ToDo;
ccc = 0;
message = i18n(" Reading todo # ");
procCount = 0;
while (!gshutdown && ccc++ < 10) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
error = Phone->GetNextToDo(&s, &ToDo, start);
if (error == ERR_EMPTY) break;
start = false;
qDebug("ReadTodo %d ", ccc);
handler.readTodo( existingCal, &ToDo, &s);
}
error=GSM_TerminateConnection(&s);
return true;
}
#include <qcstring.h>
void PhoneFormat::event2GSM( Calendar *cal,Event* ev, GSM_CalendarEntry*Note )
{
QString eText = vfconverter.eventToString( ev, cal );
int pos = 0;
GSM_ToDoEntry dummy;
qDebug( "Convert event");
QByteArray ba;
QDataStream s ( ba, IO_WriteOnly );
s << eText.utf8();
GSM_DecodeVCALENDAR_VTODO( (unsigned char*) ba.data(), &pos, Note , &dummy, Nokia_VCalendar, Nokia_VToDo );
qDebug( "Convert event done");
Note->Location = 0;
QString loc = ev->getID(mProfileName);
if ( !loc.isEmpty() ){
Note->Location = loc.toInt();
}
}
void PhoneFormat::todo2GSM( Calendar *cal, Todo* todo, GSM_ToDoEntry *gsmTodo )
{
qDebug( "Convert todo1");
QString tText = vfconverter.todoToString( todo, cal );
int pos = 0;
GSM_CalendarEntry dummy;
QByteArray ba;
QDataStream s ( ba, IO_WriteOnly );
s << tText.utf8();
GSM_DecodeVCALENDAR_VTODO( (unsigned char*) ba.data(), &pos, &dummy, gsmTodo, Nokia_VCalendar, Nokia_VToDo );
qDebug( "Convert todo done ");
gsmTodo->Location = 0;
QString loc = todo->getID(mProfileName);
if ( !loc.isEmpty() ){
gsmTodo->Location = loc.toInt();
}
}
void PhoneFormat::afterSave( Incidence* inc)
{
uint csum;
inc->removeID( mProfileName );
if ( inc->type() == "Event")
csum = PhoneFormat::getCsumEvent( (Event*) inc );
else
csum = PhoneFormat::getCsumTodo( (Todo*) inc );
inc->setCsum( mProfileName, QString::number( csum ));
inc->setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
}
bool PhoneFormat::save( Calendar *calendar)
{
GSM_StateMachine s;
qDebug(" save ");
s.opened = false;
s.msg = NULL;
s.ConfigNum = 0;
QLabel status ( i18n(" Opening device ..."), 0 );
int w = status.sizeHint().width()+20 ;
if ( w < 200 ) w = 230;
int h = status.sizeHint().height()+20 ;
int dw = QApplication::desktop()->width();
int dh = QApplication::desktop()->height();
status.setCaption(i18n("Writing to phone...") );
status.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
status.show();
status.raise();
qApp->processEvents();
int error=initDevice(&s);
qDebug("GSM Init %d (no error is %d)", error, ERR_NONE);
if ( error != ERR_NONE )
return false;
GSM_Phone_Functions *Phone;
GSM_CalendarEntry Note;
bool start = true;
Phone=s.Phone.Functions;
bool gshutdown = false;
QPtrList<Event> er = calendar->rawEvents();
Event* ev = er.first();
QString message = i18n(" Deleting event # ");
int procCount = 0;
int diffProc = 0;
bool setPossible = true;
#ifdef _WIN32_
QString fileName = locateLocal("data", "korganizer") + "\\tempfile.vcs";
#else
QString fileName = "/tmp/kdepimtemp.vcs";
#endif
//algo 1 delete event
while ( ev ) {
if ( ev->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { // event was changed during sync or is a new one
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("del event1 %d ", procCount);
//event2GSM( calendar, ev, &Note );
if ( ev->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { // delete
QString loc = ev->getID(mProfileName);
if ( !loc.isEmpty() ){
Note.Location = loc.toInt();
} else {
qDebug("error: loc is empty ");
}
error = Phone->DeleteCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
qDebug(" e error delete1 planB %d ", error);
break;
}
}
else if ( ev->getID(mProfileName).isEmpty() ) { // add new
// we have to do this later after deleting
}
else { // change existing
QString loc = ev->getID(mProfileName);
if ( !loc.isEmpty() ){
Note.Location = loc.toInt();
} else {
qDebug("error3: loc is empty ");
}
error = Phone->DeleteCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
qDebug(" e error delete2 planB %d ", error);
break;
}
ev->removeID( mProfileName );
}
}
ev = er.next();
}
//algo 1 delete todo
GSM_ToDoEntry ToDoEntry;
QPtrList<Todo> tl = calendar->rawTodos();
Todo* to = tl.first();
message = i18n(" Deleting todo # ");
procCount = 0;
while ( to ) {
if ( to->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) {
qDebug("todo3 %d ", procCount);
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("todo5 %d ", procCount);
// todo2GSM( calendar, to, &ToDoEntry );
QString loc = to->getID(mProfileName);
if ( !loc.isEmpty() ){
ToDoEntry.Location = loc.toInt();
} else {
qDebug("error2: loc is empty ");
}
if ( to->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { // delete
error=Phone->DeleteToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
qDebug("delete planB %d ", error);
}
}
else if ( to->getID(mProfileName).isEmpty() ) { // add new
;
}
else { // change existing
error=Phone->DeleteToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
qDebug("set planB %d ", error);
}
to->removeID( mProfileName );
}
}
to = tl.next();
}
//algo 2 add event
ev = er.first();
QString filec;
message = i18n(" Preparing event # ");
procCount = 0;
while ( ev ) {
if ( ev->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL && ev->tempSyncStat() != SYNC_TEMPSTATE_DELETE) {
if ( ev->getID(mProfileName).isEmpty() ) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
filec += vfconverter.eventToString( ev, calendar )+ "\n";
afterSave ( ev );
}
}
ev = er.next();
}
//algo 2 add todo
to = tl.first();
procCount = 0;
message = i18n(" Preparing todo # ");
while ( to ) {
qDebug("todo2 %d ", procCount);
if ( to->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL && to->tempSyncStat() != SYNC_TEMPSTATE_DELETE) {
qDebug("todo4 %d ", procCount);
if ( to->getID(mProfileName).isEmpty() ) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
filec += vfconverter.todoToString( to, calendar )+ "\n";
afterSave ( to );
}
}
to = tl.next();
}
if ( filec.isEmpty() ) {
qDebug("Nothing to write back.Finished. ");
error=GSM_TerminateConnection(&s);
return true;
}
//algo 3 saving file
message = i18n(" Saving temp file ... ");
status.setText ( message );
qApp->processEvents();
QFile file( fileName );
if (!file.open( IO_WriteOnly ) ) {
qDebug("error open file ");
error=GSM_TerminateConnection(&s);
return false;
}
QTextStream ts( &file );
ts.setCodec( QTextCodec::codecForName("utf8") );
ts << filec ;
file.close();
message = i18n(" Parsing temp file ... ");
status.setText ( message );
qApp->processEvents();
GSM_Backup Backup;
error=GSM_ReadBackupFile( (char*) fileName.latin1() ,&Backup);
qDebug("Read file result %d ",error );
//algo 4 writing event
int max, i;
procCount = 0;
message = i18n(" Writing event # ");
if (Backup.Calendar[0] != NULL) {
max = 0;
while (Backup.Calendar[max]!=NULL) max++;
for (i=0;i<max;i++) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
Note = *Backup.Calendar[i];
Note.Location = 0;
error=Phone->AddCalendar(&s,&Note);
qDebug("add event %d %d", error, Note.Location );
}
}
//algo 4 writing todo
procCount = 0;
message = i18n(" Writing todo # ");
if (Backup.ToDo[0] != NULL) {
max = 0;
while (Backup.ToDo[max]!=NULL) max++;
for (i=0;i<max;i++) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
ToDoEntry = *Backup.ToDo[i];
error = Phone->AddToDo(&s,&ToDoEntry);
qDebug("add todo %d ", error);
}
}
//algo 5 reread
message = i18n(" Rereading all data ... ");
status.setText ( message );
qApp->processEvents();
error=GSM_TerminateConnection(&s);
CalendarLocal* calendarTemp = new CalendarLocal();
calendarTemp->setTimeZoneId( calendar->timeZoneId());
if ( ! load( calendarTemp,calendar) ){
qDebug("error reloading calendar ");
delete calendarTemp;
return false;
}
//algo 6 compare event
ev = er.first();
message = i18n(" Comparing event # ");
QPtrList<Event> er1 = calendarTemp->rawEvents();
Event* ev1;
procCount = 0;
while ( ev ) {
if ( ev->tempSyncStat() == SYNC_TEMPSTATE_NEW_ID) {
qDebug("event new ID ");
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
QString cSum = ev->getCsum(mProfileName);
ev1 = er1.first();
while ( ev1 ) {
if ( ev1->getCsum( mProfileName ) == cSum ) {
er1.remove( ev1 );
ev->setID(mProfileName, ev1->getID(mProfileName) );
break;
}
ev1 = er1.next();
}
if ( ! ev1 ) {
ev->removeID(mProfileName);
qDebug("ERROR: No event found on phone for %s ", ev->summary().latin1());
qDebug("Probably writing back of events not supported ");
}
}
ev = er.next();
}
//algo 6 compare todo
to = tl.first();
procCount = 0;
QPtrList<Todo> tl1 = calendarTemp->rawTodos();
Todo* to1 ;
message = i18n(" Comparing todo # ");
while ( to ) {
qDebug("todo2 %d ", procCount);
if ( to->tempSyncStat() == SYNC_TEMPSTATE_NEW_ID) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
QString cSum = to->getCsum(mProfileName);
Todo* to1 = tl1.first();
while ( to1 ) {
if ( to1->getCsum( mProfileName ) == cSum ) {
tl1.remove( to1 );
to->setID(mProfileName, to1->getID(mProfileName) );
break;
}
to1 = tl1.next();
}
if ( ! to1 ) {
to->removeID(mProfileName);
qDebug("ERROR: No todo found on phone for %s ", to->summary().latin1());
qDebug("Probably writing back of todos not supported ");
}
}
to = tl.next();
}
delete calendarTemp;
return true;
// *******************************************************************
// *******************************************************************
// *******************************************************************
#if 0
while ( ev && ! planB) {
if ( ev->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { // event was changed during sync or is a new one
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("event1 %d ", procCount);
event2GSM( calendar, ev, &Note );
if ( ev->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { // delete
error = Phone->DeleteCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug(" e delete1 planB %d ", error);
break;
}
}
else if ( ev->getID(mProfileName).isEmpty() ) { // add new
// we have to do this later after deleting
}
else { // change existing
if ( setPossible ) {
error = Phone->SetCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
setPossible = false;
++diffProc;
qDebug("Set cal not supported %d ", error);
break;
}
}
if ( ! setPossible) {
++diffProc;
error = Phone->DeleteCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug(" e delete2 planB %d ", error);
break;
}
ev->removeID( mProfileName );
}
qDebug("Change Calendar. Location %d status: %d",Note.Location, error );
}
}
ev = er.next();
}
ev = er.first();
// pending get empty slots
int loc = 0;
procCount -= diffProc;
while ( ev && ! planB) {
if ( ev->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL && ev->tempSyncStat() != SYNC_TEMPSTATE_DELETE) {
qDebug("event2 %d ", procCount);
if ( ev->getID(mProfileName).isEmpty() ) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
//int newID ;//= pending
//ev->setID(mProfileName, QString::number( newID ));
event2GSM( calendar, ev, &Note );
++loc;
Note.Location = loc;
error = Phone->AddCalendar(&s, &Note);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug(" e add planB %d ", error);
break;
}
ev->setID( mProfileName, QString::number( Note.Location ) );
qDebug("New Calendar. Location %d stat %d %d",Note.Location ,error, ERR_UNKNOWN);
afterSave( ev );
} else {
afterSave( ev ); // setting temp sync stat for changed items
}
}
ev = er.next();
}
if ( planB ) {
qDebug("delete all calendar...");
status.setText ( i18n("Deleting all calendar..."));
qApp->processEvents();
error=Phone->DeleteAllCalendar(&s);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
message = i18n(" Deleting event # ");
procCount = 0;
while (1) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("deleting event ... %d", procCount);
error = Phone->GetNextCalendar(&s,&Note,true);
if (error != ERR_NONE) break;
error = Phone->DeleteCalendar(&s,&Note);
}
qDebug("deleting calendar ... finished");
} else {
status.setText ( i18n("All calendar deleted!"));
qDebug("all cal deleted");
}
bool planC = false;
ev = er.first();
procCount = 0;
message = i18n(" Writing event # ");
while ( ev && ! planC) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
event2GSM( calendar, ev, &Note );
Note.Location = procCount;
error=Phone->AddCalendar(&s,&Note);
if (error != ERR_NONE ) {
// we have currently no planC :-(
// planC = true;
//qDebug("add planC %d ", error);
//break;
// we remove the ID such that this todo is not deleted after next sync
ev->removeID(mProfileName);
ev->setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
qDebug("error :cal adding loc %d planB stat %d ", Note.Location ,error);
} else {
qDebug("cal adding loc %d planB stat %d ", Note.Location ,error);
ev->setID(mProfileName, QString::number( Note.Location ));
afterSave( ev );
}
ev = er.next();
}
if ( planC ) {
qDebug("writing cal went wrong...");
// we have currently no planC :-(
}
}
GSM_ToDoEntry ToDoEntry;
QPtrList<Todo> tl = calendar->rawTodos();
Todo* to = tl.first();
message = i18n(" Processing todo # ");
procCount = 0;
planB = false;
while ( to && ! planB ) {
if ( to->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) {
qDebug("todo3 %d ", procCount);
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
qDebug("todo5 %d ", procCount);
todo2GSM( calendar, to, &ToDoEntry );
if ( to->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { // delete
error=Phone->DeleteToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug("delete planB %d ", error);
}
}
else if ( to->getID(mProfileName).isEmpty() ) { // add new
;
}
else { // change existing
error=Phone->SetToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug("set planB %d ", error);
}
qDebug("Old Todo. Location %d %d",ToDoEntry.Location , error );
}
}
to = tl.next();
}
// pending get empty slots
to = tl.first();
while ( to && ! planB ) {
qDebug("todo2 %d ", procCount);
if ( to->tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL && to->tempSyncStat() != SYNC_TEMPSTATE_DELETE) {
qDebug("todo4 %d ", procCount);
if ( to->getID(mProfileName).isEmpty() ) {
status.setText ( message + QString::number ( ++procCount ) );
qApp->processEvents();
//int newID ;//= pending
//to->setID(mProfileName, QString::number( newID ));
todo2GSM( calendar,to, &ToDoEntry );
ToDoEntry.Location = 0;
error=Phone->AddToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
planB = true;
qDebug("new planB %d ", error);
}
to->setID(mProfileName, QString::number( ToDoEntry.Location ));
afterSave( to );
qDebug("New Todo. Location %d %d",ToDoEntry.Location, error );
} else {
afterSave( to );
}
}
to = tl.next();
}
if ( planB ) {
qDebug("delete all ...");
error=Phone->DeleteAllToDo(&s);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
while (1) {
qDebug("deleting todo ...");
error = Phone->GetNextToDo(&s,&ToDoEntry,true);
if (error != ERR_NONE) break;
error = Phone->DeleteToDo(&s,&ToDoEntry);
}
qDebug("deleting todo ... finished");
} else {
qDebug("all todo deleted");
}
bool planC = false;
to = tl.first();
while ( to && ! planC ) {
todo2GSM( calendar,to, &ToDoEntry );
ToDoEntry.Location = 0;
error=Phone->AddToDo(&s,&ToDoEntry);
if (error == ERR_NOTSUPPORTED || error == ERR_NOTIMPLEMENTED) {
// we have currently no planC :-(
// planC = true;
//qDebug("add planC %d ", error);
//break;
// we remove the ID such that this todo is not deleted after next sync
to->removeID(mProfileName);
to->setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
} else {
qDebug("adding %d planB %d ", ToDoEntry.Location ,error);
to->setID(mProfileName, QString::number( ToDoEntry.Location ));
afterSave( to );
}
to = tl.next();
}
if ( planC ) {
// we have currently no planC :-(
}
}
return true;
#endif
}
QString PhoneFormat::dtToGSM( const QDateTime& dti, bool useTZ )
{
QString datestr;
QString timestr;
int offset = KGlobal::locale()->localTimeOffset( dti );
QDateTime dt;
if (useTZ)
dt = dti.addSecs ( -(offset*60));
else
dt = dti;
if(dt.date().isValid()){
const QDate& date = dt.date();
datestr.sprintf("%04d%02d%02d",
date.year(), date.month(), date.day());
}
if(dt.time().isValid()){
const QTime& time = dt.time();
timestr.sprintf("T%02d%02d%02d",
time.hour(), time.minute(), time.second());
}
return datestr + timestr;
}
QString PhoneFormat::getEventString( Event* event )
{
#if 0
QStringList list;
list.append( QString::number(event->zaurusId() ) );
list.append( event->categories().join(",") );
if ( !event->summary().isEmpty() )
list.append( event->summary() );
else
list.append("" );
if ( !event->location().isEmpty() )
list.append( event->location() );
else
list.append("" );
if ( !event->description().isEmpty() )
list.append( event->description() );
else
list.append( "" );
if ( event->doesFloat () ) {
list.append( dtToString( QDateTime(event->dtStart().date(), QTime(0,0,0)), false ));
list.append( dtToString( QDateTime(event->dtEnd().date(),QTime(23,59,59)), false )); //6
list.append( "1" );
}
else {
list.append( dtToString( event->dtStart()) );
list.append( dtToString( event->dtEnd()) ); //6
list.append( "0" );
}
bool noAlarm = true;
if ( event->alarms().count() > 0 ) {
Alarm * al = event->alarms().first();
if ( al->enabled() ) {
noAlarm = false;
list.append( "0" ); // yes, 0 == alarm
list.append( QString::number( al->startOffset().asSeconds()/(-60) ) );
if ( al->type() == Alarm::Audio )
list.append( "1" ); // type audio
else
list.append( "0" ); // type silent
}
}
if ( noAlarm ) {
list.append( "1" ); // yes, 1 == no alarm
list.append( "0" ); // no alarm offset
list.append( "1" ); // type
}
// next is: 11
// next is: 11-16 are recurrence
Recurrence* rec = event->recurrence();
bool writeEndDate = false;
switch ( rec->doesRecur() )
{
case Recurrence::rDaily: // 0
list.append( "0" );
list.append( QString::number( rec->frequency() ));//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
case Recurrence::rWeekly:// 1
list.append( "1" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
{
int days = 0;
QBitArray weekDays = rec->days();
int i;
for( i = 1; i <= 7; ++i ) {
if ( weekDays[i-1] ) {
days += 1 << (i-1);
}
}
list.append( QString::number( days ) );
}
//pending weekdays
writeEndDate = true;
break;
case Recurrence::rMonthlyPos:// 2
list.append( "2" );
list.append( QString::number( rec->frequency()) );//12
writeEndDate = true;
{
int count = 1;
QPtrList<Recurrence::rMonthPos> rmp;
rmp = rec->monthPositions();
if ( rmp.first()->negative )
count = 5 - rmp.first()->rPos - 1;
else
count = rmp.first()->rPos - 1;
list.append( QString::number( count ) );
}
list.append( "0" );
break;
case Recurrence::rMonthlyDay:// 3
list.append( "3" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
case Recurrence::rYearlyMonth://4
list.append( "4" );
list.append( QString::number( rec->frequency()) );//12
list.append( "0" );
list.append( "0" );
writeEndDate = true;
break;
default:
list.append( "255" );
list.append( QString() );
list.append( "0" );
list.append( QString() );
list.append( "0" );
list.append( "20991231T000000" );
break;
}
if ( writeEndDate ) {
if ( rec->endDate().isValid() ) { // 15 + 16
list.append( "1" );
list.append( dtToString( rec->endDate()) );
} else {
list.append( "0" );
list.append( "20991231T000000" );
}
}
if ( event->doesFloat () ) {
list.append( dtToString( event->dtStart(), false ).left( 8 ));
list.append( dtToString( event->dtEnd(), false ).left( 8 )); //6
}
else {
list.append( QString() );
list.append( QString() );
}
if (event->dtStart().date() == event->dtEnd().date() )
list.append( "0" );
else
list.append( "1" );
for(QStringList::Iterator it=list.begin();
it!=list.end(); ++it){
QString& s = (*it);
s.replace(QRegExp("\""), "\"\"");
if(s.contains(QRegExp("[,\"\r\n]")) || s.stripWhiteSpace() != s){
s.prepend('\"');
s.append('\"');
} else if(s.isEmpty() && !s.isNull()){
s = "\"\"";
}
}
return list.join(",");
#endif
return QString();
}
QString PhoneFormat::getTodoString( Todo* todo )
{
#if 0
QStringList list;
list.append( QString::number( todo->zaurusId() ) );
list.append( todo->categories().join(",") );
if ( todo->hasStartDate() ) {
list.append( dtToString( todo->dtStart()) );
} else
list.append( QString() );
if ( todo->hasDueDate() ) {
QTime tim;
if ( todo->doesFloat()) {
list.append( dtToString( QDateTime(todo->dtDue().date(),QTime( 0,0,0 )), false)) ;
} else {
list.append( dtToString(todo->dtDue() ) );
}
} else
list.append( QString() );
if ( todo->isCompleted() ) {
list.append( dtToString( todo->completed()) );
list.append( "0" ); // yes 0 == completed
} else {
list.append( dtToString( todo->completed()) );
list.append( "1" );
}
list.append( QString::number( todo->priority() ));
if( ! todo->summary().isEmpty() )
list.append( todo->summary() );
else
list.append( "" );
if (! todo->description().isEmpty() )
list.append( todo->description() );
else
list.append( "" );
for(QStringList::Iterator it=list.begin();
it!=list.end(); ++it){
QString& s = (*it);
s.replace(QRegExp("\""), "\"\"");
if(s.contains(QRegExp("[,\"\r\n]")) || s.stripWhiteSpace() != s){
s.prepend('\"');
s.append('\"');
} else if(s.isEmpty() && !s.isNull()){
s = "\"\"";
}
}
return list.join(",");
#endif
return QString();
}
QString PhoneFormat::toString( Calendar * )
{
return QString::null;
}
bool PhoneFormat::fromString( Calendar *calendar, const QString & text)
{
return false;
}
|