Package common :: Package zeroconf :: Module roster_zeroconf
[hide private]
[frames] | no frames]

Source Code for Module common.zeroconf.roster_zeroconf

  1  ##      common/zeroconf/roster_zeroconf.py 
  2  ## 
  3  ## Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de> 
  4  ## 
  5  ## This file is part of Gajim. 
  6  ## 
  7  ## Gajim is free software; you can redistribute it and/or modify 
  8  ## it under the terms of the GNU General Public License as published 
  9  ## by the Free Software Foundation; version 3 only. 
 10  ## 
 11  ## Gajim is distributed in the hope that it will be useful, 
 12  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  ## GNU General Public License for more details. 
 15  ## 
 16  ## You should have received a copy of the GNU General Public License 
 17  ## along with Gajim.  If not, see <http://www.gnu.org/licenses/>. 
 18  ## 
 19   
 20   
 21  from common.zeroconf import zeroconf 
 22   
23 -class Roster:
24 - def __init__(self, zeroconf):
25 self._data = None 26 self.zeroconf = zeroconf # our zeroconf instance
27
28 - def update_roster(self):
29 for val in self.zeroconf.contacts.values(): 30 self.setItem(val[zeroconf.C_NAME])
31
32 - def getRoster(self):
33 #print 'roster_zeroconf.py: getRoster' 34 if self._data is None: 35 self._data = {} 36 self.update_roster() 37 return self
38
39 - def getDiffs(self):
40 """ 41 Update the roster with new data and return dict with jid -> new status 42 pairs to do notifications and stuff 43 """ 44 diffs = {} 45 old_data = self._data.copy() 46 self.update_roster() 47 for key in old_data.keys(): 48 if key in self._data: 49 if old_data[key] != self._data[key]: 50 diffs[key] = self._data[key]['status'] 51 #print 'roster_zeroconf.py: diffs:' + str(diffs) 52 return diffs
53
54 - def setItem(self, jid, name='', groups=''):
55 #print 'roster_zeroconf.py: setItem %s' % jid 56 contact = self.zeroconf.get_contact(jid) 57 if not contact: 58 return 59 60 host, address, port = contact[4:7] 61 txt = contact[8] 62 63 self._data[jid]={} 64 self._data[jid]['ask'] = 'none' 65 self._data[jid]['subscription'] = 'both' 66 self._data[jid]['groups'] = [] 67 self._data[jid]['resources'] = {} 68 self._data[jid]['address'] = address 69 self._data[jid]['host'] = host 70 self._data[jid]['port'] = port 71 txt_dict = self.zeroconf.txt_array_to_dict(txt) 72 status = txt_dict.get('status', '') 73 if not status: 74 status = 'avail' 75 nm = txt_dict.get('1st', '') 76 if 'last' in txt_dict: 77 if nm != '': 78 nm += ' ' 79 nm += txt_dict['last'] 80 if nm: 81 self._data[jid]['name'] = nm 82 else: 83 self._data[jid]['name'] = jid 84 if status == 'avail': 85 status = 'online' 86 self._data[jid]['txt_dict'] = txt_dict 87 if 'msg' not in self._data[jid]['txt_dict']: 88 self._data[jid]['txt_dict']['msg'] = '' 89 self._data[jid]['status'] = status 90 self._data[jid]['show'] = status
91
92 - def setItemMulti(self, items):
93 for i in items: 94 self.setItem(jid=i['jid'], name=i['name'], groups=i['groups'])
95
96 - def delItem(self, jid):
97 #print 'roster_zeroconf.py: delItem %s' % jid 98 if jid in self._data: 99 del self._data[jid]
100
101 - def getItem(self, jid):
102 #print 'roster_zeroconf.py: getItem: %s' % jid 103 if jid in self._data: 104 return self._data[jid]
105
106 - def __getitem__(self, jid):
107 #print 'roster_zeroconf.py: __getitem__' 108 return self._data[jid]
109
110 - def getItems(self):
111 #print 'roster_zeroconf.py: getItems' 112 # Return list of all [bare] JIDs that the roster currently tracks. 113 return self._data.keys()
114
115 - def keys(self):
116 #print 'roster_zeroconf.py: keys' 117 return self._data.keys()
118
119 - def getRaw(self):
120 #print 'roster_zeroconf.py: getRaw' 121 return self._data
122
123 - def getResources(self, jid):
124 #print 'roster_zeroconf.py: getResources(%s)' % jid 125 return {}
126
127 - def getGroups(self, jid):
128 return self._data[jid]['groups']
129
130 - def getName(self, jid):
131 if jid in self._data: 132 return self._data[jid]['name']
133
134 - def getStatus(self, jid):
135 if jid in self._data: 136 return self._data[jid]['status']
137
138 - def getMessage(self, jid):
139 if jid in self._data: 140 return self._data[jid]['txt_dict']['msg']
141
142 - def getShow(self, jid):
143 #print 'roster_zeroconf.py: getShow' 144 return self.getStatus(jid)
145
146 - def getPriority(self, jid):
147 return 5
148
149 - def getSubscription(self, jid):
150 #print 'roster_zeroconf.py: getSubscription' 151 return 'both'
152
153 - def Subscribe(self, jid):
154 pass
155
156 - def Unsubscribe(self, jid):
157 pass
158
159 - def Authorize(self, jid):
160 pass
161
162 - def Unauthorize(self, jid):
163 pass
164