]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
To know if an account is connected, use
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <gtk/gtk.h>
28 #include <libmissioncontrol/mc-profile.h>
29 #include <libmissioncontrol/mc-protocol.h>
30
31 #include "empathy-profile-chooser.h"
32 #include "empathy-ui-utils.h"
33
34 enum {
35         COL_ICON,
36         COL_LABEL,
37         COL_PROFILE,
38         COL_COUNT
39 };
40
41 McProfile*
42 empathy_profile_chooser_get_selected (GtkWidget *widget)
43 {
44         GtkTreeModel *model;
45         GtkTreeIter   iter;
46         McProfile    *profile = NULL;
47
48         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
49         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
50                 gtk_tree_model_get (model, &iter,
51                                     COL_PROFILE, &profile,
52                                     -1);
53         }
54
55         return profile;
56 }
57
58 gint
59 empathy_profile_chooser_n_profiles (GtkWidget *widget)
60 {
61         GtkTreeModel *model;
62
63         model = gtk_combo_box_get_model (GTK_COMBO_BOX (widget));
64
65         return gtk_tree_model_iter_n_children (model, NULL);
66 }
67
68 static gint
69 profile_chooser_sort_profile_value (McProfile *profile)
70 {
71         guint        i;
72         const gchar *profile_name;
73         const gchar *names[] = {"jabber",
74                                 "salut",
75                                 "gtalk",
76                                 NULL};
77
78         profile_name = mc_profile_get_unique_name (profile);
79
80         for (i = 0 ; names[i]; i++) {
81                 if (strcmp (profile_name, names[i]) == 0) {
82                         return i;
83                 }
84         }
85
86         return i;
87 }
88
89 static gint
90 profile_chooser_sort_func (GtkTreeModel *model,
91                            GtkTreeIter  *iter_a,
92                            GtkTreeIter  *iter_b,
93                            gpointer      user_data)
94 {
95         McProfile   *profile_a;
96         McProfile   *profile_b;
97         gint         cmp;
98
99         gtk_tree_model_get (model, iter_a,
100                             COL_PROFILE, &profile_a,
101                             -1);
102         gtk_tree_model_get (model, iter_b,
103                             COL_PROFILE, &profile_b,
104                             -1);
105
106         cmp = profile_chooser_sort_profile_value (profile_a);
107         cmp -= profile_chooser_sort_profile_value (profile_b);
108         if (cmp == 0) {
109                 cmp = strcmp (mc_profile_get_display_name (profile_a),
110                               mc_profile_get_display_name (profile_b));
111         }
112
113         g_object_unref (profile_a);
114         g_object_unref (profile_b);
115
116         return cmp;
117 }
118
119 GtkWidget *
120 empathy_profile_chooser_new (void)
121 {
122         GList           *profiles, *l;
123         GtkListStore    *store;
124         GtkCellRenderer *renderer;
125         GtkWidget       *combo_box;
126         GtkTreeIter      iter;
127         gboolean         iter_set = FALSE;
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         profiles = mc_profiles_list ();
151         for (l = profiles; l; l = l->next) {
152                 McProfile  *profile;
153                 McProtocol *protocol;
154
155                 profile = l->data;
156
157                 /* Check if the CM is installed, otherwise skip that profile.
158                  * Workaround SF bug #1688779 */
159                 protocol = mc_profile_get_protocol (profile);
160                 if (!protocol) {
161                         continue;
162                 }
163                 g_object_unref (protocol);
164
165                 gtk_list_store_insert_with_values (store, &iter, 0,
166                                                    COL_ICON, mc_profile_get_icon_name (profile),
167                                                    COL_LABEL, mc_profile_get_display_name (profile),
168                                                    COL_PROFILE, profile,
169                                                    -1);
170                 iter_set = TRUE;
171         }
172
173         /* Set the profile sort function */
174         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
175                                          COL_PROFILE,
176                                          profile_chooser_sort_func,
177                                          NULL, NULL);
178         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
179                                               COL_PROFILE,
180                                               GTK_SORT_ASCENDING);
181
182         if (iter_set) {
183                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
184         }
185
186         mc_profiles_free_list (profiles);
187         g_object_unref (store);
188
189         return combo_box;
190 }
191