Base: Add system tray notifications

Final "UI" part of the plugin to be completed. I'm annoyed at how
many includes are required in order to implement this feature. It
breaks quite a bit of the modularity of the plugin because suddenly
everything has to include obs libraries (for translations)
This commit is contained in:
tt2468 2021-06-13 03:58:15 -07:00
parent bbc504ce72
commit c9619ce215
4 changed files with 58 additions and 4 deletions

View File

@ -35,10 +35,12 @@ OBSWebSocket.ConnectInfo.ServerPassword="Server Password"
OBSWebSocket.ConnectInfo.ServerPasswordPlaceholderText="[Auth Disabled]"
OBSWebSocket.ConnectInfo.QrTitle="Connect QR"
OBSWebSocket.NotifyConnect.Title="New WebSocket connection."
OBSWebSocket.NotifyConnect.Message="Client %1 connected."
OBSWebSocket.NotifyDisconnect.Title="WebSocket client disconnected."
OBSWebSocket.NotifyDisconnect.Message="Client %1 disconnected"
OBSWebSocket.TrayNotification.Identified.Title="New WebSocket Connection"
OBSWebSocket.TrayNotification.Identified.Body="Client %1 identified."
OBSWebSocket.TrayNotification.AuthenticationFailed.Title="WebSocket Authentication Failure"
OBSWebSocket.TrayNotification.AuthenticationFailed.Body="Client %1 failed to authenticate."
OBSWebSocket.TrayNotification.Disconnected.Title="WebSocket Client Disconnected"
OBSWebSocket.TrayNotification.Disconnected.Body="Client %1 disconnected."
OBSWebSocket.Server.StartFailed.Title="WebSocket Server Failure"
OBSWebSocket.Server.StartFailed.Message="The WebSocket server failed to start. TCP port %1 may already be in use elsewhere on this system by another application. Try setting a different TCP port in the WebSocket server settings, or stop any application that could be using this port.\n Error message: %2"

View File

@ -1,7 +1,11 @@
#include <obs-module.h>
#include "WebSocketProtocol.h"
#include "requesthandler/RequestHandler.h"
#include "requesthandler/rpc/RequestStatus.h"
#include "obs-websocket.h"
#include "Config.h"
#include "utils/Utils.h"
#include "plugin-macros.generated.h"
@ -185,6 +189,14 @@ WebSocketProtocol::ProcessResult WebSocketProtocol::ProcessMessage(SessionPtr se
return ret;
}
if (!Utils::Crypto::CheckAuthenticationString(session->Secret(), session->Challenge(), incomingMessage["authentication"])) {
auto conf = GetConfig();
if (conf && conf->AlertsEnabled) {
obs_frontend_push_ui_translation(obs_module_get_string);
QString title = QObject::tr("OBSWebSocket.TrayNotification.AuthenticationFailed.Title");
QString body = QObject::tr("OBSWebSocket.TrayNotification.AuthenticationFailed.Body");
obs_frontend_pop_ui_translation();
Utils::Platform::SendTrayNotification(QSystemTrayIcon::Warning, title, body);
}
ret.closeCode = WebSocketServer::WebSocketCloseCode::AuthenticationFailed;
ret.closeReason = "Authentication failed.";
return ret;
@ -211,6 +223,15 @@ WebSocketProtocol::ProcessResult WebSocketProtocol::ProcessMessage(SessionPtr se
session->SetIsIdentified(true);
auto conf = GetConfig();
if (conf && conf->AlertsEnabled) {
obs_frontend_push_ui_translation(obs_module_get_string);
QString title = QObject::tr("OBSWebSocket.TrayNotification.Identified.Title");
QString body = QObject::tr("OBSWebSocket.TrayNotification.Identified.Body");
obs_frontend_pop_ui_translation();
Utils::Platform::SendTrayNotification(QSystemTrayIcon::Information, title, body);
}
ret.result["messageType"] = "Identified";
ret.result["negotiatedRpcVersion"] = session->RpcVersion();
return ret;

View File

@ -3,6 +3,9 @@
#include <QtConcurrent>
#include <QDateTime>
#include <obs-module.h>
#include <obs-frontend-api.h>
#include "obs-websocket.h"
#include "WebSocketServer.h"
@ -284,6 +287,17 @@ void WebSocketServer::onOpen(websocketpp::connection_hdl hdl)
sessionLock.unlock();
// Build SessionState object for signal
WebSocketSessionState state;
state.remoteAddress = session->RemoteAddress();
state.connectedAt = session->ConnectedAt();
state.incomingMessages = session->IncomingMessages();
state.outgoingMessages = session->OutgoingMessages();
state.isIdentified = session->IsIdentified();
// Emit signals
emit ClientConnected(state);
// Send object to client
websocketpp::lib::error_code errorCode;
auto sessionEncoding = session->Encoding();
@ -323,6 +337,22 @@ void WebSocketServer::onClose(websocketpp::connection_hdl hdl)
// Emit signals
emit ClientDisconnected(state, conn->get_local_close_code());
// Get config for tray notification
auto conf = GetConfig();
if (!conf) {
blog(LOG_ERROR, "[WebSocketServer::onClose] Unable to retreive config!");
return;
}
// If previously identified, not going away, and notifications enabled, send a tray notification
if (isIdentified && (conn->get_local_close_code() != websocketpp::close::status::going_away) && conf->AlertsEnabled) {
obs_frontend_push_ui_translation(obs_module_get_string);
QString title = QObject::tr("OBSWebSocket.TrayNotification.Disconnected.Title");
QString body = QObject::tr("OBSWebSocket.TrayNotification.Disconnected.Body");
obs_frontend_pop_ui_translation();
Utils::Platform::SendTrayNotification(QSystemTrayIcon::Information, title, body);
}
}
void WebSocketServer::onMessage(websocketpp::connection_hdl hdl, websocketpp::server<websocketpp::config::asio>::message_ptr message)

View File

@ -82,6 +82,7 @@ class WebSocketServer : QObject
void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr, uint8_t rpcVersion = 0);
signals:
void ClientConnected(const WebSocketSessionState state);
void ClientDisconnected(const WebSocketSessionState state, const uint16_t closeCode);
private: