Merge pull request #11041 from Chocobo1/splitRef

Revise operations in TorrentHandle class
This commit is contained in:
Mike Tzou
2019-08-09 12:27:44 +08:00
committed by GitHub
14 changed files with 121 additions and 100 deletions

View File

@@ -1505,10 +1505,12 @@ void Session::enableBandwidthScheduler()
void Session::populateAdditionalTrackers()
{
m_additionalTrackerList.clear();
for (QString tracker : asConst(additionalTrackers().split('\n'))) {
const QString trackers = additionalTrackers();
for (QStringRef tracker : asConst(trackers.splitRef('\n'))) {
tracker = tracker.trimmed();
if (!tracker.isEmpty())
m_additionalTrackerList << tracker;
m_additionalTrackerList << tracker.toString();
}
}

View File

@@ -104,6 +104,8 @@ namespace
std::vector<LTDownloadPriority> toLTDownloadPriorities(const QVector<DownloadPriority> &priorities)
{
std::vector<LTDownloadPriority> out;
out.reserve(priorities.size());
std::transform(priorities.cbegin(), priorities.cend()
, std::back_inserter(out), [](BitTorrent::DownloadPriority priority)
{
@@ -264,16 +266,17 @@ InfoHash TorrentHandle::hash() const
QString TorrentHandle::name() const
{
QString name = m_name;
if (name.isEmpty())
name = QString::fromStdString(m_nativeStatus.name);
if (!name.isEmpty()) return name;
if (name.isEmpty() && hasMetadata())
name = QString::fromStdString(m_nativeStatus.name);
if (!name.isEmpty()) return name;
if (hasMetadata()) {
name = QString::fromStdString(m_torrentInfo.nativeInfo()->orig_files().name());
if (!name.isEmpty()) return name;
}
if (name.isEmpty())
name = m_hash;
return name;
return m_hash;
}
QDateTime TorrentHandle::creationDate() const
@@ -353,14 +356,15 @@ QString TorrentHandle::rootPath(bool actual) const
return QDir(savePath(actual)).absoluteFilePath(firstFilePath);
}
QString TorrentHandle::contentPath(bool actual) const
QString TorrentHandle::contentPath(const bool actual) const
{
if (filesCount() == 1)
return QDir(savePath(actual)).absoluteFilePath(filePath(0));
else if (hasRootFolder())
if (hasRootFolder())
return rootPath(actual);
else
return savePath(actual);
return savePath(actual);
}
bool TorrentHandle::isAutoTMMEnabled() const
@@ -412,12 +416,14 @@ void TorrentHandle::setAutoManaged(const bool enable)
QVector<TrackerEntry> TorrentHandle::trackers() const
{
const std::vector<lt::announce_entry> announces = m_nativeHandle.trackers();
const std::vector<lt::announce_entry> nativeTrackers = m_nativeHandle.trackers();
QVector<TrackerEntry> entries;
entries.reserve(announces.size());
for (const lt::announce_entry &tracker : announces)
entries.reserve(nativeTrackers.size());
for (const lt::announce_entry &tracker : nativeTrackers)
entries << tracker;
return entries;
}
@@ -428,7 +434,9 @@ QHash<QString, TrackerInfo> TorrentHandle::trackerInfos() const
void TorrentHandle::addTrackers(const QVector<TrackerEntry> &trackers)
{
const QVector<TrackerEntry> currentTrackers = this->trackers();
QSet<TrackerEntry> currentTrackers;
for (const lt::announce_entry &entry : m_nativeHandle.trackers())
currentTrackers << entry;
QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size());
@@ -451,16 +459,17 @@ void TorrentHandle::replaceTrackers(const QVector<TrackerEntry> &trackers)
QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size());
std::vector<lt::announce_entry> announces;
std::vector<lt::announce_entry> nativeTrackers;
nativeTrackers.reserve(trackers.size());
for (const TrackerEntry &tracker : trackers) {
announces.emplace_back(tracker.nativeEntry());
nativeTrackers.emplace_back(tracker.nativeEntry());
if (!currentTrackers.removeOne(tracker))
newTrackers << tracker;
}
m_nativeHandle.replace_trackers(announces);
m_nativeHandle.replace_trackers(nativeTrackers);
if (newTrackers.isEmpty() && currentTrackers.isEmpty()) {
// when existing tracker reorders
@@ -477,12 +486,12 @@ void TorrentHandle::replaceTrackers(const QVector<TrackerEntry> &trackers)
QVector<QUrl> TorrentHandle::urlSeeds() const
{
const std::set<std::string> seeds = m_nativeHandle.url_seeds();
const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> urlSeeds;
urlSeeds.reserve(seeds.size());
urlSeeds.reserve(currentSeeds.size());
for (const std::string &urlSeed : seeds)
for (const std::string &urlSeed : currentSeeds)
urlSeeds.append(QUrl(urlSeed.c_str()));
return urlSeeds;
@@ -490,11 +499,17 @@ QVector<QUrl> TorrentHandle::urlSeeds() const
void TorrentHandle::addUrlSeeds(const QVector<QUrl> &urlSeeds)
{
const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> addedUrlSeeds;
addedUrlSeeds.reserve(urlSeeds.size());
for (const QUrl &urlSeed : urlSeeds) {
if (addUrlSeed(urlSeed))
addedUrlSeeds << urlSeed;
for (const QUrl &url : urlSeeds) {
const std::string nativeUrl = url.toString().toStdString();
if (currentSeeds.find(nativeUrl) == currentSeeds.end()) {
m_nativeHandle.add_url_seed(nativeUrl);
addedUrlSeeds << url;
}
}
if (!addedUrlSeeds.isEmpty())
@@ -503,35 +518,23 @@ void TorrentHandle::addUrlSeeds(const QVector<QUrl> &urlSeeds)
void TorrentHandle::removeUrlSeeds(const QVector<QUrl> &urlSeeds)
{
const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> removedUrlSeeds;
removedUrlSeeds.reserve(urlSeeds.size());
for (const QUrl &urlSeed : urlSeeds) {
if (removeUrlSeed(urlSeed))
removedUrlSeeds << urlSeed;
for (const QUrl &url : urlSeeds) {
const std::string nativeUrl = url.toString().toStdString();
if (currentSeeds.find(nativeUrl) != currentSeeds.end()) {
m_nativeHandle.remove_url_seed(nativeUrl);
removedUrlSeeds << url;
}
}
if (!removedUrlSeeds.isEmpty())
m_session->handleTorrentUrlSeedsRemoved(this, removedUrlSeeds);
}
bool TorrentHandle::addUrlSeed(const QUrl &urlSeed)
{
const QVector<QUrl> seeds = urlSeeds();
if (seeds.contains(urlSeed)) return false;
m_nativeHandle.add_url_seed(urlSeed.toString().toStdString());
return true;
}
bool TorrentHandle::removeUrlSeed(const QUrl &urlSeed)
{
const QVector<QUrl> seeds = urlSeeds();
if (!seeds.contains(urlSeed)) return false;
m_nativeHandle.remove_url_seed(urlSeed.toString().toStdString());
return true;
}
bool TorrentHandle::connectPeer(const PeerAddress &peerAddress)
{
lt::error_code ec;

View File

@@ -394,8 +394,6 @@ namespace BitTorrent
void move_impl(QString path, bool overwrite);
void moveStorage(const QString &newPath, bool overwrite);
void manageIncompleteFiles();
bool addUrlSeed(const QUrl &urlSeed);
bool removeUrlSeed(const QUrl &urlSeed);
void setFirstLastPiecePriorityImpl(bool enabled, const QVector<DownloadPriority> &updatedFilePrio = {});
Session *const m_session;

View File

@@ -429,9 +429,10 @@ void TorrentInfo::stripRootFolder()
lt::file_storage files = m_nativeInfo->files();
// Solution for case of renamed root folder
const std::string testName = filePath(0).split('/').value(0).toStdString();
if (files.name() != testName) {
files.set_name(testName);
const QString path = filePath(0);
const std::string newName = path.left(path.indexOf('/')).toStdString();
if (files.name() != newName) {
files.set_name(newName);
for (int i = 0; i < files.num_files(); ++i)
files.rename_file(LTFileIndex {i}, files.file_path(LTFileIndex {i}));
}

View File

@@ -131,9 +131,9 @@ bool Connection::acceptsGzipEncoding(QString codings)
{
// [rfc7231] 5.3.4. Accept-Encoding
const auto isCodingAvailable = [](const QStringList &list, const QString &encoding) -> bool
const auto isCodingAvailable = [](const QVector<QStringRef> &list, const QString &encoding) -> bool
{
for (const QString &str : list) {
for (const QStringRef &str : list) {
if (!str.startsWith(encoding))
continue;
@@ -142,11 +142,11 @@ bool Connection::acceptsGzipEncoding(QString codings)
return true;
// [rfc7231] 5.3.1. Quality Values
const QStringRef substr = str.midRef(encoding.size() + 3); // ex. skip over "gzip;q="
const QStringRef substr = str.mid(encoding.size() + 3); // ex. skip over "gzip;q="
bool ok = false;
const double qvalue = substr.toDouble(&ok);
if (!ok || (qvalue <= 0.0))
if (!ok || (qvalue <= 0))
return false;
return true;
@@ -154,7 +154,7 @@ bool Connection::acceptsGzipEncoding(QString codings)
return false;
};
const QStringList list = codings.remove(' ').remove('\t').split(',', QString::SkipEmptyParts);
const QVector<QStringRef> list = codings.remove(' ').remove('\t').splitRef(',', QString::SkipEmptyParts);
if (list.isEmpty())
return false;

View File

@@ -32,9 +32,9 @@
#include <QProcess>
#include <QTimer>
#include "../global.h"
#include "../utils/foreignapps.h"
#include "../utils/fs.h"
#include "base/global.h"
#include "base/utils/foreignapps.h"
#include "base/utils/fs.h"
#include "searchpluginmanager.h"
namespace
@@ -161,24 +161,29 @@ void SearchHandler::processFailed()
// file url | file name | file size | nb seeds | nb leechers | Search engine url
bool SearchHandler::parseSearchResult(const QString &line, SearchResult &searchResult)
{
const QStringList parts = line.split('|');
const QVector<QStringRef> parts = line.splitRef('|');
const int nbFields = parts.size();
if (nbFields < (NB_PLUGIN_COLUMNS - 1)) return false; // -1 because desc_link is optional
searchResult = SearchResult();
searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed(); // download URL
searchResult.fileName = parts.at(PL_NAME).trimmed(); // Name
searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed().toString(); // download URL
searchResult.fileName = parts.at(PL_NAME).trimmed().toString(); // Name
searchResult.fileSize = parts.at(PL_SIZE).trimmed().toLongLong(); // Size
bool ok = false;
searchResult.nbSeeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok); // Seeders
if (!ok || (searchResult.nbSeeders < 0))
searchResult.nbSeeders = -1;
searchResult.nbLeechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok); // Leechers
if (!ok || (searchResult.nbLeechers < 0))
searchResult.nbLeechers = -1;
searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed(); // Search site URL
searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed().toString(); // Search site URL
if (nbFields == NB_PLUGIN_COLUMNS)
searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed(); // Description Link
searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed().toString(); // Description Link
return true;
}

View File

@@ -376,8 +376,10 @@ void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &resu
Utils::Fs::forceRemove(filePath);
}
else {
QString pluginName = result.url.split('/').last();
const QString url = result.url;
QString pluginName = url.mid(url.lastIndexOf('/') + 1);
pluginName.replace(".py", "", Qt::CaseInsensitive);
if (pluginInfo(pluginName))
emit pluginUpdateFailed(pluginName, tr("Failed to download the plugin file. %1").arg(result.errorString));
else
@@ -462,7 +464,7 @@ void SearchPluginManager::update()
plugin->fullName = engineElem.elementsByTagName("name").at(0).toElement().text();
plugin->url = engineElem.elementsByTagName("url").at(0).toElement().text();
const auto categories = engineElem.elementsByTagName("categories").at(0).toElement().text().split(' ');
const QStringList categories = engineElem.elementsByTagName("categories").at(0).toElement().text().split(' ');
for (QString cat : categories) {
cat = cat.trimmed();
if (!cat.isEmpty())

View File

@@ -465,7 +465,7 @@ QString Utils::Misc::libtorrentVersionString()
QString Utils::Misc::opensslVersionString()
{
const QString version {OPENSSL_VERSION_TEXT};
return version.split(' ', QString::SkipEmptyParts)[1];
return version.splitRef(' ', QString::SkipEmptyParts)[1].toString();
}
QString Utils::Misc::zlibVersionString()