From e5f33608c41adfa903e9d26f856a43e34dee6d17 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Fri, 1 May 2020 22:06:03 -0700 Subject: [PATCH] refactor(core): pass Paths into BootstrapNodeUpdater In preparation of loading local bootstrap list files. --- src/net/bootstrapnodeupdater.cpp | 28 +++++++++++++++++++++++++--- src/net/bootstrapnodeupdater.h | 5 ++++- src/persistence/profile.cpp | 8 ++++---- src/persistence/profile.h | 2 +- test/net/bsu_test.cpp | 4 +++- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/net/bootstrapnodeupdater.cpp b/src/net/bootstrapnodeupdater.cpp index ecfd079bd..5379ba765 100644 --- a/src/net/bootstrapnodeupdater.cpp +++ b/src/net/bootstrapnodeupdater.cpp @@ -19,6 +19,8 @@ #include "bootstrapnodeupdater.h" +#include "src/persistence/paths.h" + #include #include #include @@ -53,9 +55,10 @@ const QStringList neededFields{status_udp, status_tcp, ipv4, ipv6, public_key, p * @brief Fetches a list of currently online bootstrap nodes from node.tox.chat * @param proxy Proxy to use for the lookup, must outlive this object */ -BootstrapNodeUpdater::BootstrapNodeUpdater(const QNetworkProxy& proxy, QObject* parent) - : QObject{parent} - , proxy{proxy} +BootstrapNodeUpdater::BootstrapNodeUpdater(const QNetworkProxy& proxy, Paths& _paths, QObject* parent) + : proxy{proxy} + , paths{_paths} + , QObject{parent} {} QList BootstrapNodeUpdater::getBootstrapnodes() @@ -97,6 +100,25 @@ QList BootstrapNodeUpdater::loadDefaultBootstrapNodes() return jsonToNodeList(d); } +QList BootstrapNodeUpdater::loadUserBootrapNodes() +{ + QFile nodesFile{builtinNodesFile}; + if (!nodesFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Couldn't read bootstrap nodes"; + return {}; + } + + QString nodesJson = nodesFile.readAll(); + nodesFile.close(); + QJsonDocument d = QJsonDocument::fromJson(nodesJson.toUtf8()); + if (d.isNull()) { + qWarning() << "Failed to parse JSON document"; + return {}; + } + + return jsonToNodeList(d); +} + void BootstrapNodeUpdater::onRequestComplete(QNetworkReply* reply) { if (reply->error() != QNetworkReply::NoError) { diff --git a/src/net/bootstrapnodeupdater.h b/src/net/bootstrapnodeupdater.h index c562f9b77..156d8ac71 100644 --- a/src/net/bootstrapnodeupdater.h +++ b/src/net/bootstrapnodeupdater.h @@ -28,12 +28,13 @@ #include "src/model/ibootstraplistgenerator.h" class QNetworkReply; +class Paths; class BootstrapNodeUpdater : public QObject, public IBootstrapListGenerator { Q_OBJECT public: - explicit BootstrapNodeUpdater(const QNetworkProxy& proxy, QObject* parent = nullptr); + explicit BootstrapNodeUpdater(const QNetworkProxy& proxy, Paths& _paths, QObject* parent = nullptr); QList getBootstrapnodes() override; void requestBootstrapNodes(); static QList loadDefaultBootstrapNodes(); @@ -47,8 +48,10 @@ private slots: private: static QList jsonToNodeList(const QJsonDocument& nodeList); static void jsonNodeToDhtServer(const QJsonObject& node, QList& outList); + QList loadUserBootrapNodes(); private: QNetworkProxy proxy; QNetworkAccessManager nam; + Paths& paths; }; diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 993a1375e..d20b3c393 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -277,12 +277,12 @@ void Profile::initCore(const QByteArray& toxsave, const ICoreSettings& s, bool i Qt::ConnectionType::QueuedConnection); } -Profile::Profile(const QString& name, const QString& password, std::unique_ptr passkey) +Profile::Profile(const QString& name, const QString& password, std::unique_ptr passkey, Paths& paths) : name{name} , passkey{std::move(passkey)} , isRemoved{false} , encrypted{this->passkey != nullptr} - , bootstrapNodes(Settings::getInstance().getProxy()) + , bootstrapNodes(Settings::getInstance().getProxy(), paths) {} /** @@ -315,7 +315,7 @@ Profile* Profile::loadProfile(const QString& name, const QString& password, Sett return nullptr; } - Profile* p = new Profile(name, password, std::move(tmpKey)); + Profile* p = new Profile(name, password, std::move(tmpKey), settings.getPaths()); // Core settings are saved per profile, need to load them before starting Core settings.updateProfileData(p, parser); @@ -346,7 +346,7 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se } settings.createPersonal(name); - Profile* p = new Profile(name, password, std::move(tmpKey)); + Profile* p = new Profile(name, password, std::move(tmpKey), settings.getPaths()); settings.updateProfileData(p, parser); p->initCore(QByteArray(), settings, /*isNewProfile*/ true); diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 4c4d07689..5689fabd0 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -102,7 +102,7 @@ private slots: void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash); private: - Profile(const QString& name, const QString& password, std::unique_ptr passkey); + Profile(const QString& name, const QString& password, std::unique_ptr passkey, Paths& paths); static QStringList getFilesByExt(QString extension); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); bool saveToxSave(QByteArray data); diff --git a/test/net/bsu_test.cpp b/test/net/bsu_test.cpp index f9d17eab6..32f301423 100644 --- a/test/net/bsu_test.cpp +++ b/test/net/bsu_test.cpp @@ -48,7 +48,9 @@ TestBootstrapNodesUpdater::TestBootstrapNodesUpdater() void TestBootstrapNodesUpdater::testOnline() { QNetworkProxy proxy{QNetworkProxy::ProxyType::NoProxy}; - BootstrapNodeUpdater updater{proxy}; + auto paths = Paths::makePaths(Paths::Portable::NonPortable); + + BootstrapNodeUpdater updater{proxy, *paths}; QSignalSpy spy(&updater, &BootstrapNodeUpdater::availableBootstrapNodes); updater.requestBootstrapNodes();