[kupfer: 20/67] Implement AppLeaf.launch and adjust launch module to use app ids



commit aa6ad80851aca3d9c938022ad580787e312f7e59
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Tue Mar 15 22:52:29 2011 +0100

    Implement AppLeaf.launch and adjust launch module to use app ids

 kupfer/desktop_launch.py |   26 +++++++++++++++++---------
 kupfer/launch.py         |   29 +++++++++++++++--------------
 kupfer/obj/objects.py    |   26 +++++++++++++++++++-------
 3 files changed, 51 insertions(+), 30 deletions(-)
---
diff --git a/kupfer/desktop_launch.py b/kupfer/desktop_launch.py
index 08eb9dd..a1f3413 100644
--- a/kupfer/desktop_launch.py
+++ b/kupfer/desktop_launch.py
@@ -227,28 +227,36 @@ def replace_format_specs(argv, location, desktop_info, gfilelist):
 
 	return supports_single_file, files_added_at_end, new_argv
 
-def _file_and_info_for_app_info(app_info):
+def _file_for_app_info(app_info):
 	desktop_info = None
 	try:
 		desktop_file = find_desktop_file(app_info.get_id())
 	except ResourceLookupError:
 		exc_log()
 		desktop_file = None
-	else:
-		try:
-			desktop_info = read_desktop_info(desktop_file)
-		except ResourceReadError:
-			exc_log()
-	return desktop_file, desktop_info
+	return desktop_file
+
+def _info_for_desktop_file(desktop_file):
+	if not desktop_file:
+		return None
+	try:
+		desktop_info = read_desktop_info(desktop_file)
+	except ResourceReadError:
+		desktop_info = None
+		exc_log()
+	return desktop_info
 
-def launch_app_info(app_info, gfiles=[], in_terminal=None, timestamp=None):
+def launch_app_info(app_info, gfiles=[], in_terminal=None, timestamp=None,
+	                desktop_file=None):
 	"""
 	Launch @app_info, opening @gfiles
 
 	@in_terminal: override Terminal flag
 	@timestamp: override timestamp
+	@desktop_file: specify location of desktop file
 	"""
-	desktop_file, desktop_info = _file_and_info_for_app_info(app_info)
+	desktop_file = desktop_file or _file_for_app_info(app_info)
+	desktop_info = _info_for_desktop_file(desktop_file)
 	if not desktop_file or not desktop_info:
 		# Allow in-memory app_info creations (without id or desktop file)
 		desktop_file = ""
diff --git a/kupfer/launch.py b/kupfer/launch.py
index cc9af01..dd08164 100644
--- a/kupfer/launch.py
+++ b/kupfer/launch.py
@@ -51,14 +51,11 @@ def _read_environ(pid, envcache=None):
 	if envcache is not None: envcache[pid] = environ
 	return environ
 
-def application_id(app_info):
+def application_id(app_info, desktop_file=None):
 	"""Return an application id (string) for GAppInfo @app_info"""
 	app_id = app_info.get_id()
 	if not app_id:
-		try:
-			app_id = app_info.init_path
-		except AttributeError:
-			app_id = ""
+		app_id = desktop_file or ""
 	if app_id.endswith(".desktop"):
 		app_id = app_id[:-len(".desktop")]
 	return app_id
@@ -66,15 +63,18 @@ def application_id(app_info):
 def _current_event_time():
 	return gtk.get_current_event_time() or keybindings.get_current_event_time()
 
-def launch_application(app_info, files=(), uris=(), paths=(), track=True, activate=True):
+def launch_application(app_info, files=(), uris=(), paths=(), track=True,
+	                   activate=True, desktop_file=None):
 	"""
-	Launch @app_info correctly, using a startup notification
+	Launch @app_rec correctly, using a startup notification
 
 	you may pass in either a list of gio.Files in @files, or 
 	a list of @uris or @paths
 
 	if @track, it is a user-level application
 	if @activate, activate rather than start a new version
+
+	@app_rec is either an GAppInfo or (GAppInfo, desktop_file_path) tuple
 	"""
 	assert app_info
 
@@ -88,7 +88,7 @@ def launch_application(app_info, files=(), uris=(), paths=(), track=True, activa
 		files = [File(p) for p in uris]
 
 	if track:
-		app_id = application_id(app_info)
+		app_id = application_id(app_info, desktop_file)
 		os.putenv(kupfer_env, app_id)
 	else:
 		app_id = ""
@@ -100,7 +100,7 @@ def launch_application(app_info, files=(), uris=(), paths=(), track=True, activa
 
 		try:
 			ret = desktop_launch.launch_app_info(app_info, files,
-					timestamp=_current_event_time())
+					timestamp=_current_event_time(), desktop_file=desktop_file)
 			if not ret:
 				pretty.print_info(__name__, "Error launching", app_info)
 		except GError, e:
@@ -108,18 +108,19 @@ def launch_application(app_info, files=(), uris=(), paths=(), track=True, activa
 			return False
 		else:
 			if track:
-				svc.launched_application(application_id(app_info))
+				app_id = application_id(app_info, desktop_file)
+				svc.launched_application(app_id)
 	finally:
 		os.unsetenv(kupfer_env)
 	return True
 
-def application_is_running(app_info):
+def application_is_running(app_id):
 	svc = GetApplicationsMatcherService()
-	return svc.application_is_running(application_id(app_info))
+	return svc.application_is_running(app_id)
 
-def application_close_all(app_info):
+def application_close_all(app_id):
 	svc = GetApplicationsMatcherService()
-	return svc.application_close_all(application_id(app_info))
+	return svc.application_close_all(app_id)
 
 class ApplicationsMatcherService (pretty.OutputMixin):
 	"""Handle launching applications and see if they still run.
diff --git a/kupfer/obj/objects.py b/kupfer/obj/objects.py
index 2cb1d90..29f53a1 100644
--- a/kupfer/obj/objects.py
+++ b/kupfer/obj/objects.py
@@ -218,16 +218,29 @@ class AppLeaf (Leaf):
 	def _get_package_name(self):
 		return gobject.filename_display_basename(self.get_id())
 
+	def launch(self, files=(), paths=(), activate=False):
+		"""
+		Launch the represented applications
+
+		@files: a seq of GFiles (gio.File)
+		@paths: a seq of bytestring paths
+		@activate: activate instead of start new
+		"""
+		return launch.launch_application(self.object, files=files, paths=paths,
+		                                 activate=activate,
+		                                 desktop_file=self.init_path)
+
+
 	def get_id(self):
 		"""Return the unique ID for this app.
 
 		This is the GIO id "gedit.desktop" minus the .desktop part for
 		system-installed applications.
 		"""
-		return launch.application_id(self.object)
+		return launch.application_id(self.object, self.init_path)
 
 	def get_actions(self):
-		if launch.application_is_running(self.object):
+		if launch.application_is_running(self.get_id()):
 			yield Launch(_("Go To"), is_running=True)
 			yield CloseAll()
 		else:
@@ -285,8 +298,7 @@ class Launch (Action):
 		self.open_new = open_new
 	
 	def activate(self, leaf):
-		desktop_item = leaf.object
-		launch.launch_application(leaf.object, activate=not self.open_new)
+		leaf.launch(activate=not self.open_new)
 
 	def get_description(self):
 		if self.is_running:
@@ -307,7 +319,7 @@ class LaunchAgain (Launch):
 	def item_types(self):
 		yield AppLeaf
 	def valid_for_item(self, leaf):
-		return launch.application_is_running(leaf.object)
+		return launch.application_is_running(leaf.get_id())
 	def get_description(self):
 		return _("Launch another instance of this application")
 
@@ -317,11 +329,11 @@ class CloseAll (Action):
 	def __init__(self):
 		Action.__init__(self, _("Close"))
 	def activate(self, leaf):
-		return launch.application_close_all(leaf.object)
+		return launch.application_close_all(leaf.get_id())
 	def item_types(self):
 		yield AppLeaf
 	def valid_for_item(self, leaf):
-		return launch.application_is_running(leaf.object)
+		return launch.application_is_running(leaf.get_id())
 	def get_description(self):
 		return _("Attempt to close all application windows")
 	def get_icon_name(self):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]