ctkPluginDatabase_p.h 6.7 KB

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