Commit 9df2861e authored by Gerard Ryan's avatar Gerard Ryan

gnome_screensaver_away.py 0.2.0: don't trample manual away

- Prevent changing manually-set away status
- Allow configuration of message and poll time
- Tolerate gnome-shell crashes
parent 9050a810
......@@ -20,6 +20,11 @@
# 2016-11-05: Gerard Ryan <gerard@ryan.lt>
# 0.1.0 : Initial version
#
# 2016-12-17: Gerard Ryan <gerard@ryan.lt>
# 0.2.0 : - Prevent changing manually-set away status
# - Allow configuration of message and poll time
# - Tolerate gnome-shell crashes
#
# Contributions welcome at:
# https://github.com/grdryn/weechat-gnome-screensaver-away
......@@ -34,29 +39,54 @@ except ImportError:
SCRIPT_NAME = 'gnome-screensaver-away'
SCRIPT_AUTHOR = 'Gerard Ryan <gerard@ryan.lt>'
SCRIPT_VERSION = '0.1.0'
SCRIPT_VERSION = '0.2.0'
SCRIPT_LICENSE = 'GPLv3+'
SCRIPT_DESC = 'Set away status based on GNOME ScreenSaver status'
SCREENSAVER = dbus.SessionBus().get_object('org.gnome.ScreenSaver',
'/org/gnome/ScreenSaver')
def set_default_configuration(away_msg, poll_interval):
if not weechat.config_get_plugin('away_msg'):
weechat.config_set_plugin('away_msg', away_msg)
if not weechat.config_get_plugin('poll_interval'):
weechat.config_set_plugin('poll_interval', str(poll_interval))
def get_poll_interval_safely():
poll_interval = 5000 # default
try:
poll_interval = int(weechat.config_get_plugin('poll_interval'))
except ValueError:
weechat.println('poll_interval is not an int, falling back to default')
return poll_interval
# TODO: Figure out how to query weechat for the away status instead of
# storing in a global variable?
away = False
# TODO: Try to track the away status of each IRC server independently?
def check_away_status():
away = (False, False)
irc_servers = weechat.infolist_get("irc_server", "", "")
while weechat.infolist_next(irc_servers):
auto_away_msg = weechat.config_get_plugin('away_msg')
current_away_msg = weechat.infolist_string(irc_servers, "away_message")
is_away_by_me = current_away_msg == auto_away_msg
is_away = bool(weechat.infolist_integer(irc_servers, "is_away"))
away = (is_away, is_away_by_me)
return away
def check_screensaver_status(data, remaining_calls):
global away
away, auto_away = check_away_status()
screensaver_on = SCREENSAVER.GetActive(
screensaver = dbus.SessionBus().get_object(
'org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
screensaver_on = screensaver.GetActive(
dbus_interface='org.gnome.ScreenSaver')
if screensaver_on and not away:
weechat.command('', '/away -all I am away')
away = True
elif away and not screensaver_on:
weechat.command('', '/away -all {0}'.format(
weechat.config_get_plugin('away_msg')))
elif away and auto_away and not screensaver_on:
weechat.command('', '/away -all')
away = False
return weechat.WEECHAT_RC_OK
......@@ -64,4 +94,7 @@ if __name__ == '__main__':
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, '', '')
weechat.hook_timer(5000, 0, 0, 'check_screensaver_status', '')
set_default_configuration('I am away', 5000)
poll_interval = get_poll_interval_safely()
weechat.hook_timer(poll_interval, 0, 0, 'check_screensaver_status', '')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment