ctkPluginFrameworkEvent.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "ctkPluginFrameworkEvent.h"
  16. #include "ctkPlugin.h"
  17. #include <QString>
  18. #include <QDebug>
  19. class ctkPluginFrameworkEventData : public QSharedData
  20. {
  21. public:
  22. ctkPluginFrameworkEventData(ctkPluginFrameworkEvent::Type type, QSharedPointer<ctkPlugin> plugin, const QString& exc)
  23. : plugin(plugin), errorString(exc), type(type)
  24. {
  25. }
  26. ctkPluginFrameworkEventData(const ctkPluginFrameworkEventData& other)
  27. : QSharedData(other), plugin(other.plugin), errorString(other.errorString),
  28. type(other.type)
  29. {
  30. }
  31. /**
  32. * Plugin related to the event.
  33. */
  34. const QSharedPointer<ctkPlugin> plugin;
  35. /**
  36. * Exception related to the event.
  37. */
  38. const QString errorString;
  39. /**
  40. * Type of event.
  41. */
  42. const ctkPluginFrameworkEvent::Type type;
  43. };
  44. //----------------------------------------------------------------------------
  45. ctkPluginFrameworkEvent::ctkPluginFrameworkEvent()
  46. : d(0)
  47. {
  48. }
  49. //----------------------------------------------------------------------------
  50. ctkPluginFrameworkEvent::~ctkPluginFrameworkEvent()
  51. {
  52. }
  53. //----------------------------------------------------------------------------
  54. bool ctkPluginFrameworkEvent::isNull() const
  55. {
  56. return !d;
  57. }
  58. //----------------------------------------------------------------------------
  59. ctkPluginFrameworkEvent::ctkPluginFrameworkEvent(Type type, QSharedPointer<ctkPlugin> plugin, const std::exception& fwException)
  60. : d(new ctkPluginFrameworkEventData(type, plugin, fwException.what()))
  61. {
  62. }
  63. //----------------------------------------------------------------------------
  64. ctkPluginFrameworkEvent::ctkPluginFrameworkEvent(Type type, QSharedPointer<ctkPlugin> plugin)
  65. : d(new ctkPluginFrameworkEventData(type, plugin, QString()))
  66. {
  67. }
  68. //----------------------------------------------------------------------------
  69. ctkPluginFrameworkEvent::ctkPluginFrameworkEvent(const ctkPluginFrameworkEvent& other)
  70. : d(other.d)
  71. {
  72. }
  73. //----------------------------------------------------------------------------
  74. ctkPluginFrameworkEvent& ctkPluginFrameworkEvent::operator=(const ctkPluginFrameworkEvent& other)
  75. {
  76. d = other.d;
  77. return *this;
  78. }
  79. //----------------------------------------------------------------------------
  80. QString ctkPluginFrameworkEvent::getErrorString() const
  81. {
  82. return d->errorString;
  83. }
  84. //----------------------------------------------------------------------------
  85. QSharedPointer<ctkPlugin> ctkPluginFrameworkEvent::getPlugin() const
  86. {
  87. return d->plugin;
  88. }
  89. //----------------------------------------------------------------------------
  90. ctkPluginFrameworkEvent::Type ctkPluginFrameworkEvent::getType() const
  91. {
  92. return d->type;
  93. }
  94. //----------------------------------------------------------------------------
  95. QDebug operator<<(QDebug dbg, ctkPluginFrameworkEvent::Type type)
  96. {
  97. switch (type)
  98. {
  99. case ctkPluginFrameworkEvent::FRAMEWORK_STARTED: return dbg << "FRAMEWORK_STARTED";
  100. case ctkPluginFrameworkEvent::PLUGIN_ERROR: return dbg << "PLUGIN_ERROR";
  101. case ctkPluginFrameworkEvent::PLUGIN_WARNING: return dbg << "PLUGIN_WARNING";
  102. case ctkPluginFrameworkEvent::PLUGIN_INFO: return dbg << "PLUGIN_INFO";
  103. case ctkPluginFrameworkEvent::FRAMEWORK_STOPPED: return dbg << "FRAMEWORK_STOPPED";
  104. case ctkPluginFrameworkEvent::FRAMEWORK_STOPPED_UPDATE: return dbg << "FRAMEWORK_STOPPED_UPDATE";
  105. case ctkPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT: return dbg << "FRAMEWORK_WATI_TIMEDOUT";
  106. default: return dbg << "unknown plugin framework event type (" << static_cast<int>(type) << ")";
  107. }
  108. }
  109. //----------------------------------------------------------------------------
  110. QDebug operator<<(QDebug dbg, const ctkPluginFrameworkEvent& event)
  111. {
  112. if (event.isNull()) return dbg << "NONE";
  113. ctkPlugin* p = event.getPlugin().data();
  114. QString err = event.getErrorString();
  115. dbg.nospace() << event.getType() << " #" << p->getPluginId() << " ("
  116. << p->getLocation() << ")" << (err.isEmpty() ? "" : " exception: ") << err;
  117. return dbg.maybeSpace();
  118. }