From 0939875ca8f1bc36133dce72b3ac33ebf43c4c8e Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 30 Jan 2017 20:06:17 +0800 Subject: [PATCH 1/5] Fix renaming files is not case sensitive on Windows platform. Closes #5128. --- src/gui/properties/propertieswidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index 5df08d842..b1378fea1 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -700,7 +700,7 @@ void PropertiesWidget::renameSelectedFile() path_items.removeLast(); path_items << new_name_last; QString new_name = path_items.join("/"); - if (Utils::Fs::sameFileNames(old_name, new_name)) { + if (old_name == new_name) { qDebug("Name did not change"); return; } From 51e3e5438b6cd8b81a4d552554d7fa1d350689e6 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 30 Jan 2017 20:22:03 +0800 Subject: [PATCH 2/5] Code formatting Refactor --- src/gui/addnewtorrentdialog.h | 4 +- src/gui/properties/propertieswidget.cpp | 221 ++++++++++++------------ 2 files changed, 114 insertions(+), 111 deletions(-) diff --git a/src/gui/addnewtorrentdialog.h b/src/gui/addnewtorrentdialog.h index ad28474d8..8bd25eb0d 100644 --- a/src/gui/addnewtorrentdialog.h +++ b/src/gui/addnewtorrentdialog.h @@ -71,7 +71,7 @@ public: private slots: void showAdvancedSettings(bool show); - void displayContentTreeMenu(const QPoint&); + void displayContentTreeMenu(const QPoint &); void updateDiskSpaceLabel(); void onSavePathChanged(int); void renameSelectedFile(); @@ -93,7 +93,7 @@ private: bool loadMagnet(const BitTorrent::MagnetUri &magnetUri); void populateSavePathComboBox(); void saveSavePathHistory() const; - int indexOfSavePath(const QString& save_path); + int indexOfSavePath(const QString &save_path); void loadState(); void saveState(); void setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText = QString()); diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index b1378fea1..267582fe6 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -210,7 +210,7 @@ void PropertiesWidget::showPiecesDownloaded(bool show) void PropertiesWidget::setVisibility(bool visible) { - if (!visible && ( state == VISIBLE) ) { + if (!visible && (state == VISIBLE)) { QSplitter *hSplitter = static_cast(parentWidget()); m_ui->stackedProperties->setVisible(false); slideSizes = hSplitter->sizes(); @@ -222,7 +222,7 @@ void PropertiesWidget::setVisibility(bool visible) return; } - if (visible && ( state == REDUCED) ) { + if (visible && (state == REDUCED)) { m_ui->stackedProperties->setVisible(true); QSplitter *hSplitter = static_cast(parentWidget()); hSplitter->handle(1)->setDisabled(false); @@ -363,7 +363,7 @@ void PropertiesWidget::readSettings() void PropertiesWidget::saveSettings() { Preferences *const pref = Preferences::instance(); - pref->setPropVisible(state==VISIBLE); + pref->setPropVisible(state == VISIBLE); // Splitter sizes QSplitter *hSplitter = static_cast(parentWidget()); QList sizes; @@ -397,10 +397,10 @@ void PropertiesWidget::loadDynamicData() m_ui->wasted->setText(Utils::Misc::friendlyUnit(m_torrent->wastedSize())); m_ui->upTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload())) - .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadUpload()))); + .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadUpload()))); m_ui->dlTotal->setText(tr("%1 (%2 this session)").arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload())) - .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadDownload()))); + .arg(Utils::Misc::friendlyUnit(m_torrent->totalPayloadDownload()))); m_ui->lbl_uplimit->setText(m_torrent->uploadLimit() <= 0 ? QString::fromUtf8(C_INFINITY) : Utils::Misc::friendlyUnit(m_torrent->uploadLimit(), true)); @@ -416,8 +416,8 @@ void PropertiesWidget::loadDynamicData() m_ui->lbl_elapsed->setText(elapsed_txt); m_ui->lbl_connections->setText(tr("%1 (%2 max)", "%1 and %2 are numbers, e.g. 3 (10 max)") - .arg(m_torrent->connectionsCount()) - .arg(m_torrent->connectionsLimit() < 0 ? QString::fromUtf8(C_INFINITY) : QString::number(m_torrent->connectionsLimit()))); + .arg(m_torrent->connectionsCount()) + .arg(m_torrent->connectionsLimit() < 0 ? QString::fromUtf8(C_INFINITY) : QString::number(m_torrent->connectionsLimit()))); m_ui->label_eta_val->setText(Utils::Misc::userFriendlyDuration(m_torrent->eta())); @@ -429,20 +429,20 @@ void PropertiesWidget::loadDynamicData() m_ui->shareRatio->setText(ratio > BitTorrent::TorrentHandle::MAX_RATIO ? QString::fromUtf8(C_INFINITY) : Utils::String::fromDouble(ratio, 2)); m_ui->label_seeds_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)") - .arg(QString::number(m_torrent->seedsCount())) - .arg(QString::number(m_torrent->totalSeedsCount()))); + .arg(QString::number(m_torrent->seedsCount())) + .arg(QString::number(m_torrent->totalSeedsCount()))); m_ui->label_peers_val->setText(tr("%1 (%2 total)", "%1 and %2 are numbers, e.g. 3 (10 total)") - .arg(QString::number(m_torrent->leechsCount())) - .arg(QString::number(m_torrent->totalLeechersCount()))); + .arg(QString::number(m_torrent->leechsCount())) + .arg(QString::number(m_torrent->totalLeechersCount()))); m_ui->label_dl_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)") - .arg(Utils::Misc::friendlyUnit(m_torrent->downloadPayloadRate(), true)) - .arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload() / (1 + m_torrent->activeTime() - m_torrent->finishedTime()), true))); + .arg(Utils::Misc::friendlyUnit(m_torrent->downloadPayloadRate(), true)) + .arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload() / (1 + m_torrent->activeTime() - m_torrent->finishedTime()), true))); m_ui->label_upload_speed_val->setText(tr("%1 (%2 avg.)", "%1 and %2 are speed rates, e.g. 200KiB/s (100KiB/s avg.)") - .arg(Utils::Misc::friendlyUnit(m_torrent->uploadPayloadRate(), true)) - .arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload() / (1 + m_torrent->activeTime()), true))); + .arg(Utils::Misc::friendlyUnit(m_torrent->uploadPayloadRate(), true)) + .arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload() / (1 + m_torrent->activeTime()), true))); m_ui->label_last_complete_val->setText(m_torrent->lastSeenComplete().isValid() ? m_torrent->lastSeenComplete().toString(Qt::DefaultLocaleShortDate) : tr("Never")); @@ -674,118 +674,121 @@ void PropertiesWidget::renameSelectedFile() const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0); if (selectedIndexes.size() != 1) return; + const QModelIndex index = selectedIndexes.first(); if (!index.isValid()) return; + // Ask for new name - bool ok; + bool ok = false; QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), tr("New name:"), QLineEdit::Normal, index.data().toString(), &ok).trimmed(); - if (ok && !new_name_last.isEmpty()) { - if (!Utils::Fs::isValidFileSystemName(new_name_last)) { - MessageBoxRaised::warning(this, tr("The file could not be renamed"), - tr("This file name contains forbidden characters, please choose a different one."), - QMessageBox::Ok); + if (!ok || new_name_last.isEmpty()) + return; + + if (!Utils::Fs::isValidFileSystemName(new_name_last)) { + MessageBoxRaised::warning(this, tr("The file could not be renamed"), + tr("This file name contains forbidden characters, please choose a different one."), + QMessageBox::Ok); + return; + } + if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { + // File renaming + const int file_index = PropListModel->getFileIndex(index); + if (!m_torrent || !m_torrent->hasMetadata()) return; + QString old_name = m_torrent->filePath(file_index); + if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) + new_name_last += ".!qB"; + QStringList path_items = old_name.split("/"); + path_items.removeLast(); + path_items << new_name_last; + QString new_name = path_items.join("/"); + if (old_name == new_name) { + qDebug("Name did not change"); return; } - if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { - // File renaming - const int file_index = PropListModel->getFileIndex(index); - if (!m_torrent || !m_torrent->hasMetadata()) return; - QString old_name = m_torrent->filePath(file_index); - if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) - new_name_last += ".!qB"; - QStringList path_items = old_name.split("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_name = path_items.join("/"); - if (old_name == new_name) { - qDebug("Name did not change"); + new_name = Utils::Fs::expandPath(new_name); + qDebug("New name: %s", qPrintable(new_name)); + // Check if that name is already used + for (int i = 0; i < m_torrent->filesCount(); ++i) { + if (i == file_index) continue; + if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) { + // Display error message + MessageBoxRaised::warning(this, tr("The file could not be renamed"), + tr("This name is already in use in this folder. Please use a different name."), + QMessageBox::Ok); return; } - new_name = Utils::Fs::expandPath(new_name); - qDebug("New name: %s", qPrintable(new_name)); - // Check if that name is already used - for (int i = 0; i < m_torrent->filesCount(); ++i) { - if (i == file_index) continue; - if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) { - // Display error message - MessageBoxRaised::warning(this, tr("The file could not be renamed"), - tr("This name is already in use in this folder. Please use a different name."), - QMessageBox::Ok); - return; - } - } - const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name); - qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name)); - m_torrent->renameFile(file_index, new_name); - // Force recheck - if (force_recheck) m_torrent->forceRecheck(); - // Rename if torrent files model too - if (new_name_last.endsWith(".!qB")) - new_name_last.chop(4); - PropListModel->setData(index, new_name_last); } - else { - // Folder renaming - QStringList path_items; - path_items << index.data().toString(); - QModelIndex parent = PropListModel->parent(index); - while (parent.isValid()) { - path_items.prepend(parent.data().toString()); - parent = PropListModel->parent(parent); - } - const QString old_path = path_items.join("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_path = path_items.join("/"); - if (Utils::Fs::sameFileNames(old_path, new_path)) { - qDebug("Name did not change"); - return; - } - if (!new_path.endsWith("/")) new_path += "/"; - // Check for overwriting - for (int i = 0; i < m_torrent->filesCount(); ++i) { - const QString ¤t_name = m_torrent->filePath(i); + const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name); + qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name)); + m_torrent->renameFile(file_index, new_name); + // Force recheck + if (force_recheck) m_torrent->forceRecheck(); + // Rename if torrent files model too + if (new_name_last.endsWith(".!qB")) + new_name_last.chop(4); + PropListModel->setData(index, new_name_last); + } + else { + // Folder renaming + QStringList path_items; + path_items << index.data().toString(); + QModelIndex parent = PropListModel->parent(index); + while (parent.isValid()) { + path_items.prepend(parent.data().toString()); + parent = PropListModel->parent(parent); + } + const QString old_path = path_items.join("/"); + path_items.removeLast(); + path_items << new_name_last; + QString new_path = path_items.join("/"); + if (Utils::Fs::sameFileNames(old_path, new_path)) { + qDebug("Name did not change"); + return; + } + if (!new_path.endsWith("/")) new_path += "/"; + // Check for overwriting + for (int i = 0; i < m_torrent->filesCount(); ++i) { + const QString ¤t_name = m_torrent->filePath(i); #if defined(Q_OS_UNIX) || defined(Q_WS_QWS) - if (current_name.startsWith(new_path, Qt::CaseSensitive)) { + if (current_name.startsWith(new_path, Qt::CaseSensitive)) { #else - if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { + if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { #endif - QMessageBox::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; - } - } - bool force_recheck = false; - // Replace path in all files - for (int i = 0; i < m_torrent->filesCount(); ++i) { - const QString current_name = m_torrent->filePath(i); - if (current_name.startsWith(old_path)) { - QString new_name = current_name; - new_name.replace(0, old_path.length(), new_path); - if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name)) - force_recheck = true; - new_name = Utils::Fs::expandPath(new_name); - qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); - m_torrent->renameFile(i, new_name); - } + QMessageBox::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; } - // Force recheck - if (force_recheck) m_torrent->forceRecheck(); - // Rename folder in torrent files model too - PropListModel->setData(index, new_name_last); - // Remove old folder - const QDir old_folder(m_torrent->savePath(true) + "/" + old_path); - int timeout = 10; - while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) { - // FIXME: We should not sleep here (freezes the UI for 1 second) - QThread::msleep(100); - --timeout; + } + bool force_recheck = false; + // Replace path in all files + for (int i = 0; i < m_torrent->filesCount(); ++i) { + const QString current_name = m_torrent->filePath(i); + if (current_name.startsWith(old_path)) { + QString new_name = current_name; + new_name.replace(0, old_path.length(), new_path); + if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name)) + force_recheck = true; + new_name = Utils::Fs::expandPath(new_name); + qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); + m_torrent->renameFile(i, new_name); } } + // Force recheck + if (force_recheck) m_torrent->forceRecheck(); + // Rename folder in torrent files model too + PropListModel->setData(index, new_name_last); + // Remove old folder + const QDir old_folder(m_torrent->savePath(true) + "/" + old_path); + int timeout = 10; + while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) { + // FIXME: We should not sleep here (freezes the UI for 1 second) + QThread::msleep(100); + --timeout; + } } } From b18bf480148336e6fe908ee6716b77ae879bd20c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 30 Jan 2017 20:27:19 +0800 Subject: [PATCH 3/5] Improve error message reported to user Should not contain "file" or "folder", because it's not clear which type is selected. --- src/gui/properties/propertieswidget.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index 267582fe6..957e8d98f 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -681,18 +681,18 @@ void PropertiesWidget::renameSelectedFile() // Ask for new name bool ok = false; - QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), + QString new_name_last = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal, index.data().toString(), &ok).trimmed(); - if (!ok || new_name_last.isEmpty()) + if (!ok) return; - - if (!Utils::Fs::isValidFileSystemName(new_name_last)) { - MessageBoxRaised::warning(this, tr("The file could not be renamed"), - tr("This file name contains forbidden characters, please choose a different one."), + if (new_name_last.isEmpty() || !Utils::Fs::isValidFileSystemName(new_name_last)) { + MessageBoxRaised::warning(this, tr("Rename error"), + tr("The name is empty or contains forbidden characters, please choose a different one."), QMessageBox::Ok); return; } + if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { // File renaming const int file_index = PropListModel->getFileIndex(index); From b9403774e670066facaef47a59573afc2c96857e Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 7 Feb 2017 14:07:59 +0800 Subject: [PATCH 4/5] Use QBT_EXT in all places Cleanup headers on the way --- src/base/utils/fs.cpp | 12 ++++++------ src/gui/previewselect.cpp | 2 +- src/gui/torrentcontentmodelfile.cpp | 4 +++- src/gui/torrentcontentmodelfolder.cpp | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index 51cfc508d..d734e16a4 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -37,8 +37,9 @@ #include #include -#ifndef Q_OS_WIN -#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) +#if defined(Q_OS_WIN) +#include +#elif defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) #include #include #elif defined(Q_OS_HAIKU) @@ -46,9 +47,8 @@ #else #include #endif -#else -#include -#endif + +#include "base/bittorrent/torrenthandle.h" /** * Converts a path to a string suitable for display. @@ -70,7 +70,7 @@ QString Utils::Fs::fromNativePath(const QString &path) */ QString Utils::Fs::fileExtension(const QString &filename) { - QString ext = QString(filename).remove(".!qB"); + QString ext = QString(filename).remove(QB_EXT); const int point_index = ext.lastIndexOf("."); return (point_index >= 0) ? ext.mid(point_index + 1) : QString(); } diff --git a/src/gui/previewselect.cpp b/src/gui/previewselect.cpp index f1bcbb81e..573f0af85 100644 --- a/src/gui/previewselect.cpp +++ b/src/gui/previewselect.cpp @@ -69,7 +69,7 @@ PreviewSelect::PreviewSelect(QWidget* parent, BitTorrent::TorrentHandle *const t int nbFiles = torrent->filesCount(); for (int i = 0; i < nbFiles; ++i) { QString fileName = torrent->fileName(i); - if (fileName.endsWith(".!qB")) + if (fileName.endsWith(QB_EXT)) fileName.chop(4); QString extension = Utils::Fs::fileExtension(fileName).toUpper(); if (Utils::Misc::isPreviewable(extension)) { diff --git a/src/gui/torrentcontentmodelfile.cpp b/src/gui/torrentcontentmodelfile.cpp index df66cb9fc..62c925428 100644 --- a/src/gui/torrentcontentmodelfile.cpp +++ b/src/gui/torrentcontentmodelfile.cpp @@ -29,6 +29,8 @@ */ #include "torrentcontentmodelfile.h" + +#include "base/bittorrent/torrenthandle.h" #include "torrentcontentmodelfolder.h" TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize, @@ -41,7 +43,7 @@ TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulong m_name = fileName; // Do not display incomplete extensions - if (m_name.endsWith(".!qB")) + if (m_name.endsWith(QB_EXT)) m_name.chop(4); m_size = fileSize; diff --git a/src/gui/torrentcontentmodelfolder.cpp b/src/gui/torrentcontentmodelfolder.cpp index 09df1e671..b74f89ded 100644 --- a/src/gui/torrentcontentmodelfolder.cpp +++ b/src/gui/torrentcontentmodelfolder.cpp @@ -28,16 +28,17 @@ * Contact : chris@qbittorrent.org */ -#include #include "torrentcontentmodelfolder.h" +#include "base/bittorrent/torrenthandle.h" + TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent) : TorrentContentModelItem(parent) { Q_ASSERT(parent); m_name = name; // Do not display incomplete extensions - if (m_name.endsWith(".!qB")) + if (m_name.endsWith(QB_EXT)) m_name.chop(4); } From 08a6c75227c6fef3406b357c5a383a4b11095089 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 7 Feb 2017 13:17:41 +0800 Subject: [PATCH 5/5] Refactors Rename variables Refactor to return early instead of introducing a scope Revise dialog title & messages Refactor code logic --- src/gui/addnewtorrentdialog.cpp | 166 ++++++++++++------------ src/gui/properties/propertieswidget.cpp | 129 +++++++++--------- 2 files changed, 144 insertions(+), 151 deletions(-) diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index bbdb6901c..6fba49b2f 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -471,101 +471,101 @@ void AddNewTorrentDialog::browseButton_clicked() void AddNewTorrentDialog::renameSelectedFile() { const QModelIndexList selectedIndexes = ui->contentTreeView->selectionModel()->selectedRows(0); - if (selectedIndexes.size() != 1) - return; - const QModelIndex &index = selectedIndexes.first(); - if (!index.isValid()) - return; + if (selectedIndexes.size() != 1) return; + + const QModelIndex modelIndex = selectedIndexes.first(); + if (!modelIndex.isValid()) return; + // Ask for new name - bool ok; - const QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), - tr("New name:"), QLineEdit::Normal, - index.data().toString(), &ok).trimmed(); - if (ok && !new_name_last.isEmpty()) { - if (!Utils::Fs::isValidFileSystemName(new_name_last)) { - MessageBoxRaised::warning(this, tr("The file could not be renamed"), - tr("This file name contains forbidden characters, please choose a different one."), - QMessageBox::Ok); + bool ok = false; + QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal, modelIndex.data().toString(), &ok) + .trimmed(); + if (!ok) return; + + if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) { + MessageBoxRaised::warning(this, tr("Rename error"), + tr("The name is empty or contains forbidden characters, please choose a different one."), + QMessageBox::Ok); + return; + } + + if (m_contentModel->itemType(modelIndex) == TorrentContentModelItem::FileType) { + // renaming a file + 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", qPrintable(oldFileName)); return; } - if (m_contentModel->itemType(index) == TorrentContentModelItem::FileType) { - // File renaming - const int file_index = m_contentModel->getFileIndex(index); - QString old_name = Utils::Fs::fromNativePath(m_torrentInfo.filePath(file_index)); - qDebug("Old name: %s", qPrintable(old_name)); - QStringList path_items = old_name.split("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_name = path_items.join("/"); - if (Utils::Fs::sameFileNames(old_name, new_name)) { - qDebug("Name did not change"); + + // 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)) { + MessageBoxRaised::warning(this, tr("Rename error"), + tr("This name is already in use in this folder. Please use a different name."), + QMessageBox::Ok); return; } - new_name = Utils::Fs::expandPath(new_name); - qDebug("New name: %s", qPrintable(new_name)); - // Check if that name is already used - for (int i = 0; i < m_torrentInfo.filesCount(); ++i) { - if (i == file_index) continue; - if (Utils::Fs::sameFileNames(m_torrentInfo.filePath(i), new_name)) { - // Display error message - MessageBoxRaised::warning(this, tr("The file could not be renamed"), - tr("This name is already in use in this folder. Please use a different name."), - QMessageBox::Ok); - return; - } - } - qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name)); - m_torrentInfo.renameFile(file_index, new_name); - // Rename in torrent files model too - m_contentModel->setData(index, new_name_last); } - else { - // Folder renaming - QStringList path_items; - path_items << index.data().toString(); - QModelIndex parent = m_contentModel->parent(index); - while (parent.isValid()) { - path_items.prepend(parent.data().toString()); - parent = m_contentModel->parent(parent); - } - const QString old_path = path_items.join("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_path = path_items.join("/"); - if (Utils::Fs::sameFileNames(old_path, new_path)) { - qDebug("Name did not change"); - return; - } - if (!new_path.endsWith("/")) new_path += "/"; - // Check for overwriting - for (int i = 0; i < m_torrentInfo.filesCount(); ++i) { - const QString ¤t_name = m_torrentInfo.filePath(i); + + qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(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 ¤tName = m_torrentInfo.filePath(i); #if defined(Q_OS_UNIX) || defined(Q_WS_QWS) - if (current_name.startsWith(new_path, Qt::CaseSensitive)) { + if (currentName.startsWith(newPath, Qt::CaseSensitive)) { #else - if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { + if (currentName.startsWith(newPath, Qt::CaseInsensitive)) { #endif - MessageBoxRaised::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; - } + MessageBoxRaised::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 ¤t_name = m_torrentInfo.filePath(i); - if (current_name.startsWith(old_path)) { - QString new_name = current_name; - new_name.replace(0, old_path.length(), new_path); - new_name = Utils::Fs::expandPath(new_name); - qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); - m_torrentInfo.renameFile(i, new_name); - } + } + // Replace path in all files + for (int i = 0; i < m_torrentInfo.filesCount(); ++i) { + const QString ¤tName = 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", qPrintable(currentName), qPrintable(newName)); + m_torrentInfo.renameFile(i, newName); } - - // Rename folder in torrent files model too - m_contentModel->setData(index, new_name_last); } + + // Rename folder in torrent files model too + m_contentModel->setData(modelIndex, newName); } } diff --git a/src/gui/properties/propertieswidget.cpp b/src/gui/properties/propertieswidget.cpp index 957e8d98f..e248b03ce 100644 --- a/src/gui/properties/propertieswidget.cpp +++ b/src/gui/properties/propertieswidget.cpp @@ -671,91 +671,84 @@ void PropertiesWidget::displayWebSeedListMenu(const QPoint &) void PropertiesWidget::renameSelectedFile() { + if (!m_torrent) return; + const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0); - if (selectedIndexes.size() != 1) - return; + if (selectedIndexes.size() != 1) return; - const QModelIndex index = selectedIndexes.first(); - if (!index.isValid()) - return; + const QModelIndex modelIndex = selectedIndexes.first(); + if (!modelIndex.isValid()) return; // Ask for new name bool ok = false; - QString new_name_last = AutoExpandableDialog::getText(this, tr("Renaming"), - tr("New name:"), QLineEdit::Normal, - index.data().toString(), &ok).trimmed(); - if (!ok) - return; - if (new_name_last.isEmpty() || !Utils::Fs::isValidFileSystemName(new_name_last)) { + QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal, modelIndex.data().toString(), &ok) + .trimmed(); + if (!ok) return; + + if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) { MessageBoxRaised::warning(this, tr("Rename error"), tr("The name is empty or contains forbidden characters, please choose a different one."), QMessageBox::Ok); return; } - if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { - // File renaming - const int file_index = PropListModel->getFileIndex(index); - if (!m_torrent || !m_torrent->hasMetadata()) return; - QString old_name = m_torrent->filePath(file_index); - if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) - new_name_last += ".!qB"; - QStringList path_items = old_name.split("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_name = path_items.join("/"); - if (old_name == new_name) { - qDebug("Name did not change"); + if (PropListModel->itemType(modelIndex) == TorrentContentModelItem::FileType) { + // renaming a file + const int fileIndex = PropListModel->getFileIndex(modelIndex); + + if (newName.endsWith(QB_EXT)) + newName.chop(QB_EXT.size()); + const QString oldFileName = m_torrent->fileName(fileIndex); + const QString oldFilePath = m_torrent->filePath(fileIndex); + const QString newFileName = newName + (BitTorrent::Session::instance()->isAppendExtensionEnabled() ? QB_EXT : QString()); + const QString newFilePath = oldFilePath.leftRef(oldFilePath.size() - oldFileName.size()) + newFileName; + + if (oldFileName == newFileName) { + qDebug("Name did not change: %s", qPrintable(oldFileName)); return; } - new_name = Utils::Fs::expandPath(new_name); - qDebug("New name: %s", qPrintable(new_name)); - // Check if that name is already used + + // check if that name is already used for (int i = 0; i < m_torrent->filesCount(); ++i) { - if (i == file_index) continue; - if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) { - // Display error message - MessageBoxRaised::warning(this, tr("The file could not be renamed"), + if (i == fileIndex) continue; + if (Utils::Fs::sameFileNames(m_torrent->filePath(i), newFilePath)) { + MessageBoxRaised::warning(this, tr("Rename error"), tr("This name is already in use in this folder. Please use a different name."), QMessageBox::Ok); return; } } - const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name); - qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name)); - m_torrent->renameFile(file_index, new_name); - // Force recheck - if (force_recheck) m_torrent->forceRecheck(); - // Rename if torrent files model too - if (new_name_last.endsWith(".!qB")) - new_name_last.chop(4); - PropListModel->setData(index, new_name_last); + + qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(newFilePath)); + m_torrent->renameFile(fileIndex, newFilePath); + + PropListModel->setData(modelIndex, newName); } else { - // Folder renaming - QStringList path_items; - path_items << index.data().toString(); - QModelIndex parent = PropListModel->parent(index); + // renaming a folder + QStringList pathItems; + pathItems << modelIndex.data().toString(); + QModelIndex parent = PropListModel->parent(modelIndex); while (parent.isValid()) { - path_items.prepend(parent.data().toString()); + pathItems.prepend(parent.data().toString()); parent = PropListModel->parent(parent); } - const QString old_path = path_items.join("/"); - path_items.removeLast(); - path_items << new_name_last; - QString new_path = path_items.join("/"); - if (Utils::Fs::sameFileNames(old_path, new_path)) { + 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 (!new_path.endsWith("/")) new_path += "/"; + if (!newPath.endsWith("/")) newPath += "/"; // Check for overwriting for (int i = 0; i < m_torrent->filesCount(); ++i) { - const QString ¤t_name = m_torrent->filePath(i); + const QString ¤tName = m_torrent->filePath(i); #if defined(Q_OS_UNIX) || defined(Q_WS_QWS) - if (current_name.startsWith(new_path, Qt::CaseSensitive)) { + if (currentName.startsWith(newPath, Qt::CaseSensitive)) { #else - if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { + if (currentName.startsWith(newPath, Qt::CaseInsensitive)) { #endif QMessageBox::warning(this, tr("The folder could not be renamed"), tr("This name is already in use in this folder. Please use a different name."), @@ -763,28 +756,28 @@ void PropertiesWidget::renameSelectedFile() return; } } - bool force_recheck = false; + bool forceRecheck = false; // Replace path in all files for (int i = 0; i < m_torrent->filesCount(); ++i) { - const QString current_name = m_torrent->filePath(i); - if (current_name.startsWith(old_path)) { - QString new_name = current_name; - new_name.replace(0, old_path.length(), new_path); - if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name)) - force_recheck = true; - new_name = Utils::Fs::expandPath(new_name); - qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); - m_torrent->renameFile(i, new_name); + const QString currentName = m_torrent->filePath(i); + if (currentName.startsWith(oldPath)) { + QString newName = currentName; + newName.replace(0, oldPath.length(), newPath); + if (!forceRecheck && QDir(m_torrent->savePath(true)).exists(newName)) + forceRecheck = true; + newName = Utils::Fs::expandPath(newName); + qDebug("Rename %s to %s", qPrintable(currentName), qPrintable(newName)); + m_torrent->renameFile(i, newName); } } // Force recheck - if (force_recheck) m_torrent->forceRecheck(); + if (forceRecheck) m_torrent->forceRecheck(); // Rename folder in torrent files model too - PropListModel->setData(index, new_name_last); + PropListModel->setData(modelIndex, newName); // Remove old folder - const QDir old_folder(m_torrent->savePath(true) + "/" + old_path); + const QDir oldFolder(m_torrent->savePath(true) + "/" + oldPath); int timeout = 10; - while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) { + while (!QDir().rmpath(oldFolder.absolutePath()) && timeout > 0) { // FIXME: We should not sleep here (freezes the UI for 1 second) QThread::msleep(100); --timeout;