mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-01-09 04:08:16 +08:00
Merge pull request #14140 from glassez/namespace
Use nested namespaces definition syntax
This commit is contained in:
commit
08e0349ca3
@ -33,15 +33,12 @@
|
||||
|
||||
class QByteArray;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::ByteArray
|
||||
{
|
||||
namespace ByteArray
|
||||
{
|
||||
// Mimic QString::splitRef(sep, behavior)
|
||||
QVector<QByteArray> splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts);
|
||||
// Mimic QString::splitRef(sep, behavior)
|
||||
QVector<QByteArray> splitToViews(const QByteArray &in, const QByteArray &sep, const QString::SplitBehavior behavior = QString::KeepEmptyParts);
|
||||
|
||||
// Mimic QByteArray::mid(pos, len) but instead of returning a full-copy,
|
||||
// we only return a partial view
|
||||
const QByteArray midView(const QByteArray &in, int pos, int len = -1);
|
||||
}
|
||||
// Mimic QByteArray::mid(pos, len) but instead of returning a full-copy,
|
||||
// we only return a partial view
|
||||
const QByteArray midView(const QByteArray &in, int pos, int len = -1);
|
||||
}
|
||||
|
@ -33,21 +33,18 @@
|
||||
|
||||
#include "base/utils/version.h"
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::ForeignApps
|
||||
{
|
||||
namespace ForeignApps
|
||||
struct PythonInfo
|
||||
{
|
||||
struct PythonInfo
|
||||
{
|
||||
using Version = Utils::Version<quint8, 3, 1>;
|
||||
using Version = Utils::Version<quint8, 3, 1>;
|
||||
|
||||
bool isValid() const;
|
||||
bool isSupportedVersion() const;
|
||||
bool isValid() const;
|
||||
bool isSupportedVersion() const;
|
||||
|
||||
QString executableName;
|
||||
Version version;
|
||||
};
|
||||
QString executableName;
|
||||
Version version;
|
||||
};
|
||||
|
||||
PythonInfo pythonInfo();
|
||||
}
|
||||
PythonInfo pythonInfo();
|
||||
}
|
||||
|
@ -34,47 +34,44 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Fs
|
||||
{
|
||||
namespace Fs
|
||||
{
|
||||
/**
|
||||
* Converts a path to a string suitable for display.
|
||||
* This function makes sure the directory separator used is consistent
|
||||
* with the OS being run.
|
||||
*/
|
||||
QString toNativePath(const QString &path);
|
||||
/**
|
||||
* Converts a path to a string suitable for processing.
|
||||
* This function makes sure the directory separator used is independent
|
||||
* from the OS being run so it is the same on all supported platforms.
|
||||
* Slash ('/') is used as "uniform" directory separator.
|
||||
*/
|
||||
QString toUniformPath(const QString &path);
|
||||
/**
|
||||
* Converts a path to a string suitable for display.
|
||||
* This function makes sure the directory separator used is consistent
|
||||
* with the OS being run.
|
||||
*/
|
||||
QString toNativePath(const QString &path);
|
||||
/**
|
||||
* Converts a path to a string suitable for processing.
|
||||
* This function makes sure the directory separator used is independent
|
||||
* from the OS being run so it is the same on all supported platforms.
|
||||
* Slash ('/') is used as "uniform" directory separator.
|
||||
*/
|
||||
QString toUniformPath(const QString &path);
|
||||
|
||||
QString fileExtension(const QString &filename);
|
||||
QString fileName(const QString &filePath);
|
||||
QString folderName(const QString &filePath);
|
||||
qint64 computePathSize(const QString &path);
|
||||
bool sameFiles(const QString &path1, const QString &path2);
|
||||
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
|
||||
, const QString &pad = QLatin1String(" "));
|
||||
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
|
||||
qint64 freeDiskSpaceOnPath(const QString &path);
|
||||
QString branchPath(const QString &filePath, QString *removed = nullptr);
|
||||
bool sameFileNames(const QString &first, const QString &second);
|
||||
QString expandPath(const QString &path);
|
||||
QString expandPathAbs(const QString &path);
|
||||
bool isRegularFile(const QString &path);
|
||||
QString fileExtension(const QString &filename);
|
||||
QString fileName(const QString &filePath);
|
||||
QString folderName(const QString &filePath);
|
||||
qint64 computePathSize(const QString &path);
|
||||
bool sameFiles(const QString &path1, const QString &path2);
|
||||
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
|
||||
, const QString &pad = QLatin1String(" "));
|
||||
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
|
||||
qint64 freeDiskSpaceOnPath(const QString &path);
|
||||
QString branchPath(const QString &filePath, QString *removed = nullptr);
|
||||
bool sameFileNames(const QString &first, const QString &second);
|
||||
QString expandPath(const QString &path);
|
||||
QString expandPathAbs(const QString &path);
|
||||
bool isRegularFile(const QString &path);
|
||||
|
||||
bool smartRemoveEmptyFolderTree(const QString &path);
|
||||
bool forceRemove(const QString &filePath);
|
||||
void removeDirRecursive(const QString &path);
|
||||
bool smartRemoveEmptyFolderTree(const QString &path);
|
||||
bool forceRemove(const QString &filePath);
|
||||
void removeDirRecursive(const QString &path);
|
||||
|
||||
QString tempPath();
|
||||
QString tempPath();
|
||||
|
||||
#if !defined Q_OS_HAIKU
|
||||
bool isNetworkFileSystem(const QString &path);
|
||||
bool isNetworkFileSystem(const QString &path);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -31,11 +31,8 @@
|
||||
|
||||
class QByteArray;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Gzip
|
||||
{
|
||||
namespace Gzip
|
||||
{
|
||||
QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
|
||||
QByteArray decompress(const QByteArray &data, bool *ok = nullptr);
|
||||
}
|
||||
QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
|
||||
QByteArray decompress(const QByteArray &data, bool *ok = nullptr);
|
||||
}
|
||||
|
@ -34,47 +34,44 @@
|
||||
class QByteArray;
|
||||
class QFileDevice;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::IO
|
||||
{
|
||||
namespace IO
|
||||
// A wrapper class that satisfy LegacyOutputIterator requirement
|
||||
class FileDeviceOutputIterator
|
||||
{
|
||||
// A wrapper class that satisfy LegacyOutputIterator requirement
|
||||
class FileDeviceOutputIterator
|
||||
public:
|
||||
// std::iterator_traits
|
||||
using iterator_category = std::output_iterator_tag;
|
||||
using difference_type = void;
|
||||
using value_type = void;
|
||||
using pointer = void;
|
||||
using reference = void;
|
||||
|
||||
explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024));
|
||||
FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default;
|
||||
~FileDeviceOutputIterator();
|
||||
|
||||
// mimic std::ostream_iterator behavior
|
||||
FileDeviceOutputIterator &operator=(char c);
|
||||
|
||||
constexpr FileDeviceOutputIterator &operator*()
|
||||
{
|
||||
public:
|
||||
// std::iterator_traits
|
||||
using iterator_category = std::output_iterator_tag;
|
||||
using difference_type = void;
|
||||
using value_type = void;
|
||||
using pointer = void;
|
||||
using reference = void;
|
||||
return *this;
|
||||
}
|
||||
|
||||
explicit FileDeviceOutputIterator(QFileDevice &device, const int bufferSize = (4 * 1024));
|
||||
FileDeviceOutputIterator(const FileDeviceOutputIterator &other) = default;
|
||||
~FileDeviceOutputIterator();
|
||||
constexpr FileDeviceOutputIterator &operator++()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
// mimic std::ostream_iterator behavior
|
||||
FileDeviceOutputIterator &operator=(char c);
|
||||
constexpr FileDeviceOutputIterator &operator++(int)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr FileDeviceOutputIterator &operator*()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr FileDeviceOutputIterator &operator++()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr FileDeviceOutputIterator &operator++(int)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
QFileDevice *m_device;
|
||||
std::shared_ptr<QByteArray> m_buffer;
|
||||
int m_bufferSize;
|
||||
};
|
||||
}
|
||||
private:
|
||||
QFileDevice *m_device;
|
||||
std::shared_ptr<QByteArray> m_buffer;
|
||||
int m_bufferSize;
|
||||
};
|
||||
}
|
||||
|
@ -41,69 +41,66 @@ enum class ShutdownDialogAction;
|
||||
|
||||
/* Miscellaneous functions that can be useful */
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Misc
|
||||
{
|
||||
namespace Misc
|
||||
// use binary prefix standards from IEC 60027-2
|
||||
// see http://en.wikipedia.org/wiki/Kilobyte
|
||||
enum class SizeUnit
|
||||
{
|
||||
// use binary prefix standards from IEC 60027-2
|
||||
// see http://en.wikipedia.org/wiki/Kilobyte
|
||||
enum class SizeUnit
|
||||
{
|
||||
Byte, // 1024^0,
|
||||
KibiByte, // 1024^1,
|
||||
MebiByte, // 1024^2,
|
||||
GibiByte, // 1024^3,
|
||||
TebiByte, // 1024^4,
|
||||
PebiByte, // 1024^5,
|
||||
ExbiByte // 1024^6,
|
||||
// int64 is used for sizes and thus the next units can not be handled
|
||||
// ZebiByte, // 1024^7,
|
||||
// YobiByte, // 1024^8
|
||||
};
|
||||
Byte, // 1024^0,
|
||||
KibiByte, // 1024^1,
|
||||
MebiByte, // 1024^2,
|
||||
GibiByte, // 1024^3,
|
||||
TebiByte, // 1024^4,
|
||||
PebiByte, // 1024^5,
|
||||
ExbiByte // 1024^6,
|
||||
// int64 is used for sizes and thus the next units can not be handled
|
||||
// ZebiByte, // 1024^7,
|
||||
// YobiByte, // 1024^8
|
||||
};
|
||||
|
||||
QString parseHtmlLinks(const QString &rawText);
|
||||
QString parseHtmlLinks(const QString &rawText);
|
||||
|
||||
void shutdownComputer(const ShutdownDialogAction &action);
|
||||
void shutdownComputer(const ShutdownDialogAction &action);
|
||||
|
||||
QString osName();
|
||||
QString boostVersionString();
|
||||
QString libtorrentVersionString();
|
||||
QString opensslVersionString();
|
||||
QString zlibVersionString();
|
||||
QString osName();
|
||||
QString boostVersionString();
|
||||
QString libtorrentVersionString();
|
||||
QString opensslVersionString();
|
||||
QString zlibVersionString();
|
||||
|
||||
QString unitString(SizeUnit unit, bool isSpeed = false);
|
||||
QString unitString(SizeUnit unit, bool isSpeed = false);
|
||||
|
||||
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
|
||||
// value must be given in bytes
|
||||
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false);
|
||||
int friendlyUnitPrecision(SizeUnit unit);
|
||||
qint64 sizeInBytes(qreal size, SizeUnit unit);
|
||||
// return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
|
||||
// value must be given in bytes
|
||||
QString friendlyUnit(qint64 bytesValue, bool isSpeed = false);
|
||||
int friendlyUnitPrecision(SizeUnit unit);
|
||||
qint64 sizeInBytes(qreal size, SizeUnit unit);
|
||||
|
||||
bool isPreviewable(const QString &extension);
|
||||
bool isPreviewable(const QString &extension);
|
||||
|
||||
// Take a number of seconds and return a user-friendly
|
||||
// time duration like "1d 2h 10m".
|
||||
QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1);
|
||||
QString getUserIDString();
|
||||
// Take a number of seconds and return a user-friendly
|
||||
// time duration like "1d 2h 10m".
|
||||
QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1);
|
||||
QString getUserIDString();
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
QString windowsSystemPath();
|
||||
QString windowsSystemPath();
|
||||
|
||||
template <typename T>
|
||||
T loadWinAPI(const QString &source, const char *funcName)
|
||||
{
|
||||
QString path = windowsSystemPath();
|
||||
if (!path.endsWith('\\'))
|
||||
path += '\\';
|
||||
template <typename T>
|
||||
T loadWinAPI(const QString &source, const char *funcName)
|
||||
{
|
||||
QString path = windowsSystemPath();
|
||||
if (!path.endsWith('\\'))
|
||||
path += '\\';
|
||||
|
||||
path += source;
|
||||
path += source;
|
||||
|
||||
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
|
||||
path.toWCharArray(pathWchar.get());
|
||||
auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
|
||||
path.toWCharArray(pathWchar.get());
|
||||
|
||||
return reinterpret_cast<T>(
|
||||
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
|
||||
}
|
||||
#endif // Q_OS_WIN
|
||||
return reinterpret_cast<T>(
|
||||
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
|
||||
}
|
||||
#endif // Q_OS_WIN
|
||||
}
|
||||
|
@ -35,24 +35,21 @@ class QSslCertificate;
|
||||
class QSslKey;
|
||||
class QString;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Net
|
||||
{
|
||||
namespace Net
|
||||
{
|
||||
using Subnet = QPair<QHostAddress, int>;
|
||||
using Subnet = QPair<QHostAddress, int>;
|
||||
|
||||
bool isValidIP(const QString &ip);
|
||||
Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr);
|
||||
bool canParseSubnet(const QString &subnetStr);
|
||||
bool isLoopbackAddress(const QHostAddress &addr);
|
||||
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
|
||||
QString subnetToString(const Subnet &subnet);
|
||||
QHostAddress canonicalIPv6Addr(const QHostAddress &addr);
|
||||
bool isValidIP(const QString &ip);
|
||||
Subnet parseSubnet(const QString &subnetStr, bool *ok = nullptr);
|
||||
bool canParseSubnet(const QString &subnetStr);
|
||||
bool isLoopbackAddress(const QHostAddress &addr);
|
||||
bool isIPInRange(const QHostAddress &addr, const QVector<Subnet> &subnets);
|
||||
QString subnetToString(const Subnet &subnet);
|
||||
QHostAddress canonicalIPv6Addr(const QHostAddress &addr);
|
||||
|
||||
const int MAX_SSL_FILE_SIZE = 1024 * 1024;
|
||||
QList<QSslCertificate> loadSSLCertificate(const QByteArray &data);
|
||||
bool isSSLCertificatesValid(const QByteArray &data);
|
||||
QSslKey loadSSLKey(const QByteArray &data);
|
||||
bool isSSLKeyValid(const QByteArray &data);
|
||||
}
|
||||
const int MAX_SSL_FILE_SIZE = 1024 * 1024;
|
||||
QList<QSslCertificate> loadSSLCertificate(const QByteArray &data);
|
||||
bool isSSLCertificatesValid(const QByteArray &data);
|
||||
QSslKey loadSSLKey(const QByteArray &data);
|
||||
bool isSSLKeyValid(const QByteArray &data);
|
||||
}
|
||||
|
@ -31,21 +31,18 @@
|
||||
class QByteArray;
|
||||
class QString;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Password
|
||||
{
|
||||
namespace Password
|
||||
// Implements constant-time comparison to protect against timing attacks
|
||||
// Taken from https://crackstation.net/hashing-security.htm
|
||||
bool slowEquals(const QByteArray &a, const QByteArray &b);
|
||||
|
||||
namespace PBKDF2
|
||||
{
|
||||
// Implements constant-time comparison to protect against timing attacks
|
||||
// Taken from https://crackstation.net/hashing-security.htm
|
||||
bool slowEquals(const QByteArray &a, const QByteArray &b);
|
||||
QByteArray generate(const QString &password);
|
||||
QByteArray generate(const QByteArray &password);
|
||||
|
||||
namespace PBKDF2
|
||||
{
|
||||
QByteArray generate(const QString &password);
|
||||
QByteArray generate(const QByteArray &password);
|
||||
|
||||
bool verify(const QByteArray &secret, const QString &password);
|
||||
bool verify(const QByteArray &secret, const QByteArray &password);
|
||||
}
|
||||
bool verify(const QByteArray &secret, const QString &password);
|
||||
bool verify(const QByteArray &secret, const QByteArray &password);
|
||||
}
|
||||
}
|
||||
|
@ -31,10 +31,7 @@
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::Random
|
||||
{
|
||||
namespace Random
|
||||
{
|
||||
uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits<uint32_t>::max());
|
||||
}
|
||||
uint32_t rand(uint32_t min = 0, uint32_t max = std::numeric_limits<uint32_t>::max());
|
||||
}
|
||||
|
@ -37,59 +37,56 @@
|
||||
|
||||
class QStringRef;
|
||||
|
||||
namespace Utils
|
||||
namespace Utils::String
|
||||
{
|
||||
namespace String
|
||||
QString fromDouble(double n, int precision);
|
||||
|
||||
int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity);
|
||||
template <Qt::CaseSensitivity caseSensitivity>
|
||||
bool naturalLessThan(const QString &left, const QString &right)
|
||||
{
|
||||
QString fromDouble(double n, int precision);
|
||||
return (naturalCompare(left, right, caseSensitivity) < 0);
|
||||
}
|
||||
|
||||
int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity);
|
||||
template <Qt::CaseSensitivity caseSensitivity>
|
||||
bool naturalLessThan(const QString &left, const QString &right)
|
||||
QString wildcardToRegex(const QString &pattern);
|
||||
|
||||
template <typename T>
|
||||
T unquote(const T &str, const QString "es = QChar('"'))
|
||||
{
|
||||
if (str.length() < 2) return str;
|
||||
|
||||
for (const QChar quote : quotes)
|
||||
{
|
||||
return (naturalCompare(left, right, caseSensitivity) < 0);
|
||||
if (str.startsWith(quote) && str.endsWith(quote))
|
||||
return str.mid(1, (str.length() - 2));
|
||||
}
|
||||
|
||||
QString wildcardToRegex(const QString &pattern);
|
||||
return str;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T unquote(const T &str, const QString "es = QChar('"'))
|
||||
{
|
||||
if (str.length() < 2) return str;
|
||||
bool parseBool(const QString &string, bool defaultValue);
|
||||
|
||||
for (const QChar quote : quotes)
|
||||
{
|
||||
if (str.startsWith(quote) && str.endsWith(quote))
|
||||
return str.mid(1, (str.length() - 2));
|
||||
}
|
||||
QString join(const QVector<QStringRef> &strings, const QString &separator);
|
||||
|
||||
return str;
|
||||
}
|
||||
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
|
||||
QString fromEnum(const T &value)
|
||||
{
|
||||
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
|
||||
"Enumeration underlying type has to be int.");
|
||||
|
||||
bool parseBool(const QString &string, bool defaultValue);
|
||||
const auto metaEnum = QMetaEnum::fromType<T>();
|
||||
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
|
||||
}
|
||||
|
||||
QString join(const QVector<QStringRef> &strings, const QString &separator);
|
||||
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
|
||||
T toEnum(const QString &serializedValue, const T &defaultValue)
|
||||
{
|
||||
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
|
||||
"Enumeration underlying type has to be int.");
|
||||
|
||||
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
|
||||
QString fromEnum(const T &value)
|
||||
{
|
||||
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
|
||||
"Enumeration underlying type has to be int.");
|
||||
|
||||
const auto metaEnum = QMetaEnum::fromType<T>();
|
||||
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
|
||||
}
|
||||
|
||||
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
|
||||
T toEnum(const QString &serializedValue, const T &defaultValue)
|
||||
{
|
||||
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
|
||||
"Enumeration underlying type has to be int.");
|
||||
|
||||
const auto metaEnum = QMetaEnum::fromType<T>();
|
||||
bool ok = false;
|
||||
const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
|
||||
return (ok ? value : defaultValue);
|
||||
}
|
||||
const auto metaEnum = QMetaEnum::fromType<T>();
|
||||
bool ok = false;
|
||||
const T value = static_cast<T>(metaEnum.keyToValue(serializedValue.toLatin1().constData(), &ok));
|
||||
return (ok ? value : defaultValue);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user