ctkEAHandlerTask.tpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <service/event/ctkEventHandler.h>
  16. #include <ctkEventAdminActivator_p.h>
  17. #include <handler/ctkEABlacklistingHandlerTasks_p.h>
  18. template<class BlacklistingHandlerTasks>
  19. class ctkEAHandlerTask<BlacklistingHandlerTasks>::_GetAndUngetEventHandler
  20. {
  21. public:
  22. _GetAndUngetEventHandler(BlacklistingHandlerTasks* handlerTasks, const ctkServiceReference& ref)
  23. : handlerTasks(handlerTasks), currHandler(0), currRef(ref)
  24. {
  25. currHandler = handlerTasks->getEventHandler(currRef);
  26. }
  27. ~_GetAndUngetEventHandler()
  28. {
  29. handlerTasks->ungetEventHandler(currHandler, currRef);
  30. }
  31. QObject* getObject() const
  32. {
  33. return dynamic_cast<QObject*>(currHandler);
  34. }
  35. ctkEventHandler* getHandler() const
  36. {
  37. return currHandler;
  38. }
  39. private:
  40. BlacklistingHandlerTasks* handlerTasks;
  41. ctkEventHandler* currHandler;
  42. ctkServiceReference currRef;
  43. };
  44. template<class BlacklistingHandlerTasks>
  45. ctkEAHandlerTask<BlacklistingHandlerTasks>::ctkEAHandlerTask(const ctkServiceReference& eventHandlerRef,
  46. const ctkEvent& event, BlacklistingHandlerTasks* handlerTasks)
  47. : eventHandlerRef(eventHandlerRef), event(event), handlerTasks(handlerTasks)
  48. {
  49. }
  50. template<class BlacklistingHandlerTasks>
  51. ctkEAHandlerTask<BlacklistingHandlerTasks>::ctkEAHandlerTask(const Self& task)
  52. : eventHandlerRef(task.eventHandlerRef), event(task.event),
  53. handlerTasks(task.handlerTasks)
  54. {
  55. }
  56. template<class BlacklistingHandlerTasks>
  57. ctkEAHandlerTask<BlacklistingHandlerTasks>&
  58. ctkEAHandlerTask<BlacklistingHandlerTasks>::operator=(const Self& task)
  59. {
  60. eventHandlerRef = task.eventHandlerRef;
  61. event = task.event;
  62. handlerTasks = task.handlerTasks;
  63. return *this;
  64. }
  65. template<class BlacklistingHandlerTasks>
  66. QString ctkEAHandlerTask<BlacklistingHandlerTasks>::getHandlerClassName() const
  67. {
  68. QObject* handler = _GetAndUngetEventHandler(handlerTasks, eventHandlerRef).getObject();
  69. return handler->metaObject()->className();
  70. }
  71. template<class BlacklistingHandlerTasks>
  72. void ctkEAHandlerTask<BlacklistingHandlerTasks>::execute()
  73. {
  74. // Get the service object
  75. ctkEventHandler* const handler = _GetAndUngetEventHandler(handlerTasks, eventHandlerRef).getHandler();
  76. try
  77. {
  78. handler->handleEvent(event);
  79. }
  80. catch (const std::exception& e)
  81. {
  82. // The spec says that we must catch exceptions and log them:
  83. CTK_WARN_SR_EXC(ctkEventAdminActivator::getLogService(), eventHandlerRef, &e)
  84. << "Exception during event dispatch [" << event.getTopic() << "| Plugin("
  85. << eventHandlerRef.getPlugin()->getSymbolicName() << ")]";
  86. }
  87. }
  88. template<class BlacklistingHandlerTasks>
  89. void ctkEAHandlerTask<BlacklistingHandlerTasks>::blackListHandler()
  90. {
  91. handlerTasks->blackListRef(eventHandlerRef);
  92. }