| 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 QtContacts 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 |  | 
| 35 | #ifndef QCONTACTMEMORYBACKEND_P_H | 
| 36 | #define QCONTACTMEMORYBACKEND_P_H | 
| 37 |  | 
| 38 | // | 
| 39 | //  W A R N I N G | 
| 40 | //  ------------- | 
| 41 | // | 
| 42 | // This file is not part of the Qt API.  It exists purely as an | 
| 43 | // implementation detail.  This header file may change from version to | 
| 44 | // version without notice, or even be removed. | 
| 45 | // | 
| 46 | // We mean it. | 
| 47 | // | 
| 48 |  | 
| 49 | #include <QtContacts/qcontact.h> | 
| 50 | #include <QtContacts/qcontactmanager.h> | 
| 51 | #include <QtContacts/qcontactmanagerengine.h> | 
| 52 | #include <QtContacts/qcontactchangeset.h> | 
| 53 | #include <QtContacts/qcontactmanagerenginefactory.h> | 
| 54 |  | 
| 55 | QT_BEGIN_NAMESPACE_CONTACTS | 
| 56 |  | 
| 57 | class QContactMemoryEngine; | 
| 58 |  | 
| 59 | class QContactMemoryEngineFactory : public QContactManagerEngineFactory | 
| 60 | { | 
| 61 |     Q_OBJECT | 
| 62 |     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QContactManagerEngineFactoryInterface"  FILE "memory.json" ) | 
| 63 | public: | 
| 64 |     QContactManagerEngine* engine(const QMap<QString, QString> ¶meters, QContactManager::Error*); | 
| 65 |     QString managerName() const; | 
| 66 | }; | 
| 67 |  | 
| 68 | class QContactMemoryEngineData : public QSharedData | 
| 69 | { | 
| 70 | public: | 
| 71 |     QContactMemoryEngineData() | 
| 72 |         : QSharedData() | 
| 73 |         , m_refCount(QAtomicInt(1)) | 
| 74 |         , m_selfContactId() | 
| 75 |         , m_nextContactId(1) | 
| 76 |         , m_anonymous(false) | 
| 77 |     { | 
| 78 |     } | 
| 79 |  | 
| 80 |     QContactMemoryEngineData(const QContactMemoryEngineData &other) | 
| 81 |         : QSharedData(other), | 
| 82 |         m_refCount(QAtomicInt(1)), | 
| 83 |         m_selfContactId(other.m_selfContactId), | 
| 84 |         m_nextContactId(other.m_nextContactId), | 
| 85 |         m_anonymous(other.m_anonymous) | 
| 86 |     { | 
| 87 |     } | 
| 88 |  | 
| 89 |     ~QContactMemoryEngineData() | 
| 90 |     { | 
| 91 |     } | 
| 92 |  | 
| 93 |     static QContactMemoryEngineData *data(QContactMemoryEngine *engine); | 
| 94 |  | 
| 95 |     QAtomicInt m_refCount; | 
| 96 |     QString m_id;                                  // the id parameter value | 
| 97 |  | 
| 98 |     QContactId m_selfContactId;               // the "MyCard" contact id | 
| 99 |     QList<QContact> m_contacts;               // list of contacts | 
| 100 |     QHash<QContactCollectionId, QContactId> m_contactsInCollections;   // hash of contacts for each collection | 
| 101 |     QHash<QContactCollectionId, QContactCollection> m_idToCollectionHash; // hash of id to the collection identified by that id | 
| 102 |     QList<QContactId> m_contactIds;           // list of contact Id's | 
| 103 |     QList<QContactRelationship> m_relationships;   // list of contact relationships | 
| 104 |     QMap<QContactId, QList<QContactRelationship> > m_orderedRelationships; // map of ordered lists of contact relationships | 
| 105 |     QList<QString> m_definitionIds;                // list of definition types (id's) | 
| 106 |     quint32 m_nextContactId; | 
| 107 |     bool m_anonymous;                              // Is this backend ever shared? | 
| 108 |     QString m_managerUri;                        // for faster lookup. | 
| 109 |  | 
| 110 |  | 
| 111 |     void emitSharedSignals(QContactChangeSet *cs) | 
| 112 |     { | 
| 113 |         foreach(QContactManagerEngine* engine, m_sharedEngines) | 
| 114 |             cs->emitSignals(engine); | 
| 115 |     } | 
| 116 |  | 
| 117 |     void emitSharedSignals(QContactCollectionChangeSet *cs) | 
| 118 |     { | 
| 119 |         foreach (QContactManagerEngine *engine, m_sharedEngines) | 
| 120 |             cs->emitSignals(engine); | 
| 121 |     } | 
| 122 |  | 
| 123 |     QList<QContactManagerEngine*> m_sharedEngines;   // The list of engines that share this data | 
| 124 | }; | 
| 125 |  | 
| 126 |  | 
| 127 | class QContactMemoryEngine : public QContactManagerEngine | 
| 128 | { | 
| 129 |     Q_OBJECT | 
| 130 |  | 
| 131 | public: | 
| 132 |     static QContactMemoryEngine *createMemoryEngine(const QMap<QString, QString> ¶meters); | 
| 133 |  | 
| 134 |     ~QContactMemoryEngine(); | 
| 135 |  | 
| 136 |     /* URI reporting */ | 
| 137 |     QString managerName() const; | 
| 138 |     QMap<QString, QString> managerParameters() const; | 
| 139 |     QMap<QString, QString> idInterpretationParameters() const; | 
| 140 |  | 
| 141 |     /*! \reimp */ | 
| 142 |     int managerVersion() const {return 1;} | 
| 143 |  | 
| 144 |     virtual QList<QContactId> contactIds(const QContactFilter &filter, const QList<QContactSortOrder> &sortOrders, QContactManager::Error *error) const; | 
| 145 |     virtual QList<QContact> contacts(const QContactFilter &filter, const QList<QContactSortOrder> &sortOrders, const QContactFetchHint &fetchHint, QContactManager::Error *error) const; | 
| 146 |     virtual QContact contact(const QContactId &contactId, const QContactFetchHint &fetchHint, QContactManager::Error *error) const; | 
| 147 |  | 
| 148 |     virtual bool saveContacts(QList<QContact> *contacts, QMap<int, QContactManager::Error> *errorMap, QContactManager::Error *error); | 
| 149 |     virtual bool removeContacts(const QList<QContactId> &contactIds, QMap<int, QContactManager::Error> *errorMap, QContactManager::Error *error); | 
| 150 |  | 
| 151 |     /* "Self" contact id (MyCard) */ | 
| 152 |     virtual bool setSelfContactId(const QContactId &contactId, QContactManager::Error *error); | 
| 153 |     virtual QContactId selfContactId(QContactManager::Error *error) const; | 
| 154 |  | 
| 155 |     /* Relationships between contacts */ | 
| 156 |     virtual QList<QContactRelationship> relationships(const QString &relationshipType, const QContactId &participantId, QContactRelationship::Role role, QContactManager::Error *error) const; | 
| 157 |     virtual bool saveRelationships(QList<QContactRelationship> *relationships, QMap<int, QContactManager::Error> *errorMap, QContactManager::Error *error); | 
| 158 |     virtual bool removeRelationships(const QList<QContactRelationship> &relationships, QMap<int, QContactManager::Error> *errorMap, QContactManager::Error *error); | 
| 159 |  | 
| 160 |     // collections | 
| 161 |     QContactCollectionId defaultCollectionId() const; | 
| 162 |     QContactCollection collection(const QContactCollectionId &collectionId, QContactManager::Error *error); | 
| 163 |     QList<QContactCollection> collections(QContactManager::Error* error); | 
| 164 |     bool saveCollection(QContactCollection* collection, QContactManager::Error* error); | 
| 165 |     bool removeCollection(const QContactCollectionId& collectionId, QContactManager::Error* error); | 
| 166 |  | 
| 167 |     /*! \reimp */ | 
| 168 |     virtual bool validateContact(const QContact &contact, QContactManager::Error *error) const | 
| 169 |     { | 
| 170 |         return QContactManagerEngine::validateContact(contact, error); | 
| 171 |     } | 
| 172 |  | 
| 173 |     /* Asynchronous Request Support */ | 
| 174 |     virtual void requestDestroyed(QContactAbstractRequest *req); | 
| 175 |     virtual bool startRequest(QContactAbstractRequest *req); | 
| 176 |     virtual bool cancelRequest(QContactAbstractRequest *req); | 
| 177 |     virtual bool waitForRequestFinished(QContactAbstractRequest *req, int msecs); | 
| 178 |  | 
| 179 |     /* Capabilities reporting */ | 
| 180 |     virtual bool isRelationshipTypeSupported(const QString &relationshipType, QContactType::TypeValues contactType) const; | 
| 181 |     virtual bool isFilterSupported(const QContactFilter &filter) const; | 
| 182 |     virtual QList<QVariant::Type> supportedDataTypes() const; | 
| 183 |     /*! \reimp */ | 
| 184 |     virtual QList<QContactType::TypeValues> supportedContactTypes() const | 
| 185 |     { | 
| 186 |         return QContactManagerEngine::supportedContactTypes(); | 
| 187 |     } | 
| 188 |     virtual QList<QContactDetail::DetailType> supportedContactDetailTypes() const | 
| 189 |     { | 
| 190 |         return QContactManagerEngine::supportedContactDetailTypes(); | 
| 191 |     } | 
| 192 |  | 
| 193 |  | 
| 194 | protected: | 
| 195 |     QContactMemoryEngine(QContactMemoryEngineData *data); | 
| 196 |  | 
| 197 | protected: | 
| 198 |     /* Implement "signal coalescing" for batch functions via change set */ | 
| 199 |     virtual bool saveContact(QContact *theContact, QContactChangeSet &changeSet, QContactManager::Error *error); | 
| 200 |     virtual bool removeContact(const QContactId &contactId, QContactChangeSet &changeSet, QContactManager::Error *error); | 
| 201 |     virtual bool saveRelationship(QContactRelationship *relationship, QContactChangeSet &changeSet, QContactManager::Error *error); | 
| 202 |     virtual bool removeRelationship(const QContactRelationship &relationship, QContactChangeSet &changeSet, QContactManager::Error *error); | 
| 203 |  | 
| 204 | private: | 
| 205 |     /* For partial save */ | 
| 206 |     bool saveContacts(QList<QContact> *contacts, QMap<int, QContactManager::Error> *errorMap, QContactManager::Error *error, const QList<QContactDetail::DetailType> &mask); | 
| 207 |     bool saveContact(QContact *theContact, QContactChangeSet &changeSet, QContactManager::Error *error, const QList<QContactDetail::DetailType> &mask); | 
| 208 |     void partiallySyncDetails(QContact *to, const QContact &from, const QList<QContactDetail::DetailType> &mask); | 
| 209 |  | 
| 210 |     void performAsynchronousOperation(QContactAbstractRequest *request); | 
| 211 |  | 
| 212 |     QContactMemoryEngineData *d; | 
| 213 |     static QMap<QString, QContactMemoryEngineData*> engineDatas; | 
| 214 |  | 
| 215 |     friend class QContactMemoryEngineData; | 
| 216 | }; | 
| 217 |  | 
| 218 | QT_END_NAMESPACE_CONTACTS | 
| 219 |  | 
| 220 | #endif // QCONTACTMEMORYBACKEND_P_H | 
| 221 |  |