ctkPluginContext.h 31 KB

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