| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems 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 QREMOTESERVICEREGISTER_H |
| 35 | #define QREMOTESERVICEREGISTER_H |
| 36 | |
| 37 | #include "qserviceframeworkglobal.h" |
| 38 | #include <QObject> |
| 39 | #include <QQueue> |
| 40 | #include <QHash> |
| 41 | #include <QDebug> |
| 42 | #include <QExplicitlySharedDataPointer> |
| 43 | |
| 44 | #include "qserviceclientcredentials.h" |
| 45 | #include "qservice.h" |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | class QRemoteServiceRegisterPrivate; |
| 50 | class QRemoteServiceRegisterEntryPrivate; |
| 51 | class QServiceClientCredentialsPrivate; |
| 52 | class QServiceClientCredentials; |
| 53 | |
| 54 | class Q_SERVICEFW_EXPORT QRemoteServiceRegister : public QObject |
| 55 | { |
| 56 | Q_OBJECT |
| 57 | Q_PROPERTY(bool quitOnLastInstanceClosed READ quitOnLastInstanceClosed WRITE setQuitOnLastInstanceClosed) |
| 58 | Q_FLAGS(SocketAccessOption SecurityAccessOptions) |
| 59 | public: |
| 60 | |
| 61 | enum InstanceType { |
| 62 | GlobalInstance = 0, |
| 63 | PrivateInstance |
| 64 | }; |
| 65 | |
| 66 | enum SecurityAccessOption { |
| 67 | NoOptions = 0x0, |
| 68 | UserAccessOption = 0x01, |
| 69 | GroupAccessOption = 0x02, |
| 70 | OtherAccessOption = 0x04, |
| 71 | WorldAccessOption = 0x07 |
| 72 | }; |
| 73 | Q_DECLARE_FLAGS(SecurityAccessOptions, SecurityAccessOption) |
| 74 | |
| 75 | typedef QObject *(*CreateServiceFunc)(); |
| 76 | |
| 77 | class Q_SERVICEFW_EXPORT Entry { |
| 78 | public: |
| 79 | Entry(); |
| 80 | Entry(const Entry &); |
| 81 | Entry &operator=(const Entry &); |
| 82 | |
| 83 | ~Entry(); |
| 84 | |
| 85 | bool operator==(const Entry &) const; |
| 86 | bool operator!=(const Entry &) const; |
| 87 | |
| 88 | bool isValid() const; |
| 89 | |
| 90 | QString interfaceName() const; |
| 91 | QString serviceName() const; |
| 92 | QString version() const; |
| 93 | |
| 94 | void setInstantiationType(QRemoteServiceRegister::InstanceType type); |
| 95 | QRemoteServiceRegister::InstanceType instantiationType() const; |
| 96 | |
| 97 | private: |
| 98 | QExplicitlySharedDataPointer<QRemoteServiceRegisterEntryPrivate> d; |
| 99 | |
| 100 | const QMetaObject* metaObject() const; |
| 101 | |
| 102 | friend class QRemoteServiceRegisterPrivate; |
| 103 | friend class QRemoteServiceRegister; |
| 104 | friend class InstanceManager; |
| 105 | friend class QServiceManager; |
| 106 | #ifndef QT_NO_DATASTREAM |
| 107 | friend Q_SERVICEFW_EXPORT QDataStream &operator<<(QDataStream &, const QRemoteServiceRegister::Entry &); |
| 108 | friend Q_SERVICEFW_EXPORT QDataStream &operator>>(QDataStream &, QRemoteServiceRegister::Entry &); |
| 109 | #endif |
| 110 | }; |
| 111 | |
| 112 | explicit QRemoteServiceRegister(QObject* parent = Q_NULLPTR); |
| 113 | ~QRemoteServiceRegister(); |
| 114 | |
| 115 | template <typename T> |
| 116 | Entry createEntry(const QString& serviceName, |
| 117 | const QString& interfaceName, const QString& version); |
| 118 | |
| 119 | void publishEntries(const QString& ident ); |
| 120 | |
| 121 | bool quitOnLastInstanceClosed() const; |
| 122 | void setQuitOnLastInstanceClosed(const bool quit); |
| 123 | |
| 124 | typedef void (*SecurityFilter)(QServiceClientCredentials *creds); |
| 125 | SecurityFilter setSecurityFilter(SecurityFilter filter); |
| 126 | |
| 127 | SecurityAccessOptions securityAccessOptions() const; |
| 128 | void setSecurityAccessOptions(SecurityAccessOptions options); |
| 129 | |
| 130 | qintptr getBaseUserIdentifier() const; |
| 131 | void setBaseUserIdentifier(qintptr uid); |
| 132 | |
| 133 | qintptr getBaseGroupIdentifier() const; |
| 134 | void setBaseGroupIdentifier(qintptr gid); |
| 135 | |
| 136 | Q_SIGNALS: |
| 137 | void allInstancesClosed(); |
| 138 | void instanceClosed(const QRemoteServiceRegister::Entry& entry); |
| 139 | |
| 140 | private: |
| 141 | |
| 142 | Entry createEntry(const QString& serviceName, |
| 143 | const QString& interfaceName, const QString& version, |
| 144 | CreateServiceFunc cptr, const QMetaObject* meta); |
| 145 | |
| 146 | void init(); |
| 147 | bool event(QEvent *e); |
| 148 | |
| 149 | QRemoteServiceRegisterPrivate* d; |
| 150 | }; |
| 151 | |
| 152 | inline uint qHash(const QRemoteServiceRegister::Entry& e) { |
| 153 | //Only consider version, iface and service name -> needs to sync with operator== |
| 154 | return ( qHash(key: e.serviceName()) + qHash(key: e.interfaceName()) + qHash(key: e.version()) ); |
| 155 | } |
| 156 | |
| 157 | #ifndef QT_NO_DATASTREAM |
| 158 | Q_SERVICEFW_EXPORT QDataStream& operator>>(QDataStream& s, QRemoteServiceRegister::Entry& entry); |
| 159 | Q_SERVICEFW_EXPORT QDataStream& operator<<(QDataStream& s, const QRemoteServiceRegister::Entry& entry); |
| 160 | #endif |
| 161 | |
| 162 | #ifndef QT_NO_DEBUG_STREAM |
| 163 | QDebug operator<<(QDebug dbg, const QRemoteServiceRegister::Entry& entry); |
| 164 | #endif |
| 165 | |
| 166 | template <typename T> |
| 167 | QObject* qServiceTypeConstructHelper() |
| 168 | { |
| 169 | return new T; |
| 170 | } |
| 171 | |
| 172 | template <typename T> |
| 173 | QRemoteServiceRegister::Entry QRemoteServiceRegister::createEntry(const QString& serviceName, |
| 174 | const QString& interfaceName, const QString& version) |
| 175 | { |
| 176 | |
| 177 | QRemoteServiceRegister::CreateServiceFunc cptr = qServiceTypeConstructHelper<T>; |
| 178 | return createEntry(serviceName, interfaceName, version, cptr, &T::staticMetaObject); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | QT_END_NAMESPACE |
| 183 | #endif //QREMOTESERVICEREGISTER_H |
| 184 | |