Added publishing of status and status changes for account with name 'zeroconf'

This commit is contained in:
Stefan Bethge 2006-05-29 15:36:18 +00:00
parent e324eb4f32
commit 589abe37f7
3 changed files with 59 additions and 24 deletions

View File

@ -39,8 +39,9 @@ from common import helpers
from common import gajim from common import gajim
from common import GnuPG from common import GnuPG
from common import zeroconf from common import zeroconf
from common import connection_handlers_zeroconf
from connection_handlers_zeroconf import * from connection_handlers_zeroconf import *
USE_GPG = GnuPG.USE_GPG USE_GPG = GnuPG.USE_GPG
from common import i18n from common import i18n
@ -51,9 +52,10 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def __init__(self, name): def __init__(self, name):
ConnectionHandlersZeroconf.__init__(self) ConnectionHandlersZeroconf.__init__(self)
self.name = name self.name = name
self.zeroconf = Zeroconf() self.zeroconf = zeroconf.Zeroconf()
self.connected = 0 # offline self.connected = 0 # offline
# self.connection = None # xmpppy ClientCommon instance self.connection = None # dummy connection variable
# this property is used to prevent double connections # this property is used to prevent double connections
# self.last_connection = None # last ClientCommon instance # self.last_connection = None # last ClientCommon instance
self.gpg = None self.gpg = None
@ -70,7 +72,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.last_io = gajim.idlequeue.current_time() self.last_io = gajim.idlequeue.current_time()
self.last_sent = [] self.last_sent = []
self.last_history_line = {} 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') # self.server_resource = gajim.config.get_per('accounts', name, 'resource')
# if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'): # 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') # self.keepalives = gajim.config.get_per('accounts', self.name,'keep_alive_every_foo_secs')
@ -253,11 +255,15 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
return h 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. ''' Start a connection to the Jabber server.
Returns connection, and connection type ('tls', 'ssl', 'tcp', '') Returns connection, and connection type ('tls', 'ssl', 'tcp', '')
data MUST contain name, hostname, resource, usessl, proxy, data MUST contain name, hostname, resource, usessl, proxy,
@ -564,12 +570,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connect_and_auth() self.connect_and_auth()
''' '''
if show == 'online':
show = 'avail'
self.zeroconf.txt['status'] = show self.zeroconf.txt['status'] = show
self.zeroconf.txt['msg'] = msg self.zeroconf.txt['msg'] = msg
self.connect() self.connect('',show)
def _init_roster(self, con): def _init_roster(self, con):
''' '''
@ -584,38 +587,43 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
pass pass
def change_status(self, show, msg, sync = False, auto = False): 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: if not show in STATUS_LIST:
return -1 return -1
if show == 'chat':
show = 'online'
elif show == 'xa':
show = 'away'
# connect # connect
if show != 'offline' and not self.connected: if show != 'offline' and not self.connected:
print "connect in change_status"
self.on_purpose = False self.on_purpose = False
self.connect_and_init(show, msg, '') self.connect_and_init(show, msg, '')
if show != 'invisible':
self.zeroconf.announce()
else:
self.connected = STATUS_LIST.index(show)
# disconnect # disconnect
elif show == 'offline' and self.connected: elif show == 'offline' and self.connected:
print "disconnect in change_status"
self.connected = 0 self.connected = 0
self._on_disconnected() self._on_disconnected()
# update status # update status
elif show != 'offline' and self.connected: elif show != 'offline' and self.connected:
print "update in change_status"
was_invisible = self.connected == STATUS_LIST.index('invisible') was_invisible = self.connected == STATUS_LIST.index('invisible')
self.connected = STATUS_LIST.index(show) self.connected = STATUS_LIST.index(show)
if show == 'invisible': if show == 'invisible':
self.zeroconf.remove_announce() self.zeroconf.remove_announce()
return return
if was_invisible: if was_invisible:
print "announce after invisible in change_status"
self.zeroconf.announce() self.zeroconf.announce()
if self.connection: if self.connection:
txt = {} txt = {}
txt['status'] = show txt['status'] = show
txt['msg'] = msg
self.zeroconf.update_txt(txt) self.zeroconf.update_txt(txt)
self.dispatch('STATUS', show) self.dispatch('STATUS', show)
def _on_disconnected(self): def _on_disconnected(self):
self.dispatch('STATUS', 'offline') self.dispatch('STATUS', 'offline')

View File

@ -94,6 +94,16 @@ class Zeroconf:
self.service_add_fail_callback('Local name collision, recreating.') self.service_add_fail_callback('Local name collision, recreating.')
# elif state == avahi.ENTRY_GROUP_FAILURE: # 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): def create_service(self):
if self.entrygroup == '': 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 = 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.entrygroup.connect_to_signal('StateChanged', self.entrygroup_state_changed_callback)
self.txt[('port.p2pj')] = self.port self.txt['port.p2pj'] = self.port
self.txt[('version')] = 1 self.txt['version'] = 1
self.txt[('textvers')] = 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) 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) 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): def remove_announce(self):
self.entrygroup.Reset() self.entrygroup.Reset()
self.entrygroup.Free() self.entrygroup.Free()
self.entrygroup = ''
def browse_domain(self, interface, protocol, domain): def browse_domain(self, interface, protocol, domain):
self.new_service_type(interface, protocol, self.stype, domain, '') self.new_service_type(interface, protocol, self.stype, domain, '')
@ -159,11 +174,15 @@ class Zeroconf:
self.resolve_all self.resolve_all
return self.contacts return self.contacts
def update_txt(self, txt): def update_txt(self, txt):
# update only given keys # update only given keys
for key in txt.keys(): for key in txt.keys():
self.txt[key]=txt[key] 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) 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) 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)

View File

@ -39,6 +39,8 @@ from chat_control import ChatControlBase
from common import exceptions from common import exceptions
from common import i18n from common import i18n
from common import connection_zeroconf
i18n.init() i18n.init()
_ = i18n._ _ = i18n._
@ -1738,8 +1740,14 @@ class Interface:
gajim.proxy65_manager = proxy65_manager.Proxy65Manager(gajim.idlequeue) gajim.proxy65_manager = proxy65_manager.Proxy65Manager(gajim.idlequeue)
self.register_handlers() self.register_handlers()
for account in gajim.config.get_per('accounts'): 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_email_hook(self.on_launch_browser_mailer, 'mail')
gtk.about_dialog_set_url_hook(self.on_launch_browser_mailer, 'url') gtk.about_dialog_set_url_hook(self.on_launch_browser_mailer, 'url')