Blame SOURCES/0001-fileItem-Support-.desktop-files-of-type-Link.patch

66b32e
From be4ab59a3f2bb9829dde390db3dd8868a08840eb Mon Sep 17 00:00:00 2001
66b32e
From: =?UTF-8?q?Florian=20M=C3=BCllner?= <fmuellner@gnome.org>
66b32e
Date: Fri, 2 Dec 2022 19:28:54 +0100
66b32e
Subject: [PATCH] fileItem: Support .desktop files of type Link
66b32e
66b32e
Gio only has direct support for .desktop files of type Application.
66b32e
66b32e
However in the context of desktop icons (and file managers), shortcuts
66b32e
of URLs are useful as well, so add explicit support for .desktop files
66b32e
of type Link.
66b32e
---
66b32e
 extensions/desktop-icons/fileItem.js | 71 +++++++++++++++++++++++-----
66b32e
 1 file changed, 60 insertions(+), 11 deletions(-)
66b32e
66b32e
diff --git a/extensions/desktop-icons/fileItem.js b/extensions/desktop-icons/fileItem.js
66b32e
index f2f03440..1c9a1e55 100644
66b32e
--- a/extensions/desktop-icons/fileItem.js
66b32e
+++ b/extensions/desktop-icons/fileItem.js
66b32e
@@ -239,12 +239,32 @@ var FileItem = class {
66b32e
             log(`desktop-icons: File ${this._displayName} is writable by others - will not allow launching`);
66b32e
 
66b32e
         if (this._isDesktopFile) {
66b32e
-            this._desktopFile = Gio.DesktopAppInfo.new_from_filename(this._file.get_path());
66b32e
-            if (!this._desktopFile) {
66b32e
-                log(`Couldn’t parse ${this._displayName} as a desktop file, will treat it as a regular file.`);
66b32e
+            try {
66b32e
+                const keyFile = new GLib.KeyFile();
66b32e
+                keyFile.load_from_file(this._file.get_path(), GLib.KeyFileFlags.NONE);
66b32e
+
66b32e
+                const type = keyFile.get_string(
66b32e
+                    GLib.KEY_FILE_DESKTOP_GROUP, GLib.KEY_FILE_DESKTOP_KEY_TYPE);
66b32e
+                switch (type) {
66b32e
+                case GLib.KEY_FILE_DESKTOP_TYPE_APPLICATION:
66b32e
+                    this._desktopFile = Gio.DesktopAppInfo.new_from_keyfile(keyFile);
66b32e
+                    if (!this._desktopFile) {
66b32e
+                        log(`Couldn’t parse ${this._displayName} as a desktop file, will treat it as a regular file.`);
66b32e
+                        this._isValidDesktopFile = false;
66b32e
+                    } else {
66b32e
+                        this._isValidDesktopFile = true;
66b32e
+                    }
66b32e
+                    break;
66b32e
+                case GLib.KEY_FILE_DESKTOP_TYPE_LINK:
66b32e
+                    const url = keyFile.get_string(
66b32e
+                        GLib.KEY_FILE_DESKTOP_GROUP, GLib.KEY_FILE_DESKTOP_KEY_URL);
66b32e
+                    if (url)
66b32e
+                        this._linkFile = keyFile;
66b32e
+                default: // fall-through
66b32e
+                    this._isValidDesktopFile = false;
66b32e
+                }
66b32e
+            } catch (e) {
66b32e
                 this._isValidDesktopFile = false;
66b32e
-            } else {
66b32e
-                this._isValidDesktopFile = true;
66b32e
             }
66b32e
         } else {
66b32e
             this._isValidDesktopFile = false;
66b32e
@@ -356,8 +376,17 @@ var FileItem = class {
66b32e
         if (this._isBrokenSymlink) {
66b32e
             this._icon.child = this._createEmblemedStIcon(null, 'text-x-generic');
66b32e
         } else {
66b32e
-            if (this.trustedDesktopFile && this._desktopFile.has_key('Icon'))
66b32e
-                this._icon.child = this._createEmblemedStIcon(null, this._desktopFile.get_string('Icon'));
66b32e
+            let iconName = null;
66b32e
+
66b32e
+            try {
66b32e
+                if (this.trustedDesktopFile)
66b32e
+                    iconName = this._desktopFile.get_string('Icon');
66b32e
+                else if (this._linkFile)
66b32e
+                    iconName = this._linkFile.get_string(GLib.KEY_FILE_DESKTOP_GROUP, GLib.KEY_FILE_DESKTOP_KEY_ICON);
66b32e
+            } catch (e) {}
66b32e
+
66b32e
+            if (iconName)
66b32e
+                this._icon.child = this._createEmblemedStIcon(null, iconName);
66b32e
             else
66b32e
                 this._icon.child = this._createEmblemedStIcon(this._fileInfo.get_icon(), null);
66b32e
         }
66b32e
@@ -411,7 +440,7 @@ var FileItem = class {
66b32e
                 itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-unreadable')));
66b32e
             else
66b32e
                 itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-symbolic-link')));
66b32e
-        } else if (this.trustedDesktopFile) {
66b32e
+        } else if (this.trustedDesktopFile || this._linkFile) {
66b32e
             itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-symbolic-link')));
66b32e
         }
66b32e
 
66b32e
@@ -440,6 +469,12 @@ var FileItem = class {
66b32e
             return;
66b32e
         }
66b32e
 
66b32e
+        if (this._linkFile) {
66b32e
+            this._openUri(this._linkFile.get_string(
66b32e
+                GLib.KEY_FILE_DESKTOP_GROUP, GLib.KEY_FILE_DESKTOP_KEY_URL));
66b32e
+            return;
66b32e
+        }
66b32e
+
66b32e
         if (this._attributeCanExecute &&
66b32e
            !this._isDirectory &&
66b32e
            !this._isValidDesktopFile &&
66b32e
@@ -449,13 +484,17 @@ var FileItem = class {
66b32e
                 return;
66b32e
         }
66b32e
 
66b32e
-        Gio.AppInfo.launch_default_for_uri_async(this.file.get_uri(),
66b32e
+        this._openUri(this.file.get_uri());
66b32e
+    }
66b32e
+
66b32e
+    _openUri(uri) {
66b32e
+        Gio.AppInfo.launch_default_for_uri_async(uri,
66b32e
             null, null,
66b32e
             (source, result) => {
66b32e
                 try {
66b32e
                     Gio.AppInfo.launch_default_for_uri_finish(result);
66b32e
                 } catch (e) {
66b32e
-                    log('Error opening file ' + this.file.get_uri() + ': ' + e.message);
66b32e
+                    log('Error opening file ' + uri + ': ' + e.message);
66b32e
                 }
66b32e
             }
66b32e
         );
66b32e
@@ -555,7 +594,9 @@ var FileItem = class {
66b32e
     }
66b32e
 
66b32e
     canRename() {
66b32e
-        return !this.trustedDesktopFile && this._fileExtra == Prefs.FileType.NONE;
66b32e
+        return !this.trustedDesktopFile &&
66b32e
+               !this._linkFile &&
66b32e
+               this._fileExtra == Prefs.FileType.NONE;
66b32e
     }
66b32e
 
66b32e
     _doOpenWith() {
66b32e
@@ -819,6 +860,14 @@ var FileItem = class {
66b32e
         if (this.trustedDesktopFile)
66b32e
             return this._desktopFile.get_name();
66b32e
 
66b32e
+        if (this._linkFile) {
66b32e
+            try {
66b32e
+                const name = this._linkFile.get_string(
66b32e
+                    GLib.KEY_FILE_DESKTOP_GROUP, GLib.KEY_FILE_DESKTOP_KEY_NAME);
66b32e
+                return name;
66b32e
+            } catch (e) {}
66b32e
+        }
66b32e
+
66b32e
         return this._displayName || null;
66b32e
     }
66b32e
 
66b32e
-- 
66b32e
2.38.1
66b32e