ctkEventHandlerWrapper_p.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef CTKEVENTHANDLERWRAPPER_P_H
  16. #define CTKEVENTHANDLERWRAPPER_P_H
  17. #include <QStringList>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <service/event/ctkEventConstants.h>
  20. #include <ctkLDAPSearchFilter.h>
  21. #include <iostream>
  22. class ctkEventHandlerWrapper : public QObject {
  23. Q_OBJECT
  24. private:
  25. ctkProperties properties;
  26. QStringList topicList;
  27. ctkLDAPSearchFilter filter;
  28. public:
  29. ctkEventHandlerWrapper(const QObject* subscriber, const char* handler, const ctkProperties& properties)
  30. : properties(properties)
  31. {
  32. connect(this, SIGNAL(notifySubscriber(ctkEvent)), subscriber, handler);
  33. }
  34. QStringList topics() const
  35. {
  36. return topicList;
  37. }
  38. bool init()
  39. {
  40. topicList.clear();
  41. // Get topic names
  42. QVariant v = properties[ctkEventConstants::EVENT_TOPIC];
  43. topicList = v.toStringList();
  44. if (topicList.empty())
  45. {
  46. return false;
  47. }
  48. v = properties[ctkEventConstants::EVENT_FILTER];
  49. filter = ctkLDAPSearchFilter(v.toString());
  50. return true;
  51. }
  52. void handleEvent(const ctkEvent& event /*, const Permission& perm */)
  53. {
  54. if (!event.matches(filter)) return;
  55. // should do permissions checks now somehow
  56. // ...
  57. try {
  58. emit notifySubscriber(event);
  59. }
  60. catch (const std::exception& e)
  61. {
  62. // TODO logging
  63. std::cerr << "Exception occured during publishing " << qPrintable(event.getTopic()) << ": " << e.what() << std::endl;
  64. }
  65. }
  66. Q_SIGNALS:
  67. void notifySubscriber(const ctkEvent&);
  68. };
  69. #endif // CTKEVENTHANDLERWRAPPER_P_H