Merge Activity Watcher plugin improvements from Joseph Crosland (with additional GTK3 fixes)

This commit is contained in:
Stephen Boddy 2015-02-26 00:02:09 +01:00
parent f949dc01a9
commit 59c0499c77
2 changed files with 50 additions and 28 deletions

View File

@ -44,6 +44,16 @@ Classes relating to configuration
{'foo': 'bar'} {'foo': 'bar'}
>>> config.plugin_get('testplugin', 'foo') >>> config.plugin_get('testplugin', 'foo')
'bar' 'bar'
>>> config.plugin_get('testplugin', 'foo', 'new')
'bar'
>>> config.plugin_get('testplugin', 'algo')
Traceback (most recent call last):
...
KeyError: 'ConfigBase::get_item: unknown key algo'
>>> config.plugin_get('testplugin', 'algo', 1)
1
>>> config.plugin_get('anothertestplugin', 'algo', 500)
500
>>> config.get_profile() >>> config.get_profile()
'default' 'default'
>>> config.set_profile('my_first_new_testing_profile') >>> config.set_profile('my_first_new_testing_profile')
@ -257,9 +267,9 @@ class Config(object):
self.set_profile(profile) self.set_profile(profile)
self.inhibited = False self.inhibited = False
def __getitem__(self, key): def __getitem__(self, key, default=None):
"""Look up a configuration item""" """Look up a configuration item"""
return(self.base.get_item(key, self.profile)) return(self.base.get_item(key, self.profile, default=default))
def __setitem__(self, key, value): def __setitem__(self, key, value):
"""Set a particular configuration item""" """Set a particular configuration item"""
@ -394,9 +404,11 @@ class Config(object):
"""Get the command line options""" """Get the command line options"""
return(self.base.command_line_options) return(self.base.command_line_options)
def plugin_get(self, pluginname, key): def plugin_get(self, pluginname, key, default=None):
"""Get a plugin config value""" """Get a plugin config value, if doesn't exist
return(self.base.get_item(key, plugin=pluginname)) return default if specified
"""
return(self.base.get_item(key, plugin=pluginname, default=default))
def plugin_set(self, pluginname, key, value): def plugin_set(self, pluginname, key, value):
"""Set a plugin config value""" """Set a plugin config value"""
@ -655,7 +667,7 @@ class ConfigBase(Borg):
except Exception, ex: except Exception, ex:
err('ConfigBase::save: Unable to save config: %s' % ex) err('ConfigBase::save: Unable to save config: %s' % ex)
def get_item(self, key, profile='default', plugin=None): def get_item(self, key, profile='default', plugin=None, default=None):
"""Look up a configuration item""" """Look up a configuration item"""
if not self.profiles.has_key(profile): if not self.profiles.has_key(profile):
# Hitting this generally implies a bug # Hitting this generally implies a bug
@ -671,10 +683,12 @@ class ConfigBase(Borg):
return(self.profiles[profile][key]) return(self.profiles[profile][key])
elif key == 'keybindings': elif key == 'keybindings':
return(self.keybindings) return(self.keybindings)
elif plugin is not None and self.plugins[plugin].has_key(key): elif plugin and plugin in self.plugins and key in self.plugins[plugin]:
dbg('ConfigBase::get_item: %s found in plugin %s: %s' % ( dbg('ConfigBase::get_item: %s found in plugin %s: %s' % (
key, plugin, self.plugins[plugin][key])) key, plugin, self.plugins[plugin][key]))
return(self.plugins[plugin][key]) return(self.plugins[plugin][key])
elif default:
return default
else: else:
raise KeyError('ConfigBase::get_item: unknown key %s' % key) raise KeyError('ConfigBase::get_item: unknown key %s' % key)

View File

@ -7,6 +7,7 @@ import time
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import GObject from gi.repository import GObject
from terminatorlib.config import Config
import terminatorlib.plugin as plugin import terminatorlib.plugin as plugin
from terminatorlib.translation import _ from terminatorlib.translation import _
from terminatorlib.util import err, dbg from terminatorlib.util import err, dbg
@ -21,6 +22,12 @@ try:
except ImportError: except ImportError:
err(_('ActivityWatch plugin unavailable: please install python-notify')) err(_('ActivityWatch plugin unavailable: please install python-notify'))
config = Config()
inactive_period = float(config.plugin_get('ActivityWatch', 'inactive_period',
10.0))
watch_interval = int(config.plugin_get('ActivityWatch', 'watch_interval',
5000))
class ActivityWatch(plugin.MenuItem): class ActivityWatch(plugin.MenuItem):
"""Add custom commands to the terminal menu""" """Add custom commands to the terminal menu"""
capabilities = ['terminal_menu'] capabilities = ['terminal_menu']
@ -40,25 +47,26 @@ class ActivityWatch(plugin.MenuItem):
Notify.init(APP_NAME.capitalize()) Notify.init(APP_NAME.capitalize())
def callback(self, menuitems, menu, terminal): def callback(self, menuitems, menu, terminal):
"""Add our menu items to the menu""" """Add our menu item to the menu"""
if not self.watches.has_key(terminal): item = Gtk.CheckMenuItem(_('Watch for activity'))
item = Gtk.MenuItem(_('Watch for activity')) item.set_active(self.watches.has_key(terminal))
item.connect("activate", self.watch, terminal) if item.get_active():
else:
item = Gtk.MenuItem(_('Stop watching for activity'))
item.connect("activate", self.unwatch, terminal) item.connect("activate", self.unwatch, terminal)
else:
item.connect("activate", self.watch, terminal)
menuitems.append(item) menuitems.append(item)
dbg('Menu item appended')
def watch(self, _widget, terminal): def watch(self, _widget, terminal):
"""Watch a terminal""" """Watch a terminal"""
vte = terminal.get_vte() vte = terminal.get_vte()
self.watches[terminal] = Vte.connect('contents-changed', self.watches[terminal] = vte.connect('contents-changed',
self.notify, terminal) self.notify, terminal)
def unwatch(self, _widget, terminal): def unwatch(self, _widget, terminal):
"""Stop watching a terminal""" """Stop watching a terminal"""
vte = terminal.get_vte() vte = terminal.get_vte()
Vte.disconnect(self.watches[terminal]) vte.disconnect(self.watches[terminal])
del(self.watches[terminal]) del(self.watches[terminal])
def notify(self, _vte, terminal): def notify(self, _vte, terminal):
@ -66,10 +74,10 @@ class ActivityWatch(plugin.MenuItem):
show_notify = False show_notify = False
# Don't notify if the user is already looking at this terminal. # Don't notify if the user is already looking at this terminal.
if terminal.vte.flags() & Gtk.HAS_FOCUS: if terminal.vte.has_focus():
return True return True
note = Notify.Notification('Terminator', 'Activity in: %s' % note = Notify.Notification.new('Terminator', 'Activity in: %s' %
terminal.get_window_title(), 'terminator') terminal.get_window_title(), 'terminator')
this_time = time.mktime(time.gmtime()) this_time = time.mktime(time.gmtime())
@ -105,29 +113,29 @@ class InactivityWatch(plugin.MenuItem):
Notify.init(APP_NAME.capitalize()) Notify.init(APP_NAME.capitalize())
def callback(self, menuitems, menu, terminal): def callback(self, menuitems, menu, terminal):
"""Add our menu items to the menu""" """Add our menu item to the menu"""
if not self.watches.has_key(terminal): item = Gtk.CheckMenuItem(_("Watch for silence"))
item = Gtk.MenuItem(_("Watch for silence")) item.set_active(self.watches.has_key(terminal))
item.connect("activate", self.watch, terminal) if item.get_active():
else:
item = Gtk.MenuItem(_("Stop watching for silence"))
item.connect("activate", self.unwatch, terminal) item.connect("activate", self.unwatch, terminal)
else:
item.connect("activate", self.watch, terminal)
menuitems.append(item) menuitems.append(item)
dbg('Menu items appended') dbg('Menu items appended')
def watch(self, _widget, terminal): def watch(self, _widget, terminal):
"""Watch a terminal""" """Watch a terminal"""
vte = terminal.get_vte() vte = terminal.get_vte()
self.watches[terminal] = Vte.connect('contents-changed', self.watches[terminal] = vte.connect('contents-changed',
self.reset_timer, terminal) self.reset_timer, terminal)
timeout_id = GObject.timeout_add(5000, self.check_times, terminal) timeout_id = GObject.timeout_add(watch_interval, self.check_times, terminal)
self.timers[terminal] = timeout_id self.timers[terminal] = timeout_id
dbg('timer %s added for %s' %(timeout_id, terminal)) dbg('timer %s added for %s' %(timeout_id, terminal))
def unwatch(self, _vte, terminal): def unwatch(self, _vte, terminal):
"""Unwatch a terminal""" """Unwatch a terminal"""
vte = terminal.get_vte() vte = terminal.get_vte()
Vte.disconnect(self.watches[terminal]) vte.disconnect(self.watches[terminal])
del(self.watches[terminal]) del(self.watches[terminal])
GObject.source_remove(self.timers[terminal]) GObject.source_remove(self.timers[terminal])
del(self.timers[terminal]) del(self.timers[terminal])
@ -146,9 +154,9 @@ class InactivityWatch(plugin.MenuItem):
return True return True
dbg('seconds since last activity: %f (%s)' % (time_now - self.last_activities[terminal], terminal)) dbg('seconds since last activity: %f (%s)' % (time_now - self.last_activities[terminal], terminal))
if time_now - self.last_activities[terminal] >= 10.0: if time_now - self.last_activities[terminal] >= inactive_period:
del(self.last_activities[terminal]) del(self.last_activities[terminal])
note = Notify.Notification('Terminator', 'Silence in: %s' % note = Notify.Notification.new('Terminator', 'Silence in: %s' %
terminal.get_window_title(), 'terminator') terminal.get_window_title(), 'terminator')
note.show() note.show()