Fixing to use active status when account is idle
[python-purple] / buddy.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 cimport purple
21
22 cdef class Buddy:
23     """
24     Buddy class
25     @param name
26     @param account
27     """
28
29     cdef object __account
30     cdef object __name
31     cdef object __exists
32
33     def __init__(self, name, account):
34         self.__name = name
35         self.__account = account
36
37         if self._get_structure() != NULL:
38             self.__exists = True
39         else:
40             self.__exists = False
41
42     cdef blist.PurpleBuddy *_get_structure(self):
43         return blist.purple_find_buddy(account.purple_accounts_find( \
44                 self.__account.username, self.__account.protocol.id), \
45                 self.__name)
46
47     def __get_exists(self):
48         return self.__exists
49     exists = property(__get_exists)
50
51     def __get_name(self):
52         if self.__exists:
53             return <char *> blist.purple_buddy_get_name(self._get_structure())
54         else:
55             return self.__name
56     name = property(__get_name)
57
58     def __get_account(self):
59         if self.__exists:
60             return self.__account
61         else:
62             return None
63     account = property(__get_account)
64
65     def __get_alias(self):
66         cdef char *c_alias = NULL
67         c_alias = <char *> blist.purple_buddy_get_alias_only( \
68                 self._get_structure())
69         if c_alias:
70             return unicode(c_alias, 'utf-8')
71         else:
72             return None
73     alias = property(__get_alias)
74
75     def __get_group(self):
76         cdef blist.PurpleGroup *c_group = NULL
77         if self.__exists:
78             c_group = blist.purple_buddy_get_group(self._get_structure())
79             return <char *> blist.purple_group_get_name(c_group)
80         else:
81             return None
82     group = property(__get_group)
83
84     def __get_server_alias(self):
85         cdef char *c_server_alias = NULL
86         c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
87                 self._get_structure())
88         if c_server_alias:
89             return c_server_alias
90         else:
91             return None
92     server_alias = property(__get_server_alias)
93
94     def __get_contact_alias(self):
95         cdef char *c_contact_alias = NULL
96         c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
97                 self._get_structure())
98         if c_contact_alias:
99             return c_contact_alias
100         else:
101             return None
102     contact_alias = property(__get_contact_alias)
103
104     def __get_local_alias(self):
105         cdef char *c_local_alias = NULL
106         c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
107                 self._get_structure())
108         if c_local_alias:
109             return c_local_alias
110         else:
111             return None
112     local_alias = property(__get_local_alias)
113
114     def __get_available(self):
115         if self.__exists:
116             return status.purple_presence_is_available( \
117                     blist.purple_buddy_get_presence(self._get_structure()))
118         else:
119             return None
120     available = property(__get_available)
121
122     def __get_online(self):
123         if self.__exists:
124             return status.purple_presence_is_online( \
125                     blist.purple_buddy_get_presence(self._get_structure()))
126         else:
127             return None
128     online = property(__get_online)
129
130     def __get_idle(self):
131         if self.__exists:
132             return status.purple_presence_is_idle( \
133                     blist.purple_buddy_get_presence(self._get_structure()))
134         else:
135             return None
136     idle = property(__get_idle)
137
138     def __get_active_status(self):
139         cdef status.PurpleStatus* c_status = NULL
140         cdef char *type = NULL
141         cdef char *name = NULL
142         cdef char *msg = NULL
143         if self.__exists:
144             active = {}
145             c_status = status.purple_presence_get_active_status( \
146                     blist.purple_buddy_get_presence(self._get_structure()))
147             type = <char *> status.purple_status_get_id(c_status)
148             name = <char *> status.purple_status_get_name(c_status)
149             msg = <char *> status.purple_status_get_attr_string(c_status,
150                 "message")
151
152             active['type'] = type
153             active['name'] = name
154             if msg:
155                 active['message'] = msg
156
157             return active
158         else:
159             return None
160     active_status = property(__get_active_status)
161
162     def set_alias(self, alias):
163         if self.__exists:
164             blist.purple_blist_alias_buddy(self._get_structure(), alias)
165             return True
166         else:
167             return False
168
169     def set_group(self, group):
170         cdef blist.PurpleContact *c_contact = NULL
171         cdef blist.PurpleGroup *c_group = NULL
172         if self.__exists and group:
173             c_group = blist.purple_find_group(group)
174             if c_group == NULL:
175                 c_group = blist.purple_group_new(group)
176
177             c_contact = blist.purple_buddy_get_contact(self._get_structure())
178             blist.purple_blist_add_contact(c_contact, c_group, NULL)
179             return True
180         else:
181             return False