Description: Fix ldmfindo returning hidden and invalid sessions.
 Cherry picked from upstream bzr r1434.
Author: Stéphane Graber <stgraber@ubuntu.com>
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/ldm/+bug/991745
Origin: upstream
Bug: https://bugs.launchpad.net/ubuntu/+source/ldm/+bug/991745

--- ldm-2.2.9.orig/ldminfod/ldminfod
+++ ldm-2.2.9/ldminfod/ldminfod
@@ -114,11 +114,22 @@ def get_sessions (dir):
             if f.endswith('.desktop') and os.path.isfile(os.path.join(dir, f)):
                 x=dict()
                 for line in file(os.path.join(dir, f), 'r').readlines():
-                    if line.count('Exec=') > 0:
+                    line = line.rstrip()
+
+                    if line.count('Exec=') > 0 or line.count('Hidden=') > 0:
                         variable, value = line.split('=', 1)
                         x[variable]=value
-                if x.has_key('Exec'):
-                    sessions.append(x['Exec'].rstrip())
+
+                if not x.has_key('Exec'):
+                    continue
+
+                if x.has_key('Hidden') and x['Hidden'].lower() == "true":
+                    continue
+
+                if x.has_key('TryExec') and call(['which',x['TryExec']], stdout=PIPE, stderr=PIPE) != 0:
+                    continue
+
+                sessions.append(x['Exec'])
     return sessions
 
 def get_sessions_with_names (dir):
@@ -131,15 +142,27 @@ def get_sessions_with_names (dir):
             if f.endswith('.desktop') and os.path.isfile(os.path.join(dir, f)):
                 x=dict()
                 for line in file(os.path.join(dir, f), 'r').readlines():
-                    if (line.count('Exec=') > 0) or (line.count("Name=") > 0):
+                    line = line.rstrip()
+
+                    if line.count('Exec=') > 0 or line.count("Name=") > 0 or line.count('Hidden=') > 0:
                         variable, value = line.split('=', 1)
                         x[variable]=value
-                if x.has_key('Exec'):
-                    if x.has_key('Name'):
-                        thing = x['Name'].rstrip()
-                    else:
-                        thing = f.replace(".desktop", "")
-                    sessions.append(thing + ":" + x['Exec'].rstrip())
+
+                if not x.has_key('Exec'):
+                    continue
+
+                if x.has_key('Hidden') and x['Hidden'].lower() == "true":
+                    continue
+
+                if x.has_key('TryExec') and call(['which',x['TryExec']], stdout=PIPE, stderr=PIPE) != 0:
+                    continue
+
+                if x.has_key('Name'):
+                    thing = x['Name']
+                else:
+                    thing = f.replace(".desktop", "")
+
+                sessions.append(thing + ":" + x['Exec'])
     return sessions
 
 def get_xsession():
