ctkPluginStorage.cpp 4.1 KB

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