added connectionController and ConnectionButton.qml

This commit is contained in:
vladimir.kuznetsov 2023-05-12 23:54:31 +08:00
parent dd0de7e8be
commit 35d4222c7a
17 changed files with 233 additions and 60 deletions

View file

@ -128,17 +128,22 @@ file(GLOB CONFIGURATORS_CPP CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/configur
file(GLOB UI_MODELS_H CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/models/*.h) file(GLOB UI_MODELS_H CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/models/*.h)
file(GLOB UI_MODELS_CPP CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/models/*.cpp) file(GLOB UI_MODELS_CPP CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/models/*.cpp)
file(GLOB UI_CONTROLLERS_H CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/controllers/*.h)
file(GLOB UI_CONTROLLERS_CPP CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/ui/controllers/*.cpp)
set(HEADERS ${HEADERS} set(HEADERS ${HEADERS}
${COMMON_FILES_H} ${COMMON_FILES_H}
${PAGE_LOGIC_H} ${PAGE_LOGIC_H}
${CONFIGURATORS_H} ${CONFIGURATORS_H}
${UI_MODELS_H} ${UI_MODELS_H}
${UI_CONTROLLERS_H}
) )
set(SOURCES ${SOURCES} set(SOURCES ${SOURCES}
${COMMON_FILES_CPP} ${COMMON_FILES_CPP}
${PAGE_LOGIC_CPP} ${PAGE_LOGIC_CPP}
${CONFIGURATORS_CPP} ${CONFIGURATORS_CPP}
${UI_MODELS_CPP} ${UI_MODELS_CPP}
${UI_CONTROLLERS_CPP}
) )
qt6_add_resources(QRC ${QRC} ${CMAKE_CURRENT_LIST_DIR}/resources.qrc) qt6_add_resources(QRC ${QRC} ${CMAKE_CURRENT_LIST_DIR}/resources.qrc)

View file

@ -5,7 +5,6 @@
#include <QTimer> #include <QTimer>
#include <QTranslator> #include <QTranslator>
#include "core/servercontroller.h" #include "core/servercontroller.h"
#include "logger.h" #include "logger.h"
#include "defines.h" #include "defines.h"
@ -100,12 +99,23 @@ void AmneziaApplication::init()
m_engine->rootContext()->setContextProperty("Debug", &Logger::Instance()); m_engine->rootContext()->setContextProperty("Debug", &Logger::Instance());
//
m_containersModel.reset(new ContainersModel(m_settings, this)); m_containersModel.reset(new ContainersModel(m_settings, this));
m_engine->rootContext()->setContextProperty("ContainersModel", m_containersModel.get()); m_engine->rootContext()->setContextProperty("ContainersModel", m_containersModel.get());
m_serversModel.reset(new ServersModel(m_settings, this)); m_serversModel.reset(new ServersModel(m_settings, this));
m_engine->rootContext()->setContextProperty("ServersModel", m_serversModel.get()); m_engine->rootContext()->setContextProperty("ServersModel", m_serversModel.get());
m_vpnConnection.reset(new VpnConnection(m_settings, m_configurator));
m_connectionController.reset(new ConnectionController(m_serversModel, m_containersModel));
connect(m_connectionController.get(), &ConnectionController::connectToVpn,
m_vpnConnection.get(), &VpnConnection::connectToVpn, Qt::QueuedConnection);
connect(m_connectionController.get(), &ConnectionController::disconnectFromVpn,
m_vpnConnection.get(), &VpnConnection::disconnectFromVpn, Qt::QueuedConnection);
m_engine->rootContext()->setContextProperty("ConnectionController", m_connectionController.get());
//
m_uiLogic->registerPagesLogic(); m_uiLogic->registerPagesLogic();
#if defined(Q_OS_IOS) #if defined(Q_OS_IOS)

View file

@ -9,11 +9,13 @@
#include <QQmlContext> #include <QQmlContext>
#include "settings.h" #include "settings.h"
#include "vpnconnection.h"
#include "ui/uilogic.h" #include "ui/uilogic.h"
#include "configurators/vpn_configurator.h" #include "configurators/vpn_configurator.h"
#include "ui/models/servers_model.h" #include "ui/models/servers_model.h"
#include "ui/models/containers_model.h" #include "ui/models/containers_model.h"
#include "ui/controllers/connectionController.h"
#define amnApp (static_cast<AmneziaApplication *>(QCoreApplication::instance())) #define amnApp (static_cast<AmneziaApplication *>(QCoreApplication::instance()))
@ -58,8 +60,12 @@ private:
QTranslator* m_translator; QTranslator* m_translator;
QCommandLineParser m_parser; QCommandLineParser m_parser;
QScopedPointer<ContainersModel> m_containersModel; QSharedPointer<ContainersModel> m_containersModel;
QScopedPointer<ServersModel> m_serversModel; QSharedPointer<ServersModel> m_serversModel;
QScopedPointer<VpnConnection> m_vpnConnection;
QScopedPointer<ConnectionController> m_connectionController;
}; };

View file

@ -222,5 +222,6 @@
<file>ui/qml/Controls2/TextTypes/ButtonTextType.qml</file> <file>ui/qml/Controls2/TextTypes/ButtonTextType.qml</file>
<file>ui/qml/Controls2/Header2Type.qml</file> <file>ui/qml/Controls2/Header2Type.qml</file>
<file>images/controls/plus.svg</file> <file>images/controls/plus.svg</file>
<file>ui/qml/Components/ConnectButton.qml</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -0,0 +1,71 @@
#include "connectionController.h"
#include <QApplication>
ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
QObject *parent) : QObject(parent), m_serversModel(serversModel), m_containersModel(containersModel)
{
}
bool ConnectionController::onConnectionButtonClicked()
{
if (!isConnected) {
openVpnConnection();
} else {
closeVpnConnection();
}
}
bool ConnectionController::openVpnConnection()
{
int serverIndex = m_serversModel->getDefaultServerIndex();
QModelIndex serverModelIndex = m_serversModel->index(serverIndex);
ServerCredentials credentials = qvariant_cast<ServerCredentials>(m_serversModel->data(serverModelIndex,
ServersModel::ServersModelRoles::CredentialsRole));
DockerContainer container = m_containersModel->getDefaultContainer();
QModelIndex containerModelIndex = m_containersModel->index(container);
const QJsonObject &containerConfig = qvariant_cast<QJsonObject>(m_containersModel->data(containerModelIndex,
ContainersModel::ContainersModelRoles::ConfigRole));
//todo error handling
qApp->processEvents();
emit connectToVpn(serverIndex, credentials, container, containerConfig);
isConnected = true;
// int serverIndex = m_settings->defaultServerIndex();
// ServerCredentials credentials = m_settings->serverCredentials(serverIndex);
// DockerContainer container = m_settings->defaultContainer(serverIndex);
// if (m_settings->containers(serverIndex).isEmpty()) {
// set_labelErrorText(tr("VPN Protocols is not installed.\n Please install VPN container at first"));
// set_pushButtonConnectChecked(false);
// return;
// }
// if (container == DockerContainer::None) {
// set_labelErrorText(tr("VPN Protocol not chosen"));
// set_pushButtonConnectChecked(false);
// return;
// }
// const QJsonObject &containerConfig = m_settings->containerConfig(serverIndex, container);
// set_labelErrorText("");
// set_pushButtonConnectChecked(true);
// set_pushButtonConnectEnabled(false);
// qApp->processEvents();
// emit connectToVpn(serverIndex, credentials, container, containerConfig);
}
bool ConnectionController::closeVpnConnection()
{
emit disconnectFromVpn();
}

View file

@ -0,0 +1,33 @@
#ifndef CONNECTIONCONTROLLER_H
#define CONNECTIONCONTROLLER_H
#include "ui/models/servers_model.h"
#include "ui/models/containers_model.h"
class ConnectionController : public QObject
{
Q_OBJECT
public:
explicit ConnectionController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
QObject *parent = nullptr);
public slots:
bool onConnectionButtonClicked();
signals:
void connectToVpn(int serverIndex, const ServerCredentials &credentials, DockerContainer container, const QJsonObject &containerConfig);
void disconnectFromVpn();
private:
bool openVpnConnection();
bool closeVpnConnection();
QSharedPointer<ServersModel> m_serversModel;
QSharedPointer<ContainersModel> m_containersModel;
bool isConnected = false;
};
#endif // CONNECTIONCONTROLLER_H

View file

@ -17,7 +17,7 @@ bool ContainersModel::setData(const QModelIndex &index, const QVariant &value, i
return false; return false;
} }
if (role == DefaultRole) { if (role == IsDefaultRole) {
DockerContainer container = ContainerProps::allContainers().at(index.row()); DockerContainer container = ContainerProps::allContainers().at(index.row());
m_settings->setDefaultContainer(m_selectedServerIndex, container); m_settings->setDefaultContainer(m_selectedServerIndex, container);
} }
@ -33,22 +33,23 @@ QVariant ContainersModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
} }
DockerContainer c = ContainerProps::allContainers().at(index.row()); DockerContainer container = ContainerProps::allContainers().at(index.row());
if (role == NameRole) {
return ContainerProps::containerHumanNames().value(c); switch (role) {
} case NameRole:
if (role == DescRole) { return ContainerProps::containerHumanNames().value(container);
return ContainerProps::containerDescriptions().value(c); case DescRole:
} return ContainerProps::containerDescriptions().value(container);
if (role == DefaultRole) { case ConfigRole:
return c == m_settings->defaultContainer(m_selectedServerIndex); return m_settings->containerConfig(m_selectedServerIndex, container);
} case ServiceTypeRole:
if (role == ServiceTypeRole) { return ContainerProps::containerService(container);
return ContainerProps::containerService(c); case IsInstalledRole:
} return m_settings->containers(m_selectedServerIndex).contains(container);
if (role == IsInstalledRole) { case IsDefaultRole:
return m_settings->containers(m_selectedServerIndex).contains(c); return container == m_settings->defaultContainer(m_selectedServerIndex);
} }
return QVariant(); return QVariant();
} }
@ -71,12 +72,17 @@ QString ContainersModel::getCurrentlyInstalledContainerName()
return data(m_currentlyInstalledContainerIndex, NameRole).toString(); return data(m_currentlyInstalledContainerIndex, NameRole).toString();
} }
DockerContainer ContainersModel::getDefaultContainer()
{
return m_settings->defaultContainer(m_selectedServerIndex);
}
QHash<int, QByteArray> ContainersModel::roleNames() const { QHash<int, QByteArray> ContainersModel::roleNames() const {
QHash<int, QByteArray> roles; QHash<int, QByteArray> roles;
roles[NameRole] = "name_role"; roles[NameRole] = "name";
roles[DescRole] = "desc_role"; roles[DescRole] = "description";
roles[DefaultRole] = "default_role"; roles[ServiceTypeRole] = "serviceType";
roles[ServiceTypeRole] = "service_type_role"; roles[IsInstalledRole] = "isInstalled";
roles[IsInstalledRole] = "is_installed_role"; roles[IsDefaultRole] = "isDefault";
return roles; return roles;
} }

View file

@ -15,12 +15,13 @@ class ContainersModel : public QAbstractListModel
public: public:
ContainersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr); ContainersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
public: public:
enum SiteRoles { enum ContainersModelRoles {
NameRole = Qt::UserRole + 1, NameRole = Qt::UserRole + 1,
DescRole, DescRole,
DefaultRole,
ServiceTypeRole, ServiceTypeRole,
IsInstalledRole ConfigRole,
IsInstalledRole,
IsDefaultRole
}; };
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
@ -32,6 +33,9 @@ public:
Q_INVOKABLE QString getCurrentlyInstalledContainerName(); Q_INVOKABLE QString getCurrentlyInstalledContainerName();
public slots:
DockerContainer getDefaultContainer();
protected: protected:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;

View file

@ -37,36 +37,23 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
const QJsonArray &servers = m_settings->serversArray(); const QJsonArray &servers = m_settings->serversArray();
const QJsonObject server = servers.at(index.row()).toObject(); const QJsonObject server = servers.at(index.row()).toObject();
if (role == DescRole) { switch (role) {
case DescRole: {
auto description = server.value(config_key::description).toString(); auto description = server.value(config_key::description).toString();
if (description.isEmpty()) { if (description.isEmpty()) {
return server.value(config_key::hostName).toString(); return server.value(config_key::hostName).toString();
} }
return description; return description;
} }
if (role == AddressRole) { case AddressRole:
return server.value(config_key::hostName).toString(); return server.value(config_key::hostName).toString();
} case CredentialsRole:
if (role == IsDefaultRole) { return QVariant::fromValue(m_settings->serverCredentials(index.row()));
case IsDefaultRole:
return index.row() == m_settings->defaultServerIndex(); return index.row() == m_settings->defaultServerIndex();
} }
return QVariant(); return QVariant();
// if (!index.isValid() || index.row() < 0
// || index.row() >= static_cast<int>(m_data.size())) {
// return QVariant();
// }
// if (role == DescRole) {
// return m_data[index.row()].desc;
// }
// if (role == AddressRole) {
// return m_data[index.row()].address;
// }
// if (role == IsDefaultRole) {
// return m_data[index.row()].isDefault;
// }
// return QVariant();
} }
void ServersModel::setDefaultServerIndex(int index) void ServersModel::setDefaultServerIndex(int index)

View file

@ -18,6 +18,7 @@ public:
enum ServersModelRoles { enum ServersModelRoles {
DescRole = Qt::UserRole + 1, DescRole = Qt::UserRole + 1,
AddressRole, AddressRole,
CredentialsRole,
IsDefaultRole IsDefaultRole
}; };

View file

@ -0,0 +1,40 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Button {
id: root
implicitHeight: 190
implicitWidth: 190
background: Rectangle {
id: background
radius: parent.width * 0.5
color: "transparent"
border.width: 2
border.color: "white"
}
contentItem: Text {
height: 24
font.family: "PT Root UI VF"
font.weight: 700
font.pixelSize: 20
color: "#D7D8DB"
text: root.text
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
onClicked: {
background.color = "red"
ConnectionController.onConnectionButtonClicked()
}
}

View file

@ -5,7 +5,7 @@ Text {
color: "#D7D8DB" color: "#D7D8DB"
font.pixelSize: 16 font.pixelSize: 16
font.weight: 500 font.weight: Font.Medium
font.family: "PT Root UI VF" font.family: "PT Root UI VF"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap

View file

@ -5,7 +5,7 @@ Text {
color: "#D7D8DB" color: "#D7D8DB"
font.pixelSize: 36 font.pixelSize: 36
font.weight: 700 font.weight: Font.Bold
font.family: "PT Root UI VF" font.family: "PT Root UI VF"
font.letterSpacing: -0.03 font.letterSpacing: -0.03

View file

@ -5,7 +5,7 @@ Text {
color: "#D7D8DB" color: "#D7D8DB"
font.pixelSize: 25 font.pixelSize: 25
font.weight: 700 font.weight: Font.Bold
font.family: "PT Root UI VF" font.family: "PT Root UI VF"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap

View file

@ -5,7 +5,7 @@ Text {
color: "#878B91" color: "#878B91"
font.pixelSize: 13 font.pixelSize: 13
font.weight: 400 font.weight: Font.Normal
font.family: "PT Root UI VF" font.family: "PT Root UI VF"
font.letterSpacing: 0.02 font.letterSpacing: 0.02

View file

@ -5,7 +5,7 @@ Text {
color: "#D7D8DB" color: "#D7D8DB"
font.pixelSize: 16 font.pixelSize: 16
font.weight: 400 font.weight: Font.Normal
font.family: "PT Root UI VF" font.family: "PT Root UI VF"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap

View file

@ -12,6 +12,7 @@ import "../Pages"
import "../Controls2" import "../Controls2"
import "../Controls2/TextTypes" import "../Controls2/TextTypes"
import "../Config" import "../Config"
import "../Components"
PageBase { PageBase {
id: root id: root
@ -24,6 +25,12 @@ PageBase {
property string currentServerName: serversMenuContent.currentItem.delegateData.desc property string currentServerName: serversMenuContent.currentItem.delegateData.desc
property string currentServerDescription: serversMenuContent.currentItem.delegateData.address property string currentServerDescription: serversMenuContent.currentItem.delegateData.address
ConnectButton {
anchors.centerIn: parent
text: "Подключиться"
}
Rectangle { Rectangle {
id: buttonBackground id: buttonBackground
anchors.fill: buttonContent anchors.fill: buttonContent
@ -136,11 +143,11 @@ PageBase {
sourceModel: ContainersModel sourceModel: ContainersModel
filters: [ filters: [
ValueFilter { ValueFilter {
roleName: "service_type_role" roleName: "serviceType"
value: ProtocolEnum.Vpn value: ProtocolEnum.Vpn
}, },
ValueFilter { ValueFilter {
roleName: "is_installed_role" roleName: "isInstalled"
value: true value: true
} }
] ]
@ -153,7 +160,7 @@ PageBase {
borderWidth: 0 borderWidth: 0
buttonImageColor: "#0E0E11" buttonImageColor: "#0E0E11"
buttonMaximumWidth: 150 buttonMaximumWidth: 150 //todo make it dynamic
defaultColor: "#D7D8DB" defaultColor: "#D7D8DB"
@ -183,7 +190,7 @@ PageBase {
checked: { checked: {
if (modelData !== null) { if (modelData !== null) {
return modelData.default_role return modelData.isDefault
} }
return false return false
} }
@ -208,7 +215,7 @@ PageBase {
// todo remove dirty hack? // todo remove dirty hack?
text: { text: {
if (modelData !== null) { if (modelData !== null) {
return modelData.name_role return modelData.name
} else } else
return "" return ""
} }
@ -235,7 +242,7 @@ PageBase {
} }
onClicked: { onClicked: {
modelData.default_role = true modelData.isDefault = true
containersDropDown.text = containerRadioButtonText.text containersDropDown.text = containerRadioButtonText.text
containersDropDown.menuVisible = false containersDropDown.menuVisible = false
@ -243,14 +250,16 @@ PageBase {
} }
Component.onCompleted: { Component.onCompleted: {
if (modelData !== null && modelData.default_role) { if (modelData !== null && modelData.isDefault) {
containersDropDown.text = modelData.name_role containersDropDown.text = modelData.name
} }
} }
} }
} }
BasicButtonType { BasicButtonType {
id: dnsButton
implicitHeight: 40 implicitHeight: 40
text: "Amnezia DNS" text: "Amnezia DNS"