Fixed RemoteLibrary to load server settings from preferences and updated

RemoteLibrarySettingsLayout. Next up: read/write values from settings.
This commit is contained in:
Casey Langen 2020-10-10 12:43:00 -07:00
parent cf522a5bb9
commit 6cb63c1a6f
8 changed files with 51 additions and 23 deletions

View File

@ -38,6 +38,7 @@
#include <core/config.h> #include <core/config.h>
#include <core/support/Common.h> #include <core/support/Common.h>
#include <core/support/Preferences.h> #include <core/support/Preferences.h>
#include <core/support/PreferenceKeys.h>
#include <core/library/Indexer.h> #include <core/library/Indexer.h>
#include <core/library/IQuery.h> #include <core/library/IQuery.h>
#include <core/library/LibraryFactory.h> #include <core/library/LibraryFactory.h>
@ -57,10 +58,6 @@ using namespace musik::core::runtime;
#define MESSAGE_RECONNECT_SOCKET 5001 #define MESSAGE_RECONNECT_SOCKET 5001
#define MESSAGE_UPDATE_CONNECTION_STATE 5002 #define MESSAGE_UPDATE_CONNECTION_STATE 5002
static const std::string kServerHost = "192.168.1.227";
static const std::string kServerPassword = "842655";
static const short kServerPort = 7905;
class NullIndexer: public musik::core::IIndexer { class NullIndexer: public musik::core::IIndexer {
public: public:
virtual ~NullIndexer() override { } virtual ~NullIndexer() override { }
@ -101,9 +98,9 @@ RemoteLibrary::RemoteLibrary(std::string name, int id)
, exit(false) , exit(false)
, messageQueue(nullptr) , messageQueue(nullptr)
, wsc(this) { , wsc(this) {
this->wsc.Connect(kServerHost, kServerPort, kServerPassword);
this->identifier = std::to_string(id); this->identifier = std::to_string(id);
this->thread = new std::thread(std::bind(&RemoteLibrary::ThreadProc, this)); this->thread = new std::thread(std::bind(&RemoteLibrary::ThreadProc, this));
this->ReloadConnectionFromPreferences();
} }
RemoteLibrary::~RemoteLibrary() { RemoteLibrary::~RemoteLibrary() {
@ -325,7 +322,7 @@ void RemoteLibrary::ProcessMessage(musik::core::runtime::IMessage &message) {
} }
else if (message.Type() == MESSAGE_RECONNECT_SOCKET) { else if (message.Type() == MESSAGE_RECONNECT_SOCKET) {
if (this->wsc.ConnectionState() == Client::State::Disconnected) { if (this->wsc.ConnectionState() == Client::State::Disconnected) {
this->wsc.Connect(kServerHost, kServerPort, kServerPassword); this->ReloadConnectionFromPreferences();
} }
} }
else if (message.Type() == MESSAGE_UPDATE_CONNECTION_STATE) { else if (message.Type() == MESSAGE_UPDATE_CONNECTION_STATE) {
@ -386,17 +383,31 @@ std::string RemoteLibrary::GetTrackUri(musik::core::sdk::ITrack* track, const st
} }
} }
const std::string uri = "http://" + kServerHost + ":7906/audio/id/" + std::to_string(track->GetId()); auto prefs = Preferences::ForComponent(core::prefs::components::Settings);
auto host = prefs->GetString(core::prefs::keys::RemoteLibraryHostname, "127.0.0.1");
auto port = (short) prefs->GetInt(core::prefs::keys::RemoteLibraryHttpPort, 7905);
auto password = prefs->GetString(core::prefs::keys::RemoteLibraryPassword, "");
const std::string uri = "http://" + host + ":" + std::to_string(port) + "/audio/id/" + std::to_string(track->GetId());
nlohmann::json path = { nlohmann::json path = {
{ "uri", uri }, { "uri", uri },
{ "type", type }, { "type", type },
{ "password", kServerPassword } { "password", password }
}; };
return "musikcore://remote-track/" + path.dump(); return "musikcore://remote-track/" + path.dump();
} }
/* RemoteLibrary */ /* RemoteLibrary */
const net::WebSocketClient& RemoteLibrary::WebSocketClient() const { const net::WebSocketClient& RemoteLibrary::WebSocketClient() const {
return this->wsc; return this->wsc;
}
void RemoteLibrary::ReloadConnectionFromPreferences() {
auto prefs = Preferences::ForComponent(core::prefs::components::Settings);
auto host = prefs->GetString(core::prefs::keys::RemoteLibraryHostname, "127.0.0.1");
auto port = (short) prefs->GetInt(core::prefs::keys::RemoteLibraryWssPort, 7905);
auto password = prefs->GetString(core::prefs::keys::RemoteLibraryPassword, "");
this->wsc.Connect(host, port, password);
} }

View File

@ -95,6 +95,7 @@ namespace musik { namespace core { namespace library {
virtual std::string GetTrackUri(musik::core::sdk::ITrack* track, const std::string& defaultUri) override; virtual std::string GetTrackUri(musik::core::sdk::ITrack* track, const std::string& defaultUri) override;
/* RemoteLibrary */ /* RemoteLibrary */
void ReloadConnectionFromPreferences();
const musik::core::net::WebSocketClient& WebSocketClient() const; const musik::core::net::WebSocketClient& WebSocketClient() const;
private: private:

View File

@ -67,7 +67,8 @@ namespace musik { namespace core { namespace prefs {
const std::string keys::AuddioApiToken = "AuddioApiToken"; const std::string keys::AuddioApiToken = "AuddioApiToken";
const std::string keys::LibraryType = "LibraryType"; const std::string keys::LibraryType = "LibraryType";
const std::string keys::RemoteLibraryHostname = "RemoteLibraryHostname"; const std::string keys::RemoteLibraryHostname = "RemoteLibraryHostname";
const std::string keys::RemoteLibraryPort = "RemoteLibraryPort"; const std::string keys::RemoteLibraryWssPort = "RemoteLibraryWssPort";
const std::string keys::RemoteLibraryHttpPort = "RemoteLibraryHttpPort";
const std::string keys::RemoteLibraryPassword = "RemoteLibraryPassword"; const std::string keys::RemoteLibraryPassword = "RemoteLibraryPassword";
} } } } } }

View File

@ -71,7 +71,8 @@ namespace musik { namespace core { namespace prefs {
extern const std::string AuddioApiToken; extern const std::string AuddioApiToken;
extern const std::string LibraryType; extern const std::string LibraryType;
extern const std::string RemoteLibraryHostname; extern const std::string RemoteLibraryHostname;
extern const std::string RemoteLibraryPort; extern const std::string RemoteLibraryWssPort;
extern const std::string RemoteLibraryHttpPort;
extern const std::string RemoteLibraryPassword; extern const std::string RemoteLibraryPassword;
} }

View File

@ -72,7 +72,8 @@ RemoteLibrarySettingsLayout::~RemoteLibrarySettingsLayout() {
void RemoteLibrarySettingsLayout::OnLayout() { void RemoteLibrarySettingsLayout::OnLayout() {
size_t labelWidth = longestStringLength({ size_t labelWidth = longestStringLength({
"settings_library_type_remote_hostname", "settings_library_type_remote_hostname",
"settings_library_type_remote_port", "settings_library_type_remote_wss_port",
"settings_library_type_remote_http_port",
"settings_library_type_remote_password" "settings_library_type_remote_password"
}); });
@ -81,10 +82,13 @@ void RemoteLibrarySettingsLayout::OnLayout() {
int y = 0; int y = 0;
this->hostLabel->MoveAndResize(0, y, labelWidth, 1); this->hostLabel->MoveAndResize(0, y, labelWidth, 1);
this->hostInput->MoveAndResize(labelWidth + 1, y++, inputWidth, 1); this->hostInput->MoveAndResize(labelWidth + 1, y++, inputWidth, 1);
this->portLabel->MoveAndResize(0, y, labelWidth, 1); this->wssPortLabel->MoveAndResize(0, y, labelWidth, 1);
this->portInput->MoveAndResize(labelWidth + 1, y++, 5, 1); this->wssPortInput->MoveAndResize(labelWidth + 1, y++, 5, 1);
this->httpPortLabel->MoveAndResize(0, y, labelWidth, 1);
this->httpPortInput->MoveAndResize(labelWidth + 1, y++, 5, 1);
this->pwLabel->MoveAndResize(0, y, labelWidth, 1); this->pwLabel->MoveAndResize(0, y, labelWidth, 1);
this->pwInput->MoveAndResize(labelWidth + 1, y++, inputWidth, 1); this->pwInput->MoveAndResize(labelWidth + 1, y++, inputWidth, 1);
++y;
this->ipv6Cb->MoveAndResize(0, y++, cx, 1); this->ipv6Cb->MoveAndResize(0, y++, cx, 1);
} }
@ -95,9 +99,13 @@ void RemoteLibrarySettingsLayout::InitializeWindows() {
this->hostLabel->SetText(_TSTR("settings_library_type_remote_hostname"), text::AlignRight); this->hostLabel->SetText(_TSTR("settings_library_type_remote_hostname"), text::AlignRight);
this->hostInput.reset(new TextInput(TextInput::StyleLine)); this->hostInput.reset(new TextInput(TextInput::StyleLine));
this->portLabel.reset(new TextLabel()); this->wssPortLabel.reset(new TextLabel());
this->portLabel->SetText(_TSTR("settings_library_type_remote_port"), text::AlignRight); this->wssPortLabel->SetText(_TSTR("settings_library_type_remote_wss_port"), text::AlignRight);
this->portInput.reset(new TextInput(TextInput::StyleLine)); this->wssPortInput.reset(new TextInput(TextInput::StyleLine));
this->httpPortLabel.reset(new TextLabel());
this->httpPortLabel->SetText(_TSTR("settings_library_type_remote_http_port"), text::AlignRight);
this->httpPortInput.reset(new TextInput(TextInput::StyleLine));
this->pwLabel.reset(new TextLabel()); this->pwLabel.reset(new TextLabel());
this->pwLabel->SetText(_TSTR("settings_library_type_remote_password"), text::AlignRight); this->pwLabel->SetText(_TSTR("settings_library_type_remote_password"), text::AlignRight);
@ -108,15 +116,20 @@ void RemoteLibrarySettingsLayout::InitializeWindows() {
this->AddWindow(this->hostLabel); this->AddWindow(this->hostLabel);
this->AddWindow(this->hostInput); this->AddWindow(this->hostInput);
this->AddWindow(this->portLabel); this->AddWindow(this->hostLabel);
this->AddWindow(this->portInput); this->AddWindow(this->hostInput);
this->AddWindow(this->wssPortLabel);
this->AddWindow(this->wssPortInput);
this->AddWindow(this->httpPortLabel);
this->AddWindow(this->httpPortInput);
this->AddWindow(this->pwLabel); this->AddWindow(this->pwLabel);
this->AddWindow(this->pwInput); this->AddWindow(this->pwInput);
this->AddWindow(this->ipv6Cb); this->AddWindow(this->ipv6Cb);
int order = 0; int order = 0;
this->hostInput->SetFocusOrder(order++); this->hostInput->SetFocusOrder(order++);
this->portInput->SetFocusOrder(order++); this->wssPortInput->SetFocusOrder(order++);
this->httpPortInput->SetFocusOrder(order++);
this->pwInput->SetFocusOrder(order++); this->pwInput->SetFocusOrder(order++);
this->ipv6Cb->SetFocusOrder(order++); this->ipv6Cb->SetFocusOrder(order++);
} }

View File

@ -61,8 +61,8 @@ namespace musik { namespace cube {
musik::core::ILibraryPtr library; musik::core::ILibraryPtr library;
std::shared_ptr<musik::core::Preferences> prefs; std::shared_ptr<musik::core::Preferences> prefs;
std::shared_ptr<cursespp::TextLabel> portLabel, hostLabel, pwLabel; std::shared_ptr<cursespp::TextLabel> httpPortLabel, wssPortLabel, hostLabel, pwLabel;
std::shared_ptr<cursespp::TextInput> portInput, hostInput, pwInput; std::shared_ptr<cursespp::TextInput> httpPortInput, wssPortInput, hostInput, pwInput;
std::shared_ptr<cursespp::Checkbox> ipv6Cb; std::shared_ptr<cursespp::Checkbox> ipv6Cb;
}; };
} } } }

View File

@ -334,7 +334,7 @@ void SettingsLayout::OnLayout() {
} }
else { else {
this->localLibraryLayout->Hide(); this->localLibraryLayout->Hide();
this->remoteLibraryLayout->MoveAndResize(2, 3, cx - 4, 4); this->remoteLibraryLayout->MoveAndResize(2, 3, cx - 4, 6);
libraryLayout = this->remoteLibraryLayout; libraryLayout = this->remoteLibraryLayout;
} }
libraryLayout->Show(); libraryLayout->Show();

View File

@ -109,7 +109,8 @@
"settings_library_type_local": "local", "settings_library_type_local": "local",
"settings_library_type_remote": "remote", "settings_library_type_remote": "remote",
"settings_library_type_remote_hostname": "hostname:", "settings_library_type_remote_hostname": "hostname:",
"settings_library_type_remote_port": "port:", "settings_library_type_remote_wss_port": "metadata port:",
"settings_library_type_remote_http_port": "audio port:",
"settings_library_type_remote_password": "password:", "settings_library_type_remote_password": "password:",
"settings_library_type_overlay_title": "library type", "settings_library_type_overlay_title": "library type",