Reworked the interaction between models. Now only serversModel directly interacts with server config
This commit is contained in:
parent
f3f98a50ed
commit
b4c89ad58f
19 changed files with 412 additions and 290 deletions
|
|
@ -1,5 +1,7 @@
|
|||
#include "servers_model.h"
|
||||
|
||||
#include "core/controllers/serverController.h"
|
||||
|
||||
ServersModel::ServersModel(std::shared_ptr<Settings> settings, QObject *parent)
|
||||
: m_settings(settings), QAbstractListModel(parent)
|
||||
{
|
||||
|
|
@ -8,6 +10,11 @@ ServersModel::ServersModel(std::shared_ptr<Settings> settings, QObject *parent)
|
|||
m_currentlyProcessedServerIndex = m_defaultServerIndex;
|
||||
|
||||
connect(this, &ServersModel::defaultServerIndexChanged, this, &ServersModel::defaultServerNameChanged);
|
||||
connect(this, &ServersModel::defaultServerIndexChanged, this, &ServersModel::defaultServerDescriptionChanged);
|
||||
connect(this, &ServersModel::defaultServerIndexChanged, this, [this](const int serverIndex) {
|
||||
auto defaultContainer = ContainerProps::containerFromString(m_servers.at(serverIndex).toObject().value(config_key::defaultContainer).toString());
|
||||
emit ServersModel::defaultContainerChanged(defaultContainer);
|
||||
});
|
||||
}
|
||||
|
||||
int ServersModel::rowCount(const QModelIndex &parent) const
|
||||
|
|
@ -50,14 +57,23 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
|
|||
}
|
||||
|
||||
const QJsonObject server = m_servers.at(index.row()).toObject();
|
||||
|
||||
const auto configVersion = server.value(config_key::configVersion).toInt();
|
||||
switch (role) {
|
||||
case NameRole: {
|
||||
auto description = server.value(config_key::description).toString();
|
||||
if (description.isEmpty()) {
|
||||
if (configVersion) {
|
||||
return server.value(config_key::name).toString();
|
||||
}
|
||||
auto name = server.value(config_key::description).toString();
|
||||
if (name.isEmpty()) {
|
||||
return server.value(config_key::hostName).toString();
|
||||
}
|
||||
return description;
|
||||
return name;
|
||||
}
|
||||
case ServerDescriptionRole: {
|
||||
if (configVersion) {
|
||||
return server.value(config_key::description).toString();
|
||||
}
|
||||
return server.value(config_key::hostName).toString();
|
||||
}
|
||||
case HostNameRole: return server.value(config_key::hostName).toString();
|
||||
case CredentialsRole: return QVariant::fromValue(serverCredentials(index.row()));
|
||||
|
|
@ -72,6 +88,9 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
|
|||
QString primaryDns = server.value(config_key::dns1).toString();
|
||||
return primaryDns == protocols::dns::amneziaDnsIp;
|
||||
}
|
||||
case DefaultContainerRole: {
|
||||
return ContainerProps::containerFromString(server.value(config_key::defaultContainer).toString());
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
|
|
@ -114,6 +133,53 @@ const QString ServersModel::getDefaultServerHostName()
|
|||
return qvariant_cast<QString>(data(m_defaultServerIndex, HostNameRole));
|
||||
}
|
||||
|
||||
QString ServersModel::getDefaultServerDescription(const QJsonObject &server)
|
||||
{
|
||||
const auto configVersion = server.value(config_key::configVersion).toInt();
|
||||
|
||||
QString description;
|
||||
|
||||
if (configVersion) {
|
||||
return server.value(config_key::description).toString();
|
||||
} else if (isDefaultServerHasWriteAccess()) {
|
||||
if (m_isAmneziaDnsEnabled
|
||||
&& isAmneziaDnsContainerInstalled(m_defaultServerIndex)) {
|
||||
description += "Amnezia DNS | ";
|
||||
}
|
||||
} else {
|
||||
if (isDefaultServerConfigContainsAmneziaDns()) {
|
||||
description += "Amnezia DNS | ";
|
||||
}
|
||||
}
|
||||
return description;
|
||||
}
|
||||
|
||||
const QString ServersModel::getDefaultServerDescriptionCollapsed()
|
||||
{
|
||||
const QJsonObject server = m_servers.at(m_defaultServerIndex).toObject();
|
||||
const auto configVersion = server.value(config_key::configVersion).toInt();
|
||||
auto description = getDefaultServerDescription(server);
|
||||
if (configVersion) {
|
||||
return description;
|
||||
}
|
||||
|
||||
auto container = ContainerProps::containerFromString(server.value(config_key::defaultContainer).toString());
|
||||
|
||||
return description += ContainerProps::containerHumanNames().value(container) + " | " + server.value(config_key::hostName).toString();
|
||||
}
|
||||
|
||||
const QString ServersModel::getDefaultServerDescriptionExpanded()
|
||||
{
|
||||
const QJsonObject server = m_servers.at(m_defaultServerIndex).toObject();
|
||||
const auto configVersion = server.value(config_key::configVersion).toInt();
|
||||
auto description = getDefaultServerDescription(server);
|
||||
if (configVersion) {
|
||||
return description;
|
||||
}
|
||||
|
||||
return description += server.value(config_key::hostName).toString();
|
||||
}
|
||||
|
||||
const int ServersModel::getServersCount()
|
||||
{
|
||||
return m_servers.count();
|
||||
|
|
@ -132,6 +198,7 @@ bool ServersModel::hasServerWithWriteAccess()
|
|||
void ServersModel::setCurrentlyProcessedServerIndex(const int index)
|
||||
{
|
||||
m_currentlyProcessedServerIndex = index;
|
||||
updateContainersModel();
|
||||
emit currentlyProcessedServerIndexChanged(m_currentlyProcessedServerIndex);
|
||||
}
|
||||
|
||||
|
|
@ -150,6 +217,11 @@ const ServerCredentials ServersModel::getCurrentlyProcessedServerCredentials()
|
|||
return serverCredentials(m_currentlyProcessedServerIndex);
|
||||
}
|
||||
|
||||
const ServerCredentials ServersModel::getServerCredentials(const int index)
|
||||
{
|
||||
return serverCredentials(index);
|
||||
}
|
||||
|
||||
bool ServersModel::isDefaultServerCurrentlyProcessed()
|
||||
{
|
||||
return m_defaultServerIndex == m_currentlyProcessedServerIndex;
|
||||
|
|
@ -179,6 +251,7 @@ void ServersModel::editServer(const QJsonObject &server)
|
|||
m_settings->editServer(m_currentlyProcessedServerIndex, server);
|
||||
m_servers = m_settings->serversArray();
|
||||
endResetModel();
|
||||
updateContainersModel();
|
||||
}
|
||||
|
||||
void ServersModel::removeServer()
|
||||
|
|
@ -206,23 +279,27 @@ bool ServersModel::isDefaultServerConfigContainsAmneziaDns()
|
|||
return primaryDns == protocols::dns::amneziaDnsIp;
|
||||
}
|
||||
|
||||
void ServersModel::updateContainersConfig()
|
||||
{
|
||||
auto server = m_settings->server(m_currentlyProcessedServerIndex);
|
||||
m_servers.replace(m_currentlyProcessedServerIndex, server);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ServersModel::roleNames() const
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
|
||||
roles[NameRole] = "serverName";
|
||||
roles[NameRole] = "name";
|
||||
roles[ServerDescriptionRole] = "serverDescription";
|
||||
|
||||
roles[HostNameRole] = "hostName";
|
||||
|
||||
roles[CredentialsRole] = "credentials";
|
||||
roles[CredentialsLoginRole] = "credentialsLogin";
|
||||
|
||||
roles[IsDefaultRole] = "isDefault";
|
||||
roles[IsCurrentlyProcessedRole] = "isCurrentlyProcessed";
|
||||
|
||||
roles[HasWriteAccessRole] = "hasWriteAccess";
|
||||
|
||||
roles[ContainsAmneziaDnsRole] = "containsAmneziaDns";
|
||||
|
||||
roles[DefaultContainerRole] = "defaultContainer";
|
||||
return roles;
|
||||
}
|
||||
|
||||
|
|
@ -239,7 +316,205 @@ ServerCredentials ServersModel::serverCredentials(int index) const
|
|||
return credentials;
|
||||
}
|
||||
|
||||
void ServersModel::updateContainersModel()
|
||||
{
|
||||
auto containers = m_servers.at(m_currentlyProcessedServerIndex).toObject().value(config_key::containers).toArray();
|
||||
emit containersUpdated(containers);
|
||||
}
|
||||
|
||||
QJsonObject ServersModel::getDefaultServerConfig()
|
||||
{
|
||||
return m_servers.at(m_defaultServerIndex).toObject();
|
||||
}
|
||||
|
||||
void ServersModel::reloadContainerConfig()
|
||||
{
|
||||
QJsonObject server = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
auto container = ContainerProps::containerFromString(server.value(config_key::defaultContainer).toString());
|
||||
|
||||
auto containers = server.value(config_key::containers).toArray();
|
||||
|
||||
auto config = m_settings->containerConfig(m_currentlyProcessedServerIndex, container);
|
||||
for (auto i = 0; i < containers.size(); i++) {
|
||||
auto c = ContainerProps::containerFromString(containers.at(i).toObject().value(config_key::container).toString());
|
||||
if (c == container) {
|
||||
containers.replace(i, config);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
server.insert(config_key::containers, containers);
|
||||
editServer(server);
|
||||
}
|
||||
|
||||
void ServersModel::updateContainerConfig(const int containerIndex, const QJsonObject config)
|
||||
{
|
||||
auto container = static_cast<DockerContainer>(containerIndex);
|
||||
QJsonObject server = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
|
||||
auto containers = server.value(config_key::containers).toArray();
|
||||
for (auto i = 0; i < containers.size(); i++) {
|
||||
auto c = ContainerProps::containerFromString(containers.at(i).toObject().value(config_key::container).toString());
|
||||
if (c == container) {
|
||||
containers.replace(i, config);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
server.insert(config_key::containers, containers);
|
||||
|
||||
auto defaultContainer = server.value(config_key::defaultContainer).toString();
|
||||
if ((ContainerProps::containerFromString(defaultContainer) == DockerContainer::None || ContainerProps::containerService(container) != ServiceType::Other)) {
|
||||
server.insert(config_key::defaultContainer, ContainerProps::containerToString(container));
|
||||
}
|
||||
|
||||
editServer(server);
|
||||
}
|
||||
|
||||
void ServersModel::addContainerConfig(const int containerIndex, const QJsonObject config)
|
||||
{
|
||||
auto container = static_cast<DockerContainer>(containerIndex);
|
||||
QJsonObject server = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
|
||||
auto containers = server.value(config_key::containers).toArray();
|
||||
containers.push_back(config);
|
||||
|
||||
server.insert(config_key::containers, containers);
|
||||
|
||||
bool isDefaultContainerChanged = false;
|
||||
auto defaultContainer = server.value(config_key::defaultContainer).toString();
|
||||
if ((ContainerProps::containerFromString(defaultContainer) == DockerContainer::None || ContainerProps::containerService(container) != ServiceType::Other)) {
|
||||
server.insert(config_key::defaultContainer, ContainerProps::containerToString(container));
|
||||
isDefaultContainerChanged = true;
|
||||
}
|
||||
|
||||
editServer(server);
|
||||
if (isDefaultContainerChanged) {
|
||||
emit defaultContainerChanged(container);
|
||||
}
|
||||
}
|
||||
|
||||
void ServersModel::setDefaultContainer(const int containerIndex)
|
||||
{
|
||||
auto container = static_cast<DockerContainer>(containerIndex);
|
||||
QJsonObject s = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
s.insert(config_key::defaultContainer, ContainerProps::containerToString(container));
|
||||
editServer(s); //check
|
||||
emit defaultContainerChanged(container);
|
||||
}
|
||||
|
||||
DockerContainer ServersModel::getDefaultContainer()
|
||||
{
|
||||
return qvariant_cast<DockerContainer>(data(m_currentlyProcessedServerIndex, DefaultContainerRole));
|
||||
}
|
||||
|
||||
const QString ServersModel::getDefaultContainerName()
|
||||
{
|
||||
auto defaultContainer = getDefaultContainer();
|
||||
return ContainerProps::containerHumanNames().value(defaultContainer);
|
||||
}
|
||||
|
||||
ErrorCode ServersModel::removeAllContainers()
|
||||
{
|
||||
ServerController serverController(m_settings);
|
||||
ErrorCode errorCode =
|
||||
serverController.removeAllContainers(m_settings->serverCredentials(m_currentlyProcessedServerIndex));
|
||||
|
||||
if (errorCode == ErrorCode::NoError) {
|
||||
QJsonObject s = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
s.insert(config_key::containers, {});
|
||||
s.insert(config_key::defaultContainer, ContainerProps::containerToString(DockerContainer::None));
|
||||
|
||||
editServer(s);
|
||||
emit defaultContainerChanged(DockerContainer::None);
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
ErrorCode ServersModel::removeContainer(const int containerIndex)
|
||||
{
|
||||
ServerController serverController(m_settings);
|
||||
auto credentials = m_settings->serverCredentials(m_currentlyProcessedServerIndex);
|
||||
auto dockerContainer = static_cast<DockerContainer>(containerIndex);
|
||||
|
||||
ErrorCode errorCode = serverController.removeContainer(credentials, dockerContainer);
|
||||
|
||||
if (errorCode == ErrorCode::NoError) {
|
||||
QJsonObject server = m_servers.at(m_currentlyProcessedServerIndex).toObject();
|
||||
|
||||
auto containers = server.value(config_key::containers).toArray();
|
||||
for (auto it = containers.begin(); it != containers.end(); it++) {
|
||||
if (it->toObject().value(config_key::container).toString() == ContainerProps::containerToString(dockerContainer)) {
|
||||
containers.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
server.insert(config_key::containers, containers);
|
||||
|
||||
auto defaultContainer = ContainerProps::containerFromString(server.value(config_key::defaultContainer).toString());
|
||||
if (defaultContainer == containerIndex) {
|
||||
if (containers.empty()) {
|
||||
defaultContainer = DockerContainer::None;
|
||||
} else {
|
||||
defaultContainer = ContainerProps::containerFromString(containers.begin()->toObject().value(config_key::container).toString());
|
||||
}
|
||||
server.insert(config_key::defaultContainer, ContainerProps::containerToString(defaultContainer));
|
||||
}
|
||||
|
||||
editServer(server);
|
||||
emit defaultContainerChanged(defaultContainer);
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
void ServersModel::clearCachedProfiles()
|
||||
{
|
||||
const auto &containers = m_settings->containers(m_currentlyProcessedServerIndex);
|
||||
for (DockerContainer container : containers.keys()) {
|
||||
m_settings->clearLastConnectionConfig(m_currentlyProcessedServerIndex, container);
|
||||
}
|
||||
|
||||
m_servers.replace(m_currentlyProcessedServerIndex, m_settings->server(m_currentlyProcessedServerIndex));
|
||||
updateContainersModel();
|
||||
}
|
||||
|
||||
bool ServersModel::isAmneziaDnsContainerInstalled(const int serverIndex)
|
||||
{
|
||||
QJsonObject server = m_servers.at(serverIndex).toObject();
|
||||
auto containers = server.value(config_key::containers).toArray();
|
||||
for (auto it = containers.begin(); it != containers.end(); it++) {
|
||||
if (it->toObject().value(config_key::container).toString() == ContainerProps::containerToString(DockerContainer::Dns)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QStringList ServersModel::getAllInstalledServicesName(const int serverIndex)
|
||||
{
|
||||
QStringList servicesName;
|
||||
QJsonObject server = m_servers.at(serverIndex).toObject();
|
||||
const auto containers = server.value(config_key::containers).toArray();
|
||||
for (auto it = containers.begin(); it != containers.end(); it++) {
|
||||
auto container = ContainerProps::containerFromString(it->toObject().value(config_key::container).toString());
|
||||
if (ContainerProps::containerService(container) == ServiceType::Other) {
|
||||
if (container == DockerContainer::Dns) {
|
||||
servicesName.append("DNS");
|
||||
} else if (container == DockerContainer::Sftp) {
|
||||
servicesName.append("SFTP");
|
||||
} else if (container == DockerContainer::TorWebSite) {
|
||||
servicesName.append("TOR");
|
||||
}
|
||||
}
|
||||
}
|
||||
servicesName.sort();
|
||||
return servicesName;
|
||||
}
|
||||
|
||||
void ServersModel::toggleAmneziaDns(bool enabled)
|
||||
{
|
||||
m_isAmneziaDnsEnabled = enabled;
|
||||
emit defaultServerDescriptionChanged();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue