Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / e2ee.c
blobed9aef3e563d977e8fdd22760523c5eb33dfe267
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "e2ee.h"
24 #include "debug.h"
26 struct _PurpleE2eeState
28 PurpleE2eeProvider *provider;
30 gchar *name;
31 gchar *stock_icon;
33 guint ref_count;
36 struct _PurpleE2eeProvider
38 gchar *name;
39 PurpleE2eeConvMenuCallback conv_menu_cb;
42 static PurpleE2eeProvider *main_provider = NULL;
44 /*** Encryption states for conversations. *************************************/
46 PurpleE2eeState *
47 purple_e2ee_state_new(PurpleE2eeProvider *provider)
49 PurpleE2eeState *state;
51 g_return_val_if_fail(provider != NULL, NULL);
53 state = g_new0(PurpleE2eeState, 1);
54 state->provider = provider;
55 state->ref_count = 1;
57 return state;
60 void
61 purple_e2ee_state_ref(PurpleE2eeState *state)
63 g_return_if_fail(state != NULL);
65 state->ref_count++;
68 PurpleE2eeState *
69 purple_e2ee_state_unref(PurpleE2eeState *state)
71 if (state == NULL)
72 return NULL;
74 state->ref_count--;
75 if (state->ref_count > 0)
76 return state;
78 g_free(state->name);
79 g_free(state->stock_icon);
80 g_free(state);
82 return NULL;
85 PurpleE2eeProvider *
86 purple_e2ee_state_get_provider(PurpleE2eeState *state)
88 g_return_val_if_fail(state != NULL, NULL);
90 return state->provider;
93 void
94 purple_e2ee_state_set_name(PurpleE2eeState *state, const gchar *name)
96 g_return_if_fail(state != NULL);
97 g_return_if_fail(name != NULL);
99 g_free(state->name);
100 state->name = g_strdup(name);
103 const gchar *
104 purple_e2ee_state_get_name(PurpleE2eeState *state)
106 g_return_val_if_fail(state != NULL, NULL);
108 return state->name;
111 void
112 purple_e2ee_state_set_stock_icon(PurpleE2eeState *state,
113 const gchar *stock_icon)
115 g_return_if_fail(state != NULL);
116 g_return_if_fail(stock_icon != NULL);
118 g_free(state->stock_icon);
119 state->stock_icon = g_strdup(stock_icon);
122 const gchar *
123 purple_e2ee_state_get_stock_icon(PurpleE2eeState *state)
125 g_return_val_if_fail(state, NULL);
127 return state->stock_icon;
130 /*** Encryption providers API. ************************************************/
132 PurpleE2eeProvider *
133 purple_e2ee_provider_new(void)
135 PurpleE2eeProvider *provider;
137 provider = g_new0(PurpleE2eeProvider, 1);
139 return provider;
142 void
143 purple_e2ee_provider_free(PurpleE2eeProvider *provider)
145 g_return_if_fail(provider != NULL);
147 if (provider == main_provider) {
148 purple_debug_error("e2ee", "This provider is still registered");
149 return;
152 g_free(provider->name);
153 g_free(provider);
156 gboolean
157 purple_e2ee_provider_register(PurpleE2eeProvider *provider)
159 g_return_val_if_fail(provider != NULL, FALSE);
161 if (main_provider != NULL)
162 return FALSE;
164 main_provider = provider;
165 return TRUE;
168 void
169 purple_e2ee_provider_unregister(PurpleE2eeProvider *provider)
171 GList *it, *clear_states = NULL;
172 g_return_if_fail(provider != NULL);
174 if (main_provider != provider) {
175 purple_debug_warning("e2ee", "This provider is not registered");
176 return;
179 for (it = purple_conversations_get_all(); it; it = g_list_next(it)) {
180 PurpleConversation *conv = it->data;
181 PurpleE2eeState *state;
183 state = purple_conversation_get_e2ee_state(conv);
184 if (!state)
185 continue;
186 if (provider == purple_e2ee_state_get_provider(state))
187 clear_states = g_list_prepend(clear_states, conv);
190 main_provider = NULL;
192 g_list_foreach(clear_states, (GFunc)purple_conversation_set_e2ee_state, NULL);
193 g_list_free(clear_states);
196 PurpleE2eeProvider *
197 purple_e2ee_provider_get_main(void)
199 return main_provider;
202 void
203 purple_e2ee_provider_set_name(PurpleE2eeProvider *provider, const gchar *name)
205 g_return_if_fail(provider != NULL);
206 g_return_if_fail(name != NULL);
208 g_free(provider->name);
209 provider->name = g_strdup(name);
212 const gchar *
213 purple_e2ee_provider_get_name(PurpleE2eeProvider *provider)
215 g_return_val_if_fail(provider != NULL, NULL);
217 return provider->name;
220 void
221 purple_e2ee_provider_set_conv_menu_cb(PurpleE2eeProvider *provider,
222 PurpleE2eeConvMenuCallback conv_menu_cb)
224 g_return_if_fail(provider != NULL);
226 provider->conv_menu_cb = conv_menu_cb;
229 PurpleE2eeConvMenuCallback
230 purple_e2ee_provider_get_conv_menu_cb(PurpleE2eeProvider *provider)
232 g_return_val_if_fail(provider != NULL, NULL);
234 return provider->conv_menu_cb;
237 GList *
238 purple_e2ee_provider_get_conv_menu_actions(PurpleE2eeProvider *provider,
239 PurpleConversation *conv)
241 PurpleE2eeConvMenuCallback cb;
243 g_return_val_if_fail(provider, NULL);
244 g_return_val_if_fail(conv, NULL);
246 cb = purple_e2ee_provider_get_conv_menu_cb(provider);
247 if (cb == NULL)
248 return NULL;
250 return cb(conv);