3 # xdg-autostart runs things based on the XDG autostart specification
4 # Copyright (C) 2008 Dana Jansens
6 # XDG autostart specification can be found here:
7 # http://standards.freedesktop.org/autostart-spec/
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
25 from xdg import BaseDirectory
26 from xdg.DesktopEntry import DesktopEntry
27 from xdg.Exceptions import ParsingError
30 def main(argv=sys.argv):
31 if "--help" in argv[1:]:
34 if "--version" in argv[1:]:
38 # get the autostart directories
39 autodirs = BaseDirectory.load_config_paths("autostart")
41 # find all the autostart files
44 for path in glob.glob(os.path.join(dir, '*.desktop')):
46 autofile = AutostartFile(path)
48 print "Invalid .desktop file: " + path
50 if not autofile in files:
51 files.append(autofile)
54 if "--list" in argv[1:]:
59 environments = argv[1:]
60 for autofile in files:
61 if list: autofile.list(environments)
62 else: autofile.run(environments)
64 class AutostartFile():
65 def __init__(self, path):
67 self.filename = os.path.basename(path)
68 self.dirname = os.path.dirname(path)
69 self.de = DesktopEntry(path)
71 def __eq__(self, other):
72 return self.filename == other.filename
75 return self.path + " : " + self.de.getName()
78 return os.access(path, os.X_OK)
80 def findFile(self, path, search, match_func):
82 if not path: return None
85 if match_func(path): return path
88 for dirname in search.split(os.pathsep):
90 candidate = os.path.join(dirname, path)
91 if (match_func(candidate)): return candidate
93 def alert(self, str, info=False):
99 def showInEnvironment(self, envs, verbose=False):
100 default = not self.de.getOnlyShowIn()
103 for i in self.de.getOnlyShowIn():
104 if i in envs: force = True
105 for i in self.de.getNotShowIn():
106 if i in envs: noshow = True
109 if not default and not force:
111 for i in self.de.getOnlyShowIn():
114 self.alert("Excluded by: OnlyShowIn (" + s + ")")
115 if default and noshow and not force:
117 for i in self.de.getOnlyShowIn():
120 self.alert("Excluded by: NotShowIn (" + s + ")")
121 return (default and not noshow) or force
123 def shouldRun(self, envs, verbose=False):
124 if not self.de.getExec():
125 if verbose: self.alert("Excluded by: Missing Exec field")
127 if self.de.getHidden():
128 if verbose: self.alert("Excluded by: Hidden")
130 if self.de.getTryExec():
131 if not self.findFile(self.de.getTryExec(), os.getenv("PATH"),
133 if verbose: self.alert("Excluded by: TryExec (" +
134 self.de.getTryExec() + ")")
136 if not self.showInEnvironment(envs, verbose):
140 def list(self, envs):
142 if self.shouldRun(envs):
143 print "[*] " + self.de.getName()
145 print "[ ] " + self.de.getName()
146 self.alert("File: " + self.path, info=True)
147 if self.de.getExec():
148 self.alert("Executes: " + self.de.getExec(), info=True)
149 self.shouldRun(envs, True)
154 if self.de.getPath():
155 os.chdir(self.de.getPath())
156 if self.shouldRun(envs):
157 print "Running autostart file: " + self.path
158 os.system(self.de.getExec());
162 print "Usage:", ME, "[OPTION]... [ENVIRONMENT]..."
164 print "This tool will run xdg autostart .desktop files"
167 print " --list Show a list of the files which would be run"
168 print " Files which would be run are marked with an asterix"
169 print " symbol [*]. For files which would not be run,"
170 print " information is given for why they are excluded"
171 print " --help Show this help and exit"
172 print " --version Show version and copyright information"
174 print "ENVIRONMENT specifies a list of environments for which to run autostart"
175 print "applications for. If none are specified, only applications which do not "
176 print "limit themselves to certain environments will be run."
178 print "ENVIRONMENT can be one or more of:"
179 print " GNOME Gnome Desktop"
180 print " KDE KDE Desktop"
181 print " ROX ROX Desktop"
182 print " XFCE XFCE Desktop"
183 print " Old Legacy systems"
188 print "Copyright (c) 2008 Dana Jansens"
191 if __name__ == "__main__":