Browse Source

Merge pull request #6330 from Chocobo1/rename

Fix renaming files is not case sensitive on Windows platform
adaptive-webui-19844
Mike Tzou 8 years ago committed by GitHub
parent
commit
92decf83b8
  1. 12
      src/base/utils/fs.cpp
  2. 116
      src/gui/addnewtorrentdialog.cpp
  3. 2
      src/gui/previewselect.cpp
  4. 138
      src/gui/properties/propertieswidget.cpp
  5. 4
      src/gui/torrentcontentmodelfile.cpp
  6. 5
      src/gui/torrentcontentmodelfolder.cpp

12
src/base/utils/fs.cpp

@ -38,8 +38,9 @@ @@ -38,8 +38,9 @@
#include <QCoreApplication>
#include <QStorageInfo>
#ifndef Q_OS_WIN
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
#if defined(Q_OS_WIN)
#include <Windows.h>
#elif defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(Q_OS_HAIKU)
@ -47,9 +48,8 @@ @@ -47,9 +48,8 @@
#else
#include <sys/vfs.h>
#endif
#else
#include <Windows.h>
#endif
#include "base/bittorrent/torrenthandle.h"
/**
* Converts a path to a string suitable for display.
@ -71,7 +71,7 @@ QString Utils::Fs::fromNativePath(const QString &path) @@ -71,7 +71,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();
}

116
src/gui/addnewtorrentdialog.cpp

@ -443,79 +443,80 @@ void AddNewTorrentDialog::setSavePath(const QString &newPath) @@ -443,79 +443,80 @@ void AddNewTorrentDialog::setSavePath(const QString &newPath)
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."),
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(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");
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;
}
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_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"),
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;
}
}
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);
qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(newFilePath));
m_torrentInfo.renameFile(fileIndex, newFilePath);
m_contentModel->setData(modelIndex, newName);
}
else {
// Folder renaming
QStringList path_items;
path_items << index.data().toString();
QModelIndex parent = m_contentModel->parent(index);
// renaming a folder
QStringList pathItems;
pathItems << modelIndex.data().toString();
QModelIndex parent = m_contentModel->parent(modelIndex);
while (parent.isValid()) {
path_items.prepend(parent.data().toString());
pathItems.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)) {
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_torrentInfo.filesCount(); ++i) {
const QString &current_name = m_torrentInfo.filePath(i);
const QString &currentName = 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."),
@ -525,19 +526,18 @@ void AddNewTorrentDialog::renameSelectedFile() @@ -525,19 +526,18 @@ void AddNewTorrentDialog::renameSelectedFile()
}
// Replace path in all files
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
const QString &current_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);
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", qPrintable(currentName), qPrintable(newName));
m_torrentInfo.renameFile(i, newName);
}
}
// Rename folder in torrent files model too
m_contentModel->setData(index, new_name_last);
}
m_contentModel->setData(modelIndex, newName);
}
}

2
src/gui/previewselect.cpp

@ -69,7 +69,7 @@ PreviewSelect::PreviewSelect(QWidget* parent, BitTorrent::TorrentHandle *const t @@ -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)) {

138
src/gui/properties/propertieswidget.cpp

@ -671,87 +671,84 @@ void PropertiesWidget::displayWebSeedListMenu(const QPoint &) @@ -671,87 +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;
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;
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."),
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 (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 (Utils::Fs::sameFileNames(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 &current_name = m_torrent->filePath(i);
const QString &currentName = 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."),
@ -759,35 +756,34 @@ void PropertiesWidget::renameSelectedFile() @@ -759,35 +756,34 @@ 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;
}
}
}
}
void PropertiesWidget::openSelectedFile()
{

4
src/gui/torrentcontentmodelfile.cpp

@ -29,6 +29,8 @@ @@ -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 @@ -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;

5
src/gui/torrentcontentmodelfolder.cpp

@ -28,16 +28,17 @@ @@ -28,16 +28,17 @@
* Contact : chris@qbittorrent.org
*/
#include <QDebug>
#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);
}

Loading…
Cancel
Save