Browse Source

Revamp content layout handling

Apply content layout only if desired file names aren't provided.
Remove helpers with confusing signatures.
Don't remove root folder twice.

PR #16724.
Closes #16259.
adaptive-webui-19844
Vladimir Golovnev 3 years ago committed by GitHub
parent
commit
df2d449f9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/base/CMakeLists.txt
  2. 1
      src/base/base.pri
  3. 23
      src/base/bittorrent/session.cpp
  4. 69
      src/base/bittorrent/torrentcontentlayout.cpp
  5. 3
      src/base/bittorrent/torrentcontentlayout.h
  6. 19
      src/base/bittorrent/torrentimpl.cpp
  7. 1
      src/base/bittorrent/torrentimpl.h
  8. 9
      src/base/bittorrent/torrentinfo.cpp
  9. 2
      src/base/bittorrent/torrentinfo.h
  10. 5
      src/base/path.cpp
  11. 1
      src/base/path.h
  12. 53
      src/gui/addnewtorrentdialog.cpp
  13. 7
      src/gui/addnewtorrentdialog.h

1
src/base/CMakeLists.txt

@ -123,7 +123,6 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.cpp bittorrent/speedmonitor.cpp
bittorrent/statistics.cpp bittorrent/statistics.cpp
bittorrent/torrent.cpp bittorrent/torrent.cpp
bittorrent/torrentcontentlayout.cpp
bittorrent/torrentcreatorthread.cpp bittorrent/torrentcreatorthread.cpp
bittorrent/torrentimpl.cpp bittorrent/torrentimpl.cpp
bittorrent/torrentinfo.cpp bittorrent/torrentinfo.cpp

1
src/base/base.pri

@ -123,7 +123,6 @@ SOURCES += \
$$PWD/bittorrent/speedmonitor.cpp \ $$PWD/bittorrent/speedmonitor.cpp \
$$PWD/bittorrent/statistics.cpp \ $$PWD/bittorrent/statistics.cpp \
$$PWD/bittorrent/torrent.cpp \ $$PWD/bittorrent/torrent.cpp \
$$PWD/bittorrent/torrentcontentlayout.cpp \
$$PWD/bittorrent/torrentcreatorthread.cpp \ $$PWD/bittorrent/torrentcreatorthread.cpp \
$$PWD/bittorrent/torrentimpl.cpp \ $$PWD/bittorrent/torrentimpl.cpp \
$$PWD/bittorrent/torrentinfo.cpp \ $$PWD/bittorrent/torrentinfo.cpp \

23
src/base/bittorrent/session.cpp

@ -2255,10 +2255,25 @@ bool Session::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source
Q_ASSERT(addTorrentParams.filePaths.isEmpty() || (addTorrentParams.filePaths.size() == torrentInfo.filesCount())); Q_ASSERT(addTorrentParams.filePaths.isEmpty() || (addTorrentParams.filePaths.size() == torrentInfo.filesCount()));
const TorrentContentLayout contentLayout = ((loadTorrentParams.contentLayout == TorrentContentLayout::Original) PathList filePaths = addTorrentParams.filePaths;
? detectContentLayout(torrentInfo.filePaths()) : loadTorrentParams.contentLayout); if (filePaths.isEmpty())
PathList filePaths = (!addTorrentParams.filePaths.isEmpty() ? addTorrentParams.filePaths : torrentInfo.filePaths()); {
applyContentLayout(filePaths, contentLayout, Path::findRootFolder(torrentInfo.filePaths())); filePaths = torrentInfo.filePaths();
if (loadTorrentParams.contentLayout != TorrentContentLayout::Original)
{
const Path originalRootFolder = Path::findRootFolder(filePaths);
const auto originalContentLayout = (originalRootFolder.isEmpty()
? TorrentContentLayout::NoSubfolder
: TorrentContentLayout::Subfolder);
if (loadTorrentParams.contentLayout != originalContentLayout)
{
if (loadTorrentParams.contentLayout == TorrentContentLayout::NoSubfolder)
Path::stripRootFolder(filePaths);
else
Path::addRootFolder(filePaths, filePaths.at(0).removedExtension());
}
}
}
// if torrent name wasn't explicitly set we handle the case of // if torrent name wasn't explicitly set we handle the case of
// initial renaming of torrent content and rename torrent accordingly // initial renaming of torrent content and rename torrent accordingly

69
src/base/bittorrent/torrentcontentlayout.cpp

@ -1,69 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2020-2021 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "torrentcontentlayout.h"
#include "base/utils/fs.h"
namespace
{
Path removeExtension(const Path &fileName)
{
Path result = fileName;
result.removeExtension();
return result;
}
}
BitTorrent::TorrentContentLayout BitTorrent::detectContentLayout(const PathList &filePaths)
{
const Path rootFolder = Path::findRootFolder(filePaths);
return (rootFolder.isEmpty()
? TorrentContentLayout::NoSubfolder
: TorrentContentLayout::Subfolder);
}
void BitTorrent::applyContentLayout(PathList &filePaths, const BitTorrent::TorrentContentLayout contentLayout, const Path &rootFolder)
{
Q_ASSERT(!filePaths.isEmpty());
switch (contentLayout)
{
case TorrentContentLayout::Subfolder:
if (Path::findRootFolder(filePaths).isEmpty())
Path::addRootFolder(filePaths, !rootFolder.isEmpty() ? rootFolder : removeExtension(filePaths.at(0)));
break;
case TorrentContentLayout::NoSubfolder:
Path::stripRootFolder(filePaths);
break;
default:
break;
}
}

3
src/base/bittorrent/torrentcontentlayout.h

@ -51,7 +51,4 @@ namespace BitTorrent
Q_ENUM_NS(TorrentContentLayout) Q_ENUM_NS(TorrentContentLayout)
} }
TorrentContentLayout detectContentLayout(const PathList &filePaths);
void applyContentLayout(PathList &filePaths, TorrentContentLayout contentLayout, const Path &rootFolder = {});
} }

19
src/base/bittorrent/torrentimpl.cpp

@ -1769,11 +1769,24 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p)
TorrentInfo metadata = TorrentInfo(*m_nativeHandle.torrent_file()); TorrentInfo metadata = TorrentInfo(*m_nativeHandle.torrent_file());
const auto nativeIndexes = metadata.nativeIndexes(); const auto &renamedFiles = m_ltAddTorrentParams.renamed_files;
PathList filePaths = metadata.filePaths(); PathList filePaths = metadata.filePaths();
applyContentLayout(filePaths, m_contentLayout); if (renamedFiles.empty() && (m_contentLayout != TorrentContentLayout::Original))
{
const Path originalRootFolder = Path::findRootFolder(filePaths);
const auto originalContentLayout = (originalRootFolder.isEmpty()
? TorrentContentLayout::NoSubfolder
: TorrentContentLayout::Subfolder);
if (m_contentLayout != originalContentLayout)
{
if (m_contentLayout == TorrentContentLayout::NoSubfolder)
Path::stripRootFolder(filePaths);
else
Path::addRootFolder(filePaths, filePaths.at(0).removedExtension());
}
}
const auto &renamedFiles = m_ltAddTorrentParams.renamed_files; const auto nativeIndexes = metadata.nativeIndexes();
m_indexMap.reserve(filePaths.size()); m_indexMap.reserve(filePaths.size());
for (int i = 0; i < filePaths.size(); ++i) for (int i = 0; i < filePaths.size(); ++i)
{ {

1
src/base/bittorrent/torrentimpl.h

@ -51,6 +51,7 @@
#include "infohash.h" #include "infohash.h"
#include "speedmonitor.h" #include "speedmonitor.h"
#include "torrent.h" #include "torrent.h"
#include "torrentcontentlayout.h"
#include "torrentinfo.h" #include "torrentinfo.h"
namespace BitTorrent namespace BitTorrent

9
src/base/bittorrent/torrentinfo.cpp

@ -41,6 +41,7 @@
#include <QVector> #include <QVector>
#include "base/global.h" #include "base/global.h"
#include "base/path.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/io.h" #include "base/utils/io.h"
#include "base/utils/misc.h" #include "base/utils/misc.h"
@ -408,14 +409,6 @@ int TorrentInfo::fileIndex(const Path &filePath) const
return -1; return -1;
} }
TorrentContentLayout TorrentInfo::contentLayout() const
{
if (!isValid())
return TorrentContentLayout::Original;
return detectContentLayout(filePaths());
}
std::shared_ptr<lt::torrent_info> TorrentInfo::nativeInfo() const std::shared_ptr<lt::torrent_info> TorrentInfo::nativeInfo() const
{ {
if (!isValid()) if (!isValid())

2
src/base/bittorrent/torrentinfo.h

@ -36,7 +36,6 @@
#include "base/3rdparty/expected.hpp" #include "base/3rdparty/expected.hpp"
#include "base/indexrange.h" #include "base/indexrange.h"
#include "base/pathfwd.h" #include "base/pathfwd.h"
#include "torrentcontentlayout.h"
class QByteArray; class QByteArray;
class QDateTime; class QDateTime;
@ -99,7 +98,6 @@ namespace BitTorrent
private: private:
// returns file index or -1 if fileName is not found // returns file index or -1 if fileName is not found
int fileIndex(const Path &filePath) const; int fileIndex(const Path &filePath) const;
TorrentContentLayout contentLayout() const;
std::shared_ptr<const lt::torrent_info> m_nativeInfo; std::shared_ptr<const lt::torrent_info> m_nativeInfo;

5
src/base/path.cpp

@ -181,6 +181,11 @@ void Path::removeExtension()
m_pathStr.chop(extension().size()); m_pathStr.chop(extension().size());
} }
Path Path::removedExtension() const
{
return createUnchecked(m_pathStr.chopped(extension().size()));
}
void Path::removeExtension(const QString &ext) void Path::removeExtension(const QString &ext)
{ {
if (hasExtension(ext)) if (hasExtension(ext))

1
src/base/path.h

@ -60,6 +60,7 @@ public:
QString extension() const; QString extension() const;
bool hasExtension(const QString &ext) const; bool hasExtension(const QString &ext) const;
void removeExtension(); void removeExtension();
Path removedExtension() const;
void removeExtension(const QString &ext); void removeExtension(const QString &ext);
bool hasAncestor(const Path &other) const; bool hasAncestor(const Path &other) const;

53
src/gui/addnewtorrentdialog.cpp

@ -256,6 +256,38 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
m_ui->categoryComboBox->setFocus(); m_ui->categoryComboBox->setFocus();
} }
void AddNewTorrentDialog::applyContentLayout()
{
Q_ASSERT(hasMetadata());
Q_ASSERT(!m_torrentParams.filePaths.isEmpty());
const auto originalContentLayout = (m_originalRootFolder.isEmpty()
? BitTorrent::TorrentContentLayout::NoSubfolder
: BitTorrent::TorrentContentLayout::Subfolder);
const int currentIndex = m_ui->contentLayoutComboBox->currentIndex();
const auto contentLayout = ((currentIndex == 0)
? originalContentLayout
: static_cast<BitTorrent::TorrentContentLayout>(currentIndex));
if (contentLayout != m_currentContentLayout)
{
PathList &filePaths = m_torrentParams.filePaths;
if (contentLayout == BitTorrent::TorrentContentLayout::NoSubfolder)
{
Path::stripRootFolder(filePaths);
}
else
{
const auto rootFolder = ((originalContentLayout == BitTorrent::TorrentContentLayout::Subfolder)
? m_originalRootFolder
: filePaths.at(0).removedExtension());
Path::addRootFolder(filePaths, rootFolder);
}
m_currentContentLayout = contentLayout;
}
}
AddNewTorrentDialog::~AddNewTorrentDialog() AddNewTorrentDialog::~AddNewTorrentDialog()
{ {
saveState(); saveState();
@ -538,7 +570,7 @@ void AddNewTorrentDialog::categoryChanged(int index)
} }
} }
void AddNewTorrentDialog::contentLayoutChanged(const int index) void AddNewTorrentDialog::contentLayoutChanged()
{ {
if (!hasMetadata()) if (!hasMetadata())
return; return;
@ -546,11 +578,8 @@ void AddNewTorrentDialog::contentLayoutChanged(const int index)
const auto filePriorities = m_contentModel->model()->getFilePriorities(); const auto filePriorities = m_contentModel->model()->getFilePriorities();
m_contentModel->model()->clear(); m_contentModel->model()->clear();
Q_ASSERT(!m_torrentParams.filePaths.isEmpty()); applyContentLayout();
const auto contentLayout = ((index == 0)
? BitTorrent::detectContentLayout(m_torrentInfo.filePaths())
: static_cast<BitTorrent::TorrentContentLayout>(index));
BitTorrent::applyContentLayout(m_torrentParams.filePaths, contentLayout, Path::findRootFolder(m_torrentInfo.filePaths()));
m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths)); m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths));
m_contentModel->model()->updateFilesPriorities(filePriorities); m_contentModel->model()->updateFilesPriorities(filePriorities);
@ -905,12 +934,16 @@ void AddNewTorrentDialog::setupTreeview()
connect(m_ui->buttonSelectAll, &QPushButton::clicked, m_contentModel, &TorrentContentFilterModel::selectAll); connect(m_ui->buttonSelectAll, &QPushButton::clicked, m_contentModel, &TorrentContentFilterModel::selectAll);
connect(m_ui->buttonSelectNone, &QPushButton::clicked, m_contentModel, &TorrentContentFilterModel::selectNone); connect(m_ui->buttonSelectNone, &QPushButton::clicked, m_contentModel, &TorrentContentFilterModel::selectNone);
const auto contentLayout = ((m_ui->contentLayoutComboBox->currentIndex() == 0)
? BitTorrent::detectContentLayout(m_torrentInfo.filePaths())
: static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex()));
if (m_torrentParams.filePaths.isEmpty()) if (m_torrentParams.filePaths.isEmpty())
m_torrentParams.filePaths = m_torrentInfo.filePaths(); m_torrentParams.filePaths = m_torrentInfo.filePaths();
BitTorrent::applyContentLayout(m_torrentParams.filePaths, contentLayout, Path::findRootFolder(m_torrentInfo.filePaths()));
m_originalRootFolder = Path::findRootFolder(m_torrentInfo.filePaths());
m_currentContentLayout = (m_originalRootFolder.isEmpty()
? BitTorrent::TorrentContentLayout::NoSubfolder
: BitTorrent::TorrentContentLayout::Subfolder);
applyContentLayout();
// List files in torrent // List files in torrent
m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths)); m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths));
if (const QByteArray state = m_storeTreeHeaderState; !state.isEmpty()) if (const QByteArray state = m_storeTreeHeaderState; !state.isEmpty())

7
src/gui/addnewtorrentdialog.h

@ -35,6 +35,7 @@
#include "base/bittorrent/addtorrentparams.h" #include "base/bittorrent/addtorrentparams.h"
#include "base/bittorrent/magneturi.h" #include "base/bittorrent/magneturi.h"
#include "base/bittorrent/torrentinfo.h" #include "base/bittorrent/torrentinfo.h"
#include "base/path.h"
#include "base/settingvalue.h" #include "base/settingvalue.h"
namespace BitTorrent namespace BitTorrent
@ -88,7 +89,7 @@ private slots:
void handleDownloadFinished(const Net::DownloadResult &downloadResult); void handleDownloadFinished(const Net::DownloadResult &downloadResult);
void TMMChanged(int index); void TMMChanged(int index);
void categoryChanged(int index); void categoryChanged(int index);
void contentLayoutChanged(int index); void contentLayoutChanged();
void doNotDeleteTorrentClicked(bool checked); void doNotDeleteTorrentClicked(bool checked);
void renameSelectedFile(); void renameSelectedFile();
@ -97,6 +98,8 @@ private slots:
private: private:
explicit AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent); explicit AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent);
void applyContentLayout();
bool loadTorrentFile(const QString &source); bool loadTorrentFile(const QString &source);
bool loadTorrentImpl(); bool loadTorrentImpl();
bool loadMagnet(const BitTorrent::MagnetUri &magnetUri); bool loadMagnet(const BitTorrent::MagnetUri &magnetUri);
@ -115,6 +118,8 @@ private:
PropListDelegate *m_contentDelegate = nullptr; PropListDelegate *m_contentDelegate = nullptr;
BitTorrent::MagnetUri m_magnetURI; BitTorrent::MagnetUri m_magnetURI;
BitTorrent::TorrentInfo m_torrentInfo; BitTorrent::TorrentInfo m_torrentInfo;
Path m_originalRootFolder;
BitTorrent::TorrentContentLayout m_currentContentLayout;
int m_savePathIndex = -1; int m_savePathIndex = -1;
int m_downloadPathIndex = -1; int m_downloadPathIndex = -1;
bool m_useDownloadPath = false; bool m_useDownloadPath = false;

Loading…
Cancel
Save