pidgin/pidgincredentialproviderstore.c

changeset 40730
12b38cca63d7
child 40743
7aa6153abf4d
equal deleted inserted replaced
40729:23985cfb6b99 40730:12b38cca63d7
1 /*
2 * Pidgin - Internet Messenger
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
4 *
5 * Pidgin is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23 #include <glib/gi18n-lib.h>
24
25 #include "pidgin/pidgincredentialproviderstore.h"
26
27 struct _PidginCredentialProviderStore {
28 GtkListStore parent;
29 };
30
31 /******************************************************************************
32 * Helpers
33 *****************************************************************************/
34 static void
35 pidgin_credential_provider_store_add(PidginCredentialProviderStore *store,
36 PurpleCredentialProvider *provider)
37 {
38 GtkTreeIter iter;
39 const gchar *id = NULL, *name = NULL;
40
41 id = purple_credential_provider_get_id(provider);
42 name = purple_credential_provider_get_name(provider);
43
44 gtk_list_store_append(GTK_LIST_STORE(store), &iter);
45 gtk_list_store_set(
46 GTK_LIST_STORE(store),
47 &iter,
48 PIDGIN_CREDENTIAL_PROVIDER_STORE_COLUMN_ID, id,
49 PIDGIN_CREDENTIAL_PROVIDER_STORE_COLUMN_NAME, name,
50 -1
51 );
52 }
53
54 static void
55 pidgin_credential_provider_store_add_helper(PurpleCredentialProvider *provider,
56 gpointer data)
57 {
58 pidgin_credential_provider_store_add(PIDGIN_CREDENTIAL_PROVIDER_STORE(data),
59 provider);
60 }
61
62 static void
63 pidgin_credential_provider_store_add_providers(PidginCredentialProviderStore *store)
64 {
65 PurpleCredentialManager *manager = NULL;
66
67 manager = purple_credential_manager_get_default();
68
69 purple_credential_manager_foreach_provider(manager,
70 pidgin_credential_provider_store_add_helper,
71 store);
72 }
73
74 static void
75 pidgin_credential_provider_store_remove(PidginCredentialProviderStore *store,
76 PurpleCredentialProvider *provider)
77 {
78 GtkTreeIter iter;
79 const gchar *id = NULL;
80
81 id = purple_credential_provider_get_id(provider);
82
83 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter) == FALSE) {
84 purple_debug_warning("credential-provider-store",
85 "asked to remove provider %s but the store is "
86 "empty",
87 id);
88 return;
89 }
90
91 /* walk through the store and look for the provider to remove */
92 do {
93 gchar *found = NULL;
94
95 gtk_tree_model_get(
96 GTK_TREE_MODEL(store),
97 &iter,
98 PIDGIN_CREDENTIAL_PROVIDER_STORE_COLUMN_ID, &found,
99 -1
100 );
101
102 if(purple_strequal(found, id)) {
103 g_free(found);
104
105 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
106
107 return;
108 }
109
110 g_free(found);
111 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
112 }
113
114 /******************************************************************************
115 * Callbacks
116 *****************************************************************************/
117 static void
118 purple_credential_provider_store_registered_cb(PurpleCredentialManager *manager,
119 PurpleCredentialProvider *provider,
120 gpointer data)
121 {
122 pidgin_credential_provider_store_add(PIDGIN_CREDENTIAL_PROVIDER_STORE(data),
123 provider);
124 }
125
126 static void
127 purple_credential_provider_store_unregistered_cb(PurpleCredentialManager *manager,
128 PurpleCredentialProvider *provider,
129 gpointer data)
130 {
131 pidgin_credential_provider_store_remove(PIDGIN_CREDENTIAL_PROVIDER_STORE(data),
132 provider);
133 }
134
135 /******************************************************************************
136 * GObject Implementation
137 *****************************************************************************/
138 G_DEFINE_TYPE(PidginCredentialProviderStore, pidgin_credential_provider_store, GTK_TYPE_LIST_STORE)
139
140 static void
141 pidgin_credential_provider_store_init(PidginCredentialProviderStore *store) {
142 PurpleCredentialManager *manager = NULL;
143 GType types[PIDGIN_CREDENTIAL_PROVIDER_STORE_N_COLUMNS] = {
144 G_TYPE_STRING,
145 G_TYPE_STRING,
146 };
147
148 gtk_list_store_set_column_types(
149 GTK_LIST_STORE(store),
150 PIDGIN_CREDENTIAL_PROVIDER_STORE_N_COLUMNS,
151 types
152 );
153
154 /* add the known providers */
155 pidgin_credential_provider_store_add_providers(store);
156
157 manager = purple_credential_manager_get_default();
158
159 g_signal_connect(G_OBJECT(manager), "provider-registered",
160 G_CALLBACK(purple_credential_provider_store_registered_cb),
161 store);
162 g_signal_connect(G_OBJECT(manager), "provider-unregistered",
163 G_CALLBACK(purple_credential_provider_store_unregistered_cb),
164 store);
165 }
166
167 static void
168 pidgin_credential_provider_store_class_init(PidginCredentialProviderStoreClass *klass) {
169 }
170
171 /******************************************************************************
172 * API
173 *****************************************************************************/
174 GtkListStore *
175 pidgin_credential_provider_store_new(void) {
176 return GTK_LIST_STORE(g_object_new(PIDGIN_TYPE_CREDENTIAL_PROVIDER_STORE,
177 NULL));
178 }

mercurial