ctkPluginStorageSQL_p.h 9.9 KB

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