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