summaryrefslogtreecommitdiff
path: root/sunshine/handle.py
blob: 4b965babcbca8850af99bad09ccad2eba5be5e09 (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
# telepathy-sunshine is the GaduGadu connection manager for Telepathy
#
# Copyright (C) 2007 Ali Sabil <ali.sabil@gmail.com>
# 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 weakref

import telepathy

__all__ = ['SunshineHandleFactory']

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


def SunshineHandleFactory(connection, type, *args):
    mapping = {'self': SunshineSelfHandle,
               'contact': SunshineContactHandle,
               'list': SunshineListHandle,
               'group': SunshineGroupHandle}
    handle = mapping[type](connection, *args)
    connection._handles[handle.get_type(), handle.get_id()] = handle
    return handle


class SunshineHandleMeta(type):
    def __call__(cls, connection, *args):
        obj, newly_created = cls.__new__(cls, connection, *args)
        if newly_created:
            obj.__init__(connection, connection.get_handle_id(), *args)
            logger.info("New Handle %s" % unicode(obj))
        return obj 


class SunshineHandle(telepathy.server.Handle):
    __metaclass__ = SunshineHandleMeta

    instances = weakref.WeakValueDictionary()
    def __new__(cls, connection, *args):
        key = (cls, connection._account[0], args)
        if key not in cls.instances.keys():
            instance = object.__new__(cls, connection, *args)
	    #instance = super(object, self).__init__(cls, connection, *args)
            cls.instances[key] = instance # TRICKY: instances is a weakdict
            return instance, True
        return cls.instances[key], False

    def __init__(self, connection, id, handle_type, name):
        telepathy.server.Handle.__init__(self, id, handle_type, name)
        self._conn = weakref.proxy(connection)

    def __unicode__(self):
        type_mapping = {telepathy.HANDLE_TYPE_CONTACT : 'Contact',
                telepathy.HANDLE_TYPE_ROOM : 'Room',
                telepathy.HANDLE_TYPE_LIST : 'List',
                telepathy.HANDLE_TYPE_GROUP : 'Group'}
        type_str = type_mapping.get(self.type, '')
        return "<Sunshine%sHandle id=%u name='%s'>" % \
            (type_str, self.id, self.name)

    id = property(telepathy.server.Handle.get_id)
    type = property(telepathy.server.Handle.get_type)
    name = property(telepathy.server.Handle.get_name)


class SunshineSelfHandle(SunshineHandle):
    instance = None

    def __init__(self, connection, id):
        handle_type = telepathy.HANDLE_TYPE_CONTACT
        handle_name = connection._account[0]
        self._connection = connection
        SunshineHandle.__init__(self, connection, id, handle_type, handle_name)

    @property
    def profile(self):
        #TODO: thats not required, it should be removed someday :)
        return False


class SunshineContactHandle(SunshineHandle):
    #TODO: GG using just UIN to indenrify user so we need just contact_uin instead of contact_account and contact_network)
    def __init__(self, connection, id, contact_account, contact_network):
        handle_type = telepathy.HANDLE_TYPE_CONTACT
        handle_name = str(contact_account)
        self.account = str(contact_account)
        self.network = contact_network
        self.pending_groups = set()
        self.pending_alias = None
        self._connection = connection
        SunshineHandle.__init__(self, connection, id, handle_type, handle_name)

    @property
    def contact(self):
        result = self._connection.gadu_client.get_contact(int(self.account))
        return result


class SunshineListHandle(SunshineHandle):
    def __init__(self, connection, id, list_name):
        handle_type = telepathy.HANDLE_TYPE_LIST
        handle_name = list_name
        SunshineHandle.__init__(self, connection, id, handle_type, handle_name)


class SunshineGroupHandle(SunshineHandle):
    def __init__(self, connection, id, group_name):
        handle_type = telepathy.HANDLE_TYPE_GROUP
        handle_name = group_name
        self._connection = connection
        self.handle_name = group_name
        SunshineHandle.__init__(self, connection, id, handle_type, handle_name)

    @property
    def group(self):
        for group in self._connection.gadu_client.groups:
            if group.Name == self.handle_name:
                return group
        return None