ctkPluginStorageSQL_p.h 9.1 KB

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