ctkPluginContext.h 31 KB

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