Browse Source

Refactors

Rename variables
Refactor to return early instead of introducing a scope
Revise dialog title & messages
Refactor code logic
adaptive-webui-19844
Chocobo1 8 years ago
parent
commit
08a6c75227
  1. 166
      src/gui/addnewtorrentdialog.cpp
  2. 129
      src/gui/properties/propertieswidget.cpp

166
src/gui/addnewtorrentdialog.cpp

@ -471,101 +471,101 @@ void AddNewTorrentDialog::browseButton_clicked()
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);
} }
} }

129
src/gui/properties/propertieswidget.cpp

@ -671,91 +671,84 @@ 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 = false; bool ok = false;
QString new_name_last = AutoExpandableDialog::getText(this, tr("Renaming"), 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)
return; if (newName.isEmpty() || !Utils::Fs::isValidFileSystemName(newName)) {
if (new_name_last.isEmpty() || !Utils::Fs::isValidFileSystemName(new_name_last)) {
MessageBoxRaised::warning(this, tr("Rename error"), MessageBoxRaised::warning(this, tr("Rename error"),
tr("The name is empty or 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; return;
} }
if (PropListModel->itemType(index) == TorrentContentModelItem::FileType) { if (PropListModel->itemType(modelIndex) == TorrentContentModelItem::FileType) {
// File renaming // renaming a file
const int file_index = PropListModel->getFileIndex(index); const int fileIndex = PropListModel->getFileIndex(modelIndex);
if (!m_torrent || !m_torrent->hasMetadata()) return;
QString old_name = m_torrent->filePath(file_index); if (newName.endsWith(QB_EXT))
if (old_name.endsWith(".!qB") && !new_name_last.endsWith(".!qB")) newName.chop(QB_EXT.size());
new_name_last += ".!qB"; const QString oldFileName = m_torrent->fileName(fileIndex);
QStringList path_items = old_name.split("/"); const QString oldFilePath = m_torrent->filePath(fileIndex);
path_items.removeLast(); const QString newFileName = newName + (BitTorrent::Session::instance()->isAppendExtensionEnabled() ? QB_EXT : QString());
path_items << new_name_last; const QString newFilePath = oldFilePath.leftRef(oldFilePath.size() - oldFileName.size()) + newFileName;
QString new_name = path_items.join("/");
if (old_name == new_name) { if (oldFileName == newFileName) {
qDebug("Name did not change"); qDebug("Name did not change: %s", qPrintable(oldFileName));
return; 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) { for (int i = 0; i < m_torrent->filesCount(); ++i) {
if (i == file_index) continue; if (i == fileIndex) continue;
if (Utils::Fs::sameFileNames(m_torrent->filePath(i), new_name)) { if (Utils::Fs::sameFileNames(m_torrent->filePath(i), newFilePath)) {
// Display error message MessageBoxRaised::warning(this, tr("Rename error"),
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."), tr("This name is already in use in this folder. Please use a different name."),
QMessageBox::Ok); QMessageBox::Ok);
return; return;
} }
} }
const bool force_recheck = QFile::exists(m_torrent->savePath(true) + "/" + new_name);
qDebug("Renaming %s to %s", qPrintable(old_name), qPrintable(new_name)); qDebug("Renaming %s to %s", qPrintable(oldFilePath), qPrintable(newFilePath));
m_torrent->renameFile(file_index, new_name); m_torrent->renameFile(fileIndex, newFilePath);
// Force recheck
if (force_recheck) m_torrent->forceRecheck(); PropListModel->setData(modelIndex, newName);
// Rename if torrent files model too
if (new_name_last.endsWith(".!qB"))
new_name_last.chop(4);
PropListModel->setData(index, new_name_last);
} }
else { else {
// Folder renaming // renaming a folder
QStringList path_items; QStringList pathItems;
path_items << index.data().toString(); pathItems << modelIndex.data().toString();
QModelIndex parent = PropListModel->parent(index); QModelIndex parent = PropListModel->parent(modelIndex);
while (parent.isValid()) { while (parent.isValid()) {
path_items.prepend(parent.data().toString()); pathItems.prepend(parent.data().toString());
parent = PropListModel->parent(parent); parent = PropListModel->parent(parent);
} }
const QString old_path = path_items.join("/"); const QString oldPath = pathItems.join("/");
path_items.removeLast(); pathItems.removeLast();
path_items << new_name_last; pathItems << newName;
QString new_path = path_items.join("/"); QString newPath = pathItems.join("/");
if (Utils::Fs::sameFileNames(old_path, new_path)) { if (Utils::Fs::sameFileNames(oldPath, newPath)) {
qDebug("Name did not change"); qDebug("Name did not change");
return; return;
} }
if (!new_path.endsWith("/")) new_path += "/"; if (!newPath.endsWith("/")) newPath += "/";
// Check for overwriting // Check for overwriting
for (int i = 0; i < m_torrent->filesCount(); ++i) { 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 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."),
@ -763,28 +756,28 @@ void PropertiesWidget::renameSelectedFile()
return; return;
} }
} }
bool force_recheck = false; bool forceRecheck = false;
// Replace path in all files // Replace path in all files
for (int i = 0; i < m_torrent->filesCount(); ++i) { 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 (current_name.startsWith(old_path)) { if (currentName.startsWith(oldPath)) {
QString new_name = current_name; QString newName = currentName;
new_name.replace(0, old_path.length(), new_path); newName.replace(0, oldPath.length(), newPath);
if (!force_recheck && QDir(m_torrent->savePath(true)).exists(new_name)) if (!forceRecheck && QDir(m_torrent->savePath(true)).exists(newName))
force_recheck = true; forceRecheck = true;
new_name = Utils::Fs::expandPath(new_name); newName = Utils::Fs::expandPath(newName);
qDebug("Rename %s to %s", qPrintable(current_name), qPrintable(new_name)); qDebug("Rename %s to %s", qPrintable(currentName), qPrintable(newName));
m_torrent->renameFile(i, new_name); m_torrent->renameFile(i, newName);
} }
} }
// Force recheck // Force recheck
if (force_recheck) m_torrent->forceRecheck(); if (forceRecheck) m_torrent->forceRecheck();
// Rename folder in torrent files model too // Rename folder in torrent files model too
PropListModel->setData(index, new_name_last); PropListModel->setData(modelIndex, newName);
// Remove old folder // Remove old folder
const QDir old_folder(m_torrent->savePath(true) + "/" + old_path); const QDir oldFolder(m_torrent->savePath(true) + "/" + oldPath);
int timeout = 10; 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) // FIXME: We should not sleep here (freezes the UI for 1 second)
QThread::msleep(100); QThread::msleep(100);
--timeout; --timeout;

Loading…
Cancel
Save