ctkPluginContext.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKPLUGINCONTEXT_H_
  16. #define CTKPLUGINCONTEXT_H_
  17. #include <QHash>
  18. #include <QString>
  19. #include <QVariant>
  20. #include <QUrl>
  21. #include <QSharedPointer>
  22. #include <QFileInfo>
  23. #include "ctkPluginFramework_global.h"
  24. #include "ctkPluginEvent.h"
  25. #include "ctkServiceException.h"
  26. #include "ctkServiceReference.h"
  27. #include "ctkServiceRegistration.h"
  28. #include "ctkPluginFrameworkExport.h"
  29. // CTK class forward declarations
  30. class ctkPlugin;
  31. class ctkPluginPrivate;
  32. class ctkPluginContextPrivate;
  33. /**
  34. * \ingroup PluginFramework
  35. *
  36. * A plugin's execution context within the Framework. The context is used to
  37. * grant access to other methods so that this plugin can interact with the
  38. * Framework.
  39. *
  40. * <p>
  41. * <code>ctkPluginContext</code> methods allow a plugin to:
  42. * <ul>
  43. * <li>Subscribe to events published by the Framework.
  44. * <li>Register service objects with the Framework service registry.
  45. * <li>Retrieve <code>ServiceReferences</code> from the Framework service
  46. * registry.
  47. * <li>Get and release service objects for a referenced service.
  48. * <li>Install new plugins in the Framework.
  49. * <li>Get the list of plugins installed in the Framework.
  50. * <li>Get the {@link ctkPlugin} object for a plugin.
  51. * <li>Create <code>QFile</code> objects for files in a persistent storage
  52. * area provided for the plugin by the Framework.
  53. * </ul>
  54. *
  55. * <p>
  56. * A <code>ctkPluginContext</code> object will be created and provided to the
  57. * plugin associated with this context when it is started using the
  58. * {@link ctkPluginActivator::start} method. The same <code>ctkPluginContext</code>
  59. * object will be passed to the plugin associated with this context when it is
  60. * stopped using the {@link ctkPluginActivator::stop} method. A
  61. * <code>ctkPluginContext</code> object is generally for the private use of its
  62. * associated plugin and is not meant to be shared with other plugins in the
  63. * plugin environment.
  64. *
  65. * <p>
  66. * The <code>ctkPlugin</code> object associated with a <code>ctkPluginContext</code>
  67. * object is called the <em>context plugin</em>.
  68. *
  69. * <p>
  70. * The <code>ctkPluginContext</code> object is only valid during the execution of
  71. * its context plugin; that is, during the period from when the context plugin
  72. * is in the <code>STARTING</code>, <code>STOPPING</code>, and
  73. * <code>ACTIVE</code> plugin states. If the <code>ctkPluginContext</code>
  74. * object is used subsequently, a <code>ctkIllegalStateException</code> must be
  75. * thrown. The <code>ctkPluginContext</code> object must never be reused after
  76. * its context plugin is stopped.
  77. *
  78. * <p>
  79. * The Framework is the only entity that can create <code>ctkPluginContext</code>
  80. * objects and they are only valid within the Framework that created them.
  81. *
  82. * @remarks This class is thread safe.
  83. */
  84. class CTK_PLUGINFW_EXPORT ctkPluginContext
  85. {
  86. public:
  87. ~ctkPluginContext();
  88. /**
  89. * Returns the value of the specified property. If the key is not found in
  90. * the Framework properties, the system properties are then searched. The
  91. * method returns an invalid QVariant if the property is not found.
  92. *
  93. * @param key The name of the requested property.
  94. * @return The value of the requested property, or an invalid QVariant if
  95. * the property is undefined.
  96. */
  97. QVariant getProperty(const QString& key) const;
  98. /**
  99. * Returns the <code>ctkPlugin</code> object associated with this
  100. * <code>ctkPluginContext</code>. This plugin is called the context plugin.
  101. *
  102. * @return The <code>ctkPlugin</code> object associated with this
  103. * <code>ctkPluginContext</code>.
  104. * @throws ctkIllegalStateException If this ctkPluginContext is no
  105. * longer valid.
  106. */
  107. QSharedPointer<ctkPlugin> getPlugin() const;
  108. /**
  109. * Returns the plugin with the specified identifier.
  110. *
  111. * @param id The identifier of the plugin to retrieve.
  112. * @return A <code>ctkPlugin</code> object or <code>0</code> if the
  113. * identifier does not match any installed plugin.
  114. */
  115. QSharedPointer<ctkPlugin> getPlugin(long id) const;
  116. /**
  117. * Returns a list of all installed plugins.
  118. * <p>
  119. * This method returns a list of all plugins installed in the plugin
  120. * environment at the time of the call to this method. However, since the
  121. * Framework is a very dynamic environment, plugins can be installed or
  122. * uninstalled at anytime.
  123. *
  124. * @return A QList of <code>ctkPlugin</code> objects, one object per
  125. * installed plugin.
  126. */
  127. QList<QSharedPointer<ctkPlugin> > getPlugins() const;
  128. /**
  129. * Registers the specified service object with the specified properties
  130. * under the specified class names into the Framework. A
  131. * <code>ctkServiceRegistration</code> object is returned. The
  132. * <code>ctkServiceRegistration</code> object is for the private use of the
  133. * plugin registering the service and should not be shared with other
  134. * plugins. The registering plugin is defined to be the context plugin.
  135. * Other plugins can locate the service by using either the
  136. * {@link #getServiceReferences} or {@link #getServiceReference} method.
  137. *
  138. * <p>
  139. * A plugin can register a service object that implements the
  140. * {@link ctkServiceFactory} interface to have more flexibility in providing
  141. * service objects to other plugins.
  142. *
  143. * <p>
  144. * The following steps are required to register a service:
  145. * <ol>
  146. * <li>If <code>service</code> is not a <code>ctkServiceFactory</code>, an
  147. * <code>ctkInvalidArgumentException</code> is thrown if <code>service</code>
  148. * is not an instance of all the specified class names.
  149. * <li>The Framework adds the following service properties to the service
  150. * properties from the specified <code>ctkDictionary</code> (which may be
  151. * omitted): <br/>
  152. * A property named {@link ctkPluginConstants#SERVICE_ID} identifying the
  153. * registration number of the service <br/>
  154. * A property named {@link ctkPluginConstants#OBJECTCLASS} containing all the
  155. * specified classes. <br/>
  156. * Properties with these names in the specified <code>ctkDictionary</code> will
  157. * be ignored.
  158. * <li>The service is added to the Framework service registry and may now be
  159. * used by other plugins.
  160. * <li>A service event of type {@link ctkServiceEvent#REGISTERED} is fired.
  161. * <li>A <code>ctkServiceRegistration</code> object for this registration is
  162. * returned.
  163. * </ol>
  164. *
  165. * @param clazzes The class names under which the service can be located.
  166. * The class names will be stored in the service's
  167. * properties under the key {@link ctkPluginConstants#OBJECTCLASS}.
  168. * @param service The service object or a <code>ctkServiceFactory</code>
  169. * object.
  170. * @param properties The properties for this service. The keys in the
  171. * properties object must all be <code>QString</code> objects. See
  172. * {@link ctkPluginConstants} for a list of standard service property keys.
  173. * Changes should not be made to this object after calling this
  174. * method. To update the service's properties the
  175. * {@link ctkServiceRegistration::setProperties} method must be called.
  176. * The set of properties may be omitted if the service has
  177. * no properties.
  178. * @return A <code>ctkServiceRegistration</code> object for use by the plugin
  179. * registering the service to update the service's properties or to
  180. * unregister the service.
  181. * @throws ctkInvalidArgumentException If one of the following is true:
  182. * <ul>
  183. * <li><code>service</code> is <code>0</code>. <li><code>service
  184. * </code> is not a <code>ctkServiceFactory</code> object and is not an
  185. * instance of all the named classes in <code>clazzes</code>. <li>
  186. * <code>properties</code> contains case variants of the same key
  187. * name.
  188. * </ul>
  189. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  190. * @see ctkServiceRegistration
  191. * @see ctkServiceFactory
  192. */
  193. ctkServiceRegistration registerService(const QStringList& clazzes, QObject* service, const ctkDictionary& properties = ctkDictionary());
  194. /**
  195. * Registers the specified service object with the specified properties
  196. * under the specified class name with the Framework.
  197. *
  198. * <p>
  199. * This method is otherwise identical to
  200. * registerService(const QStringList&, QObject*, const ctkDictionary&) and is provided as
  201. * a convenience when <code>service</code> will only be registered under a single
  202. * class name. Note that even in this case the value of the service's
  203. * ctkPluginConstants::OBJECTCLASS property will be a QStringList, rather
  204. * than just a single string.
  205. *
  206. * @param clazz The class name under which the service can be located.
  207. * @param service The service object or a ctkServiceFactory object.
  208. * @param properties The properties for this service.
  209. * @return A ctkServiceRegistration object for use by the plugin
  210. * registering the service to update the service's properties or to
  211. * unregister the service.
  212. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  213. * @see registerService(const QStringList&, QObject*, const ctkDictionary&)
  214. */
  215. ctkServiceRegistration registerService(const char* clazz, QObject* service, const ctkDictionary& properties = ctkDictionary());
  216. template<class S>
  217. ctkServiceRegistration registerService(QObject* service, const ctkDictionary& properties = ctkDictionary())
  218. {
  219. const char* clazz = qobject_interface_iid<S*>();
  220. if (clazz == 0)
  221. {
  222. throw ctkServiceException(QString("The interface class you are registering your service %1 against has no Q_DECLARE_INTERFACE macro")
  223. .arg(service->metaObject()->className()));
  224. }
  225. return registerService(clazz, service, properties);
  226. }
  227. /**
  228. * Returns a list of <code>ctkServiceReference</code> objects. The returned
  229. * list contains services that
  230. * were registered under the specified class and match the specified filter
  231. * expression.
  232. *
  233. * <p>
  234. * The list is valid at the time of the call to this method. However since
  235. * the Framework is a very dynamic environment, services can be modified or
  236. * unregistered at any time.
  237. *
  238. * <p>
  239. * The specified <code>filter</code> expression is used to select the
  240. * registered services whose service properties contain keys and values
  241. * which satisfy the filter expression. See {@link ctkLDAPSearchFilter} for a description
  242. * of the filter syntax. If the specified <code>filter</code> is
  243. * empty, all registered services are considered to match the
  244. * filter. If the specified <code>filter</code> expression cannot be parsed,
  245. * an <code>ctkInvalidArgumentException</code> will be thrown with a human readable
  246. * message where the filter became unparsable.
  247. *
  248. * <p>
  249. * The result is a list of <code>ctkServiceReference</code> objects for all
  250. * services that meet all of the following conditions:
  251. * <ul>
  252. * <li>If the specified class name, <code>clazz</code>, is not
  253. * empty, the service must have been registered with the
  254. * specified class name. The complete list of class names with which a
  255. * service was registered is available from the service's
  256. * {@link ctkPluginConstants::OBJECTCLASS objectClass} property.
  257. * <li>If the specified <code>filter</code> is not empty, the
  258. * filter expression must match the service.
  259. * </ul>
  260. *
  261. * @param clazz The class name with which the service was registered or
  262. * an empty string for all services.
  263. * @param filter The filter expression or empty for all
  264. * services.
  265. * @return A list of <code>ctkServiceReference</code> objects or
  266. * an empty list if no services are registered which satisfy the
  267. * search.
  268. * @throws ctkInvalidArgumentException If the specified <code>filter</code>
  269. * contains an invalid filter expression that cannot be parsed.
  270. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  271. */
  272. QList<ctkServiceReference> getServiceReferences(const QString& clazz, const QString& filter = QString());
  273. /**
  274. * Returns a list of <code>ctkServiceReference</code> objects. The returned
  275. * list contains services that
  276. * were registered under the Qt interface id of the template argument <code>S</code>
  277. * and match the specified filter expression.
  278. *
  279. * <p>
  280. * This method is identical to getServiceReferences(const QString&, const QString&) except that
  281. * the class name for the service object is automatically deduced from the template argument.
  282. *
  283. * @param filter The filter expression or empty for all
  284. * services.
  285. * @return A list of <code>ctkServiceReference</code> objects or
  286. * an empty list if no services are registered which satisfy the
  287. * search.
  288. * @throws ctkInvalidArgumentException If the specified <code>filter</code>
  289. * contains an invalid filter expression that cannot be parsed.
  290. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  291. * @see getServiceReferences(const QString&, const QString&)
  292. */
  293. template<class S>
  294. QList<ctkServiceReference> getServiceReferences(const QString& filter = QString())
  295. {
  296. const char* clazz = qobject_interface_iid<S*>();
  297. if (clazz == 0) throw ctkServiceException("The service interface class has no Q_DECLARE_INTERFACE macro");
  298. return getServiceReferences(QString(clazz), filter);
  299. }
  300. /**
  301. * Returns a <code>ctkServiceReference</code> object for a service that
  302. * implements and was registered under the specified class.
  303. *
  304. * <p>
  305. * The returned <code>ctkServiceReference</code> object is valid at the time of
  306. * the call to this method. However as the Framework is a very dynamic
  307. * environment, services can be modified or unregistered at any time.
  308. *
  309. * <p>
  310. * This method is the same as calling
  311. * {@link ctkPluginContext::getServiceReferences(const QString&, const QString&)} with an
  312. * empty filter expression. It is provided as a convenience for
  313. * when the caller is interested in any service that implements the
  314. * specified class.
  315. * <p>
  316. * If multiple such services exist, the service with the highest ranking (as
  317. * specified in its {@link ctkPluginConstants::SERVICE_RANKING} property) is returned.
  318. * <p>
  319. * If there is a tie in ranking, the service with the lowest service ID (as
  320. * specified in its {@link ctkPluginConstants::SERVICE_ID} property); that is, the
  321. * service that was registered first is returned.
  322. *
  323. * @param clazz The class name with which the service was registered.
  324. * @return A <code>ctkServiceReference</code> object, or <code>0</code> if
  325. * no services are registered which implement the named class.
  326. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  327. * @throws ctkServiceException It no service was registered under the given class name.
  328. * @see #getServiceReferences(const QString&, const QString&)
  329. */
  330. ctkServiceReference getServiceReference(const QString& clazz);
  331. /**
  332. * Returns a <code>ctkServiceReference</code> object for a service that
  333. * implements and was registered under the specified template class argument.
  334. *
  335. * <p>
  336. * This method is identical to getServiceReference(const QString&) except that
  337. * the class name for the service object is automatically deduced from the template argument.
  338. *
  339. * @return A <code>ctkServiceReference</code> object, or <code>0</code> if
  340. * no services are registered which implement the named class.
  341. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  342. * @throws ctkServiceException It no service was registered under the given class name.
  343. * @see #getServiceReference(const QString&)
  344. * @see #getServiceReferences(const QString&)
  345. */
  346. template<class S>
  347. ctkServiceReference getServiceReference()
  348. {
  349. const char* clazz = qobject_interface_iid<S*>();
  350. if (clazz == 0) throw ctkServiceException("The service interface class has no Q_DECLARE_INTERFACE macro");
  351. return getServiceReference(QString(clazz));
  352. }
  353. /**
  354. * Returns the service object referenced by the specified
  355. * <code>ctkServiceReference</code> object.
  356. * <p>
  357. * A plugin's use of a service is tracked by the plugin's use count of that
  358. * service. Each time a service's service object is returned by
  359. * {@link #getService(const ctkServiceReference&)} the context plugin's use count for
  360. * that service is incremented by one. Each time the service is released by
  361. * {@link #ungetService(const ctkServiceReference&)} the context plugin's use count
  362. * for that service is decremented by one.
  363. * <p>
  364. * When a plugin's use count for a service drops to zero, the plugin should
  365. * no longer use that service.
  366. *
  367. * <p>
  368. * This method will always return <code>0</code> when the service
  369. * associated with this <code>reference</code> has been unregistered.
  370. *
  371. * <p>
  372. * The following steps are required to get the service object:
  373. * <ol>
  374. * <li>If the service has been unregistered, <code>0</code> is returned.
  375. * <li>The context plugin's use count for this service is incremented by
  376. * one.
  377. * <li>If the context plugin's use count for the service is currently one
  378. * and the service was registered with an object implementing the
  379. * <code>ctkServiceFactory</code> interface, the
  380. * {@link ctkServiceFactory::getService} method is
  381. * called to create a service object for the context plugin. This service
  382. * object is cached by the Framework. While the context plugin's use count
  383. * for the service is greater than zero, subsequent calls to get the
  384. * services's service object for the context plugin will return the cached
  385. * service object. <br>
  386. * If the service object returned by the <code>ctkServiceFactory</code> object
  387. * is not an instance of all the classes named when the service
  388. * was registered or the <code>ctkServiceFactory</code> object throws an
  389. * exception, <code>0</code> is returned and a Framework event of type
  390. * {@link ctkPluginFrameworkEvent::PLUGIN_ERROR} containing a {@link ctkServiceException}
  391. * describing the error is fired.
  392. * <li>The service object for the service is returned.
  393. * </ol>
  394. *
  395. * @param reference A reference to the service.
  396. * @return A service object for the service associated with
  397. * <code>reference</code> or <code>0</code> if the service is not
  398. * registered, the service object returned by a
  399. * <code>ctkServiceFactory</code> does not implement the classes under
  400. * which it was registered or the <code>ctkServiceFactory</code> threw
  401. * an exception.
  402. * @throws ctkIllegalStateException If this ctkPluginContext is no
  403. * longer valid.
  404. * @throws ctkInvalidArgumentException If the specified
  405. * <code>ctkServiceReference</code> was not created by the same
  406. * framework instance as this <code>ctkPluginContext</code> or
  407. * if it is invalid (default constructed).
  408. * @see #ungetService(const ctkServiceReference&)
  409. * @see ctkServiceFactory
  410. */
  411. QObject* getService(const ctkServiceReference& reference);
  412. /**
  413. * Returns the service object referenced by the specified
  414. * <code>ctkServiceReference</code> object.
  415. * <p>
  416. * This is a convenience method which is identical to QObject* getService(ctkServiceReference)
  417. * except that it casts the service object to the supplied template argument type
  418. *
  419. * @return A service object for the service associated with
  420. * <code>reference</code> or <code>0</code> if the service is not
  421. * registered, the service object returned by a
  422. * <code>ctkServiceFactory</code> does not implement the classes under
  423. * which it was registered, the <code>ctkServiceFactory</code> threw
  424. * an exception or the service could not be casted to the desired type.
  425. * @throws ctkIllegalStateException If this ctkPluginContext is no
  426. * longer valid.
  427. * @throws ctkInvalidArgumentException If the specified
  428. * <code>ctkServiceReference</code> was not created by the same
  429. * framework instance as this <code>ctkPluginContext</code> or
  430. * if it is invalid (default constructed).
  431. * @see #getService(const ctkServiceReference&)
  432. * @see #ungetService(const ctkServiceReference&)
  433. * @see ctkServiceFactory
  434. */
  435. template<class S>
  436. S* getService(const ctkServiceReference& reference)
  437. {
  438. return qobject_cast<S*>(getService(reference));
  439. }
  440. /**
  441. * Releases the service object referenced by the specified
  442. * <code>ctkServiceReference</code> object. If the context plugin's use count
  443. * for the service is zero, this method returns <code>false</code>.
  444. * Otherwise, the context plugins's use count for the service is decremented
  445. * by one.
  446. *
  447. * <p>
  448. * The service's service object should no longer be used and all references
  449. * to it should be destroyed when a bundle's use count for the service drops
  450. * to zero.
  451. *
  452. * <p>
  453. * The following steps are required to unget the service object:
  454. * <ol>
  455. * <li>If the context plugin's use count for the service is zero or the
  456. * service has been unregistered, <code>false</code> is returned.
  457. * <li>The context plugin's use count for this service is decremented by
  458. * one.
  459. * <li>If the context plugin's use count for the service is currently zero
  460. * and the service was registered with a <code>ctkServiceFactory</code> object,
  461. * the
  462. * {@link ctkServiceFactory#ungetService}
  463. * method is called to release the service object for the context plugin.
  464. * <li><code>true</code> is returned.
  465. * </ol>
  466. *
  467. * @param reference A reference to the service to be released.
  468. * @return <code>false</code> if the context plugin's use count for the
  469. * service is zero or if the service has been unregistered;
  470. * <code>true</code> otherwise.
  471. * @throws ctkIllegalStateException If this ctkPluginContext is no
  472. * longer valid.
  473. * @throws ctkInvalidArgumentException If the specified
  474. * <code>ctkServiceReference</code> was not created by the same
  475. * framework instance as this <code>ctkPluginContext</code>.
  476. * @see #getService
  477. * @see ctkServiceFactory
  478. */
  479. bool ungetService(const ctkServiceReference& reference);
  480. /**
  481. * Creates a <code>QFileInfo</code> object for a file or directoryin the
  482. * persistent storage area provided for the plugin by the Framework.
  483. *
  484. * <p>
  485. * A <code>QFileInfo</code> object for the base directory of the persistent
  486. * storage area provided for the context plugin by the Framework can be
  487. * obtained by calling this method with an empty string as
  488. * <code>filename</code>.
  489. *
  490. * <p>
  491. * If the permissions are enabled, the Framework will
  492. * ensure that the plugin has the <code>ctkFilePermission</code> with
  493. * actions <code>read</code>,<code>write</code>,<code>delete</code>
  494. * for all files (recursively) in the persistent storage area provided for
  495. * the context plugin.
  496. *
  497. * @param filename A relative name to the file or directory to be accessed.
  498. * @return A <code>QFileInfo</code> object that represents the requested file
  499. * or directory.
  500. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  501. */
  502. QFileInfo getDataFile(const QString& filename);
  503. /**
  504. * Installs a plugin from the specified <code>QIODevice</code> object.
  505. *
  506. * <p>
  507. * If the specified <code>QIODevice</code> is <code>null</code>, the
  508. * Framework must create the <code>QIODevice</code> from which to read the
  509. * plugin by interpreting, in an implementation dependent manner, the
  510. * specified <code>location</code>.
  511. *
  512. * <p>
  513. * The specified <code>location</code> identifier will be used as the
  514. * identity of the plugin. Every installed plugin is uniquely identified by
  515. * its location identifier which is typically in the form of a URL.
  516. *
  517. * <p>
  518. * The following steps are required to install a plugin:
  519. * <ol>
  520. * <li>If a plugin containing the same location identifier is already
  521. * installed, the <code>ctkPlugin</code> object for that plugin is returned.
  522. *
  523. * <li>The plugin's content is read from the input stream. If this fails, a
  524. * {@link ctkPluginException} is thrown.
  525. *
  526. * <li>The plugin's associated resources are allocated. The associated
  527. * resources minimally consist of a unique identifier and a persistent
  528. * storage area. If this step fails, a <code>ctkPluginException</code>
  529. * is thrown.
  530. *
  531. * <li>The plugin's state is set to <code>INSTALLED</code>.
  532. *
  533. * <li>A plugin event of type {@link ctkPluginEvent#INSTALLED} is fired.
  534. *
  535. * <li>The <code>ctkPlugin</code> object for the newly or previously installed
  536. * plugin is returned.
  537. * </ol>
  538. *
  539. * <b>Postconditions, no exceptions thrown </b>
  540. * <ul>
  541. * <li><code>getState()</code> in &#123; <code>INSTALLED</code>,
  542. * <code>RESOLVED</code> &#125;.
  543. * <li>Plugin has a unique ID.
  544. * </ul>
  545. * <b>Postconditions, when an exception is thrown </b>
  546. * <ul>
  547. * <li>Plugin is not installed and no trace of the plugin exists.
  548. * </ul>
  549. *
  550. * @param location The location identifier of the plugin to install.
  551. * @param input The <code>QIODevice</code> object from which this plugin
  552. * will be read or <code>null</code> to indicate the Framework must
  553. * create the I/O device from the specified location identifier.
  554. * The I/O device must always be closed when this method completes,
  555. * even if an exception is thrown.
  556. * @return The <code>ctkPlugin</code> object of the installed plugin.
  557. * @throws ctkPluginException If the I/O device cannot be read or the
  558. * installation failed.
  559. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  560. */
  561. QSharedPointer<ctkPlugin> installPlugin(const QUrl& location, QIODevice* input = 0);
  562. /**
  563. * Connects the specified <code>slot</code> to the context
  564. * plugins's signal which is emitted when a plugin has
  565. * a lifecycle state change. The signature of the slot
  566. * must be "slotName(ctkPluginEvent)".
  567. *
  568. * @param receiver The object to connect to.
  569. * @param slot The slot to be connected.
  570. * @param type The Qt connection type. Only Qt::DirectConnection,
  571. * Qt::QueuedConnection, or Qt::BlockingQueuedConnection is allowed.
  572. * @returns <code>true</code> if the connection was successfull;
  573. * <code>false</code> otherwise.
  574. * @throws ctkIllegalStateException If this ctkPluginContext is no
  575. * longer valid.
  576. * @see ctkPluginEvent
  577. * @see ctkEventBus
  578. */
  579. bool connectPluginListener(const QObject* receiver, const char* slot, Qt::ConnectionType type = Qt::QueuedConnection);
  580. /**
  581. * Disconnects the specified <code>slot</code> from the context
  582. * plugin.
  583. *
  584. * <p>
  585. * If <code>slot</code> is not connected to the context plugin,
  586. * this method does nothing.
  587. *
  588. * @param receiver The object which has previously connected <code>slot</code>.
  589. * @param slot The Qt slot to be disconnected. If <code>NULL</code>, all slots
  590. * previously connected via connectPluginListener are disconnected.
  591. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  592. */
  593. void disconnectPluginListener(const QObject* receiver, const char* slot = 0);
  594. /**
  595. * Connects the specified <code>slot</code> to the context
  596. * plugin's signal which emits general Framework events. The signature
  597. * of the slot must be "slotName(ctkPluginFrameworkEvent)".
  598. *
  599. * @param receiver The object to connect to.
  600. * @param slot The slot to be connected.
  601. * @param type The Qt connection type.
  602. * @returns <code>true</code> if the connection was successfull;
  603. * <code>false</code> otherwise.
  604. * @throws ctkIllegalStateException If this ctkPluginContext is no
  605. * longer valid.
  606. * @see ctkPluginFrameworkEvent
  607. * @see ctkEventBus
  608. */
  609. bool connectFrameworkListener(const QObject* receiver, const char* slot, Qt::ConnectionType type = Qt::QueuedConnection);
  610. /**
  611. * Disconnects the specified <code>slot</code> from the context
  612. * plugin.
  613. *
  614. * <p>
  615. * If <code>slot</code> is not connected to the context plugin,
  616. * this method does nothing.
  617. *
  618. * @param receiver The object which has previously connected <code>slot</code>.
  619. * @param slot The Qt slot to be disconnected. If <code>NULL</code>, all slots
  620. * previously connected via connectFrameworkListener are disconnected.
  621. * @throws ctkIllegalStateException If this ctkPluginContext is no longer valid.
  622. */
  623. void disconnectFrameworkListener(const QObject* receiver, const char* slot = 0);
  624. /**
  625. * Connects the specified <code>slot</code> with the
  626. * specified <code>filter</code> to the context plugins's signal emitting
  627. * service events when a service has a lifecycle state change. The signature
  628. * of the slot must be "slotName(const ctkServiceEvent&)", but only the name
  629. * of the slot must be provided as the argument.
  630. * See {@link ctkLDAPSearchFilter} for a description of
  631. * the filter syntax.
  632. *
  633. * <p>
  634. * If the object to connect to is destroyed, the slot is automatically
  635. * disconnected. To explicitly disconnect the slot, use
  636. * disconnectServiceListener().
  637. *
  638. * <p>
  639. * If the context plugin's list of listeners already contains the same
  640. * slot for the given receiver, then this
  641. * method replaces that slot's filter (which may be <code>null</code>)
  642. * with the specified one (which may be <code>null</code>).
  643. *
  644. * <p>
  645. * The slot is called if the filter criteria is met. To filter based
  646. * upon the class of the service, the filter should reference the
  647. * {@link ctkPluginConstants#OBJECTCLASS} property. If <code>filter</code> is
  648. * <code>null</code>, all services are considered to match the filter.
  649. *
  650. * <p>
  651. * When using a <code>filter</code>, it is possible that the
  652. * <code>ctkServiceEvent</code>s for the complete lifecycle of a service
  653. * will not be delivered to the slot. For example, if the
  654. * <code>filter</code> only matches when the property <code>x</code> has
  655. * the value <code>1</code>, the listener will not be called if the
  656. * service is registered with the property <code>x</code> not set to the
  657. * value <code>1</code>. Subsequently, when the service is modified
  658. * setting property <code>x</code> to the value <code>1</code>, the
  659. * filter will match and the slot will be called with a
  660. * <code>ServiceEvent</code> of type <code>MODIFIED</code>. Thus, the
  661. * slot will not be called with a <code>ServiceEvent</code> of type
  662. * <code>REGISTERED</code>.
  663. *
  664. * @param receiver The object to connect to.
  665. * @param slot The name of the slot to be connected.
  666. * @param filter The filter criteria.
  667. * @throws ctkInvalidArgumentException If <code>filter</code> contains an
  668. * invalid filter string that cannot be parsed.
  669. * @throws ctkIllegalStateException If this ctkPluginContext is no
  670. * longer valid.
  671. * @see ctkServiceEvent
  672. * @see disconnectServiceListener()
  673. * @see ctkEventBus
  674. */
  675. void connectServiceListener(QObject* receiver, const char* slot,
  676. const QString& filter = QString());
  677. /**
  678. * Disconnects a slot which has been previously connected
  679. * with a call to connectServiceListener().
  680. *
  681. * @param receiver The object containing the slot.
  682. * @param slot The slot to be disconnected.
  683. * @see connectServiceListener()
  684. */
  685. void disconnectServiceListener(QObject* receiver, const char* slot);
  686. protected:
  687. friend class ctkPluginFrameworkPrivate;
  688. friend class ctkPlugin;
  689. friend class ctkPluginPrivate;
  690. ctkPluginContext(ctkPluginPrivate* plugin);
  691. ctkPluginContextPrivate * const d_ptr;
  692. private:
  693. Q_DECLARE_PRIVATE(ctkPluginContext)
  694. };
  695. #endif /* CTKPLUGINCONTEXT_H_ */