gajim-remote now support nicknames. Fixes #871

This commit is contained in:
Yann Leboulanger 2006-08-03 10:48:11 +00:00
parent 274d5270d1
commit e261e0a7a0

View file

@ -54,7 +54,7 @@ ident = lambda e: e
if dbus_support.version[1] >= 43: if dbus_support.version[1] >= 43:
# in most cases it is a utf-8 string # in most cases it is a utf-8 string
DBUS_STRING = dbus.String DBUS_STRING = dbus.String
# general type (for use in dicts, # general type (for use in dicts,
# where all values should have the same type) # where all values should have the same type)
DBUS_VARIANT = dbus.Variant DBUS_VARIANT = dbus.Variant
@ -67,7 +67,7 @@ if dbus_support.version[1] >= 43:
DBUS_DICT_SS = lambda : dbus.Dictionary({}, signature="ss") DBUS_DICT_SS = lambda : dbus.Dictionary({}, signature="ss")
# empty type # empty type
DBUS_NONE = lambda : dbus.Variant(0) DBUS_NONE = lambda : dbus.Variant(0)
else: # 33, 35, 36 else: # 33, 35, 36
DBUS_DICT_SV = lambda : {} DBUS_DICT_SV = lambda : {}
DBUS_DICT_SS = lambda : {} DBUS_DICT_SS = lambda : {}
@ -122,7 +122,7 @@ class Remote:
def __init__(self): def __init__(self):
self.signal_object = None self.signal_object = None
session_bus = dbus_support.session_bus.SessionBus() session_bus = dbus_support.session_bus.SessionBus()
if dbus_support.version[1] >= 41: if dbus_support.version[1] >= 41:
service = dbus.service.BusName(SERVICE, bus=session_bus) service = dbus.service.BusName(SERVICE, bus=session_bus)
self.signal_object = SignalObject(service) self.signal_object = SignalObject(service)
@ -139,7 +139,7 @@ class Remote:
class SignalObject(DbusPrototype): class SignalObject(DbusPrototype):
''' Local object definition for /org/gajim/dbus/RemoteObject. This doc must ''' Local object definition for /org/gajim/dbus/RemoteObject. This doc must
not be visible, because the clients can access only the remote object. ''' not be visible, because the clients can access only the remote object. '''
def __init__(self, service): def __init__(self, service):
self.first_show = True self.first_show = True
self.vcard_account = None self.vcard_account = None
@ -180,7 +180,7 @@ class SignalObject(DbusPrototype):
i = message.get_iter(True) i = message.get_iter(True)
i.append(arg) i.append(arg)
self._connection.send(message) self._connection.send(message)
def get_status(self, *args): def get_status(self, *args):
'''get_status(account = None) '''get_status(account = None)
returns status (show to be exact) which is the global one returns status (show to be exact) which is the global one
@ -193,7 +193,7 @@ class SignalObject(DbusPrototype):
# return show for the given account # return show for the given account
index = gajim.connections[account].connected index = gajim.connections[account].connected
return DBUS_STRING(STATUS_LIST[index]) return DBUS_STRING(STATUS_LIST[index])
def get_status_message(self, *args): def get_status_message(self, *args):
'''get_status(account = None) '''get_status(account = None)
returns status which is the global one returns status which is the global one
@ -206,7 +206,7 @@ class SignalObject(DbusPrototype):
# return show for the given account # return show for the given account
status = gajim.connections[account].status status = gajim.connections[account].status
return DBUS_STRING(status) return DBUS_STRING(status)
def get_account_and_contact(self, account, jid): def get_account_and_contact(self, account, jid):
''' get the account (if not given) and contact instance from jid''' ''' get the account (if not given) and contact instance from jid'''
@ -232,7 +232,7 @@ class SignalObject(DbusPrototype):
break break
if not contact: if not contact:
contact = jid contact = jid
return connected_account, contact return connected_account, contact
def send_file(self, *args): def send_file(self, *args):
@ -240,6 +240,7 @@ class SignalObject(DbusPrototype):
send file, located at 'file_path' to 'jid', using account send file, located at 'file_path' to 'jid', using account
(optional) 'account' ''' (optional) 'account' '''
file_path, jid, account = self._get_real_arguments(args, 3) file_path, jid, account = self._get_real_arguments(args, 3)
jid = self._get_real_jid(jid, account)
connected_account, contact = self.get_account_and_contact(account, jid) connected_account, contact = self.get_account_and_contact(account, jid)
if connected_account: if connected_account:
@ -250,7 +251,7 @@ class SignalObject(DbusPrototype):
connected_account, contact, file_path) connected_account, contact, file_path)
return True return True
return False return False
def _send_message(self, jid, message, keyID, account, type = 'chat', subject = None): def _send_message(self, jid, message, keyID, account, type = 'chat', subject = None):
''' can be called from send_chat_message (default when send_message) ''' can be called from send_chat_message (default when send_message)
or send_single_message''' or send_single_message'''
@ -258,20 +259,21 @@ class SignalObject(DbusPrototype):
return None # or raise error return None # or raise error
if not keyID: if not keyID:
keyID = '' keyID = ''
connected_account, contact = self.get_account_and_contact(account, jid) connected_account, contact = self.get_account_and_contact(account, jid)
if connected_account: if connected_account:
connection = gajim.connections[connected_account] connection = gajim.connections[connected_account]
res = connection.send_message(jid, message, keyID, type, subject) res = connection.send_message(jid, message, keyID, type, subject)
return True return True
return False return False
def send_chat_message(self, *args): def send_chat_message(self, *args):
''' send_message(jid, message, keyID=None, account=None) ''' send_message(jid, message, keyID=None, account=None)
send chat 'message' to 'jid', using account (optional) 'account'. send chat 'message' to 'jid', using account (optional) 'account'.
if keyID is specified, encrypt the message with the pgp key ''' if keyID is specified, encrypt the message with the pgp key '''
jid, message, keyID, account = self._get_real_arguments(args, 4) jid, message, keyID, account = self._get_real_arguments(args, 4)
jid = self._get_real_jid(jid, account)
return self._send_message(jid, message, keyID, account) return self._send_message(jid, message, keyID, account)
def send_single_message(self, *args): def send_single_message(self, *args):
@ -279,6 +281,7 @@ class SignalObject(DbusPrototype):
send single 'message' to 'jid', using account (optional) 'account'. send single 'message' to 'jid', using account (optional) 'account'.
if keyID is specified, encrypt the message with the pgp key ''' if keyID is specified, encrypt the message with the pgp key '''
jid, subject, message, keyID, account = self._get_real_arguments(args, 5) jid, subject, message, keyID, account = self._get_real_arguments(args, 5)
jid = self._get_real_jid(jid, account)
return self._send_message(jid, message, keyID, account, type, subject) return self._send_message(jid, message, keyID, account, type, subject)
def open_chat(self, *args): def open_chat(self, *args):
@ -288,9 +291,8 @@ class SignalObject(DbusPrototype):
if not jid: if not jid:
# FIXME: raise exception for missing argument (dbus0.35+) # FIXME: raise exception for missing argument (dbus0.35+)
return None return None
if jid.startswith('xmpp:'): jid = self._get_real_jid(jid, account)
jid = jid[5:] # len('xmpp:') = 5
if account: if account:
accounts = [account] accounts = [account]
else: else:
@ -315,11 +317,11 @@ class SignalObject(DbusPrototype):
connected_account = acct connected_account = acct
elif first_connected_acct is None: elif first_connected_acct is None:
first_connected_acct = acct first_connected_acct = acct
# if jid is not a conntact, open-chat with first connected account # if jid is not a conntact, open-chat with first connected account
if connected_account is None and first_connected_acct: if connected_account is None and first_connected_acct:
connected_account = first_connected_acct connected_account = first_connected_acct
if connected_account: if connected_account:
gajim.interface.roster.new_chat_from_jid(connected_account, jid) gajim.interface.roster.new_chat_from_jid(connected_account, jid)
# preserve the 'steal focus preservation' # preserve the 'steal focus preservation'
@ -328,7 +330,7 @@ class SignalObject(DbusPrototype):
win.window.focus() win.window.focus()
return True return True
return False return False
def change_status(self, *args, **keywords): def change_status(self, *args, **keywords):
''' change_status(status, message, account). account is optional - ''' change_status(status, message, account). account is optional -
if not specified status is changed for all accounts. ''' if not specified status is changed for all accounts. '''
@ -346,13 +348,13 @@ class SignalObject(DbusPrototype):
gobject.idle_add(gajim.interface.roster.send_status, acc, gobject.idle_add(gajim.interface.roster.send_status, acc,
status, message) status, message)
return None return None
def show_next_unread(self, *args): def show_next_unread(self, *args):
''' Show the window(s) with next waiting messages in tabbed/group chats. ''' ''' Show the window(s) with next waiting messages in tabbed/group chats. '''
#FIXME: when systray is disabled this method does nothing. #FIXME: when systray is disabled this method does nothing.
if len(gajim.interface.systray.jids) != 0: if len(gajim.interface.systray.jids) != 0:
gajim.interface.systray.handle_first_event() gajim.interface.systray.handle_first_event()
def contact_info(self, *args): def contact_info(self, *args):
''' get vcard info for a contact. Return cached value of the vcard. ''' get vcard info for a contact. Return cached value of the vcard.
''' '''
@ -362,14 +364,15 @@ class SignalObject(DbusPrototype):
if not jid: if not jid:
# FIXME: raise exception for missing argument (0.3+) # FIXME: raise exception for missing argument (0.3+)
return None return None
jid = self._get_real_jid(jid, account)
cached_vcard = gajim.connections.values()[0].get_cached_vcard(jid) cached_vcard = gajim.connections.values()[0].get_cached_vcard(jid)
if cached_vcard: if cached_vcard:
return get_dbus_struct(cached_vcard) return get_dbus_struct(cached_vcard)
# return empty dict # return empty dict
return DBUS_DICT_SV() return DBUS_DICT_SV()
def list_accounts(self, *args): def list_accounts(self, *args):
''' list register accounts ''' ''' list register accounts '''
result = gajim.contacts.get_accounts() result = gajim.contacts.get_accounts()
@ -379,7 +382,7 @@ class SignalObject(DbusPrototype):
result_array.append(DBUS_STRING(account)) result_array.append(DBUS_STRING(account))
return result_array return result_array
return None return None
def account_info(self, *args): def account_info(self, *args):
''' show info on account: resource, jid, nick, prio, message ''' ''' show info on account: resource, jid, nick, prio, message '''
[for_account] = self._get_real_arguments(args, 1) [for_account] = self._get_real_arguments(args, 1)
@ -398,7 +401,7 @@ class SignalObject(DbusPrototype):
result['resource'] = DBUS_STRING(unicode(gajim.config.get_per('accounts', result['resource'] = DBUS_STRING(unicode(gajim.config.get_per('accounts',
account.name, 'resource'))) account.name, 'resource')))
return result return result
def list_contacts(self, *args): def list_contacts(self, *args):
''' list all contacts in the roster. If the first argument is specified, ''' list all contacts in the roster. If the first argument is specified,
then return the contacts for the specified account ''' then return the contacts for the specified account '''
@ -422,7 +425,7 @@ class SignalObject(DbusPrototype):
if result == []: if result == []:
return None return None
return result return result
def toggle_roster_appearance(self, *args): def toggle_roster_appearance(self, *args):
''' shows/hides the roster window ''' ''' shows/hides the roster window '''
win = gajim.interface.roster.window win = gajim.interface.roster.window
@ -449,14 +452,14 @@ class SignalObject(DbusPrototype):
prefs_dict[DBUS_STRING(key)] = DBUS_STRING(value[1]) prefs_dict[DBUS_STRING(key)] = DBUS_STRING(value[1])
gajim.config.foreach(get_prefs) gajim.config.foreach(get_prefs)
return prefs_dict return prefs_dict
def prefs_store(self, *args): def prefs_store(self, *args):
try: try:
gajim.interface.save_config() gajim.interface.save_config()
except Exception, e: except Exception, e:
return False return False
return True return True
def prefs_del(self, *args): def prefs_del(self, *args):
[key] = self._get_real_arguments(args, 1) [key] = self._get_real_arguments(args, 1)
if not key: if not key:
@ -469,7 +472,7 @@ class SignalObject(DbusPrototype):
else: else:
gajim.config.del_per(key_path[0], key_path[1], key_path[2]) gajim.config.del_per(key_path[0], key_path[1], key_path[2])
return True return True
def prefs_put(self, *args): def prefs_put(self, *args):
[key] = self._get_real_arguments(args, 1) [key] = self._get_real_arguments(args, 1)
if not key: if not key:
@ -482,7 +485,7 @@ class SignalObject(DbusPrototype):
subname, value = key_path[2].split('=', 1) subname, value = key_path[2].split('=', 1)
gajim.config.set_per(key_path[0], key_path[1], subname, value) gajim.config.set_per(key_path[0], key_path[1], subname, value)
return True return True
def add_contact(self, *args): def add_contact(self, *args):
[jid, account] = self._get_real_arguments(args, 2) [jid, account] = self._get_real_arguments(args, 2)
if account: if account:
@ -497,11 +500,12 @@ class SignalObject(DbusPrototype):
# if account is not given, show account combobox # if account is not given, show account combobox
AddNewContactWindow(account = None, jid = jid) AddNewContactWindow(account = None, jid = jid)
return True return True
def remove_contact(self, *args): def remove_contact(self, *args):
[jid, account] = self._get_real_arguments(args, 2) [jid, account] = self._get_real_arguments(args, 2)
jid = self._get_real_jid(jid, account)
accounts = gajim.contacts.get_accounts() accounts = gajim.contacts.get_accounts()
# if there is only one account in roster, take it as default # if there is only one account in roster, take it as default
if account: if account:
accounts = [account] accounts = [account]
@ -515,7 +519,7 @@ class SignalObject(DbusPrototype):
gajim.contacts.remove_jid(account, jid) gajim.contacts.remove_jid(account, jid)
contact_exists = True contact_exists = True
return contact_exists return contact_exists
def _is_first(self): def _is_first(self):
if self.first_show: if self.first_show:
self.first_show = False self.first_show = False
@ -535,7 +539,35 @@ class SignalObject(DbusPrototype):
args.extend([None] * (desired_length - len(args))) args.extend([None] * (desired_length - len(args)))
args = args[:desired_length] args = args[:desired_length]
return args return args
def _get_real_jid(self, jid, account = None):
'''get the real jid from the given one: removes xmpp: or get jid from nick
if account is specified, search only in this account
'''
if account:
accounts = [account]
else:
accounts = gajim.connections.keys()
if jid.startswith('xmpp:'):
return jid[5:] # len('xmpp:') = 5
nick_in_roster = None # Is jid a nick ?
for account in accounts:
# Does jid exists in roster of one account ?
if gajim.contacts.get_contacts_from_jid(account, jid):
return jid
if not nick_in_roster:
# look in all contact if one has jid as nick
for jid_ in gajim.contacts.get_jid_list(account):
c = gajim.contacts.get_contacts_from_jid(account, jid_)
if c[0].name == jid:
nick_in_roster = jid_
break
if nick_in_roster:
# We have not found jid in roster, but we found is as a nick
return nick_in_roster
# We have not found it as jid nor as nick, probably a not in roster jid
return jid
def _contacts_as_dbus_structure(self, contacts): def _contacts_as_dbus_structure(self, contacts):
''' get info from list of Contact objects and create dbus dict ''' ''' get info from list of Contact objects and create dbus dict '''
if not contacts: if not contacts:
@ -580,7 +612,7 @@ class SignalObject(DbusPrototype):
elif dbus_support.version[1] >= 41: elif dbus_support.version[1] >= 41:
method = dbus.service.method method = dbus.service.method
signal = dbus.service.signal signal = dbus.service.signal
# prevent using decorators, because they are not supported # prevent using decorators, because they are not supported
# on python < 2.4 # on python < 2.4
# FIXME: use decorators when python2.3 (and dbus 0.23) is OOOOOOLD # FIXME: use decorators when python2.3 (and dbus 0.23) is OOOOOOLD