ctkPluginStorage.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "ctkPluginStorage_p.h"
  16. #include <QPluginLoader>
  17. #include <QFileInfo>
  18. #include <QUrl>
  19. #include <QDir>
  20. // CTK includes
  21. #include "ctkPluginArchive_p.h"
  22. #include "ctkPluginFrameworkContext_p.h"
  23. #include "ctkPluginFrameworkUtil_p.h"
  24. #include "ctkPluginDatabaseException.h"
  25. //----------------------------------------------------------------------------
  26. ctkPluginStorage::ctkPluginStorage(ctkPluginFrameworkContext* framework)
  27. : framework(framework), pluginDatabase(this)
  28. {
  29. // See if we have a storage database
  30. QString path = ctkPluginFrameworkUtil::getFileStorage(framework, "").absoluteFilePath("plugins.db");
  31. pluginDatabase.setDatabasePath(path);
  32. pluginDatabase.open();
  33. archives << pluginDatabase.getPluginArchives();
  34. }
  35. //----------------------------------------------------------------------------
  36. ctkPluginFrameworkContext* ctkPluginStorage::getFrameworkContext() const
  37. {
  38. return framework;
  39. }
  40. //----------------------------------------------------------------------------
  41. ctkPluginArchive* ctkPluginStorage::insertPlugin(const QUrl& location, const QString& localPath)
  42. {
  43. ctkPluginArchive* pa = pluginDatabase.insertPlugin(location, localPath);
  44. archives.push_back(pa);
  45. return pa;
  46. }
  47. //----------------------------------------------------------------------------
  48. ctkPluginArchive* ctkPluginStorage::updatePluginArchive(ctkPluginArchive* old, const QString& localPath)
  49. {
  50. Q_UNUSED(old)
  51. Q_UNUSED(localPath)
  52. //TODO: updatePluginArchive
  53. //return new BundleArchiveImpl((BundleArchiveImpl)old, is);
  54. return 0;
  55. }
  56. //----------------------------------------------------------------------------
  57. void ctkPluginStorage::replacePluginArchive(ctkPluginArchive* oldPA, ctkPluginArchive* newPA)
  58. {
  59. Q_UNUSED(oldPA)
  60. Q_UNUSED(newPA)
  61. //TODO: replacePluginArchive
  62. // int pos;
  63. // long id = oldBA.getBundleId();
  64. // synchronized (archives) {
  65. // pos = find(id);
  66. // if (pos >= archives.size() || archives.get(pos) != oldBA) {
  67. // throw new Exception("replaceBundleJar: Old bundle archive not found, pos=" + pos);
  68. // }
  69. // archives.set(pos, newBA);
  70. // }
  71. }
  72. //----------------------------------------------------------------------------
  73. void ctkPluginStorage::setStartLevel(ctkPluginArchive* pa)
  74. {
  75. pluginDatabase.setStartLevel(pa->getPluginId(), pa->getStartLevel());
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkPluginStorage::setLastModified(ctkPluginArchive* pa)
  79. {
  80. pluginDatabase.setLastModified(pa->getPluginId(), pa->getLastModified());
  81. }
  82. //----------------------------------------------------------------------------
  83. void ctkPluginStorage::setAutostartSetting(ctkPluginArchive* pa)
  84. {
  85. pluginDatabase.setAutostartSetting(pa->getPluginId(), pa->getAutostartSetting());
  86. }
  87. //----------------------------------------------------------------------------
  88. QList<ctkPluginArchive*> ctkPluginStorage::getAllPluginArchives() const
  89. {
  90. return archives;
  91. }
  92. //----------------------------------------------------------------------------
  93. QList<QString> ctkPluginStorage::getStartOnLaunchPlugins()
  94. {
  95. QList<QString> res;
  96. QListIterator<ctkPluginArchive*> i(archives);
  97. while(i.hasNext())
  98. {
  99. ctkPluginArchive* pa = i.next();
  100. if (pa->getAutostartSetting() != -1)
  101. {
  102. res.push_back(pa->getPluginLocation().toString());
  103. }
  104. }
  105. return res;
  106. }
  107. //----------------------------------------------------------------------------
  108. ctkPluginStorage::~ctkPluginStorage()
  109. {
  110. close();
  111. // ctkPluginArchive pointers in archives list are deleted
  112. // in ~ctkPluginPrivate()
  113. }
  114. //----------------------------------------------------------------------------
  115. void ctkPluginStorage::close()
  116. {
  117. pluginDatabase.close();
  118. }
  119. //----------------------------------------------------------------------------
  120. bool ctkPluginStorage::removeArchive(ctkPluginArchive* pa)
  121. {
  122. QMutexLocker lock(&archivesLock);
  123. bool removed = false;
  124. try
  125. {
  126. pluginDatabase.removeArchive(pa);
  127. removed = archives.removeAll(pa);
  128. }
  129. catch (const ctkPluginDatabaseException& exc)
  130. {
  131. qDebug() << "Removing plugin archive failed:" << exc;
  132. removed = false;
  133. }
  134. return removed;
  135. }
  136. //----------------------------------------------------------------------------
  137. QByteArray ctkPluginStorage::getPluginResource(long pluginId, const QString& res) const
  138. {
  139. try
  140. {
  141. return pluginDatabase.getPluginResource(pluginId, res);
  142. }
  143. catch (const ctkPluginDatabaseException& exc)
  144. {
  145. qDebug() << QString("Getting plugin resource %1 failed:").arg(res) << exc;
  146. return QByteArray();
  147. }
  148. }
  149. //----------------------------------------------------------------------------
  150. QStringList ctkPluginStorage::findResourcesPath(long pluginId, const QString& path) const
  151. {
  152. try
  153. {
  154. return pluginDatabase.findResourcesPath(pluginId, path);
  155. }
  156. catch (const ctkPluginDatabaseException& exc)
  157. {
  158. qDebug() << QString("Getting plugin resource paths for %1 failed:").arg(path) << exc;
  159. return QStringList();
  160. }
  161. }