From 40f01f30a799613652a780b34fb7b29c613bda8d Mon Sep 17 00:00:00 2001 From: tokun-mobica Date: Fri, 14 Jan 2022 09:37:35 +0100 Subject: [PATCH] Add folder rename action Signed-off-by: tokun-mobica --- src/gui/accountsettings.cpp | 64 +++++++++++++++++++++++++++++++++++++ src/gui/accountsettings.h | 1 + 2 files changed, 65 insertions(+) diff --git a/src/gui/accountsettings.cpp b/src/gui/accountsettings.cpp index 05e48d0c56967..4ae3d3433aa47 100644 --- a/src/gui/accountsettings.cpp +++ b/src/gui/accountsettings.cpp @@ -579,6 +579,9 @@ void AccountSettings::slotCustomContextMenuRequested(const QPoint &pos) ac = menu->addAction(folderPaused ? tr("Resume sync") : tr("Pause sync")); connect(ac, &QAction::triggered, this, &AccountSettings::slotEnableCurrentFolder); + ac = menu->addAction(tr("Rename local folder")); + connect(ac, &QAction::triggered, this, &AccountSettings::slotRenameFolder); + ac = menu->addAction(tr("Remove folder sync connection")); connect(ac, &QAction::triggered, this, &AccountSettings::slotRemoveCurrentFolder); @@ -671,6 +674,67 @@ void AccountSettings::slotAddFolder() folderWizard->open(); } +void AccountSettings::slotRenameFolder() +{ + auto dialog = new QDialog(nullptr, Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); + dialog->setSizeGripEnabled(true); + dialog->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); + dialog->setFixedHeight(100); + dialog->setMaximumWidth(this->width()); + dialog->setWindowTitle(tr("Rename local folder")); + dialog->setAttribute(Qt::WA_DeleteOnClose); + + auto vbox = new QVBoxLayout; + auto lineEdit = new QLineEdit; + lineEdit->setText(QDir(FolderMan::instance()->folder(selectedFolderAlias())->path()).dirName()); + vbox->addWidget(lineEdit); + auto hbox = new QHBoxLayout; + auto pushButtonConfirm = new QPushButton(tr("Rename")); + auto pushButtonCancel = new QPushButton(tr("Cancel")); + hbox->addWidget(pushButtonConfirm); + hbox->addWidget(pushButtonCancel); + vbox->addLayout(hbox); + + dialog->setLayout(vbox); + dialog->open(); + + connect(pushButtonConfirm, &QPushButton::clicked, dialog, &QDialog::accept); + connect(pushButtonCancel, &QPushButton::clicked, dialog, &QDialog::reject); + + connect(dialog, &QDialog::finished, this, [this, lineEdit](const int result) { + if (result == QDialog::Accepted) { + auto folder = FolderMan::instance()->folder(selectedFolderAlias()); + QDir currentDirectory(folder->path()); + QString folderPath = currentDirectory.path().chopped(currentDirectory.dirName().length()); + const auto newFolderPath = folderPath.append(lineEdit->text()); + + if (currentDirectory.path() != newFolderPath) { + auto folderMan = FolderMan::instance(); + folderMan->setSyncEnabled(false); // do not start more syncs. + + if (currentDirectory.rename(currentDirectory.path(), newFolderPath)) { + FolderDefinition definition; + definition.localPath = FolderDefinition::prepareLocalPath(newFolderPath); + definition.targetPath = FolderDefinition::prepareTargetPath(folder->remotePath()); + definition.ignoreHiddenFiles = folderMan->ignoreHiddenFiles(); + + Utility::removeFavLink(folder->path()); + FolderMan::instance()->removeFolder(folder); + Utility::setupFavLink(definition.localPath); + + if (auto f = folderMan->addFolder(_accountState, definition)) { + folderMan->scheduleFolder(f); + emit folderChanged(); + } + } else { + QMessageBox::critical(this, tr("Folder rename failed!"), + tr("Folder: %1 \n can't be renamed to: %2 !").arg(currentDirectory.dirName(), lineEdit->text()), QMessageBox::Ok); + } + folderMan->setSyncEnabled(true); + } + } + }); +} void AccountSettings::slotFolderWizardAccepted() { diff --git a/src/gui/accountsettings.h b/src/gui/accountsettings.h index a9908138f99fc..a9fb9c09cb867 100644 --- a/src/gui/accountsettings.h +++ b/src/gui/accountsettings.h @@ -77,6 +77,7 @@ public slots: protected slots: void slotAddFolder(); + void slotRenameFolder(); void slotEnableCurrentFolder(bool terminate = false); void slotScheduleCurrentFolder(); void slotScheduleCurrentFolderForceRemoteDiscovery();