From 7fbae7cf34747580da056edccbc1376ed83994c8 Mon Sep 17 00:00:00 2001 From: gfdgd_xi <3025613752@qq.com> Date: Sun, 22 Sep 2024 07:35:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96.desktop=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=96=B9=E5=BC=8F;.desktop=E6=8F=92=E4=BB=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dde-file-manager-lib/dde-file-manager-lib.pro | 2 +- .../interfaces/dfilemenumanager.cpp | 49 +++++++++++++++---- .../interfaces/dfilemenumanager.h | 2 +- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/dde-file-manager-lib/dde-file-manager-lib.pro b/dde-file-manager-lib/dde-file-manager-lib.pro index c59d412..dce63dc 100644 --- a/dde-file-manager-lib/dde-file-manager-lib.pro +++ b/dde-file-manager-lib/dde-file-manager-lib.pro @@ -31,7 +31,7 @@ isEmpty(PREFIX){ } CONFIG += c++11 link_pkgconfig -PKGCONFIG += gsettings-qt libsecret-1 gio-unix-2.0 poppler-cpp dtkwidget udisks2-qt5 disomaster +PKGCONFIG += gsettings-qt libsecret-1 gio-unix-2.0 poppler-cpp dtkwidget udisks2-qt5 disomaster Qt5Xdg #DEFINES += QT_NO_DEBUG_OUTPUT DEFINES += QT_MESSAGELOGCONTEXT diff --git a/dde-file-manager-lib/interfaces/dfilemenumanager.cpp b/dde-file-manager-lib/interfaces/dfilemenumanager.cpp index 8ddc562..4986dbf 100644 --- a/dde-file-manager-lib/interfaces/dfilemenumanager.cpp +++ b/dde-file-manager-lib/interfaces/dfilemenumanager.cpp @@ -64,6 +64,8 @@ #include #include +#include + namespace DFileMenuData { static QMap actionKeys; @@ -464,13 +466,14 @@ QList DFileMenuManager::loadMenuExtensionActions(const DUrlList &urlL foreach (QFileInfo fileInfo, menuExtensionDir.entryInfoList(QDir::Files)) { if (fileInfo.fileName().endsWith(".desktop")) { qDebug() << fileInfo.absoluteFilePath(); - QFile file(fileInfo.absoluteFilePath()); + /*QFile file(fileInfo.absoluteFilePath()); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Couldn't open" << fileInfo.absoluteFilePath(); return actions; } - QByteArray data = file.readAll(); - actions << desktopToActions(data, urlList, currentUrl, menuType, onDesktop); + QByteArray data = file.readAll();*/ + //actions << desktopToActions(data, urlList, currentUrl, menuType, onDesktop); + actions << desktopToActions(fileInfo.absoluteFilePath(), urlList, currentUrl, menuType, onDesktop); } if (fileInfo.fileName().endsWith(".json")) { qDebug() << fileInfo.absoluteFilePath(); @@ -490,24 +493,52 @@ QList DFileMenuManager::loadMenuExtensionActions(const DUrlList &urlL return actions; } -QList DFileMenuManager::desktopToActions(const QString data, const DUrlList &urlList, const DUrl ¤tUrl, +QList DFileMenuManager::desktopToActions(const QString path, const DUrlList &urlList, const DUrl ¤tUrl, const QString &menuExtensionType, const bool onDesktop) { QList actions; + XdgDesktopFile desktopFile; + desktopFile.load(path); + if (!desktopFile.isValid()) { + // 不可读则直接退出 + return actions; + } QString nowShowIn = onDesktop ? QStringLiteral("Desktop") : QStringLiteral("FileManager"); QString menuType = "SingleFile"; QString suffix = ""; QString mimeType = ""; - QString icon = ""; + QIcon icon; + QString iconStr = ""; QString textKey = QString("Name[%1]=").arg(QLocale::system().name()); QString text = ""; QString exec = ""; QStringList args; QStringList notShowIn; QVariantList subMenuDataList; - for (QString i: data.split("\n")) { + menuType = desktopFile.value("X-DFM-MenuTypes").toString().split(";").at(0); + if (menuType == "") { + menuType = desktopFile.value("MimeType").toString(); + } + icon = desktopFile.icon(); + iconStr = desktopFile.iconName(); + text = desktopFile.name(); + exec = desktopFile.value("Exec").toString(); + QString argsText = exec; + args = argsText.replace(" ", " ").split(" "); + exec = args.at(0); + if (args.count() == 1) { + args.clear(); + } + else { + args = args.mid(1); + } + suffix = desktopFile.value("X-DDE-FileManager-SupportSuffix").toString(); + notShowIn << desktopFile.value("X-DDE-FileManager-NotShowIn").toString().split(";"); + + /*for (QString i: data.split("\n")) { if (i.startsWith("X-DFM-MenuTypes=")) { - menuType = i.replace("X-DFM-MenuTypes=", "").split(";").at(0); + desktopFile.value("X-DFM-MenuTypes=") + menuType = i.replace("X-DFM-MenuTypes=", "").split(";", QString::SkipEmptyParts).at(0); } else if (i.startsWith("MimeType=")) { mimeType = i.replace("MimeType=", ""); @@ -545,7 +576,7 @@ QList DFileMenuManager::desktopToActions(const QString data, const DU else if (i.startsWith("X-DDE-FileManager-NotShowIn=")) { notShowIn << i.replace("X-DDE-FileManager-NotShowIn=", "").split(";"); } - } + }*/ qDebug() << exec; bool canCreateAction = false; @@ -600,7 +631,7 @@ QList DFileMenuManager::desktopToActions(const QString data, const DU } if (canCreateAction) { - QAction *action = new QAction(QIcon(icon), text, nullptr); + QAction *action = new QAction(iconStr.isEmpty() ? QIcon() : icon, text, nullptr); if (subMenuDataList.count() > 1) { QJsonArray subActionsArray; diff --git a/dde-file-manager-lib/interfaces/dfilemenumanager.h b/dde-file-manager-lib/interfaces/dfilemenumanager.h index fd28dc6..d31f3c5 100644 --- a/dde-file-manager-lib/interfaces/dfilemenumanager.h +++ b/dde-file-manager-lib/interfaces/dfilemenumanager.h @@ -59,7 +59,7 @@ class DFileMenuManager : public QObject static QList loadEmptyAreaExtensionMenu(DFileMenu *menu, const DUrl ¤tUrl, bool onDesktop); static QList loadMenuExtensionActions(const DUrlList &urlList, const DUrl ¤tUrl, bool onDesktop = false); - static QList desktopToActions(const QString data, const DUrlList &urlList, const DUrl ¤tUrl, + static QList desktopToActions(const QString path, const DUrlList &urlList, const DUrl ¤tUrl, const QString &menuExtensionType, const bool onDesktop); static QList jsonToActions(const QJsonArray &data, const DUrlList &urlList, const DUrl ¤tUrl, const QString &menuExtensionType, const bool onDesktop);