| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtOrganizer module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 2.1 or version 3 as published by the Free |
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
| 22 | ** following information to ensure the GNU Lesser General Public License |
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 25 | ** |
| 26 | ** As a special exception, The Qt Company gives you certain additional |
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception |
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 29 | ** |
| 30 | ** $QT_END_LICENSE$ |
| 31 | ** |
| 32 | ****************************************************************************/ |
| 33 | |
| 34 | #ifndef QDECLARATIVEORGANIZERMODEL_H |
| 35 | #define QDECLARATIVEORGANIZERMODEL_H |
| 36 | |
| 37 | #include <QtCore/qabstractitemmodel.h> |
| 38 | |
| 39 | #include <QtQml/qqml.h> |
| 40 | |
| 41 | #include <QtOrganizer/qorganizerabstractrequest.h> |
| 42 | #include <QtOrganizer/qorganizercollectionid.h> |
| 43 | #include <QtOrganizer/qorganizeritem.h> |
| 44 | |
| 45 | #include <QtVersit/qversitreader.h> |
| 46 | #include <QtVersit/qversitwriter.h> |
| 47 | |
| 48 | #include "qdeclarativeorganizercollection_p.h" |
| 49 | #include "qdeclarativeorganizeritem_p.h" |
| 50 | #include "qdeclarativeorganizeritemfetchhint_p.h" |
| 51 | #include "qdeclarativeorganizeritemfilter_p.h" |
| 52 | #include "qdeclarativeorganizeritemsortorder_p.h" |
| 53 | |
| 54 | QTORGANIZER_USE_NAMESPACE |
| 55 | QTVERSIT_USE_NAMESPACE |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | class QDeclarativeOrganizerModelPrivate; |
| 60 | class QDeclarativeOrganizerModel : public QAbstractListModel, public QQmlParserStatus |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | Q_PROPERTY(QString manager READ manager WRITE setManager NOTIFY managerChanged) |
| 64 | Q_PROPERTY(QString managerName READ managerName NOTIFY managerChanged) |
| 65 | Q_PROPERTY(QStringList availableManagers READ availableManagers) |
| 66 | Q_PROPERTY(bool autoUpdate READ autoUpdate WRITE setAutoUpdate NOTIFY autoUpdateChanged) |
| 67 | Q_PROPERTY(QDateTime startPeriod READ startPeriod WRITE setStartPeriod NOTIFY startPeriodChanged) |
| 68 | Q_PROPERTY(QDateTime endPeriod READ endPeriod WRITE setEndPeriod NOTIFY endPeriodChanged) |
| 69 | Q_PROPERTY(QDeclarativeOrganizerItemFilter* filter READ filter WRITE setFilter NOTIFY filterChanged) |
| 70 | Q_PROPERTY(QDeclarativeOrganizerItemFetchHint* fetchHint READ fetchHint WRITE setFetchHint NOTIFY fetchHintChanged) |
| 71 | Q_PROPERTY(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> sortOrders READ sortOrders NOTIFY sortOrdersChanged) |
| 72 | Q_PROPERTY(QQmlListProperty<QDeclarativeOrganizerItem> items READ items NOTIFY modelChanged) |
| 73 | Q_PROPERTY(QQmlListProperty<QDeclarativeOrganizerCollection> collections READ collections NOTIFY collectionsChanged) |
| 74 | Q_PROPERTY(QString error READ error NOTIFY errorChanged) |
| 75 | Q_PROPERTY(int itemCount READ itemCount NOTIFY modelChanged) |
| 76 | Q_ENUMS(ExportError) |
| 77 | Q_ENUMS(ImportError) |
| 78 | Q_INTERFACES(QQmlParserStatus) |
| 79 | public: |
| 80 | enum { |
| 81 | OrganizerItemRole = Qt::UserRole + 500 |
| 82 | }; |
| 83 | |
| 84 | enum ExportError { |
| 85 | ExportNoError = QVersitWriter::NoError, |
| 86 | ExportUnspecifiedError = QVersitWriter::UnspecifiedError, |
| 87 | ExportIOError = QVersitWriter::IOError, |
| 88 | ExportOutOfMemoryError = QVersitWriter::OutOfMemoryError, |
| 89 | ExportNotReadyError = QVersitWriter::NotReadyError |
| 90 | }; |
| 91 | |
| 92 | enum ImportError { |
| 93 | ImportNoError = QVersitReader::NoError, |
| 94 | ImportUnspecifiedError = QVersitReader::UnspecifiedError, |
| 95 | ImportIOError = QVersitReader::IOError, |
| 96 | ImportOutOfMemoryError = QVersitReader::OutOfMemoryError, |
| 97 | ImportNotReadyError = QVersitReader::NotReadyError, |
| 98 | ImportParseError = QVersitReader::ParseError |
| 99 | }; |
| 100 | |
| 101 | explicit QDeclarativeOrganizerModel(QObject *parent = Q_NULLPTR); |
| 102 | explicit QDeclarativeOrganizerModel(QOrganizerManager* manager, const QDateTime& start, const QDateTime& end, QObject *parent = Q_NULLPTR); |
| 103 | ~QDeclarativeOrganizerModel(); |
| 104 | |
| 105 | QString error() const; |
| 106 | int itemCount() const; |
| 107 | |
| 108 | QString manager() const; |
| 109 | void setManager(const QString& managerUri); |
| 110 | QString managerName() const; |
| 111 | QStringList availableManagers() const; |
| 112 | QDateTime startPeriod() const; |
| 113 | void setStartPeriod(const QDateTime& start); |
| 114 | |
| 115 | QDateTime endPeriod() const; |
| 116 | void setEndPeriod(const QDateTime& end); |
| 117 | |
| 118 | // From QQmlParserStatus |
| 119 | virtual void classBegin() {} |
| 120 | virtual void componentComplete(); |
| 121 | |
| 122 | int rowCount(const QModelIndex &parent) const; |
| 123 | QVariant data(const QModelIndex &index, int role) const; |
| 124 | |
| 125 | QDeclarativeOrganizerItemFilter* filter() const; |
| 126 | |
| 127 | QDeclarativeOrganizerItemFetchHint* fetchHint() const; |
| 128 | |
| 129 | |
| 130 | QQmlListProperty<QDeclarativeOrganizerItem> items() ; |
| 131 | QQmlListProperty<QDeclarativeOrganizerItemSortOrder> sortOrders() ; |
| 132 | QQmlListProperty<QDeclarativeOrganizerCollection> collections(); |
| 133 | |
| 134 | Q_INVOKABLE void removeItem(const QString& id); |
| 135 | Q_INVOKABLE void removeItem(QDeclarativeOrganizerItem *item); |
| 136 | Q_INVOKABLE void removeItems(const QStringList& ids); |
| 137 | Q_INVOKABLE void removeItems(const QList<QDeclarativeOrganizerItem> &items); |
| 138 | Q_INVOKABLE void saveItem(QDeclarativeOrganizerItem* item); |
| 139 | Q_INVOKABLE int fetchItems(const QStringList &itemIds); |
| 140 | Q_INVOKABLE int fetchItems(const QDateTime &start, const QDateTime &end, |
| 141 | QDeclarativeOrganizerItemFilter *filter = new QDeclarativeOrganizerItemFilter(), |
| 142 | int maxCount = -1, |
| 143 | const QVariantList &sortOrders = QVariantList(), |
| 144 | QDeclarativeOrganizerItemFetchHint *fetchHint = new QDeclarativeOrganizerItemFetchHint()); |
| 145 | Q_INVOKABLE void removeCollection(const QString& collectionId); |
| 146 | Q_INVOKABLE void saveCollection(QDeclarativeOrganizerCollection* collection); |
| 147 | // FIXME : Naming indicates fetch from database |
| 148 | Q_INVOKABLE void fetchCollections(); |
| 149 | |
| 150 | Q_INVOKABLE QList<bool> containsItems(const QDateTime &start, const QDateTime &end, int interval); |
| 151 | Q_INVOKABLE bool containsItems(const QDateTime &start, const QDateTime &end = QDateTime()); |
| 152 | Q_INVOKABLE QVariantList itemsByTimePeriod(const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()); |
| 153 | Q_INVOKABLE QDeclarativeOrganizerItem* item(const QString& id); |
| 154 | Q_INVOKABLE QStringList itemIds(const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()); |
| 155 | Q_INVOKABLE QString defaultCollectionId() const; |
| 156 | Q_INVOKABLE QDeclarativeOrganizerCollection* collection(const QString& collectionId); |
| 157 | |
| 158 | Q_INVOKABLE void importItems(const QUrl& url, const QStringList& profiles = QStringList()); |
| 159 | Q_INVOKABLE void exportItems(const QUrl& url, const QStringList& profiles = QStringList()); |
| 160 | |
| 161 | bool autoUpdate() const; |
| 162 | void setAutoUpdate(bool autoUpdate); |
| 163 | |
| 164 | void setFilter(QDeclarativeOrganizerItemFilter* filter); |
| 165 | void setFetchHint(QDeclarativeOrganizerItemFetchHint* fetchHint); |
| 166 | |
| 167 | |
| 168 | signals: |
| 169 | void managerChanged(); |
| 170 | void availableManagersChanged(); |
| 171 | void filterChanged(); |
| 172 | void fetchHintChanged(); |
| 173 | void modelChanged(); |
| 174 | void sortOrdersChanged(); |
| 175 | void errorChanged(); |
| 176 | void startPeriodChanged(); |
| 177 | void endPeriodChanged(); |
| 178 | void autoUpdateChanged(); |
| 179 | void collectionsChanged(); |
| 180 | void itemsFetched(int requestId, const QVariantList &fetchedItems); |
| 181 | void exportCompleted(ExportError error, QUrl url); |
| 182 | void importCompleted(ImportError error, QUrl url, const QStringList &ids); |
| 183 | |
| 184 | public slots: |
| 185 | void update(); |
| 186 | void updateItems(); |
| 187 | void updateCollections(); |
| 188 | void cancelUpdate(); |
| 189 | |
| 190 | private slots: |
| 191 | void doUpdate(); |
| 192 | void doUpdateItems(); |
| 193 | void fetchAgain(); |
| 194 | void requestUpdated(); |
| 195 | |
| 196 | // handle request from saveItem(), removeItem(), saveCollection(), and removeCollection() |
| 197 | void onRequestStateChanged(QOrganizerAbstractRequest::State newState); |
| 198 | |
| 199 | // handle fetch request from fetchItems() |
| 200 | void onFetchItemsRequestStateChanged(QOrganizerAbstractRequest::State state); |
| 201 | |
| 202 | // handle signals from organizer manager |
| 203 | void onItemsModified(const QList<QPair<QOrganizerItemId, QOrganizerManager::Operation> > &itemIds); |
| 204 | |
| 205 | // handle fetch request from onItemsModified() |
| 206 | void onItemsModifiedFetchRequestStateChanged(QOrganizerAbstractRequest::State state); |
| 207 | |
| 208 | void collectionsFetched(); |
| 209 | |
| 210 | void startImport(QVersitReader::State state); |
| 211 | void itemsExported(QVersitWriter::State state); |
| 212 | |
| 213 | |
| 214 | private: |
| 215 | void removeItemsFromModel(const QList<QString>& ids); |
| 216 | bool itemHasRecurrence(const QOrganizerItem& oi) const; |
| 217 | QDeclarativeOrganizerItem* createItem(const QOrganizerItem& item); |
| 218 | void checkError(const QOrganizerAbstractRequest *request); |
| 219 | |
| 220 | static int item_count(QQmlListProperty<QDeclarativeOrganizerItem> *p); |
| 221 | static QDeclarativeOrganizerItem * item_at(QQmlListProperty<QDeclarativeOrganizerItem> *p, int idx); |
| 222 | |
| 223 | static void sortOrder_append(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p, QDeclarativeOrganizerItemSortOrder *sortOrder); |
| 224 | static int sortOrder_count(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p); |
| 225 | static QDeclarativeOrganizerItemSortOrder * sortOrder_at(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p, int idx); |
| 226 | static void sortOrder_clear(QQmlListProperty<QDeclarativeOrganizerItemSortOrder> *p); |
| 227 | |
| 228 | static int collection_count(QQmlListProperty<QDeclarativeOrganizerCollection> *p); |
| 229 | static QDeclarativeOrganizerCollection* collection_at(QQmlListProperty<QDeclarativeOrganizerCollection> *p, int idx); |
| 230 | |
| 231 | QScopedPointer<QDeclarativeOrganizerModelPrivate> d_ptr; |
| 232 | Q_DECLARE_PRIVATE(QDeclarativeOrganizerModel) |
| 233 | }; |
| 234 | |
| 235 | QT_END_NAMESPACE |
| 236 | |
| 237 | QML_DECLARE_TYPE(QDeclarativeOrganizerModel) |
| 238 | |
| 239 | #endif // QDECLARATIVEORGANIZERMODEL_H |
| 240 | |