updating the roster with status changes

This commit is contained in:
Stefan Bethge 2006-06-13 22:42:37 +00:00
parent 7da0b4c220
commit d69327630c
3 changed files with 47 additions and 19 deletions

View File

@ -155,11 +155,16 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def _on_resolve_timeout(self):
if self.connected:
self.zeroconf.resolve_all()
diffs = self.roster.getDiffs()
for key in diffs:
# display_key = self.zeroconf.check_jid(key)
self.dispatch('NOTIFY', (key, diffs[key], '', 'Gajim', 0, None, 0))
return self.call_resolve_timeout
# callbacks called from zeroconf
def _on_new_service(self,jid):
self.roster.setItem(jid)
self.roster.setItem(jid)
# display_jid = self.zeroconf.check_jid(jid)
self.dispatch('ROSTER_INFO', (jid, jid, 'both', 'no', self.roster.getGroups(jid)))
self.dispatch('NOTIFY', (jid, self.roster.getStatus(jid), '', 'Gajim', 0, None, 0))
@ -168,8 +173,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.roster.delItem(jid)
# 'NOTIFY' (account, (jid, status, status message, resource, priority,
# keyID, timestamp))
self.dispatch('NOTIFY', (jid, 'offline', '', 'Gajim', 0
, None, 0))
# jid = self.zeroconf.check_jid(jid)
self.dispatch('NOTIFY', (jid, 'offline', '', 'Gajim', 0, None, 0))
def connect(self, data = None, show = 'online'):
@ -183,7 +188,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connected = STATUS_LIST.index(show)
# refresh all contacts data all 10 seconds
self.call_timeout = True
self.call_resolve_timeout = True
gobject.timeout_add(10000, self._on_resolve_timeout)
def connect_and_init(self, show, msg, signed):

View File

@ -5,13 +5,32 @@ class Roster:
self._data = {}
self.zeroconf = zeroconf # our zeroconf instance
def update_roster(self):
for (jid, dom, interf, proto, host, addr, port, txt) in self.zeroconf.get_contacts().values():
self.setItem(jid)
def getRoster(self):
print 'roster_zeroconf.py: getRoster'
self._data = self.zeroconf.get_contacts().copy()
self.update_roster()
return self
def getDiffs(self):
''' update the roster with new data and return dict with
jid -> new status pairs to do notifications and stuff '''
diffs = {}
old_data = self._data.copy()
self.update_roster()
for key in old_data.keys():
if self._data.has_key(key):
if old_data[key] != self._data[key]:
diffs[key] = self._data[key]['status']
print 'roster_zeroconf.py: diffs:',
print diffs
return diffs
def setItem(self, jid, name = '', groups = ''):
print 'roster_zeroconf.py: setItem %s' % jid
#print 'roster_zeroconf.py: setItem %s' % jid
(service_jid, domain, interface, protocol, host, address, port, txt) \
= self.zeroconf.get_contact(jid)
@ -68,11 +87,9 @@ class Roster:
return {}
def getGroups(self, jid):
print 'roster_zeroconf.py: getGroups(%s)' % jid
return self._data[jid]['groups']
def getStatus(self, jid):
print 'roster_zeroconf.py: getStatus %s' % jid
return self._data[jid]['status']
def getShow(self, jid):

View File

@ -38,7 +38,7 @@ class Zeroconf:
def new_service_callback(self, interface, protocol, name, stype, domain, flags):
if name != self.name:
print "Found service '%s' in domain '%s' on %i.%i." % (name, domain, interface, protocol)
# print "Found service '%s' in domain '%s' on %i.%i." % (name, domain, interface, protocol)
#synchronous resolving
self.server.ResolveService( int(interface), int(protocol), name, stype, \
@ -46,7 +46,7 @@ class Zeroconf:
reply_handler=self.service_resolved_callback, error_handler=self.print_error_callback)
def remove_service_callback(self, interface, protocol, name, stype, domain, flags):
print "Service '%s' in domain '%s' on %i.%i disappeared." % (name, domain, interface, protocol)
# print "Service '%s' in domain '%s' on %i.%i disappeared." % (name, domain, interface, protocol)
del self.contacts[name]
self.remove_serviceCB(name)
@ -55,7 +55,7 @@ class Zeroconf:
if self.service_browsers.has_key((interface, protocol, stype, domain)):
return
print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
# print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \
self.server.ServiceBrowserNew(interface, protocol, \
@ -69,6 +69,13 @@ class Zeroconf:
if domain != "local":
self.browse_domain(interface, protocol, domain)
def check_jid(self, jid):
# miranda uses bad service names, so change them...
if jid.find('@') == -1:
return 'miranda@' + jid
else:
return jid
def txt_array_to_dict(self,t):
l = {}
@ -90,13 +97,16 @@ class Zeroconf:
self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt)
def service_added_callback(self):
print 'Service successfully added'
# print 'Service successfully added'
pass
def service_committed_callback(self):
print 'Service successfully committed'
# print 'Service successfully committed'
pass
def service_updated_callback(self):
print 'Service successfully updated'
# print 'Service successfully updated'
pass
def service_add_fail_callback(self, err):
print 'Error while adding service:', str(err)
@ -142,14 +152,11 @@ class Zeroconf:
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.Commit(reply_handler=self.service_committed_callback, error_handler=self.print_error_callback)
def announce(self):
#for testing
#self.contacts['stefan@munin'] = ('stefan@munin', 'local', '8', '0', 'munin', '192.168.1.29', '5121',avahi.string_array_to_txt_array(['status','avail']))
#self.new_serviceCB('stefan@munin')
state = self.server.GetState()
if state == avahi.SERVER_RUNNING:
@ -196,7 +203,6 @@ class Zeroconf:
self.server.ResolveService(int(val[2]), int(val[3]), val[0], \
self.stype, val[1], avahi.PROTO_UNSPEC, dbus.UInt32(0),\
reply_handler=self.service_resolved_all_callback, error_handler=self.print_error_callback)
print "zeroconf.py: resolve_all"
def get_contacts(self):
return self.contacts