Files
amnezia-client/client/ui/pages_logic/ServerSettingsLogic.cpp
T

142 lines
5.3 KiB
C++
Raw Normal View History

2021-09-03 20:17:13 +03:00
#include "ServerSettingsLogic.h"
#include "vpnconnection.h"
2021-09-03 22:15:05 +03:00
#include "../uilogic.h"
2021-09-06 11:44:03 +03:00
#include "ShareConnectionLogic.h"
2021-09-07 21:01:56 +03:00
#include "VpnLogic.h"
2021-09-03 22:15:05 +03:00
#include "core/errorstrings.h"
#include <core/servercontroller.h>
2022-02-22 02:08:57 +03:00
#include <QTimer>
2021-09-03 20:17:13 +03:00
2022-09-19 12:27:33 +03:00
#if defined(Q_OS_ANDROID)
2022-12-27 15:37:58 +03:00
#include "../../platforms/android/androidutils.h"
2022-09-19 12:27:33 +03:00
#endif
2021-09-07 21:01:56 +03:00
ServerSettingsLogic::ServerSettingsLogic(UiLogic *logic, QObject *parent):
PageLogicBase(logic, parent),
2021-09-08 14:23:02 +03:00
m_labelWaitInfoVisible{true},
m_pushButtonClearClientCacheVisible{true},
m_pushButtonShareFullVisible{true},
m_pushButtonClearClientCacheText{tr("Clear client cached profile")}
2022-12-23 17:32:20 +03:00
{ }
2021-09-03 22:15:05 +03:00
void ServerSettingsLogic::onUpdatePage()
2021-09-03 22:15:05 +03:00
{
2021-09-08 14:23:02 +03:00
set_labelWaitInfoVisible(false);
set_labelWaitInfoText("");
2023-02-20 09:46:50 +03:00
set_pushButtonClearClientCacheVisible(m_settings->haveAuthData(uiLogic()->m_selectedServerIndex));
set_pushButtonShareFullVisible(m_settings->haveAuthData(uiLogic()->m_selectedServerIndex));
const QJsonObject &server = m_settings->server(uiLogic()->m_selectedServerIndex);
2022-02-01 19:48:59 +03:00
const QString &port = server.value(config_key::port).toString();
2023-01-08 21:24:06 +00:00
const QString &userName = server.value(config_key::userName).toString();
const QString &hostName = server.value(config_key::hostName).toString();
QString name = QString("%1%2%3%4%5")
.arg(userName)
.arg(userName.isEmpty() ? "" : "@")
.arg(hostName)
.arg(port.isEmpty() ? "" : ":")
.arg(port);
set_labelServerText(name);
2021-09-08 14:23:02 +03:00
set_lineEditDescriptionText(server.value(config_key::description).toString());
2021-11-19 23:04:35 +03:00
2023-02-20 09:46:50 +03:00
DockerContainer selectedContainer = m_settings->defaultContainer(uiLogic()->m_selectedServerIndex);
2021-11-19 23:04:35 +03:00
QString selectedContainerName = ContainerProps::containerHumanNames().value(selectedContainer);
2021-11-17 15:01:48 +03:00
set_labelCurrentVpnProtocolText(tr("Service: ") + selectedContainerName);
2021-09-03 22:15:05 +03:00
}
2021-09-08 14:23:02 +03:00
void ServerSettingsLogic::onPushButtonForgetServer()
2021-09-03 22:15:05 +03:00
{
2023-02-20 09:46:50 +03:00
if (m_settings->defaultServerIndex() == uiLogic()->m_selectedServerIndex && uiLogic()->m_vpnConnection->isConnected()) {
2022-08-29 01:32:42 +03:00
uiLogic()->pageLogic<VpnLogic>()->onDisconnect();
2021-09-03 22:15:05 +03:00
}
2023-02-20 09:46:50 +03:00
m_settings->removeServer(uiLogic()->m_selectedServerIndex);
2021-09-03 22:15:05 +03:00
2023-02-20 09:46:50 +03:00
if (m_settings->defaultServerIndex() == uiLogic()->m_selectedServerIndex) {
2022-08-25 12:47:02 +03:00
m_settings->setDefaultServer(0);
2021-09-03 22:15:05 +03:00
}
2023-02-20 09:46:50 +03:00
else if (m_settings->defaultServerIndex() > uiLogic()->m_selectedServerIndex) {
2022-08-25 12:47:02 +03:00
m_settings->setDefaultServer(m_settings->defaultServerIndex() - 1);
2021-09-03 22:15:05 +03:00
}
2022-08-25 12:47:02 +03:00
if (m_settings->serversCount() == 0) {
m_settings->setDefaultServer(-1);
2021-09-03 22:15:05 +03:00
}
2023-02-20 09:46:50 +03:00
uiLogic()->m_selectedServerIndex = -1;
2021-11-17 15:01:48 +03:00
uiLogic()->onUpdateAllPages();
2021-09-03 22:15:05 +03:00
2022-08-25 12:47:02 +03:00
if (m_settings->serversCount() == 0) {
2021-09-07 21:01:56 +03:00
uiLogic()->setStartPage(Page::Start);
2021-09-03 22:15:05 +03:00
}
else {
2021-09-07 21:01:56 +03:00
uiLogic()->closePage();
2021-09-03 22:15:05 +03:00
}
}
2021-09-08 14:23:02 +03:00
void ServerSettingsLogic::onPushButtonClearClientCacheClicked()
2021-09-03 22:15:05 +03:00
{
2021-09-08 14:23:02 +03:00
set_pushButtonClearClientCacheText(tr("Cache cleared"));
2021-09-03 22:15:05 +03:00
2023-02-20 09:46:50 +03:00
const auto &containers = m_settings->containers(uiLogic()->m_selectedServerIndex);
for (DockerContainer container : containers.keys()) {
m_settings->clearLastConnectionConfig(uiLogic()->m_selectedServerIndex, container);
2021-09-03 22:15:05 +03:00
}
QTimer::singleShot(3000, this, [this]() {
2021-09-08 14:23:02 +03:00
set_pushButtonClearClientCacheText(tr("Clear client cached profile"));
2021-09-03 22:15:05 +03:00
});
}
2021-09-08 14:23:02 +03:00
void ServerSettingsLogic::onLineEditDescriptionEditingFinished()
2021-09-03 22:15:05 +03:00
{
2021-09-08 14:23:02 +03:00
const QString &newText = lineEditDescriptionText();
2023-02-20 09:46:50 +03:00
QJsonObject server = m_settings->server(uiLogic()->m_selectedServerIndex);
2021-09-03 22:15:05 +03:00
server.insert(config_key::description, newText);
2023-02-20 09:46:50 +03:00
m_settings->editServer(uiLogic()->m_selectedServerIndex, server);
2021-11-17 15:01:48 +03:00
uiLogic()->onUpdateAllPages();
2021-09-03 22:15:05 +03:00
}
2022-09-19 12:27:33 +03:00
#if defined(Q_OS_ANDROID)
/* Auth result handler for Android */
2022-12-23 17:32:20 +03:00
void authResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QJniObject &data)
2022-09-19 12:27:33 +03:00
{
qDebug() << "receiverRequestCode" << receiverRequestCode << "resultCode" << resultCode;
if (resultCode == -1) { //ResultOK
uiLogic()->pageLogic<ShareConnectionLogic>()->updateSharingPage(m_serverIndex, DockerContainer::None);
emit uiLogic()->goToShareProtocolPage(Proto::Any);
}
}
#endif
2021-09-08 14:23:02 +03:00
void ServerSettingsLogic::onPushButtonShareFullClicked()
2021-09-03 22:15:05 +03:00
{
2022-09-19 12:27:33 +03:00
#if defined(Q_OS_ANDROID)
/* We use builtin keyguard for ssh key export protection on Android */
2022-12-23 17:32:20 +03:00
QJniObject activity = AndroidUtils::getActivity();
auto appContext = activity.callObjectMethod(
2022-09-19 12:27:33 +03:00
"getApplicationContext", "()Landroid/content/Context;");
if (appContext.isValid()) {
2023-02-25 18:48:10 +03:00
QAndroidActivityResultReceiver *receiver = new authResultReceiver(uiLogic(), uiLogic()->m_selectedServerIndex);
2022-12-23 17:32:20 +03:00
auto intent = QJniObject::callStaticObjectMethod(
2022-09-19 12:27:33 +03:00
"org/amnezia/vpn/AuthHelper", "getAuthIntent",
"(Landroid/content/Context;)Landroid/content/Intent;", appContext.object());
if (intent.isValid()) {
if (intent.object<jobject>() != nullptr) {
2022-12-23 17:32:20 +03:00
QtAndroidPrivate::startActivity(intent.object<jobject>(), 1, receiver);
2022-09-19 12:27:33 +03:00
}
} else {
2023-02-25 18:48:10 +03:00
uiLogic()->pageLogic<ShareConnectionLogic>()->updateSharingPage(uiLogic()->m_selectedServerIndex, DockerContainer::None);
2022-09-19 12:27:33 +03:00
emit uiLogic()->goToShareProtocolPage(Proto::Any);
}
}
#else
2023-02-20 09:46:50 +03:00
uiLogic()->pageLogic<ShareConnectionLogic>()->updateSharingPage(uiLogic()->m_selectedServerIndex, DockerContainer::None);
emit uiLogic()->goToShareProtocolPage(Proto::Any);
2022-09-19 12:27:33 +03:00
#endif
2021-09-03 20:17:13 +03:00
}