Skip to content

Commit

Permalink
added connectionController and ConnectionButton.qml
Browse files Browse the repository at this point in the history
  • Loading branch information
Nethius committed May 12, 2023
1 parent dd0de7e commit 35d4222
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 60 deletions.
5 changes: 5 additions & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_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}
${COMMON_FILES_H}
${PAGE_LOGIC_H}
${CONFIGURATORS_H}
${UI_MODELS_H}
${UI_CONTROLLERS_H}
)
set(SOURCES ${SOURCES}
${COMMON_FILES_CPP}
${PAGE_LOGIC_CPP}
${CONFIGURATORS_CPP}
${UI_MODELS_CPP}
${UI_CONTROLLERS_CPP}
)

qt6_add_resources(QRC ${QRC} ${CMAKE_CURRENT_LIST_DIR}/resources.qrc)
Expand Down
12 changes: 11 additions & 1 deletion client/amnezia_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QTimer>
#include <QTranslator>


#include "core/servercontroller.h"
#include "logger.h"
#include "defines.h"
Expand Down Expand Up @@ -100,12 +99,23 @@ void AmneziaApplication::init()

m_engine->rootContext()->setContextProperty("Debug", &Logger::Instance());

//
m_containersModel.reset(new ContainersModel(m_settings, this));
m_engine->rootContext()->setContextProperty("ContainersModel", m_containersModel.get());

m_serversModel.reset(new ServersModel(m_settings, this));
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();

#if defined(Q_OS_IOS)
Expand Down
10 changes: 8 additions & 2 deletions client/amnezia_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include <QQmlContext>

#include "settings.h"
#include "vpnconnection.h"

#include "ui/uilogic.h"
#include "configurators/vpn_configurator.h"
#include "ui/models/servers_model.h"
#include "ui/models/containers_model.h"
#include "ui/controllers/connectionController.h"

#define amnApp (static_cast<AmneziaApplication *>(QCoreApplication::instance()))

Expand Down Expand Up @@ -58,8 +60,12 @@ class AmneziaApplication : public AMNEZIA_BASE_CLASS
QTranslator* m_translator;
QCommandLineParser m_parser;

QScopedPointer<ContainersModel> m_containersModel;
QScopedPointer<ServersModel> m_serversModel;
QSharedPointer<ContainersModel> m_containersModel;
QSharedPointer<ServersModel> m_serversModel;

QScopedPointer<VpnConnection> m_vpnConnection;

QScopedPointer<ConnectionController> m_connectionController;

};

Expand Down
1 change: 1 addition & 0 deletions client/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,6 @@
<file>ui/qml/Controls2/TextTypes/ButtonTextType.qml</file>
<file>ui/qml/Controls2/Header2Type.qml</file>
<file>images/controls/plus.svg</file>
<file>ui/qml/Components/ConnectButton.qml</file>
</qresource>
</RCC>
71 changes: 71 additions & 0 deletions client/ui/controllers/connectionController.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

33 changes: 33 additions & 0 deletions client/ui/controllers/connectionController.h
Original file line number Diff line number Diff line change
@@ -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
48 changes: 27 additions & 21 deletions client/ui/models/containers_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bool ContainersModel::setData(const QModelIndex &index, const QVariant &value, i
return false;
}

if (role == DefaultRole) {
if (role == IsDefaultRole) {
DockerContainer container = ContainerProps::allContainers().at(index.row());
m_settings->setDefaultContainer(m_selectedServerIndex, container);
}
Expand All @@ -33,22 +33,23 @@ QVariant ContainersModel::data(const QModelIndex &index, int role) const
return QVariant();
}

DockerContainer c = ContainerProps::allContainers().at(index.row());
if (role == NameRole) {
return ContainerProps::containerHumanNames().value(c);
}
if (role == DescRole) {
return ContainerProps::containerDescriptions().value(c);
}
if (role == DefaultRole) {
return c == m_settings->defaultContainer(m_selectedServerIndex);
}
if (role == ServiceTypeRole) {
return ContainerProps::containerService(c);
}
if (role == IsInstalledRole) {
return m_settings->containers(m_selectedServerIndex).contains(c);
DockerContainer container = ContainerProps::allContainers().at(index.row());

switch (role) {
case NameRole:
return ContainerProps::containerHumanNames().value(container);
case DescRole:
return ContainerProps::containerDescriptions().value(container);
case ConfigRole:
return m_settings->containerConfig(m_selectedServerIndex, container);
case ServiceTypeRole:
return ContainerProps::containerService(container);
case IsInstalledRole:
return m_settings->containers(m_selectedServerIndex).contains(container);
case IsDefaultRole:
return container == m_settings->defaultContainer(m_selectedServerIndex);
}

return QVariant();
}

Expand All @@ -71,12 +72,17 @@ QString ContainersModel::getCurrentlyInstalledContainerName()
return data(m_currentlyInstalledContainerIndex, NameRole).toString();
}

DockerContainer ContainersModel::getDefaultContainer()
{
return m_settings->defaultContainer(m_selectedServerIndex);
}

QHash<int, QByteArray> ContainersModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name_role";
roles[DescRole] = "desc_role";
roles[DefaultRole] = "default_role";
roles[ServiceTypeRole] = "service_type_role";
roles[IsInstalledRole] = "is_installed_role";
roles[NameRole] = "name";
roles[DescRole] = "description";
roles[ServiceTypeRole] = "serviceType";
roles[IsInstalledRole] = "isInstalled";
roles[IsDefaultRole] = "isDefault";
return roles;
}
10 changes: 7 additions & 3 deletions client/ui/models/containers_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class ContainersModel : public QAbstractListModel
public:
ContainersModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
public:
enum SiteRoles {
enum ContainersModelRoles {
NameRole = Qt::UserRole + 1,
DescRole,
DefaultRole,
ServiceTypeRole,
IsInstalledRole
ConfigRole,
IsInstalledRole,
IsDefaultRole
};

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Expand All @@ -32,6 +33,9 @@ class ContainersModel : public QAbstractListModel

Q_INVOKABLE QString getCurrentlyInstalledContainerName();

public slots:
DockerContainer getDefaultContainer();

protected:
QHash<int, QByteArray> roleNames() const override;

Expand Down
27 changes: 7 additions & 20 deletions client/ui/models/servers_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,23 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
const QJsonArray &servers = m_settings->serversArray();
const QJsonObject server = servers.at(index.row()).toObject();

if (role == DescRole) {
switch (role) {
case DescRole: {
auto description = server.value(config_key::description).toString();
if (description.isEmpty()) {
return server.value(config_key::hostName).toString();
}
return description;
}
if (role == AddressRole) {
case AddressRole:
return server.value(config_key::hostName).toString();
}
if (role == IsDefaultRole) {
case CredentialsRole:
return QVariant::fromValue(m_settings->serverCredentials(index.row()));
case IsDefaultRole:
return index.row() == m_settings->defaultServerIndex();
}
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();
return QVariant();
}

void ServersModel::setDefaultServerIndex(int index)
Expand Down
1 change: 1 addition & 0 deletions client/ui/models/servers_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ServersModel : public QAbstractListModel
enum ServersModelRoles {
DescRole = Qt::UserRole + 1,
AddressRole,
CredentialsRole,
IsDefaultRole
};

Expand Down
40 changes: 40 additions & 0 deletions client/ui/qml/Components/ConnectButton.qml
Original file line number Diff line number Diff line change
@@ -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()
}
}
2 changes: 1 addition & 1 deletion client/ui/qml/Controls2/TextTypes/ButtonTextType.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Text {

color: "#D7D8DB"
font.pixelSize: 16
font.weight: 500
font.weight: Font.Medium
font.family: "PT Root UI VF"

wrapMode: Text.WordWrap
Expand Down
Loading

0 comments on commit 35d4222

Please sign in to comment.