From 30c59779e3edb03d7e20baa8209afc61673b2a8e Mon Sep 17 00:00:00 2001 From: xust Date: Thu, 25 Apr 2024 17:12:54 +0800 Subject: [PATCH] feat: [342337/titlebar] show/copy network url if path is smb/cifs/ftp/sftp. Log: as title. Task: https://pms.uniontech.com/task-view-342337.html --- .../dfmplugin_titlebar_global.h | 1 + .../core/dfmplugin-titlebar/titlebar.h | 4 +- .../dfmplugin-titlebar/views/addressbar.cpp | 11 +++- .../dfmplugin-titlebar/views/crumbbar.cpp | 6 ++- .../events/smbbrowsereventreceiver.cpp | 52 ++++++++++++++++++- .../events/smbbrowsereventreceiver.h | 9 +++- .../dfmplugin-smbbrowser/smbbrowser.cpp | 3 ++ 7 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/plugins/filemanager/core/dfmplugin-titlebar/dfmplugin_titlebar_global.h b/src/plugins/filemanager/core/dfmplugin-titlebar/dfmplugin_titlebar_global.h index 7ff6cd01d1..14a3a56ce2 100644 --- a/src/plugins/filemanager/core/dfmplugin-titlebar/dfmplugin_titlebar_global.h +++ b/src/plugins/filemanager/core/dfmplugin-titlebar/dfmplugin_titlebar_global.h @@ -140,5 +140,6 @@ inline constexpr char kAcComputerCrumbBarListView[] { "crumb_list_view" }; DPTITLEBAR_END_NAMESPACE Q_DECLARE_METATYPE(QList *); +Q_DECLARE_METATYPE(QUrl *); #endif // DFMPLUGIN_TITLEBAR_GLOBAL_H diff --git a/src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.h b/src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.h index 4f38fa64fc..780d8bd46c 100644 --- a/src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.h +++ b/src/plugins/filemanager/core/dfmplugin-titlebar/titlebar.h @@ -16,7 +16,7 @@ class TitleBar : public dpf::Plugin Q_OBJECT Q_PLUGIN_METADATA(IID "org.deepin.plugin.filemanager" FILE "titlebar.json") - DPF_EVENT_NAMESPACE(DPTITLEBAR_NAMESPACE) + DPF_EVENT_NAMESPACE(DPTITLEBAR_NAMESPACE) // singnal events DPF_EVENT_REG_SIGNAL(signal_Search_Start) DPF_EVENT_REG_SIGNAL(signal_Search_Stop) @@ -37,6 +37,8 @@ class TitleBar : public dpf::Plugin // hook events DPF_EVENT_REG_HOOK(hook_Crumb_Seprate) + DPF_EVENT_REG_HOOK(hook_Show_Addr) + DPF_EVENT_REG_HOOK(hook_Copy_Addr) public: virtual void initialize() override; diff --git a/src/plugins/filemanager/core/dfmplugin-titlebar/views/addressbar.cpp b/src/plugins/filemanager/core/dfmplugin-titlebar/views/addressbar.cpp index 7c5b48c17b..afd5dca7c3 100644 --- a/src/plugins/filemanager/core/dfmplugin-titlebar/views/addressbar.cpp +++ b/src/plugins/filemanager/core/dfmplugin-titlebar/views/addressbar.cpp @@ -13,6 +13,8 @@ #include #include +#include + #include #ifdef DTKWIDGET_CLASS_DSizeMode # include @@ -583,8 +585,13 @@ bool AddressBar::completerViewVisible() void AddressBar::setCurrentUrl(const QUrl &url) { - QString text = dfmbase::FileUtils::isLocalFile(url) ? url.toLocalFile() : UrlRoute::urlToLocalPath(url.toString()); - this->setText(text); + QUrl u(url); + if (dpfHookSequence->run("dfmplugin_titlebar", "hook_Show_Addr", &u)) { + this->setText(u.toString()); + } else { + QString text = dfmbase::FileUtils::isLocalFile(url) ? url.toLocalFile() : UrlRoute::urlToLocalPath(url.toString()); + this->setText(text); + } } QUrl AddressBar::currentUrl() diff --git a/src/plugins/filemanager/core/dfmplugin-titlebar/views/crumbbar.cpp b/src/plugins/filemanager/core/dfmplugin-titlebar/views/crumbbar.cpp index 8474deab8f..88b6f74e87 100644 --- a/src/plugins/filemanager/core/dfmplugin-titlebar/views/crumbbar.cpp +++ b/src/plugins/filemanager/core/dfmplugin-titlebar/views/crumbbar.cpp @@ -396,7 +396,11 @@ void CrumbBar::onCustomContextMenu(const QPoint &point) } menu->addAction(copyIcon, QObject::tr("Copy path"), [this, url]() { - d->writeUrlToClipboard(url); + QUrl u(url); + if (dpfHookSequence->run("dfmplugin_titlebar", "hook_Copy_Addr", &u)) + d->writeUrlToClipboard(u); + else + d->writeUrlToClipboard(url); }); if (displayNewWindowAndTab) { diff --git a/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.cpp b/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.cpp index a4ec44fc98..057bc7a602 100644 --- a/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.cpp +++ b/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.cpp @@ -3,8 +3,10 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "smbbrowsereventreceiver.h" +#include #include #include +#include #include #include @@ -80,5 +82,53 @@ bool SmbBrowserEventReceiver::hookSetTabName(const QUrl &url, QString *tabName) return false; } +bool SmbBrowserEventReceiver::hookTitleBarAddrHandle(QUrl *url) +{ + Q_ASSERT(url); + QUrl in(*url), out; + if (getOriginalUri(in, &out)) { + *url = out; + return true; + } + return false; +} + +bool SmbBrowserEventReceiver::getOriginalUri(const QUrl &in, QUrl *out) +{ + QString path = in.path(); + + // is cifs + static const QRegularExpression kCifsPrefix { R"(^/media/[^/]*/smbmounts/smb-share:[^/]*)" }; + if (path.contains(kCifsPrefix)) { + QString host, share, port; + if (!DeviceUtils::parseSmbInfo(path, host, share, &port)) + return false; + + if (out) { + out->setScheme("smb"); + out->setHost(host); + if (!port.isEmpty()) + out->setPort(port.toInt()); + QString subPath = "/" + share; + subPath += path.remove(kCifsPrefix); + out->setPath(subPath); + return true; + } + } + + // is gvfs: since mtp/gphoto... scheme are not supported path lookup, only handle ftp/sftp/smb + // use GIO to obtain the original URI + if (path.contains(QRegularExpression(R"(((^/run/user/[0-9]*/gvfs)|(^/root/.gvfs))/(ftp|sftp|smb))"))) { + SyncFileInfo f(in); + QUrl u = f.urlOf(dfmbase::FileInfo::FileUrlInfoType::kOriginalUrl); + if (u.isValid() && out) { + *out = u; + return true; + } + } + + return false; +} + SmbBrowserEventReceiver::SmbBrowserEventReceiver(QObject *parent) - : QObject(parent) { } + : QObject(parent) {} diff --git a/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.h b/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.h index e95a5bbbe3..7d5c0825f5 100644 --- a/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.h +++ b/src/plugins/filemanager/dfmplugin-smbbrowser/events/smbbrowsereventreceiver.h @@ -21,11 +21,16 @@ class SmbBrowserEventReceiver : public QObject public Q_SLOTS: bool detailViewIcon(const QUrl &url, QString *iconName); - bool cancelDelete(quint64, const QList &urls, const QUrl&rootUrl); - bool cancelMoveToTrash(quint64, const QList &, const QUrl&rootUrl); + bool cancelDelete(quint64, const QList &urls, const QUrl &rootUrl); + bool cancelMoveToTrash(quint64, const QList &, const QUrl &rootUrl); bool hookSetTabName(const QUrl &url, QString *tabName); + bool hookTitleBarAddrHandle(QUrl *url); + +private: + bool getOriginalUri(const QUrl &in, QUrl *out); + private: explicit SmbBrowserEventReceiver(QObject *parent = nullptr); }; diff --git a/src/plugins/filemanager/dfmplugin-smbbrowser/smbbrowser.cpp b/src/plugins/filemanager/dfmplugin-smbbrowser/smbbrowser.cpp index d85cd17f34..e285b42bf9 100644 --- a/src/plugins/filemanager/dfmplugin-smbbrowser/smbbrowser.cpp +++ b/src/plugins/filemanager/dfmplugin-smbbrowser/smbbrowser.cpp @@ -22,6 +22,7 @@ #include Q_DECLARE_METATYPE(QString *) +Q_DECLARE_METATYPE(QUrl *) namespace dfmplugin_smbbrowser { DFM_LOG_REISGER_CATEGORY(DPSMBBROWSER_NAMESPACE) @@ -152,6 +153,8 @@ void SmbBrowser::followEvents() dpfHookSequence->follow("dfmplugin_workspace", "hook_ShortCut_CutFiles", SmbBrowserEventReceiver::instance(), &SmbBrowserEventReceiver::cancelMoveToTrash); dpfHookSequence->follow("dfmplugin_workspace", "hook_ShortCut_PreViewFiles", SmbBrowserEventReceiver::instance(), &SmbBrowserEventReceiver::cancelMoveToTrash); dpfHookSequence->follow("dfmplugin_workspace", "hook_Tab_SetTabName", SmbBrowserEventReceiver::instance(), &SmbBrowserEventReceiver::hookSetTabName); + dpfHookSequence->follow("dfmplugin_titlebar", "hook_Show_Addr", SmbBrowserEventReceiver::instance(), &SmbBrowserEventReceiver::hookTitleBarAddrHandle); + dpfHookSequence->follow("dfmplugin_titlebar", "hook_Copy_Addr", SmbBrowserEventReceiver::instance(), &SmbBrowserEventReceiver::hookTitleBarAddrHandle); } void SmbBrowser::updateNeighborToSidebar()