| |
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 <purple.h> |
| |
24 |
| |
25 #include "pidgincredentialspage.h" |
| |
26 |
| |
27 #include "pidgincredentialproviderstore.h" |
| |
28 |
| |
29 struct _PidginCredentialsPage { |
| |
30 GtkBox parent; |
| |
31 |
| |
32 GtkWidget *combo; |
| |
33 }; |
| |
34 |
| |
35 G_DEFINE_TYPE(PidginCredentialsPage, pidgin_credentials_page, |
| |
36 GTK_TYPE_BOX) |
| |
37 |
| |
38 /****************************************************************************** |
| |
39 * Helpers |
| |
40 *****************************************************************************/ |
| |
41 static void |
| |
42 pidgin_credentials_page_combo_changed_cb(GtkComboBox *widget, gpointer data) { |
| |
43 PidginCredentialsPage *page = PIDGIN_CREDENTIALS_PAGE(data); |
| |
44 GtkTreeIter iter; |
| |
45 |
| |
46 if(gtk_combo_box_get_active_iter(GTK_COMBO_BOX(page->combo), &iter)) { |
| |
47 PurpleCredentialManager *manager = NULL; |
| |
48 GError *error = NULL; |
| |
49 GtkTreeModel *model = NULL; |
| |
50 gchar *id = NULL; |
| |
51 |
| |
52 model = gtk_combo_box_get_model(GTK_COMBO_BOX(page->combo)); |
| |
53 |
| |
54 gtk_tree_model_get(model, &iter, |
| |
55 PIDGIN_CREDENTIAL_PROVIDER_STORE_COLUMN_ID, &id, |
| |
56 -1); |
| |
57 |
| |
58 manager = purple_credential_manager_get_default(); |
| |
59 if(purple_credential_manager_set_active_provider(manager, id, &error)) { |
| |
60 purple_prefs_set_string("/purple/credentials/active-provider", id); |
| |
61 |
| |
62 g_free(id); |
| |
63 |
| |
64 return; |
| |
65 } |
| |
66 |
| |
67 purple_debug_warning("credentials-page", "failed to set the active " |
| |
68 "credential provider to '%s': %s", |
| |
69 id, |
| |
70 error ? error->message : "unknown error"); |
| |
71 |
| |
72 g_free(id); |
| |
73 g_clear_error(&error); |
| |
74 } |
| |
75 } |
| |
76 |
| |
77 static void |
| |
78 pidgin_credentials_page_set_active_provider(PidginCredentialsPage *page, |
| |
79 const gchar *new_id) |
| |
80 { |
| |
81 GtkTreeIter iter; |
| |
82 GtkTreeModel *model = NULL; |
| |
83 |
| |
84 model = gtk_combo_box_get_model(GTK_COMBO_BOX(page->combo)); |
| |
85 |
| |
86 if(gtk_tree_model_get_iter_first(model, &iter)) { |
| |
87 do { |
| |
88 gchar *id = NULL; |
| |
89 |
| |
90 gtk_tree_model_get(model, &iter, |
| |
91 PIDGIN_CREDENTIAL_PROVIDER_STORE_COLUMN_ID, &id, |
| |
92 -1); |
| |
93 |
| |
94 if(purple_strequal(new_id, id)) { |
| |
95 g_signal_handlers_block_by_func(page->combo, |
| |
96 pidgin_credentials_page_combo_changed_cb, |
| |
97 page); |
| |
98 |
| |
99 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(page->combo), |
| |
100 &iter); |
| |
101 |
| |
102 g_signal_handlers_unblock_by_func(page->combo, |
| |
103 pidgin_credentials_page_combo_changed_cb, |
| |
104 page); |
| |
105 |
| |
106 g_free(id); |
| |
107 |
| |
108 return; |
| |
109 } |
| |
110 |
| |
111 g_free(id); |
| |
112 } while(gtk_tree_model_iter_next(model, &iter)); |
| |
113 } |
| |
114 } |
| |
115 |
| |
116 static void |
| |
117 pidgin_credentials_page_active_provider_changed_cb(const gchar *name, |
| |
118 PurplePrefType type, |
| |
119 gconstpointer value, |
| |
120 gpointer data) |
| |
121 { |
| |
122 PidginCredentialsPage *page = PIDGIN_CREDENTIALS_PAGE(data); |
| |
123 |
| |
124 pidgin_credentials_page_set_active_provider(page, (const gchar *)value); |
| |
125 } |
| |
126 |
| |
127 /****************************************************************************** |
| |
128 * GObjectImplementation |
| |
129 *****************************************************************************/ |
| |
130 static void |
| |
131 pidgin_credentials_page_finalize(GObject *obj) { |
| |
132 purple_prefs_disconnect_by_handle(obj); |
| |
133 |
| |
134 G_OBJECT_CLASS(pidgin_credentials_page_parent_class)->finalize(obj); |
| |
135 } |
| |
136 |
| |
137 static void |
| |
138 pidgin_credentials_page_init(PidginCredentialsPage *page) { |
| |
139 const gchar *active = NULL; |
| |
140 |
| |
141 gtk_widget_init_template(GTK_WIDGET(page)); |
| |
142 |
| |
143 purple_prefs_add_none("/purple/credentials"); |
| |
144 purple_prefs_add_string("/purple/credentials/active-provider", NULL); |
| |
145 |
| |
146 purple_prefs_connect_callback(page, "/purple/credentials/active-provider", |
| |
147 pidgin_credentials_page_active_provider_changed_cb, |
| |
148 page); |
| |
149 |
| |
150 g_signal_connect(G_OBJECT(page->combo), "changed", |
| |
151 G_CALLBACK(pidgin_credentials_page_combo_changed_cb), |
| |
152 page); |
| |
153 |
| |
154 active = purple_prefs_get_string("/purple/credentials/active-provider"); |
| |
155 if(active != NULL) { |
| |
156 pidgin_credentials_page_set_active_provider(page, active); |
| |
157 } |
| |
158 } |
| |
159 |
| |
160 static void |
| |
161 pidgin_credentials_page_class_init(PidginCredentialsPageClass *klass) { |
| |
162 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
| |
163 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| |
164 |
| |
165 obj_class->finalize = pidgin_credentials_page_finalize; |
| |
166 |
| |
167 gtk_widget_class_set_template_from_resource( |
| |
168 widget_class, |
| |
169 "/im/pidgin/Pidgin/Prefs/credentials.ui" |
| |
170 ); |
| |
171 |
| |
172 gtk_widget_class_bind_template_child(widget_class, PidginCredentialsPage, |
| |
173 combo); |
| |
174 } |
| |
175 |
| |
176 /****************************************************************************** |
| |
177 * API |
| |
178 *****************************************************************************/ |
| |
179 GtkWidget * |
| |
180 pidgin_credentials_page_new(void) { |
| |
181 return GTK_WIDGET(g_object_new(PIDGIN_TYPE_CREDENTIALS_PAGE, NULL)); |
| |
182 } |