Qt 4.8
qaxfactory.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the ActiveQt framework of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef QAXFACTORY_H
42 #define QAXFACTORY_H
43 
44 #include <QtCore/qhash.h>
45 #include <QtCore/quuid.h>
46 #include <QtCore/qfactoryinterface.h>
47 #include <QtCore/qmetaobject.h>
48 #include <QtCore/qstringlist.h>
49 
50 struct IUnknown;
51 struct IDispatch;
52 
54 
56 
57 QT_MODULE(ActiveQt)
58 
59 #ifndef QT_NO_WIN_ACTIVEQT
60 
61 class QWidget;
62 class QSettings;
63 
64 class QAxFactory : public QObject
65 {
66 public:
67  QAxFactory(const QUuid &libId, const QUuid &appId);
68  virtual ~QAxFactory();
69 
70  virtual QStringList featureList() const = 0;
71 
72  virtual QObject *createObject(const QString &key) = 0;
73  virtual const QMetaObject *metaObject(const QString &key) const = 0;
74  virtual bool createObjectWrapper(QObject *object, IDispatch **wrapper);
75 
76  virtual QUuid classID(const QString &key) const;
77  virtual QUuid interfaceID(const QString &key) const;
78  virtual QUuid eventsID(const QString &key) const;
79 
80  virtual QUuid typeLibID() const;
81  virtual QUuid appID() const;
82 
83  virtual void registerClass(const QString &key, QSettings *) const;
84  virtual void unregisterClass(const QString &key, QSettings *) const;
85 
86  virtual bool validateLicenseKey(const QString &key, const QString &licenseKey) const;
87 
88  virtual QString exposeToSuperClass(const QString &key) const;
89  virtual bool stayTopLevel(const QString &key) const;
90  virtual bool hasStockEvents(const QString &key) const;
91  virtual bool isService() const;
92 
93  enum ServerType {
96  };
97 
98  static bool isServer();
99  static QString serverDirPath();
100  static QString serverFilePath();
102  static bool stopServer();
103 
104  static bool registerActiveObject(QObject *object);
105 
106 private:
109 };
110 
111 extern QAxFactory *qAxFactory();
112 
114 
116 {
117  // implementation in qaxservermain.cpp
118  return qax_startServer(type);
119 }
120 
121 extern bool qax_stopServer();
122 
124 {
125  // implementation in qaxservermain.cpp
126  return qax_stopServer();
127 }
128 
129 #define QAXFACTORY_EXPORT(IMPL, TYPELIB, APPID) \
130  QT_BEGIN_NAMESPACE \
131  QAxFactory *qax_instantiate() \
132  { \
133  IMPL *impl = new IMPL(QUuid(TYPELIB), QUuid(APPID)); \
134  return impl; \
135  } \
136  QT_END_NAMESPACE
137 
138 #define QAXFACTORY_DEFAULT(Class, IIDClass, IIDInterface, IIDEvents, IIDTypeLib, IIDApp) \
139  QT_BEGIN_NAMESPACE \
140  class QAxDefaultFactory : public QAxFactory \
141  { \
142  public: \
143  QAxDefaultFactory(const QUuid &app, const QUuid &lib) \
144  : QAxFactory(app, lib), className(QLatin1String(#Class)) {} \
145  QStringList featureList() const \
146  { \
147  QStringList list; \
148  list << className; \
149  return list; \
150  } \
151  const QMetaObject *metaObject(const QString &key) const \
152  { \
153  if (key == className) \
154  return &Class::staticMetaObject; \
155  return 0; \
156  } \
157  QObject *createObject(const QString &key) \
158  { \
159  if (key == className) \
160  return new Class(0); \
161  return 0; \
162  } \
163  QUuid classID(const QString &key) const \
164  { \
165  if (key == className) \
166  return QUuid(IIDClass); \
167  return QUuid(); \
168  } \
169  QUuid interfaceID(const QString &key) const \
170  { \
171  if (key == className) \
172  return QUuid(IIDInterface); \
173  return QUuid(); \
174  } \
175  QUuid eventsID(const QString &key) const \
176  { \
177  if (key == className) \
178  return QUuid(IIDEvents); \
179  return QUuid(); \
180  } \
181  private: \
182  QString className; \
183  }; \
184  QT_END_NAMESPACE \
185  QAXFACTORY_EXPORT(QAxDefaultFactory, IIDTypeLib, IIDApp) \
186 
187 template<class T>
188 class QAxClass : public QAxFactory
189 {
190 public:
191  QAxClass(const QString &libId, const QString &appId)
192  : QAxFactory(libId, appId)
193  {}
194 
195  const QMetaObject *metaObject(const QString &) const { return &T::staticMetaObject; }
196  QStringList featureList() const { return QStringList(QString(T::staticMetaObject.className())); }
198  {
199  const QMetaObject &mo = T::staticMetaObject;
200  if (key != QLatin1String(mo.className()))
201  return 0;
202  if (!qstrcmp(mo.classInfo(mo.indexOfClassInfo("Creatable")).value(), "no"))
203  return 0;
204  return new T(0);
205  }
206 };
207 
208 #define QAXFACTORY_BEGIN(IDTypeLib, IDApp) \
209  QT_BEGIN_NAMESPACE \
210  class QAxFactoryList : public QAxFactory \
211  { \
212  QStringList factoryKeys; \
213  QHash<QString, QAxFactory*> factories; \
214  QHash<QString, bool> creatable; \
215  public: \
216  QAxFactoryList() \
217  : QAxFactory(IDTypeLib, IDApp) \
218  { \
219  QAxFactory *factory = 0; \
220  QStringList keys; \
221  QStringList::Iterator it; \
222 
223 #define QAXCLASS(Class) \
224  factory = new QAxClass<Class>(typeLibID(), appID()); \
225  qRegisterMetaType<Class*>(#Class"*"); \
226  keys = factory->featureList(); \
227  for (it = keys.begin(); it != keys.end(); ++it) { \
228  factoryKeys += *it; \
229  factories.insert(*it, factory); \
230  creatable.insert(*it, true); \
231  }\
232 
233 #define QAXTYPE(Class) \
234  factory = new QAxClass<Class>(typeLibID(), appID()); \
235  qRegisterMetaType<Class*>(#Class"*"); \
236  keys = factory->featureList(); \
237  for (it = keys.begin(); it != keys.end(); ++it) { \
238  factoryKeys += *it; \
239  factories.insert(*it, factory); \
240  creatable.insert(*it, false); \
241  }\
242 
243 #define QAXFACTORY_END() \
244  } \
245  ~QAxFactoryList() { qDeleteAll(factories); } \
246  QStringList featureList() const { return factoryKeys; } \
247  const QMetaObject *metaObject(const QString&key) const { \
248  QAxFactory *f = factories[key]; \
249  return f ? f->metaObject(key) : 0; \
250  } \
251  QObject *createObject(const QString &key) { \
252  if (!creatable.value(key)) \
253  return 0; \
254  QAxFactory *f = factories[key]; \
255  return f ? f->createObject(key) : 0; \
256  } \
257  QUuid classID(const QString &key) { \
258  QAxFactory *f = factories.value(key); \
259  return f ? f->classID(key) : QUuid(); \
260  } \
261  QUuid interfaceID(const QString &key) { \
262  QAxFactory *f = factories.value(key); \
263  return f ? f->interfaceID(key) : QUuid(); \
264  } \
265  QUuid eventsID(const QString &key) { \
266  QAxFactory *f = factories.value(key); \
267  return f ? f->eventsID(key) : QUuid(); \
268  } \
269  void registerClass(const QString &key, QSettings *s) const { \
270  QAxFactory *f = factories.value(key); \
271  if (f) f->registerClass(key, s); \
272  } \
273  void unregisterClass(const QString &key, QSettings *s) const { \
274  QAxFactory *f = factories.value(key); \
275  if (f) f->unregisterClass(key, s); \
276  } \
277  QString exposeToSuperClass(const QString &key) const { \
278  QAxFactory *f = factories.value(key); \
279  return f ? f->exposeToSuperClass(key) : QString(); \
280  } \
281  bool stayTopLevel(const QString &key) const { \
282  QAxFactory *f = factories.value(key); \
283  return f ? f->stayTopLevel(key) : false; \
284  } \
285  bool hasStockEvents(const QString &key) const { \
286  QAxFactory *f = factories.value(key); \
287  return f ? f->hasStockEvents(key) : false; \
288  } \
289  }; \
290  QAxFactory *qax_instantiate() \
291  { \
292  QAxFactoryList *impl = new QAxFactoryList(); \
293  return impl; \
294  } \
295  QT_END_NAMESPACE
296 
298 
299 #ifndef Q_COM_METATYPE_DECLARED
300 #define Q_COM_METATYPE_DECLARED
301 
302 Q_DECLARE_METATYPE(IUnknown*)
303 Q_DECLARE_METATYPE(IDispatch*)
304 
305 #endif
306 
307 #endif // QT_NO_WIN_ACTIVEQT
308 
310 
311 #endif // QAXFACTORY_H
virtual QUuid typeLibID() const
Reimplement this function to return the ActiveX server&#39;s type library identifier. ...
Definition: qaxfactory.cpp:124
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QAxFactory * qAxFactory()
Definition: qaxserver.cpp:81
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
static bool stopServer()
Stops the COM server and returns true if successful, otherwise returns false.
Definition: qaxfactory.h:123
QMetaClassInfo classInfo(int index) const
Returns the meta-data for the item of class information with the given index.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QAxFactory class defines a factory for the creation of COM components.
Definition: qaxfactory.h:64
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QString serverDirPath()
Returns the directory that contains the server binary.
Definition: qaxfactory.cpp:386
The QString class provides a Unicode character string.
Definition: qstring.h:83
QStringList featureList() const
Reimplement this function to return a list of the widgets (class names) supported by this factory...
Definition: qaxfactory.h:196
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual QStringList featureList() const =0
Reimplement this function to return a list of the widgets (class names) supported by this factory...
const char * className
Definition: qwizard.cpp:137
virtual QString exposeToSuperClass(const QString &key) const
Reimplement this function to return the name of the super class of key up to which methods and proper...
Definition: qaxfactory.cpp:317
static QString serverFilePath()
Returns the file path of the server binary.
Definition: qaxfactory.cpp:398
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QUuid app
Definition: qaxfactory.h:108
virtual ~QAxFactory()
Destroys the QAxFactory object.
Definition: qaxfactory.cpp:111
virtual bool hasStockEvents(const QString &key) const
Reimplement this function to return true if the ActiveX control key should support the standard Activ...
Definition: qaxfactory.cpp:353
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QObject * createObject(const QString &key)
Reimplement this function to return a new object for key, or 0 if this factory doesn&#39;t support the va...
Definition: qaxfactory.h:197
virtual QUuid appID() const
Reimplement this function to return the ActiveX server&#39;s application identifier.
Definition: qaxfactory.cpp:138
virtual QUuid interfaceID(const QString &key) const
Reimplement this function to return the interface identifier for each key returned by the featureList...
Definition: qaxfactory.cpp:209
virtual bool createObjectWrapper(QObject *object, IDispatch **wrapper)
Reimplement this function to provide the COM object for object in wrapper.
static bool registerActiveObject(QObject *object)
Registers the QObject object with COM as a running object, and returns true if the registration succe...
Definition: qaxfactory.cpp:512
virtual bool stayTopLevel(const QString &key) const
Reimplement this function to return true if the ActiveX control key should be a top level window...
Definition: qaxfactory.cpp:330
virtual QUuid eventsID(const QString &key) const
Reimplement this function to return the identifier of the event interface for each key returned by th...
Definition: qaxfactory.cpp:228
QAxFactory(const QUuid &libId, const QUuid &appId)
Constructs a QAxFactory object that returns libid and appid in the implementation of the respective i...
Definition: qaxfactory.cpp:103
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
virtual bool isService() const
Reimplement this function to return true if the server is running as a persistent service (e...
Definition: qaxfactory.cpp:410
static bool isServer()
Returns true if the application has been started (by COM) as an ActiveX server, otherwise returns fal...
Definition: qaxfactory.cpp:371
virtual void unregisterClass(const QString &key, QSettings *) const
Unregisters any additional values for the class key from the system registry using the settings objec...
Definition: qaxfactory.cpp:266
virtual bool validateLicenseKey(const QString &key, const QString &licenseKey) const
Reimplement this function to return true if licenseKey is a valid license for the class key...
Definition: qaxfactory.cpp:282
static bool startServer(ServerType type=MultipleInstances)
Starts the COM server with type and returns true if successful, otherwise returns false...
Definition: qaxfactory.h:115
ServerType
This enum specifies the different types of servers that can be started with startServer.
Definition: qaxfactory.h:93
int key
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
bool qax_stopServer()
const QMetaObject * metaObject(const QString &) const
Reimplement this function to return the QMetaObject corresponding to key, or 0 if this factory doesn&#39;...
Definition: qaxfactory.h:195
bool qax_startServer(QAxFactory::ServerType)
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
virtual QObject * createObject(const QString &key)=0
Reimplement this function to return a new object for key, or 0 if this factory doesn&#39;t support the va...
int indexOfClassInfo(const char *name) const
Finds class information item name and returns its index; otherwise returns -1.
#define QT_END_HEADER
Definition: qglobal.h:137
QAxClass(const QString &libId, const QString &appId)
Definition: qaxfactory.h:191
QUuid typelib
Definition: qaxfactory.h:107
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
The QUuid class stores a Universally Unique Identifier (UUID).
Definition: quuid.h:67
virtual QUuid classID(const QString &key) const
Reimplement this function to return the class identifier for each key returned by the featureList() i...
Definition: qaxfactory.cpp:191
virtual void registerClass(const QString &key, QSettings *) const
Registers additional values for the class key in the system registry using the settings object...
Definition: qaxfactory.cpp:252