summaryrefslogtreecommitdiff
path: root/src/roster.py
blob: 7191e5cab1573074928dd007476fb9dd4a867680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2010 Le Coz Florent <louiz@louiz.org>
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Poezio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Poezio.  If not, see <http://www.gnu.org/licenses/>.


"""
Defines the Roster and RosterGroup classes
"""

from config import config
from contact import Contact, Resource

class Roster(object):
    def __init__(self):
        self._contacts = {}     # key = bare jid; value = Contact()
        self._roster_groups = []

    def add_contact(self, contact, jid):
        """
        Add a contact to the contact list
        """
        assert jid not in self._contacts
        self._contacts[jid] = contact

    def get_contact_len(self):
        """
        Return the number of contacts in this group
        """
        return len(self._contacts.keys())

    def get_contact_by_jid(self, jid):
        """
        Returns the contact with the given bare JID
        """
        if jid in self._contacts:
            return self._contacts[jid]
        return None

    def edit_groups_of_contact(self, contact, groups):
        """
        Edit the groups the contact is in
        Add or remove RosterGroup if needed
        """
        # add the contact to each group he is in
        # If the contact hasn't any group, we put her in
        # the virtual default 'none' group
        if not len(groups): 
            groups = ['none']
        for group in groups:
            if group not in contact._groups:
                # create the group if it doesn't exist yet
                contact._groups.append(group)
            self.add_contact_to_group(group, contact)
        # remove the contact from each group he is not in
        for group in contact._groups:
            if group not in groups:
                # the contact is not in the group anymore
                self.remove_contact_from_group(group, contact)

    def remove_contact_from_group(self, group_name, contact):
        """
        Remove the contact from the group.
        Delete the group if this makes it empty
        """
        for group in self._roster_groups:
            if group.name == group_name:
                group.remove_contact(contact)
                if group.is_empty():
                    self._roster_groups.remove(group)
                return

    def add_contact_to_group(self, group_name, contact):
        """
        Add the contact to the group.
        Create the group if it doesn't already exist
        """
        for group in self._roster_groups:
            if group.name == group_name:
                if not group.has_contact(contact):
                    group.add_contact(contact)
                return
        new_group = RosterGroup(group_name)
        self._roster_groups.append(new_group)
        new_group.add_contact(contact)

    def get_groups(self):
        """
        Returns the list of groups
        """
        return self._roster_groups

    def __len__(self):
        """
        Return the number of line that would be printed
        for the whole roster
        """
        length = 0
        for group in self._roster_groups:
            if group.get_nb_connected_contacts() == 0:
                continue
            length += 1              # One for the group's line itself
            if not group.folded:
                for contact in group.get_contacts():
                    # We do not count the offline contacts (depending on config)
                    if config.get('roster_show_offline', 'false') == 'false' and\
                            contact.get_nb_resources() == 0:
                        continue
                    length += 1      # One for the contact's line
                    if not contact._folded:
                        # One for each resource, if the contact is unfolded
                        length += contact.get_nb_resources()
        return length

    def __repr__(self):
        ret = '== Roster:\nContacts:\n'
        for contact in self._contacts:
            ret += '%s\n' % (contact,)
        ret += 'Groups\n'
        for group in self._roster_groups:
            ret += '%s\n' % (group,)
        return ret + '\n'

PRESENCE_PRIORITY = {'unavailable': 0,
                     'xa': 1,
                     'away': 2,
                     'dnd': 3,
                     '': 4,
                     'available': 4}

class RosterGroup(object):
    """
    A RosterGroup is a group containing contacts
    It can be Friends/Family etc, but also can be
    Online/Offline or whatever
    """
    def __init__(self, name, folded=False):
        self._contacts = []
        self.name = name
        self.folded = folded    # if the group content is to be shown

    def is_empty(self):
        return len(self._contacts) == 0

    def has_contact(self, contact):
        """
        Return a bool, telling if the contact
        is already in the group
        """
        if contact in self._contacts:
            return True
        return False

    def remove_contact(self, contact):
        """
        Remove a Contact object to the list
        """
        assert isinstance(contact, Contact)
        assert contact in self._contacts
        self._contacts.remove(contact)

    def add_contact(self, contact):
        """
        append a Contact object to the list
        """
        assert isinstance(contact, Contact)
        assert contact not in self._contacts
        self._contacts.append(contact)

    def get_contacts(self):
        def compare_contact(a):
            if not a.get_highest_priority_resource():
                return 0
            show = a.get_highest_priority_resource().get_presence()
            if show not in PRESENCE_PRIORITY:
                return 5
            return PRESENCE_PRIORITY[show]
        return sorted(self._contacts, key=compare_contact, reverse=True)

    def toggle_folded(self):
        self.folded = not self.folded

    def __repr__(self):
        return '<Roster_group: %s; %s>' % (self.name, self._contacts)

    def __len__(self):
        return len(self._contacts)

    def get_nb_connected_contacts(self):
        l = 0
        for contact in self._contacts:
            if contact.get_highest_priority_resource():
                l += 1
        return l