1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
27 #include <libmissioncontrol/mc-profile.h>
28 #include <libmissioncontrol/mc-protocol.h>
30 #include "empathy-profile-chooser.h"
31 #include "empathy-ui-utils.h"
34 * SECTION:empathy-profile-chooser
35 * @title: EmpathyProfileChooser
36 * @short_description: A widget used to choose from a list of profiles
37 * @include: libempathy-gtk/empathy-account-chooser.h
39 * #EmpathyProfileChooser is a widget which provides a chooser of available
51 * empathy_profile_chooser_dup_selected:
52 * @widget: an #EmpathyProfileChooser
54 * Returns a new reference to the selected #McProfile in @widget.
56 * Return value: a new reference to the selected #McProfile
59 empathy_profile_chooser_dup_selected (GtkWidget *widget)
63 McProfile *profile = NULL;
65 model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
66 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
67 gtk_tree_model_get (model, &iter,
68 COL_PROFILE, &profile,
76 * empathy_profile_chooser_n_profiles:
77 * @widget: an #EmpathyProfileChooser
79 * Returns the number of profiles in @widget.
81 * Return value: the number of profiles in @widget
84 empathy_profile_chooser_n_profiles (GtkWidget *widget)
88 model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
90 return gtk_tree_model_iter_n_children (model, NULL);
94 profile_chooser_sort_profile_value (McProfile *profile)
97 const gchar *profile_name;
98 const gchar *names[] = {"jabber",
103 profile_name = mc_profile_get_unique_name (profile);
105 for (i = 0 ; names[i]; i++) {
106 if (strcmp (profile_name, names[i]) == 0) {
115 profile_chooser_sort_func (GtkTreeModel *model,
120 McProfile *profile_a;
121 McProfile *profile_b;
124 gtk_tree_model_get (model, iter_a,
125 COL_PROFILE, &profile_a,
127 gtk_tree_model_get (model, iter_b,
128 COL_PROFILE, &profile_b,
131 cmp = profile_chooser_sort_profile_value (profile_a);
132 cmp -= profile_chooser_sort_profile_value (profile_b);
134 cmp = strcmp (mc_profile_get_display_name (profile_a),
135 mc_profile_get_display_name (profile_b));
138 g_object_unref (profile_a);
139 g_object_unref (profile_b);
145 * empathy_profile_chooser_new:
147 * Creates a new #EmpathyProfileChooser widget.
149 * Return value: a new #EmpathyProfileChooser widget
152 empathy_profile_chooser_new (void)
154 GList *profiles, *l, *seen;
156 GtkCellRenderer *renderer;
157 GtkWidget *combo_box;
159 gboolean iter_set = FALSE;
162 /* set up combo box with new store */
163 store = gtk_list_store_new (COL_COUNT,
164 G_TYPE_STRING, /* Icon name */
165 G_TYPE_STRING, /* Label */
166 MC_TYPE_PROFILE); /* Profile */
167 combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
170 renderer = gtk_cell_renderer_pixbuf_new ();
171 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
172 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
173 "icon-name", COL_ICON,
175 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
177 renderer = gtk_cell_renderer_text_new ();
178 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
179 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
183 btf_cm = mc_manager_lookup ("butterfly");
184 profiles = mc_profiles_list ();
186 for (l = profiles; l; l = l->next) {
188 McProtocol *protocol;
189 const gchar *unique_name;
193 /* Check if the CM is installed, otherwise skip that profile.
194 * Workaround SF bug #1688779 */
195 protocol = mc_profile_get_protocol (profile);
199 g_object_unref (protocol);
201 /* Skip MSN-Haze if we have butterfly */
202 unique_name = mc_profile_get_unique_name (profile);
203 if (btf_cm && strcmp (unique_name, "msn-haze") == 0) {
207 if (g_list_find_custom (seen, unique_name, (GCompareFunc) strcmp)) {
210 seen = g_list_append (seen, (char*) unique_name);
212 gtk_list_store_insert_with_values (store, &iter, 0,
213 COL_ICON, mc_profile_get_icon_name (profile),
214 COL_LABEL, mc_profile_get_display_name (profile),
215 COL_PROFILE, profile,
223 g_object_unref (btf_cm);
226 /* Set the profile sort function */
227 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
229 profile_chooser_sort_func,
231 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
236 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
239 mc_profiles_free_list (profiles);
240 g_object_unref (store);