summaryrefslogtreecommitdiff
path: root/src/settingsdialognexus.cpp
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2020-07-30 00:48:46 -0400
committerisanae <14251494+isanae@users.noreply.github.com>2020-11-03 11:39:06 -0500
commitc4df6b8ec98feb34cc9b9785e6d4d0eac6b3f571 (patch)
treec874d57f52243362ab2e7114c3d409b6326729cc /src/settingsdialognexus.cpp
parentc699aeb6d862f2c0960de126538931e4f13368b7 (diff)
split nexus connection stuff into NexusConnectionUI so it can be reused in the instance creation dialog
removed the PluginContainer* parameter from NexusInterface::instance() - it's a singleton, so it only needs to be given once, not every time - it doesn't even matter because the first time instance() is called, it creates the singleton, but the plugin container is always null - and setPluginContainer() is called much later from OrganizerCore with the correct value added non functional nexus page to instance creation dialog
Diffstat (limited to 'src/settingsdialognexus.cpp')
-rw-r--r--src/settingsdialognexus.cpp382
1 files changed, 213 insertions, 169 deletions
diff --git a/src/settingsdialognexus.cpp b/src/settingsdialognexus.cpp
index d49e0a33..a76b9ad7 100644
--- a/src/settingsdialognexus.cpp
+++ b/src/settingsdialognexus.cpp
@@ -71,112 +71,45 @@ private:
};
-NexusSettingsTab::NexusSettingsTab(Settings& s, SettingsDialog& d)
- : SettingsTab(s, d)
+NexusConnectionUI::NexusConnectionUI(Settings& s, QWidget* parent) :
+ m_parent(parent), m_settings(s),
+ m_connect(nullptr), m_disconnect(nullptr), m_manual(nullptr), m_log(nullptr)
{
- ui->offlineBox->setChecked(settings().network().offlineMode());
- ui->proxyBox->setChecked(settings().network().useProxy());
- ui->endorsementBox->setChecked(settings().nexus().endorsementIntegration());
- ui->hideAPICounterBox->setChecked(settings().interface().hideAPICounter());
-
- // display server preferences
- for (const auto& server : s.network().servers()) {
- QString descriptor = server.name();
-
- if (!descriptor.compare("CDN", Qt::CaseInsensitive)) {
- descriptor += QStringLiteral(" (automatic)");
- }
-
- const auto averageSpeed = server.averageSpeed();
- if (averageSpeed > 0) {
- descriptor += QString(" (%1)").arg(MOBase::localizedByteSpeed(averageSpeed));
- }
-
- QListWidgetItem *newItem = new ServerItem<int>(descriptor, Qt::UserRole + 1);
-
- newItem->setData(Qt::UserRole, server.name());
- newItem->setData(Qt::UserRole + 1, server.preferred());
-
- if (server.preferred() > 0) {
- ui->preferredServersList->addItem(newItem);
- } else {
- ui->knownServersList->addItem(newItem);
- }
-
- ui->preferredServersList->sortItems(Qt::DescendingOrder);
- }
-
- QObject::connect(ui->nexusConnect, &QPushButton::clicked, [&]{ on_nexusConnect_clicked(); });
- QObject::connect(ui->nexusManualKey, &QPushButton::clicked, [&]{ on_nexusManualKey_clicked(); });
- QObject::connect(ui->nexusDisconnect, &QPushButton::clicked, [&]{ on_nexusDisconnect_clicked(); });
- QObject::connect(ui->clearCacheButton, &QPushButton::clicked, [&]{ on_clearCacheButton_clicked(); });
- QObject::connect(ui->associateButton, &QPushButton::clicked, [&]{ on_associateButton_clicked(); });
-
- if (settings().nexus().hasApiKey()) {
- addNexusLog(QObject::tr("Connected."));
- } else {
- addNexusLog(QObject::tr("Not connected."));
- }
-
- updateNexusState();
}
-void NexusSettingsTab::update()
+void NexusConnectionUI::set(
+ QAbstractButton* connectButton,
+ QAbstractButton* disconnectButton,
+ QAbstractButton* manualButton,
+ QListWidget* logList)
{
- settings().network().setOfflineMode(ui->offlineBox->isChecked());
- settings().network().setUseProxy(ui->proxyBox->isChecked());
- settings().nexus().setEndorsementIntegration(ui->endorsementBox->isChecked());
- settings().interface().setHideAPICounter(ui->hideAPICounterBox->isChecked());
-
- auto servers = settings().network().servers();
-
- // store server preference
- for (int i = 0; i < ui->knownServersList->count(); ++i) {
- const QString key = ui->knownServersList->item(i)->data(Qt::UserRole).toString();
-
- bool found = false;
-
- for (auto& server : servers) {
- if (server.name() == key) {
- server.setPreferred(0);
- found = true;
- break;
- }
- }
-
- if (!found) {
- log::error("while setting preferred to 0, server '{}' not found", key);
- }
+ m_connect = connectButton;
+ if (m_connect) {
+ QObject::connect(m_connect, &QPushButton::clicked, [&]{ connect(); });
}
- const int count = ui->preferredServersList->count();
-
- for (int i = 0; i < count; ++i) {
- const QString key = ui->preferredServersList->item(i)->data(Qt::UserRole).toString();
- const int newPreferred = count - i;
+ m_disconnect = disconnectButton;
+ if (m_disconnect) {
+ QObject::connect(m_disconnect, &QPushButton::clicked, [&]{ disconnect(); });
+ }
- bool found = false;
+ m_manual = manualButton;
+ if (m_manual) {
+ QObject::connect(manualButton, &QPushButton::clicked, [&]{ manual(); });
+ }
- for (auto& server : servers) {
+ m_log = logList;
- if (server.name() == key) {
- server.setPreferred(newPreferred);
- found = true;
- break;
- }
- }
-
- if (!found) {
- log::error(
- "while setting preference to {}, server '{}' not found",
- newPreferred, key);
- }
+ if (m_settings.nexus().hasApiKey()) {
+ addLog(tr("Connected."));
+ } else {
+ addLog(tr("Not connected."));
}
- settings().network().updateServers(servers);
+ updateState();
}
-void NexusSettingsTab::on_nexusConnect_clicked()
+void NexusConnectionUI::connect()
{
if (m_nexusLogin && m_nexusLogin->isActive()) {
m_nexusLogin->cancel();
@@ -195,19 +128,19 @@ void NexusSettingsTab::on_nexusConnect_clicked()
};
}
- ui->nexusLog->clear();
+ m_log->clear();
m_nexusLogin->start();
- updateNexusState();
+ updateState();
}
-void NexusSettingsTab::on_nexusManualKey_clicked()
+void NexusConnectionUI::manual()
{
if (m_nexusValidator && m_nexusValidator->isActive()) {
m_nexusValidator->cancel();
return;
}
- NexusManualKeyDialog d(&dialog());
+ NexusManualKeyDialog d(m_parent);
if (d.exec() != QDialog::Accepted) {
return;
}
@@ -218,162 +151,273 @@ void NexusSettingsTab::on_nexusManualKey_clicked()
return;
}
- ui->nexusLog->clear();
+ m_log->clear();
validateKey(key);
}
-void NexusSettingsTab::on_nexusDisconnect_clicked()
+void NexusConnectionUI::disconnect()
{
clearKey();
- ui->nexusLog->clear();
- addNexusLog(QObject::tr("Disconnected."));
-}
-
-void NexusSettingsTab::on_clearCacheButton_clicked()
-{
- QDir(Settings::instance().paths().cache()).removeRecursively();
- NexusInterface::instance(dialog().pluginContainer())->clearCache();
-}
-
-void NexusSettingsTab::on_associateButton_clicked()
-{
- Settings::instance().nexus().registerAsNXMHandler(true);
+ m_log->clear();
+ addLog(tr("Disconnected."));
}
-void NexusSettingsTab::validateKey(const QString& key)
+void NexusConnectionUI::validateKey(const QString& key)
{
if (!m_nexusValidator) {
m_nexusValidator.reset(new NexusKeyValidator(
- *NexusInterface::instance(dialog().pluginContainer())->getAccessManager()));
+ *NexusInterface::instance().getAccessManager()));
m_nexusValidator->finished = [&](auto&& r, auto&& m, auto&& u) {
onValidatorFinished(r, m, u);
};
}
- addNexusLog(QObject::tr("Checking API key..."));
+ addLog(tr("Checking API key..."));
m_nexusValidator->start(key, NexusKeyValidator::OneShot);
}
-void NexusSettingsTab::onSSOKeyChanged(const QString& key)
+void NexusConnectionUI::onSSOKeyChanged(const QString& key)
{
if (key.isEmpty()) {
clearKey();
} else {
- addNexusLog(QObject::tr("Received API key."));
+ addLog(tr("Received API key."));
validateKey(key);
}
}
-void NexusSettingsTab::onSSOStateChanged(NexusSSOLogin::States s, const QString& e)
+void NexusConnectionUI::onSSOStateChanged(
+ NexusSSOLogin::States s, const QString& e)
{
if (s != NexusSSOLogin::Finished) {
// finished state is handled in onSSOKeyChanged()
const auto log = NexusSSOLogin::stateToString(s, e);
for (auto&& line : log.split("\n")) {
- addNexusLog(line);
+ addLog(line);
}
}
- updateNexusState();
+ updateState();
}
-void NexusSettingsTab::onValidatorFinished(
+void NexusConnectionUI::onValidatorFinished(
ValidationAttempt::Result r, const QString& message,
std::optional<APIUserAccount> user)
{
if (user) {
- NexusInterface::instance(dialog().pluginContainer())->setUserAccount(*user);
- addNexusLog(QObject::tr("Received user acount information"));
+ NexusInterface::instance().setUserAccount(*user);
+ addLog(tr("Received user account information"));
if (setKey(user->apiKey())) {
- addNexusLog(QObject::tr("Linked with Nexus successfully."));
+ addLog(tr("Linked with Nexus successfully."));
} else {
- addNexusLog(QObject::tr("Failed to set API key"));
+ addLog(tr("Failed to set API key"));
}
} else {
if (message.isEmpty()) {
// shouldn't happen
- addNexusLog("Unknown error");
+ addLog("Unknown error");
} else {
- addNexusLog(message);
+ addLog(message);
}
}
- updateNexusState();
+ updateState();
}
-void NexusSettingsTab::addNexusLog(const QString& s)
+void NexusConnectionUI::addLog(const QString& s)
{
- ui->nexusLog->addItem(s);
- ui->nexusLog->scrollToBottom();
+ m_log->addItem(s);
+ m_log->scrollToBottom();
}
-bool NexusSettingsTab::setKey(const QString& key)
+bool NexusConnectionUI::setKey(const QString& key)
{
- dialog().setExitNeeded(Exit::Restart);
- const bool ret = settings().nexus().setApiKey(key);
- updateNexusState();
+ const bool ret = m_settings.nexus().setApiKey(key);
+ updateState();
+
+ emit keyChanged();
+
return ret;
}
-bool NexusSettingsTab::clearKey()
+bool NexusConnectionUI::clearKey()
{
- dialog().setExitNeeded(Exit::Restart);
- const auto ret = settings().nexus().clearApiKey();
+ const auto ret = m_settings.nexus().clearApiKey();
+
+ NexusInterface::instance().getAccessManager()->clearApiKey();
+ updateState();
- NexusInterface::instance(dialog().pluginContainer())->getAccessManager()->clearApiKey();
- updateNexusState();
+ emit keyChanged();
return ret;
}
-void NexusSettingsTab::updateNexusState()
+void NexusConnectionUI::updateState()
{
- updateNexusButtons();
- updateNexusData();
-}
+ auto setButton = [&](QAbstractButton* b, bool enabled, QString caption={}) {
+ if (b) {
+ b->setEnabled(enabled);
+ if (!caption.isEmpty()) {
+ b->setText(caption);
+ }
+ }
+ };
-void NexusSettingsTab::updateNexusButtons()
-{
if (m_nexusLogin && m_nexusLogin->isActive()) {
// api key is in the process of being retrieved
- ui->nexusConnect->setText(QObject::tr("Cancel"));
- ui->nexusConnect->setEnabled(true);
- ui->nexusDisconnect->setEnabled(false);
- ui->nexusManualKey->setText(QObject::tr("Enter API Key Manually"));
- ui->nexusManualKey->setEnabled(false);
+ setButton(m_connect, true, QObject::tr("Cancel"));
+ setButton(m_disconnect, false);
+ setButton(m_manual, false,QObject::tr("Enter API Key Manually"));
}
else if (m_nexusValidator && m_nexusValidator->isActive()) {
// api key is in the process of being tested
- ui->nexusConnect->setText(QObject::tr("Connect to Nexus"));
- ui->nexusConnect->setEnabled(false);
- ui->nexusDisconnect->setEnabled(false);
- ui->nexusManualKey->setText(QObject::tr("Cancel"));
- ui->nexusManualKey->setEnabled(true);
+ setButton(m_connect, false, QObject::tr("Connect to Nexus"));
+ setButton(m_disconnect, false);
+ setButton(m_manual, true, QObject::tr("Cancel"));
}
- else if (settings().nexus().hasApiKey()) {
+ else if (m_settings.nexus().hasApiKey()) {
// api key is present
- ui->nexusConnect->setText(QObject::tr("Connect to Nexus"));
- ui->nexusConnect->setEnabled(false);
- ui->nexusDisconnect->setEnabled(true);
- ui->nexusManualKey->setText(QObject::tr("Enter API Key Manually"));
- ui->nexusManualKey->setEnabled(false);
+ setButton(m_connect, false, QObject::tr("Connect to Nexus"));
+ setButton(m_disconnect, true);
+ setButton(m_manual, false, QObject::tr("Enter API Key Manually"));
} else {
// api key not present
- ui->nexusConnect->setText(QObject::tr("Connect to Nexus"));
- ui->nexusConnect->setEnabled(true);
- ui->nexusDisconnect->setEnabled(false);
- ui->nexusManualKey->setText(QObject::tr("Enter API Key Manually"));
- ui->nexusManualKey->setEnabled(true);
+ setButton(m_connect, true, QObject::tr("Connect to Nexus"));
+ setButton(m_disconnect, false);
+ setButton(m_manual, true, QObject::tr("Enter API Key Manually"));
+ }
+
+ emit stateChanged();
+}
+
+
+
+NexusSettingsTab::NexusSettingsTab(Settings& s, SettingsDialog& d)
+ : SettingsTab(s, d), m_connectionUI(s, &d)
+{
+ ui->offlineBox->setChecked(settings().network().offlineMode());
+ ui->proxyBox->setChecked(settings().network().useProxy());
+ ui->endorsementBox->setChecked(settings().nexus().endorsementIntegration());
+ ui->hideAPICounterBox->setChecked(settings().interface().hideAPICounter());
+
+ // display server preferences
+ for (const auto& server : s.network().servers()) {
+ QString descriptor = server.name();
+
+ if (!descriptor.compare("CDN", Qt::CaseInsensitive)) {
+ descriptor += QStringLiteral(" (automatic)");
+ }
+
+ const auto averageSpeed = server.averageSpeed();
+ if (averageSpeed > 0) {
+ descriptor += QString(" (%1)").arg(MOBase::localizedByteSpeed(averageSpeed));
+ }
+
+ QListWidgetItem *newItem = new ServerItem<int>(descriptor, Qt::UserRole + 1);
+
+ newItem->setData(Qt::UserRole, server.name());
+ newItem->setData(Qt::UserRole + 1, server.preferred());
+
+ if (server.preferred() > 0) {
+ ui->preferredServersList->addItem(newItem);
+ } else {
+ ui->knownServersList->addItem(newItem);
+ }
+
+ ui->preferredServersList->sortItems(Qt::DescendingOrder);
}
+
+ m_connectionUI.set(
+ ui->nexusConnect,
+ ui->nexusDisconnect,
+ ui->nexusManualKey,
+ ui->nexusLog);
+
+ QObject::connect(
+ &m_connectionUI, &NexusConnectionUI::stateChanged, &d,
+ [&]{ updateNexusData(); }, Qt::QueuedConnection);
+
+ QObject::connect(
+ &m_connectionUI, &NexusConnectionUI::keyChanged, &d,
+ [&]{ dialog().setExitNeeded(Exit::Restart); });
+
+
+ QObject::connect(ui->clearCacheButton, &QPushButton::clicked, [&]{ on_clearCacheButton_clicked(); });
+ QObject::connect(ui->associateButton, &QPushButton::clicked, [&]{ on_associateButton_clicked(); });
+}
+
+void NexusSettingsTab::update()
+{
+ settings().network().setOfflineMode(ui->offlineBox->isChecked());
+ settings().network().setUseProxy(ui->proxyBox->isChecked());
+ settings().nexus().setEndorsementIntegration(ui->endorsementBox->isChecked());
+ settings().interface().setHideAPICounter(ui->hideAPICounterBox->isChecked());
+
+ auto servers = settings().network().servers();
+
+ // store server preference
+ for (int i = 0; i < ui->knownServersList->count(); ++i) {
+ const QString key = ui->knownServersList->item(i)->data(Qt::UserRole).toString();
+
+ bool found = false;
+
+ for (auto& server : servers) {
+ if (server.name() == key) {
+ server.setPreferred(0);
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ log::error("while setting preferred to 0, server '{}' not found", key);
+ }
+ }
+
+ const int count = ui->preferredServersList->count();
+
+ for (int i = 0; i < count; ++i) {
+ const QString key = ui->preferredServersList->item(i)->data(Qt::UserRole).toString();
+ const int newPreferred = count - i;
+
+ bool found = false;
+
+ for (auto& server : servers) {
+
+ if (server.name() == key) {
+ server.setPreferred(newPreferred);
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ log::error(
+ "while setting preference to {}, server '{}' not found",
+ newPreferred, key);
+ }
+ }
+
+ settings().network().updateServers(servers);
+}
+
+void NexusSettingsTab::on_clearCacheButton_clicked()
+{
+ QDir(Settings::instance().paths().cache()).removeRecursively();
+ NexusInterface::instance().clearCache();
+}
+
+void NexusSettingsTab::on_associateButton_clicked()
+{
+ Settings::instance().nexus().registerAsNXMHandler(true);
}
void NexusSettingsTab::updateNexusData()
{
- const auto user = NexusInterface::instance(dialog().pluginContainer())
- ->getAPIUserAccount();
+ const auto user = NexusInterface::instance().getAPIUserAccount();
if (user.isValid()) {
ui->nexusUserID->setText(user.id());