Implement class for handling filesystem paths

PR #15915.
This commit is contained in:
Vladimir Golovnev
2022-02-08 06:03:48 +03:00
committed by GitHub
parent facfa26eed
commit dd1bd8ad10
131 changed files with 2252 additions and 1868 deletions

View File

@@ -42,14 +42,14 @@ const int MAX_REDIRECTIONS = 20; // the common value for web browsers
namespace
{
nonstd::expected<QString, QString> saveToTempFile(const QByteArray &data)
nonstd::expected<Path, QString> saveToTempFile(const QByteArray &data)
{
QTemporaryFile file {Utils::Fs::tempPath()};
QTemporaryFile file {Utils::Fs::tempPath().data()};
if (!file.open() || (file.write(data) != data.length()) || !file.flush())
return nonstd::make_unexpected(file.errorString());
file.setAutoRemove(false);
return file.fileName();
return Path(file.fileName());
}
}
@@ -127,10 +127,10 @@ void DownloadHandlerImpl::processFinishedDownload()
if (m_downloadRequest.saveToFile())
{
const QString destinationPath = m_downloadRequest.destFileName();
const Path destinationPath = m_downloadRequest.destFileName();
if (destinationPath.isEmpty())
{
const nonstd::expected<QString, QString> result = saveToTempFile(m_result.data);
const nonstd::expected<Path, QString> result = saveToTempFile(m_result.data);
if (result)
m_result.filePath = result.value();
else

View File

@@ -348,12 +348,12 @@ Net::DownloadRequest &Net::DownloadRequest::saveToFile(const bool value)
return *this;
}
QString Net::DownloadRequest::destFileName() const
Path Net::DownloadRequest::destFileName() const
{
return m_destFileName;
}
Net::DownloadRequest &Net::DownloadRequest::destFileName(const QString &value)
Net::DownloadRequest &Net::DownloadRequest::destFileName(const Path &value)
{
m_destFileName = value;
return *this;

View File

@@ -35,6 +35,8 @@
#include <QQueue>
#include <QSet>
#include "base/path.h"
class QNetworkCookie;
class QNetworkReply;
class QSslError;
@@ -81,15 +83,15 @@ namespace Net
// if saveToFile is set, the file is saved in destFileName
// (deprecated) if destFileName is not provided, the file will be saved
// in a temporary file, the name of file is set in DownloadResult::filePath
QString destFileName() const;
DownloadRequest &destFileName(const QString &value);
Path destFileName() const;
DownloadRequest &destFileName(const Path &value);
private:
QString m_url;
QString m_userAgent;
qint64 m_limit = 0;
bool m_saveToFile = false;
QString m_destFileName;
Path m_destFileName;
};
struct DownloadResult
@@ -98,7 +100,7 @@ namespace Net
DownloadStatus status;
QString errorString;
QByteArray data;
QString filePath;
Path filePath;
QString magnet;
};

View File

@@ -26,13 +26,15 @@
* exception statement from your version.
*/
#include "geoipdatabase.h"
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QHostAddress>
#include <QVariant>
#include "geoipdatabase.h"
#include "base/path.h"
namespace
{
@@ -84,10 +86,10 @@ GeoIPDatabase::GeoIPDatabase(const quint32 size)
{
}
GeoIPDatabase *GeoIPDatabase::load(const QString &filename, QString &error)
GeoIPDatabase *GeoIPDatabase::load(const Path &filename, QString &error)
{
GeoIPDatabase *db = nullptr;
QFile file(filename);
QFile file {filename.data()};
if (file.size() > MAX_FILE_SIZE)
{
error = tr("Unsupported database file size.");

View File

@@ -28,11 +28,15 @@
#pragma once
#include <QCoreApplication>
#include <QtGlobal>
#include <QCoreApplication>
#include <QDateTime>
#include <QHash>
#include <QVariant>
#include "base/pathfwd.h"
class QByteArray;
class QDateTime;
class QHostAddress;
class QString;
@@ -43,7 +47,7 @@ class GeoIPDatabase
Q_DECLARE_TR_FUNCTIONS(GeoIPDatabase)
public:
static GeoIPDatabase *load(const QString &filename, QString &error);
static GeoIPDatabase *load(const Path &filename, QString &error);
static GeoIPDatabase *load(const QByteArray &data, QString &error);
~GeoIPDatabase();

View File

@@ -30,7 +30,6 @@
#include "geoipmanager.h"
#include <QDateTime>
#include <QDir>
#include <QHostAddress>
#include <QLocale>
@@ -43,9 +42,9 @@
#include "downloadmanager.h"
#include "geoipdatabase.h"
static const QString DATABASE_URL = QStringLiteral("https://download.db-ip.com/free/dbip-country-lite-%1.mmdb.gz");
static const char GEODB_FOLDER[] = "GeoDB";
static const char GEODB_FILENAME[] = "dbip-country-lite.mmdb";
const QString DATABASE_URL = QStringLiteral("https://download.db-ip.com/free/dbip-country-lite-%1.mmdb.gz");
const char GEODB_FOLDER[] = "GeoDB";
const char GEODB_FILENAME[] = "dbip-country-lite.mmdb";
using namespace Net;
@@ -88,17 +87,21 @@ void GeoIPManager::loadDatabase()
delete m_geoIPDatabase;
m_geoIPDatabase = nullptr;
const QString filepath = Utils::Fs::expandPathAbs(
QString::fromLatin1("%1/%2/%3").arg(specialFolderLocation(SpecialFolder::Data), GEODB_FOLDER, GEODB_FILENAME));
const Path filepath = specialFolderLocation(SpecialFolder::Data)
/ Path(GEODB_FOLDER) / Path(GEODB_FILENAME);
QString error;
m_geoIPDatabase = GeoIPDatabase::load(filepath, error);
if (m_geoIPDatabase)
{
Logger::instance()->addMessage(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
.arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()),
Log::INFO);
.arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()),
Log::INFO);
}
else
{
Logger::instance()->addMessage(tr("Couldn't load IP geolocation database. Reason: %1").arg(error), Log::WARNING);
}
manageDatabaseUpdate();
}
@@ -445,14 +448,13 @@ void GeoIPManager::downloadFinished(const DownloadResult &result)
delete m_geoIPDatabase;
m_geoIPDatabase = geoIPDatabase;
LogMsg(tr("IP geolocation database loaded. Type: %1. Build time: %2.")
.arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString()),
Log::INFO);
const QString targetPath = Utils::Fs::expandPathAbs(
QDir(specialFolderLocation(SpecialFolder::Data)).absoluteFilePath(GEODB_FOLDER));
if (!QDir(targetPath).exists())
QDir().mkpath(targetPath);
.arg(m_geoIPDatabase->type(), m_geoIPDatabase->buildEpoch().toString())
, Log::INFO);
const Path targetPath = specialFolderLocation(SpecialFolder::Data) / Path(GEODB_FOLDER);
if (!targetPath.exists())
Utils::Fs::mkpath(targetPath);
const auto path = QString::fromLatin1("%1/%2").arg(targetPath, GEODB_FILENAME);
const auto path = targetPath / Path(GEODB_FILENAME);
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, data);
if (result)
{