empathy-tube-handler: wait that tube is ready before announcing it
[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_CONTACT_LIST
41 };
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
44 typedef struct
45 {
46   EmpathyContactList *contact_list;
47   EmpathyContactListStore *store;
48   GtkTreeModel *model;
49   gboolean dispose_run;
50 } EmpathyContactSelectorPriv;
51
52 static void contact_selector_manage_blank_contact (
53     EmpathyContactSelector *selector);
54
55 static guint
56 contact_selector_get_number_online_contacts (GtkTreeModel *model)
57 {
58   GtkTreeIter tmp_iter;
59   gboolean is_online;
60   guint number_online_contacts = 0;
61   gboolean ok;
62
63   for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
64       ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
65     {
66       gtk_tree_model_get (model,
67           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
68           &is_online, -1);
69       if (is_online)
70         number_online_contacts++;
71     }
72
73   return number_online_contacts;
74 }
75
76 static gboolean
77 contact_selector_get_iter_for_blank_contact (GtkTreeStore *model,
78                                              GtkTreeIter *blank_iter)
79 {
80   GtkTreeIter tmp_iter;
81   EmpathyContact *tmp_contact;
82   gboolean is_present = FALSE;
83   gboolean ok;
84
85   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &tmp_iter);
86       ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &tmp_iter))
87     {
88       gtk_tree_model_get (GTK_TREE_MODEL (model),
89           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
90           &tmp_contact, -1);
91       if (tmp_contact == NULL)
92         {
93           *blank_iter = tmp_iter;
94           is_present = TRUE;
95           break;
96         }
97       g_object_unref (tmp_contact);
98     }
99
100   return is_present;
101 }
102
103 static void
104 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
105 {
106   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
107   GtkTreeIter blank_iter, iter;
108
109   gtk_tree_store_insert_with_values (
110       GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0,
111       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
112       EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
113       EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
114
115   /* look up blank_iter in the filter model */
116   g_return_if_fail (gtk_tree_model_filter_convert_child_iter_to_iter (
117       GTK_TREE_MODEL_FILTER (priv->model), &iter, &blank_iter));
118
119   g_signal_handlers_block_by_func (selector,
120       contact_selector_manage_blank_contact, selector);
121   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &iter);
122   g_signal_handlers_unblock_by_func (selector,
123       contact_selector_manage_blank_contact, selector);
124 }
125
126 static void
127 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
128 {
129   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
130   GtkTreeIter blank_iter;
131
132   if (contact_selector_get_iter_for_blank_contact
133       (GTK_TREE_STORE (priv->store), &blank_iter))
134     gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
135 }
136
137 static void
138 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
139 {
140   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
141   guint number_online_contacts;
142
143   number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
144
145   if (number_online_contacts != 0)
146     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
147   else
148     gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
149 }
150
151 static void
152 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
153 {
154   gboolean is_popup_shown;
155
156   g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
157
158   if (is_popup_shown)
159     {
160       contact_selector_remove_blank_contact (selector);
161     }
162   else
163     {
164       if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
165         {
166           contact_selector_add_blank_contact (selector);
167         }
168       else
169         {
170           contact_selector_remove_blank_contact (selector);
171         }
172     }
173
174   contact_selector_manage_sensitivity (selector);
175 }
176
177 static GObject *
178 contact_selector_constructor (GType type,
179                               guint n_construct_params,
180                               GObjectConstructParam *construct_params)
181 {
182   GObject *object;
183   EmpathyContactSelector *contact_selector;
184   EmpathyContactSelectorPriv *priv;
185   GtkCellLayout *cell_layout;
186   GtkCellRenderer *renderer;
187
188   object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor 
189     (type, n_construct_params, construct_params);
190   priv = GET_PRIV (object);
191   contact_selector = EMPATHY_CONTACT_SELECTOR (object);
192   cell_layout = GTK_CELL_LAYOUT (object);
193
194   priv->store = empathy_contact_list_store_new (priv->contact_list);
195
196   g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
197       "show-offline", FALSE, "show-groups", FALSE,
198       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
199
200   g_signal_connect_swapped (priv->store, "row-changed",
201       G_CALLBACK (contact_selector_manage_sensitivity),
202       contact_selector);
203   g_signal_connect_swapped (contact_selector, "changed",
204       G_CALLBACK (contact_selector_manage_blank_contact),
205       contact_selector);
206   g_signal_connect_swapped (contact_selector, "notify::popup-shown",
207       G_CALLBACK (contact_selector_manage_blank_contact),
208       contact_selector);
209
210   priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
211
212   gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
213   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
214
215   renderer = gtk_cell_renderer_pixbuf_new ();
216   gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
217   gtk_cell_layout_set_attributes (cell_layout, renderer,
218       "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
219
220   renderer = gtk_cell_renderer_text_new ();
221   gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
222   gtk_cell_layout_set_attributes (cell_layout, renderer,
223       "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
224
225   contact_selector_manage_blank_contact (contact_selector);
226   contact_selector_manage_sensitivity (contact_selector);
227
228   return object;
229 }
230
231 static void
232 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
233 {
234   EmpathyContactSelectorPriv *priv =
235       G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
236       EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
237
238   empathy_contact_selector->priv = priv;
239
240   priv->dispose_run = FALSE;
241 }
242
243 static void
244 contact_selector_set_property (GObject *object,
245                                guint prop_id,
246                                const GValue *value,
247                                GParamSpec *pspec)
248 {
249   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
250
251   switch (prop_id)
252     {
253       case PROP_CONTACT_LIST:
254         priv->contact_list = g_value_dup_object (value);
255         break;
256       default:
257         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258         break;
259     }
260 }
261
262 static void
263 contact_selector_get_property (GObject *object,
264                                guint prop_id,
265                                GValue *value,
266                                GParamSpec *pspec)
267 {
268   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
269
270   switch (prop_id)
271     {
272       case PROP_CONTACT_LIST:
273         g_value_set_object (value, priv->contact_list);
274         break;
275       default:
276         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277         break;
278     }
279 }
280
281 static void
282 contact_selector_dispose (GObject *object)
283 {
284   EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
285   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
286
287   if (priv->dispose_run)
288     return;
289
290   priv->dispose_run = TRUE;
291
292   if (priv->contact_list)
293     {
294       g_object_unref (priv->contact_list);
295       priv->contact_list = NULL;
296     }
297
298   if (priv->model)
299     {
300       g_object_unref (priv->model);
301       priv->model = NULL;
302     }
303
304   if (priv->store)
305     {
306       g_object_unref (priv->store);
307       priv->store = NULL;
308     }
309
310   (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
311 }
312
313 static void
314 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
315 {
316   GObjectClass *object_class = G_OBJECT_CLASS (klass);
317   object_class->constructor = contact_selector_constructor;
318   object_class->dispose = contact_selector_dispose;
319   object_class->set_property = contact_selector_set_property;
320   object_class->get_property = contact_selector_get_property;
321   g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
322
323   g_object_class_install_property (object_class, PROP_CONTACT_LIST,
324       g_param_spec_object ("contact-list", "contact list", "contact list",
325       EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
326       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
327 }
328
329 /* public methods */
330
331 GtkWidget *
332 empathy_contact_selector_new (EmpathyContactList *contact_list)
333 {
334   g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
335
336   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
337       "contact-list", contact_list, NULL));
338 }
339
340 EmpathyContact *
341 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
342 {
343   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
344   EmpathyContact *contact = NULL;
345   GtkTreeIter iter;
346
347   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
348
349   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
350     return NULL;
351
352   gtk_tree_model_get (priv->model, &iter,
353       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
354
355   return contact;
356 }
357
358 typedef struct
359 {
360   EmpathyContactSelectorFilterFunc func;
361   gpointer user_data;
362 } FilterData;
363
364 static void
365 filter_data_free (gpointer data)
366 {
367   g_slice_free (FilterData, data);
368 }
369
370 static gboolean
371 contact_selector_filter_visible_func (GtkTreeModel *model,
372                                       GtkTreeIter *iter,
373                                       gpointer user_data)
374 {
375   EmpathyContact *contact;
376   gboolean visible = TRUE;
377   FilterData *data = (FilterData *) user_data;
378
379   gtk_tree_model_get (model, iter,
380       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
381       -1);
382
383   if (contact != NULL)
384     {
385       visible = data->func(contact, data->user_data);
386
387       g_object_unref (contact);
388     }
389
390   return visible;
391 }
392
393 void
394 empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
395                                       EmpathyContactSelectorFilterFunc func,
396                                       gpointer user_data)
397 {
398   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
399   FilterData *data;
400
401   data = g_slice_new0 (FilterData);
402   data->func = func;
403   data->user_data = user_data;
404
405   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
406       contact_selector_filter_visible_func, data, filter_data_free);
407
408   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));
409 }