]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-profile-chooser.c
Add missing include string.h.
[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
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_get_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 static gint
58 profile_chooser_sort_profile_value (McProfile *profile)
59 {
60         guint        i;
61         const gchar *profile_name;
62         const gchar *names[] = {"jabber",
63                                 "salut",
64                                 "gtalk",
65                                 NULL};
66
67         profile_name = mc_profile_get_unique_name (profile);
68
69         for (i = 0 ; names[i]; i++) {
70                 if (strcmp (profile_name, names[i]) == 0) {
71                         return i;
72                 }
73         }
74
75         return i;
76 }
77
78 static gint
79 profile_chooser_sort_func (GtkTreeModel *model,
80                            GtkTreeIter  *iter_a,
81                            GtkTreeIter  *iter_b,
82                            gpointer      user_data)
83 {
84         McProfile   *profile_a;
85         McProfile   *profile_b;
86         gint         cmp;
87
88         gtk_tree_model_get (model, iter_a,
89                             COL_PROFILE, &profile_a,
90                             -1);
91         gtk_tree_model_get (model, iter_b,
92                             COL_PROFILE, &profile_b,
93                             -1);
94
95         cmp = profile_chooser_sort_profile_value (profile_a);
96         cmp -= profile_chooser_sort_profile_value (profile_b);
97         if (cmp == 0) {
98                 cmp = strcmp (mc_profile_get_display_name (profile_a),
99                               mc_profile_get_display_name (profile_b));
100         }
101
102         g_object_unref (profile_a);
103         g_object_unref (profile_b);
104
105         return cmp;
106 }
107
108 GtkWidget *
109 empathy_profile_chooser_new (void)
110 {
111         GList           *profiles, *l;
112         GtkListStore    *store;
113         GtkCellRenderer *renderer;
114         GtkWidget       *combo_box;
115         GtkTreeIter      iter;
116
117         /* set up combo box with new store */
118         store = gtk_list_store_new (COL_COUNT,
119                                     G_TYPE_STRING,    /* Icon name */
120                                     G_TYPE_STRING,    /* Label     */
121                                     MC_TYPE_PROFILE); /* Profile   */
122         combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
123
124
125         renderer = gtk_cell_renderer_pixbuf_new ();
126         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
127         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
128                                         "icon-name", COL_ICON,
129                                         NULL);
130         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
131
132         renderer = gtk_cell_renderer_text_new ();
133         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
134         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
135                                         "text", COL_LABEL,
136                                         NULL);
137
138         /* Set the profile sort function */
139         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
140                                               COL_PROFILE,
141                                               GTK_SORT_ASCENDING);
142         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
143                                          COL_PROFILE,
144                                          profile_chooser_sort_func,
145                                          NULL, NULL);
146
147         profiles = mc_profiles_list ();
148         for (l = profiles; l; l = l->next) {
149                 McProfile *profile;
150
151                 profile = l->data;
152
153                 gtk_list_store_append (store, &iter);
154                 gtk_list_store_set (store, &iter,
155                                     COL_ICON, mc_profile_get_icon_name (profile),
156                                     COL_LABEL, mc_profile_get_display_name (profile),
157                                     COL_PROFILE, profile,
158                                     -1);
159         }
160
161         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo_box), &iter);
162
163         mc_profiles_free_list (profiles);
164         g_object_unref (store);
165
166         return combo_box;
167 }
168