ctkEventHandlerWrapper_p.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef CTKEVENTHANDLERWRAPPER_P_H
  2. #define CTKEVENTHANDLERWRAPPER_P_H
  3. #include <QStringList>
  4. #include <EventBus/ctkEventBus.h>
  5. #include <EventBus/ctkEventConstants.h>
  6. #include <PluginFramework/ctkLDAPSearchFilter.h>
  7. #include <iostream>
  8. namespace ctk {
  9. class EventHandlerWrapper : public QObject {
  10. Q_OBJECT
  11. private:
  12. EventBus::Properties properties;
  13. QStringList topicList;
  14. LDAPSearchFilter filter;
  15. public:
  16. EventHandlerWrapper(const QObject* subscriber, const char* handler, const EventBus::Properties& properties)
  17. : properties(properties)
  18. {
  19. connect(this, SIGNAL(notifySubscriber(Event)), subscriber, handler);
  20. }
  21. QStringList topics() const
  22. {
  23. return topicList;
  24. }
  25. bool init()
  26. {
  27. topicList.clear();
  28. // Get topic names
  29. QVariant v = properties[EventConstants::EVENT_TOPIC];
  30. topicList = v.toStringList();
  31. if (topicList.empty())
  32. {
  33. return false;
  34. }
  35. v = properties[EventConstants::EVENT_FILTER];
  36. filter = LDAPSearchFilter(v.toString());
  37. }
  38. void handleEvent(const Event& event /*, const Permission& perm */)
  39. {
  40. if (!event.matches(filter)) return;
  41. // should do permissions checks now somehow
  42. // ...
  43. try {
  44. emit notifySubscriber(event);
  45. }
  46. catch (const std::exception& e)
  47. {
  48. // TODO logging
  49. std::cerr << "Exception occured during publishing " << qPrintable(event.topic()) << ": " << e.what() << std::endl;
  50. }
  51. }
  52. signals:
  53. void notifySubscriber(const Event&);
  54. };
  55. }
  56. #endif // CTKEVENTHANDLERWRAPPER_P_H