| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2007, Nico Gulden |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or |
|---|
| 7 | # modify it under the terms of the GNU General Public License |
|---|
| 8 | # as published by the Free Software Foundation; either version 2 |
|---|
| 9 | # of the License, or (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 19 | |
|---|
| 20 | """ |
|---|
| 21 | This script is a wrapper for kdesktop_lock to enable auto-status for gajim. |
|---|
| 22 | It switches the status to away on session locking and restores the previous status |
|---|
| 23 | for each account. |
|---|
| 24 | """ |
|---|
| 25 | |
|---|
| 26 | import sys |
|---|
| 27 | import commands |
|---|
| 28 | |
|---|
| 29 | def whereis(command): |
|---|
| 30 | """Return the correct path to command using the whereis command.""" |
|---|
| 31 | return commands.getoutput('whereis %s' % command).split(' ')[1] |
|---|
| 32 | |
|---|
| 33 | KDESKTOP_LOCK_BIN = '/usr/kde/current/bin/kdesktop_lock.kde' |
|---|
| 34 | GAJIM_REMOTE_BIN = whereis('gajim-remote') |
|---|
| 35 | |
|---|
| 36 | AWAY_STATUS_MSG = 'Abwesend - Away' |
|---|
| 37 | |
|---|
| 38 | def get_status(): |
|---|
| 39 | """Find out all accounts in gajim and save the current status. |
|---|
| 40 | Output: dictionary with account:status mapping.""" |
|---|
| 41 | status = {} |
|---|
| 42 | accounts = commands.getoutput("%s list_accounts" % GAJIM_REMOTE_BIN).split() |
|---|
| 43 | for a in accounts: |
|---|
| 44 | acc_props = {} |
|---|
| 45 | acc_props['status'] = commands.getoutput("%s get_status %s" % (GAJIM_REMOTE_BIN, a)) |
|---|
| 46 | acc_props['msg'] = commands.getoutput("%s get_status_message %s" % (GAJIM_REMOTE_BIN, a)) |
|---|
| 47 | status[a] = acc_props |
|---|
| 48 | return status |
|---|
| 49 | |
|---|
| 50 | def exec_change_status(status_to, msg, a): |
|---|
| 51 | """Execute the change_status command of gajim-remote.""" |
|---|
| 52 | (exitstatus, outtext) = commands.getstatusoutput("%s change_status %s '%s' %s" % (GAJIM_REMOTE_BIN, status_to, msg, a)) |
|---|
| 53 | |
|---|
| 54 | def change_status(status, status_from, status_to): |
|---|
| 55 | """Change the status for all accounts matching the status given with status_from |
|---|
| 56 | and change them to the status give in status_to.""" |
|---|
| 57 | for a in status.keys(): |
|---|
| 58 | # switch only online accounts to away |
|---|
| 59 | if (status_to == 'away' and status[a]['status'] == 'online'): |
|---|
| 60 | exec_change_status(status_to, AWAY_STATUS_MSG, a) |
|---|
| 61 | # switch only accounts back to away that have been online before |
|---|
| 62 | elif (status_to == 'online' and status[a]['status'] == 'online'): |
|---|
| 63 | exec_change_status(status_to, status[a]['msg'], a) |
|---|
| 64 | |
|---|
| 65 | if __name__ == '__main__': |
|---|
| 66 | if KDESKTOP_LOCK_BIN is None: |
|---|
| 67 | print """You need to configure this script. Find the binary kdesktop_lock and |
|---|
| 68 | move it to kdesktop_lock.kde. Copy %s to /usr/local/bin and make a symlink |
|---|
| 69 | from the kdesktop_lock path to /usr/local/bin/%s and name it kdesktop_lock. |
|---|
| 70 | """ % (sys.argv[0],sys.argv[0]) |
|---|
| 71 | sys.exit(1) |
|---|
| 72 | |
|---|
| 73 | options = " ".join(sys.argv[1:]) |
|---|
| 74 | status = get_status() |
|---|
| 75 | change_status(status, status_from='online', status_to='away') |
|---|
| 76 | commands.getoutput("%s %s" % (KDESKTOP_LOCK_BIN, options)) |
|---|
| 77 | change_status(status, status_from='away', status_to='online') |
|---|