pidgin/pidginaccountstore.c

changeset 41864
6f490dec468f
parent 41863
0067a0ff5b74
child 41865
41e66d907bc9
equal deleted inserted replaced
41863:0067a0ff5b74 41864:6f490dec468f
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/pidginaccountstore.h"
26
27 #include "gtkutils.h"
28
29 struct _PidginAccountStore {
30 GtkListStore parent;
31 };
32
33 /******************************************************************************
34 * Helpers
35 *****************************************************************************/
36 static void
37 pidgin_account_store_add_account(PidginAccountStore *store,
38 PurpleAccount *account)
39 {
40 PurpleProtocol *protocol = NULL;
41 GtkTreeIter iter;
42 gchar *markup = NULL;
43 const gchar *alias = NULL, *icon_name = NULL;
44
45 protocol = purple_account_get_protocol(account);
46 icon_name = purple_protocol_get_icon_name(protocol);
47
48 alias = purple_account_get_private_alias(account);
49 if(alias != NULL) {
50 markup = g_strdup_printf(_("%s (%s) (%s)"),
51 purple_account_get_username(account),
52 alias,
53 purple_account_get_protocol_name(account));
54 } else {
55 markup = g_strdup_printf(_("%s (%s)"),
56 purple_account_get_username(account),
57 purple_account_get_protocol_name(account));
58 }
59
60 gtk_list_store_append(GTK_LIST_STORE(store), &iter);
61 gtk_list_store_set(
62 GTK_LIST_STORE(store),
63 &iter,
64 PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, account,
65 PIDGIN_ACCOUNT_STORE_COLUMN_MARKUP, markup,
66 PIDGIN_ACCOUNT_STORE_COLUMN_ICON_NAME, icon_name,
67 -1
68 );
69
70 g_free(markup);
71 }
72
73 static void
74 pidgin_account_store_add_account_helper(PurpleAccount *account, gpointer data) {
75 if(PURPLE_IS_ACCOUNT(account)) {
76 pidgin_account_store_add_account(PIDGIN_ACCOUNT_STORE(data),
77 PURPLE_ACCOUNT(account));
78 }
79 }
80
81 static void
82 pidgin_account_store_remove_account(PidginAccountStore *store,
83 PurpleAccount *account)
84 {
85 GtkTreeIter iter;
86
87 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter) == FALSE) {
88 purple_debug_warning("account-store",
89 "account %s was removed but not in the store",
90 purple_account_get_username(account));
91 return;
92 }
93
94 /* walk through the store and look for the account to remove */
95 do {
96 PurpleAccount *found = NULL;
97
98 gtk_tree_model_get(
99 GTK_TREE_MODEL(store),
100 &iter,
101 PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, &found,
102 -1
103 );
104
105 if(found == account) {
106 g_object_unref(G_OBJECT(found));
107
108 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
109
110 return;
111 }
112 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
113 }
114
115 /******************************************************************************
116 * Callbacks
117 *****************************************************************************/
118 static void
119 pidgin_account_store_account_added_cb(G_GNUC_UNUSED PurpleAccountManager *manager,
120 PurpleAccount *account,
121 gpointer data)
122 {
123 pidgin_account_store_add_account(PIDGIN_ACCOUNT_STORE(data), account);
124 }
125
126 static void
127 pidgin_account_store_account_removed_cb(G_GNUC_UNUSED PurpleAccountManager *manager,
128 PurpleAccount *account,
129 gpointer data)
130 {
131 pidgin_account_store_remove_account(PIDGIN_ACCOUNT_STORE(data), account);
132 }
133
134 /******************************************************************************
135 * GObject Implementation
136 *****************************************************************************/
137 G_DEFINE_TYPE(PidginAccountStore, pidgin_account_store, GTK_TYPE_LIST_STORE)
138
139 static void
140 pidgin_account_store_init(PidginAccountStore *store) {
141 PurpleAccountManager *manager = NULL;
142 GType types[PIDGIN_ACCOUNT_STORE_N_COLUMNS] = {
143 PURPLE_TYPE_ACCOUNT,
144 G_TYPE_STRING,
145 G_TYPE_STRING,
146 };
147
148 gtk_list_store_set_column_types(
149 GTK_LIST_STORE(store),
150 PIDGIN_ACCOUNT_STORE_N_COLUMNS,
151 types
152 );
153
154 /* add the known accounts */
155 manager = purple_account_manager_get_default();
156 purple_account_manager_foreach(manager,
157 pidgin_account_store_add_account_helper,
158 store);
159
160 /* add the signal handlers to dynamically add/remove accounts */
161 g_signal_connect_object(manager, "added",
162 G_CALLBACK(pidgin_account_store_account_added_cb),
163 store, 0);
164 g_signal_connect_object(manager, "removed",
165 G_CALLBACK(pidgin_account_store_account_removed_cb),
166 store, 0);
167 }
168
169 static void
170 pidgin_account_store_finalize(GObject *obj) {
171 purple_signals_disconnect_by_handle(obj);
172
173 G_OBJECT_CLASS(pidgin_account_store_parent_class)->finalize(obj);
174 }
175
176 static void
177 pidgin_account_store_class_init(PidginAccountStoreClass *klass) {
178 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
179
180 obj_class->finalize = pidgin_account_store_finalize;
181 }
182
183 /******************************************************************************
184 * API
185 *****************************************************************************/
186 GtkListStore *
187 pidgin_account_store_new(void) {
188 return GTK_LIST_STORE(g_object_new(PIDGIN_TYPE_ACCOUNT_STORE, NULL));
189 }

mercurial