| 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 QtQml 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 QDECLARATIVECONTACTMODEL_P_H |
| 35 | #define QDECLARATIVECONTACTMODEL_P_H |
| 36 | |
| 37 | #include <QtCore/qabstractitemmodel.h> |
| 38 | |
| 39 | #include <QtQml/qqml.h> |
| 40 | |
| 41 | #include <QtContacts/qcontact.h> |
| 42 | #include <QtContacts/qcontactrequests.h> |
| 43 | |
| 44 | #include <QtVersit/qversitreader.h> |
| 45 | #include <QtVersit/qversitwriter.h> |
| 46 | |
| 47 | #include "qdeclarativecontact_p.h" |
| 48 | #include "qdeclarativecontactcollection_p.h" |
| 49 | #include "qdeclarativecontactfetchhint_p.h" |
| 50 | #include "qdeclarativecontactfilter_p.h" |
| 51 | #include "qdeclarativecontactsortorder_p.h" |
| 52 | |
| 53 | QTCONTACTS_USE_NAMESPACE |
| 54 | QTVERSIT_USE_NAMESPACE |
| 55 | |
| 56 | QT_BEGIN_NAMESPACE |
| 57 | |
| 58 | class QDeclarativeContactModelPrivate; |
| 59 | class QDeclarativeContactModel : public QAbstractListModel, public QQmlParserStatus |
| 60 | { |
| 61 | Q_OBJECT |
| 62 | Q_PROPERTY(QString manager READ manager WRITE setManager NOTIFY managerChanged) |
| 63 | Q_PROPERTY(QStringList availableManagers READ availableManagers) |
| 64 | Q_PROPERTY(QString error READ error NOTIFY errorChanged) |
| 65 | Q_PROPERTY(bool autoUpdate READ autoUpdate WRITE setAutoUpdate NOTIFY autoUpdateChanged) |
| 66 | Q_PROPERTY(QDeclarativeContactFilter* filter READ filter WRITE setFilter NOTIFY filterChanged) |
| 67 | Q_PROPERTY(QDeclarativeContactFetchHint* fetchHint READ fetchHint WRITE setFetchHint NOTIFY fetchHintChanged) |
| 68 | Q_PROPERTY(QQmlListProperty<QDeclarativeContact> contacts READ contacts NOTIFY contactsChanged) |
| 69 | Q_PROPERTY(QQmlListProperty<QDeclarativeContactCollection> collections READ collections NOTIFY collectionsChanged) |
| 70 | Q_PROPERTY(QQmlListProperty<QDeclarativeContactSortOrder> sortOrders READ sortOrders NOTIFY sortOrdersChanged) |
| 71 | Q_ENUMS(ExportError) |
| 72 | Q_ENUMS(ImportError) |
| 73 | Q_INTERFACES(QQmlParserStatus) |
| 74 | |
| 75 | public: |
| 76 | explicit QDeclarativeContactModel(QObject *parent = Q_NULLPTR); |
| 77 | ~QDeclarativeContactModel(); |
| 78 | |
| 79 | enum { |
| 80 | ContactRole = Qt::UserRole + 500 |
| 81 | }; |
| 82 | |
| 83 | enum ExportError { |
| 84 | ExportNoError = QVersitWriter::NoError, |
| 85 | ExportUnspecifiedError = QVersitWriter::UnspecifiedError, |
| 86 | ExportIOError = QVersitWriter::IOError, |
| 87 | ExportOutOfMemoryError = QVersitWriter::OutOfMemoryError, |
| 88 | ExportNotReadyError = QVersitWriter::NotReadyError |
| 89 | }; |
| 90 | |
| 91 | enum ImportError { |
| 92 | ImportNoError = QVersitReader::NoError, |
| 93 | ImportUnspecifiedError = QVersitReader::UnspecifiedError, |
| 94 | ImportIOError = QVersitReader::IOError, |
| 95 | ImportOutOfMemoryError = QVersitReader::OutOfMemoryError, |
| 96 | ImportNotReadyError = QVersitReader::NotReadyError, |
| 97 | ImportParseError = QVersitReader::ParseError |
| 98 | }; |
| 99 | |
| 100 | QString manager() const; |
| 101 | void setManager(const QString& manager); |
| 102 | |
| 103 | QStringList availableManagers() const; |
| 104 | |
| 105 | QString error() const; |
| 106 | |
| 107 | QDeclarativeContactFilter* filter() const; |
| 108 | void setFilter(QDeclarativeContactFilter* filter); |
| 109 | |
| 110 | QDeclarativeContactFetchHint* fetchHint() const; |
| 111 | void setFetchHint(QDeclarativeContactFetchHint* fetchHint); |
| 112 | |
| 113 | // From QQmlParserStatus |
| 114 | virtual void classBegin() {} |
| 115 | virtual void componentComplete(); |
| 116 | |
| 117 | // From QAbstractListModel |
| 118 | int rowCount(const QModelIndex &parent) const; |
| 119 | QVariant data(const QModelIndex &index, int role) const; |
| 120 | |
| 121 | bool autoUpdate() const; |
| 122 | void setAutoUpdate(bool autoUpdate); |
| 123 | |
| 124 | QQmlListProperty<QDeclarativeContact> contacts() ; |
| 125 | static void contacts_append(QQmlListProperty<QDeclarativeContact>* prop, QDeclarativeContact* contact); |
| 126 | static int contacts_count(QQmlListProperty<QDeclarativeContact>* prop); |
| 127 | static QDeclarativeContact* contacts_at(QQmlListProperty<QDeclarativeContact>* prop, int index); |
| 128 | static void contacts_clear(QQmlListProperty<QDeclarativeContact>* prop); |
| 129 | |
| 130 | QQmlListProperty<QDeclarativeContactSortOrder> sortOrders(); |
| 131 | static void sortOrder_append(QQmlListProperty<QDeclarativeContactSortOrder> *p, QDeclarativeContactSortOrder *sortOrder); |
| 132 | static int sortOrder_count(QQmlListProperty<QDeclarativeContactSortOrder> *p); |
| 133 | static QDeclarativeContactSortOrder * sortOrder_at(QQmlListProperty<QDeclarativeContactSortOrder> *p, int idx); |
| 134 | static void sortOrder_clear(QQmlListProperty<QDeclarativeContactSortOrder> *p); |
| 135 | |
| 136 | QQmlListProperty<QDeclarativeContactCollection> collections(); |
| 137 | static int collection_count(QQmlListProperty<QDeclarativeContactCollection> *p); |
| 138 | static QDeclarativeContactCollection* collection_at(QQmlListProperty<QDeclarativeContactCollection> *p, int idx); |
| 139 | |
| 140 | Q_INVOKABLE void removeContact(QString id); |
| 141 | Q_INVOKABLE void removeContacts(const QStringList& ids); |
| 142 | Q_INVOKABLE void saveContact(QDeclarativeContact* dc); |
| 143 | Q_INVOKABLE int fetchContacts(const QStringList& contactIds); |
| 144 | Q_INVOKABLE void removeCollection(const QString& collectionId); |
| 145 | Q_INVOKABLE void saveCollection(QDeclarativeContactCollection* collection); |
| 146 | // FIXME : Naming indicates fetch from database |
| 147 | Q_INVOKABLE void fetchCollections(); |
| 148 | Q_INVOKABLE void importContacts(const QUrl& url, const QStringList& profiles = QStringList()); |
| 149 | Q_INVOKABLE void exportContacts(const QUrl& url, const QStringList& profiles = QStringList(), const QVariantList &declarativeContacts = QVariantList()); |
| 150 | |
| 151 | signals: |
| 152 | void managerChanged(); |
| 153 | void filterChanged(); |
| 154 | void errorChanged(); |
| 155 | void fetchHintChanged(); |
| 156 | void contactsChanged(); |
| 157 | void collectionsChanged(); |
| 158 | void sortOrdersChanged(); |
| 159 | void autoUpdateChanged(); |
| 160 | void exportCompleted(ExportError error, QUrl url); |
| 161 | void importCompleted(ImportError error, QUrl url, const QStringList &ids); |
| 162 | void contactsFetched(int requestId, const QVariantList &fetchedContacts); |
| 163 | |
| 164 | public slots: |
| 165 | void update(); |
| 166 | void updateContacts(); |
| 167 | void updateCollections(); |
| 168 | void cancelUpdate(); |
| 169 | |
| 170 | private slots: |
| 171 | void clearContacts(); |
| 172 | void fetchAgain(); |
| 173 | void requestUpdated(); |
| 174 | void fetchRequestStateChanged(QContactAbstractRequest::State newState); |
| 175 | void doUpdate(); |
| 176 | void doContactUpdate(); |
| 177 | void onRequestStateChanged(QContactAbstractRequest::State newState); |
| 178 | void onContactsAdded(const QList<QContactId>& ids); |
| 179 | void onContactsRemoved(const QList<QContactId>& ids); |
| 180 | void onContactsChanged(const QList<QContactId>& ids); |
| 181 | void startImport(QVersitReader::State state); |
| 182 | void contactsExported(QVersitWriter::State state); |
| 183 | void onFetchedContactDestroyed(QObject *obj); |
| 184 | |
| 185 | // handle fetch request from onContactsAdded() |
| 186 | void onContactsAddedFetchRequestStateChanged(QContactAbstractRequest::State state); |
| 187 | |
| 188 | // handle fetch request from onContactsChanged() |
| 189 | void onContactsChangedFetchRequestStateChanged(QContactAbstractRequest::State state); |
| 190 | |
| 191 | // handle fetch request from fetchContacts() |
| 192 | void onFetchContactsRequestStateChanged(QContactAbstractRequest::State state); |
| 193 | |
| 194 | void collectionsFetched(); |
| 195 | |
| 196 | private: |
| 197 | QContactFetchRequest *createContactFetchRequest(const QList<QContactId> &ids); |
| 198 | void checkError(const QContactAbstractRequest *request); |
| 199 | void updateError(QContactManager::Error error); |
| 200 | int contactIndex(const QDeclarativeContact* contact); |
| 201 | |
| 202 | private: |
| 203 | QScopedPointer<QDeclarativeContactModelPrivate> d; |
| 204 | }; |
| 205 | |
| 206 | QT_END_NAMESPACE |
| 207 | |
| 208 | QML_DECLARE_TYPE(QDeclarativeContactModel) |
| 209 | |
| 210 | #endif // QDECLARATIVECONTACTMODEL_P_H |
| 211 | |