From 59c0499c774fdd9d587593a424a202eb7bbcc74e Mon Sep 17 00:00:00 2001 From: Stephen Boddy Date: Thu, 26 Feb 2015 00:02:09 +0100 Subject: [PATCH] Merge Activity Watcher plugin improvements from Joseph Crosland (with additional GTK3 fixes) --- terminatorlib/config.py | 28 +++++++++++---- terminatorlib/plugins/activitywatch.py | 50 +++++++++++++++----------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 75f91dcd..4a293097 100755 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -44,6 +44,16 @@ Classes relating to configuration {'foo': 'bar'} >>> config.plugin_get('testplugin', 'foo') '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() 'default' >>> config.set_profile('my_first_new_testing_profile') @@ -257,9 +267,9 @@ class Config(object): self.set_profile(profile) self.inhibited = False - def __getitem__(self, key): + def __getitem__(self, key, default=None): """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): """Set a particular configuration item""" @@ -394,9 +404,11 @@ class Config(object): """Get the command line options""" return(self.base.command_line_options) - def plugin_get(self, pluginname, key): - """Get a plugin config value""" - return(self.base.get_item(key, plugin=pluginname)) + def plugin_get(self, pluginname, key, default=None): + """Get a plugin config value, if doesn't exist + return default if specified + """ + return(self.base.get_item(key, plugin=pluginname, default=default)) def plugin_set(self, pluginname, key, value): """Set a plugin config value""" @@ -655,7 +667,7 @@ class ConfigBase(Borg): except Exception, 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""" if not self.profiles.has_key(profile): # Hitting this generally implies a bug @@ -671,10 +683,12 @@ class ConfigBase(Borg): return(self.profiles[profile][key]) elif key == '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' % ( key, plugin, self.plugins[plugin][key])) return(self.plugins[plugin][key]) + elif default: + return default else: raise KeyError('ConfigBase::get_item: unknown key %s' % key) diff --git a/terminatorlib/plugins/activitywatch.py b/terminatorlib/plugins/activitywatch.py index b4fa5e09..2c49e508 100755 --- a/terminatorlib/plugins/activitywatch.py +++ b/terminatorlib/plugins/activitywatch.py @@ -7,6 +7,7 @@ import time from gi.repository import Gtk from gi.repository import GObject +from terminatorlib.config import Config import terminatorlib.plugin as plugin from terminatorlib.translation import _ from terminatorlib.util import err, dbg @@ -21,6 +22,12 @@ try: except ImportError: 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): """Add custom commands to the terminal menu""" capabilities = ['terminal_menu'] @@ -40,25 +47,26 @@ class ActivityWatch(plugin.MenuItem): Notify.init(APP_NAME.capitalize()) def callback(self, menuitems, menu, terminal): - """Add our menu items to the menu""" - if not self.watches.has_key(terminal): - item = Gtk.MenuItem(_('Watch for activity')) - item.connect("activate", self.watch, terminal) - else: - item = Gtk.MenuItem(_('Stop watching for activity')) + """Add our menu item to the menu""" + item = Gtk.CheckMenuItem(_('Watch for activity')) + item.set_active(self.watches.has_key(terminal)) + if item.get_active(): item.connect("activate", self.unwatch, terminal) + else: + item.connect("activate", self.watch, terminal) menuitems.append(item) + dbg('Menu item appended') def watch(self, _widget, terminal): """Watch a terminal""" vte = terminal.get_vte() - self.watches[terminal] = Vte.connect('contents-changed', + self.watches[terminal] = vte.connect('contents-changed', self.notify, terminal) def unwatch(self, _widget, terminal): """Stop watching a terminal""" vte = terminal.get_vte() - Vte.disconnect(self.watches[terminal]) + vte.disconnect(self.watches[terminal]) del(self.watches[terminal]) def notify(self, _vte, terminal): @@ -66,10 +74,10 @@ class ActivityWatch(plugin.MenuItem): show_notify = False # 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 - note = Notify.Notification('Terminator', 'Activity in: %s' % + note = Notify.Notification.new('Terminator', 'Activity in: %s' % terminal.get_window_title(), 'terminator') this_time = time.mktime(time.gmtime()) @@ -105,29 +113,29 @@ class InactivityWatch(plugin.MenuItem): Notify.init(APP_NAME.capitalize()) def callback(self, menuitems, menu, terminal): - """Add our menu items to the menu""" - if not self.watches.has_key(terminal): - item = Gtk.MenuItem(_("Watch for silence")) - item.connect("activate", self.watch, terminal) - else: - item = Gtk.MenuItem(_("Stop watching for silence")) + """Add our menu item to the menu""" + item = Gtk.CheckMenuItem(_("Watch for silence")) + item.set_active(self.watches.has_key(terminal)) + if item.get_active(): item.connect("activate", self.unwatch, terminal) + else: + item.connect("activate", self.watch, terminal) menuitems.append(item) dbg('Menu items appended') def watch(self, _widget, terminal): """Watch a terminal""" vte = terminal.get_vte() - self.watches[terminal] = Vte.connect('contents-changed', + self.watches[terminal] = vte.connect('contents-changed', 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 dbg('timer %s added for %s' %(timeout_id, terminal)) def unwatch(self, _vte, terminal): """Unwatch a terminal""" vte = terminal.get_vte() - Vte.disconnect(self.watches[terminal]) + vte.disconnect(self.watches[terminal]) del(self.watches[terminal]) GObject.source_remove(self.timers[terminal]) del(self.timers[terminal]) @@ -146,9 +154,9 @@ class InactivityWatch(plugin.MenuItem): return True 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]) - note = Notify.Notification('Terminator', 'Silence in: %s' % + note = Notify.Notification.new('Terminator', 'Silence in: %s' % terminal.get_window_title(), 'terminator') note.show()