]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector.c
Pass an EmpathyContactList to _new and create the store from that.
[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   guint number_online_contacts;
135
136   number_online_contacts = contact_selector_get_number_online_contacts (
137       GTK_TREE_STORE (priv->store));
138
139   if (number_online_contacts != 0)
140     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
141   else
142     gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
143 }
144
145 static void
146 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
147 {
148   gboolean is_popup_shown;
149
150   g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
151
152   if (is_popup_shown)
153     {
154       contact_selector_remove_blank_contact (selector);
155     }
156   else
157     {
158       if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
159         {
160           contact_selector_add_blank_contact (selector);
161         }
162       else
163         {
164           contact_selector_remove_blank_contact (selector);
165         }
166     }
167
168   contact_selector_manage_sensitivity (selector);
169 }
170
171 static GObject *
172 contact_selector_constructor (GType type,
173                               guint n_construct_params,
174                               GObjectConstructParam *construct_params)
175 {
176   GObject *object;
177   EmpathyContactSelector *contact_selector;
178   EmpathyContactSelectorPriv *priv;
179   GtkCellLayout *cell_layout;
180   GtkCellRenderer *renderer;
181
182   object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor 
183     (type, n_construct_params, construct_params);
184   priv = GET_PRIV (object);
185   contact_selector = EMPATHY_CONTACT_SELECTOR (object);
186   cell_layout = GTK_CELL_LAYOUT (object);
187
188   g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
189       "show-offline", FALSE, "show-groups", FALSE,
190       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
191
192   g_signal_connect_swapped (priv->store, "row-changed",
193       G_CALLBACK (contact_selector_manage_sensitivity),
194       contact_selector);
195   g_signal_connect_swapped (contact_selector, "changed",
196       G_CALLBACK (contact_selector_manage_blank_contact),
197       contact_selector);
198   g_signal_connect_swapped (contact_selector, "notify::popup-shown",
199       G_CALLBACK (contact_selector_manage_blank_contact),
200       contact_selector);
201
202   gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
203       GTK_TREE_MODEL (priv->store));
204   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
205
206   renderer = gtk_cell_renderer_pixbuf_new ();
207   gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
208   gtk_cell_layout_set_attributes (cell_layout, renderer,
209       "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
210
211   renderer = gtk_cell_renderer_text_new ();
212   gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
213   gtk_cell_layout_set_attributes (cell_layout, renderer,
214       "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
215
216   contact_selector_manage_blank_contact (contact_selector);
217   contact_selector_manage_sensitivity (contact_selector);
218
219   return object;
220 }
221
222 static void
223 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
224 {
225   EmpathyContactSelectorPriv *priv =
226       G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
227       EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
228
229   empathy_contact_selector->priv = priv;
230
231   priv->dispose_run = FALSE;
232 }
233
234 static void
235 contact_selector_set_property (GObject *object,
236                                guint prop_id,
237                                const GValue *value,
238                                GParamSpec *pspec)
239 {
240   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
241
242   switch (prop_id)
243     {
244       case PROP_STORE:
245         priv->store = g_value_dup_object (value);
246         break;
247       default:
248         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249         break;
250     }
251 }
252
253 static void
254 contact_selector_get_property (GObject *object,
255                                guint prop_id,
256                                GValue *value,
257                                GParamSpec *pspec)
258 {
259   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
260
261   switch (prop_id)
262     {
263       case PROP_STORE:
264         g_value_set_object (value, priv->store);
265         break;
266       default:
267         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268         break;
269     }
270 }
271
272 static void
273 contact_selector_dispose (GObject *object)
274 {
275   EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
276   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
277
278   if (priv->dispose_run)
279     return;
280
281   priv->dispose_run = TRUE;
282
283   if (priv->store)
284     {
285       g_object_unref (priv->store);
286       priv->store = NULL;
287     }
288
289   (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
290 }
291
292 static void
293 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
294 {
295   GObjectClass *object_class = G_OBJECT_CLASS (klass);
296   object_class->constructor = contact_selector_constructor;
297   object_class->dispose = contact_selector_dispose;
298   object_class->set_property = contact_selector_set_property;
299   object_class->get_property = contact_selector_get_property;
300   g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
301
302   g_object_class_install_property (object_class, PROP_STORE,
303       g_param_spec_object ("store", "store", "store",
304       EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
305       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
306 }
307
308 /* public methods */
309
310 GtkWidget *
311 empathy_contact_selector_new (EmpathyContactList *contact_list)
312 {
313   EmpathyContactListStore *store;
314
315   g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
316
317   store = empathy_contact_list_store_new (contact_list);
318
319   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL));
320 }
321
322 EmpathyContact *
323 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
324 {
325   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
326   EmpathyContact *contact = NULL;
327   GtkTreeIter iter;
328
329   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
330
331   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
332     return NULL;
333
334   gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
335       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
336
337   return contact;
338 }