Merged in default (pull request #594)
[pidgin-git.git] / pidgin / pidginaccountchooser.c
blob174229702301ed651d2f75676fe002fc6fcd0138
1 /* pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "pidgin.h"
24 #include "gtkutils.h"
25 #include "pidginaccountchooser.h"
27 /******************************************************************************
28 * Enums
29 *****************************************************************************/
31 enum
33 AOP_ICON_COLUMN,
34 AOP_NAME_COLUMN,
35 AOP_DATA_COLUMN,
36 AOP_COLUMN_COUNT
39 /******************************************************************************
40 * Structs
41 *****************************************************************************/
43 struct _PidginAccountChooser {
44 GtkComboBox parent;
46 GtkListStore *model;
48 PurpleFilterAccountFunc filter_func;
49 gboolean show_all;
52 /******************************************************************************
53 * Code
54 *****************************************************************************/
55 G_DEFINE_TYPE(PidginAccountChooser, pidgin_account_chooser, GTK_TYPE_COMBO_BOX)
57 static gpointer
58 account_chooser_get_selected(PidginAccountChooser *chooser)
60 gpointer data = NULL;
61 GtkTreeIter iter;
63 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(chooser), &iter)) {
64 gtk_tree_model_get(
65 gtk_combo_box_get_model(GTK_COMBO_BOX(chooser)), &iter,
66 AOP_DATA_COLUMN, &data, -1);
69 return data;
72 static void
73 account_chooser_select_by_data(GtkWidget *chooser, gpointer data)
75 GtkTreeModel *model;
76 GtkTreeIter iter;
77 gpointer iter_data;
78 model = gtk_combo_box_get_model(GTK_COMBO_BOX(chooser));
79 if (gtk_tree_model_get_iter_first(model, &iter)) {
80 do {
81 gtk_tree_model_get(model, &iter, AOP_DATA_COLUMN,
82 &iter_data, -1);
83 if (iter_data == data) {
84 gtk_combo_box_set_active_iter(
85 GTK_COMBO_BOX(chooser), &iter);
86 return;
88 } while (gtk_tree_model_iter_next(model, &iter));
92 static void
93 set_account_menu(PidginAccountChooser *chooser, PurpleAccount *default_account)
95 PurpleAccount *account;
96 GdkPixbuf *pixbuf = NULL;
97 GList *list;
98 GList *p;
99 GtkTreeIter iter;
100 gint default_item = 0;
101 gint i;
102 gchar buf[256];
104 if (chooser->show_all) {
105 list = purple_accounts_get_all();
106 } else {
107 list = purple_connections_get_all();
110 gtk_list_store_clear(chooser->model);
111 for (p = list, i = 0; p != NULL; p = p->next, i++) {
112 if (chooser->show_all) {
113 account = (PurpleAccount *)p->data;
114 } else {
115 PurpleConnection *gc = (PurpleConnection *)p->data;
117 account = purple_connection_get_account(gc);
120 if (chooser->filter_func && !chooser->filter_func(account)) {
121 i--;
122 continue;
125 pixbuf = pidgin_create_protocol_icon(
126 account, PIDGIN_PROTOCOL_ICON_SMALL);
128 if (pixbuf) {
129 if (purple_account_is_disconnected(account) &&
130 chooser->show_all && purple_connections_get_all()) {
131 gdk_pixbuf_saturate_and_pixelate(pixbuf, pixbuf,
132 0.0, FALSE);
136 if (purple_account_get_private_alias(account)) {
137 g_snprintf(buf, sizeof(buf), "%s (%s) (%s)",
138 purple_account_get_username(account),
139 purple_account_get_private_alias(account),
140 purple_account_get_protocol_name(account));
141 } else {
142 g_snprintf(buf, sizeof(buf), "%s (%s)",
143 purple_account_get_username(account),
144 purple_account_get_protocol_name(account));
147 gtk_list_store_append(chooser->model, &iter);
148 gtk_list_store_set(chooser->model, &iter, AOP_ICON_COLUMN,
149 pixbuf, AOP_NAME_COLUMN, buf,
150 AOP_DATA_COLUMN, account, -1);
152 if (pixbuf) {
153 g_object_unref(pixbuf);
156 if (default_account && account == default_account) {
157 default_item = i;
161 gtk_combo_box_set_active(GTK_COMBO_BOX(chooser), default_item);
164 static void
165 regenerate_account_menu(PidginAccountChooser *chooser)
167 PurpleAccount *account;
169 account = (PurpleAccount *)account_chooser_get_selected(chooser);
171 set_account_menu(chooser, account);
174 static void
175 account_menu_sign_on_off_cb(PurpleConnection *gc, PidginAccountChooser *chooser)
177 regenerate_account_menu(chooser);
180 static void
181 account_menu_added_removed_cb(PurpleAccount *account,
182 PidginAccountChooser *chooser)
184 regenerate_account_menu(chooser);
187 static gboolean
188 account_menu_destroyed_cb(GtkWidget *chooser, GdkEvent *event, void *user_data)
190 purple_signals_disconnect_by_handle(chooser);
192 return FALSE;
195 /******************************************************************************
196 * GObject implementation
197 *****************************************************************************/
198 static void
199 pidgin_account_chooser_class_init(PidginAccountChooserClass *klass)
201 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
203 gtk_widget_class_set_template_from_resource(
204 widget_class, "/im/pidgin/Pidgin/Accounts/chooser.ui");
206 gtk_widget_class_bind_template_child(widget_class, PidginAccountChooser,
207 model);
209 gtk_widget_class_bind_template_callback(widget_class,
210 account_menu_destroyed_cb);
213 static void
214 pidgin_account_chooser_init(PidginAccountChooser *chooser)
216 gtk_widget_init_template(GTK_WIDGET(chooser));
218 /* Register the purple sign on/off event callbacks. */
219 purple_signal_connect(
220 purple_connections_get_handle(), "signed-on", chooser,
221 PURPLE_CALLBACK(account_menu_sign_on_off_cb), chooser);
222 purple_signal_connect(
223 purple_connections_get_handle(), "signed-off", chooser,
224 PURPLE_CALLBACK(account_menu_sign_on_off_cb), chooser);
225 purple_signal_connect(
226 purple_accounts_get_handle(), "account-added", chooser,
227 PURPLE_CALLBACK(account_menu_added_removed_cb), chooser);
228 purple_signal_connect(
229 purple_accounts_get_handle(), "account-removed", chooser,
230 PURPLE_CALLBACK(account_menu_added_removed_cb), chooser);
233 /******************************************************************************
234 * Public API
235 *****************************************************************************/
236 GtkWidget *
237 pidgin_account_chooser_new(PurpleAccount *default_account, gboolean show_all)
239 PidginAccountChooser *chooser = NULL;
241 chooser = g_object_new(PIDGIN_TYPE_ACCOUNT_CHOOSER, NULL);
242 chooser->show_all = show_all;
243 set_account_menu(PIDGIN_ACCOUNT_CHOOSER(chooser), default_account);
245 return GTK_WIDGET(chooser);
248 void
249 pidgin_account_chooser_set_filter_func(PidginAccountChooser *chooser,
250 PurpleFilterAccountFunc filter_func)
252 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser));
254 chooser->filter_func = filter_func;
255 regenerate_account_menu(chooser);
258 PurpleAccount *
259 pidgin_account_chooser_get_selected(GtkWidget *chooser)
261 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser), NULL);
263 return (PurpleAccount *)account_chooser_get_selected(
264 PIDGIN_ACCOUNT_CHOOSER(chooser));
267 void
268 pidgin_account_chooser_set_selected(GtkWidget *chooser, PurpleAccount *account)
270 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser));
272 account_chooser_select_by_data(chooser, account);