Initial Buddy class reimplementation.
[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_name(self):
48         if self.__exists:
49             return <char *> blist.purple_buddy_get_name(self._get_structure())
50         else:
51             return self.__name
52     name = property(__get_name)
53
54     def __get_account(self):
55         if self.__exists:
56             return self.__account
57         else:
58             return None
59     account = property(__get_account)
60
61     def __get_alias(self):
62         cdef char *c_alias = NULL
63         c_alias = <char *> blist.purple_buddy_get_alias_only( \
64                 self._get_structure())
65         if c_alias:
66             return c_alias
67         else:
68             return None
69     alias = property(__get_alias)
70
71     def __get_server_alias(self):
72         cdef char *c_server_alias = NULL
73         c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
74                 self._get_structure())
75         if c_server_alias:
76             return c_server_alias
77         else:
78             return None
79     server_alias = property(__get_server_alias)
80
81     def __get_contact_alias(self):
82         cdef char *c_contact_alias = NULL
83         c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
84                 self._get_structure())
85         if c_contact_alias:
86             return c_contact_alias
87         else:
88             return None
89     contact_alias = property(__get_contact_alias)
90
91     def __get_local_alias(self):
92         cdef char *c_local_alias = NULL
93         c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
94                 self._get_structure())
95         if c_local_alias:
96             return c_local_alias
97         else:
98             return None
99     local_alias = property(__get_local_alias)
100
101     def __get_available(self):
102         if self.__exists:
103             return status.purple_presence_is_available( \
104                     blist.purple_buddy_get_presence(self._get_structure()))
105         else:
106             return None
107     available = property(__get_available)
108
109     def __get_online(self):
110         if self.__exists:
111             return status.purple_presence_is_online( \
112                     blist.purple_buddy_get_presence(self._get_structure()))
113         else:
114             return None
115     online = property(__get_online)
116
117     def __get_idle(self):
118         if self.__exists:
119             return status.purple_presence_is_idle( \
120                     blist.purple_buddy_get_presence(self._get_structure()))
121         else:
122             return None
123     idle = property(__get_idle)
124
125     def new(self, alias=None):
126         """
127         Created a new buddy.
128
129         @param alias (optional)
130         @return True if successful, False if buddy already exists
131         """
132         cdef char *c_alias = NULL
133
134         if alias:
135             c_alias = alias
136         else:
137             c_alias = NULL
138
139         if self.__exists:
140             return False
141         else:
142             blist.purple_buddy_new(account.purple_accounts_find( \
143                     self.__account.username, self.__account.protocol.id), \
144                     self.__name, c_alias)