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
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
25 #include "pidginaccountchooser.h"
27 /******************************************************************************
29 *****************************************************************************/
39 /******************************************************************************
41 *****************************************************************************/
43 struct _PidginAccountChooser
{
48 PurpleFilterAccountFunc filter_func
;
52 /******************************************************************************
54 *****************************************************************************/
55 G_DEFINE_TYPE(PidginAccountChooser
, pidgin_account_chooser
, GTK_TYPE_COMBO_BOX
)
58 account_chooser_get_selected(PidginAccountChooser
*chooser
)
63 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(chooser
), &iter
)) {
65 gtk_combo_box_get_model(GTK_COMBO_BOX(chooser
)), &iter
,
66 AOP_DATA_COLUMN
, &data
, -1);
73 account_chooser_select_by_data(GtkWidget
*chooser
, gpointer data
)
78 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(chooser
));
79 if (gtk_tree_model_get_iter_first(model
, &iter
)) {
81 gtk_tree_model_get(model
, &iter
, AOP_DATA_COLUMN
,
83 if (iter_data
== data
) {
84 gtk_combo_box_set_active_iter(
85 GTK_COMBO_BOX(chooser
), &iter
);
88 } while (gtk_tree_model_iter_next(model
, &iter
));
93 set_account_menu(PidginAccountChooser
*chooser
, PurpleAccount
*default_account
)
95 PurpleAccount
*account
;
96 GdkPixbuf
*pixbuf
= NULL
;
100 gint default_item
= 0;
104 if (chooser
->show_all
) {
105 list
= purple_accounts_get_all();
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
;
115 PurpleConnection
*gc
= (PurpleConnection
*)p
->data
;
117 account
= purple_connection_get_account(gc
);
120 if (chooser
->filter_func
&& !chooser
->filter_func(account
)) {
125 pixbuf
= pidgin_create_protocol_icon(
126 account
, PIDGIN_PROTOCOL_ICON_SMALL
);
129 if (purple_account_is_disconnected(account
) &&
130 chooser
->show_all
&& purple_connections_get_all()) {
131 gdk_pixbuf_saturate_and_pixelate(pixbuf
, pixbuf
,
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
));
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);
153 g_object_unref(pixbuf
);
156 if (default_account
&& account
== default_account
) {
161 gtk_combo_box_set_active(GTK_COMBO_BOX(chooser
), default_item
);
165 regenerate_account_menu(PidginAccountChooser
*chooser
)
167 PurpleAccount
*account
;
169 account
= (PurpleAccount
*)account_chooser_get_selected(chooser
);
171 set_account_menu(chooser
, account
);
175 account_menu_sign_on_off_cb(PurpleConnection
*gc
, PidginAccountChooser
*chooser
)
177 regenerate_account_menu(chooser
);
181 account_menu_added_removed_cb(PurpleAccount
*account
,
182 PidginAccountChooser
*chooser
)
184 regenerate_account_menu(chooser
);
188 account_menu_destroyed_cb(GtkWidget
*chooser
, GdkEvent
*event
, void *user_data
)
190 purple_signals_disconnect_by_handle(chooser
);
195 /******************************************************************************
196 * GObject implementation
197 *****************************************************************************/
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
,
209 gtk_widget_class_bind_template_callback(widget_class
,
210 account_menu_destroyed_cb
);
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 /******************************************************************************
235 *****************************************************************************/
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
);
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
);
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
));
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
);