From 507d1ae6d8e8d1b1868879feae565b0b8f8688b9 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 29 Jul 2022 23:51:32 +0200 Subject: [PATCH] Allow renaming currently opened file in GUI --- CMakeLists.txt | 1 + gui/tageditorwidget.cpp | 62 ++++++++++++++++++++++++++++++++++++++++- gui/tageditorwidget.h | 1 + gui/tageditorwidget.ui | 44 +++++++++++++++++++++++------ 4 files changed, 99 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db22c6d..0c1aec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ set(REQUIRED_ICONS document-open edit-clear edit-copy + edit-rename emblem-error emblem-warning emblem-information diff --git a/gui/tageditorwidget.cpp b/gui/tageditorwidget.cpp index a426573..923d859 100644 --- a/gui/tageditorwidget.cpp +++ b/gui/tageditorwidget.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,7 @@ TagEditorWidget::TagEditorWidget(QWidget *parent) connect(m_ui->deleteTagsButton, &QPushButton::clicked, this, &TagEditorWidget::deleteAllTagsAndSave); connect(m_ui->nextButton, &QPushButton::clicked, this, &TagEditorWidget::saveAndShowNextFile); connect(m_ui->closeButton, &QPushButton::clicked, this, &TagEditorWidget::closeFile); + connect(m_ui->renamePushButton, &QPushButton::clicked, this, &TagEditorWidget::renameFile); // misc connect(m_ui->abortButton, &QPushButton::clicked, [this] { @@ -487,11 +489,12 @@ void TagEditorWidget::updateFileStatusStatus() const bool showDocumentTitle = opened && m_fileInfo.container() && m_fileInfo.container()->supportsTitle(); m_ui->docTitleLabel->setVisible(showDocumentTitle); m_ui->docTitleWidget->setVisible(showDocumentTitle); - // buttons and actions to save, delete, close + // buttons and actions to save, delete, close and rename m_ui->saveButton->setEnabled(opened); m_ui->nextButton->setEnabled(opened); m_ui->deleteTagsButton->setEnabled(hasTag); m_ui->buttonsWidget->setEnabled(opened); + m_ui->renamePushButton->setEnabled(opened); // clear and restore buttons m_ui->clearEntriesPushButton->setEnabled(hasTag); m_ui->restoreEntriesPushButton->setEnabled(hasTag); @@ -1377,6 +1380,63 @@ void TagEditorWidget::closeFile() updateFileStatusStatus(); } +void QtGui::TagEditorWidget::renameFile() +{ + if (isFileOperationOngoing()) { + emit statusMessage(tr("Unable to rename the file because the current process hasn't been finished yet.")); + return; + } + if (m_currentPath.isEmpty() || m_fileName.isEmpty() || !m_fileInfo.isOpen()) { + return; + } + static const auto windowTitle = tr("Renaming file - ") + QCoreApplication::applicationName(); + const auto newFileName = QInputDialog::getText(this, windowTitle, tr("New file name:"), QLineEdit::Normal, m_fileName); + if (newFileName.isEmpty()) { + return; + } + + auto errorMsg = QString(); + try { + // remove watcher, close file + m_fileWatcher->removePath(m_currentPath); + m_fileInfo.stream().close(); + + // rename file + auto oldPath = std::filesystem::path(makeNativePath(m_currentPath.toStdString())); + auto newPath = oldPath.parent_path(); + newPath.append(makeNativePath(newFileName.toStdString())); + std::filesystem::rename(oldPath, newPath); + + // open again with write access + m_fileInfo.reportPathChanged(newPath.u8string()); + try { + m_fileInfo.stream().open(m_fileInfo.path().data(), ios_base::in | ios_base::out | ios_base::binary); + } catch (const std::ios_base::failure &) { + // try to open read-only if opening with write access failed + m_fileInfo.stream().open(m_fileInfo.path().data(), ios_base::in | ios_base::binary); + } + m_currentPath = QString::fromStdString(m_fileInfo.path()); + m_fileName = QString::fromStdString(m_fileInfo.fileName()); + m_ui->fileNameLabel->setText(m_fileName); + emit currentPathChanged(m_currentPath); + + } catch (const std::runtime_error &e) { + auto msgBox = new QMessageBox(this); + msgBox->setIcon(QMessageBox::Critical); + msgBox->setAttribute(Qt::WA_DeleteOnClose, true); + msgBox->setWindowTitle(windowTitle); + msgBox->setText(errorMsg = tr("Unable to rename file: %1").arg(e.what())); + msgBox->setInformativeText(tr("Tried to rename file: ") + m_currentPath); + msgBox->show(); + } + + // add new current path to file watcher + m_fileWatcher->addPath(m_currentPath); + // update ui + emit statusMessage(errorMsg.isEmpty() ? tr("The file has been renamed.") : errorMsg); + updateFileStatusStatus(); +} + bool TagEditorWidget::handleFileInfoUnavailable() { if (isFileOperationOngoing()) { diff --git a/gui/tageditorwidget.h b/gui/tageditorwidget.h index ec59392..c3f3349 100644 --- a/gui/tageditorwidget.h +++ b/gui/tageditorwidget.h @@ -73,6 +73,7 @@ public Q_SLOTS: bool applyEntriesAndSaveChangings(); bool deleteAllTagsAndSave(); void closeFile(); + void renameFile(); void saveFileInfo(); void openFileInfoInBrowser(); // misc diff --git a/gui/tageditorwidget.ui b/gui/tageditorwidget.ui index 44535fc..af8fb7e 100644 --- a/gui/tageditorwidget.ui +++ b/gui/tageditorwidget.ui @@ -16,14 +16,42 @@ 4 - - - true - - - No file selected - - + + + + + true + + + No file selected + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Rename file + + + + .. + + + +