forked from feedc0de/evcharger-app
202 lines
6.4 KiB
C++
202 lines
6.4 KiB
C++
#include "appsettings.h"
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
namespace
|
|
{
|
|
QByteArray basicAuthorization(const QString &username, const QString &password)
|
|
{
|
|
QByteArray credentials = username.toUtf8();
|
|
credentials.append(':');
|
|
credentials.append(password.toUtf8());
|
|
return "Basic " + credentials.toBase64();
|
|
}
|
|
}
|
|
|
|
AppSettings::AppSettings()
|
|
{
|
|
m_solalawebVerificationTimeout.setSingleShot(true);
|
|
m_solalawebVerificationTimeout.setInterval(15s);
|
|
|
|
connect(&m_solalawebVerificationTimeout, &QTimer::timeout,
|
|
this, [this] {
|
|
finishSolalawebVerification(false, tr("The Solalaweb authentication check timed out."));
|
|
});
|
|
connect(&m_solalawebVerificationSocket, &QWebSocket::connected,
|
|
this, [this] {
|
|
finishSolalawebVerification(true);
|
|
});
|
|
connect(&m_solalawebVerificationSocket, &QWebSocket::errorOccurred,
|
|
this, [this](QAbstractSocket::SocketError) {
|
|
finishSolalawebVerification(
|
|
false,
|
|
tr("Authentication could not be verified: %1")
|
|
.arg(m_solalawebVerificationSocket.errorString()));
|
|
});
|
|
|
|
// Old installations may still have the visibility flag set from the
|
|
// certificate-based implementation. Never expose Solalaweb until the new
|
|
// Authentik credentials have completed a successful check.
|
|
if (!solalawebCredentialsVerified() && showSolalaweb())
|
|
setShowSolalaweb(false);
|
|
}
|
|
|
|
QLocale::Language AppSettings::uiLanguage() const
|
|
{
|
|
const QVariant storedLanguage = value("uiLanguage");
|
|
bool isNumber = false;
|
|
const int language = storedLanguage.toInt(&isNumber);
|
|
|
|
if (isNumber && language >= QLocale::AnyLanguage && language <= QLocale::LastLanguage)
|
|
return static_cast<QLocale::Language>(language);
|
|
|
|
// Also understand string values written by development versions.
|
|
if (storedLanguage.toString() == QStringLiteral("de"))
|
|
return QLocale::German;
|
|
if (storedLanguage.toString() == QStringLiteral("en"))
|
|
return QLocale::English;
|
|
if (storedLanguage.toString() == QStringLiteral("sv"))
|
|
return QLocale::Swedish;
|
|
if (storedLanguage.toString() == QStringLiteral("fi"))
|
|
return QLocale::Finnish;
|
|
if (storedLanguage.toString() == QStringLiteral("nb")
|
|
|| storedLanguage.toString() == QStringLiteral("no"))
|
|
return QLocale::NorwegianBokmal;
|
|
if (storedLanguage.toString() == QStringLiteral("da"))
|
|
return QLocale::Danish;
|
|
if (storedLanguage.toString() == QStringLiteral("tr"))
|
|
return QLocale::Turkish;
|
|
|
|
return QLocale::AnyLanguage;
|
|
}
|
|
|
|
void AppSettings::setUiLanguage(QLocale::Language uiLanguage)
|
|
{
|
|
if (uiLanguage < QLocale::AnyLanguage || uiLanguage > QLocale::LastLanguage)
|
|
return;
|
|
|
|
if (uiLanguage == this->uiLanguage())
|
|
return;
|
|
|
|
if (uiLanguage == QLocale::AnyLanguage)
|
|
remove("uiLanguage");
|
|
else
|
|
setValue("uiLanguage", static_cast<int>(uiLanguage));
|
|
|
|
emit uiLanguageChanged(uiLanguage);
|
|
}
|
|
|
|
QString AppSettings::solalawebUsername() const
|
|
{
|
|
return value("solalawebUsername").toString();
|
|
}
|
|
|
|
QString AppSettings::solalawebPassword() const
|
|
{
|
|
return value("solalawebPassword").toString();
|
|
}
|
|
|
|
bool AppSettings::solalawebCredentialsVerified() const
|
|
{
|
|
return value("solalawebCredentialsVerified", false).toBool()
|
|
&& !solalawebUsername().isEmpty()
|
|
&& !solalawebPassword().isEmpty();
|
|
}
|
|
|
|
void AppSettings::verifySolalawebCredentials(const QString &username,
|
|
const QString &appPassword)
|
|
{
|
|
if (m_solalawebVerificationInProgress)
|
|
return;
|
|
|
|
m_pendingSolalawebUsername = username.trimmed();
|
|
m_pendingSolalawebPassword = appPassword;
|
|
if (m_pendingSolalawebUsername.isEmpty() || m_pendingSolalawebPassword.isEmpty())
|
|
{
|
|
setSolalawebVerificationError(tr("Enter both the username and the app password."));
|
|
emit solalawebVerificationFinished(false);
|
|
return;
|
|
}
|
|
|
|
setSolalawebVerificationError({});
|
|
m_solalawebVerificationInProgress = true;
|
|
emit solalawebVerificationInProgressChanged(true);
|
|
|
|
QNetworkRequest request{QUrl{QStringLiteral("wss://solalaweb.com/123456")}};
|
|
request.setRawHeader("Authorization",
|
|
basicAuthorization(m_pendingSolalawebUsername,
|
|
m_pendingSolalawebPassword));
|
|
m_solalawebVerificationTimeout.start();
|
|
m_solalawebVerificationSocket.open(request);
|
|
}
|
|
|
|
void AppSettings::clearSolalawebCredentials()
|
|
{
|
|
const auto oldUsername = solalawebUsername();
|
|
remove("solalawebUsername");
|
|
remove("solalawebPassword");
|
|
remove("solalawebCredentialsVerified");
|
|
setShowSolalaweb(false);
|
|
setSolalawebVerificationError({});
|
|
if (!oldUsername.isEmpty())
|
|
emit solalawebUsernameChanged({});
|
|
emit solalawebCredentialsVerifiedChanged(false);
|
|
}
|
|
|
|
void AppSettings::invalidateSolalawebCredentials(const QString &reason)
|
|
{
|
|
setSolalawebCredentialsVerified(false);
|
|
setShowSolalaweb(false);
|
|
setSolalawebVerificationError(reason);
|
|
}
|
|
|
|
void AppSettings::finishSolalawebVerification(bool success, const QString &error)
|
|
{
|
|
if (!m_solalawebVerificationInProgress)
|
|
return;
|
|
|
|
m_solalawebVerificationInProgress = false;
|
|
emit solalawebVerificationInProgressChanged(false);
|
|
m_solalawebVerificationTimeout.stop();
|
|
|
|
if (success)
|
|
{
|
|
const auto oldUsername = solalawebUsername();
|
|
setValue("solalawebUsername", m_pendingSolalawebUsername);
|
|
setValue("solalawebPassword", m_pendingSolalawebPassword);
|
|
if (oldUsername != m_pendingSolalawebUsername)
|
|
emit solalawebUsernameChanged(m_pendingSolalawebUsername);
|
|
setSolalawebCredentialsVerified(true);
|
|
setSolalawebVerificationError({});
|
|
}
|
|
else
|
|
{
|
|
setSolalawebVerificationError(error);
|
|
}
|
|
|
|
m_pendingSolalawebUsername.clear();
|
|
m_pendingSolalawebPassword.clear();
|
|
m_solalawebVerificationSocket.abort();
|
|
emit solalawebVerificationFinished(success);
|
|
}
|
|
|
|
void AppSettings::setSolalawebCredentialsVerified(bool verified)
|
|
{
|
|
if (solalawebCredentialsVerified() == verified)
|
|
return;
|
|
|
|
setValue("solalawebCredentialsVerified", verified);
|
|
emit solalawebCredentialsVerifiedChanged(verified);
|
|
}
|
|
|
|
void AppSettings::setSolalawebVerificationError(const QString &error)
|
|
{
|
|
if (m_solalawebVerificationError == error)
|
|
return;
|
|
|
|
m_solalawebVerificationError = error;
|
|
emit solalawebVerificationErrorChanged(error);
|
|
}
|