moved handling of connection states from qml in connectionController

- added a check for already installed containers before installing the server/container
- added a button to scan the server for installed containers
- added separation for read/write and readonly servers for pageHome
This commit is contained in:
vladimir.kuznetsov 2023-06-21 20:56:00 +09:00
parent 3a264e6baf
commit 249be451f7
21 changed files with 466 additions and 245 deletions

View file

@ -6,39 +6,27 @@
ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
const QSharedPointer<VpnConnection> &vpnConnection,
QObject *parent)
: QObject(parent)
, m_serversModel(serversModel)
, m_containersModel(containersModel)
, m_vpnConnection(vpnConnection)
const QSharedPointer<VpnConnection> &vpnConnection, QObject *parent)
: QObject(parent), m_serversModel(serversModel), m_containersModel(containersModel), m_vpnConnection(vpnConnection)
{
connect(m_vpnConnection.get(),
&VpnConnection::connectionStateChanged,
this,
&ConnectionController::connectionStateChanged);
connect(this,
&ConnectionController::connectToVpn,
m_vpnConnection.get(),
&VpnConnection::connectToVpn,
connect(m_vpnConnection.get(), &VpnConnection::connectionStateChanged, this,
&ConnectionController::onConnectionStateChanged);
connect(this, &ConnectionController::connectToVpn, m_vpnConnection.get(), &VpnConnection::connectToVpn,
Qt::QueuedConnection);
connect(this,
&ConnectionController::disconnectFromVpn,
m_vpnConnection.get(),
&VpnConnection::disconnectFromVpn,
connect(this, &ConnectionController::disconnectFromVpn, m_vpnConnection.get(), &VpnConnection::disconnectFromVpn,
Qt::QueuedConnection);
}
void ConnectionController::openConnection()
{
int serverIndex = m_serversModel->getDefaultServerIndex();
ServerCredentials credentials = qvariant_cast<ServerCredentials>(
m_serversModel->data(serverIndex, ServersModel::ServersModelRoles::CredentialsRole));
ServerCredentials credentials =
qvariant_cast<ServerCredentials>(m_serversModel->data(serverIndex, ServersModel::Roles::CredentialsRole));
DockerContainer container = m_containersModel->getDefaultContainer();
QModelIndex containerModelIndex = m_containersModel->index(container);
const QJsonObject &containerConfig = qvariant_cast<QJsonObject>(m_containersModel->data(containerModelIndex,
ContainersModel::Roles::ConfigRole));
const QJsonObject &containerConfig =
qvariant_cast<QJsonObject>(m_containersModel->data(containerModelIndex, ContainersModel::Roles::ConfigRole));
if (container == DockerContainer::None) {
emit connectionErrorOccurred(tr("VPN Protocols is not installed.\n Please install VPN container at first"));
@ -59,13 +47,65 @@ QString ConnectionController::getLastConnectionError()
return errorString(m_vpnConnection->lastError());
}
bool ConnectionController::isConnected()
void ConnectionController::onConnectionStateChanged(Vpn::ConnectionState state)
{
m_isConnected = false;
m_connectionStateText = tr("Connection...");
switch (state) {
case Vpn::ConnectionState::Connected: {
m_isConnectionInProgress = false;
m_isConnected = true;
m_connectionStateText = tr("Disconnect");
break;
}
case Vpn::ConnectionState::Connecting: {
m_isConnectionInProgress = true;
break;
}
case Vpn::ConnectionState::Reconnecting: {
m_isConnectionInProgress = true;
m_connectionStateText = tr("Reconnection...");
break;
}
case Vpn::ConnectionState::Disconnected: {
m_isConnectionInProgress = false;
m_connectionStateText = tr("Connect");
break;
}
case Vpn::ConnectionState::Disconnecting: {
m_isConnectionInProgress = true;
m_connectionStateText = tr("Disconnection...");
break;
}
case Vpn::ConnectionState::Preparing: {
m_isConnectionInProgress = true;
break;
}
case Vpn::ConnectionState::Error: {
m_isConnectionInProgress = false;
emit connectionErrorOccurred(getLastConnectionError());
break;
}
case Vpn::ConnectionState::Unknown: {
m_isConnectionInProgress = false;
emit connectionErrorOccurred(getLastConnectionError());
break;
}
}
emit connectionStateChanged();
}
QString ConnectionController::connectionStateText() const
{
return m_connectionStateText;
}
bool ConnectionController::isConnectionInProgress() const
{
return m_isConnectionInProgress;
}
bool ConnectionController::isConnected() const
{
return m_isConnected;
}
void ConnectionController::setIsConnected(bool isConnected)
{
m_isConnected = isConnected;
emit isConnectedChanged();
}

View file

@ -11,28 +11,30 @@ class ConnectionController : public QObject
Q_OBJECT
public:
Q_PROPERTY(bool isConnected READ isConnected WRITE setIsConnected NOTIFY isConnectedChanged)
Q_PROPERTY(bool isConnected READ isConnected NOTIFY connectionStateChanged)
Q_PROPERTY(bool isConnectionInProgress READ isConnectionInProgress NOTIFY connectionStateChanged)
Q_PROPERTY(QString connectionStateText READ connectionStateText NOTIFY connectionStateChanged)
explicit ConnectionController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
const QSharedPointer<VpnConnection> &vpnConnection,
QObject *parent = nullptr);
bool isConnected();
void setIsConnected(bool isConnected); //todo take state from vpnconnection?
bool isConnected() const;
bool isConnectionInProgress() const;
QString connectionStateText() const;
public slots:
void openConnection();
void closeConnection();
QString getLastConnectionError();
Vpn::ConnectionState connectionState(){return {};}; //todo update ConnectButton text on page change
void onConnectionStateChanged(Vpn::ConnectionState state);
signals:
void connectToVpn(int serverIndex, const ServerCredentials &credentials, DockerContainer container, const QJsonObject &containerConfig);
void disconnectFromVpn();
void connectionStateChanged(Vpn::ConnectionState state);
void isConnectedChanged();
void connectionStateChanged();
void connectionErrorOccurred(QString errorMessage);
@ -43,6 +45,8 @@ private:
QSharedPointer<VpnConnection> m_vpnConnection;
bool m_isConnected = false;
bool m_isConnectionInProgress = false;
QString m_connectionStateText = tr("Connect");
};
#endif // CONNECTIONCONTROLLER_H

View file

@ -44,7 +44,7 @@ void ExportController::generateConnectionConfig()
{
int serverIndex = m_serversModel->getCurrentlyProcessedServerIndex();
ServerCredentials credentials = qvariant_cast<ServerCredentials>(
m_serversModel->data(serverIndex, ServersModel::ServersModelRoles::CredentialsRole));
m_serversModel->data(serverIndex, ServersModel::Roles::CredentialsRole));
DockerContainer container = static_cast<DockerContainer>(
m_containersModel->getCurrentlyProcessedContainerIndex());

View file

@ -89,7 +89,7 @@ void ImportController::importConfig()
if (!m_config.value(config_key::containers).toArray().isEmpty()) {
auto newServerIndex = m_serversModel->index(m_serversModel->getServersCount() - 1);
m_serversModel->setData(newServerIndex, true, ServersModel::ServersModelRoles::IsDefaultRole);
m_serversModel->setData(newServerIndex, true, ServersModel::Roles::IsDefaultRole);
}
emit importFinished();

View file

@ -2,40 +2,54 @@
#include <QJsonObject>
#include "core/servercontroller.h"
#include "core/errorstrings.h"
#include "core/servercontroller.h"
#include "utilities.h"
InstallController::InstallController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
const std::shared_ptr<Settings> &settings,
QObject *parent) : QObject(parent), m_serversModel(serversModel), m_containersModel(containersModel), m_settings(settings)
{}
const QSharedPointer<ContainersModel> &containersModel,
const std::shared_ptr<Settings> &settings, QObject *parent)
: QObject(parent), m_serversModel(serversModel), m_containersModel(containersModel), m_settings(settings)
{
}
void InstallController::install(DockerContainer container, int port, TransportProto transportProto)
{
Proto mainProto = ContainerProps::defaultProtocol(container);
QJsonObject containerConfig {
{ config_key::port, QString::number(port) },
{ config_key::transport_proto, ProtocolProps::transportProtoToString(transportProto, mainProto) }
};
QJsonObject config {
{ config_key::container, ContainerProps::containerToString(container) },
{ ProtocolProps::protoToString(mainProto), containerConfig }
};
QJsonObject containerConfig { { config_key::port, QString::number(port) },
{ config_key::transport_proto,
ProtocolProps::transportProtoToString(transportProto, mainProto) } };
QJsonObject config { { config_key::container, ContainerProps::containerToString(container) },
{ ProtocolProps::protoToString(mainProto), containerConfig } };
if (m_shouldCreateServer) {
if (isServerAlreadyExists()) {
return;
}
installServer(container, config);
} else {
installContainer(container, config);
}
}
void InstallController::installServer(DockerContainer container, QJsonObject& config)
void InstallController::installServer(DockerContainer container, QJsonObject &config)
{
//todo check if container already installed
ServerController serverController(m_settings);
ErrorCode errorCode = serverController.setupContainer(m_currentlyInstalledServerCredentials, container, config);
QMap<DockerContainer, QJsonObject> installedContainers;
ErrorCode errorCode =
serverController.getAlreadyInstalledContainers(m_currentlyInstalledServerCredentials, installedContainers);
if (!installedContainers.contains(container)) {
errorCode = serverController.setupContainer(m_currentlyInstalledServerCredentials, container, config);
installedContainers.insert(container, config);
}
bool isInstalledContainerFound = false;
if (!installedContainers.isEmpty()) {
isInstalledContainerFound = true;
}
if (errorCode == ErrorCode::NoError) {
QJsonObject server;
server.insert(config_key::hostName, m_currentlyInstalledServerCredentials.hostName);
@ -44,43 +58,123 @@ void InstallController::installServer(DockerContainer container, QJsonObject& co
server.insert(config_key::port, m_currentlyInstalledServerCredentials.port);
server.insert(config_key::description, m_settings->nextAvailableServerName());
server.insert(config_key::containers, QJsonArray{ config });
QJsonArray containerConfigs;
for (const QJsonObject &containerConfig : qAsConst(installedContainers)) {
containerConfigs.append(containerConfig);
}
server.insert(config_key::containers, containerConfigs);
server.insert(config_key::defaultContainer, ContainerProps::containerToString(container));
m_serversModel->addServer(server);
auto newServerIndex = m_serversModel->index(m_serversModel->getServersCount() - 1);
m_serversModel->setData(newServerIndex, true, ServersModel::ServersModelRoles::IsDefaultRole);
m_serversModel->setData(newServerIndex, true, ServersModel::Roles::IsDefaultRole);
emit installServerFinished();
emit installServerFinished(isInstalledContainerFound);
return;
}
emit installationErrorOccurred(errorString(errorCode));
}
void InstallController::installContainer(DockerContainer container, QJsonObject& config)
void InstallController::installContainer(DockerContainer container, QJsonObject &config)
{
//todo check if container already installed
int serverIndex = m_serversModel->getCurrentlyProcessedServerIndex();
ServerCredentials serverCredentials = qvariant_cast<ServerCredentials>(
m_serversModel->data(serverIndex, ServersModel::ServersModelRoles::CredentialsRole));
ServerCredentials serverCredentials =
qvariant_cast<ServerCredentials>(m_serversModel->data(serverIndex, ServersModel::Roles::CredentialsRole));
ServerController serverController(m_settings);
ErrorCode errorCode = serverController.setupContainer(serverCredentials, container, config);
QMap<DockerContainer, QJsonObject> installedContainers;
ErrorCode errorCode = serverController.getAlreadyInstalledContainers(serverCredentials, installedContainers);
bool isInstalledContainerFound = false;
if (!installedContainers.isEmpty()) {
isInstalledContainerFound = true;
}
if (!installedContainers.contains(container)) {
errorCode = serverController.setupContainer(serverCredentials, container, config);
installedContainers.insert(container, config);
}
if (errorCode == ErrorCode::NoError) {
m_containersModel->setData(m_containersModel->index(container), config, ContainersModel::Roles::ConfigRole);
emit installContainerFinished();
for (auto iterator = installedContainers.begin(); iterator != installedContainers.end(); iterator++) {
auto modelIndex = m_containersModel->index(iterator.key());
QJsonObject containerConfig =
qvariant_cast<QJsonObject>(m_containersModel->data(modelIndex, ContainersModel::Roles::ConfigRole));
if (containerConfig.isEmpty()) {
m_containersModel->setData(m_containersModel->index(iterator.key()), iterator.value(),
ContainersModel::Roles::ConfigRole);
}
}
emit installContainerFinished(isInstalledContainerFound);
return;
}
emit installationErrorOccurred(errorString(errorCode));
}
void InstallController::setCurrentlyInstalledServerCredentials(const QString &hostName, const QString &userName, const QString &secretData)
bool InstallController::isServerAlreadyExists()
{
for (int i = 0; i < m_serversModel->getServersCount(); i++) {
auto modelIndex = m_serversModel->index(i);
const ServerCredentials credentials =
qvariant_cast<ServerCredentials>(m_serversModel->data(modelIndex, ServersModel::Roles::CredentialsRole));
if (m_currentlyInstalledServerCredentials.hostName == credentials.hostName
&& m_currentlyInstalledServerCredentials.port == credentials.port) {
emit serverAlreadyExists(i);
return true;
}
}
return false;
}
void InstallController::scanServerForInstalledContainers()
{
int serverIndex = m_serversModel->getCurrentlyProcessedServerIndex();
ServerCredentials serverCredentials =
qvariant_cast<ServerCredentials>(m_serversModel->data(serverIndex, ServersModel::Roles::CredentialsRole));
ServerController serverController(m_settings);
QMap<DockerContainer, QJsonObject> installedContainers;
ErrorCode errorCode = serverController.getAlreadyInstalledContainers(serverCredentials, installedContainers);
if (errorCode == ErrorCode::NoError) {
bool isInstalledContainerAddedToGui = false;
for (auto iterator = installedContainers.begin(); iterator != installedContainers.end(); iterator++) {
auto modelIndex = m_containersModel->index(iterator.key());
QJsonObject containerConfig =
qvariant_cast<QJsonObject>(m_containersModel->data(modelIndex, ContainersModel::Roles::ConfigRole));
if (containerConfig.isEmpty()) {
m_containersModel->setData(m_containersModel->index(iterator.key()), iterator.value(),
ContainersModel::Roles::ConfigRole);
isInstalledContainerAddedToGui = true;
}
}
emit scanServerFinished(isInstalledContainerAddedToGui);
return;
}
emit installationErrorOccurred(errorString(errorCode));
}
QRegularExpression InstallController::ipAddressPortRegExp()
{
return Utils::ipAddressPortRegExp();
}
void InstallController::setCurrentlyInstalledServerCredentials(const QString &hostName, const QString &userName,
const QString &secretData)
{
m_currentlyInstalledServerCredentials.hostName = hostName;
if (m_currentlyInstalledServerCredentials.hostName.contains(":")) {
m_currentlyInstalledServerCredentials.port = m_currentlyInstalledServerCredentials.hostName.split(":").at(1).toInt();
m_currentlyInstalledServerCredentials.port =
m_currentlyInstalledServerCredentials.hostName.split(":").at(1).toInt();
m_currentlyInstalledServerCredentials.hostName = m_currentlyInstalledServerCredentials.hostName.split(":").at(0);
}
m_currentlyInstalledServerCredentials.userName = userName;

View file

@ -3,10 +3,10 @@
#include <QObject>
#include "core/defs.h"
#include "containers/containers_defs.h"
#include "ui/models/servers_model.h"
#include "core/defs.h"
#include "ui/models/containers_model.h"
#include "ui/models/servers_model.h"
class InstallController : public QObject
{
@ -14,22 +14,32 @@ class InstallController : public QObject
public:
explicit InstallController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
const std::shared_ptr<Settings> &settings,
QObject *parent = nullptr);
const std::shared_ptr<Settings> &settings, QObject *parent = nullptr);
public slots:
void install(DockerContainer container, int port, TransportProto transportProto);
void setCurrentlyInstalledServerCredentials(const QString &hostName, const QString &userName, const QString &secretData);
void setCurrentlyInstalledServerCredentials(const QString &hostName, const QString &userName,
const QString &secretData);
void setShouldCreateServer(bool shouldCreateServer);
void scanServerForInstalledContainers();
QRegularExpression ipAddressPortRegExp();
signals:
void installContainerFinished();
void installServerFinished();
void installContainerFinished(bool isInstalledContainerFound);
void installServerFinished(bool isInstalledContainerFound);
void scanServerFinished(bool isInstalledContainerFound);
void installationErrorOccurred(QString errorMessage);
void serverAlreadyExists(int serverIndex);
private:
void installServer(DockerContainer container, QJsonObject& config);
void installContainer(DockerContainer container, QJsonObject& config);
void installServer(DockerContainer container, QJsonObject &config);
void installContainer(DockerContainer container, QJsonObject &config);
bool isServerAlreadyExists();
QSharedPointer<ServersModel> m_serversModel;
QSharedPointer<ContainersModel> m_containersModel;

View file

@ -10,7 +10,7 @@ QString PageController::getInitialPage()
if (m_serversModel->getServersCount()) {
if (m_serversModel->getDefaultServerIndex() < 0) {
auto defaultServerIndex = m_serversModel->index(0);
m_serversModel->setData(defaultServerIndex, true, ServersModel::ServersModelRoles::IsDefaultRole);
m_serversModel->setData(defaultServerIndex, true, ServersModel::Roles::IsDefaultRole);
}
return getPagePath(PageLoader::PageEnum::PageStart);
} else {

View file

@ -40,14 +40,9 @@ namespace PageLoader
};
Q_ENUM_NS(PageEnum)
static void declareQmlPageEnum() {
qmlRegisterUncreatableMetaObject(
PageLoader::staticMetaObject,
"PageEnum",
1, 0,
"PageEnum",
"Error: only enums"
);
static void declareQmlPageEnum()
{
qmlRegisterUncreatableMetaObject(PageLoader::staticMetaObject, "PageEnum", 1, 0, "PageEnum", "Error: only enums");
}
}
@ -55,8 +50,7 @@ class PageController : public QObject
{
Q_OBJECT
public:
explicit PageController(const QSharedPointer<ServersModel> &serversModel,
QObject *parent = nullptr);
explicit PageController(const QSharedPointer<ServersModel> &serversModel, QObject *parent = nullptr);
public slots:
QString getInitialPage();
@ -64,9 +58,11 @@ public slots:
signals:
void goToPageHome();
void goToPageSettings();
void restorePageHomeState(bool isContainerInstalled = false);
void replaceStartPage();
void showErrorMessage(QString errorMessage);
void showInfoMessage(QString message);
private:
QSharedPointer<ServersModel> m_serversModel;