summaryrefslogtreecommitdiff
path: root/src/settingsdialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/settingsdialog.cpp')
-rw-r--r--src/settingsdialog.cpp122
1 files changed, 110 insertions, 12 deletions
diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp
index 1bbc256f..ee7edead 100644
--- a/src/settingsdialog.cpp
+++ b/src/settingsdialog.cpp
@@ -29,12 +29,17 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#include "nexusinterface.h"
#include "plugincontainer.h"
+#include <boost/uuid/uuid_generators.hpp>
+#include <boost/uuid/uuid_io.hpp>
+
#include <QDirIterator>
#include <QFileDialog>
#include <QMessageBox>
#include <QShortcut>
#include <QColorDialog>
#include <QInputDialog>
+#include <QJsonDocument>
+#include <QDesktopServices>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
@@ -46,6 +51,8 @@ SettingsDialog::SettingsDialog(PluginContainer *pluginContainer, QWidget *parent
: TutorableDialog("SettingsDialog", parent)
, ui(new Ui::SettingsDialog)
, m_PluginContainer(pluginContainer)
+ , m_nexusLogin(new QWebSocket)
+ , m_KeyReceived(false)
{
ui->setupUi(this);
ui->pluginSettingsList->setStyleSheet("QTreeWidget::item {padding-right: 10px;}");
@@ -53,10 +60,19 @@ SettingsDialog::SettingsDialog(PluginContainer *pluginContainer, QWidget *parent
QShortcut *delShortcut
= new QShortcut(QKeySequence(Qt::Key_Delete), ui->pluginBlacklist);
connect(delShortcut, SIGNAL(activated()), this, SLOT(deleteBlacklistItem()));
+ connect(m_nexusLogin, SIGNAL(connected()), this, SLOT(dispatchLogin()));
+ connect(m_nexusLogin, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(authError(QAbstractSocket::SocketError)));
+ connect(m_nexusLogin, SIGNAL(textMessageReceived(const QString &)), this, SLOT(receiveApiKey(const QString &)));
+ connect(m_nexusLogin, SIGNAL(disconnected()), this, SLOT(completeApiConnection()));
+ connect(this, SIGNAL(retryApiConnection()), this, SLOT(on_nexusConnect_clicked()));
+ m_loginTimer.callOnTimeout(this, &SettingsDialog::loginPing);
}
SettingsDialog::~SettingsDialog()
{
+ m_loginTimer.stop();
+ m_nexusLogin->close();
+ disconnect(this);
delete ui;
}
@@ -115,18 +131,18 @@ bool SettingsDialog::getResetGeometries()
return ui->resetGeometryBtn->isChecked();
}
-void SettingsDialog::on_loginCheckBox_toggled(bool checked)
-{
- QLineEdit *usernameEdit = findChild<QLineEdit*>("usernameEdit");
- QLineEdit *passwordEdit = findChild<QLineEdit*>("passwordEdit");
- if (checked) {
- passwordEdit->setEnabled(true);
- usernameEdit->setEnabled(true);
- } else {
- passwordEdit->setEnabled(false);
- usernameEdit->setEnabled(false);
- }
-}
+//void SettingsDialog::on_loginCheckBox_toggled(bool checked)
+//{
+// QLineEdit *usernameEdit = findChild<QLineEdit*>("usernameEdit");
+// QLineEdit *passwordEdit = findChild<QLineEdit*>("passwordEdit");
+// if (checked) {
+// passwordEdit->setEnabled(true);
+// usernameEdit->setEnabled(true);
+// } else {
+// passwordEdit->setEnabled(false);
+// usernameEdit->setEnabled(false);
+// }
+//}
void SettingsDialog::on_categoriesBtn_clicked()
{
@@ -326,6 +342,83 @@ void SettingsDialog::on_resetDialogsButton_clicked()
}
}
+void SettingsDialog::on_nexusConnect_clicked()
+{
+ ui->nexusConnect->setText("Connecting the API. Please login within the browser and accept the request. This will time out after 30 minutes.");
+ ui->nexusConnect->setDisabled(true);
+ QUrl url = QUrl("wss://sso.nexusmods.com");
+ m_nexusLogin->open(url);
+}
+
+void SettingsDialog::dispatchLogin()
+{
+ m_KeyReceived = false;
+ QJsonObject login;
+ if (m_UUID.isEmpty()) {
+ boost::uuids::random_generator generator;
+ boost::uuids::uuid sessionId = generator();
+ m_UUID = boost::uuids::to_string(sessionId).c_str();
+ }
+ login.insert(QString("id"), QJsonValue(m_UUID));
+ login.insert(QString("token"), QJsonValue(m_AuthToken));
+ login.insert(QString("protocol"), 2);
+ QJsonDocument loginDoc(login);
+ QString finalMessage(loginDoc.toJson());
+ m_nexusLogin->sendTextMessage(finalMessage);
+ QDesktopServices::openUrl(QUrl(QString("https://www.nexusmods.com/sso?id=%1&application=%2").arg(m_UUID).arg("modorganizer2")));
+ m_loginTimer.start(30000);
+}
+
+void SettingsDialog::loginPing()
+{
+ if (m_nexusLogin->isValid()) {
+ m_nexusLogin->ping();
+ m_totalPings++;
+ }
+ if (m_totalPings >= 60) {
+ m_loginTimer.stop();
+ m_totalPings = 0;
+ m_nexusLogin->close(QWebSocketProtocol::CloseCodeGoingAway, "Timeout: No response received after thirty minutes. Cancelling request.");
+ }
+}
+
+void SettingsDialog::authError(QAbstractSocket::SocketError error)
+{
+ auto errorInfo = m_nexusLogin->errorString();
+ qCritical() << "An error occurred: " << errorInfo;
+}
+
+void SettingsDialog::receiveApiKey(const QString &response)
+{
+ QJsonDocument responseDoc = QJsonDocument::fromJson(response.toUtf8());
+ QVariantMap responseData = responseDoc.object().toVariantMap();
+ if (responseData["success"].toBool()) {
+ QVariantMap data = responseData["data"].toMap();
+ if (data.contains("connection_token")) {
+ m_AuthToken = data["connection_token"].toString();
+ } else {
+ m_KeyReceived = true;
+ emit processApiKey(data["api_key"].toString());
+ m_nexusLogin->close();
+ ui->nexusConnect->setText("Nexus API Key Stored");
+ m_loginTimer.stop();
+ m_totalPings = 0;
+ }
+ } else {
+ QString error("There was a problem with SSO initialization: %1");
+ qCritical() << error.arg(responseData["error"].toString());
+ m_nexusLogin->close();
+ }
+}
+
+void SettingsDialog::completeApiConnection()
+{
+ if (m_KeyReceived == true || !m_loginTimer.isActive())
+ emit closeApiConnection(ui->nexusConnect);
+ else
+ emit retryApiConnection();
+}
+
void SettingsDialog::storeSettings(QListWidgetItem *pluginItem)
{
if (pluginItem != nullptr) {
@@ -393,6 +486,11 @@ void SettingsDialog::on_clearCacheButton_clicked()
NexusInterface::instance(m_PluginContainer)->clearCache();
}
+void SettingsDialog::on_revokeNexusAuthButton_clicked()
+{
+ emit revokeApiKey(ui->nexusConnect);
+}
+
void SettingsDialog::normalizePath(QLineEdit *lineEdit)
{
QString text = lineEdit->text();