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 QSERVICEMANAGER_H
35#define QSERVICEMANAGER_H
36
37#include "qserviceframeworkglobal.h"
38
39#include "qservice.h"
40#include "qserviceinterfacedescriptor.h"
41#include "qservicefilter.h"
42
43#include <QObject>
44#include <QList>
45#include <QStringList>
46#include <QDebug>
47
48QT_BEGIN_NAMESPACE
49
50class QServiceContext;
51class QServiceFilter;
52class QServiceManagerPrivate;
53class QServiceReply;
54class Q_SERVICEFW_EXPORT QServiceManager : public QObject
55{
56 Q_OBJECT
57
58public:
59
60 enum Error {
61 NoError,
62 StorageAccessError,
63 InvalidServiceLocation,
64 InvalidServiceXml,
65 InvalidServiceInterfaceDescriptor,
66 ServiceAlreadyExists,
67 ImplementationAlreadyExists,
68 PluginLoadingFailed,
69 ComponentNotFound,
70 ServiceCapabilityDenied,
71 UnknownError = 100
72 };
73
74 explicit QServiceManager(QObject *parent = Q_NULLPTR);
75 explicit QServiceManager(QService::Scope scope, QObject *parent = Q_NULLPTR);
76 ~QServiceManager();
77
78 QService::Scope scope() const;
79
80 QStringList findServices(const QString& interfaceName = QString()) const;
81 QList<QServiceInterfaceDescriptor> findInterfaces(const QServiceFilter& filter = QServiceFilter()) const;
82 QList<QServiceInterfaceDescriptor> findInterfaces(const QString& serviceName) const;
83
84 QObject* loadInterface(const QString& interfaceName);
85 QObject* loadInterface(const QServiceInterfaceDescriptor& descriptor);
86
87 bool isInterfaceRunning(const QString& interfaceName);
88 bool isInterfaceRunning(const QServiceInterfaceDescriptor& descriptor);
89
90 template <class T>
91 T* loadLocalTypedInterface(const QString& interfaceName);
92
93 template <class T>
94 T* loadLocalTypedInterface(const QServiceInterfaceDescriptor& descriptor);
95
96 QServiceReply *loadInterfaceRequest(const QString& interfaceName);
97 QServiceReply *loadInterfaceRequest(const QServiceInterfaceDescriptor& descriptor);
98
99 bool addService(const QString& xmlFilePath);
100 bool addService(QIODevice* xmlDevice);
101 bool removeService(const QString& serviceName);
102
103 bool setInterfaceDefault(const QString &service, const QString &interfaceName);
104 bool setInterfaceDefault(const QServiceInterfaceDescriptor& descriptor);
105
106 QServiceInterfaceDescriptor interfaceDefault(const QString& interfaceName) const;
107
108 QServiceManager::Error error() const;
109
110 bool event(QEvent *);
111
112protected:
113 void connectNotify(const QMetaMethod &signal);
114 void disconnectNotify(const QMetaMethod &signal);
115
116Q_SIGNALS:
117 void serviceAdded(const QString& serviceName, QService::Scope scope);
118 void serviceRemoved(const QString& serviceName, QService::Scope scope);
119 void errorChanged();
120
121private:
122 QObject *loadInterProcessService(const QServiceInterfaceDescriptor &descriptor,
123 const QString &location) const;
124 QObject *loadInProcessService(const QServiceInterfaceDescriptor &descriptor,
125 const QString &serviceFilePath) const;
126
127 static QString resolveLibraryPath(const QString &libNameOrPath);
128
129 friend class QServiceManagerPrivate;
130 friend class QServiceOperationProcessor;
131 QServiceManagerPrivate* d;
132};
133
134template <class T>
135Q_INLINE_TEMPLATE T* QServiceManager::loadLocalTypedInterface(const QString& interfaceName)
136{
137 return loadLocalTypedInterface<T>(interfaceDefault(interfaceName));
138}
139
140template <class T>
141Q_OUTOFLINE_TEMPLATE T* QServiceManager::loadLocalTypedInterface(const QServiceInterfaceDescriptor& descriptor)
142{
143 T* instance = Q_NULLPTR;
144 if (descriptor.isValid()) {
145 QObject* obj = loadInterface(descriptor);
146 if (!obj) return Q_NULLPTR;
147
148 //TODO this should really be
149 //instance = qobject_cast<T *>(loadInterface(descriptor, context, session));
150 //check why qobject_cast fails
151 const char* templateClassName = T::staticMetaObject.className();
152 const QMetaObject* source = obj->metaObject();
153 do {
154 if (strcmp(s1: templateClassName,s2: source->className())==0) {
155 instance = static_cast<T *>(obj);
156 break;
157 }
158 source = source->superClass();
159 } while (source);
160 if (!instance)
161 delete obj;
162 }
163 return instance;
164}
165
166QT_END_NAMESPACE
167
168#endif
169

source code of qtsystems/src/serviceframework/qservicemanager.h