13
12
# Lesser General Public License for more details.
15
14
# You should have received a copy of the GNU Lesser General Public
16
# License along with this library; if not, write to the
17
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
# Boston, MA 02111-1307, USA.
15
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
24
#Borrowed LaunchManager from "gimmie"
20
from desktopagnostic import fdo, vfs
25
23
class LaunchManager:
27
A program lauching utility which handles opening a URI or executing a
28
program or .desktop launcher, handling variable expansion in the Exec
31
Adds the launched URI or launcher to the ~/.recently-used log. Sets a
32
DESKTOP_STARTUP_ID environment variable containing useful information such
33
as the URI which caused the program execution and a timestamp.
25
"""A program lauching utility which handles opening a URI or
26
executing a program or .desktop launcher.
28
Sets a DESKTOP_STARTUP_ID environment variable containing useful
29
information such as the URI which caused the program execution and
35
32
See the startup notification spec for more information on
36
33
DESKTOP_STARTUP_IDs.
41
def launch_uri(self, uri, mimetype = None):
42
assert uri, "Must specify URI to launch"
48
os.environ['STACKS_LAUNCHER'] = uri
49
os.environ['DESKTOP_STARTUP_ID'] = self.make_startup_id(uri)
50
os.spawnlp(os.P_NOWAIT, "xdg-open", "xdg-open", uri)
56
def get_local_path(self, uri):
57
scheme, path = urllib.splittype(uri)
60
elif scheme == "file":
61
path = urllib.url2pathname(path)
67
def launch_command_with_uris(self, command, uri_list, launcher_uri = None):
68
if command.rfind("%U") > -1:
71
uri_str = uri_str + " " + uri
72
return self.launch_command(command.replace("%U", uri_str), launcher_uri)
73
elif command.rfind("%F") > -1:
76
uri = self.get_local_path(self, uri)
78
file_str = file_str + " " + uri
80
print " !!! Command does not support non-file URLs: ", command
81
return self.launch_command(command.replace("%F", file_str), launcher_uri)
82
elif command.rfind("%u") > -1:
85
startup_ids.append(self.launch_command(command.replace("%u", uri), launcher_uri))
87
return self.launch_command(command.replace("%u", ""), launcher_uri)
89
elif command.rfind("%f") > -1:
92
uri = self.get_local_path(self, uri)
94
startup_ids.append(self.launch_command(command.replace("%f", uri),
97
print " !!! Command does not support non-file URLs: ", command
99
return self.launch_command(command.replace("%f", ""), launcher_uri)
102
return self.launch_command(command, launcher_uri)
104
def make_startup_id(self, key, ev_time = None):
106
ev_time = gtk.get_current_event_time()
108
return "STACKS_TIME%d" % ev_time
110
return "STACKS:%s_TIME%d" % (key, ev_time)
112
def parse_startup_id(self, id):
113
if id and id.startswith("STACKS:"):
37
def launch_uri(self, uri, mimetype=None):
38
file = vfs.File.for_uri(uri)
40
if file is not None and file.exists():
115
uri = id[len("STACKS:"):id.rfind("_TIME")]
116
timestamp = id[id.rfind("_TIME") + len("_TIME"):]
117
return (uri, timestamp)
122
def launch_command(self, command, launcher_uri = None):
123
startup_id = self.make_startup_id(launcher_uri)
126
# Inside forked child
128
os.environ['DESKTOP_STARTUP_ID'] = startup_id
130
os.environ['STACKS_LAUNCHER'] = launcher_uri
135
return (child, startup_id)
137
def launch_dot_desktop(self, uri):
138
item = gnomedesktop.item_new_from_uri(
139
uri, gnomedesktop.LOAD_ONLY_IF_EXISTS)
142
type = item.get_string(gnomedesktop.KEY_TYPE)
143
if type == "Application":
144
command = item.get_string(gnomedesktop.KEY_EXEC)
145
self.launch_command(command, uri)
147
command = "xdg-open " + item.get_string(gnomedesktop.KEY_URL)
148
self.launch_command(command, uri)
43
except glib.GError, e:
44
print "Error when opening: %s" % e
46
print "File at URI not found (%s)" % uri
48
def launch_dot_desktop(self, desktop_uri):
49
file = vfs.File.for_uri(desktop_uri)
51
if file is not None and file.exists():
52
entry = fdo.DesktopEntry.for_file(file)
54
if entry.key_exists("Exec"):
55
if entry.key_exists("StartupNotify"):
56
ev_time = gtk.get_current_event_time()
57
os.environ['DESKTOP_STARTUP_ID'] = "STACKS:%s_TIME%d" % (desktop_uri, ev_time)
61
except glib.GError, e:
62
print "Error when launching: %s" % e
64
if entry.key_exists("StartupNotify"):
65
del os.environ['DESKTOP_STARTUP_ID']
67
print "File not found (%s)" % desktop_uri