summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/contact.py
blob: 2b08f9a60c11bf8da6e9c484234a976405155686 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import kolabformat
import datetime
import pytz
import base64

from pykolab.xml import utils as xmlutils
from pykolab.xml.utils import ustr

def contact_from_vcard(string):
    # TODO: implement this
    pass

def contact_from_string(string):
    _xml = kolabformat.readContact(string, False)
    return Contact(_xml)

def contact_from_message(message):
    contact = None
    if message.is_multipart():
        for part in message.walk():
            if part.get_content_type() == "application/vcard+xml":
                payload = part.get_payload(decode=True)
                contact = contact_from_string(payload)

            # append attachment parts to Contact object
            elif contact and part.has_key('Content-ID'):
                contact._attachment_parts.append(part)

    return contact


class Contact(kolabformat.Contact):
    type = 'contact'

    related_map = {
        'manager': kolabformat.Related.Manager,
        'assistant': kolabformat.Related.Assistant,
        'spouse': kolabformat.Related.Spouse,
        'children': kolabformat.Related.Child,
        None: kolabformat.Related.NoRelation,
    }

    addresstype_map = {
        'home': kolabformat.Address.Home,
        'work': kolabformat.Address.Work,
    }

    phonetype_map = {
        'home':    kolabformat.Telephone.Home,
        'work':    kolabformat.Telephone.Work,
        'text':    kolabformat.Telephone.Text,
        'main':    kolabformat.Telephone.Voice,
        'homefax': kolabformat.Telephone.Fax,
        'workfax': kolabformat.Telephone.Fax,
        'mobile':  kolabformat.Telephone.Cell,
        'video':   kolabformat.Telephone.Video,
        'pager':   kolabformat.Telephone.Pager,
        'car':     kolabformat.Telephone.Car,
        'other':   kolabformat.Telephone.Textphone,
    }

    emailtype_map = {
        'home': kolabformat.Email.Home,
        'work': kolabformat.Email.Work,
        'other': kolabformat.Email.Work,
    }

    urltype_map = {
        'homepage': kolabformat.Url.NoType,
        'blog': kolabformat.Url.Blog,
    }

    keytype_map = {
        'pgp': kolabformat.Key.PGP,
        'pkcs7': kolabformat.Key.PKCS7_MIME,
        None: kolabformat.Key.Invalid,
    }

    gender_map = {
        'female': kolabformat.Contact.Female,
        'male': kolabformat.Contact.Male,
        None: kolabformat.Contact.NotSet,
    }

    properties_map = {
        'uid':               'get_uid',
        'lastmodified-date': 'get_lastmodified',
        'fn':                'name',
        'nickname':          'nickNames',
        'title':             'titles',
        'email':             'emailAddresses',
        'tel':               'telephones',
        'url':               'urls',
        'im':                'imAddresses',
        'address':           'addresses',
        'note':              'note',
        'freebusyurl':       'freeBusyUrl',
        'birthday':          'bDay',
        'anniversary':       'anniversary',
        'categories':        'categories',
        'lang':              'languages',
        'gender':            'get_gender',
        'gpspos':            'gpsPos',
        'key':               'keys',
    }

    def __init__(self, *args, **kw):
        self._attachment_parts = []
        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_lastmodified(self):
        try:
            _datetime = self.lastModified()
            if _datetime == None or not _datetime.isValid():
                self.__str__()
        except:
            return datetime.datetime.now(pytz.utc)

        return xmlutils.from_cdatetime(self.lastModified(), True)

    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(ustr(name))

    def get_gender(self, translated=True):
        _gender = self.gender()
        if translated:
            return self._translate_value(_gender, self.gender_map)
        return _gender

    def _translate_value(self, val, map):
        name_map = dict([(v, k) for (k, v) in map.iteritems()])
        return name_map[val] if name_map.has_key(val) else 'UNKNOWN'

    def to_dict(self):
        if not self.isValid():
            return None

        data = self._names2dict(self.nameComponents())

        for p, getter in self.properties_map.iteritems():
            val = None
            if hasattr(self, getter):
                val = getattr(self, getter)()
            if isinstance(val, kolabformat.cDateTime):
                val = xmlutils.from_cdatetime(val, True)
            elif isinstance(val, kolabformat.vectori):
                val = [int(x) for x in val]
            elif isinstance(val, kolabformat.vectors):
                val = [str(x) for x in val]
            elif isinstance(val, kolabformat.vectortelephone):
                val = [self._struct2dict(x, 'number', self.phonetype_map) for x in val]
            elif isinstance(val, kolabformat.vectoremail):
                val = [self._struct2dict(x, 'address', self.emailtype_map) for x in val]
            elif isinstance(val, kolabformat.vectorurl):
                val = [self._struct2dict(x, 'url', self.urltype_map) for x in val]
            elif isinstance(val, kolabformat.vectorkey):
                val = [self._struct2dict(x, 'key', self.keytype_map) for x in val]
            elif isinstance(val, kolabformat.vectoraddress):
                val = [self._address2dict(x) for x in val]
            elif isinstance(val, kolabformat.vectorgeo):
                val = [[x.latitude, x.longitude] for x in val]

            if val is not None:
                data[p] = val

        affiliations = self.affiliations()
        if len(affiliations) > 0:
            _affiliation = self._affiliation2dict(affiliations[0])
            if _affiliation.has_key('address'):
                data['address'].extend(_affiliation['address'])
                _affiliation.pop('address', None)
            data.update(_affiliation)

        data.update(self._relateds2dict(self.relateds()))

        if self.photoMimetype():
            data['photo'] = dict(mimetype=self.photoMimetype(), base64=base64.b64encode(self.photo()))
        elif self.photo():
            data['photo'] = dict(uri=self.photo())

        return data

    def _names2dict(self, namecomp):
        names_map = {
            'surname':    'surnames',
            'given':      'given',
            'additional': 'additional',
            'prefix':     'prefixes',
            'suffix':     'suffixes',
        }

        data = dict()

        for p, getter in names_map.iteritems():
            val = None
            if hasattr(namecomp, getter):
                val = getattr(namecomp, getter)()
            if isinstance(val, kolabformat.vectors):
                val = [str(x) for x in val][0] if len(val) > 0 else None
            if val is not None:
                data[p] = val

        return data

    def _affiliation2dict(self, affiliation):
        props_map = {
            'organization': 'organisation',
            'department':   'organisationalUnits',
            'role':         'roles',
        }

        data = dict()

        for p, getter in props_map.iteritems():
            val = None
            if hasattr(affiliation, getter):
                val = getattr(affiliation, getter)()
            if isinstance(val, kolabformat.vectors):
                val = [str(x) for x in val][0] if len(val) > 0 else None
            if val is not None:
                data[p] = val

        data.update(self._relateds2dict(affiliation.relateds(), True))

        addresses = affiliation.addresses()
        if len(addresses):
            data['address'] = [self._address2dict(adr, 'office') for adr in addresses]

        return data

    def _address2dict(self, adr, adrtype=None):
        props_map = {
            'label':    'label',
            'street':   'street',
            'locality': 'locality',
            'region':   'region',
            'code':     'code',
            'country':  'country',
        }
        addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.iteritems()])

        data = dict()

        if adrtype is None:
            adrtype = addresstype_map.get(adr.types(), None)

        if adrtype is not None:
            data['type'] = adrtype

        for p, getter in props_map.iteritems():
            val = None
            if hasattr(adr, getter):
                val = getattr(adr, getter)()
            if isinstance(val, kolabformat.vectors):
                val = [str(x) for x in val][0] if len(val) > 0 else None
            if val is not None:
                data[p] = val

        return data

    def _relateds2dict(self, relateds, aslist=True):
        data = dict()

        related_map = dict([(v, k) for (k, v) in self.related_map.iteritems()])
        for rel in relateds:
            reltype = related_map.get(rel.relationTypes(), None)
            val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text()
            if reltype and val is not None:
                if aslist:
                    if not data.has_key(reltype):
                        data[reltype] = []
                    data[reltype].append(val)
                else:
                    data[reltype] = val

        return data

    def _struct2dict(self, struct, propname, map):
        type_map = dict([(v, k) for (k, v) in map.iteritems()])
        result = dict()

        if hasattr(struct, 'types'):
            result['type'] = type_map.get(struct.types(), None)
        elif hasattr(struct, 'type'):
            result['type'] = type_map.get(struct.type(), None)

        if hasattr(struct, propname):
            result[propname] = getattr(struct, propname)()

        return result

    def __str__(self):
        xml = kolabformat.writeContact(self)
        error = kolabformat.error()

        if error == None or not error:
            return xml
        else:
            raise ContactIntegrityError, kolabformat.errorMessage()


class ContactIntegrityError(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)