diff --git a/src/general.cpp b/src/general.cpp index 18078d2e..61a721cb 100644 --- a/src/general.cpp +++ b/src/general.cpp @@ -538,8 +538,8 @@ void SwitcherData::loadSettings(obs_data_t *obj) // selections to be available. loadSceneGroups(obj); loadVariables(obj); - loadMacros(obj); loadConnections(obj); + loadMacros(obj); loadWindowTitleSwitches(obj); loadScreenRegionSwitches(obj); loadPauseSwitches(obj); diff --git a/src/macro-core/macro-action-websocket.cpp b/src/macro-core/macro-action-websocket.cpp index d78d9ff4..9ef47f87 100644 --- a/src/macro-core/macro-action-websocket.cpp +++ b/src/macro-core/macro-action-websocket.cpp @@ -18,7 +18,7 @@ static std::map actionTypes = { void MacroActionWebsocket::SendRequest() { - auto connection = GetConnectionByName(_connection); + auto connection = _connection.lock(); if (!connection) { return; } @@ -46,7 +46,7 @@ void MacroActionWebsocket::LogAction() const switch (_type) { case MacroActionWebsocket::Type::REQUEST: vblog(LOG_INFO, "sent msg \"%s\" via \"%s\"", _message.c_str(), - _connection.c_str()); + GetWeakConnectionName(_connection).c_str()); break; case MacroActionWebsocket::Type::EVENT: vblog(LOG_INFO, "sent event \"%s\" to connected clients", @@ -62,7 +62,8 @@ bool MacroActionWebsocket::Save(obs_data_t *obj) const MacroAction::Save(obj); obs_data_set_int(obj, "type", static_cast(_type)); _message.Save(obj, "message"); - obs_data_set_string(obj, "connection", _connection.c_str()); + obs_data_set_string(obj, "connection", + GetWeakConnectionName(_connection).c_str()); return true; } @@ -71,14 +72,15 @@ bool MacroActionWebsocket::Load(obs_data_t *obj) MacroAction::Load(obj); _type = static_cast(obs_data_get_int(obj, "type")); _message.Load(obj, "message"); - _connection = obs_data_get_string(obj, "connection"); + _connection = + GetWeakConnectionByName(obs_data_get_string(obj, "connection")); return true; } std::string MacroActionWebsocket::GetShortDesc() const { if (_type == Type::REQUEST) { - return _connection; + return GetWeakConnectionName(_connection); } return ""; } @@ -206,6 +208,6 @@ void MacroActionWebsocketEdit::ConnectionSelectionChanged( } std::lock_guard lock(switcher->m); - _entryData->_connection = connection.toStdString(); + _entryData->_connection = GetWeakConnectionByQString(connection); emit(HeaderInfoChanged(connection)); } diff --git a/src/macro-core/macro-action-websocket.hpp b/src/macro-core/macro-action-websocket.hpp index ec17c388..491bc0bd 100644 --- a/src/macro-core/macro-action-websocket.hpp +++ b/src/macro-core/macro-action-websocket.hpp @@ -29,7 +29,7 @@ public: Type _type = Type::REQUEST; StringVariable _message = obs_module_text("AdvSceneSwitcher.enterText"); - std::string _connection; + std::weak_ptr _connection; private: void SendRequest(); diff --git a/src/macro-core/macro-condition-websocket.cpp b/src/macro-core/macro-condition-websocket.cpp index 167530aa..b9ffd6b7 100644 --- a/src/macro-core/macro-condition-websocket.cpp +++ b/src/macro-core/macro-condition-websocket.cpp @@ -38,7 +38,7 @@ bool MacroConditionWebsocket::CheckCondition() messages = &switcher->websocketMessages; break; case MacroConditionWebsocket::Type::EVENT: { - auto connection = GetConnectionByName(_connection); + auto connection = _connection.lock(); if (!connection) { return false; } @@ -76,7 +76,8 @@ bool MacroConditionWebsocket::Save(obs_data_t *obj) const obs_data_set_int(obj, "type", static_cast(_type)); _message.Save(obj, "message"); _regex.Save(obj); - obs_data_set_string(obj, "connection", _connection.c_str()); + obs_data_set_string(obj, "connection", + GetWeakConnectionName(_connection).c_str()); return true; } @@ -91,7 +92,8 @@ bool MacroConditionWebsocket::Load(obs_data_t *obj) _regex.CreateBackwardsCompatibleRegex( obs_data_get_bool(obj, "useRegex"), false); } - _connection = obs_data_get_string(obj, "connection"); + _connection = + GetWeakConnectionByName(obs_data_get_string(obj, "connection")); return true; } @@ -100,7 +102,7 @@ std::string MacroConditionWebsocket::GetShortDesc() const if (_type == Type::REQUEST) { return ""; } - return _connection; + return GetWeakConnectionName(_connection); } static inline void populateConditionSelection(QComboBox *list) @@ -237,7 +239,7 @@ void MacroConditionWebsocketEdit::ConnectionSelectionChanged( } std::lock_guard lock(switcher->m); - _entryData->_connection = connection.toStdString(); + _entryData->_connection = GetWeakConnectionByQString(connection); emit(HeaderInfoChanged(connection)); } diff --git a/src/macro-core/macro-condition-websocket.hpp b/src/macro-core/macro-condition-websocket.hpp index 377fd90a..163d53d1 100644 --- a/src/macro-core/macro-condition-websocket.hpp +++ b/src/macro-core/macro-condition-websocket.hpp @@ -27,7 +27,7 @@ public: Type _type = Type::REQUEST; StringVariable _message = obs_module_text("AdvSceneSwitcher.enterText"); RegexConfig _regex; - std::string _connection; + std::weak_ptr _connection; private: static bool _registered; diff --git a/src/utils/connection-manager.cpp b/src/utils/connection-manager.cpp index b2c92eb2..732ec2b6 100644 --- a/src/utils/connection-manager.cpp +++ b/src/utils/connection-manager.cpp @@ -133,6 +133,32 @@ Connection *GetConnectionByName(const std::string &name) return nullptr; } +std::weak_ptr GetWeakConnectionByName(const std::string &name) +{ + for (const auto &c : switcher->connections) { + if (c->Name() == name) { + std::weak_ptr wp = + std::dynamic_pointer_cast(c); + return wp; + } + } + return std::weak_ptr(); +} + +std::weak_ptr GetWeakConnectionByQString(const QString &name) +{ + return GetWeakConnectionByName(name.toStdString()); +} + +std::string GetWeakConnectionName(std::weak_ptr connection) +{ + auto con = connection.lock(); + if (!con) { + return "invalid connection selection"; + } + return con->Name(); +} + bool ConnectionNameAvailable(const QString &name) { return !GetConnectionByName(name); @@ -187,6 +213,18 @@ void ConnectionSelection::SetConnection(const std::string &con) } } +void ConnectionSelection::SetConnection( + const std::weak_ptr &connection_) +{ + const QSignalBlocker blocker(_selection); + auto connection = connection_.lock(); + if (connection) { + SetConnection(connection->Name()); + } else { + _selection->setCurrentIndex(0); + } +} + ConnectionSettingsDialog::ConnectionSettingsDialog(QWidget *parent, const Connection &settings) : ItemSettingsDialog(settings, switcher->connections, diff --git a/src/utils/connection-manager.hpp b/src/utils/connection-manager.hpp index 4d41a590..8020dc00 100644 --- a/src/utils/connection-manager.hpp +++ b/src/utils/connection-manager.hpp @@ -56,6 +56,9 @@ private: Connection *GetConnectionByName(const QString &); Connection *GetConnectionByName(const std::string &); +std::weak_ptr GetWeakConnectionByName(const std::string &name); +std::weak_ptr GetWeakConnectionByQString(const QString &name); +std::string GetWeakConnectionName(std::weak_ptr); class ConnectionSettingsDialog : public ItemSettingsDialog { Q_OBJECT @@ -92,4 +95,5 @@ class ConnectionSelection : public ItemSelection { public: ConnectionSelection(QWidget *parent = 0); void SetConnection(const std::string &); + void SetConnection(const std::weak_ptr &); };