mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-19 04:49:47 -07:00
Move renameSelectedFile(BitTorrent::TorrentInfo &)
This commit is contained in:
parent
377f31085c
commit
e2f3dad7b8
4 changed files with 112 additions and 105 deletions
|
@ -154,8 +154,10 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
|
||||||
// Signal / slots
|
// Signal / slots
|
||||||
connect(m_ui->doNotDeleteTorrentCheckBox, &QCheckBox::clicked, this, &AddNewTorrentDialog::doNotDeleteTorrentClicked);
|
connect(m_ui->doNotDeleteTorrentCheckBox, &QCheckBox::clicked, this, &AddNewTorrentDialog::doNotDeleteTorrentClicked);
|
||||||
QShortcut *editHotkey = new QShortcut(Qt::Key_F2, m_ui->contentTreeView, nullptr, nullptr, Qt::WidgetShortcut);
|
QShortcut *editHotkey = new QShortcut(Qt::Key_F2, m_ui->contentTreeView, nullptr, nullptr, Qt::WidgetShortcut);
|
||||||
connect(editHotkey, &QShortcut::activated, this, &AddNewTorrentDialog::renameSelectedFile);
|
connect(editHotkey, &QShortcut::activated
|
||||||
connect(m_ui->contentTreeView, &QAbstractItemView::doubleClicked, this, &AddNewTorrentDialog::renameSelectedFile);
|
, this, [this]() { m_ui->contentTreeView->renameSelectedFile(m_torrentInfo); });
|
||||||
|
connect(m_ui->contentTreeView, &QAbstractItemView::doubleClicked
|
||||||
|
, this, [this]() { m_ui->contentTreeView->renameSelectedFile(m_torrentInfo); });
|
||||||
|
|
||||||
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
|
||||||
}
|
}
|
||||||
|
@ -450,107 +452,6 @@ void AddNewTorrentDialog::setSavePath(const QString &newPath)
|
||||||
onSavePathChanged(newPath);
|
onSavePathChanged(newPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddNewTorrentDialog::renameSelectedFile()
|
|
||||||
{
|
|
||||||
const QModelIndexList selectedIndexes = m_ui->contentTreeView->selectionModel()->selectedRows(0);
|
|
||||||
if (selectedIndexes.size() != 1) return;
|
|
||||||
|
|
||||||
const QModelIndex modelIndex = selectedIndexes.first();
|
|
||||||
if (!modelIndex.isValid()) return;
|
|
||||||
|
|
||||||
// Ask for new name
|
|
||||||
bool ok = false;
|
|
||||||
const bool isFile = (m_contentModel->itemType(modelIndex) == TorrentContentModelItem::FileType);
|
|
||||||
QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal
|
|
||||||
, modelIndex.data().toString(), &ok, isFile).trimmed();
|
|
||||||
if (!ok) return;
|
|
||||||
|
|
||||||
if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) {
|
|
||||||
RaisedMessageBox::warning(this, tr("Rename error"),
|
|
||||||
tr("The name is empty or contains forbidden characters, please choose a different one."),
|
|
||||||
QMessageBox::Ok);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFile) {
|
|
||||||
const int fileIndex = m_contentModel->getFileIndex(modelIndex);
|
|
||||||
|
|
||||||
if (newName.endsWith(QB_EXT))
|
|
||||||
newName.chop(QB_EXT.size());
|
|
||||||
const QString oldFileName = m_torrentInfo.fileName(fileIndex);
|
|
||||||
const QString oldFilePath = m_torrentInfo.filePath(fileIndex);
|
|
||||||
const QString newFilePath = oldFilePath.leftRef(oldFilePath.size() - oldFileName.size()) + newName;
|
|
||||||
|
|
||||||
if (oldFileName == newName) {
|
|
||||||
qDebug("Name did not change: %s", qUtf8Printable(oldFileName));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if that name is already used
|
|
||||||
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
|
|
||||||
if (i == fileIndex) continue;
|
|
||||||
if (Utils::Fs::sameFileNames(m_torrentInfo.filePath(i), newFilePath)) {
|
|
||||||
RaisedMessageBox::warning(this, tr("Rename error"),
|
|
||||||
tr("This name is already in use in this folder. Please use a different name."),
|
|
||||||
QMessageBox::Ok);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
qDebug("Renaming %s to %s", qUtf8Printable(oldFilePath), qUtf8Printable(newFilePath));
|
|
||||||
m_torrentInfo.renameFile(fileIndex, newFilePath);
|
|
||||||
|
|
||||||
m_contentModel->setData(modelIndex, newName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// renaming a folder
|
|
||||||
QStringList pathItems;
|
|
||||||
pathItems << modelIndex.data().toString();
|
|
||||||
QModelIndex parent = m_contentModel->parent(modelIndex);
|
|
||||||
while (parent.isValid()) {
|
|
||||||
pathItems.prepend(parent.data().toString());
|
|
||||||
parent = m_contentModel->parent(parent);
|
|
||||||
}
|
|
||||||
const QString oldPath = pathItems.join('/');
|
|
||||||
pathItems.removeLast();
|
|
||||||
pathItems << newName;
|
|
||||||
QString newPath = pathItems.join('/');
|
|
||||||
if (Utils::Fs::sameFileNames(oldPath, newPath)) {
|
|
||||||
qDebug("Name did not change");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!newPath.endsWith('/')) newPath += '/';
|
|
||||||
// Check for overwriting
|
|
||||||
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
|
|
||||||
const QString currentName = m_torrentInfo.filePath(i);
|
|
||||||
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS)
|
|
||||||
if (currentName.startsWith(newPath, Qt::CaseSensitive)) {
|
|
||||||
#else
|
|
||||||
if (currentName.startsWith(newPath, Qt::CaseInsensitive)) {
|
|
||||||
#endif
|
|
||||||
RaisedMessageBox::warning(this, tr("The folder could not be renamed"),
|
|
||||||
tr("This name is already in use in this folder. Please use a different name."),
|
|
||||||
QMessageBox::Ok);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Replace path in all files
|
|
||||||
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
|
|
||||||
const QString currentName = m_torrentInfo.filePath(i);
|
|
||||||
if (currentName.startsWith(oldPath)) {
|
|
||||||
QString newName = currentName;
|
|
||||||
newName.replace(0, oldPath.length(), newPath);
|
|
||||||
newName = Utils::Fs::expandPath(newName);
|
|
||||||
qDebug("Rename %s to %s", qUtf8Printable(currentName), qUtf8Printable(newName));
|
|
||||||
m_torrentInfo.renameFile(i, newName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rename folder in torrent files model too
|
|
||||||
m_contentModel->setData(modelIndex, newName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddNewTorrentDialog::populateSavePathComboBox()
|
void AddNewTorrentDialog::populateSavePathComboBox()
|
||||||
{
|
{
|
||||||
m_ui->savePath->clear();
|
m_ui->savePath->clear();
|
||||||
|
@ -590,7 +491,7 @@ void AddNewTorrentDialog::displayContentTreeMenu(const QPoint &)
|
||||||
QAction *act = myFilesLlistMenu.exec(QCursor::pos());
|
QAction *act = myFilesLlistMenu.exec(QCursor::pos());
|
||||||
if (act) {
|
if (act) {
|
||||||
if (act == actRename) {
|
if (act == actRename) {
|
||||||
renameSelectedFile();
|
m_ui->contentTreeView->renameSelectedFile(m_torrentInfo);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal;
|
BitTorrent::FilePriority prio = BitTorrent::FilePriority::Normal;
|
||||||
|
|
|
@ -75,7 +75,6 @@ private slots:
|
||||||
void displayContentTreeMenu(const QPoint &);
|
void displayContentTreeMenu(const QPoint &);
|
||||||
void updateDiskSpaceLabel();
|
void updateDiskSpaceLabel();
|
||||||
void onSavePathChanged(const QString &newPath);
|
void onSavePathChanged(const QString &newPath);
|
||||||
void renameSelectedFile();
|
|
||||||
void updateMetadata(const BitTorrent::TorrentInfo &info);
|
void updateMetadata(const BitTorrent::TorrentInfo &info);
|
||||||
void handleDownloadFailed(const QString &url, const QString &reason);
|
void handleDownloadFailed(const QString &url, const QString &reason);
|
||||||
void handleRedirectedToMagnet(const QString &url, const QString &magnetUri);
|
void handleRedirectedToMagnet(const QString &url, const QString &magnetUri);
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
|
|
||||||
#include "base/bittorrent/session.h"
|
#include "base/bittorrent/session.h"
|
||||||
#include "base/bittorrent/torrenthandle.h"
|
#include "base/bittorrent/torrenthandle.h"
|
||||||
|
#include "base/bittorrent/torrentinfo.h"
|
||||||
#include "base/global.h"
|
#include "base/global.h"
|
||||||
#include "base/utils/fs.h"
|
#include "base/utils/fs.h"
|
||||||
#include "autoexpandabledialog.h"
|
#include "autoexpandabledialog.h"
|
||||||
|
@ -209,6 +210,110 @@ void TorrentContentTreeView::renameSelectedFile(BitTorrent::TorrentHandle *torre
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TorrentContentTreeView::renameSelectedFile(BitTorrent::TorrentInfo &torrent)
|
||||||
|
{
|
||||||
|
const QModelIndexList selectedIndexes = selectionModel()->selectedRows(0);
|
||||||
|
if (selectedIndexes.size() != 1) return;
|
||||||
|
|
||||||
|
const QModelIndex modelIndex = selectedIndexes.first();
|
||||||
|
if (!modelIndex.isValid()) return;
|
||||||
|
|
||||||
|
auto model = dynamic_cast<TorrentContentFilterModel *>(TorrentContentTreeView::model());
|
||||||
|
if (!model) return;
|
||||||
|
|
||||||
|
// Ask for new name
|
||||||
|
bool ok = false;
|
||||||
|
const bool isFile = (model->itemType(modelIndex) == TorrentContentModelItem::FileType);
|
||||||
|
QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal
|
||||||
|
, modelIndex.data().toString(), &ok, isFile).trimmed();
|
||||||
|
if (!ok) return;
|
||||||
|
|
||||||
|
if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) {
|
||||||
|
RaisedMessageBox::warning(this, tr("Rename error"),
|
||||||
|
tr("The name is empty or contains forbidden characters, please choose a different one."),
|
||||||
|
QMessageBox::Ok);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFile) {
|
||||||
|
const int fileIndex = model->getFileIndex(modelIndex);
|
||||||
|
|
||||||
|
if (newName.endsWith(QB_EXT))
|
||||||
|
newName.chop(QB_EXT.size());
|
||||||
|
const QString oldFileName = torrent.fileName(fileIndex);
|
||||||
|
const QString oldFilePath = torrent.filePath(fileIndex);
|
||||||
|
const QString newFilePath = oldFilePath.leftRef(oldFilePath.size() - oldFileName.size()) + newName;
|
||||||
|
|
||||||
|
if (oldFileName == newName) {
|
||||||
|
qDebug("Name did not change: %s", qUtf8Printable(oldFileName));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if that name is already used
|
||||||
|
for (int i = 0; i < torrent.filesCount(); ++i) {
|
||||||
|
if (i == fileIndex) continue;
|
||||||
|
if (Utils::Fs::sameFileNames(torrent.filePath(i), newFilePath)) {
|
||||||
|
RaisedMessageBox::warning(this, tr("Rename error"),
|
||||||
|
tr("This name is already in use in this folder. Please use a different name."),
|
||||||
|
QMessageBox::Ok);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug("Renaming %s to %s", qUtf8Printable(oldFilePath), qUtf8Printable(newFilePath));
|
||||||
|
torrent.renameFile(fileIndex, newFilePath);
|
||||||
|
|
||||||
|
model->setData(modelIndex, newName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// renaming a folder
|
||||||
|
QStringList pathItems;
|
||||||
|
pathItems << modelIndex.data().toString();
|
||||||
|
QModelIndex parent = model->parent(modelIndex);
|
||||||
|
while (parent.isValid()) {
|
||||||
|
pathItems.prepend(parent.data().toString());
|
||||||
|
parent = model->parent(parent);
|
||||||
|
}
|
||||||
|
const QString oldPath = pathItems.join('/');
|
||||||
|
pathItems.removeLast();
|
||||||
|
pathItems << newName;
|
||||||
|
QString newPath = pathItems.join('/');
|
||||||
|
if (Utils::Fs::sameFileNames(oldPath, newPath)) {
|
||||||
|
qDebug("Name did not change");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!newPath.endsWith('/')) newPath += '/';
|
||||||
|
// Check for overwriting
|
||||||
|
for (int i = 0; i < torrent.filesCount(); ++i) {
|
||||||
|
const QString currentName = torrent.filePath(i);
|
||||||
|
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS)
|
||||||
|
if (currentName.startsWith(newPath, Qt::CaseSensitive)) {
|
||||||
|
#else
|
||||||
|
if (currentName.startsWith(newPath, Qt::CaseInsensitive)) {
|
||||||
|
#endif
|
||||||
|
RaisedMessageBox::warning(this, tr("The folder could not be renamed"),
|
||||||
|
tr("This name is already in use in this folder. Please use a different name."),
|
||||||
|
QMessageBox::Ok);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Replace path in all files
|
||||||
|
for (int i = 0; i < torrent.filesCount(); ++i) {
|
||||||
|
const QString currentName = torrent.filePath(i);
|
||||||
|
if (currentName.startsWith(oldPath)) {
|
||||||
|
QString newName = currentName;
|
||||||
|
newName.replace(0, oldPath.length(), newPath);
|
||||||
|
newName = Utils::Fs::expandPath(newName);
|
||||||
|
qDebug("Rename %s to %s", qUtf8Printable(currentName), qUtf8Printable(newName));
|
||||||
|
torrent.renameFile(i, newName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rename folder in torrent files model too
|
||||||
|
model->setData(modelIndex, newName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex TorrentContentTreeView::currentNameCell()
|
QModelIndex TorrentContentTreeView::currentNameCell()
|
||||||
{
|
{
|
||||||
QModelIndex current = currentIndex();
|
QModelIndex current = currentIndex();
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
namespace BitTorrent
|
namespace BitTorrent
|
||||||
{
|
{
|
||||||
class TorrentHandle;
|
class TorrentHandle;
|
||||||
|
class TorrentInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
class TorrentContentTreeView : public QTreeView
|
class TorrentContentTreeView : public QTreeView
|
||||||
|
@ -45,6 +46,7 @@ public:
|
||||||
void keyPressEvent(QKeyEvent *event) override;
|
void keyPressEvent(QKeyEvent *event) override;
|
||||||
|
|
||||||
void renameSelectedFile(BitTorrent::TorrentHandle *torrent);
|
void renameSelectedFile(BitTorrent::TorrentHandle *torrent);
|
||||||
|
void renameSelectedFile(BitTorrent::TorrentInfo &torrent);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QModelIndex currentNameCell();
|
QModelIndex currentNameCell();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue