ctkPluginStorage.cpp 4.4 KB

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