Prevent traceback when gajim-remote is used while gajim is not running. Add a check_gajim_running command to gajim-remote

This commit is contained in:
Yann Leboulanger 2008-02-05 22:13:16 +00:00
parent 36438f44b6
commit 0ac8ce3c6e
1 changed files with 54 additions and 28 deletions

View File

@ -59,7 +59,7 @@ BASENAME = 'gajim-remote'
class GajimRemote: class GajimRemote:
def __init__(self): def __init__(self):
self.argv_len = len(sys.argv) self.argv_len = len(sys.argv)
# define commands dict. Prototype : # define commands dict. Prototype :
# { # {
# 'command': [comment, [list of arguments] ] # 'command': [comment, [list of arguments] ]
@ -73,14 +73,14 @@ class GajimRemote:
_('Shows a help on specific command'), _('Shows a help on specific command'),
[ [
#User gets help for the command, specified by this parameter #User gets help for the command, specified by this parameter
(_('command'), (_('command'),
_('show help on command'), False) _('show help on command'), False)
] ]
], ],
'toggle_roster_appearance' : [ 'toggle_roster_appearance' : [
_('Shows or hides the roster window'), _('Shows or hides the roster window'),
[] []
], ],
'show_next_pending_event': [ 'show_next_pending_event': [
_('Pops up a window with the next pending event'), _('Pops up a window with the next pending event'),
[] []
@ -93,27 +93,27 @@ class GajimRemote:
False) False)
] ]
], ],
'list_accounts': [ 'list_accounts': [
_('Prints a list of registered accounts'), _('Prints a list of registered accounts'),
[] []
], ],
'change_status': [ 'change_status': [
_('Changes the status of account or accounts'), _('Changes the status of account or accounts'),
[ [
#offline, online, chat, away, xa, dnd, invisible should not be translated #offline, online, chat, away, xa, dnd, invisible should not be translated
(_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True), (_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True),
(_('message'), _('status message'), False), (_('message'), _('status message'), False),
(_('account'), _('change status of account "account". ' (_('account'), _('change status of account "account". '
'If not specified, try to change status of all accounts that have ' 'If not specified, try to change status of all accounts that have '
'"sync with global status" option set'), False) '"sync with global status" option set'), False)
] ]
], ],
'open_chat': [ 'open_chat': [
_('Shows the chat dialog so that you can send messages to a contact'), _('Shows the chat dialog so that you can send messages to a contact'),
[ [
('jid', _('JID of the contact that you want to chat with'), ('jid', _('JID of the contact that you want to chat with'),
True), True),
(_('account'), _('if specified, contact is taken from the ' (_('account'), _('if specified, contact is taken from the '
'contact list of this account'), False) 'contact list of this account'), False)
] ]
@ -121,7 +121,7 @@ class GajimRemote:
'send_chat_message':[ 'send_chat_message':[
_('Sends new chat message to a contact in the roster. Both OpenPGP key ' _('Sends new chat message to a contact in the roster. Both OpenPGP key '
'and account are optional. If you want to set only \'account\', ' 'and account are optional. If you want to set only \'account\', '
'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'), 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
[ [
('jid', _('JID of the contact that will receive the message'), True), ('jid', _('JID of the contact that will receive the message'), True),
(_('message'), _('message contents'), True), (_('message'), _('message contents'), True),
@ -134,7 +134,7 @@ class GajimRemote:
'send_single_message':[ 'send_single_message':[
_('Sends new single message to a contact in the roster. Both OpenPGP key ' _('Sends new single message to a contact in the roster. Both OpenPGP key '
'and account are optional. If you want to set only \'account\', ' 'and account are optional. If you want to set only \'account\', '
'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'), 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
[ [
('jid', _('JID of the contact that will receive the message'), True), ('jid', _('JID of the contact that will receive the message'), True),
(_('subject'), _('message subject'), True), (_('subject'), _('message subject'), True),
@ -144,9 +144,9 @@ class GajimRemote:
(_('account'), _('if specified, the message will be sent ' (_('account'), _('if specified, the message will be sent '
'using this account'), False), 'using this account'), False),
] ]
], ],
'send_groupchat_message':[ 'send_groupchat_message':[
_('Sends new message to a groupchat you\'ve joined.'), _('Sends new message to a groupchat you\'ve joined.'),
[ [
('room_jid', _('JID of the room that will receive the message'), True), ('room_jid', _('JID of the room that will receive the message'), True),
(_('message'), _('message contents'), True), (_('message'), _('message contents'), True),
@ -155,13 +155,13 @@ class GajimRemote:
] ]
], ],
'contact_info': [ 'contact_info': [
_('Gets detailed info on a contact'), _('Gets detailed info on a contact'),
[ [
('jid', _('JID of the contact'), True) ('jid', _('JID of the contact'), True)
] ]
], ],
'account_info': [ 'account_info': [
_('Gets detailed info on a account'), _('Gets detailed info on a account'),
[ [
('account', _('Name of the account'), True) ('account', _('Name of the account'), True)
] ]
@ -188,8 +188,8 @@ class GajimRemote:
], ],
'prefs_del': [ 'prefs_del': [
_('Deletes a preference item'), _('Deletes a preference item'),
[ [
(_('key'), _('name of the preference to be deleted'), True) (_('key'), _('name of the preference to be deleted'), True)
] ]
], ],
'prefs_store': [ 'prefs_store': [
@ -199,7 +199,7 @@ class GajimRemote:
], ],
'remove_contact': [ 'remove_contact': [
_('Removes contact from roster'), _('Removes contact from roster'),
[ [
('jid', _('JID of the contact'), True), ('jid', _('JID of the contact'), True),
(_('account'), _('if specified, contact is taken from the ' (_('account'), _('if specified, contact is taken from the '
'contact list of this account'), False) 'contact list of this account'), False)
@ -208,7 +208,7 @@ class GajimRemote:
], ],
'add_contact': [ 'add_contact': [
_('Adds contact to roster'), _('Adds contact to roster'),
[ [
(_('jid'), _('JID of the contact'), True), (_('jid'), _('JID of the contact'), True),
(_('account'), _('Adds new contact to this account'), False) (_('account'), _('Adds new contact to this account'), False)
] ]
@ -226,7 +226,7 @@ class GajimRemote:
[ [
(_('account'), _(''), False) (_('account'), _(''), False)
] ]
], ],
'get_unread_msgs_number': [ 'get_unread_msgs_number': [
_('Returns number of unread messages'), _('Returns number of unread messages'),
@ -239,7 +239,7 @@ class GajimRemote:
] ]
], ],
'send_xml': [ 'send_xml': [
_('Sends custom XML'), _('Sends custom XML'),
[ [
('xml', _('XML to send'), True), ('xml', _('XML to send'), True),
('account', _('Account in which the xml will be sent; ' ('account', _('Account in which the xml will be sent; '
@ -263,8 +263,13 @@ class GajimRemote:
(_('account'), _(''), False) (_('account'), _(''), False)
] ]
], ],
'check_gajim_running':[
_('Check if Gajim is running'),
[]
],
} }
self.sbus = None
if self.argv_len < 2 or \ if self.argv_len < 2 or \
sys.argv[1] not in self.commands.keys(): # no args or bad args sys.argv[1] not in self.commands.keys(): # no args or bad args
send_error(self.compose_help()) send_error(self.compose_help())
@ -277,6 +282,9 @@ class GajimRemote:
sys.exit(0) sys.exit(0)
if self.command == 'handle_uri': if self.command == 'handle_uri':
self.handle_uri() self.handle_uri()
if self.command == 'check_gajim_running':
print self.check_gajim_running()
sys.exit(0)
self.init_connection() self.init_connection()
self.check_arguments() self.check_arguments()
@ -323,7 +331,7 @@ class GajimRemote:
pref_keys.sort() pref_keys.sort()
for pref_key in pref_keys: for pref_key in pref_keys:
result = '%s = %s' % (pref_key, res[pref_key]) result = '%s = %s' % (pref_key, res[pref_key])
if isinstance(result, unicode): if isinstance(result, unicode):
print result.encode(PREFERRED_ENCODING) print result.encode(PREFERRED_ENCODING)
else: else:
print result print result
@ -332,6 +340,22 @@ class GajimRemote:
elif res: elif res:
print unicode(res).encode(PREFERRED_ENCODING) print unicode(res).encode(PREFERRED_ENCODING)
def check_gajim_running(self):
if not self.sbus:
try:
self.sbus = dbus.SessionBus()
except:
raise exceptions.SessionBusNotPresent
test = False
if hasattr(self.sbus, 'name_has_owner'):
if self.sbus.name_has_owner(SERVICE):
test = True
elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
SERVICE):
test = True
return test
def init_connection(self): def init_connection(self):
''' create the onnection to the session dbus, ''' create the onnection to the session dbus,
or exit if it is not possible ''' or exit if it is not possible '''
@ -340,6 +364,8 @@ class GajimRemote:
except: except:
raise exceptions.SessionBusNotPresent raise exceptions.SessionBusNotPresent
if not self.check_gajim_running():
send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
obj = self.sbus.get_object(SERVICE, OBJ_PATH) obj = self.sbus.get_object(SERVICE, OBJ_PATH)
interface = dbus.Interface(obj, INTERFACE) interface = dbus.Interface(obj, INTERFACE)
@ -368,7 +394,7 @@ class GajimRemote:
if command in self.commands: if command in self.commands:
command_props = self.commands[command] command_props = self.commands[command]
arguments_str = self.make_arguments_row(command_props[1]) arguments_str = self.make_arguments_row(command_props[1])
str = _('Usage: %s %s %s \n\t %s') % (BASENAME, command, str = _('Usage: %s %s %s \n\t %s') % (BASENAME, command,
arguments_str, command_props[0]) arguments_str, command_props[0])
if len(command_props[1]) > 0: if len(command_props[1]) > 0:
str += '\n\n' + _('Arguments:') + '\n' str += '\n\n' + _('Arguments:') + '\n'
@ -383,7 +409,7 @@ class GajimRemote:
commands = self.commands.keys() commands = self.commands.keys()
commands.sort() commands.sort()
for command in commands: for command in commands:
str += ' ' + command str += ' ' + command
for argument in self.commands[command][1]: for argument in self.commands[command][1]:
str += ' ' str += ' '
if argument[2]: if argument[2]:
@ -457,7 +483,7 @@ class GajimRemote:
if len(args) > argv_len: if len(args) > argv_len:
if args[argv_len][2]: if args[argv_len][2]:
send_error(_('Argument "%s" is not specified. \n' send_error(_('Argument "%s" is not specified. \n'
'Type "%s help %s" for more info') % 'Type "%s help %s" for more info') %
(args[argv_len][0], BASENAME, self.command)) (args[argv_len][0], BASENAME, self.command))
self.arguments = [] self.arguments = []
i = 0 i = 0
@ -485,7 +511,7 @@ class GajimRemote:
if action == 'join': if action == 'join':
self.command = sys.argv[1] = 'join_room' self.command = sys.argv[1] = 'join_room'
return return
sys.exit(0) sys.exit(0)
def call_remote_method(self): def call_remote_method(self):