ctkPluginStorageSQL_p.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 ctkPluginStorageSQL_P_H
  16. #define ctkPluginStorageSQL_P_H
  17. #include "ctkPluginStorage_p.h"
  18. #include <QtSql>
  19. // CTK class forward declarations
  20. class ctkPluginFrameworkContext;
  21. class ctkPluginArchiveSQL;
  22. /**
  23. * \ingroup PluginFramework
  24. */
  25. class ctkPluginStorageSQL : public ctkPluginStorage
  26. {
  27. public:
  28. /**
  29. * Create a container for all plugin data in this framework.
  30. * Try to restore all saved plugin archive state.
  31. *
  32. */
  33. ctkPluginStorageSQL(ctkPluginFrameworkContext* framework);
  34. virtual ~ctkPluginStorageSQL();
  35. /**
  36. * Inserts a new plugin into the database. This method assumes that
  37. * the an entry with the same \a location and \a localPath does not
  38. * yet exist in the database.
  39. *
  40. * @param location The URL to the plugin.
  41. * @param updateLocation Location of the updated plugin.
  42. * @param localPath The path to the plugin library on the local file system.
  43. * @param createArchive If \c true (default) a new ctkPluginArchive instance is returned.
  44. *
  45. * @throws ctkPluginDatabaseException
  46. */
  47. QSharedPointer<ctkPluginArchive> insertPlugin(const QUrl& location, const QString& localPath);
  48. /**
  49. * Insert a new plugin (shared library) into the persistent
  50. * storagedata as an update
  51. * to an existing plugin archive. To commit this data a call to
  52. * <code>replacePluginArchive</code> is needed.
  53. *
  54. * @param old ctkPluginArchive to be replaced.
  55. * @param localPath Path to a plugin on the local file system.
  56. * @return Plugin archive object.
  57. */
  58. QSharedPointer<ctkPluginArchive> updatePluginArchive(QSharedPointer<ctkPluginArchive> old,
  59. const QUrl& updateLocation, const QString& localPath);
  60. /**
  61. * Replace old plugin archive with a new updated plugin archive, that
  62. * was created with updatePluginArchive.
  63. *
  64. * @param oldPA ctkPluginArchive to be replaced.
  65. * @param newPA new ctkPluginArchive.
  66. */
  67. void replacePluginArchive(QSharedPointer<ctkPluginArchive> oldPA, QSharedPointer<ctkPluginArchive> newPA);
  68. /**
  69. * Removes all persisted data related to the given ctkPluginArchive.
  70. *
  71. * @throws ctkPluginDatabaseException
  72. */
  73. bool removeArchive(QSharedPointer<ctkPluginArchive> pa);
  74. /**
  75. * Get all plugin archive objects.
  76. *
  77. * @return QList of all PluginArchives.
  78. */
  79. QList<QSharedPointer<ctkPluginArchive> > getAllPluginArchives() const;
  80. /**
  81. * Get all plugins to start at next launch of framework.
  82. * This list is sorted in increasing plugin id order.
  83. *
  84. * @return A List with plugin locations.
  85. */
  86. QList<QString> getStartOnLaunchPlugins() const;
  87. /**
  88. * Closes the plugin database. Throws a ctkPluginDatabaseException
  89. * of type DB_CONNECTION_INVALID if the database is invalid.
  90. *
  91. * @throws ctkPluginDatabaseException
  92. */
  93. void close();
  94. // -------------------------------------------------------------
  95. // end ctkPluginStorage interface
  96. // -------------------------------------------------------------
  97. /**
  98. * Sets the path of the service database to \a databasePath
  99. */
  100. void setDatabasePath(const QString &databasePath);
  101. /**
  102. * Returns the path of the plugin database
  103. */
  104. QString getDatabasePath() const;
  105. /**
  106. * Get a Qt resource cached in the database. The resource path \a res
  107. * must be relative to the plugin specific resource prefix, but may
  108. * start with a '/'.
  109. *
  110. * @param pluginId The id of the plugin from which to get the resource
  111. * @param res The path to the resource in the plugin
  112. * @return The byte array of the cached resource
  113. *
  114. * @throws ctkPluginDatabaseException
  115. */
  116. QByteArray getPluginResource(int key, const QString& res) const;
  117. /**
  118. * Get a list of resource entries under the given path.
  119. *
  120. * @param pluginId The id of the plugin from which to get the entries
  121. * @param path A resource path relative to the plugin specific resource prefix.
  122. * @return A QStringList containing the cached resource entries.
  123. *
  124. * @throws ctkPluginDatabaseException
  125. */
  126. QStringList findResourcesPath(int archiveKey, const QString& path) const;
  127. /**
  128. * Persist the start level
  129. *
  130. * @param pluginId The Plugin id
  131. * @param startLevel The new start level
  132. */
  133. void setStartLevel(int key, int startLevel);
  134. /**
  135. * Persist the last modification (state change) time
  136. *
  137. * @param pluginId The Plugin id
  138. * @param lastModified The modification time
  139. */
  140. void setLastModified(int key, const QDateTime& lastModified);
  141. /**
  142. * Persist the auto start setting.
  143. *
  144. * @param pluginId The Plugin id
  145. * @param autostart The new auto start setting
  146. */
  147. void setAutostartSetting(int key, int autostart);
  148. /**
  149. * Removes all persisted data related to the given ctkPluginArchiveSQL.
  150. * This is identical to removeArchive(QSharedPointer<ctkPluginArchive>).
  151. *
  152. * @throws ctkPluginDatabaseException
  153. */
  154. bool removeArchive(ctkPluginArchiveSQL* pa);
  155. private:
  156. enum TransactionType{Read, Write};
  157. /**
  158. * Opens the plugin database. If the database does not
  159. * yet exist, it is created using the path from getDatabasePath().
  160. *
  161. * @see setDatabasePath(const QString&)
  162. * @see getDatabasePath()
  163. * @see ctkPluginDatabaseException
  164. *
  165. * @throws ctkPluginDatabaseException
  166. */
  167. void open();
  168. /**
  169. * Checks if the database is open
  170. */
  171. bool isOpen() const;
  172. /**
  173. * Find position for ctkPluginArchive with specified id
  174. *
  175. * @param id Plugin archive id to find.
  176. * @return Position in the m_archives List.
  177. */
  178. int find(long id) const;
  179. /**
  180. * Find position for ctkPluginArchive
  181. *
  182. * @param id Plugin archive id to find.
  183. * @return Position in the m_archives List.
  184. */
  185. int find(ctkPluginArchive* pa) const;
  186. void initNextFreeIds();
  187. /**
  188. * Reads the persisted plugin data and creates a ctkPluginArchive object
  189. * for each plugin which is not in state UNINSTALLED.
  190. *
  191. * @throws ctkPluginDatabaseException
  192. */
  193. void restorePluginArchives();
  194. /**
  195. * Get load hints from the framework for plugins.
  196. */
  197. QLibrary::LoadHints getPluginLoadHints() const;
  198. /**
  199. * Helper method that creates the database tables:
  200. *
  201. * @throws ctkPluginDatabaseException
  202. */
  203. void createTables();
  204. bool dropTables();
  205. /**
  206. * Remove all plugins which have been marked as uninstalled
  207. * (startLevel == -2).
  208. */
  209. void cleanupDB();
  210. /**
  211. * Helper method that checks if all the expected tables exist in the database.
  212. *
  213. * Returns true if they all exist and false if any of them don't
  214. */
  215. bool checkTables() const;
  216. /**
  217. * Checks the database connection.
  218. *
  219. * @throws ctkPluginDatabaseException
  220. */
  221. void checkConnection() const;
  222. /**
  223. * Compares the persisted plugin modification time with the
  224. * file system modification time and updates the database
  225. * if the persisted data is outdated.
  226. *
  227. * This should only be called once when the database is initially opened.
  228. */
  229. void updateDB();
  230. void insertArchive(QSharedPointer<ctkPluginArchiveSQL> pa);
  231. void insertArchive(QSharedPointer<ctkPluginArchiveSQL> pa, QSqlQuery* query);
  232. void removeArchiveFromDB(ctkPluginArchiveSQL *pa, QSqlQuery *query);
  233. /**
  234. * Helper function that executes the sql query specified in \a statement.
  235. * It is assumed that the \a statement uses positional placeholders and
  236. * corresponding parameters are placed in the list of \a bindValues.
  237. *
  238. * Aside: This function may be safely called standalone or within an explicit
  239. * transaction. If called standalone, it's single query is implicitly
  240. * wrapped in it's own transaction.
  241. *
  242. * @throws ctkPluginDatabaseException
  243. */
  244. void executeQuery(QSqlQuery* query, const QString &statement, const QList<QVariant> &bindValues = QList<QVariant>()) const;
  245. /**
  246. * Begins a transcaction based on the \a type which can be Read or Write.
  247. *
  248. * @throws ctkPluginDatabaseException
  249. */
  250. void beginTransaction(QSqlQuery* query, TransactionType);
  251. /**
  252. * Commits a transaction
  253. *
  254. * @throws ctkPluginDatabaseException
  255. */
  256. void commitTransaction(QSqlQuery* query);
  257. /**
  258. * Rolls back a transaction
  259. *
  260. * @throws ctkPluginDatabaseException
  261. */
  262. void rollbackTransaction(QSqlQuery* query);
  263. /**
  264. * Returns a string representation of a QDateTime instance.
  265. */
  266. QString getStringFromQDateTime(const QDateTime& dateTime) const;
  267. /**
  268. * Returns a QDateTime from a string representation.
  269. */
  270. QDateTime getQDateTimeFromString(const QString& dateTimeString) const;
  271. QString m_databasePath;
  272. QString m_connectionName;
  273. bool m_isDatabaseOpen;
  274. bool m_inTransaction;
  275. QMutex m_archivesLock;
  276. /**
  277. * Plugin id sorted list of all active plugin archives.
  278. */
  279. QList<QSharedPointer<ctkPluginArchive> > m_archives;
  280. /**
  281. * Framework handle.
  282. */
  283. ctkPluginFrameworkContext* m_framework;
  284. /**
  285. * Keep track of the next free plug-in id
  286. */
  287. long m_nextFreeId;
  288. /**
  289. * Keep track of the next free generation for each plugin
  290. */
  291. QHash<int,int> /* <plugin id, generation> */ m_generations;
  292. };
  293. #endif // ctkPluginStorageSQL_P_H