Fixing UnicodeDecodeError in buddy's alias
[python-purple] / protocol.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 Protocol:
23     """
24     Protocol class
25     @param id
26     """
27     cdef object __id
28     cdef object __exists
29
30     def __init__(self, id):
31         self.__id = id
32
33         if self._get_structure() != NULL:
34             self.__exists = True
35         else:
36             self.__exists = False
37
38     cdef plugin.PurplePlugin *_get_structure(self):
39         return plugin.purple_plugins_find_with_id(self.__id)
40
41     def __get_exists(self):
42         return self.__exists
43     exists = property(__get_exists)
44
45     def __get_id(self):
46         return self.__id
47     id = property(__get_id)
48
49     def __get_name(self):
50         cdef char *name = NULL
51         if self.__exists:
52             name = <char *> plugin.purple_plugin_get_name(self._get_structure())
53             if name != NULL:
54                 return name
55             else:
56                 return None
57         return None
58     name = property(__get_name)
59
60     def __get_options_labels(self):
61         cdef prpl.PurplePluginProtocolInfo *prpl_info
62         cdef glib.GList *iter
63         cdef accountopt.PurpleAccountOption *option
64         cdef prefs.PurplePrefType type
65         cdef const_char *label_name
66         cdef const_char *setting
67
68         if not self.__exists:
69             return None
70
71         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(self._get_structure())
72
73         po = {}
74
75         iter = prpl_info.protocol_options
76
77         while iter:
78
79             option = <accountopt.PurpleAccountOption *> iter.data
80             type = accountopt.purple_account_option_get_type(option)
81             label_name = accountopt.purple_account_option_get_text(option)
82             setting = accountopt.purple_account_option_get_setting(option)
83
84             sett = str(<char *> setting)
85             label = str(<char *> label_name)
86
87             iter = iter.next
88
89             po[sett] = label
90
91         return po
92     options_labels = property(__get_options_labels)
93
94     def __get_options_values(self):
95         cdef prpl.PurplePluginProtocolInfo *prpl_info
96         cdef glib.GList *iter
97         cdef accountopt.PurpleAccountOption *option
98         cdef prefs.PurplePrefType type
99         cdef const_char *str_value
100         cdef const_char *setting
101         cdef int int_value
102         cdef glib.gboolean bool_value
103
104         if not self.__exists:
105             return None
106
107         prpl_info = plugin.PURPLE_PLUGIN_PROTOCOL_INFO(self._get_structure())
108
109         po = {}
110
111         iter = prpl_info.protocol_options
112
113         while iter:
114
115             option = <accountopt.PurpleAccountOption *> iter.data
116             type = accountopt.purple_account_option_get_type(option)
117             setting = accountopt.purple_account_option_get_setting(option)
118
119             sett = str(<char *> setting)
120
121             if type == prefs.PURPLE_PREF_STRING:
122                 str_value = accountopt.purple_account_option_get_default_string(option)
123                 # Google Talk default domain hackery!
124                 if str_value == NULL and sett == "connect_server":
125                     str_value = "talk.google.com"
126
127                 val = str(<char *> str_value)
128
129             elif type == prefs.PURPLE_PREF_INT:
130                 int_value = accountopt.purple_account_option_get_default_int(option)
131                 if sett == "port":
132                         int_value = int(443)
133
134                 val = int(int_value)
135
136             elif type == prefs.PURPLE_PREF_BOOLEAN:
137                 bool_value = accountopt.purple_account_option_get_default_bool(option)
138
139                 val = bool(bool_value)
140
141             elif type == prefs.PURPLE_PREF_STRING_LIST:
142                 str_value = accountopt.purple_account_option_get_default_list_value(option)
143
144                 val = str(<char *> str_value)
145
146             iter = iter.next
147
148             po[sett] = val
149
150         return po
151     options_values = property(__get_options_values)