diff --git a/share/ring/bootstrap/servers.json b/share/ring/bootstrap/servers.json new file mode 100755 index 0000000000000000000000000000000000000000..4c5e3942ff034bb9126e1aad445f78b192503653 --- /dev/null +++ b/share/ring/bootstrap/servers.json @@ -0,0 +1,3 @@ +[ + {"host": "bootstrap.ring.cx", "port": 4222} +] diff --git a/src/account.cpp b/src/account.cpp index c1990fd05259039c4b7987dfe693746ca3f492b7..0e117f0beea7dfbe67b1a9455ff012014e5feaa8 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -181,7 +181,18 @@ Account* AccountPrivate::buildNewAccountFromAlias(Account::Protocol proto, const iter.next(); a->d_ptr->m_hAccountDetails[iter.key()] = iter.value(); } - a->setHostname(a->d_ptr->m_hAccountDetails[DRing::Account::ConfProperties::HOSTNAME]); + + if (proto == Account::Protocol::RING) + { + /* Set LRC-provided bootstrap servers */ + a->bootstrapModel() << BootstrapModel::EditAction::RESET; + a->bootstrapModel() << BootstrapModel::EditAction::SAVE; + } + else + { + a->setHostname(a->d_ptr->m_hAccountDetails[DRing::Account::ConfProperties::HOSTNAME]); + } + a->d_ptr->setAccountProperty(DRing::Account::ConfProperties::ALIAS,alias); a->d_ptr->m_RemoteEnabledState = a->isEnabled(); //a->setObjectName(a->id()); diff --git a/src/bootstrapmodel.cpp b/src/bootstrapmodel.cpp index c80186c97603fc661953fe2a24208ee236480f13..d4c07d2a7c0dfc7d7d3a165a4c009f8d525f67ab 100644 --- a/src/bootstrapmodel.cpp +++ b/src/bootstrapmodel.cpp @@ -17,6 +17,15 @@ ***************************************************************************/ #include "bootstrapmodel.h" +//Qt +#include <QtCore/QDir> +#include <QtCore/QFile> +#include <QtCore/QDirIterator> +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonArray> +#include <QtCore/QJsonObject> +#include <QtCore/QStandardPaths> + //Ring daemon #include <account_const.h> @@ -47,6 +56,7 @@ public: //Helper void clearLines(); inline void performAction(const BootstrapModel::EditAction action); + QVector<Lines*> loadDefaultBootstrapServers(); //Attributes Account* m_pAccount ; @@ -69,6 +79,7 @@ Matrix2D<BootstrapModel::EditState, BootstrapModel::EditAction, BootstrapModelPr /* MODIFIED */ {{ BMP::save , BMP::nothing, BMP::reload , BMP::clear, BMP::reset }}, /* OUTDATED */ {{ BMP::save , BMP::nothing, BMP::reload , BMP::clear, BMP::reset }}, /* RELOADING */ {{ BMP::nothing, BMP::nothing, BMP::nothing, BMP::nothing, BMP::nothing }}, + /* RESETING */ {{ BMP::nothing, BMP::modify, BMP::nothing, BMP::nothing, BMP::nothing }}, }}; #undef BMP @@ -155,6 +166,80 @@ void BootstrapModelPrivate::clearLines() } } +QVector<BootstrapModelPrivate::Lines*> BootstrapModelPrivate::loadDefaultBootstrapServers() +{ + auto servers = QVector<BootstrapModelPrivate::Lines*>(); + + /* get the bootstrap directory */ + QString bootstrapDirPath = QStandardPaths::locate( + QStandardPaths::DataLocation, + "bootstrap", + QStandardPaths::LocateDirectory + ); + QDir bootstrapDir = QDir(bootstrapDirPath); + + auto bootstrapFiles = QVector<QFileInfo>(); + + // Main bootstrap servers file + auto mainBootstrapFile = QFileInfo(bootstrapDir.path() + "/servers.json"); + if (mainBootstrapFile.exists() && mainBootstrapFile.isFile()) + { + bootstrapFiles << mainBootstrapFile; + } + + // Secondary bootstrap servers files + auto secondaryBootstrapServersDir = QFileInfo(bootstrapDir.path() + "/servers.json.d/"); + if (secondaryBootstrapServersDir.exists() && secondaryBootstrapServersDir.isDir()) + { + QDirIterator dirIter( + secondaryBootstrapServersDir.path(), + QDirIterator::Subdirectories + ); + while (dirIter.hasNext()) { + dirIter.next(); + auto secBootstrapFileInfo = QFileInfo(dirIter.filePath()); + if (secBootstrapFileInfo.isFile() && secBootstrapFileInfo.suffix() == "json") + { + bootstrapFiles << secBootstrapFileInfo; + } + } + } + + //Parse JSON files + foreach(const auto fileInfo, bootstrapFiles) + { + QFile bootstrapFile(fileInfo.filePath()); + if (bootstrapFile.open(QIODevice::ReadOnly | QIODevice::Text)) + { + auto jsonDoc = QJsonDocument::fromJson(bootstrapFile.readAll()); + bootstrapFile.close(); + if (jsonDoc.isNull() == false && jsonDoc.isArray() == true) + { + QJsonArray jsonArray = jsonDoc.array(); + foreach(const auto jsonValue, jsonArray) + { + auto hostObject = jsonValue.toObject(); + auto hostValue = hostObject.value("host"); + auto portValue = hostObject.value("port"); + + if (hostValue.isUndefined() == false && + hostValue.isString() && + portValue.isUndefined() == false && + portValue.isDouble()) + { + BootstrapModelPrivate::Lines* server = new BootstrapModelPrivate::Lines(); + server->hostname = hostValue.toString(); + server->port = portValue.toInt(-1); + servers << server; + } + } + } + } + } + + return servers; +} + BootstrapModel::BootstrapModel(Account* a) : QAbstractTableModel(a), d_ptr(new BootstrapModelPrivate(this,a)) { d_ptr->m_EditState = BootstrapModel::EditState::LOADING; @@ -311,15 +396,32 @@ bool BootstrapModel::isCustom() const void BootstrapModelPrivate::reset() { - clearLines(); + m_EditState = BootstrapModel::EditState::RESETING; - BootstrapModelPrivate::Lines* l = new BootstrapModelPrivate::Lines(); - l->hostname = "bootstrap.ring.cx"; - l->port = -1; + clearLines(); - q_ptr->beginInsertRows(QModelIndex(), m_lines.size(), m_lines.size()); - m_lines << l; - q_ptr->endInsertRows(); + auto defaultBootStrapServers = loadDefaultBootstrapServers(); + if (defaultBootStrapServers.size() >= 1) + { + q_ptr->beginInsertRows( + QModelIndex(), + m_lines.size(), + m_lines.size() + defaultBootStrapServers.size() - 1 + ); + m_lines << defaultBootStrapServers; + q_ptr->endInsertRows(); + } + else + { + /* If we can't load anything from file, default to bootstrap.ring.cx */ + BootstrapModelPrivate::Lines* l = new BootstrapModelPrivate::Lines(); + l->hostname = "bootstrap.ring.cx"; + l->port = -1; + + q_ptr->beginInsertRows(QModelIndex(), m_lines.size(), m_lines.size()); + m_lines << l; + q_ptr->endInsertRows(); + } q_ptr << BootstrapModel::EditAction::MODIFY; } diff --git a/src/bootstrapmodel.h b/src/bootstrapmodel.h index d7727c498329ba8484ef23faba064e8277368e1f..ebdb19b4e7381533b7ce0218e8a571f6f82351de 100644 --- a/src/bootstrapmodel.h +++ b/src/bootstrapmodel.h @@ -53,6 +53,7 @@ public: MODIFIED = 2, /*!< Our version differ from the remote one */ OUTDATED = 3, /*!< The remote version differ from ours */ RELOADING = 4, /*!< During a reload */ + RESETING = 5, /*!< During a reset */ COUNT__ };