1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Pidgin - Internet Messenger
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* Pidgin is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <glib/gi18n.h>
#include <purple.h>
#include "pidginaccountmanager.h"
#include "gtkaccount.h"
#include "pidgincore.h"
#include "pidginaccounteditor.h"
struct _PidginAccountManager {
GtkDialog parent;
GtkListStore *model;
GtkTreeSelection *selection;
GtkWidget *modify_button;
GtkWidget *remove_button;
};
enum {
RESPONSE_ADD,
RESPONSE_MODIFY,
RESPONSE_REMOVE,
RESPONSE_ADD_OLD,
RESPONSE_MODIFY_OLD
};
enum {
COLUMN_ENABLED,
COLUMN_AVATAR,
COLUMN_USERNAME,
COLUMN_PROTOCOL_ICON,
COLUMN_PROTOCOL_NAME,
COLUMN_ACCOUNT
};
G_DEFINE_TYPE(PidginAccountManager, pidgin_account_manager, GTK_TYPE_DIALOG)
static void pidgin_account_manager_account_notify_cb(GObject *obj, GParamSpec *pspec, gpointer data);
/******************************************************************************
* Helpers
*****************************************************************************/
static gboolean
pidgin_account_manager_find_account(PidginAccountManager *manager,
PurpleAccount *account, GtkTreeIter *iter)
{
GtkTreeModel *model = GTK_TREE_MODEL(manager->model);
if(!gtk_tree_model_get_iter_first(model, iter)) {
return FALSE;
}
do {
PurpleAccount *current = NULL;
gtk_tree_model_get(model, iter,
COLUMN_ACCOUNT, ¤t,
-1);
if(current == account) {
g_clear_object(¤t);
return TRUE;
}
g_clear_object(¤t);
} while(gtk_tree_model_iter_next(model, iter));
return FALSE;
}
static PurpleAccount *
pidgin_account_manager_get_selected_account(PidginAccountManager *manager) {
PurpleAccount *account = NULL;
GtkTreeIter iter;
if(gtk_tree_selection_count_selected_rows(manager->selection) == 0) {
return NULL;
}
gtk_tree_selection_get_selected(manager->selection, NULL, &iter);
gtk_tree_model_get(GTK_TREE_MODEL(manager->model), &iter,
COLUMN_ACCOUNT, &account,
-1);
return account;
}
static void
pidgin_account_manager_refresh_account(PidginAccountManager *manager,
PurpleAccount *account,
GtkTreeIter *iter)
{
PurpleImage *image = NULL;
PurpleProtocol *protocol = NULL;
GdkPixbuf *avatar = NULL;
const gchar *protocol_icon = NULL, *protocol_name = NULL;
/* Try to find the avatar for the account. */
image = purple_buddy_icons_find_account_icon(account);
if(image != NULL) {
GdkPixbuf *raw = NULL;
raw = purple_gdk_pixbuf_from_image(image);
g_object_unref(image);
avatar = gdk_pixbuf_scale_simple(raw, 22, 22, GDK_INTERP_HYPER);
g_clear_object(&raw);
}
/* Get the protocol fields. */
protocol = purple_account_get_protocol(account);
if(PURPLE_IS_PROTOCOL(protocol)) {
protocol_name = purple_protocol_get_name(protocol);
protocol_icon = purple_protocol_get_icon_name(protocol);
} else {
protocol_name = _("Unknown");
}
gtk_list_store_set(manager->model, iter,
COLUMN_ENABLED, purple_account_get_enabled(account),
COLUMN_AVATAR, avatar,
COLUMN_USERNAME, purple_account_get_username(account),
COLUMN_PROTOCOL_ICON, protocol_icon,
COLUMN_PROTOCOL_NAME, protocol_name,
COLUMN_ACCOUNT, account,
-1);
g_clear_object(&avatar);
}
static void
pidgin_account_manager_update_account(PidginAccountManager *manager,
PurpleAccount *account)
{
GtkTreeIter iter;
if(pidgin_account_manager_find_account(manager, account, &iter)) {
pidgin_account_manager_refresh_account(manager, account, &iter);
}
}
static void
pidgin_account_manager_add_account(PidginAccountManager *manager,
PurpleAccount *account)
{
GtkTreeIter iter;
gtk_list_store_append(manager->model, &iter);
pidgin_account_manager_refresh_account(manager, account, &iter);
g_signal_connect_object(account, "notify",
G_CALLBACK(pidgin_account_manager_account_notify_cb),
manager, 0);
}
static void
pidgin_account_manager_populate_helper(PurpleAccount *account, gpointer data) {
pidgin_account_manager_add_account(PIDGIN_ACCOUNT_MANAGER(data), account);
}
static void
pidgin_account_manager_populate(PidginAccountManager *manager) {
purple_account_manager_foreach(purple_account_manager_get_default(),
pidgin_account_manager_populate_helper,
manager);
}
/******************************************************************************
* Callbacks
*****************************************************************************/
static void
pidgin_account_manager_account_notify_cb(GObject *obj,
G_GNUC_UNUSED GParamSpec *pspec,
gpointer data)
{
PidginAccountManager *manager = PIDGIN_ACCOUNT_MANAGER(data);
PurpleAccount *account = PURPLE_ACCOUNT(obj);
pidgin_account_manager_update_account(manager, account);
}
static void
pidgin_account_manager_response_cb(GtkDialog *dialog, gint response_id,
G_GNUC_UNUSED gpointer data)
{
PidginAccountManager *manager = PIDGIN_ACCOUNT_MANAGER(dialog);
PurpleAccount *account = NULL;
GtkWidget *editor = NULL;
switch(response_id) {
case RESPONSE_ADD:
editor = pidgin_account_editor_new(NULL);
gtk_widget_show(editor);
break;
case RESPONSE_ADD_OLD:
pidgin_account_dialog_show(PIDGIN_ADD_ACCOUNT_DIALOG, NULL);
break;
case RESPONSE_MODIFY:
account = pidgin_account_manager_get_selected_account(manager);
editor = pidgin_account_editor_new(account);
gtk_widget_show(editor);
g_clear_object(&account);
break;
case RESPONSE_MODIFY_OLD:
account = pidgin_account_manager_get_selected_account(manager);
pidgin_account_dialog_show(PIDGIN_MODIFY_ACCOUNT_DIALOG, account);
g_clear_object(&account);
break;
case RESPONSE_REMOVE:
account = pidgin_account_manager_get_selected_account(manager);
purple_accounts_delete(account);
g_clear_object(&account);
break;
case GTK_RESPONSE_DELETE_EVENT:
/* fallthrough */
case GTK_RESPONSE_CLOSE:
gtk_window_destroy(GTK_WINDOW(dialog));
break;
default:
g_warning("not sure how you got here...");
}
}
static void
pidgin_account_manager_selection_changed_cb(GtkTreeSelection *selection,
gpointer data)
{
PidginAccountManager *manager = data;
gboolean sensitive = TRUE;
if(gtk_tree_selection_count_selected_rows(selection) == 0) {
sensitive = FALSE;
}
gtk_widget_set_sensitive(manager->modify_button, sensitive);
gtk_widget_set_sensitive(manager->remove_button, sensitive);
}
static void
pidgin_account_manager_row_activated_cb(G_GNUC_UNUSED GtkTreeView *tree_view,
GtkTreePath *path,
G_GNUC_UNUSED GtkTreeViewColumn *column,
gpointer data)
{
PidginAccountManager *manager = data;
GtkTreeIter iter;
if(gtk_tree_model_get_iter(GTK_TREE_MODEL(manager->model), &iter, path)) {
PurpleAccount *account = NULL;
gtk_tree_model_get(GTK_TREE_MODEL(manager->model), &iter,
COLUMN_ACCOUNT, &account,
-1);
pidgin_account_dialog_show(PIDGIN_MODIFY_ACCOUNT_DIALOG, account);
g_clear_object(&account);
}
}
static void
pidgin_account_manager_enable_toggled_cb(G_GNUC_UNUSED GtkCellRendererToggle *renderer,
gchar *path, gpointer data)
{
PidginAccountManager *manager = data;
GtkTreeModel *model = GTK_TREE_MODEL(manager->model);
GtkTreeIter iter;
if(gtk_tree_model_get_iter_from_string(model, &iter, path)) {
PurpleAccount *account = NULL;
gboolean enabled = FALSE;
/* The value of enabled in the model is the old value, so if enabled
* is currently set to TRUE, we are disabling the account and vice
* versa.
*/
gtk_tree_model_get(model, &iter,
COLUMN_ENABLED, &enabled,
COLUMN_ACCOUNT, &account,
-1);
/* The account was just enabled, so set its status. */
if(!enabled) {
PurpleSavedStatus *status = purple_savedstatus_get_current();
purple_savedstatus_activate_for_account(status, account);
}
purple_account_set_enabled(account, !enabled);
/* We don't update the model here, as it's updated via the notify
* signal.
*/
}
}
static void
pidgin_account_manager_account_added_cb(G_GNUC_UNUSED PurpleAccountManager *purple_manager,
PurpleAccount *account,
gpointer data)
{
PidginAccountManager *manager = data;
pidgin_account_manager_add_account(manager, account);
}
static void
pidgin_account_manager_account_removed_cb(G_GNUC_UNUSED PurpleAccountManager *purple_manager,
PurpleAccount *account,
gpointer data)
{
PidginAccountManager *manager = data;
GtkTreeIter iter;
if(pidgin_account_manager_find_account(manager, account, &iter)) {
gtk_list_store_remove(manager->model, &iter);
}
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
static void
pidgin_account_manager_init(PidginAccountManager *manager) {
PurpleAccountManager *purple_manager = NULL;
gtk_widget_init_template(GTK_WIDGET(manager));
pidgin_account_manager_populate(manager);
purple_manager = purple_account_manager_get_default();
g_signal_connect_object(purple_manager, "added",
G_CALLBACK(pidgin_account_manager_account_added_cb),
manager, 0);
g_signal_connect_object(purple_manager, "removed",
G_CALLBACK(pidgin_account_manager_account_removed_cb),
manager, 0);
}
static void
pidgin_account_manager_class_init(PidginAccountManagerClass *klass) {
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
gtk_widget_class_set_template_from_resource(
widget_class,
"/im/pidgin/Pidgin3/Accounts/manager.ui"
);
gtk_widget_class_bind_template_child(widget_class, PidginAccountManager,
model);
gtk_widget_class_bind_template_child(widget_class, PidginAccountManager,
selection);
gtk_widget_class_bind_template_child(widget_class, PidginAccountManager,
modify_button);
gtk_widget_class_bind_template_child(widget_class, PidginAccountManager,
remove_button);
gtk_widget_class_bind_template_callback(widget_class,
pidgin_account_manager_enable_toggled_cb);
gtk_widget_class_bind_template_callback(widget_class,
pidgin_account_manager_response_cb);
gtk_widget_class_bind_template_callback(widget_class,
pidgin_account_manager_row_activated_cb);
gtk_widget_class_bind_template_callback(widget_class,
pidgin_account_manager_selection_changed_cb);
}
/******************************************************************************
* Public API
*****************************************************************************/
GtkWidget *
pidgin_account_manager_new(void) {
return g_object_new(PIDGIN_TYPE_ACCOUNT_MANAGER, NULL);
}
|