| 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 | #ifndef QCONTACTMANAGER_P_H |
| 35 | #define QCONTACTMANAGER_P_H |
| 36 | |
| 37 | // |
| 38 | // W A R N I N G |
| 39 | // ------------- |
| 40 | // |
| 41 | // This file is not part of the Qt API. It exists purely as an |
| 42 | // implementation detail. This header file may change from version to |
| 43 | // version without notice, or even be removed. |
| 44 | // |
| 45 | // We mean it. |
| 46 | // |
| 47 | |
| 48 | #include <QtCore/qhash.h> |
| 49 | #include <QtCore/qjsonobject.h> |
| 50 | #include <QtCore/qmap.h> |
| 51 | #include <QtCore/qstringlist.h> |
| 52 | |
| 53 | #include <QtContacts/qcontactid.h> |
| 54 | #include <QtContacts/qcontactmanager.h> |
| 55 | #include <QtContacts/qcontactmanagerengine.h> |
| 56 | #include <QtContacts/qcontactobserver.h> |
| 57 | #include <QtContacts/private/qcontactactionmanager_p.h> |
| 58 | |
| 59 | |
| 60 | QT_BEGIN_NAMESPACE_CONTACTS |
| 61 | |
| 62 | class QContactManagerEngineFactory; |
| 63 | |
| 64 | /* Data and stuff that is shared amongst all backends */ |
| 65 | class QContactManagerData |
| 66 | { |
| 67 | public: |
| 68 | QContactManagerData() |
| 69 | : m_engine(0), |
| 70 | m_lastError(QContactManager::NoError) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | ~QContactManagerData() |
| 75 | { |
| 76 | delete m_engine; |
| 77 | } |
| 78 | |
| 79 | // helpers |
| 80 | static bool parseUri(const QString &uriString, QString *managerName, QMap<QString, QString> *params, bool strict = true); |
| 81 | static QString buildUri(const QString &managerName, const QMap<QString, QString> ¶ms); |
| 82 | |
| 83 | static bool parseIdData(const QByteArray &idData, QString *managerName, QMap<QString, QString> *params, QString *managerUri = 0, QByteArray *localId = 0); |
| 84 | |
| 85 | static QByteArray buildIdData(const QString &managerUri, const QByteArray &localId = QByteArray()); |
| 86 | static QByteArray buildIdData(const QString &managerName, const QMap<QString, QString> ¶ms, const QByteArray &localId = QByteArray()); |
| 87 | |
| 88 | static QString cachedUri(const QString &managerUri); |
| 89 | |
| 90 | void createEngine(const QString &managerName, const QMap<QString, QString> ¶meters); |
| 91 | static QContactManagerData* get(const QContactManager *manager); |
| 92 | static QContactManagerEngine* engine(const QContactManager *manager); |
| 93 | |
| 94 | QContactManagerEngine* m_engine; |
| 95 | QContactManager::Error m_lastError; |
| 96 | QMap<int, QContactManager::Error> m_lastErrorMap; |
| 97 | |
| 98 | /* Manager plugins */ |
| 99 | static QHash<QString, QContactManagerEngineFactory*> m_engines; |
| 100 | static QSet<QContactManager*> m_aliveEngines; |
| 101 | static QContactManagerData* managerData(QContactManager *manager) {return manager->d;} |
| 102 | static QContactManagerData* managerData(const QContactManager *manager) {return manager->d;} // laziness to avoid const_cast |
| 103 | static QList<QContactActionManagerPlugin*> m_actionManagers; |
| 104 | static bool m_discoveredStatic; |
| 105 | static QList<QJsonObject> m_pluginPaths; |
| 106 | static QList<QJsonObject> m_metaData; |
| 107 | static QStringList m_managerNames; |
| 108 | static void loadFactoriesMetadata(); |
| 109 | static void loadStaticFactories(); |
| 110 | |
| 111 | // Observer stuff |
| 112 | static void registerObserver(QContactManager *m, QContactObserver *observer); |
| 113 | static void unregisterObserver(QContactManager *m, QContactObserver *observer); |
| 114 | void _q_contactsUpdated(const QList<QContactId> &ids, const QList<QContactDetail::DetailType> &typesChanged); |
| 115 | void _q_contactsDeleted(const QList<QContactId> &ids); |
| 116 | |
| 117 | QMultiHash<QContactId, QContactObserver*> m_observerForContact; |
| 118 | private: |
| 119 | Q_DISABLE_COPY(QContactManagerData) |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | Helper to hold the error state of a synchronous operation - when destructed, updates the |
| 124 | manager's last error variables to the result of this operation. This means that during |
| 125 | callbacks the error state can't be modified behind the engines back. and it's more conceptually |
| 126 | correct. |
| 127 | */ |
| 128 | class QContactManagerSyncOpErrorHolder |
| 129 | { |
| 130 | public: |
| 131 | QContactManagerSyncOpErrorHolder(const QContactManager *m, QMap<int, QContactManager::Error> *pUserError = 0) |
| 132 | : error(QContactManager::NoError), |
| 133 | data(QContactManagerData::managerData(manager: m)), |
| 134 | userError(pUserError) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | ~QContactManagerSyncOpErrorHolder() |
| 139 | { |
| 140 | data->m_lastError = error; |
| 141 | data->m_lastErrorMap = errorMap; |
| 142 | if (userError) |
| 143 | *userError = errorMap; |
| 144 | } |
| 145 | |
| 146 | QContactManager::Error error; |
| 147 | QContactManagerData* data; |
| 148 | QMap<int, QContactManager::Error> errorMap; |
| 149 | QMap<int, QContactManager::Error> *userError; |
| 150 | }; |
| 151 | |
| 152 | QT_END_NAMESPACE_CONTACTS |
| 153 | |
| 154 | #endif // QCONTACTMANAGER_P_H |
| 155 | |