Renamed empathy_profile_chooser_get_selected to empathy_profile_chooser_dup_selected...
[empathy.git] / libempathy-gtk / empathy-profile-chooser.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27 #include <libmissioncontrol/mc-profile.h>
28 #include <libmissioncontrol/mc-protocol.h>
29
30 #include "empathy-profile-chooser.h"
31 #include "empathy-ui-utils.h"
32
33 enum {
34         COL_ICON,
35         COL_LABEL,
36         COL_PROFILE,
37         COL_COUNT
38 };
39
40 McProfile*
41 empathy_profile_chooser_dup_selected (GtkWidget *widget)
42 {
43         GtkTreeModel *model;
44         GtkTreeIter   iter;
45         McProfile    *profile = NULL;
46
47         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
48         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
49                 gtk_tree_model_get (model, &iter,
50                                     COL_PROFILE, &profile,
51                                     -1);
52         }
53
54         return profile;
55 }
56
57 gint
58 empathy_profile_chooser_n_profiles (GtkWidget *widget)
59 {
60         GtkTreeModel *model;
61
62         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
63
64         return gtk_tree_model_iter_n_children (model, NULL);
65 }
66
67 static gint
68 profile_chooser_sort_profile_value (McProfile *profile)
69 {
70         guint        i;
71         const gchar *profile_name;
72         const gchar *names[] = {"jabber",
73                                 "salut",
74                                 "gtalk",
75                                 NULL};
76
77         profile_name = mc_profile_get_unique_name (profile);
78
79         for (i = 0 ; names[i]; i++) {
80                 if (strcmp (profile_name, names[i]) == 0) {
81                         return i;
82                 }
83         }
84
85         return i;
86 }
87
88 static gint
89 profile_chooser_sort_func (GtkTreeModel *model,
90                            GtkTreeIter  *iter_a,
91                            GtkTreeIter  *iter_b,
92                            gpointer      user_data)
93 {
94         McProfile   *profile_a;
95         McProfile   *profile_b;
96         gint         cmp;
97
98         gtk_tree_model_get (model, iter_a,
99                             COL_PROFILE, &profile_a,
100                             -1);
101         gtk_tree_model_get (model, iter_b,
102                             COL_PROFILE, &profile_b,
103                             -1);
104
105         cmp = profile_chooser_sort_profile_value (profile_a);
106         cmp -= profile_chooser_sort_profile_value (profile_b);
107         if (cmp == 0) {
108                 cmp = strcmp (mc_profile_get_display_name (profile_a),
109                               mc_profile_get_display_name (profile_b));
110         }
111
112         g_object_unref (profile_a);
113         g_object_unref (profile_b);
114
115         return cmp;
116 }
117
118 GtkWidget *
119 empathy_profile_chooser_new (void)
120 {
121         GList           *profiles, *l, *seen;
122         GtkListStore    *store;
123         GtkCellRenderer *renderer;
124         GtkWidget       *combo_box;
125         GtkTreeIter      iter;
126         gboolean         iter_set = FALSE;
127         McManager       *btf_cm;
128
129         /* set up combo box with new store */
130         store = gtk_list_store_new (COL_COUNT,
131                                     G_TYPE_STRING,    /* Icon name */
132                                     G_TYPE_STRING,    /* Label     */
133                                     MC_TYPE_PROFILE); /* Profile   */
134         combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
135
136
137         renderer = gtk_cell_renderer_pixbuf_new ();
138         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
139         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
140                                         "icon-name", COL_ICON,
141                                         NULL);
142         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
143
144         renderer = gtk_cell_renderer_text_new ();
145         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
146         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
147                                         "text", COL_LABEL,
148                                         NULL);
149
150         btf_cm = mc_manager_lookup ("butterfly");
151         profiles = mc_profiles_list ();
152         seen = NULL;
153         for (l = profiles; l; l = l->next) {
154                 McProfile   *profile;
155                 McProtocol  *protocol;
156                 const gchar *unique_name;
157
158                 profile = l->data;
159
160                 /* Check if the CM is installed, otherwise skip that profile.
161                  * Workaround SF bug #1688779 */
162                 protocol = mc_profile_get_protocol (profile);
163                 if (!protocol) {
164                         continue;
165                 }
166                 g_object_unref (protocol);
167
168                 /* Skip MSN-Haze if we have butterfly */
169                 unique_name = mc_profile_get_unique_name (profile);
170                 if (btf_cm && strcmp (unique_name, "msn-haze") == 0) {
171                         continue;
172                 }
173
174                 if (g_list_find_custom (seen, unique_name, (GCompareFunc) strcmp)) {
175                         continue;
176                 }
177                 seen = g_list_append (seen, (char*) unique_name);
178
179                 gtk_list_store_insert_with_values (store, &iter, 0,
180                                                    COL_ICON, mc_profile_get_icon_name (profile),
181                                                    COL_LABEL, mc_profile_get_display_name (profile),
182                                                    COL_PROFILE, profile,
183                                                    -1);
184                 iter_set = TRUE;
185         }
186
187         g_list_free (seen);
188
189         if (btf_cm) {
190                 g_object_unref (btf_cm);
191         }
192
193         /* Set the profile sort function */
194         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
195                                          COL_PROFILE,
196                                          profile_chooser_sort_func,
197                                          NULL, NULL);
198         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
199                                               COL_PROFILE,
200                                               GTK_SORT_ASCENDING);
201
202         if (iter_set) {
203                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
204         }
205
206         mc_profiles_free_list (profiles);
207         g_object_unref (store);
208
209         return combo_box;
210 }
211