Skip to content

Commit

Permalink
优化.desktop读取方式;.desktop插件支持显示图标
Browse files Browse the repository at this point in the history
  • Loading branch information
gfdgd-xi committed Sep 21, 2024
1 parent c9cb15a commit 7fbae7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dde-file-manager-lib/dde-file-manager-lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 40 additions & 9 deletions dde-file-manager-lib/interfaces/dfilemenumanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#include <QPushButton>
#include <QWidgetAction>

#include <XdgDesktopFile>

namespace DFileMenuData
{
static QMap<MenuAction, QString> actionKeys;
Expand Down Expand Up @@ -464,13 +466,14 @@ QList<QAction *> 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();
Expand All @@ -490,24 +493,52 @@ QList<QAction *> DFileMenuManager::loadMenuExtensionActions(const DUrlList &urlL
return actions;
}

QList<QAction *> DFileMenuManager::desktopToActions(const QString data, const DUrlList &urlList, const DUrl &currentUrl,
QList<QAction *> DFileMenuManager::desktopToActions(const QString path, const DUrlList &urlList, const DUrl &currentUrl,
const QString &menuExtensionType, const bool onDesktop)
{
QList<QAction *> 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=", "");
Expand Down Expand Up @@ -545,7 +576,7 @@ QList<QAction *> 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;
Expand Down Expand Up @@ -600,7 +631,7 @@ QList<QAction *> 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;
Expand Down
2 changes: 1 addition & 1 deletion dde-file-manager-lib/interfaces/dfilemenumanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DFileMenuManager : public QObject
static QList<QAction *> loadEmptyAreaExtensionMenu(DFileMenu *menu, const DUrl &currentUrl, bool onDesktop);

static QList<QAction *> loadMenuExtensionActions(const DUrlList &urlList, const DUrl &currentUrl, bool onDesktop = false);
static QList<QAction *> desktopToActions(const QString data, const DUrlList &urlList, const DUrl &currentUrl,
static QList<QAction *> desktopToActions(const QString path, const DUrlList &urlList, const DUrl &currentUrl,
const QString &menuExtensionType, const bool onDesktop);
static QList<QAction *> jsonToActions(const QJsonArray &data, const DUrlList &urlList, const DUrl &currentUrl,
const QString &menuExtensionType, const bool onDesktop);
Expand Down

0 comments on commit 7fbae7c

Please sign in to comment.