From 589abe37f7bd460373d63ed95ab02dd25c2f03ea Mon Sep 17 00:00:00 2001 From: Stefan Bethge Date: Mon, 29 May 2006 15:36:18 +0000 Subject: [PATCH] Added publishing of status and status changes for account with name 'zeroconf' --- src/common/connection_zeroconf.py | 44 ++++++++++++++++++------------- src/common/zeroconf.py | 27 ++++++++++++++++--- src/gajim.py | 12 +++++++-- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/common/connection_zeroconf.py b/src/common/connection_zeroconf.py index 4398d9c31..0d31f201f 100644 --- a/src/common/connection_zeroconf.py +++ b/src/common/connection_zeroconf.py @@ -39,8 +39,9 @@ from common import helpers from common import gajim from common import GnuPG from common import zeroconf - +from common import connection_handlers_zeroconf from connection_handlers_zeroconf import * + USE_GPG = GnuPG.USE_GPG from common import i18n @@ -51,9 +52,10 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): def __init__(self, name): ConnectionHandlersZeroconf.__init__(self) self.name = name - self.zeroconf = Zeroconf() + self.zeroconf = zeroconf.Zeroconf() self.connected = 0 # offline -# self.connection = None # xmpppy ClientCommon instance + self.connection = None # dummy connection variable + # this property is used to prevent double connections # self.last_connection = None # last ClientCommon instance self.gpg = None @@ -70,7 +72,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): self.last_io = gajim.idlequeue.current_time() self.last_sent = [] self.last_history_line = {} -# self.password = gajim.config.get_per('accounts', name, 'password') + self.password = gajim.config.get_per('accounts', name, 'password') # self.server_resource = gajim.config.get_per('accounts', name, 'resource') # if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'): # self.keepalives = gajim.config.get_per('accounts', self.name,'keep_alive_every_foo_secs') @@ -253,11 +255,15 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): return h - def connect(self, data = None): + def connect(self, data = None, show = 'online'): + + if self.connection: + return self.connection, '' + + self.zeroconf.connect() + self.connection = 1 + self.connected = STATUS_LIST.index(show) - zeroconf.connect() - - ''' Start a connection to the Jabber server. Returns connection, and connection type ('tls', 'ssl', 'tcp', '') data MUST contain name, hostname, resource, usessl, proxy, @@ -564,12 +570,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): self.connect_and_auth() ''' - if show == 'online': - show = 'avail' - self.zeroconf.txt['status'] = show self.zeroconf.txt['msg'] = msg - self.connect() + self.connect('',show) def _init_roster(self, con): ''' @@ -584,38 +587,43 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): pass def change_status(self, show, msg, sync = False, auto = False): + print "change_status: show: %s msg: %s" % (show, msg) if not show in STATUS_LIST: return -1 - if show == 'chat': - show = 'online' - elif show == 'xa': - show = 'away' - # connect if show != 'offline' and not self.connected: + print "connect in change_status" self.on_purpose = False self.connect_and_init(show, msg, '') + if show != 'invisible': + self.zeroconf.announce() + else: + self.connected = STATUS_LIST.index(show) # disconnect elif show == 'offline' and self.connected: + print "disconnect in change_status" self.connected = 0 self._on_disconnected() # update status elif show != 'offline' and self.connected: + print "update in change_status" was_invisible = self.connected == STATUS_LIST.index('invisible') self.connected = STATUS_LIST.index(show) if show == 'invisible': self.zeroconf.remove_announce() return if was_invisible: + print "announce after invisible in change_status" self.zeroconf.announce() if self.connection: txt = {} txt['status'] = show + txt['msg'] = msg self.zeroconf.update_txt(txt) - self.dispatch('STATUS', show) + self.dispatch('STATUS', show) def _on_disconnected(self): self.dispatch('STATUS', 'offline') diff --git a/src/common/zeroconf.py b/src/common/zeroconf.py index 5067c1eb9..0389f0926 100755 --- a/src/common/zeroconf.py +++ b/src/common/zeroconf.py @@ -94,6 +94,16 @@ class Zeroconf: self.service_add_fail_callback('Local name collision, recreating.') # elif state == avahi.ENTRY_GROUP_FAILURE: + + # make zeroconf-valid names + def replace_show(self, show): + if show == 'chat' or show == '': + show = 'online' + elif show == 'xa': + show = 'away' + elif show == 'online': + show = 'avail' + return show def create_service(self): if self.entrygroup == '': @@ -101,9 +111,13 @@ class Zeroconf: self.entrygroup = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) self.entrygroup.connect_to_signal('StateChanged', self.entrygroup_state_changed_callback) - self.txt[('port.p2pj')] = self.port - self.txt[('version')] = 1 - self.txt[('textvers')] = 1 + self.txt['port.p2pj'] = self.port + self.txt['version'] = 1 + self.txt['textvers'] = 1 + + # replace gajim's status messages with proper ones + if self.txt.has_key('status'): + self.txt['status'] = self.replace_show(self.txt['status']) print "Publishing service '%s' of type %s" % (self.name, self.stype) self.entrygroup.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '', '', self.port, avahi.dict_to_txt_array(self.txt), reply_handler=self.service_added_callback, error_handler=self.service_add_fail_callback) @@ -118,6 +132,7 @@ class Zeroconf: def remove_announce(self): self.entrygroup.Reset() self.entrygroup.Free() + self.entrygroup = '' def browse_domain(self, interface, protocol, domain): self.new_service_type(interface, protocol, self.stype, domain, '') @@ -159,11 +174,15 @@ class Zeroconf: self.resolve_all return self.contacts + def update_txt(self, txt): # update only given keys for key in txt.keys(): self.txt[key]=txt[key] - + + if txt.has_key('status'): + self.txt['status'] = self.replace_show(txt['status']) + txt = avahi.dict_to_txt_array(self.txt) self.entrygroup.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype,'', txt, reply_handler=self.service_updated_callback, error_handler=self.print_error_callback) diff --git a/src/gajim.py b/src/gajim.py index ff68d6a2c..cf3a7e758 100755 --- a/src/gajim.py +++ b/src/gajim.py @@ -39,6 +39,8 @@ from chat_control import ChatControlBase from common import exceptions from common import i18n +from common import connection_zeroconf + i18n.init() _ = i18n._ @@ -1738,8 +1740,14 @@ class Interface: gajim.proxy65_manager = proxy65_manager.Proxy65Manager(gajim.idlequeue) self.register_handlers() for account in gajim.config.get_per('accounts'): - gajim.connections[account] = common.connection.Connection(account) - + if account == 'zeroconf': + print 'Added zeroconf account to list' + gajim.connections[account] = common.connection_zeroconf.ConnectionZeroconf(account) + else: + gajim.connections[account] = common.connection.Connection(account) + + + gtk.about_dialog_set_email_hook(self.on_launch_browser_mailer, 'mail') gtk.about_dialog_set_url_hook(self.on_launch_browser_mailer, 'url')