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. 166
      src/gui/addnewtorrentdialog.cpp
  3. 4
      src/gui/addnewtorrentdialog.h
  4. 2
      src/gui/previewselect.cpp
  5. 230
      src/gui/properties/propertieswidget.cpp
  6. 4
      src/gui/torrentcontentmodelfile.cpp
  7. 5
      src/gui/torrentcontentmodelfolder.cpp

12
src/base/utils/fs.cpp

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

166
src/gui/addnewtorrentdialog.cpp

@ -443,101 +443,101 @@ void AddNewTorrentDialog::setSavePath(const QString &newPath)
void AddNewTorrentDialog::renameSelectedFile() void AddNewTorrentDialog::renameSelectedFile()
{ {
const QModelIndexList selectedIndexes = ui->contentTreeView->selectionModel()->selectedRows(0); const QModelIndexList selectedIndexes = ui->contentTreeView->selectionModel()->selectedRows(0);
if (selectedIndexes.size() != 1) if (selectedIndexes.size() != 1) return;
return;
const QModelIndex &index = selectedIndexes.first(); const QModelIndex modelIndex = selectedIndexes.first();
if (!index.isValid()) if (!modelIndex.isValid()) return;
return;
// Ask for new name // Ask for new name
bool ok; bool ok = false;
const QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal, modelIndex.data().toString(), &ok)
tr("New name:"), QLineEdit::Normal, .trimmed();
index.data().toString(), &ok).trimmed(); if (!ok) return;
if (ok && !new_name_last.isEmpty()) {
if (!Utils::Fs::isValidFileSystemName(new_name_last)) { if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) {
MessageBoxRaised::warning(this, tr("The file could not be renamed"), MessageBoxRaised::warning(this, tr("Rename error"),
tr("This file name contains forbidden characters, please choose a different one."), tr("The name is empty or contains forbidden characters, please choose a different one."),
QMessageBox::Ok); 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; return;
} }
if (m_contentModel->itemType(index) == TorrentContentModelItem::FileType) {
// File renaming // check if that name is already used
const int file_index = m_contentModel->getFileIndex(index); for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
QString old_name = Utils::Fs::fromNativePath(m_torrentInfo.filePath(file_index)); if (i == fileIndex) continue;
qDebug("Old name: %s", qPrintable(old_name)); if (Utils::Fs::sameFileNames(m_torrentInfo.filePath(i), newFilePath)) {
QStringList path_items = old_name.split("/"); MessageBoxRaised::warning(this, tr("Rename error"),
path_items.removeLast(); tr("This name is already in use in this folder. Please use a different name."),
path_items << new_name_last; QMessageBox::Ok);
QString new_name = path_items.join("/");
if (Utils::Fs::sameFileNames(old_name, new_name)) {
qDebug("Name did not change");
return; 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 qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(newFilePath));
QStringList path_items; m_torrentInfo.renameFile(fileIndex, newFilePath);
path_items << index.data().toString();
QModelIndex parent = m_contentModel->parent(index); m_contentModel->setData(modelIndex, newName);
while (parent.isValid()) { }
path_items.prepend(parent.data().toString()); else {
parent = m_contentModel->parent(parent); // renaming a folder
} QStringList pathItems;
const QString old_path = path_items.join("/"); pathItems << modelIndex.data().toString();
path_items.removeLast(); QModelIndex parent = m_contentModel->parent(modelIndex);
path_items << new_name_last; while (parent.isValid()) {
QString new_path = path_items.join("/"); pathItems.prepend(parent.data().toString());
if (Utils::Fs::sameFileNames(old_path, new_path)) { parent = m_contentModel->parent(parent);
qDebug("Name did not change"); }
return; const QString oldPath = pathItems.join("/");
} pathItems.removeLast();
if (!new_path.endsWith("/")) new_path += "/"; pathItems << newName;
// Check for overwriting QString newPath = pathItems.join("/");
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) { if (Utils::Fs::sameFileNames(oldPath, newPath)) {
const QString &current_name = m_torrentInfo.filePath(i); 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 defined(Q_OS_UNIX) || defined(Q_WS_QWS)
if (current_name.startsWith(new_path, Qt::CaseSensitive)) { if (currentName.startsWith(newPath, Qt::CaseSensitive)) {
#else #else
if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { if (currentName.startsWith(newPath, Qt::CaseInsensitive)) {
#endif #endif
MessageBoxRaised::warning(this, tr("The folder could not be renamed"), 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."), tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok); QMessageBox::Ok);
return; return;
}
} }
// Replace path in all files }
for (int i = 0; i < m_torrentInfo.filesCount(); ++i) { // Replace path in all files
const QString &current_name = m_torrentInfo.filePath(i); for (int i = 0; i < m_torrentInfo.filesCount(); ++i) {
if (current_name.startsWith(old_path)) { const QString &currentName = m_torrentInfo.filePath(i);
QString new_name = current_name; if (currentName.startsWith(oldPath)) {
new_name.replace(0, old_path.length(), new_path); QString newName = currentName;
new_name = Utils::Fs::expandPath(new_name); newName.replace(0, oldPath.length(), newPath);
qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); newName = Utils::Fs::expandPath(newName);
m_torrentInfo.renameFile(i, new_name); 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);
} }
} }

4
src/gui/addnewtorrentdialog.h

@ -71,7 +71,7 @@ public:
private slots: private slots:
void showAdvancedSettings(bool show); void showAdvancedSettings(bool show);
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 renameSelectedFile();
@ -92,7 +92,7 @@ private:
bool loadMagnet(const BitTorrent::MagnetUri &magnetUri); bool loadMagnet(const BitTorrent::MagnetUri &magnetUri);
void populateSavePathComboBox(); void populateSavePathComboBox();
void saveSavePathHistory() const; void saveSavePathHistory() const;
int indexOfSavePath(const QString& save_path); int indexOfSavePath(const QString &save_path);
void loadState(); void loadState();
void saveState(); void saveState();
void setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText = QString()); void setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText = QString());

2
src/gui/previewselect.cpp

@ -69,7 +69,7 @@ PreviewSelect::PreviewSelect(QWidget* parent, BitTorrent::TorrentHandle *const t
int nbFiles = torrent->filesCount(); int nbFiles = torrent->filesCount();
for (int i = 0; i < nbFiles; ++i) { for (int i = 0; i < nbFiles; ++i) {
QString fileName = torrent->fileName(i); QString fileName = torrent->fileName(i);
if (fileName.endsWith(".!qB")) if (fileName.endsWith(QB_EXT))
fileName.chop(4); fileName.chop(4);
QString extension = Utils::Fs::fileExtension(fileName).toUpper(); QString extension = Utils::Fs::fileExtension(fileName).toUpper();
if (Utils::Misc::isPreviewable(extension)) { if (Utils::Misc::isPreviewable(extension)) {

230
src/gui/properties/propertieswidget.cpp

@ -210,7 +210,7 @@ void PropertiesWidget::showPiecesDownloaded(bool show)
void PropertiesWidget::setVisibility(bool visible) void PropertiesWidget::setVisibility(bool visible)
{ {
if (!visible && ( state == VISIBLE) ) { if (!visible && (state == VISIBLE)) {
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
m_ui->stackedProperties->setVisible(false); m_ui->stackedProperties->setVisible(false);
slideSizes = hSplitter->sizes(); slideSizes = hSplitter->sizes();
@ -222,7 +222,7 @@ void PropertiesWidget::setVisibility(bool visible)
return; return;
} }
if (visible && ( state == REDUCED) ) { if (visible && (state == REDUCED)) {
m_ui->stackedProperties->setVisible(true); m_ui->stackedProperties->setVisible(true);
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
hSplitter->handle(1)->setDisabled(false); hSplitter->handle(1)->setDisabled(false);
@ -363,7 +363,7 @@ void PropertiesWidget::readSettings()
void PropertiesWidget::saveSettings() void PropertiesWidget::saveSettings()
{ {
Preferences *const pref = Preferences::instance(); Preferences *const pref = Preferences::instance();
pref->setPropVisible(state==VISIBLE); pref->setPropVisible(state == VISIBLE);
// Splitter sizes // Splitter sizes
QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget()); QSplitter *hSplitter = static_cast<QSplitter *>(parentWidget());
QList<int> sizes; QList<int> sizes;
@ -397,10 +397,10 @@ void PropertiesWidget::loadDynamicData()
m_ui->wasted->setText(Utils::Misc::friendlyUnit(m_torrent->wastedSize())); 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())) 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())) 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)); 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_elapsed->setText(elapsed_txt);
m_ui->lbl_connections->setText(tr("%1 (%2 max)", "%1 and %2 are numbers, e.g. 3 (10 max)") 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->connectionsCount())
.arg(m_torrent->connectionsLimit() < 0 ? QString::fromUtf8(C_INFINITY) : QString::number(m_torrent->connectionsLimit()))); .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())); 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->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)") 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->seedsCount()))
.arg(QString::number(m_torrent->totalSeedsCount()))); .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)") 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->leechsCount()))
.arg(QString::number(m_torrent->totalLeechersCount()))); .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.)") 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->downloadPayloadRate(), true))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalDownload() / (1 + m_torrent->activeTime() - m_torrent->finishedTime()), 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.)") 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->uploadPayloadRate(), true))
.arg(Utils::Misc::friendlyUnit(m_torrent->totalUpload() / (1 + m_torrent->activeTime()), 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")); m_ui->label_last_complete_val->setText(m_torrent->lastSeenComplete().isValid() ? m_torrent->lastSeenComplete().toString(Qt::DefaultLocaleShortDate) : tr("Never"));
@ -671,121 +671,117 @@ void PropertiesWidget::displayWebSeedListMenu(const QPoint &)
void PropertiesWidget::renameSelectedFile() void PropertiesWidget::renameSelectedFile()
{ {
if (!m_torrent) return;
const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0); const QModelIndexList selectedIndexes = m_ui->filesList->selectionModel()->selectedRows(0);
if (selectedIndexes.size() != 1) if (selectedIndexes.size() != 1) return;
return;
const QModelIndex index = selectedIndexes.first(); const QModelIndex modelIndex = selectedIndexes.first();
if (!index.isValid()) if (!modelIndex.isValid()) return;
return;
// Ask for new name // Ask for new name
bool ok; bool ok = false;
QString new_name_last = AutoExpandableDialog::getText(this, tr("Rename the file"), QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal, modelIndex.data().toString(), &ok)
tr("New name:"), QLineEdit::Normal, .trimmed();
index.data().toString(), &ok).trimmed(); if (!ok) return;
if (ok && !new_name_last.isEmpty()) {
if (!Utils::Fs::isValidFileSystemName(new_name_last)) { if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) {
MessageBoxRaised::warning(this, tr("The file could not be renamed"), MessageBoxRaised::warning(this, tr("Rename error"),
tr("This file name contains forbidden characters, please choose a different one."), tr("The name is empty or contains forbidden characters, please choose a different one."),
QMessageBox::Ok); QMessageBox::Ok);
return;
}
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; return;
} }
if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) {
// File renaming // check if that name is already used
const int file_index = PropListModel->getFileIndex(index); for (int i = 0; i < m_torrent->filesCount(); ++i) {
if (!m_torrent || !m_torrent->hasMetadata()) return; if (i == fileIndex) continue;
QString old_name = m_torrent->filePath(file_index); if (Utils::Fs::sameFileNames(m_torrent->filePath(i), newFilePath)) {
if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) MessageBoxRaised::warning(this, tr("Rename error"),
new_name_last += ".!qB"; tr("This name is already in use in this folder. Please use a different name."),
QStringList path_items = old_name.split("/"); QMessageBox::Ok);
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");
return; 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 qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(newFilePath));
QStringList path_items; m_torrent->renameFile(fileIndex, newFilePath);
path_items << index.data().toString();
QModelIndex parent = PropListModel->parent(index); PropListModel->setData(modelIndex, newName);
while (parent.isValid()) { }
path_items.prepend(parent.data().toString()); else {
parent = PropListModel->parent(parent); // renaming a folder
} QStringList pathItems;
const QString old_path = path_items.join("/"); pathItems << modelIndex.data().toString();
path_items.removeLast(); QModelIndex parent = PropListModel->parent(modelIndex);
path_items << new_name_last; while (parent.isValid()) {
QString new_path = path_items.join("/"); pathItems.prepend(parent.data().toString());
if (Utils::Fs::sameFileNames(old_path, new_path)) { parent = PropListModel->parent(parent);
qDebug("Name did not change"); }
return; const QString oldPath = pathItems.join("/");
} pathItems.removeLast();
if (!new_path.endsWith("/")) new_path += "/"; pathItems << newName;
// Check for overwriting QString newPath = pathItems.join("/");
for (int i = 0; i < m_torrent->filesCount(); ++i) { if (Utils::Fs::sameFileNames(oldPath, newPath)) {
const QString &current_name = m_torrent->filePath(i); qDebug("Name did not change");
return;
}
if (!newPath.endsWith("/")) newPath += "/";
// Check for overwriting
for (int i = 0; i < m_torrent->filesCount(); ++i) {
const QString &currentName = m_torrent->filePath(i);
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS) #if defined(Q_OS_UNIX) || defined(Q_WS_QWS)
if (current_name.startsWith(new_path, Qt::CaseSensitive)) { if (currentName.startsWith(newPath, Qt::CaseSensitive)) {
#else #else
if (current_name.startsWith(new_path, Qt::CaseInsensitive)) { if (currentName.startsWith(newPath, Qt::CaseInsensitive)) {
#endif #endif
QMessageBox::warning(this, tr("The folder could not be renamed"), 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."), tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok); QMessageBox::Ok);
return; 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);
}
} }
// Force recheck }
if (force_recheck) m_torrent->forceRecheck(); bool forceRecheck = false;
// Rename folder in torrent files model too // Replace path in all files
PropListModel->setData(index, new_name_last); for (int i = 0; i < m_torrent->filesCount(); ++i) {
// Remove old folder const QString currentName = m_torrent->filePath(i);
const QDir old_folder(m_torrent->savePath(true) + "/" + old_path); if (currentName.startsWith(oldPath)) {
int timeout = 10; QString newName = currentName;
while (!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) { newName.replace(0, oldPath.length(), newPath);
// FIXME: We should not sleep here (freezes the UI for 1 second) if (!forceRecheck && QDir(m_torrent->savePath(true)).exists(newName))
QThread::msleep(100); forceRecheck = true;
--timeout; newName = Utils::Fs::expandPath(newName);
qDebug("Rename %s to %s", qPrintable(currentName), qPrintable(newName));
m_torrent->renameFile(i, newName);
} }
} }
// Force recheck
if (forceRecheck) m_torrent->forceRecheck();
// Rename folder in torrent files model too
PropListModel->setData(modelIndex, newName);
// Remove old folder
const QDir oldFolder(m_torrent->savePath(true) + "/" + oldPath);
int timeout = 10;
while (!QDir().rmpath(oldFolder.absolutePath()) && timeout > 0) {
// FIXME: We should not sleep here (freezes the UI for 1 second)
QThread::msleep(100);
--timeout;
}
} }
} }

4
src/gui/torrentcontentmodelfile.cpp

@ -29,6 +29,8 @@
*/ */
#include "torrentcontentmodelfile.h" #include "torrentcontentmodelfile.h"
#include "base/bittorrent/torrenthandle.h"
#include "torrentcontentmodelfolder.h" #include "torrentcontentmodelfolder.h"
TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize, TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize,
@ -41,7 +43,7 @@ TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulong
m_name = fileName; m_name = fileName;
// Do not display incomplete extensions // Do not display incomplete extensions
if (m_name.endsWith(".!qB")) if (m_name.endsWith(QB_EXT))
m_name.chop(4); m_name.chop(4);
m_size = fileSize; m_size = fileSize;

5
src/gui/torrentcontentmodelfolder.cpp

@ -28,16 +28,17 @@
* Contact : chris@qbittorrent.org * Contact : chris@qbittorrent.org
*/ */
#include <QDebug>
#include "torrentcontentmodelfolder.h" #include "torrentcontentmodelfolder.h"
#include "base/bittorrent/torrenthandle.h"
TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent) TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent)
: TorrentContentModelItem(parent) : TorrentContentModelItem(parent)
{ {
Q_ASSERT(parent); Q_ASSERT(parent);
m_name = name; m_name = name;
// Do not display incomplete extensions // Do not display incomplete extensions
if (m_name.endsWith(".!qB")) if (m_name.endsWith(QB_EXT))
m_name.chop(4); m_name.chop(4);
} }

Loading…
Cancel
Save