Skip to content

Commit

Permalink
Add back ability to hide unregistered hosts in the menu
Browse files Browse the repository at this point in the history
Closes #267
  • Loading branch information
streetpea committed Oct 20, 2024
1 parent 88c9f9b commit 614b7f5
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 12 deletions.
17 changes: 17 additions & 0 deletions gui/include/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ static bool operator==(const HostMAC &a, const HostMAC &b) { return memcmp(a.Get
static bool operator!=(const HostMAC &a, const HostMAC &b) { return !(a == b); }
static bool operator<(const HostMAC &a, const HostMAC &b) { return a.GetValue() < b.GetValue(); }

class HiddenHost
{
private:
HostMAC server_mac;
QString server_nickname;
public:
HiddenHost() { ; }
HiddenHost(HostMAC server_mac, QString server_nickname) { this->server_mac = server_mac; this->server_nickname = server_nickname; }
HostMAC GetMAC() const { return server_mac; }
QString GetNickname() const { return server_nickname; }
void SetNickname(const QString &nickname) { this->server_nickname = nickname; }

void SaveToSettings(QSettings *settings) const;
static HiddenHost LoadFromSettings(QSettings *settings);
};

static bool operator==(const HiddenHost &a, const HiddenHost &b) { return (a.GetMAC() == b.GetMAC() && a.GetNickname() == b.GetNickname()); }
class RegisteredHost
{
private:
Expand Down
6 changes: 6 additions & 0 deletions gui/include/qmlbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class QmlBackend : public QObject
Q_PROPERTY(QList<QmlController*> controllers READ qmlControllers NOTIFY controllersChanged)
Q_PROPERTY(bool discoveryEnabled READ discoveryEnabled WRITE setDiscoveryEnabled NOTIFY discoveryEnabledChanged)
Q_PROPERTY(QVariantList hosts READ hosts NOTIFY hostsChanged)
Q_PROPERTY(QVariantList hiddenHosts READ hiddenHosts NOTIFY hiddenHostsChanged)
Q_PROPERTY(bool autoConnect READ autoConnect NOTIFY autoConnectChanged)
Q_PROPERTY(PsnConnectState connectState READ connectState WRITE setConnectState NOTIFY connectStateChanged)
Q_PROPERTY(QVariantList currentControllerMapping READ currentControllerMapping NOTIFY currentControllerMappingChanged)
Expand Down Expand Up @@ -93,6 +94,8 @@ class QmlBackend : public QObject
void setConnectState(PsnConnectState connect_state);
QVariantList hosts() const;

QVariantList hiddenHosts() const;

QVariantList currentControllerMapping() const;

QString currentControllerType() const { return controller_mapping_controller_type; }
Expand Down Expand Up @@ -135,6 +138,8 @@ class QmlBackend : public QObject
Q_INVOKABLE void deleteHost(int index);
Q_INVOKABLE void wakeUpHost(int index, QString nickname = QString());
Q_INVOKABLE void addManualHost(int index, const QString &address);
Q_INVOKABLE void hideHost(const QString &mac_string, const QString &host_nickname);
Q_INVOKABLE void unhideHost(const QString &mac_string);
Q_INVOKABLE bool registerHost(const QString &host, const QString &psn_id, const QString &pin, const QString &cpin, bool broadcast, int target, const QJSValue &callback);
Q_INVOKABLE void connectToHost(int index, QString nickname = QString());
Q_INVOKABLE void stopSession(bool sleep);
Expand Down Expand Up @@ -176,6 +181,7 @@ class QmlBackend : public QObject
void enableAnalogStickMappingChanged();
void discoveryEnabledChanged();
void hostsChanged();
void hiddenHostsChanged();
void psnTokenChanged();
void psnCredsExpired();
void autoConnectChanged();
Expand Down
9 changes: 9 additions & 0 deletions gui/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Settings : public QObject
QSettings default_settings;
QSettings placebo_settings;
QString time_format;
QMap<HostMAC, HiddenHost> hidden_hosts;

QMap<HostMAC, RegisteredHost> registered_hosts;
QMap<QString, RegisteredHost> nickname_registered_hosts;
Expand All @@ -189,6 +190,8 @@ class Settings : public QObject
void LoadRegisteredHosts(QSettings *qsettings = nullptr);
void SaveRegisteredHosts(QSettings *qsettings = nullptr);

void LoadHiddenHosts(QSettings *qsettings = nullptr);
void SaveHiddenHosts(QSettings *qsettings = nullptr);

void LoadManualHosts(QSettings *qsettings = nullptr);
void SaveManualHosts(QSettings *qsettings = nullptr);
Expand Down Expand Up @@ -562,6 +565,11 @@ class Settings : public QObject
void RemoveRegisteredHost(const HostMAC &mac);
bool GetRegisteredHostRegistered(const HostMAC &mac) const { return registered_hosts.contains(mac); }
RegisteredHost GetRegisteredHost(const HostMAC &mac) const { return registered_hosts[mac]; }
QList<HiddenHost> GetHiddenHosts() const { return hidden_hosts.values(); }
void AddHiddenHost(const HiddenHost &host);
void RemoveHiddenHost(const HostMAC &mac);
bool GetHiddenHostHidden(const HostMAC &mac) const { return hidden_hosts.contains(mac); }
HiddenHost GetHiddenHost(const HostMAC &mac) const { return hidden_hosts[mac]; }
bool GetNicknameRegisteredHostRegistered(const QString &nickname) const { return nickname_registered_hosts.contains(nickname); }
RegisteredHost GetNicknameRegisteredHost(const QString &nickname) const { return nickname_registered_hosts[nickname]; }
size_t GetPS4RegisteredHostsRegistered() const { return ps4s_registered; }
Expand All @@ -584,6 +592,7 @@ class Settings : public QObject

signals:
void RegisteredHostsUpdated();
void HiddenHostsUpdated();
void ManualHostsUpdated();
void ControllerMappingsUpdated();
void CurrentProfileChanged();
Expand Down
16 changes: 16 additions & 0 deletions gui/src/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ RegisteredHost RegisteredHost::LoadFromSettings(QSettings *settings)
return r;
}

void HiddenHost::SaveToSettings(QSettings *settings) const
{
settings->setValue("server_nickname", server_nickname);
settings->setValue("server_mac", QByteArray((const char *)server_mac.GetMAC(), 6));
}

HiddenHost HiddenHost::LoadFromSettings(QSettings *settings)
{
HiddenHost r;
r.server_nickname = settings->value("server_nickname").toString();
auto server_mac = settings->value("server_mac").toByteArray();
if(server_mac.size() == 6)
r.server_mac = HostMAC((const uint8_t *)server_mac.constData());
return r;
}

ManualHost::ManualHost()
{
id = -1;
Expand Down
23 changes: 16 additions & 7 deletions gui/src/qml/MainView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ Pane {
}
}
Keys.onUpPressed: {
hostsView.decrementCurrentIndex()
while(hostsView.currentItem && !hostsView.currentItem.visible)
if(hostsView.currentItem && hostsView.currentItem.visible)
{
hostsView.decrementCurrentIndex()
while(!hostsView.currentItem.visible)
hostsView.decrementCurrentIndex()
}
}
Keys.onDownPressed: {
hostsView.incrementCurrentIndex()
while(hostsView.currentItem && !hostsView.currentItem.visible)
hostsView.incrementCurrentIndex()
if(hostsView.currentItem && hostsView.currentItem.visible)
{
hostsView.incrementCurrentIndex()
while(!hostsView.currentItem.visible)
hostsView.incrementCurrentIndex()
}
}
Keys.onMenuPressed: settingsButton.clicked()
Keys.onReturnPressed: if (hostsView.currentItem) hostsView.currentItem.connectToHost()
Expand Down Expand Up @@ -165,6 +171,9 @@ Pane {
function deleteHost() {
if (modelData.manual)
root.showConfirmDialog(qsTr("Delete Console"), qsTr("Are you sure you want to delete this console?"), () => Chiaki.deleteHost(index));
else if (modelData.discovered && !modelData.registered)
root.showConfirmDialog(qsTr("Hide Console"), qsTr("Are you sure you want to hide this console?") + "\n\n" + qsTr("Note: You can unhide from the Consoles section of the Settings under Hidden Consoles"), () => Chiaki.hideHost(modelData.mac, modelData.name));

}

function setConsolePin() {
Expand Down Expand Up @@ -245,12 +254,12 @@ Pane {

Button {
Layout.alignment: Qt.AlignCenter
text: qsTr("Delete")
text: modelData.manual ? qsTr("Delete") : qsTr("Hide")
flat: true
padding: 20
leftPadding: delegate.highlighted ? 50 : undefined
focusPolicy: Qt.NoFocus
visible: modelData.manual
visible: modelData.manual || (modelData.discovered && !modelData.registered)
onClicked: delegate.deleteHost()
Material.roundedScale: Material.SmallScale

Expand Down
43 changes: 42 additions & 1 deletion gui/src/qml/SettingsDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,6 @@ DialogView {
anchors {
top: consolesLabel.bottom
horizontalCenter: consolesLabel.horizontalCenter
bottom: parent.bottom
topMargin: 10
}
width: 700
Expand Down Expand Up @@ -1111,6 +1110,48 @@ DialogView {
}
}
}

Label {
id: hiddenConsolesLabel
anchors {
top: consolesView.bottom
horizontalCenter: consolesView.horizontalCenter
topMargin: 50
}
text: qsTr("Hidden Consoles")
font.bold: true
}

ListView {
id: hiddenConsolesView
anchors {
top: hiddenConsolesLabel.bottom
horizontalCenter: hiddenConsolesLabel.horizontalCenter
bottom: parent.bottom
topMargin: 10
}
width: 700
clip: true
model: Chiaki.hiddenHosts
delegate: ItemDelegate {
text: "%1 (%2)".arg(modelData.mac).arg(modelData.name)
height: 80
width: parent ? parent.width : 0

C.Button {
anchors {
right: parent.right
verticalCenter: parent.verticalCenter
rightMargin: 20
}
lastInFocusChain: index == consolesView.count - 1
text: qsTr("Unhide")
onClicked: root.showConfirmDialog(qsTr("Unhide Console"), qsTr("Are you sure you want to unhide this console?"), () => Chiaki.unhideHost(modelData.mac));
Material.roundedScale: Material.SmallScale
Material.accent: Material.Red
}
}
}
}

Item {
Expand Down
71 changes: 67 additions & 4 deletions gui/src/qmlbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ QmlBackend::QmlBackend(Settings *settings, QmlMainWindow *window)

setConnectState(PsnConnectState::NotStarted);
connect(settings, &Settings::RegisteredHostsUpdated, this, &QmlBackend::hostsChanged);
connect(settings, &Settings::HiddenHostsUpdated, this, &QmlBackend::hiddenHostsChanged);
connect(settings, &Settings::ManualHostsUpdated, this, &QmlBackend::hostsChanged);
connect(settings, &Settings::CurrentProfileChanged, this, &QmlBackend::profileChanged);
connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &QmlBackend::updateDiscoveryHosts);
Expand Down Expand Up @@ -290,6 +291,7 @@ void QmlBackend::profileChanged()
emit hostsChanged();
updateControllerMappings();
connect(settings, &Settings::RegisteredHostsUpdated, this, &QmlBackend::hostsChanged);
connect(settings, &Settings::HiddenHostsUpdated, this, &QmlBackend::hiddenHostsChanged);
connect(settings, &Settings::ManualHostsUpdated, this, &QmlBackend::hostsChanged);
connect(settings, &Settings::CurrentProfileChanged, this, &QmlBackend::profileChanged);
connect(settings, &Settings::ControllerMappingsUpdated, this, &QmlBackend::updateControllerMappings);
Expand Down Expand Up @@ -407,6 +409,7 @@ void QmlBackend::profileChanged()
});
refreshPsnToken();
emit hostsChanged();
emit hiddenHostsChanged();
}

bool QmlBackend::discoveryEnabled() const
Expand Down Expand Up @@ -451,13 +454,31 @@ QVariantList QmlBackend::hosts() const
auto manual_hosts = settings->GetManualHosts();
for (const auto &host : discovery_manager.GetHosts()) {
QVariantMap m;
bool registered = settings->GetRegisteredHostRegistered(host.GetHostMAC());
HostMAC host_mac = host.GetHostMAC();
bool registered = settings->GetRegisteredHostRegistered(host_mac);
bool hidden = settings->GetHiddenHostHidden(host_mac);
if(registered && hidden)
{
settings->RemoveHiddenHost(host_mac);
bool hidden = false;
}
// Update hidden host nickname if it's changed
if(hidden)
{
auto hidden_host = settings->GetHiddenHost(host_mac);
if(hidden_host.GetNickname() != host.host_name)
{
hidden_host.SetNickname(host.host_name);
settings->RemoveHiddenHost(host_mac);
settings->AddHiddenHost(hidden_host);
}
}
m["discovered"] = true;
bool manual = false;
for(int i = 0; i < manual_hosts.length(); i++)
{
const auto &manual_host = manual_hosts.at(i);
if(manual_host.GetRegistered() && manual_host.GetMAC() == host.GetHostMAC() && manual_host.GetHost() == host.host_addr)
if(manual_host.GetRegistered() && manual_host.GetMAC() == host_mac && manual_host.GetHost() == host.host_addr)
{
manual = true;
discovered_manual_hosts.append(manual_host);
Expand All @@ -468,12 +489,12 @@ QVariantList QmlBackend::hosts() const
m["duid"] = "";
m["address"] = host.host_addr;
m["ps5"] = host.ps5;
m["mac"] = host.GetHostMAC().ToString();
m["mac"] = host_mac.ToString();
m["state"] = chiaki_discovery_host_state_string(host.state);
m["app"] = host.running_app_name;
m["titleId"] = host.running_app_titleid;
m["registered"] = registered;
m["display"] = true;
m["display"] = hidden ? false : true;
discovered_nicknames.append(host.host_name);
out.append(m);
if(!host.ps5 && registered)
Expand Down Expand Up @@ -849,6 +870,48 @@ void QmlBackend::addManualHost(int index, const QString &address)
settings->SetManualHost(host);
}

void QmlBackend::hideHost(const QString &mac_string, const QString &host_nickname)
{
QByteArray mac_array = QByteArray::fromHex(mac_string.toUtf8());
const char *mac_ptr = mac_array.constData();
if (strlen(mac_ptr) != 6)
{
qCCritical(chiakiGui) << " Aborting hidden host creation because mac string couldn't be converted to a valid host mac!";
return;
}
HostMAC mac((const uint8_t *)mac_ptr);
HiddenHost hidden_host(mac, host_nickname);
settings->AddHiddenHost(hidden_host);
emit hostsChanged();
}

void QmlBackend::unhideHost(const QString &mac_string)
{
QByteArray mac_array = QByteArray::fromHex(mac_string.toUtf8());
const char *mac_ptr = mac_array.constData();
if (strlen(mac_ptr) != 6)
{
qCCritical(chiakiGui) << " Aborting hidden host creation because mac string couldn't be converted to a valid host mac!";
return;
}
HostMAC mac((const uint8_t *)mac_ptr);
settings->RemoveHiddenHost(mac);
emit hostsChanged();
}

QVariantList QmlBackend::hiddenHosts() const
{
QVariantList out;
for (const auto &host : settings->GetHiddenHosts()) {
QVariantMap m;
m["name"] = host.GetNickname();
m["mac"] = host.GetMAC().ToString();
out.append(m);
}
return out;
}


bool QmlBackend::registerHost(const QString &host, const QString &psn_id, const QString &pin, const QString &cpin, bool broadcast, int target, const QJSValue &callback)
{
ChiakiRegistInfo info = {};
Expand Down
Loading

0 comments on commit 614b7f5

Please sign in to comment.