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