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 @@ -123,7 +123,6 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.cpp
bittorrent/statistics.cpp
bittorrent/torrent.cpp
bittorrent/torrentcontentlayout.cpp
bittorrent/torrentcreatorthread.cpp
bittorrent/torrentimpl.cpp
bittorrent/torrentinfo.cpp

1
src/base/base.pri

@ -123,7 +123,6 @@ SOURCES += \ @@ -123,7 +123,6 @@ SOURCES += \
$$PWD/bittorrent/speedmonitor.cpp \
$$PWD/bittorrent/statistics.cpp \
$$PWD/bittorrent/torrent.cpp \
$$PWD/bittorrent/torrentcontentlayout.cpp \
$$PWD/bittorrent/torrentcreatorthread.cpp \
$$PWD/bittorrent/torrentimpl.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 @@ -2255,10 +2255,25 @@ bool Session::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source
Q_ASSERT(addTorrentParams.filePaths.isEmpty() || (addTorrentParams.filePaths.size() == torrentInfo.filesCount()));
const TorrentContentLayout contentLayout = ((loadTorrentParams.contentLayout == TorrentContentLayout::Original)
? detectContentLayout(torrentInfo.filePaths()) : loadTorrentParams.contentLayout);
PathList filePaths = (!addTorrentParams.filePaths.isEmpty() ? addTorrentParams.filePaths : torrentInfo.filePaths());
applyContentLayout(filePaths, contentLayout, Path::findRootFolder(torrentInfo.filePaths()));
PathList filePaths = addTorrentParams.filePaths;
if (filePaths.isEmpty())
{
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
// initial renaming of torrent content and rename torrent accordingly

69
src/base/bittorrent/torrentcontentlayout.cpp

@ -1,69 +0,0 @@ @@ -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 @@ -51,7 +51,4 @@ namespace BitTorrent
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) @@ -1769,11 +1769,24 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p)
TorrentInfo metadata = TorrentInfo(*m_nativeHandle.torrent_file());
const auto nativeIndexes = metadata.nativeIndexes();
const auto &renamedFiles = m_ltAddTorrentParams.renamed_files;
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());
for (int i = 0; i < filePaths.size(); ++i)
{

1
src/base/bittorrent/torrentimpl.h

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

9
src/base/bittorrent/torrentinfo.cpp

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

2
src/base/bittorrent/torrentinfo.h

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

5
src/base/path.cpp

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

1
src/base/path.h

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

53
src/gui/addnewtorrentdialog.cpp

@ -256,6 +256,38 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP @@ -256,6 +256,38 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
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()
{
saveState();
@ -538,7 +570,7 @@ void AddNewTorrentDialog::categoryChanged(int index) @@ -538,7 +570,7 @@ void AddNewTorrentDialog::categoryChanged(int index)
}
}
void AddNewTorrentDialog::contentLayoutChanged(const int index)
void AddNewTorrentDialog::contentLayoutChanged()
{
if (!hasMetadata())
return;
@ -546,11 +578,8 @@ void AddNewTorrentDialog::contentLayoutChanged(const int index) @@ -546,11 +578,8 @@ void AddNewTorrentDialog::contentLayoutChanged(const int index)
const auto filePriorities = m_contentModel->model()->getFilePriorities();
m_contentModel->model()->clear();
Q_ASSERT(!m_torrentParams.filePaths.isEmpty());
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()));
applyContentLayout();
m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths));
m_contentModel->model()->updateFilesPriorities(filePriorities);
@ -905,12 +934,16 @@ void AddNewTorrentDialog::setupTreeview() @@ -905,12 +934,16 @@ void AddNewTorrentDialog::setupTreeview()
connect(m_ui->buttonSelectAll, &QPushButton::clicked, m_contentModel, &TorrentContentFilterModel::selectAll);
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())
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
m_contentModel->model()->setupModelData(FileStorageAdaptor(m_torrentInfo, m_torrentParams.filePaths));
if (const QByteArray state = m_storeTreeHeaderState; !state.isEmpty())

7
src/gui/addnewtorrentdialog.h

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

Loading…
Cancel
Save