ctkPluginStorage.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginDatabaseException.h"
  24. namespace ctk {
  25. PluginStorage::PluginStorage(PluginFrameworkContext* framework)
  26. : framework(framework), pluginDatabase(this)
  27. {
  28. // // See if we have a storage database
  29. // bundlesDir = Util.getFileStorage(framework, "bs");
  30. // if (bundlesDir == null) {
  31. // throw RuntimeException("No plugin storage area available!");
  32. // }
  33. pluginDatabase.open();
  34. archives << pluginDatabase.getPluginArchives();
  35. }
  36. PluginArchive* PluginStorage::insertPlugin(const QUrl& location, const QString& localPath)
  37. {
  38. PluginArchive* pa = pluginDatabase.insertPlugin(location, localPath);
  39. archives.push_back(pa);
  40. return pa;
  41. }
  42. PluginArchive* PluginStorage::updatePluginArchive(PluginArchive* old, const QString& localPath)
  43. {
  44. //return new BundleArchiveImpl((BundleArchiveImpl)old, is);
  45. return 0;
  46. }
  47. void PluginStorage::replacePluginArchive(PluginArchive* oldPA, PluginArchive* newPA)
  48. {
  49. // int pos;
  50. // long id = oldBA.getBundleId();
  51. // synchronized (archives) {
  52. // pos = find(id);
  53. // if (pos >= archives.size() || archives.get(pos) != oldBA) {
  54. // throw new Exception("replaceBundleJar: Old bundle archive not found, pos=" + pos);
  55. // }
  56. // archives.set(pos, newBA);
  57. // }
  58. }
  59. QList<PluginArchive*> PluginStorage::getAllPluginArchives() const
  60. {
  61. return archives;
  62. }
  63. QList<QString> PluginStorage::getStartOnLaunchPlugins()
  64. {
  65. QList<QString> res;
  66. QListIterator<PluginArchive*> i(archives);
  67. while(i.hasNext())
  68. {
  69. PluginArchive* pa = i.next();
  70. if (pa->getAutostartSetting() != -1)
  71. {
  72. res.push_back(pa->getPluginLocation().toString());
  73. }
  74. }
  75. return res;
  76. }
  77. PluginStorage::~PluginStorage()
  78. {
  79. close();
  80. }
  81. void PluginStorage::close()
  82. {
  83. pluginDatabase.close();
  84. qDeleteAll(archives);
  85. }
  86. bool PluginStorage::removeArchive(PluginArchive* pa)
  87. {
  88. QMutexLocker lock(&archivesLock);
  89. bool removed = false;
  90. try
  91. {
  92. pluginDatabase.removeArchive(pa);
  93. removed = archives.removeAll(pa);
  94. delete pa;
  95. }
  96. catch (const PluginDatabaseException& exc)
  97. {
  98. qDebug() << "Removing plugin archive failed:" << exc;
  99. removed = false;
  100. }
  101. return removed;
  102. }
  103. QByteArray PluginStorage::getPluginResource(long pluginId, const QString& res) const
  104. {
  105. try
  106. {
  107. return pluginDatabase.getPluginResource(pluginId, res);
  108. }
  109. catch (const PluginDatabaseException& exc)
  110. {
  111. qDebug() << QString("Getting plugin resource %1 failed:").arg(res) << exc;
  112. return QByteArray();
  113. }
  114. }
  115. QStringList PluginStorage::findResourcesPath(long pluginId, const QString& path) const
  116. {
  117. try
  118. {
  119. return pluginDatabase.findResourcesPath(pluginId, path);
  120. }
  121. catch (const PluginDatabaseException& exc)
  122. {
  123. qDebug() << QString("Getting plugin resource paths for %1 failed:").arg(path) << exc;
  124. return QStringList();
  125. }
  126. }
  127. }