| |
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 <handy.h> |
| |
26 |
| |
27 #include "pidgincredentialprefs.h" |
| |
28 |
| |
29 #include "pidgincredentialproviderrow.h" |
| |
30 |
| |
31 struct _PidginCredentialPrefs { |
| |
32 HdyPreferencesPage parent; |
| |
33 |
| |
34 GtkWidget *credential_list; |
| |
35 }; |
| |
36 |
| |
37 G_DEFINE_TYPE(PidginCredentialPrefs, pidgin_credential_prefs, |
| |
38 HDY_TYPE_PREFERENCES_PAGE) |
| |
39 |
| |
40 /****************************************************************************** |
| |
41 * Helpers |
| |
42 *****************************************************************************/ |
| |
43 static void |
| |
44 pidgin_credential_prefs_create_row(PurpleCredentialProvider *provider, |
| |
45 gpointer data) |
| |
46 { |
| |
47 GtkListBox *box = GTK_LIST_BOX(data); |
| |
48 GtkWidget *row = NULL; |
| |
49 |
| |
50 row = pidgin_credential_provider_row_new(provider); |
| |
51 gtk_list_box_prepend(box, row); |
| |
52 } |
| |
53 |
| |
54 static gint |
| |
55 pidgin_credential_prefs_sort_rows(GtkListBoxRow *row1, GtkListBoxRow *row2, |
| |
56 G_GNUC_UNUSED gpointer user_data) |
| |
57 { |
| |
58 PidginCredentialProviderRow *pcprow = NULL; |
| |
59 PurpleCredentialProvider *provider = NULL; |
| |
60 const gchar *id1 = NULL; |
| |
61 gboolean is_noop1 = FALSE; |
| |
62 const gchar *id2 = NULL; |
| |
63 gboolean is_noop2 = FALSE; |
| |
64 |
| |
65 pcprow = PIDGIN_CREDENTIAL_PROVIDER_ROW(row1); |
| |
66 provider = pidgin_credential_provider_row_get_provider(pcprow); |
| |
67 id1 = purple_credential_provider_get_id(provider); |
| |
68 is_noop1 = purple_strequal(id1, "noop-provider"); |
| |
69 |
| |
70 pcprow = PIDGIN_CREDENTIAL_PROVIDER_ROW(row2); |
| |
71 provider = pidgin_credential_provider_row_get_provider(pcprow); |
| |
72 id2 = purple_credential_provider_get_id(provider); |
| |
73 is_noop2 = purple_strequal(id2, "noop-provider"); |
| |
74 |
| |
75 /* Sort None provider after everything else. */ |
| |
76 if (is_noop1 && is_noop2) { |
| |
77 return 0; |
| |
78 } else if (is_noop1 && !is_noop2) { |
| |
79 return 1; |
| |
80 } else if (!is_noop1 && is_noop2) { |
| |
81 return -1; |
| |
82 } |
| |
83 /* Sort normally by ID. */ |
| |
84 return g_strcmp0(id1, id2); |
| |
85 } |
| |
86 |
| |
87 static void |
| |
88 pidgin_credential_prefs_list_row_activated_cb(GtkListBox *box, |
| |
89 GtkListBoxRow *row, |
| |
90 G_GNUC_UNUSED gpointer data) |
| |
91 { |
| |
92 PurpleCredentialManager *manager = NULL; |
| |
93 PurpleCredentialProvider *provider = NULL; |
| |
94 const gchar *id = NULL; |
| |
95 GError *error = NULL; |
| |
96 |
| |
97 provider = pidgin_credential_provider_row_get_provider( |
| |
98 PIDGIN_CREDENTIAL_PROVIDER_ROW(row)); |
| |
99 id = purple_credential_provider_get_id(provider); |
| |
100 |
| |
101 manager = purple_credential_manager_get_default(); |
| |
102 if(purple_credential_manager_set_active(manager, id, &error)) { |
| |
103 return; |
| |
104 } |
| |
105 |
| |
106 purple_debug_warning("credential-prefs", "failed to set the active " |
| |
107 "credential provider to '%s': %s", |
| |
108 id, error ? error->message : "unknown error"); |
| |
109 |
| |
110 g_clear_error(&error); |
| |
111 } |
| |
112 |
| |
113 static void |
| |
114 pidgin_credential_prefs_set_active_provider(PidginCredentialPrefs *prefs, |
| |
115 const gchar *new_id) |
| |
116 { |
| |
117 GList *rows = NULL; |
| |
118 |
| |
119 rows = gtk_container_get_children(GTK_CONTAINER(prefs->credential_list)); |
| |
120 for (; rows; rows = g_list_delete_link(rows, rows)) { |
| |
121 PidginCredentialProviderRow *row = NULL; |
| |
122 PurpleCredentialProvider *provider = NULL; |
| |
123 const gchar *id = NULL; |
| |
124 |
| |
125 row = PIDGIN_CREDENTIAL_PROVIDER_ROW(rows->data); |
| |
126 provider = pidgin_credential_provider_row_get_provider(row); |
| |
127 id = purple_credential_provider_get_id(provider); |
| |
128 |
| |
129 pidgin_credential_provider_row_set_active(row, |
| |
130 purple_strequal(new_id, id)); |
| |
131 } |
| |
132 } |
| |
133 |
| |
134 static void |
| |
135 pidgin_credential_prefs_active_provider_changed_cb(const gchar *name, |
| |
136 PurplePrefType type, |
| |
137 gconstpointer value, |
| |
138 gpointer data) |
| |
139 { |
| |
140 PidginCredentialPrefs *prefs = PIDGIN_CREDENTIAL_PREFS(data); |
| |
141 |
| |
142 pidgin_credential_prefs_set_active_provider(prefs, (const gchar *)value); |
| |
143 } |
| |
144 |
| |
145 /****************************************************************************** |
| |
146 * GObject Implementation |
| |
147 *****************************************************************************/ |
| |
148 static void |
| |
149 pidgin_credential_prefs_finalize(GObject *obj) { |
| |
150 purple_prefs_disconnect_by_handle(obj); |
| |
151 |
| |
152 G_OBJECT_CLASS(pidgin_credential_prefs_parent_class)->finalize(obj); |
| |
153 } |
| |
154 |
| |
155 static void |
| |
156 pidgin_credential_prefs_init(PidginCredentialPrefs *prefs) { |
| |
157 PurpleCredentialManager *manager = NULL; |
| |
158 const gchar *active = NULL; |
| |
159 |
| |
160 gtk_widget_init_template(GTK_WIDGET(prefs)); |
| |
161 |
| |
162 manager = purple_credential_manager_get_default(); |
| |
163 purple_credential_manager_foreach( |
| |
164 manager, |
| |
165 pidgin_credential_prefs_create_row, |
| |
166 prefs->credential_list); |
| |
167 gtk_list_box_set_sort_func(GTK_LIST_BOX(prefs->credential_list), |
| |
168 pidgin_credential_prefs_sort_rows, NULL, NULL); |
| |
169 |
| |
170 purple_prefs_connect_callback(prefs, "/purple/credentials/active-provider", |
| |
171 pidgin_credential_prefs_active_provider_changed_cb, |
| |
172 prefs); |
| |
173 |
| |
174 active = purple_prefs_get_string("/purple/credentials/active-provider"); |
| |
175 if(active != NULL) { |
| |
176 pidgin_credential_prefs_set_active_provider(prefs, active); |
| |
177 } |
| |
178 } |
| |
179 |
| |
180 static void |
| |
181 pidgin_credential_prefs_class_init(PidginCredentialPrefsClass *klass) { |
| |
182 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
| |
183 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| |
184 |
| |
185 obj_class->finalize = pidgin_credential_prefs_finalize; |
| |
186 |
| |
187 gtk_widget_class_set_template_from_resource( |
| |
188 widget_class, |
| |
189 "/im/pidgin/Pidgin3/Prefs/credentials.ui" |
| |
190 ); |
| |
191 |
| |
192 gtk_widget_class_bind_template_child(widget_class, PidginCredentialPrefs, |
| |
193 credential_list); |
| |
194 gtk_widget_class_bind_template_callback(widget_class, |
| |
195 pidgin_credential_prefs_list_row_activated_cb); |
| |
196 } |
| |
197 |
| |
198 /****************************************************************************** |
| |
199 * API |
| |
200 *****************************************************************************/ |
| |
201 GtkWidget * |
| |
202 pidgin_credential_prefs_new(void) { |
| |
203 return GTK_WIDGET(g_object_new(PIDGIN_TYPE_CREDENTIAL_PREFS, NULL)); |
| |
204 } |