summaryrefslogtreecommitdiff
path: root/sunshine/contacts_info.py
blob: d7e00607dec63522014b1dfab1475805803dd1d6 (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
# telepathy-sunshine is the GaduGadu connection manager for Telepathy
#
# Copyright (C) 2010 Krzysztof Klinikowski <kkszysiu@gmail.com>
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import logging
import dbus
import dbus.service

from sunshine.Connection_Interface_Contact_Info import ConnectionInterfaceContactInfo

import telepathy
import telepathy.constants
import telepathy.errors

from twisted.internet.defer import Deferred

from sunshine.handle import SunshineHandleFactory
from sunshine.util.decorator import async

__all__ = ['SunshineContactInfo']

logger = logging.getLogger('Sunshine.ContactInfo')

CONNECTION_INTERFACE_CONTACT_INFO = 'org.freedesktop.Telepathy.Connection.Interface.ContactInfo'

# Contact_Info_Flag
CONTACT_INFO_FLAG_CAN_SET = 1
CONTACT_INFO_FLAG_PUSH = 2
LAST_CONTACT_INFO_FLAG = 2
# Contact_Info_Field_Flags (bitfield/set of flags, 0 for none)
CONTACT_INFO_FIELD_FLAG_PARAMETERS_MANDATORY = 1

class SunshineContactInfo(ConnectionInterfaceContactInfo):
    def __init__(self):
        logger.info('SunshineContactInfo called.')
        ConnectionInterfaceContactInfo.__init__(self)
        
        self.ggapi.onUserInfo = self.onUserInfo
        
        dbus_interface = CONNECTION_INTERFACE_CONTACT_INFO
        
        self._implement_property_get(dbus_interface, {
            'ContactInfoFlags': lambda: self.contact_info_flags,
            'SupportedFields': lambda: self.contact_info_supported_fields,
        })

    @property
    def contact_info_flags(self):
        return (CONTACT_INFO_FLAG_CAN_SET | CONTACT_INFO_FLAG_PUSH)

    @property
    def contact_info_supported_fields(self):
        return dbus.Array([
                  ('nickname', ['type=home'], 0, 1),
                  ('fn', ['type=home'], 0, 1),
                  ('label', ['type=home'], 0, 1),
                  ('bday', ['type=home'], 0, 1),
                  ('url', ['type=home', 'type=mg'], 0, 2)
                ])

    def GetContactInfo(self, contacts):
        logger.info("GetContactInfo")
        tmp = {}
        for contact in contacts:
            tmp[contact] = []
        return tmp

    def RefreshContactInfo(self, contacts):
        logger.info('RefreshContactInfo')
        for handle_id in contacts:
            handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handle_id)
            self.ggapi.getUserInfo(str(handle.name))
        pass
    
    @dbus.service.method(CONNECTION_INTERFACE_CONTACT_INFO, in_signature='u', out_signature='a(sasas)',
                         async_callbacks=('reply_handler', 'error_handler'))
    def RequestContactInfo(self, contact, reply_handler, error_handler):
        logger.info('RequestContactInfo')
        handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, contact)
        self.ggapi.getUserInfo(str(handle.name))
        #TODO: This need to be fixed as soon as possible.
        #d = Deferred()
        #d = self.ggapi.getUserInfoDeffered(str(handle.name))
        #d.addCallbacks(reply_handler, error_handler)
        #d.callback(result)
        #print 'result:', result
        #return d
        reply_handler([])
        return []
        
    def SetContactInfo(self, contactinfo):
        logger.info('SetContactInfo')
        pass
        
    def onUserInfo(self, result):
        if 'users' in result:
            for user in result['users']:
                if '_uin' in user:
                    info = []
                    if 'nick' in user:
                        if '_content' in user['nick']:
                            info.append(('nickname', ['type=home'], [user['nick']['_content']]))
                    if 'name' in user:
                        if '_content' in user['name']:
                            fn = user['name']['_content']
                            if 'surname' in user:
                                if '_content' in user['surname']:
                                    fn += ' '+user['surname']['_content']
                            info.append(('fn', ['type=home'], [fn]))
                    if 'city' in user:
                        if '_content' in user['city']:
                            info.append(('label', ['type=home'], [user['city']['_content']]))
                    if 'birth' in user:
                        if '_content' in user['birth']:
                            info.append(('bday', ['type=home'], [user['birth']['_content'][:-15]]))
                    if 'hasActiveMGProfile' in user:
                        if user['hasActiveMGProfile'] == True:
                            mg = 'https://www.mojageneracja.pl/%s' % (user['_uin'])
                            info.append(('url', ['type=mg'], [mg]))
                    if 'wwwUrl' in user:
                        if '_content' in user['wwwUrl']:
                            info.append(('url', ['type=home'], [user['wwwUrl']['_content']]))

                    handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT, str(user['_uin']))
                    handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
                    self.ContactInfoChanged(handle, info)