From 878935a34f4c5d00af2a0617257e2c1e22b280d3 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Tue, 14 Dec 2021 21:36:46 +0100 Subject: [PATCH] [knewfilemenu] Replace setPopupFiles with setWorkingDirectory For all intents and purposes setPopupFiles is the current directory, i.e. new files are created relative to it. The name doesn't really reflect that. It being a list is also weird since we are only really interested in passing one file. Some parts of the implementation only look at the first element in the list --- autotests/knewfilemenutest.cpp | 7 ++----- src/filewidgets/CMakeLists.txt | 2 +- src/filewidgets/kdiroperator.cpp | 4 ++-- src/filewidgets/knewfilemenu.cpp | 30 ++++++++++++++++++++++++++++++ src/filewidgets/knewfilemenu.h | 30 ++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/autotests/knewfilemenutest.cpp b/autotests/knewfilemenutest.cpp index 7ce7b8f4c..6c7904625 100644 --- a/autotests/knewfilemenutest.cpp +++ b/autotests/knewfilemenutest.cpp @@ -192,9 +192,7 @@ private Q_SLOTS: menu.setModal(false); menu.setParentWidget(&parentWidget); menu.setSelectDirWhenAlreadyExist(true); - QList lst; - lst << QUrl::fromLocalFile(m_tmpDir.path()); - menu.setPopupFiles(lst); + menu.setWorkingDirectory(QUrl::fromLocalFile(m_tmpDir.path())); menu.checkUpToDate(); QAction *action = coll.action(QStringLiteral("the_action")); QVERIFY(action); @@ -291,8 +289,7 @@ private Q_SLOTS: { KActionCollection coll(this, QStringLiteral("foo")); KNewFileMenu menu(&coll, QStringLiteral("the_action"), this); - const QList lst{QUrl::fromLocalFile(m_tmpDir.path())}; - menu.setPopupFiles(lst); + menu.setWorkingDirectory(QUrl::fromLocalFile(m_tmpDir.path())); menu.checkUpToDate(); QAction *action = coll.action(QStringLiteral("the_action")); const auto list = action->menu()->actions(); diff --git a/src/filewidgets/CMakeLists.txt b/src/filewidgets/CMakeLists.txt index 4f908ab06..2a93a3d75 100644 --- a/src/filewidgets/CMakeLists.txt +++ b/src/filewidgets/CMakeLists.txt @@ -84,7 +84,7 @@ ecm_generate_export_header(KF5KIOFileWidgets GROUP_BASE_NAME KF VERSION ${KF_VERSION} DEPRECATED_BASE_VERSION 0 - DEPRECATION_VERSIONS 4.3 4.5 5.0 5.33 5.66 5.70 5.76 5.78 5.86 + DEPRECATION_VERSIONS 4.3 4.5 5.0 5.33 5.66 5.70 5.76 5.78 5.86 5.97 EXCLUDE_DEPRECATED_BEFORE_AND_AT ${EXCLUDE_DEPRECATED_BEFORE_AND_AT} ) # TODO: add support for EXCLUDE_DEPRECATED_BEFORE_AND_AT to all KIO libs diff --git a/src/filewidgets/kdiroperator.cpp b/src/filewidgets/kdiroperator.cpp index 150daec6c..f077dfc85 100644 --- a/src/filewidgets/kdiroperator.cpp +++ b/src/filewidgets/kdiroperator.cpp @@ -698,7 +698,7 @@ void KDirOperatorPrivate::slotToggleIgnoreCase() void KDirOperator::mkdir() { - d->m_newFileMenu->setPopupFiles(QList{url()}); + d->m_newFileMenu->setWorkingDirectory(url()); d->m_newFileMenu->createDirectory(); } @@ -1299,7 +1299,7 @@ void KDirOperator::activatedMenu(const KFileItem &item, const QPoint &pos) { updateSelectionDependentActions(); - d->m_newFileMenu->setPopupFiles(QList{item.url()}); + d->m_newFileMenu->setWorkingDirectory(item.url()); d->m_newFileMenu->checkUpToDate(); d->m_actionCollection->action(QStringLiteral("new"))->setEnabled(item.isDir()); diff --git a/src/filewidgets/knewfilemenu.cpp b/src/filewidgets/knewfilemenu.cpp index 727051c57..cae2fc512 100644 --- a/src/filewidgets/knewfilemenu.cpp +++ b/src/filewidgets/knewfilemenu.cpp @@ -378,6 +378,9 @@ public: /** * When the user pressed the right mouse button over an URL a popup menu * is displayed. The URL belonging to this popup menu is stored here. + * For all intents and purposes this is the current directory where the menu is + * opened. + * TODO KF6 make it a single QUrl. */ QList m_popupFiles; @@ -1482,16 +1485,19 @@ bool KNewFileMenu::isModal() const return d->m_modal; } +#if KIOFILEWIDGETS_BUILD_DEPRECATED_SINCE(5, 97) QList KNewFileMenu::popupFiles() const { return d->m_popupFiles; } +#endif void KNewFileMenu::setModal(bool modal) { d->m_modal = modal; } +#if KIOFILEWIDGETS_BUILD_DEPRECATED_SINCE(5, 97) void KNewFileMenu::setPopupFiles(const QList &files) { d->m_popupFiles = files; @@ -1509,6 +1515,7 @@ void KNewFileMenu::setPopupFiles(const QList &files) } } } +#endif void KNewFileMenu::setParentWidget(QWidget *parentWidget) { @@ -1571,4 +1578,27 @@ QStringList KNewFileMenu::supportedMimeTypes() const return d->m_supportedMimeTypes; } +void KNewFileMenu::setWorkingDirectory(const QUrl &directory) +{ + d->m_popupFiles = {directory}; + + if (directory.isEmpty()) { + d->m_newMenuGroup->setEnabled(false); + } else { + if (KProtocolManager::supportsWriting(directory)) { + d->m_newMenuGroup->setEnabled(true); + if (d->m_newDirAction) { + d->m_newDirAction->setEnabled(KProtocolManager::supportsMakeDir(directory)); // e.g. trash:/ + } + } else { + d->m_newMenuGroup->setEnabled(true); + } + } +} + +QUrl KNewFileMenu::workingDirectory() const +{ + return d->m_popupFiles.isEmpty() ? QUrl() : d->m_popupFiles.first(); +} + #include "moc_knewfilemenu.cpp" diff --git a/src/filewidgets/knewfilemenu.h b/src/filewidgets/knewfilemenu.h index b74a4c0bf..4a88283f7 100644 --- a/src/filewidgets/knewfilemenu.h +++ b/src/filewidgets/knewfilemenu.h @@ -77,10 +77,14 @@ public: */ bool isModal() const; +#if KIOFILEWIDGETS_ENABLE_DEPRECATED_SINCE(5, 97) /** * Returns the files that the popup is shown for + * @deprecated since 5.97, use KNewFileMenu::workingDirectory(). */ + KIOFILEWIDGETS_DEPRECATED_VERSION(5, 97, "Use KNewFileMenu::workingDirectory()") QList popupFiles() const; +#endif /** * Sets the modality of dialogs created by KNewFile. Set to false if you do not want to block @@ -94,17 +98,35 @@ public: */ void setParentWidget(QWidget *parentWidget); +#if KIOFILEWIDGETS_ENABLE_DEPRECATED_SINCE(5, 97) /** * Set the files the popup is shown for * Call this before showing up the menu + * @deprecated since 5.97, use KNewFileMenu::setWorkingDirectory(const QUrl &). */ + KIOFILEWIDGETS_DEPRECATED_VERSION(5, 97, "Use KNewFileMenu::setWorkingDirectory(const QUrl &)") void setPopupFiles(const QList &files); +#endif + + /** + * Set the working directory. + * Files will be created relative to this directory. + * @since 5.97. + */ + void setWorkingDirectory(const QUrl &directory); + + /** + * Returns the working directory. + * Files will be created relative to this directory. + * @since 5.97. + */ + QUrl workingDirectory() const; #if KIOFILEWIDGETS_ENABLE_DEPRECATED_SINCE(5, 0) - KIOFILEWIDGETS_DEPRECATED_VERSION(5, 0, "Use KNewFileMenu::setPopupFiles(const QList &)") + KIOFILEWIDGETS_DEPRECATED_VERSION(5, 0, "Use KNewFileMenu::setWorkingDirectory(const QUrl &)") void setPopupFiles(const QUrl &file) { - setPopupFiles(QList{file}); + setWorkingDirectory(file); } #endif @@ -147,7 +169,7 @@ public Q_SLOTS: * a popupmenu. This is useful to make sure that creating a directory with * a key shortcut (e.g. F10) triggers the exact same code as when using * the New menu. - * Requirements: call setPopupFiles first, and keep this KNewFileMenu instance + * Requirements: since 5.97 call setWorkingDirectory first (for older releases call setPopupFiles first), and keep this KNewFileMenu instance * alive (the mkdir is async). */ void createDirectory(); @@ -157,7 +179,7 @@ public Q_SLOTS: * a popupmenu. This is useful to make sure that creating a directory with * a key shortcut (e.g. Shift-F10) triggers the exact same code as when using * the New menu. - * Requirements: call setPopupFiles first, and keep this KNewFileMenu instance + * Requirements: since 5.97 call setWorkingDirectory first (for older releases call setPopupFiles first), and keep this KNewFileMenu instance * alive (the copy is async). * @since 5.53 */ -- GitLab