mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): pass Paths into BootstrapNodeUpdater
In preparation of loading local bootstrap list files.
This commit is contained in:
parent
5a877d742b
commit
e5f33608c4
|
@ -19,6 +19,8 @@
|
|||
|
||||
#include "bootstrapnodeupdater.h"
|
||||
|
||||
#include "src/persistence/paths.h"
|
||||
|
||||
#include <QDirIterator>
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
|
@ -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<DhtServer> BootstrapNodeUpdater::getBootstrapnodes()
|
||||
|
@ -97,6 +100,25 @@ QList<DhtServer> BootstrapNodeUpdater::loadDefaultBootstrapNodes()
|
|||
return jsonToNodeList(d);
|
||||
}
|
||||
|
||||
QList<DhtServer> 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) {
|
||||
|
|
|
@ -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<DhtServer> getBootstrapnodes() override;
|
||||
void requestBootstrapNodes();
|
||||
static QList<DhtServer> loadDefaultBootstrapNodes();
|
||||
|
@ -47,8 +48,10 @@ private slots:
|
|||
private:
|
||||
static QList<DhtServer> jsonToNodeList(const QJsonDocument& nodeList);
|
||||
static void jsonNodeToDhtServer(const QJsonObject& node, QList<DhtServer>& outList);
|
||||
QList<DhtServer> loadUserBootrapNodes();
|
||||
|
||||
private:
|
||||
QNetworkProxy proxy;
|
||||
QNetworkAccessManager nam;
|
||||
Paths& paths;
|
||||
};
|
||||
|
|
|
@ -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<ToxEncrypt> passkey)
|
||||
Profile::Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> 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);
|
||||
|
|
|
@ -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<ToxEncrypt> passkey);
|
||||
Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> passkey, Paths& paths);
|
||||
static QStringList getFilesByExt(QString extension);
|
||||
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
|
||||
bool saveToxSave(QByteArray data);
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue
Block a user