qBittorrent
BitTorrent::AbstractFileStorage Class Referenceabstract

#include <abstractfilestorage.h>

Inheritance diagram for BitTorrent::AbstractFileStorage:

Public Member Functions

virtual ~AbstractFileStorage ()=default
 
virtual int filesCount () const =0
 
virtual QString filePath (int index) const =0
 
virtual qlonglong fileSize (int index) const =0
 
virtual void renameFile (int index, const QString &name)=0
 
void renameFile (const QString &oldPath, const QString &newPath)
 
void renameFolder (const QString &oldPath, const QString &newPath)
 

Detailed Description

Definition at line 38 of file abstractfilestorage.h.

Constructor & Destructor Documentation

◆ ~AbstractFileStorage()

virtual BitTorrent::AbstractFileStorage::~AbstractFileStorage ( )
virtualdefault

Member Function Documentation

◆ filePath()

virtual QString BitTorrent::AbstractFileStorage::filePath ( int  index) const
pure virtual

◆ filesCount()

◆ fileSize()

virtual qlonglong BitTorrent::AbstractFileStorage::fileSize ( int  index) const
pure virtual

◆ renameFile() [1/2]

void BitTorrent::AbstractFileStorage::renameFile ( const QString &  oldPath,
const QString &  newPath 
)

Definition at line 52 of file abstractfilestorage.cpp.

53 {
54  if (!Utils::Fs::isValidFileSystemName(oldPath, true))
55  throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
56  if (!Utils::Fs::isValidFileSystemName(newPath, true))
57  throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
58 
59  const QString oldFilePath = Utils::Fs::toUniformPath(oldPath);
60  if (oldFilePath.endsWith(QLatin1Char {'/'}))
61  throw RuntimeError {tr("Invalid file path: '%1'.").arg(oldFilePath)};
62 
63  const QString newFilePath = Utils::Fs::toUniformPath(newPath);
64  if (newFilePath.endsWith(QLatin1Char {'/'}))
65  throw RuntimeError {tr("Invalid file path: '%1'.").arg(newFilePath)};
66  if (QDir().isAbsolutePath(newFilePath))
67  throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFilePath)};
68 
69  int renamingFileIndex = -1;
70  for (int i = 0; i < filesCount(); ++i)
71  {
72  const QString path = filePath(i);
73 
74  if ((renamingFileIndex < 0) && areSameFileNames(path, oldFilePath))
75  renamingFileIndex = i;
76  else if (areSameFileNames(path, newFilePath))
77  throw RuntimeError {tr("The file already exists: '%1'.").arg(newFilePath)};
78  }
79 
80  if (renamingFileIndex < 0)
81  throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)};
82 
83  renameFile(renamingFileIndex, newFilePath);
84 }
virtual void renameFile(int index, const QString &name)=0
virtual int filesCount() const =0
virtual QString filePath(int index) const =0
bool isValidFileSystemName(const QString &name, bool allowSeparators=false)
Definition: fs.cpp:248
QString toUniformPath(const QString &path)
Definition: fs.cpp:69
bool areSameFileNames(QString first, QString second)

References anonymous_namespace{abstractfilestorage.cpp}::areSameFileNames(), filePath(), filesCount(), Utils::Fs::isValidFileSystemName(), renameFile(), and Utils::Fs::toUniformPath().

Here is the call graph for this function:

◆ renameFile() [2/2]

virtual void BitTorrent::AbstractFileStorage::renameFile ( int  index,
const QString &  name 
)
pure virtual

◆ renameFolder()

void BitTorrent::AbstractFileStorage::renameFolder ( const QString &  oldPath,
const QString &  newPath 
)

Definition at line 86 of file abstractfilestorage.cpp.

87 {
88  if (!Utils::Fs::isValidFileSystemName(oldPath, true))
89  throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
90  if (!Utils::Fs::isValidFileSystemName(newPath, true))
91  throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
92 
93  const auto cleanFolderPath = [](const QString &path) -> QString
94  {
95  const QString uniformPath = Utils::Fs::toUniformPath(path);
96  return (uniformPath.endsWith(QLatin1Char {'/'}) ? uniformPath : uniformPath + QLatin1Char {'/'});
97  };
98 
99  const QString oldFolderPath = cleanFolderPath(oldPath);
100  const QString newFolderPath = cleanFolderPath(newPath);
101  if (QDir().isAbsolutePath(newFolderPath))
102  throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFolderPath)};
103 
104  QVector<int> renamingFileIndexes;
105  renamingFileIndexes.reserve(filesCount());
106 
107  for (int i = 0; i < filesCount(); ++i)
108  {
109  const QString path = filePath(i);
110 
111  if (path.startsWith(oldFolderPath, CASE_SENSITIVITY))
112  renamingFileIndexes.append(i);
113  else if (path.startsWith(newFolderPath, CASE_SENSITIVITY))
114  throw RuntimeError {tr("The folder already exists: '%1'.").arg(newFolderPath)};
115  }
116 
117  if (renamingFileIndexes.isEmpty())
118  throw RuntimeError {tr("No such folder: '%1'.").arg(oldFolderPath)};
119 
120  for (const int index : renamingFileIndexes)
121  {
122  const QString newFilePath = newFolderPath + filePath(index).mid(oldFolderPath.size());
123  renameFile(index, newFilePath);
124  }
125 }
const Qt::CaseSensitivity CASE_SENSITIVITY

References CASE_SENSITIVITY, Utils::Fs::isValidFileSystemName(), and Utils::Fs::toUniformPath().

Referenced by TorrentsController::renameFolderAction(), and TorrentContentTreeView::renameSelectedFile().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: