summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/contact.py
blob: 1577b585adf0e80dfd1a2c0292a7cdd2d0199e34 (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
import kolabformat

class Contact(kolabformat.Contact):
    def __init__(self, *args, **kw):
        kolabformat.Contact.__init__(self, *args, **kw)

    def get_uid(self):
        uid = self.uid()
        if not uid == '':
            return uid
        else:
            self.__str__()
            return kolabformat.getSerializedUID()

    def get_email(self, preferred=True):
        if preferred:
            return self.emailAddresses()[self.emailAddressPreferredIndex()]
        else:
            return [x for x in self.emailAddresses()]

    def set_email(self, email, preferred_index=0):
        if isinstance(email, basestring):
            self.setEmailAddresses([email], preferred_index)
        else:
            self.setEmailAddresses(email, preferred_index)

    def add_email(self, email):
        if isinstance(email, basestring):
            self.add_emails([email])
        elif isinstance(email, list):
            self.add_emails(email)

    def add_emails(self, emails):
        preferred_email = self.get_email()
        emails = [x for x in set(self.get_email(preferred=False) + emails)]
        preferred_email_index = emails.index(preferred_email)
        self.setEmailAddresses(emails, preferred_email_index)

    def set_name(self, name):
        self.setName(name)

    def __str__(self):
        return kolabformat.writeContact(self)