]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector.c
467feb44a83c3b5bde167b964b5e1783479ff537
[empathy.git] / libempathy-gtk / empathy-contact-selector.c
1 /*
2 *  Copyright (C) 2007 Marco Barisione <marco@barisione.org>
3 *  Copyright (C) 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: Marco Barisione <marco@barisione.org>
20 *           Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21 */
22
23 #include "config.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include <libempathy/empathy-contact.h>
29 #include <libempathy-gtk/empathy-contact-list-store.h>
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-contact-selector.h"
33
34 G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
35     GTK_TYPE_COMBO_BOX)
36
37 enum
38 {
39   PROP_0,
40   PROP_STORE
41 };
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
44 typedef struct
45 {
46   EmpathyContactListStore *store;
47   gboolean dispose_run;
48 } EmpathyContactSelectorPriv;
49
50 static void contact_selector_manage_blank_contact (
51     EmpathyContactSelector *selector);
52
53 static guint
54 contact_selector_get_number_online_contacts (GtkTreeStore *store)
55 {
56   GtkTreeIter tmp_iter;
57   gboolean is_online;
58   guint number_online_contacts = 0;
59   gboolean ok;
60
61   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
62       ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
63     {
64       gtk_tree_model_get (GTK_TREE_MODEL (store),
65           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
66           &is_online, -1);
67       if (is_online)
68         number_online_contacts++;
69     }
70
71   return number_online_contacts;
72 }
73
74 static gboolean
75 contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
76                                              GtkTreeIter *blank_iter)
77 {
78   GtkTreeIter tmp_iter;
79   EmpathyContact *tmp_contact;
80   gboolean is_present = FALSE;
81   gboolean ok;
82
83   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
84       ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
85     {
86       gtk_tree_model_get (GTK_TREE_MODEL (store),
87           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
88           &tmp_contact, -1);
89       if (tmp_contact == NULL)
90         {
91           *blank_iter = tmp_iter;
92           is_present = TRUE;
93           break;
94         }
95       g_object_unref (tmp_contact);
96     }
97
98   return is_present;
99 }
100
101 static void
102 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
103 {
104   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
105   GtkTreeIter blank_iter;
106
107   gtk_tree_store_insert_with_values (
108       GTK_TREE_STORE (priv->store),&blank_iter, NULL, 0,
109       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
110       EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
111       EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
112   g_signal_handlers_block_by_func (selector,
113       contact_selector_manage_blank_contact, selector);
114   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
115   g_signal_handlers_unblock_by_func (selector,
116       contact_selector_manage_blank_contact, selector);
117 }
118
119 static void
120 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
121 {
122   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
123   GtkTreeIter blank_iter;
124
125   if (contact_selector_get_iter_for_blank_contact (
126         GTK_TREE_STORE (priv->store), &blank_iter))
127     gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
128 }
129
130 static void
131 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
132 {
133   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
134
135   /* FIXME - make this work when offline contacts are shown.
136    * The following value needs to be the number of entries shown
137    * excluding the blank entry (if present).
138    */
139   guint number_online_contacts = contact_selector_get_number_online_contacts (
140       GTK_TREE_STORE (priv->store));
141
142   if (number_online_contacts != 0)
143     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
144   else
145     gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
146 }
147
148 static void
149 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
150 {
151   gboolean is_popup_shown;
152
153   g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
154
155   if (is_popup_shown)
156     {
157       contact_selector_remove_blank_contact (selector);
158     }
159   else
160     {
161       if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
162         {
163           contact_selector_add_blank_contact (selector);
164         }
165       else
166         {
167           contact_selector_remove_blank_contact (selector);
168         }
169     }
170
171   contact_selector_manage_sensitivity (selector);
172 }
173
174 static GObject *
175 contact_selector_constructor (GType type,
176                               guint n_construct_params,
177                               GObjectConstructParam *construct_params)
178 {
179   GObject *object;
180   EmpathyContactSelector *contact_selector;
181   EmpathyContactSelectorPriv *priv;
182   GtkCellLayout *cell_layout;
183   GtkCellRenderer *renderer;
184
185   object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor 
186     (type, n_construct_params, construct_params);
187   priv = GET_PRIV (object);
188   contact_selector = EMPATHY_CONTACT_SELECTOR (object);
189   cell_layout = GTK_CELL_LAYOUT (object);
190
191   g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
192       "show-offline", FALSE, "show-groups", FALSE,
193       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
194
195   g_signal_connect_swapped (priv->store, "row-changed",
196       G_CALLBACK (contact_selector_manage_sensitivity),
197       contact_selector);
198   g_signal_connect_swapped (contact_selector, "changed",
199       G_CALLBACK (contact_selector_manage_blank_contact),
200       contact_selector);
201   g_signal_connect_swapped (contact_selector, "notify::popup-shown",
202       G_CALLBACK (contact_selector_manage_blank_contact),
203       contact_selector);
204
205   gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
206       GTK_TREE_MODEL (priv->store));
207   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
208
209   renderer = gtk_cell_renderer_pixbuf_new ();
210   gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
211   gtk_cell_layout_set_attributes (cell_layout, renderer,
212       "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
213
214   renderer = gtk_cell_renderer_text_new ();
215   gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
216   gtk_cell_layout_set_attributes (cell_layout, renderer,
217       "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
218
219   contact_selector_manage_blank_contact (contact_selector);
220   contact_selector_manage_sensitivity (contact_selector);
221
222   return object;
223 }
224
225 static void
226 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
227 {
228   EmpathyContactSelectorPriv *priv =
229       G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
230       EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
231
232   empathy_contact_selector->priv = priv;
233
234   priv->dispose_run = FALSE;
235 }
236
237 static void
238 contact_selector_set_property (GObject *object,
239                                guint prop_id,
240                                const GValue *value,
241                                GParamSpec *pspec)
242 {
243   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
244
245   switch (prop_id)
246     {
247       case PROP_STORE:
248         priv->store = g_value_dup_object (value);
249         break;
250       default:
251         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252         break;
253     }
254 }
255
256 static void
257 contact_selector_get_property (GObject *object,
258                                guint prop_id,
259                                GValue *value,
260                                GParamSpec *pspec)
261 {
262   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
263
264   switch (prop_id)
265     {
266       case PROP_STORE:
267         g_value_set_object (value, priv->store);
268         break;
269       default:
270         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271         break;
272     }
273 }
274
275 static void
276 contact_selector_dispose (GObject *object)
277 {
278   EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
279   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
280
281   if (priv->dispose_run)
282     return;
283
284   priv->dispose_run = TRUE;
285
286   if (priv->store)
287     {
288       g_object_unref (priv->store);
289       priv->store = NULL;
290     }
291
292   (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
293 }
294
295 static void
296 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
297 {
298   GObjectClass *object_class = G_OBJECT_CLASS (klass);
299   object_class->constructor = contact_selector_constructor;
300   object_class->dispose = contact_selector_dispose;
301   object_class->set_property = contact_selector_set_property;
302   object_class->get_property = contact_selector_get_property;
303   g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
304
305   g_object_class_install_property (object_class, PROP_STORE,
306       g_param_spec_object ("store", "store", "store",
307       EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
308       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
309 }
310
311 /* public methods */
312
313 GtkWidget *
314 empathy_contact_selector_new (EmpathyContactListStore *store)
315 {
316   g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
317
318   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL));
319 }
320
321 EmpathyContact *
322 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
323 {
324   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
325   EmpathyContact *contact = NULL;
326   GtkTreeIter iter;
327
328   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
329
330   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
331     return NULL;
332
333   gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
334       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
335
336   return contact;
337 }