summaryrefslogtreecommitdiffstats
path: root/src/imports/organizer/qdeclarativeorganizermodel.cpp
blob: f830e7befdac2afb5bdcb1bc4d6bcfb85a17fb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtOrganizer module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativeorganizermodel_p.h"

#include <QtCore/qfile.h>
#include <QtCore/qmath.h>
#include <QtCore/qurl.h>
#include <QtCore/qpointer.h>
#include <QtCore/qtimer.h>

#include <QtQml/qqmlinfo.h>

#include <QtOrganizer/qorganizeritemdetails.h>
#include <QtOrganizer/qorganizeritemrequests.h>
#include <QtOrganizer/qorganizermanager.h>

#include <QtVersitOrganizer/qversitorganizerimporter.h>
#include <QtVersitOrganizer/qversitorganizerexporter.h>

#include "qdeclarativeorganizercollection_p.h"

QTORGANIZER_USE_NAMESPACE
QTVERSITORGANIZER_USE_NAMESPACE

QT_BEGIN_NAMESPACE

// TODO:
// - Improve handling of itemsModified signal. Instead of fetching all items each time the
//   signal is received, only modified items should be fetched. Item based fetching allows easier
//   use of any item id based caches backends might have.
// - Full update is not needed every time some model property changes. Collections should
//   be updated only if collections have been changed while autoUpdate is off.
// - Changing the time period is by far the most common use case and should be optimized.
//   If new time period overlaps with the old, no need to fetch all the items in new time period.
// - All changes happening during autoUpdate is off should be monitored. If only timePeriod changes
//   there is no need for full update after switching autoUpdate on.


static QString urlToLocalFileName(const QUrl& url)
{
   if (!url.isValid()) {
      return url.toString();
   } else if (url.scheme() == "qrc") {
      return url.toString().remove(0, 5).prepend(':');
   } else {
      return url.toLocalFile();
   }

}

static const char ITEM_TO_SAVE_PROPERTY[] = {"ITEM_TO_SAVE_PROPERTY"};
static const char MANUALLY_TRIGGERED_PROPERTY[] = {"MANUALLY_TRIGGERED"};

class QDeclarativeOrganizerModelPrivate
{
public:
    QDeclarativeOrganizerModelPrivate()
        :m_manager(0),
        m_fetchHint(0),
        m_filter(0),
        m_fetchRequest(0),
        m_occurrenceFetchRequest(0),
        m_reader(0),
        m_writer(0),
        m_startPeriod(QDateTime::currentDateTime()),
        m_endPeriod(QDateTime::currentDateTime()),
        m_error(QOrganizerManager::NoError),
        m_autoUpdate(true),
        m_updatePendingFlag(QDeclarativeOrganizerModelPrivate::NonePending),
        m_componentCompleted(false),
        m_initialUpdate(false),
        m_lastRequestId(0)
    {
    }
    ~QDeclarativeOrganizerModelPrivate()
    {
        if (m_manager)
            delete m_manager;
        delete m_reader;
        delete m_writer;
}

    QList<QDeclarativeOrganizerItem*> m_items;
    QHash<QString, QDeclarativeOrganizerItem *> m_itemIdHash;
    QOrganizerManager* m_manager;
    QDeclarativeOrganizerItemFetchHint* m_fetchHint;
    QList<QOrganizerItemSortOrder> m_sortOrders;
    QList<QDeclarativeOrganizerItemSortOrder*> m_declarativeSortOrders;
    QDeclarativeOrganizerItemFilter* m_filter;
    QOrganizerItemFetchRequest* m_fetchRequest;
    QSet<QOrganizerItemId> m_addedItemIds;
    QMap<QOrganizerAbstractRequest*, QSet<QOrganizerItemId> > m_notifiedItems;
    QOrganizerItemOccurrenceFetchRequest* m_occurrenceFetchRequest;
    QStringList m_importProfiles;
    QVersitReader *m_reader;
    QVersitWriter *m_writer;
    QDateTime m_startPeriod;
    QDateTime m_endPeriod;
    QList<QDeclarativeOrganizerCollection*> m_collections;

    QTimer m_updateTimer;
    QTimer m_updateItemsTimer;
    QTimer m_fetchCollectionsTimer;
    QTimer m_modelChangedTimer;

    QOrganizerManager::Error m_error;

    bool m_autoUpdate;
    enum UpdateTypePending {
        NonePending = 0x0,
        UpdatingItemsPending = 0x1,
        UpdatingCollectionsPending = 0x2
    };
    int m_updatePendingFlag;
    bool m_componentCompleted;
    bool m_initialUpdate;

    QAtomicInt m_lastRequestId;
    QHash<QOrganizerAbstractRequest *, int> m_requestIdHash;
    QUrl m_lastExportUrl;
    QUrl m_lastImportUrl;
};

/*!
    \qmltype OrganizerModel
    \instantiates QDeclarativeOrganizerModel
    \brief The OrganizerModel element provides access to organizer items from the organizer store.
    \inqmlmodule QtOrganizer
    \ingroup qml-organizer-main

    OrganizerModel provides a model of organizer items from the organizer store.
    The contents of the model can be specified with \l filter, \l sortOrders and \l fetchHint properties.
    Whether the model is automatically updated when the store or
    \l{Qt Organizer Overview} {C++ organizer} item changes, can be
    controlled with \l OrganizerModel::autoUpdate property.

    There are two ways of accessing the organizer item data: via the model by using views and delegates,
    or alternatively via \l items list property. Of the two, the model access is preferred.
    Direct list access (i.e. non-model) is not guaranteed to be in order set by \l sortOrder.

    At the moment the model roles provided by OrganizerModel are \c display and \c item.
    Through the \c item role can access any data provided by the OrganizerItem element.


    \note Both the \c startPeriod and \c endPeriod are set by default to the current time (when the OrganizerModel was created).
     In most cases, both (or at least one) of the startPeriod and endPeriod should be set; otherwise, the OrganizerModel will contain
     zero items because the \c startPeriod and \c endPeriod are the same value. For example, if only \c endPeriod is provided,
     the OrganizerModel will contain all items from now (the time of the OrganizerModel's creation) to the \c endPeriod time.

    \sa OrganizerItem, {QOrganizerManager}
*/

/*!
  \qmlsignal OrganizerModel::onModelChanged()

  This signal is emitted, when there are changes in items contained by \l OrganizerModel's data model. Items have either
  been added, removed or modified. This signal is also always emitted during OrganizerModel construction when data model is
  ready for use, even in cases when data model is not having any items in it.
 */
QDeclarativeOrganizerModel::QDeclarativeOrganizerModel(QObject *parent) :
    QAbstractListModel(parent),
    d_ptr(new QDeclarativeOrganizerModelPrivate)
{
    d_ptr->m_updateTimer.setSingleShot(true);
    d_ptr->m_updateItemsTimer.setSingleShot(true);
    d_ptr->m_fetchCollectionsTimer.setSingleShot(true);
    d_ptr->m_modelChangedTimer.setSingleShot(true);
    d_ptr->m_updateTimer.setInterval(1);
    d_ptr->m_updateItemsTimer.setInterval(1);
    d_ptr->m_fetchCollectionsTimer.setInterval(1);
    d_ptr->m_modelChangedTimer.setInterval(1);
    connect(&d_ptr->m_updateTimer, &QTimer::timeout, this, &QDeclarativeOrganizerModel::doUpdate);
    connect(&d_ptr->m_updateItemsTimer, &QTimer::timeout, this, &QDeclarativeOrganizerModel::doUpdateItems);
    connect(&d_ptr->m_fetchCollectionsTimer, &QTimer::timeout, this, &QDeclarativeOrganizerModel::fetchCollections);
    connect(&d_ptr->m_modelChangedTimer, &QTimer::timeout, this, &QDeclarativeOrganizerModel::modelChanged);

    connect(this, &QDeclarativeOrganizerModel::filterChanged, &d_ptr->m_updateItemsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(this, &QDeclarativeOrganizerModel::fetchHintChanged, &d_ptr->m_updateItemsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(this, &QDeclarativeOrganizerModel::sortOrdersChanged, &d_ptr->m_updateItemsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(this, &QDeclarativeOrganizerModel::startPeriodChanged, &d_ptr->m_updateItemsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(this, &QDeclarativeOrganizerModel::endPeriodChanged, &d_ptr->m_updateItemsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
}

QDeclarativeOrganizerModel::~QDeclarativeOrganizerModel()
{
}

QHash<int, QByteArray> QDeclarativeOrganizerModel::roleNames() const
{
    QHash<int, QByteArray> roleNames = QAbstractItemModel::roleNames();
    roleNames.insert(OrganizerItemRole, "item");
    return roleNames;
}

/*!
  \qmlproperty string OrganizerModel::manager

  This property holds the manager name or manager uri of the organizer backend engine.
  The manager uri format: qtorganizer:<managerid>:<key>=<value>&<key>=<value>.

  For example, memory organizer engine has an optional id parameter, if user want to
  share the same memory engine with multiple OrganizerModel instances, the manager property
  should declared like this:
  \code
    model : OrganizerModel {
       manager:"qtorganizer:memory:id=organizer1
    }
  \endcode

  instead of just the manager name:
  \code
    model : OrganizerModel {
       manager:"memory"
    }
  \endcode

  \sa QOrganizerManager::fromUri()
  */
QString QDeclarativeOrganizerModel::manager() const
{
    Q_D(const QDeclarativeOrganizerModel);
    if (d->m_manager)
        return d->m_manager->managerUri();
    return QString();
}

/*!
  \qmlproperty string OrganizerModel::managerName

  This property holds the manager name of the organizer backend engine.
  This property is read only.
  \sa QOrganizerManager::fromUri()
  */
QString QDeclarativeOrganizerModel::managerName() const
{
    Q_D(const QDeclarativeOrganizerModel);
    if (d->m_manager)
        return d->m_manager->managerName();
    return QString();
}

/*!
  \qmlproperty list<string> OrganizerModel::availableManagers

  This property holds the list of available manager names.
  This property is read only.
  */
QStringList QDeclarativeOrganizerModel::availableManagers() const
{
    return QOrganizerManager::availableManagers();
}

/*!
  \qmlproperty bool OrganizerModel::autoUpdate

  This property indicates whether or not the organizer model should be updated automatically, default value is true.

  \sa OrganizerModel::update()
  */
void QDeclarativeOrganizerModel::setAutoUpdate(bool autoUpdate)
{
    Q_D(QDeclarativeOrganizerModel);
    if (autoUpdate == d->m_autoUpdate)
        return;
    d->m_autoUpdate = autoUpdate;
    emit autoUpdateChanged();
}

bool QDeclarativeOrganizerModel::autoUpdate() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_autoUpdate;
}

/*!
  \qmlmethod OrganizerModel::update()

  Manually update the organizer model content including both
  items and collections.

  \sa OrganizerModel::updateItems
  \sa OrganizerModel::updateCollections
  \sa OrganizerModel::autoUpdate
  */
void QDeclarativeOrganizerModel::update()
{
    Q_D(QDeclarativeOrganizerModel);
    if (!d->m_componentCompleted || d->m_updatePendingFlag)
        return;

    // Disallow possible duplicate request triggering
    d->m_updatePendingFlag = (QDeclarativeOrganizerModelPrivate::UpdatingItemsPending | QDeclarativeOrganizerModelPrivate::UpdatingCollectionsPending);
    d->m_fetchCollectionsTimer.setProperty(MANUALLY_TRIGGERED_PROPERTY, QVariant::fromValue<bool>(true));
    d->m_fetchCollectionsTimer.start();
}

void QDeclarativeOrganizerModel::doUpdate()
{
    Q_D(QDeclarativeOrganizerModel);
    if (d->m_autoUpdate)
        update();
}

void QDeclarativeOrganizerModel::doUpdateItems()
{
    Q_D(QDeclarativeOrganizerModel);
    if (d->m_autoUpdate)
        updateItems();
}

/*!
  \qmlmethod OrganizerModel::updateItems()

  Manually update the organizer model items.

  \sa OrganizerModel::update
  \sa OrganizerModel::updateCollections
  \sa OrganizerModel::autoUpdate
  */
void QDeclarativeOrganizerModel::updateItems()
{
    Q_D(QDeclarativeOrganizerModel);
    if (!d->m_componentCompleted || d->m_updatePendingFlag)
        return;
    d->m_updatePendingFlag = QDeclarativeOrganizerModelPrivate::UpdatingItemsPending;// Disallow possible duplicate request triggering
    QMetaObject::invokeMethod(this, "fetchAgain", Qt::QueuedConnection);
}

/*!
  \qmlmethod OrganizerModel::updateCollections()

  Manually update the organizer model collections.

  \sa OrganizerModel::update
  \sa OrganizerModel::updateItems
  \sa OrganizerModel::autoUpdate
  */
void QDeclarativeOrganizerModel::updateCollections()
{
    Q_D(QDeclarativeOrganizerModel);
    if (!d->m_componentCompleted || d->m_updatePendingFlag)
        return;
    d->m_updatePendingFlag = QDeclarativeOrganizerModelPrivate::UpdatingCollectionsPending;// Disallow possible duplicate request triggering
    d->m_fetchCollectionsTimer.setProperty(MANUALLY_TRIGGERED_PROPERTY, QVariant::fromValue<bool>(true));
    d->m_fetchCollectionsTimer.start();
}

/*!
  \qmlmethod OrganizerModel::cancelUpdate()

  Cancel the running organizer model content update request.

  \sa OrganizerModel::autoUpdate, OrganizerModel::update
  */
void QDeclarativeOrganizerModel::cancelUpdate()
{
    Q_D(QDeclarativeOrganizerModel);
    if (d->m_fetchRequest) {
        d->m_fetchRequest->cancel();
        d->m_fetchRequest->deleteLater();
        d->m_fetchRequest = 0;
        d->m_updatePendingFlag = QDeclarativeOrganizerModelPrivate::NonePending;
    }
}
/*!
  \qmlproperty date OrganizerModel::startPeriod

  This property holds the start date and time period used by the organizer model to fetch organizer items.
  The default value is the datetime of OrganizerModel creation.
  */
QDateTime QDeclarativeOrganizerModel::startPeriod() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_startPeriod;
}
void QDeclarativeOrganizerModel::setStartPeriod(const QDateTime& start)
{
    Q_D(QDeclarativeOrganizerModel);
    if (start != d->m_startPeriod) {
        d->m_startPeriod = start;
        emit startPeriodChanged();
    }
}

/*!
  \qmlproperty date OrganizerModel::endPeriod

  This property holds the end date and time period used by the organizer model to fetch organizer items.
  The default value is the datetime of OrganizerModel creation.
  */
QDateTime QDeclarativeOrganizerModel::endPeriod() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_endPeriod;
}
void QDeclarativeOrganizerModel::setEndPeriod(const QDateTime& end)
{
    Q_D(QDeclarativeOrganizerModel);
    if (end != d->m_endPeriod) {
        d->m_endPeriod = end;
        emit endPeriodChanged();
    }
}

/*!
  \qmlproperty enumeration OrganizerModel::ImportError

  Defines the errors cases for \l OrganizerModel::importItems() -function.

  \list
  \li OrganizerModel::ImportNoError             Completed successfully, no error.
  \li OrganizerModel::ImportUnspecifiedError    Unspecified error.
  \li OrganizerModel::ImportIOError             Input/output error.
  \li OrganizerModel::ImportOutOfMemoryError    Out of memory error.
  \li OrganizerModel::ImportNotReadyError       Not ready for importing. Only one import operation can be active at a time.
  \li OrganizerModel::ImportParseError          Error during parsing.
  \endlist
*/

/*!
  \qmlsignal OrganizerModel::onImportCompleted(ImportError error, URL url, list<string> ids)

  This signal is emitted, when \l OrganizerModel::importItems() completes. The success of operation
  can be seen on \a error which is defined in \l OrganizerModel::ImportError. \a url indicates the
  file, which was imported. \a ids contains the imported items ids.

  If the operation was successful, items are now imported to backend. If \l OrganizerModel::autoUpdate
  is enabled, \l OrganizerModel::modelChanged will be emitted when imported items are also visible on
  \l OrganizerModel's data model.

  \sa OrganizerModel::importItems
 */

/*!
  \qmlmethod OrganizerModel::importItems(url url, list<string> profiles)

  Import organizer items from a vcalendar by the given \a url and optional \a profiles.
  Only one import operation can be active at a time.
 */
void QDeclarativeOrganizerModel::importItems(const QUrl& url, const QStringList &profiles)
{
    Q_D(QDeclarativeOrganizerModel);

    ImportError importError = ImportNotReadyError;

    // Reader is capable of handling only one request at the time.
  if (!d->m_reader || (d->m_reader->state() != QVersitReader::ActiveState)) {

        d->m_importProfiles = profiles;

        //TODO: need to allow download vcard from network
        QFile *file = new QFile(urlToLocalFileName(url));
        if (file->open(QIODevice::ReadOnly)) {
            if (!d->m_reader) {
                d->m_reader = new QVersitReader;
                connect(d->m_reader, SIGNAL(stateChanged(QVersitReader::State)), this, SLOT(startImport(QVersitReader::State)));
            }

            d->m_reader->setDevice(file);
            if (d->m_reader->startReading()) {
                d->m_lastImportUrl = url;
                return;
            }
            importError = QDeclarativeOrganizerModel::ImportError(d->m_reader->error());
        } else {
            importError = ImportIOError;
        }
  }

    // If cannot startReading because already running then report the import error now
    emit importCompleted(importError, url, QStringList());
}

/*!
  \qmlsignal OrganizerModel::onExportCompleted()

  This signal is emitted, when \l OrganizerModel::exportItems() completes. The success of operation
  can be seen on \a error which is defined in \l OrganizerModel::ExportError. \a url indicates the
  file, which was exported.
 */
/*!
  \qmlmethod OrganizerModel::exportItems(url url, list<string> profiles)
  Export organizer items into a vcalendar file to the given \a url by optional \a profiles.
  At the moment only the local file url is supported in export method.
  */
void QDeclarativeOrganizerModel::exportItems(const QUrl &url, const QStringList &profiles)
{
    Q_D(QDeclarativeOrganizerModel);
    ExportError exportError = ExportNotReadyError;

    // Writer is capable of handling only one request at the time.
    if (!d->m_writer || (d->m_writer->state() != QVersitWriter::ActiveState)) {

        QString profile = profiles.isEmpty() ? QString() : profiles.at(0);

        QVersitOrganizerExporter exporter(profile);
        QList<QOrganizerItem> items;
        foreach (QDeclarativeOrganizerItem *di, d->m_items)
            items.append(di->item());

        exporter.exportItems(items, QVersitDocument::ICalendar20Type);
        QVersitDocument document = exporter.document();
        QFile *file = new QFile(urlToLocalFileName(url));
        if (file->open(QIODevice::ReadWrite)) {
            if (!d->m_writer) {
                d->m_writer = new QVersitWriter;
                connect(d->m_writer, SIGNAL(stateChanged(QVersitWriter::State)), this, SLOT(itemsExported(QVersitWriter::State)));
            }
            d->m_writer->setDevice(file);
            if (d->m_writer->startWriting(document)) {
                d->m_lastExportUrl = url;
                return;
            }
            exportError = QDeclarativeOrganizerModel::ExportError(d->m_writer->error());
        } else {
            exportError = ExportIOError;
        }
    }
    emit exportCompleted(exportError, url);
}

void QDeclarativeOrganizerModel::itemsExported(QVersitWriter::State state)
{
    Q_D(QDeclarativeOrganizerModel);
    if (state == QVersitWriter::FinishedState || state == QVersitWriter::CanceledState) {
         emit exportCompleted(QDeclarativeOrganizerModel::ExportError(d->m_writer->error()), d->m_lastExportUrl);
         delete d->m_writer->device();
         d->m_writer->setDevice(0);
    }
}

int QDeclarativeOrganizerModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_items.count();
}

void QDeclarativeOrganizerModel::setManager(const QString& managerName)
{
    Q_D(QDeclarativeOrganizerModel);
    if (d->m_manager && (managerName == d->m_manager->managerName() || managerName == d->m_manager->managerUri()))
        return;

    if (d->m_manager) {
        cancelUpdate();
        d->m_updatePendingFlag = QDeclarativeOrganizerModelPrivate::NonePending;
        delete d->m_manager;
    }

    if (managerName.startsWith("qtorganizer:")) {
        d->m_manager = QOrganizerManager::fromUri(managerName, this);
    } else {
        d->m_manager = new QOrganizerManager(managerName, QMap<QString, QString>(), this);
    }

    connect(d->m_manager, &QOrganizerManager::collectionsAdded, &d->m_fetchCollectionsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(d->m_manager, &QOrganizerManager::collectionsChanged, &d->m_fetchCollectionsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(d->m_manager, &QOrganizerManager::collectionsRemoved, &d->m_fetchCollectionsTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(d->m_manager, &QOrganizerManager::dataChanged, &d->m_updateTimer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
    connect(d->m_manager, SIGNAL(itemsModified(QList<QPair<QOrganizerItemId,QOrganizerManager::Operation> >)), this, SLOT(onItemsModified(QList<QPair<QOrganizerItemId,QOrganizerManager::Operation> >)));

    const QOrganizerManager::Error managerError = d->m_manager->error();
    if (QOrganizerManager::NoError != managerError && d->m_error != managerError) {
        d->m_error = managerError;
        emit errorChanged();
    } else if (QOrganizerManager::NoError != d->m_error) {
        d->m_error = QOrganizerManager::NoError;
        emit errorChanged();
    }

    emit managerChanged();
}

void QDeclarativeOrganizerModel::componentComplete()
{
    Q_D(QDeclarativeOrganizerModel);
    d->m_componentCompleted = true;
    if (!d->m_manager)
        setManager(QString());

    if (d->m_autoUpdate) {
        d->m_initialUpdate = true;
        update();
    } else {
        emit modelChanged();
    }
}
/*!
  \qmlproperty Filter OrganizerModel::filter

  This property holds the filter instance used by the organizer model.

  Set filter property to 'null', when you want to reset it to default value.

  \sa Filter
  */
QDeclarativeOrganizerItemFilter* QDeclarativeOrganizerModel::filter() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_filter;
}

void QDeclarativeOrganizerModel::setFilter(QDeclarativeOrganizerItemFilter* filter)
{
    Q_D(QDeclarativeOrganizerModel);
    if (filter != d->m_filter) {
        if (d->m_filter)
            disconnect(d->m_filter, SIGNAL(filterChanged()), this, SIGNAL(filterChanged()));
        d->m_filter = filter;
        if (d->m_filter)
            connect(d->m_filter, SIGNAL(filterChanged()), this, SIGNAL(filterChanged()), Qt::UniqueConnection);
        emit filterChanged();
    }
}

/*!
  \qmlproperty FetchHint OrganizerModel::fetchHint

  This property holds the fetch hint instance used by the organizer model.

  \sa FetchHint
  */
QDeclarativeOrganizerItemFetchHint* QDeclarativeOrganizerModel::fetchHint() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_fetchHint;
}

void QDeclarativeOrganizerModel::setFetchHint(QDeclarativeOrganizerItemFetchHint* fetchHint)
{
    Q_D(QDeclarativeOrganizerModel);
    if (fetchHint != d->m_fetchHint) {
        if (d->m_fetchHint)
            disconnect(d->m_fetchHint, SIGNAL(fetchHintChanged()), this, SIGNAL(fetchHintChanged()));
        d->m_fetchHint = fetchHint;
        if (d->m_fetchHint)
            connect(d->m_fetchHint, SIGNAL(fetchHintChanged()), this, SIGNAL(fetchHintChanged()), Qt::UniqueConnection);
        emit fetchHintChanged();
    }
}
/*!
  \qmlproperty int OrganizerModel::itemCount

  This property holds the size of organizer items the OrganizerModel currently holds.

  This property is read only.
  */
int QDeclarativeOrganizerModel::itemCount() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_items.size();
}
/*!
  \qmlproperty string OrganizerModel::error

  This property holds the latest error code returned by the organizer manager.

  This property is read only.
  */
QString QDeclarativeOrganizerModel::error() const
{
    Q_D(const QDeclarativeOrganizerModel);
    if (d->m_manager) {
        switch (d->m_error) {
        case QOrganizerManager::DoesNotExistError:
            return QStringLiteral("DoesNotExist");
        case QOrganizerManager::AlreadyExistsError:
            return QStringLiteral("AlreadyExists");
        case QOrganizerManager::InvalidDetailError:
            return QStringLiteral("InvalidDetail");
        case QOrganizerManager::InvalidCollectionError:
            return QStringLiteral("InvalidCollection");
        case QOrganizerManager::LockedError:
            return QStringLiteral("LockedError");
        case QOrganizerManager::DetailAccessError:
            return QStringLiteral("DetailAccessError");
        case QOrganizerManager::PermissionsError:
            return QStringLiteral("PermissionsError");
        case QOrganizerManager::OutOfMemoryError:
            return QStringLiteral("OutOfMemory");
        case QOrganizerManager::NotSupportedError:
            return QStringLiteral("NotSupported");
        case QOrganizerManager::BadArgumentError:
            return QStringLiteral("BadArgument");
        case QOrganizerManager::UnspecifiedError:
            return QStringLiteral("UnspecifiedError");
        case QOrganizerManager::LimitReachedError:
            return QStringLiteral("LimitReached");
        case QOrganizerManager::InvalidItemTypeError:
            return QStringLiteral("InvalidItemType");
        case QOrganizerManager::InvalidOccurrenceError:
            return QStringLiteral("InvalidOccurrence");
        default:
            break;
        }
    }
    return QStringLiteral("NoError");
}

/*!
  \qmlproperty list<SortOrder> OrganizerModel::sortOrders

  This property holds a list of sort orders used by the organizer model.

  \sa SortOrder
  */
QQmlListProperty<QDeclarativeOrganizerItemSortOrder> QDeclarativeOrganizerModel::sortOrders()
{
    return { this,
             nullptr,
             &sortOrder_append,
             &sortOrder_count,
             &sortOrder_at,
             &sortOrder_clear };
}

void QDeclarativeOrganizerModel::startImport(QVersitReader::State state)
{
    Q_D(QDeclarativeOrganizerModel);
    if (state == QVersitReader::FinishedState || state == QVersitReader::CanceledState) {
        QStringList ids;

        if (!d->m_reader->results().isEmpty()) {
            QVersitOrganizerImporter importer;
            importer.importDocument(d->m_reader->results().at(0));
            QList<QOrganizerItem> items = importer.items();
            delete d->m_reader->device();
            d->m_reader->setDevice(0);

            if (d->m_manager && !d->m_manager->saveItems(&items)) {
                if (d->m_error != d->m_manager->error()) {
                    d->m_error = d->m_manager->error();
                    emit errorChanged();
                }
            } else {
                foreach (const QOrganizerItem i, items) {
                    ids << i.id().toString();
                }
            }
        }
        emit importCompleted(QDeclarativeOrganizerModel::ImportError(d->m_reader->error()), d->m_lastImportUrl, ids);
    }
}

bool QDeclarativeOrganizerModel::itemHasRecurrence(const QOrganizerItem& oi) const
{
    if (oi.type() == QOrganizerItemType::TypeEvent || oi.type() == QOrganizerItemType::TypeTodo) {
        QOrganizerItemRecurrence recur = oi.detail(QOrganizerItemDetail::TypeRecurrence);
        return !recur.recurrenceDates().isEmpty() || !recur.recurrenceRules().isEmpty();
    }

    return false;
}

QDeclarativeOrganizerItem* QDeclarativeOrganizerModel::createItem(const QOrganizerItem& item)
{
    QDeclarativeOrganizerItem* di;
    if (item.type() == QOrganizerItemType::TypeEvent)
        di = new QDeclarativeOrganizerEvent(this);
    else if (item.type() == QOrganizerItemType::TypeEventOccurrence)
        di = new QDeclarativeOrganizerEventOccurrence(this);
    else if (item.type() == QOrganizerItemType::TypeTodo)
        di = new QDeclarativeOrganizerTodo(this);
    else if (item.type() == QOrganizerItemType::TypeTodoOccurrence)
        di = new QDeclarativeOrganizerTodoOccurrence(this);
    else if (item.type() == QOrganizerItemType::TypeJournal)
        di = new QDeclarativeOrganizerJournal(this);
    else if (item.type() == QOrganizerItemType::TypeNote)
        di = new QDeclarativeOrganizerNote(this);
    else
        di = new QDeclarativeOrganizerItem(this);
    di->setItem(item);
    return di;
}

void QDeclarativeOrganizerModel::checkError(const QOrganizerAbstractRequest *request)
{
    Q_D(QDeclarativeOrganizerModel);
    if (request && d->m_error != request->error()) {
        d->m_error = request->error();
        emit errorChanged();
    }
}

/*!
    \qmlsignal OrganizerModel::onItemsFetched(int requestId, list<OrganizerItem> fetchedItems)

    This handler is called when request of the given \a requestId is finished with the \a fetchedItems.

    \sa fetchItems
 */

/*!
    \qmlmethod int OrganizerModel::fetchItems(date start, date end,
                               Filter filter,
                               int maxCount,
                               list<SortOrder> sortOrders,
                               FetchHint fetchHint)

    This method will start a request to fetch items between the given \a start and \a end dates.
    Optionally a \a sort order, \a filter, \a fetchHint and \a maxCount can
    be specified to narrow the search. If nothing is set for these optional paramenters then
    defaults are applied, essentially any sort order, default filter, default storage location and all items.

    The unique ID of this request will be returned.  If the request can't be started -1 is returned.
    The end date must be greater than the start date for this method to start a fetch request.

    Note that the items fetched won't be added to the model, but can be accessed through the onItemsFetched
    handler. No properties in the model are updated at all.

    \sa onItemsFetched
  */
int QDeclarativeOrganizerModel::fetchItems(const QDateTime &start, const QDateTime &end,
                                           QDeclarativeOrganizerItemFilter *filter,
                                           int maxCount,
                                           const QVariantList &sortOrders,
                                           QDeclarativeOrganizerItemFetchHint *fetchHint)
{
    Q_D(QDeclarativeOrganizerModel);
    if (!start.isValid() || !end.isValid() || !(end > start))
    {
        return -1;
    }

    // Parameter validation left to fetch request method.
    QOrganizerItemFetchRequest *fetchRequest = new QOrganizerItemFetchRequest(this);
    connect(fetchRequest, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)),
            this, SLOT(onFetchItemsRequestStateChanged(QOrganizerAbstractRequest::State)));

    QList<QOrganizerItemSortOrder> sList;
    QVariantList::const_iterator it = sortOrders.begin();
    while (it != sortOrders.end()) {
        if ((*it).canConvert<QObject *>()) {
            QDeclarativeOrganizerItemSortOrder *sortOrderItem = (*it).value<QDeclarativeOrganizerItemSortOrder *>();
            sList << sortOrderItem->sortOrder();
        }
        ++it;
    }

    const QOrganizerItemFilter &fetchFilter = filter->filter();
    fetchRequest->setFilter( fetchFilter );
    const QOrganizerItemFetchHint &hint = fetchHint->fetchHint();

    fetchRequest->setManager(d->m_manager);
    fetchRequest->setStartDate(start);
    fetchRequest->setEndDate(end);
    fetchRequest->setSorting(sList);
    fetchRequest->setMaxCount(maxCount);
    fetchRequest->setFetchHint(hint);

    int requestId = d->m_lastRequestId.fetchAndAddOrdered(1);
    d->m_requestIdHash.insert(fetchRequest, requestId);
    if (fetchRequest->start()) {
        return requestId;
    } else {
        d->m_requestIdHash.remove(fetchRequest);
        return -1;
    }
}

/*!
    \qmlmethod int OrganizerModel::fetchItems(stringlist itemIds)

    Starts a request to fetch items by the given \a itemIds, and returns the unique ID of this request.
    -1 is returned if the request can't be started.

    Note that the items fetched won't be added to the model, but can be accessed through the onItemsFetched
    handler.

    \sa onItemsFetched
  */
int QDeclarativeOrganizerModel::fetchItems(const QStringList &itemIds)
{
    Q_D(QDeclarativeOrganizerModel);
    if (itemIds.isEmpty())
        return -1;

    QOrganizerItemFetchByIdRequest *fetchRequest = new QOrganizerItemFetchByIdRequest(this);
    connect(fetchRequest, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)),
            this, SLOT(onFetchItemsRequestStateChanged(QOrganizerAbstractRequest::State)));
    fetchRequest->setManager(d->m_manager);

    QList<QOrganizerItemId> ids;
    foreach (const QString &itemId, itemIds)
        ids.append(QOrganizerItemId::fromString(itemId));
    fetchRequest->setIds(ids);
    int requestId = d->m_lastRequestId.fetchAndAddOrdered(1);
    d->m_requestIdHash.insert(fetchRequest, requestId);
    if (fetchRequest->start()) {
        return requestId;
    } else {
        d->m_requestIdHash.remove(fetchRequest);
        return -1;
    }
}

/*!
    \internal
 */
void QDeclarativeOrganizerModel::onFetchItemsRequestStateChanged(QOrganizerAbstractRequest::State state)
{
    Q_D(QDeclarativeOrganizerModel);

    QOrganizerAbstractRequest *request=qobject_cast<QOrganizerAbstractRequest *>(sender());

    if (state != QOrganizerAbstractRequest::FinishedState || !request)
        return;

    QOrganizerItemFetchRequest *itemFetchRequest = qobject_cast<QOrganizerItemFetchRequest *>(request);
    QOrganizerItemFetchByIdRequest *itemByIdFetchRequest = qobject_cast<QOrganizerItemFetchByIdRequest *>(request);

    if (!itemFetchRequest && !itemByIdFetchRequest)
        return;

    checkError(request);
    const int requestId = d->m_requestIdHash.value(request, -1);
    if (requestId == -1)
        qWarning() << Q_FUNC_INFO << "transaction not found from the request hash";
    else
        d->m_requestIdHash.remove(request);

    QVariantList list;
    if (request->error() == QOrganizerManager::NoError) {
            const QList<QOrganizerItem> &items((!itemFetchRequest) ? itemByIdFetchRequest->items():itemFetchRequest->items());
        QDeclarativeOrganizerItem *declarativeItem(0);
        foreach (const QOrganizerItem &item, items) {
            switch (item.type()) {
            case QOrganizerItemType::TypeEvent:
                declarativeItem = new QDeclarativeOrganizerEvent(this);
                break;
            case QOrganizerItemType::TypeEventOccurrence:
                declarativeItem = new QDeclarativeOrganizerEventOccurrence(this);
                break;
            case QOrganizerItemType::TypeTodo:
                declarativeItem = new QDeclarativeOrganizerTodo(this);
                break;
            case QOrganizerItemType::TypeTodoOccurrence:
                declarativeItem = new QDeclarativeOrganizerTodoOccurrence(this);
                break;
            default:
                declarativeItem = new QDeclarativeOrganizerItem(this);
                break;
            }
            declarativeItem->setItem(item);
            list.append(QVariant::fromValue((QObject *)declarativeItem));
        }
    }
    emit itemsFetched(requestId, list);

    request->deleteLater();
}

/*!
    \qmlmethod list<bool> OrganizerModel::containsItems(date start, date end, int interval)

    Returns a list of booleans telling if there is any item falling in the given time range.

    For example, if the \a start time is 2011-12-08 14:00:00, the \a end time is 2011-12-08 20:00:00,
    and the \a interval is 3600 (seconds), a list of size 6 is returned, telling if there is any item
    falling in the range of 14:00:00 to 15:00:00, 15:00:00 to 16:00:00, ..., 19:00:00 to 20:00:00.
 */
QList<bool> QDeclarativeOrganizerModel::containsItems(const QDateTime &start, const QDateTime &end, int interval)
{
    Q_D(QDeclarativeOrganizerModel);

    if (!(start.isValid() && end.isValid() && start < end && interval > 0))
        return QList<bool>();

    int i(0);
    int count = qCeil(start.secsTo(end) / static_cast<double>(interval));
    QVector<bool> occupiedTimeSlots(count, false);

    QVector<QDateTime> dateTime(count + 1);
    dateTime[0] = start;
    for (i = 1; i < count; ++i)
        dateTime[i] = dateTime.at(i - 1).addSecs(interval);
    dateTime[count] = end;

    QDateTime startTime;
    QDateTime endTime;
    bool itemStartFound;

    foreach (QDeclarativeOrganizerItem *item, d->m_items) {
        startTime = item->itemStartTime();
        endTime = item->itemEndTime();

        // check if item is occurring between start and end
        if (!((!startTime.isNull() && startTime >= start && startTime < end)
              || (!endTime.isNull() && endTime > start && endTime <= end)
              || (!startTime.isNull() && !endTime.isNull() && startTime <= start && endTime >= end)))
            continue;

        itemStartFound = (!startTime.isNull() &&  startTime <= start);
        for (i = 0; i < count; ++i) {

            if (!endTime.isNull() && endTime > dateTime.at(i) && endTime <= dateTime.at(i + 1)) {
                // item end time found, no need to check more time slots
                occupiedTimeSlots[i] = true;
                break;
            }
            if (occupiedTimeSlots.at(i))
                continue;

            if (itemStartFound) {
                occupiedTimeSlots[i] = true;
            } else if (!startTime.isNull() && startTime < dateTime.at(i + 1)) {
                if (startTime >= dateTime.at(i))
                    occupiedTimeSlots[i] = true;
                if (endTime.isNull())
                    break;
                itemStartFound = true;
            }
        }
    }
    return occupiedTimeSlots.toList();
}

/*!
  \qmlmethod bool OrganizerModel::containsItems(date start, date end)

  Returns true if there is at least one OrganizerItem between the given date range.
  Both the \a start and  \a end parameters are optional, if no \a end parameter, returns true
  if there are item(s) after \a start, if neither start nor end date time provided, returns true if
  items in the current model is not empty, otherwise return false.

  \sa itemIds()
  */
bool QDeclarativeOrganizerModel::containsItems(const QDateTime &start, const QDateTime &end)
{
    return !itemIds(start, end).isEmpty();
}

/*!
    \qmlmethod list<OrganizerItem> OrganizerModel::itemsByTimePeriod(date start, date end)

    Returns the list of organizer items between the given \a start and \a end period.
 */
QVariantList QDeclarativeOrganizerModel::itemsByTimePeriod(const QDateTime &start, const QDateTime &end)
{
    Q_D(QDeclarativeOrganizerModel);
    QVariantList list;

    if (start.isValid() && end.isValid()) {
        QDateTime startTime;
        QDateTime endTime;
        foreach (QDeclarativeOrganizerItem *item, d->m_items) {
            startTime = item->itemStartTime();
            endTime = item->itemEndTime();
            if ((startTime.isValid() && startTime <= start && endTime >= end)
                || (startTime >= start && startTime <= end)
                || (endTime >= start && endTime <= end)) {
                list.append(QVariant::fromValue((QObject *)item));
            }
        }
    } else if (start.isValid()) {
        foreach (QDeclarativeOrganizerItem *item, d->m_items) {
            if (item->itemEndTime() >= start)
                list.append(QVariant::fromValue((QObject *)item));
        }
    } else if (end.isValid()) {
        foreach (QDeclarativeOrganizerItem *item, d->m_items) {
            if (item->itemStartTime() <= end)
                list.append(QVariant::fromValue((QObject *)item));
        }
    } else {
        foreach (QDeclarativeOrganizerItem *item, d->m_items)
            list.append(QVariant::fromValue((QObject *)item));
    }

    return list;
}

/*!
    \qmlmethod OrganizerItem OrganizerModel::item(string itemId)

    Returns the OrganizerItem object with the given \a itemId.
 */
QDeclarativeOrganizerItem *QDeclarativeOrganizerModel::item(const QString &itemId)
{
    Q_D(QDeclarativeOrganizerModel);
    if (itemId.isEmpty())
        return 0;

    return d->m_itemIdHash.value(itemId, 0);
}

/*!
  \qmlmethod list<string> OrganizerModel::itemIds(date start, date end)

  Returns the list of organizer item ids between the given date range \a start and \a end,
  excluding generated occurrences. Both the \a start and \a end parameters are optional,
  if no \a end parameter, returns all item ids from \a start, if neither start nor end date
  time provided, returns all item ids in the current model.

  \sa containsItems()
  */
QStringList QDeclarativeOrganizerModel::itemIds(const QDateTime &start, const QDateTime &end)
{
    Q_D(QDeclarativeOrganizerModel);
    //TODO: quick search this
    QStringList ids;
    if (!end.isNull()) {
        // both start date and end date are valid
        foreach (QDeclarativeOrganizerItem* item, d->m_items) {
            if (item->generatedOccurrence())
                continue;
            if ( (item->itemStartTime() >= start && item->itemStartTime() <= end)
                 || (item->itemEndTime() >= start && item->itemEndTime() <= end)
                 || (item->itemEndTime() > end && item->itemStartTime() < start))
                ids << item->itemId();
        }
    } else if (!start.isNull()) {
        // only a valid start date is valid
        foreach (QDeclarativeOrganizerItem* item, d->m_items) {
            if (!item->generatedOccurrence() && item->itemStartTime() >= start)
                ids << item->itemId();
        }
    } else {
        // neither start nor end date is valid
        foreach (QDeclarativeOrganizerItem* item, d->m_items) {
            if (!item->generatedOccurrence())
                ids << item->itemId();
        }
    }
    return ids;
}

void QDeclarativeOrganizerModel::fetchAgain()
{
    Q_D(QDeclarativeOrganizerModel);
    cancelUpdate();

    d->m_fetchRequest  = new QOrganizerItemFetchRequest(this);
    d->m_fetchRequest->setManager(d->m_manager);
    d->m_fetchRequest->setSorting(d->m_sortOrders);
    d->m_fetchRequest->setStartDate(d->m_startPeriod);
    d->m_fetchRequest->setEndDate(d->m_endPeriod);

    if (d->m_filter){
        d->m_fetchRequest->setFilter(d->m_filter->filter());
    } else {
        d->m_fetchRequest->setFilter(QOrganizerItemFilter());
    }
    d->m_fetchRequest->setFetchHint(d->m_fetchHint ? d->m_fetchHint->fetchHint() : QOrganizerItemFetchHint());

    connect(d->m_fetchRequest, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(requestUpdated()));
    d->m_fetchRequest->start();
}

/*
  This slot function is connected with item fetch requests and item occurrence fetch requests,
  so the QObject::sender() must be checked for the right sender type.
  During update() function, the fetchAgain() will be invoked, inside fetchAgain(), a QOrganizerItemFetchRequest object
  is created and started, when this fetch request finished, this requestUpdate() slot will be invoked for the first time.
  Then check each of the organizer items returned by the item fetch request, if the item is a recurrence item,
  a QOrganizerItemOccurrenceFetchRequest object will be created and started. When each of these occurrence fetch requests
  finishes, this requestUpdated() slot will be invoked again and insert the returned occurrence items into the d->m_items
  list.
  */
void QDeclarativeOrganizerModel::requestUpdated()
{
    Q_D(QDeclarativeOrganizerModel);
    QList<QOrganizerItem> items;
    QOrganizerItemFetchRequest* ifr = qobject_cast<QOrganizerItemFetchRequest*>(QObject::sender());
    Q_ASSERT(ifr);
    if (ifr->isFinished()) {
        items = ifr->items();
        checkError(ifr);
        ifr->deleteLater();
        d->m_fetchRequest = 0;
        d->m_updatePendingFlag &= ~QDeclarativeOrganizerModelPrivate::UpdatingItemsPending;
    } else {
        return;
    }

    if (!items.isEmpty() || !d->m_items.isEmpty() || d->m_initialUpdate) {
        // full update: first go through new items and check if they
        // existed earlier. if they did, use the existing declarative wrapper.
        // otherwise create new declarative item.
        // for occurrences new declarative item is always created.
        QList<QDeclarativeOrganizerItem *> newList;
        QHash<QString, QDeclarativeOrganizerItem *> newItemIdHash;
        QHash<QString, QDeclarativeOrganizerItem *>::iterator iterator;
        QOrganizerItem item;
        QString idString;
        QDeclarativeOrganizerItem *declarativeItem;
        d->m_initialUpdate = false;

        int i;
        for (i = 0; i < items.size(); i++) {
            item = items[i];
            idString = item.id().toString();
            if (item.id().isNull()) {
                // this is occurrence
                declarativeItem = createItem(item);
            } else {
                iterator = d->m_itemIdHash.find(idString);
                if (iterator != d->m_itemIdHash.end()) {
                    declarativeItem = iterator.value();
                    declarativeItem->setItem(item);
                } else {
                    declarativeItem = createItem(item);
                }
                newItemIdHash.insert(idString, declarativeItem);
            }
            newList.append(declarativeItem);
        }

        // go through old items and delete items, which are not part of the
        // new item set. delete also all old occurrences.
        for (i = 0; i < d->m_items.size(); i++) {
            // FIXME: avoid unnecessary usage of item getter which copies all details...
            if (d->m_items[i]->item().id().isNull()) {
                d->m_items[i]->deleteLater();
            } else {
                iterator = newItemIdHash.find(d->m_items[i]->itemId());
                if (iterator == newItemIdHash.end())
                    d->m_items[i]->deleteLater();
            }
        }
        beginResetModel();
        d->m_items = newList;
        endResetModel();

        d->m_itemIdHash = newItemIdHash;
        d->m_modelChangedTimer.start();
    }
}

/*!
  \qmlmethod OrganizerModel::saveItem(OrganizerItem item)

  Saves asynchronously the given \a item into the organizer backend.
 */
void QDeclarativeOrganizerModel::saveItem(QDeclarativeOrganizerItem* di)
{
    Q_D(QDeclarativeOrganizerModel);
    if (di) {
        QOrganizerItem item = di->item();
        QOrganizerItemSaveRequest* req = new QOrganizerItemSaveRequest(this);
        req->setManager(d->m_manager);
        req->setItem(item);

        if (di->itemId().isEmpty()) {
            // if the item id is empty this means that this item is a new event
            // we need to keep trace of this declarative item to update with the
            // new Id as soon as this request finish
            QPointer<QDeclarativeOrganizerItem> pItem = di;
            req->setProperty(ITEM_TO_SAVE_PROPERTY, QVariant::fromValue(pItem));
        }

        connect(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(onRequestStateChanged(QOrganizerAbstractRequest::State)));

        req->start();
    }
}

/*!
  \qmlmethod OrganizerModel::removeItem(string itemId)
  Removes the organizer item with the given \a itemId from the backend.

  */
void QDeclarativeOrganizerModel::removeItem(const QString& id)
{
    QList<QString> ids;
    ids << id;
    removeItems(ids);
}

/*!
  \qmlmethod OrganizerModel::removeItem(OrganizerItem item)
  Removes the given organizer \a item from the backend.
  */
void QDeclarativeOrganizerModel::removeItem(QDeclarativeOrganizerItem *item)
{
    Q_D(QDeclarativeOrganizerModel);
    QOrganizerItemRemoveRequest* req = new QOrganizerItemRemoveRequest(this);
    req->setManager(d->m_manager);
    req->setItem(item->item());
    connect(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(onRequestStateChanged(QOrganizerAbstractRequest::State)));
    req->start();
}

/*!
  \qmlmethod OrganizerModel::removeItems(list<string> itemId)
  Removes asynchronously the organizer items with the given \a ids from the backend.

  */
void QDeclarativeOrganizerModel::removeItems(const QStringList& ids)
{
    Q_D(QDeclarativeOrganizerModel);
    QOrganizerItemRemoveByIdRequest* req = new QOrganizerItemRemoveByIdRequest(this);
    req->setManager(d->m_manager);
    QList<QOrganizerItemId> oids;

    // FIXME: no special format for occurrence ids
    foreach (const QString& id, ids) {
        if (id.startsWith(QString("qtorganizer:occurrence"))) {
            qmlWarning(this) << tr("Can't remove an occurrence item, please modify the parent item's recurrence rule instead!");
            continue;
        }
        QOrganizerItemId itemId = QOrganizerItemId::fromString(id);
        if (!itemId.isNull()) {
             oids.append(itemId);
        }
    }

    req->setItemIds(oids);

    connect(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(onRequestStateChanged(QOrganizerAbstractRequest::State)));

    req->start();
}

/*!
    \internal
 */
void QDeclarativeOrganizerModel::onRequestStateChanged(QOrganizerAbstractRequest::State newState)
{
    if (newState != QOrganizerAbstractRequest::FinishedState)
        return;

    QOrganizerAbstractRequest *request = qobject_cast<QOrganizerAbstractRequest *>(sender());
    Q_ASSERT(request);

    if (request->error() == QOrganizerManager::NoError &&
        request->type() == QOrganizerAbstractRequest::ItemSaveRequest) {
        QVariant vItem = request->property(ITEM_TO_SAVE_PROPERTY);
        if (vItem.isValid()) {
            QPointer<QDeclarativeOrganizerItem> pItem = vItem.value<QPointer<QDeclarativeOrganizerItem> >();
            // Fill declarative item id
            QOrganizerItemSaveRequest *sr = static_cast<QOrganizerItemSaveRequest *>(request);
            if (pItem && sr->items().length() == 1)
                pItem->setItem(sr->items()[0]);
        }
    }

    checkError(request);
    request->deleteLater();
}

void QDeclarativeOrganizerModel::removeItemsFromModel(const QList<QString> &itemIds)
{
    Q_D(QDeclarativeOrganizerModel);
    bool emitSignal = false;
    bool itemIdFound = false;

    foreach (const QString &itemId, itemIds) {
        itemIdFound = false;
        // generated occurrences are not in m_itemIdHash
        if (d->m_itemIdHash.remove(itemId) > 0)
            itemIdFound = true;
        for (int i = d->m_items.count() - 1; i >= 0; i--) {
            if (itemIdFound) {
                if (d->m_items.at(i)->itemId() == itemId) {
                    beginRemoveRows(QModelIndex(), i, i);
                    d->m_items.removeAt(i);
                    endRemoveRows();
                    emitSignal = true;
                    break;
                }
            } else if (d->m_items.at(i)->generatedOccurrence()) {
                QDeclarativeOrganizerItemDetail *parentDetail = d->m_items.at(i)->detail(QDeclarativeOrganizerItemDetail::Parent);
                if (parentDetail->value(QDeclarativeOrganizerItemParent::FieldParentId).toString() == itemId) {
                    beginRemoveRows(QModelIndex(), i, i);
                    d->m_items.removeAt(i);
                    endRemoveRows();
                    emitSignal = true;
                }
            }
        }
    }
    if (emitSignal)
        d->m_modelChangedTimer.start();
}


/*!
    \internal

    It's invoked upon the QOrganizerManager::itemsModified() signal.
 */
void QDeclarativeOrganizerModel::onItemsModified(const QList<QPair<QOrganizerItemId, QOrganizerManager::Operation> > &itemIds)
{
    Q_D(QDeclarativeOrganizerModel);
    if (!d->m_autoUpdate)
        return;

    QSet<QOrganizerItemId> addedAndChangedItems;
    QList<QString> removedItems;
    for (int i = itemIds.size() - 1; i >= 0; i--) {
        if (itemIds[i].second == QOrganizerManager::Remove) {
            // check that item has not been added after removing it
            if (!addedAndChangedItems.contains(itemIds[i].first))
                removedItems.append(itemIds[i].first.toString());
        } else {
            addedAndChangedItems.insert(itemIds[i].first);
        }
    }
    if (!removedItems.isEmpty())
        removeItemsFromModel(removedItems);

    if (!addedAndChangedItems.isEmpty()) {
        // FIXME; to be optimized with fetching only the modified items
        // from the storage locations modified items are on
        QOrganizerItemFetchRequest *fetchRequest = new QOrganizerItemFetchRequest(this);
        connect(fetchRequest, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)),
                this, SLOT(onItemsModifiedFetchRequestStateChanged(QOrganizerAbstractRequest::State)));
        fetchRequest->setManager(d->m_manager);
        fetchRequest->setStartDate(d->m_startPeriod);
        fetchRequest->setEndDate(d->m_endPeriod);
        fetchRequest->setFilter(d->m_filter ? d->m_filter->filter() : QOrganizerItemFilter());
        fetchRequest->setSorting(d->m_sortOrders);
        fetchRequest->setFetchHint(d->m_fetchHint ? d->m_fetchHint->fetchHint() : QOrganizerItemFetchHint());
        d->m_notifiedItems.insert(fetchRequest, addedAndChangedItems);

        fetchRequest->start();
    }
}

/*!
    \internal

    It's invoked by the fetch request from onItemsModified().
 */
void QDeclarativeOrganizerModel::onItemsModifiedFetchRequestStateChanged(QOrganizerAbstractRequest::State state)
{
    // NOTE: this function assumes the sorting algorithm gives always the same result with
    // same data. E.g. items which have the identical sorting key must be sorted too.

    Q_D(QDeclarativeOrganizerModel);
    if (state != QOrganizerAbstractRequest::FinishedState)
        return;

    QOrganizerItemFetchRequest *request = qobject_cast<QOrganizerItemFetchRequest *>(sender());
    Q_ASSERT(request);

    checkError(request);

    QSet<QOrganizerItemId> notifiedItems = d->m_notifiedItems.value(request);
    if (notifiedItems.isEmpty())
        return;

    if (request->error() == QOrganizerManager::NoError) {
        bool emitSignal = false;
        QList<QOrganizerItem> fetchedItems = request->items();
        QOrganizerItem oldItem;
        QOrganizerItem newItem;
        QOrganizerItemParent oldParentDetail;
        QOrganizerItemParent newParentDetail;
        QDeclarativeOrganizerItem *declarativeItem;
        QSet<QOrganizerItemId> removedIds;
        QSet<QOrganizerItemId> addedIds;
        int oldInd = 0;
        int newInd = 0;
        while (newInd < fetchedItems.size()) {
            bool addNewItem = false;
            bool oldItemExists = false;

            newItem = fetchedItems[newInd];
            if (oldInd < d->m_items.size()) {
                // quick check if items are same in old and new event lists
                // FIXME: avoid unnecessary usage of item getter which copies all details
                oldItem = d->m_items[oldInd]->item();
                oldItemExists = true;
                if (!newItem.id().isNull() && !oldItem.id().isNull() && newItem.id() == oldItem.id()) {
                    if (notifiedItems.contains(newItem.id())) {
                        d->m_items[oldInd]->setItem(newItem);
                        const QModelIndex idx = index(oldInd, 0);
                        emit dataChanged(idx, idx);
                        emitSignal = true;
                    }
                    newInd++;
                    oldInd++;
                    continue;
                }
            }

            // check should we remove old item
            if (oldItemExists) {
                if (oldItem.id().isNull()) {
                    // this is generated occurrence
                    oldParentDetail = oldItem.detail(QOrganizerItemDetail::TypeParent);
                    if (notifiedItems.contains(oldParentDetail.parentId())) {
                        beginRemoveRows(QModelIndex(), oldInd, oldInd);
                        d->m_items.takeAt(oldInd)->deleteLater();
                        endRemoveRows();
                        emitSignal = true;
                        continue;
                    }
                } else if (notifiedItems.contains(oldItem.id())) {
                    // if notifiedItems contains the oldItem id, it means the item has been
                    // changed and we should reuse the declarative part and only remove
                    // rows from abstract list model
                    // it might also mean that oldItem has been changed so that it does not belong to
                    // the model anymore (e.g. changing fron normal item to recurring item)
                    beginRemoveRows(QModelIndex(), oldInd, oldInd);
                    d->m_items.removeAt(oldInd);
                    endRemoveRows();
                    removedIds.insert(oldItem.id());
                    emitSignal = true;
                    continue;
                } else if (notifiedItems.contains(newItem.id())) {
                    // if newItem is a notified item and does not correspond to an oldItem
                    // then find a correspondent item by id in the old items list
                    // and remove it from the hash and list
                    for (int removeInd = oldInd + 1; removeInd < d->m_items.size(); ++removeInd) {
                        if (newItem.id() == d->m_items[removeInd]->item().id()) {
                            beginRemoveRows(QModelIndex(), removeInd, removeInd);
                            d->m_itemIdHash.remove(d->m_items[removeInd]->itemId());
                            d->m_items.takeAt(removeInd)->deleteLater();
                            endRemoveRows();
                            emitSignal = true;
                            break;
                        }
                    }
                }
            }
            // check should we add the new item
            if (newItem.id().isNull() && (newItem.type() == QOrganizerItemType::TypeEventOccurrence || newItem.type() == QOrganizerItemType::TypeTodoOccurrence)) {
                // this is occurrence (generated or exception)
                newParentDetail = newItem.detail(QOrganizerItemDetail::TypeParent);
                if (notifiedItems.contains(newParentDetail.parentId())) {
                    declarativeItem = createItem(newItem);
                    addNewItem = true;
                }
            } else if (notifiedItems.contains(newItem.id())) {
                QHash<QString, QDeclarativeOrganizerItem *>::const_iterator iterator = d->m_itemIdHash.find(newItem.id().toString());
                if (iterator == d->m_itemIdHash.end()) {
                    declarativeItem = createItem(newItem);
                    d->m_itemIdHash.insert(declarativeItem->itemId(), declarativeItem);
                } else {
                    declarativeItem = d->m_itemIdHash.value(newItem.id().toString());
                    addedIds.insert(newItem.id());
                }
                addNewItem = true;
            }

            if (addNewItem) {
                beginInsertRows(QModelIndex(), oldInd, oldInd);
                d->m_items.insert(oldInd, declarativeItem);
                endInsertRows();
                emitSignal = true;
            }
            oldInd++;
            newInd++;
        }
        // remove the rest of the old items
        if (oldInd <= d->m_items.size() - 1) {
            beginRemoveRows(QModelIndex(), oldInd, d->m_items.size() - 1);
            while (oldInd < d->m_items.size()) {
                d->m_itemIdHash.remove(d->m_items[oldInd]->itemId());
                d->m_items.takeAt(oldInd)->deleteLater();
                emitSignal = true;
                oldInd++;
            }
            endRemoveRows();
        }
        // remove items which were changed so that they are no longer part of the model
        // they have been removed from the model earlier, but need to still be removed from the hash
        // and deleted

        removedIds.subtract(addedIds);
        foreach (const QOrganizerItemId &id, removedIds) {
            QDeclarativeOrganizerItem *changedItem = d->m_itemIdHash.take(id.toString());
            if (changedItem) {
                changedItem->deleteLater();
                emitSignal = true;
            }
        }

        if (emitSignal)
            d->m_modelChangedTimer.start();
    }
    d->m_notifiedItems.remove(request);
    request->deleteLater();
}

/*!
  \qmlmethod OrganizerModel::fetchCollections()
  Fetch asynchronously a list of organizer collections from the organizer backend.
  */
void QDeclarativeOrganizerModel::fetchCollections()
{
    Q_D(QDeclarativeOrganizerModel);
    // fetchCollections() is used for both direct calls and
    // signals from model. For signal from model, check also the
    // autoupdate-flag.
    if (qobject_cast<QTimer*>(sender()) == &d->m_fetchCollectionsTimer) {
        if (!d->m_fetchCollectionsTimer.property(MANUALLY_TRIGGERED_PROPERTY).toBool() && !d->m_autoUpdate)
            return; // it was automatically triggered, but autoUpdate is false, so don't update the model data.
        // reset the state of the manually triggered properly, for next time.
        d->m_fetchCollectionsTimer.setProperty(MANUALLY_TRIGGERED_PROPERTY, QVariant::fromValue<bool>(false));
    }

    QOrganizerCollectionFetchRequest* req = new QOrganizerCollectionFetchRequest(this);
    req->setManager(d->m_manager);

    connect(req,SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(collectionsFetched()));

    req->start();
}

void QDeclarativeOrganizerModel::collectionsFetched()
{
    Q_D(QDeclarativeOrganizerModel);
    QOrganizerCollectionFetchRequest* req = qobject_cast<QOrganizerCollectionFetchRequest*>(QObject::sender());
    Q_ASSERT(req);
    if (req->isFinished() && QOrganizerManager::NoError == req->error()) {
        d->m_updatePendingFlag &= ~QDeclarativeOrganizerModelPrivate::UpdatingCollectionsPending;
        // prepare tables
        QHash<QString, const QOrganizerCollection*> collections;
        foreach (const QOrganizerCollection& collection, req->collections()) {
            collections.insert(collection.id().toString(), &collection);
        }
        QHash<QString, QDeclarativeOrganizerCollection*> declCollections;
        foreach(QDeclarativeOrganizerCollection* declCollection, d->m_collections) {
            declCollections.insert(declCollection->collection().id().toString(), declCollection);
        }
        // go tables through
        for (auto it = collections.cbegin(), end = collections.cend(); it != end; ++it) {
            if (declCollections.contains(it.key())) {
                // collection on both sides, update the declarative collection
                declCollections.value(it.key())->setCollection(*it.value());
            } else {
                // new collection, add it to declarative collection list
                QDeclarativeOrganizerCollection* declCollection = new QDeclarativeOrganizerCollection(this);
                declCollection->setCollection(*it.value());
                d->m_collections.append(declCollection);
            }
        }
        for (auto it = declCollections.cbegin(), end = declCollections.cend(); it != end; ++it) {
            if (!collections.contains(it.key())) {
                // collection deleted on the backend side, delete from declarative collection list
                QDeclarativeOrganizerCollection* toBeDeletedColl = it.value();
                d->m_collections.removeOne(toBeDeletedColl);
                toBeDeletedColl->deleteLater();
            }
        }
        emit collectionsChanged();
        if (d->m_updatePendingFlag & QDeclarativeOrganizerModelPrivate::UpdatingItemsPending)
            QMetaObject::invokeMethod(this, "fetchAgain", Qt::QueuedConnection);
        req->deleteLater();
    }
    checkError(req);
}

/*!
  \qmlmethod OrganizerModel::saveCollection(Collection collection)

  Saves asynchronously the given \a collection into the organizer backend.
 */
void QDeclarativeOrganizerModel::saveCollection(QDeclarativeOrganizerCollection* declColl)
{
    Q_D(QDeclarativeOrganizerModel);
    if (declColl) {
        QOrganizerCollection collection = declColl->collection();
        QOrganizerCollectionSaveRequest* req = new QOrganizerCollectionSaveRequest(this);
        req->setManager(d->m_manager);
        req->setCollection(collection);

        connect(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(onRequestStateChanged(QOrganizerAbstractRequest::State)));

        req->start();
    }
}

/*!
  \qmlmethod OrganizerModel::removeCollection(string collectionId)
  Removes asynchronously the organizer collection with the given \a collectionId from the backend.
  */
void QDeclarativeOrganizerModel::removeCollection(const QString &collectionId)
{
    Q_D(QDeclarativeOrganizerModel);
    QOrganizerCollectionRemoveRequest* req = new QOrganizerCollectionRemoveRequest(this);
    req->setManager(d->m_manager);
    req->setCollectionId(QOrganizerCollectionId::fromString(collectionId));

    connect(req, SIGNAL(stateChanged(QOrganizerAbstractRequest::State)), this, SLOT(onRequestStateChanged(QOrganizerAbstractRequest::State)));

    req->start();
}

/*!
  \qmlmethod string OrganizerModel::defaultCollectionId()
  Returns the id of a default Collection object.
  */
QString QDeclarativeOrganizerModel::defaultCollectionId() const
{
    Q_D(const QDeclarativeOrganizerModel);
    return d->m_manager->defaultCollectionId().toString();
}

/*!
  \qmlmethod Collection OrganizerModel::collection(string collectionId)
  Returns the Collection object which collection id is the given \a collectionId and
  null if collection id is not found.
  */
QDeclarativeOrganizerCollection* QDeclarativeOrganizerModel::collection(const QString &collectionId)
{
    Q_D(QDeclarativeOrganizerModel);
    foreach (QDeclarativeOrganizerCollection* collection, d->m_collections) {
        if (collection->id() == collectionId)
            return collection;
    }
    return 0;
}

QVariant QDeclarativeOrganizerModel::data(const QModelIndex &index, int role) const
{
    Q_D(const QDeclarativeOrganizerModel);
    //Check if QList itme's index is valid before access it, index should be between 0 and count - 1
    if (index.row() < 0 || index.row() >= d->m_items.count()) {
        return QVariant();
    }

    QDeclarativeOrganizerItem* di = d->m_items.at(index.row());
    Q_ASSERT(di);
    QOrganizerItem item = di->item();
    switch(role) {
        case Qt::DisplayRole:
            return item.displayLabel();
        case Qt::DecorationRole:
            //return pixmap for this item type
        case OrganizerItemRole:
            return QVariant::fromValue(di);
    }
    return QVariant();
}

/*!
  \qmlproperty list<OrganizerItem> OrganizerModel::items

  This property holds a list of organizer items in the organizer model.

  \sa OrganizerItem
  */
QQmlListProperty<QDeclarativeOrganizerItem> QDeclarativeOrganizerModel::items()
{
    return { this, nullptr, item_count, item_at };
}

/*!
  \qmlproperty list<Collection> OrganizerModel::collections

  This property holds a list of collections in the organizer model.

  \sa Collection
  */
QQmlListProperty<QDeclarativeOrganizerCollection> QDeclarativeOrganizerModel::collections()
{
    return { this, nullptr, collection_count, collection_at };
}

qsizetype QDeclarativeOrganizerModel::item_count(QQmlListProperty<QDeclarativeOrganizerItem> *p)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    if (model)
        return model->d_ptr->m_items.count();
    return 0;
}

QDeclarativeOrganizerItem * QDeclarativeOrganizerModel::item_at(QQmlListProperty<QDeclarativeOrganizerItem> *p, qsizetype idx)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    if (model && idx >= 0 && idx < model->d_ptr->m_items.size())
        return model->d_ptr->m_items.at(idx);
    return 0;
}

void QDeclarativeOrganizerModel::sortOrder_append(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p, QDeclarativeOrganizerItemSortOrder *sortOrder)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    if (model && sortOrder) {
        QObject::connect(sortOrder, SIGNAL(sortOrderChanged()), model, SIGNAL(sortOrdersChanged()));
        model->d_ptr->m_declarativeSortOrders.append(sortOrder);
        model->d_ptr->m_sortOrders.append(sortOrder->sortOrder());
        emit model->sortOrdersChanged();
    }
}

qsizetype QDeclarativeOrganizerModel::sortOrder_count(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    if (model)
        return model->d_ptr->m_declarativeSortOrders.size();
    return 0;
}

QDeclarativeOrganizerItemSortOrder * QDeclarativeOrganizerModel::sortOrder_at(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p, qsizetype idx)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);

    QDeclarativeOrganizerItemSortOrder* sortOrder = 0;
    if (model) {
        qsizetype i = 0;
        foreach (QDeclarativeOrganizerItemSortOrder* s, model->d_ptr->m_declarativeSortOrders) {
            if (i == idx) {
                sortOrder = s;
                break;
            } else {
                i++;
            }
        }
    }
    return sortOrder;
}

void  QDeclarativeOrganizerModel::sortOrder_clear(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);

    if (model) {
        model->d_ptr->m_sortOrders.clear();
        model->d_ptr->m_declarativeSortOrders.clear();
        emit model->sortOrdersChanged();
    }
}

qsizetype QDeclarativeOrganizerModel::collection_count(QQmlListProperty<QDeclarativeOrganizerCollection> *p)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    return model ? model->d_ptr->m_collections.count() : 0;
}

QDeclarativeOrganizerCollection* QDeclarativeOrganizerModel::collection_at(QQmlListProperty<QDeclarativeOrganizerCollection> *p, qsizetype idx)
{
    QDeclarativeOrganizerModel* model = qobject_cast<QDeclarativeOrganizerModel*>(p->object);
    QDeclarativeOrganizerCollection* collection = 0;
    if (model) {
        if (!model->d_ptr->m_collections.isEmpty() && idx >= 0 && idx < model->d_ptr->m_collections.count())
            collection = model->d_ptr->m_collections.at(idx);
    }
    return collection;
}

QT_END_NAMESPACE

#include "moc_qdeclarativeorganizermodel_p.cpp"