MythTV  master
mythwebsocketevent.cpp
Go to the documentation of this file.
2 #include "mythcorecontext.h"
3 
5 {
6  setObjectName("MythWebSocketEvent");
8 }
9 
11 {
13 }
14 
16 {
17  // TODO: Payloads potentially has multiple messages to process
18  // We only handle the first one here.
20  (*p_message).append(*(Payloads.front()));
21  return HandleTextMessage(p_message);
22 }
23 
25 {
26  QString message = *Text;
27 
28  if (message.isEmpty())
29  return false;
30 
31  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
32  if (tokens[0] == "WS_EVENT_ENABLE") // Only send events if asked
33  {
34  m_sendEvents = true;
35  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Enabled");
36  }
37  else if (tokens[0] == "WS_EVENT_DISABLE")
38  {
39  m_sendEvents = false;
40  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Disabled");
41  }
42  else if (tokens[0] == "WS_EVENT_SET_FILTER")
43  {
44  m_filters.clear();
45 
46  if (tokens.length() == 1)
47  return true;
48 
49  m_filters = tokens.mid(1);
50 
51  QString filterString = m_filters.join(", ");
52  LOG(VB_HTTP, LOG_NOTICE, QString("WebSocketMythEvent: Updated filters (%1)").arg(filterString));
53  }
54 
55  return false;
56 }
57 
59 {
60  if (event->type() == MythEvent::kMythEventMessage)
61  {
62  if (!m_sendEvents)
63  return;
64 
65  auto *me = dynamic_cast<MythEvent *>(event);
66  if (me == nullptr)
67  return;
68  QString message = me->Message();
69 
70  if (message.startsWith("SYSTEM_EVENT"))
71  message.remove(0, 13); // Strip SYSTEM_EVENT from the frontend, it's not useful
72 
73  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
74  if (tokens.isEmpty())
75  return;
76 
77  // If no-one is listening for this event, then ignore it
78  if (!m_filters.contains("ALL") && !m_filters.contains(tokens[0]))
79  return;
80 
81  emit SendTextMessage(message);
82  }
83 }
StringPayload
std::shared_ptr< MythSharedString > StringPayload
Definition: mythhttpcommon.h:56
MythEvent::kMythEventMessage
static const Type kMythEventMessage
Definition: mythevent.h:79
MythWebSocketEvent::m_filters
QStringList m_filters
Definition: mythwebsocketevent.h:25
MythSharedString::CreateString
static StringPayload CreateString()
Definition: mythhttpcommon.cpp:37
MythWebSocketEvent::customEvent
void customEvent(QEvent *) override
Definition: mythwebsocketevent.cpp:58
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythWebSocketEvent::HandleRawTextMessage
bool HandleRawTextMessage(const DataPayloads &Payloads)
Definition: mythwebsocketevent.cpp:15
MythEvent::Message
const QString & Message() const
Definition: mythevent.h:65
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
MythWebSocketEvent::MythWebSocketEvent
MythWebSocketEvent()
Definition: mythwebsocketevent.cpp:4
MythWebSocketEvent::SendTextMessage
void SendTextMessage(const QString &)
MythWebSocketEvent::HandleTextMessage
bool HandleTextMessage(const StringPayload &Text)
Definition: mythwebsocketevent.cpp:24
MythWebSocketEvent::~MythWebSocketEvent
~MythWebSocketEvent() override
Definition: mythwebsocketevent.cpp:10
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
mythcorecontext.h
mythwebsocketevent.h
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
MythWebSocketEvent::m_sendEvents
bool m_sendEvents
Definition: mythwebsocketevent.h:26
DataPayloads
std::vector< DataPayload > DataPayloads
Definition: mythhttpcommon.h:37