ctkPluginDatabase_p.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 CTKPLUGINDATABASE_P_H
  16. #define CTKPLUGINDATABASE_P_H
  17. #include <QtSql>
  18. #include <QList>
  19. // CTK class forward declarations
  20. class ctkPluginStorage;
  21. class ctkPluginArchive;
  22. /**
  23. * \ingroup PluginFramework
  24. */
  25. class ctkPluginDatabase
  26. {
  27. public:
  28. ctkPluginDatabase(ctkPluginStorage* storage);
  29. virtual ~ctkPluginDatabase();
  30. /**
  31. * Opens the plugin database. If the database does not
  32. * yet exist, it is created using the path from getDatabasePath().
  33. *
  34. * @see setDatabasePath(const QString&)
  35. * @see getDatabasePath()
  36. * @see ctkPluginDatabaseException
  37. *
  38. * @throws ctkPluginDatabaseException
  39. */
  40. void open();
  41. /**
  42. * Closes the plugin database. Throws a ctkPluginDatabaseException
  43. * of type DB_CONNECTION_INVALID if the database is invalid.
  44. *
  45. * @throws ctkPluginDatabaseException
  46. */
  47. void close();
  48. /**
  49. * Checks if the database is open
  50. */
  51. bool isOpen() const;
  52. /**
  53. * Sets the path of the service database to \a databasePath
  54. */
  55. void setDatabasePath(const QString &databasePath);
  56. /**
  57. * Returns the path of the plugin database
  58. */
  59. QString getDatabasePath() const;
  60. /**
  61. * Get a Qt resource cached in the database. The resource path \a res
  62. * must be relative to the plugin specific resource prefix, but may
  63. * start with a '/'.
  64. *
  65. * @param pluginId The id of the plugin from which to get the resource
  66. * @param res The path to the resource in the plugin
  67. * @return The byte array of the cached resource
  68. *
  69. * @throws ctkPluginDatabaseException
  70. */
  71. QByteArray getPluginResource(long pluginId, const QString& res) const;
  72. /**
  73. * Get a list of resource entries under the given path.
  74. *
  75. * @param pluginId The id of the plugin from which to get the entries
  76. * @param path A resource path relative to the plugin specific resource prefix.
  77. * @return A QStringList containing the cached resource entries.
  78. *
  79. * @throws ctkPluginDatabaseException
  80. */
  81. QStringList findResourcesPath(long pluginId, const QString& path) const;
  82. /**
  83. * Inserts a new plugin into the database. This method assumes that
  84. * the an entry with the same \a location and \a localPath does not
  85. * yet exist in the database.
  86. *
  87. * @param location The URL to the plugin.
  88. * @param localPath The path to the plugin library on the local file system.
  89. * @param createArchive If \c true (default) a new ctkPluginArchive instance is returned.
  90. *
  91. * @throws ctkPluginDatabaseException
  92. */
  93. ctkPluginArchive* insertPlugin(const QUrl& location, const QString& localPath, bool createArchive = true);
  94. /**
  95. * Removes all persisted data related to the given ctkPluginArchive.
  96. *
  97. * @throws ctkPluginDatabaseException
  98. */
  99. void removeArchive(const ctkPluginArchive* pa);
  100. /**
  101. * Persist the start level
  102. *
  103. * @param pluginId The Plugin id
  104. * @param startLevel The new start level
  105. */
  106. void setStartLevel(long pluginId, int startLevel);
  107. /**
  108. * Persist the last modification (state change) time
  109. *
  110. * @param pluginId The Plugin id
  111. * @param lastModified The modification time
  112. */
  113. void setLastModified(long pluginId, const QDateTime& lastModified);
  114. /**
  115. * Persist the auto start setting.
  116. *
  117. * @param pluginId The Plugin id
  118. * @param autostart The new auto start setting
  119. */
  120. void setAutostartSetting(long pluginId, int autostart);
  121. /**
  122. * Reads the persisted plugin data and returns a ctkPluginArchive object
  123. * for each plugin which is not in state UNINSTALLED.
  124. *
  125. * @throws ctkPluginDatabaseException
  126. */
  127. QList<ctkPluginArchive*> getPluginArchives() const;
  128. private:
  129. enum TransactionType{Read, Write};
  130. /**
  131. * Get load hints from the framework for plugins.
  132. */
  133. QLibrary::LoadHints getPluginLoadHints() const;
  134. /**
  135. * Helper method that creates the database tables:
  136. *
  137. * @throws ctkPluginDatabaseException
  138. */
  139. void createTables();
  140. bool dropTables();
  141. /**
  142. * Remove all plugins which have been marked as uninstalled
  143. * (startLevel == -2).
  144. */
  145. void removeUninstalledPlugins();
  146. /**
  147. * Helper method that checks if all the expected tables exist in the database.
  148. *
  149. * Returns true if they all exist and false if any of them don't
  150. */
  151. bool checkTables() const;
  152. /**
  153. * Checks the database connection.
  154. *
  155. * @throws ctkPluginDatabaseException
  156. */
  157. void checkConnection() const;
  158. /**
  159. * Compares the persisted plugin modification time with the
  160. * file system modification time and updates the database
  161. * if the persisted data is outdated.
  162. *
  163. * This should only be called once when the database is initially opened.
  164. */
  165. void updateDB();
  166. /**
  167. * Helper function that executes the sql query specified in \a statement.
  168. * It is assumed that the \a statement uses positional placeholders and
  169. * corresponding parameters are placed in the list of \a bindValues.
  170. *
  171. * Aside: This function may be safely called standalone or within an explicit
  172. * transaction. If called standalone, it's single query is implicitly
  173. * wrapped in it's own transaction.
  174. *
  175. * @throws ctkPluginDatabaseException
  176. */
  177. void executeQuery(QSqlQuery* query, const QString &statement, const QList<QVariant> &bindValues = QList<QVariant>()) const;
  178. /**
  179. * Begins a transcaction based on the \a type which can be Read or Write.
  180. *
  181. * @throws ctkPluginDatabaseException
  182. */
  183. void beginTransaction(QSqlQuery* query, TransactionType);
  184. /**
  185. * Commits a transaction
  186. *
  187. * @throws ctkPluginDatabaseException
  188. */
  189. void commitTransaction(QSqlQuery* query);
  190. /**
  191. * Rolls back a transaction
  192. *
  193. * @throws ctkPluginDatabaseException
  194. */
  195. void rollbackTransaction(QSqlQuery* query);
  196. /**
  197. * Returns a string representation of a QDateTime instance.
  198. */
  199. QString getStringFromQDateTime(const QDateTime& dateTime) const;
  200. /**
  201. * Returns a QDateTime from a string representation.
  202. */
  203. QDateTime getQDateTimeFromString(const QString& dateTimeString) const;
  204. QString m_databasePath;
  205. QString m_connectionName;
  206. bool m_isDatabaseOpen;
  207. bool m_inTransaction;
  208. ctkPluginStorage* m_PluginStorage;
  209. };
  210. #endif // CTKPLUGINDATABASE_P_H