[kupfer: 13/67] Allow configuring which terminal to use



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

    Allow configuring which terminal to use

 data/defaults.cfg        |    3 ++
 kupfer/core/settings.py  |   11 +++++++++
 kupfer/desktop_launch.py |   12 +++++++---
 kupfer/terminal.py       |   52 ++++++++++++++++++++++++++++++++++++++++++++++
 kupfer/utils.py          |   26 +++++++++-------------
 5 files changed, 85 insertions(+), 19 deletions(-)
---
diff --git a/data/defaults.cfg b/data/defaults.cfg
index e818ab2..015f2b4 100644
--- a/data/defaults.cfg
+++ b/data/defaults.cfg
@@ -27,6 +27,9 @@ show_preferences = <Control>semicolon
 switch_to_source = <Control>s
 toggle_text_mode_quick = <Control>period
 
+[Tools]
+terminal = gnome-terminal.desktop
+
 
 # Catalog: The sources of the plugin are included
 # as subcatalogs in the main search catalog, and
diff --git a/kupfer/core/settings.py b/kupfer/core/settings.py
index 0ff0bf5..6c58cad 100644
--- a/kupfer/core/settings.py
+++ b/kupfer/core/settings.py
@@ -41,6 +41,7 @@ class SettingsController (gobject.GObject, pretty.OutputMixin):
 		"Directories" : { "direct" : default_directories, "catalog" : (), },
 		"DeepDirectories" : { "direct" : (), "catalog" : (), "depth" : 1, },
 		'Keybindings': {},
+		"Tools": {},
 	}
 	def __init__(self):
 		gobject.GObject.__init__(self)
@@ -373,6 +374,16 @@ class SettingsController (gobject.GObject, pretty.OutputMixin):
 		for key, value in self.get_from_defaults('Keybindings'):
 			self._set_config('Keybindings', key, value)
 
+	def get_preferred_tool(self, tool_id):
+		"""
+		Get preferred ID for a @tool_id
+
+		Supported: 'terminal'
+		"""
+		return self.get_config("Tools", tool_id)
+
+	def set_preferred_tool(self, tool_id, value):
+		return self._set_config("Tools", tool_id, key)
 
 
 # Section, Key, Value
diff --git a/kupfer/desktop_launch.py b/kupfer/desktop_launch.py
index 71f0be9..fc2502b 100644
--- a/kupfer/desktop_launch.py
+++ b/kupfer/desktop_launch.py
@@ -11,11 +11,10 @@ import xdg.DesktopEntry
 import xdg.Exceptions
 
 from kupfer import desktop_parse
+from kupfer import terminal
 
 __all__ = ['launch_app_info']
 
-TERMINAL = ["gnome-terminal", "-x"]
-TERM_STARTUPNOTIFY = True
 STARTUP_ENV = "DESKTOP_STARTUP_ID"
 
 # TODO: Broadcast Gio's launched message on dbus
@@ -270,11 +269,16 @@ def launch_app_info(app_info, gfiles=[], in_terminal=None):
 	if in_terminal is None:
 		in_terminal = desktop_info["Terminal"]
 	if in_terminal:
-		notify = notify or TERM_STARTUPNOTIFY
+		term = terminal.get_configured_terminal()
+		notify = notify or term.startup_notify
 
 	for argv, gfiles in launch_records:
 		if in_terminal:
-			argv = TERMINAL + argv
+			term = terminal.get_configured_terminal()
+			targv = list(term.argv)
+			if term.exearg:
+				targv.append(term.exearg)
+			argv = targv + argv
 		ret = spawn_app(app_info, argv, gfiles, workdir, notify)
 		if not ret:
 			return False
diff --git a/kupfer/terminal.py b/kupfer/terminal.py
new file mode 100644
index 0000000..8c610db
--- /dev/null
+++ b/kupfer/terminal.py
@@ -0,0 +1,52 @@
+
+_TERMINALS = []
+
+class Terminal (object):
+	"""
+	@id_ should be unique and if possible the
+	application id
+	"""
+	def __init__(self, name, argv, exearg, id_, startup_notify=False):
+		self.name = unicode(name)
+		self.argv = list(argv)
+		self.exearg = str(exearg)
+		self.startup_notify = bool(startup_notify)
+		self.app_id = str(id_)
+	def __unicode__(self):
+		return self.name
+	def get_id(self):
+		return self.app_id
+
+def register_terminal(terminal_description):
+	"""Register @terminal_description (can be used by plugins)"""
+	_TERMINALS.append(terminal_description)
+
+def unregister_terminal(terminal_id):
+	_TERMINALS[:] = [t for t in _TERMINALS if t.app_id != terminal_id]
+
+def get_configured_terminal():
+	"""
+	Return the configured Terminal object
+	"""
+	from kupfer.core import settings
+	setctl = settings.SettingsController()
+	id_ = setctl.get_preferred_tool('terminal')
+	for term in _TERMINALS:
+		if term.app_id == id_:
+			return term
+	return _TERMINALS[0]
+
+# Insert default terminals
+
+register_terminal(Terminal(_("GNOME Terminal"), ["gnome-terminal"],
+                             "-x", "gnome-terminal.desktop", True))
+
+register_terminal(Terminal(_("XFCE Terminal"), ["xfce4-terminal"],
+                             "-x", "xfce4-terminal.desktop", True))
+
+register_terminal(Terminal(_("Urxvt"), ["urxvt"],
+                             "-e", "urxvt.desktop", False))
+
+register_terminal(Terminal(_("X Terminal"), ["xterm"],
+                             "-e", "xterm.desktop", False))
+
diff --git a/kupfer/utils.py b/kupfer/utils.py
index 30ed00b..344e7d6 100644
--- a/kupfer/utils.py
+++ b/kupfer/utils.py
@@ -12,6 +12,7 @@ from kupfer import pretty
 from kupfer import kupferstring
 from kupfer import desktop_launch
 from kupfer import desktop_parse
+from kupfer import terminal
 
 def get_dirlist(folder, depth=0, include=None, exclude=None):
 	"""
@@ -180,24 +181,19 @@ class AsyncCommand (object):
 			os.kill(self.pid, signal.SIGKILL)
 
 
-TERM = "gnome-terminal"
-TERM_STARTUPNOTIFY = True
-TERM_EXECARG = "-x"
-TERM_ID = "gnome-terminal.desktop"
-
-def _get_term_argv(_argv):
-	argv = [TERM]
-	if TERM_EXECARG:
-		argv.append(TERM_EXECARG)
-	argv.extend(_argv)
-	return argv
-
 def spawn_terminal(workdir=None):
-	desktop_launch.spawn_app_id(TERM_ID, [TERM], workdir, TERM_STARTUPNOTIFY)
+	term = terminal.get_configured_terminal()
+	notify = term.startup_notify
+	desktop_launch.spawn_app_id(term.app_id, term.argv, workdir, notify)
 
 def spawn_in_terminal(argv, workdir=None):
-	_argv = _get_term_argv(argv)
-	desktop_launch.spawn_app_id(TERM_ID, _argv , workdir, TERM_STARTUPNOTIFY)
+	term = terminal.get_configured_terminal()
+	notify = term.startup_notify
+	_argv = list(term.argv)
+	if term.exearg:
+		_argv.append(term.exearg)
+	_argv.extend(argv)
+	desktop_launch.spawn_app_id(term.app_id, _argv , workdir, notify)
 
 def spawn_async_notify_as(app_id, argv):
 	"""



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