Blob Blame History Raw
From ba265f969b9061d221915c2528122c44a85ad7c2 Mon Sep 17 00:00:00 2001
From: Till Maas <opensource@till.name>
Date: Fri, 11 Apr 2014 18:35:34 +0200
Subject: [PATCH 10/22] xdg-open: Support multiple groups

Desktop files might contain multiple groups. Check for keys only in the
Desktop Entry group.
---
 scripts/xdg-open.in | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/scripts/xdg-open.in b/scripts/xdg-open.in
index e7a15a9..b994fa5 100644
--- a/scripts/xdg-open.in
+++ b/scripts/xdg-open.in
@@ -36,15 +36,36 @@ last_word()
     echo "$rest"
 }
 
-# Get the value of a key in a .desktop file.
+# Get the value of a key in a desktop file's Desktop Entry group.
 # Example: Use get_key foo.desktop Exec
-# to get the values of the Exec= key
+# to get the values of the Exec= key for the Desktop Entry group.
 get_key()
 {
     local file="${1}"
     local key="${2}"
-
-    grep -E "^${key}=" "${file}" | cut -d= -f 2-
+    local desktop_entry=""
+
+    IFS_="${IFS}"
+    IFS=""
+    while read line
+    do
+        case "$line" in
+            "[Desktop Entry]")
+                desktop_entry="y"
+            ;;
+            # Reset match flag for other groups
+            [*)
+                desktop_entry=""
+            ;;
+            *)
+                # Only match Desktop Entry group
+                if [ -n "${desktop_entry}" ]
+                then
+                    echo "${line}" | grep -E "^${key}=" "${file}" | cut -d= -f 2-
+                fi
+        esac
+    done < "${file}"
+    IFS="${IFS_}"
 }
 
 open_darwin()
-- 
2.1.0