2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2008, 2010 Collabora Ltd.
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.
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.
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
23 * Philip Withnall <philip.withnall@collabora.co.uk>
25 * Based off EmpathyContactListView.
32 #include <glib/gi18n-lib.h>
35 #include <telepathy-glib/util.h>
37 #include <folks/folks.h>
38 #include <folks/folks-telepathy.h>
40 #include <libempathy/empathy-utils.h>
42 #include "empathy-persona-view.h"
43 #include "empathy-contact-widget.h"
44 #include "empathy-images.h"
45 #include "empathy-cell-renderer-text.h"
46 #include "empathy-cell-renderer-activatable.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
49 #include <libempathy/empathy-debug.h>
52 * SECTION:empathy-persona-view
53 * @title: EmpathyPersonaView
54 * @short_description: A tree view which displays personas from an individual
55 * @include: libempathy-gtk/empathy-persona-view.h
57 * #EmpathyPersonaView is a tree view widget which displays the personas from
58 * a given #EmpathyPersonaStore.
60 * It supports hiding offline personas and highlighting active personas. Active
61 * personas are those which have recently changed state (e.g. online, offline or
62 * from normal to a busy state).
65 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyPersonaView)
69 GtkTreeModelFilter *filter;
70 GtkWidget *tooltip_widget;
71 gboolean show_offline;
72 } EmpathyPersonaViewPriv;
81 G_DEFINE_TYPE (EmpathyPersonaView, empathy_persona_view, GTK_TYPE_TREE_VIEW);
84 filter_visible_func (GtkTreeModel *model,
86 EmpathyPersonaView *self)
88 EmpathyPersonaViewPriv *priv = GET_PRIV (self);
91 gtk_tree_model_get (model, iter,
92 EMPATHY_PERSONA_STORE_COL_IS_ONLINE, &is_online,
95 return (priv->show_offline || is_online);
99 set_model (EmpathyPersonaView *self,
102 EmpathyPersonaViewPriv *priv = GET_PRIV (self);
104 tp_clear_object (&priv->filter);
106 priv->filter = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model,
108 gtk_tree_model_filter_set_visible_func (priv->filter,
109 (GtkTreeModelFilterVisibleFunc) filter_visible_func, self, NULL);
111 gtk_tree_view_set_model (GTK_TREE_VIEW (self), GTK_TREE_MODEL (priv->filter));
115 tooltip_destroy_cb (GtkWidget *widget,
116 EmpathyPersonaView *self)
118 EmpathyPersonaViewPriv *priv = GET_PRIV (self);
120 if (priv->tooltip_widget)
122 DEBUG ("Tooltip destroyed");
123 g_object_unref (priv->tooltip_widget);
124 priv->tooltip_widget = NULL;
129 query_tooltip_cb (EmpathyPersonaView *self,
132 gboolean keyboard_mode,
136 EmpathyPersonaViewPriv *priv = GET_PRIV (self);
137 FolksPersona *persona;
138 EmpathyContact *contact;
142 static gint running = 0;
143 gboolean ret = FALSE;
145 /* Avoid an infinite loop. See GNOME bug #574377 */
150 if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (self), &x, &y,
151 keyboard_mode, &model, &path, &iter))
156 gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (self), tooltip, path);
157 gtk_tree_path_free (path);
159 gtk_tree_model_get (model, &iter,
160 EMPATHY_PERSONA_STORE_COL_PERSONA, &persona,
165 contact = empathy_contact_dup_from_tp_contact (tpf_persona_get_contact (
166 TPF_PERSONA (persona)));
168 if (priv->tooltip_widget == NULL)
170 priv->tooltip_widget = empathy_contact_widget_new (contact,
171 EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP |
172 EMPATHY_CONTACT_WIDGET_SHOW_LOCATION);
173 gtk_container_set_border_width (GTK_CONTAINER (priv->tooltip_widget), 8);
174 g_object_ref (priv->tooltip_widget);
175 g_signal_connect (priv->tooltip_widget, "destroy",
176 (GCallback) tooltip_destroy_cb, self);
177 gtk_widget_show (priv->tooltip_widget);
181 empathy_contact_widget_set_contact (priv->tooltip_widget, contact);
184 gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
187 g_object_unref (contact);
188 g_object_unref (persona);
197 cell_set_background (EmpathyPersonaView *self,
198 GtkCellRenderer *cell,
204 style = gtk_widget_get_style (GTK_WIDGET (self));
208 color = style->bg[GTK_STATE_SELECTED];
210 /* Here we take the current theme colour and add it to
211 * the colour for white and average the two. This
212 * gives a colour which is inline with the theme but
215 color.red = (color.red + (style->white).red) / 2;
216 color.green = (color.green + (style->white).green) / 2;
217 color.blue = (color.blue + (style->white).blue) / 2;
219 g_object_set (cell, "cell-background-gdk", &color, NULL);
223 g_object_set (cell, "cell-background-gdk", NULL, NULL);
228 pixbuf_cell_data_func (GtkTreeViewColumn *tree_column,
229 GtkCellRenderer *cell,
232 EmpathyPersonaView *self)
237 gtk_tree_model_get (model, iter,
238 EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, &is_active,
239 EMPATHY_PERSONA_STORE_COL_ICON_STATUS, &pixbuf,
242 g_object_set (cell, "pixbuf", pixbuf, NULL);
243 tp_clear_object (&pixbuf);
245 cell_set_background (self, cell, is_active);
249 audio_call_cell_data_func (GtkTreeViewColumn *tree_column,
250 GtkCellRenderer *cell,
253 EmpathyPersonaView *self)
256 gboolean can_audio, can_video;
258 gtk_tree_model_get (model, iter,
259 EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, &is_active,
260 EMPATHY_PERSONA_STORE_COL_CAN_AUDIO_CALL, &can_audio,
261 EMPATHY_PERSONA_STORE_COL_CAN_VIDEO_CALL, &can_video,
265 "visible", (can_audio || can_video),
266 "icon-name", can_video? EMPATHY_IMAGE_VIDEO_CALL : EMPATHY_IMAGE_VOIP,
269 cell_set_background (self, cell, is_active);
273 avatar_cell_data_func (GtkTreeViewColumn *tree_column,
274 GtkCellRenderer *cell,
277 EmpathyPersonaView *self)
280 gboolean show_avatar, is_active;
282 gtk_tree_model_get (model, iter,
283 EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR, &pixbuf,
284 EMPATHY_PERSONA_STORE_COL_PIXBUF_AVATAR_VISIBLE, &show_avatar,
285 EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, &is_active,
289 "visible", show_avatar,
293 tp_clear_object (&pixbuf);
295 cell_set_background (self, cell, is_active);
299 text_cell_data_func (GtkTreeViewColumn *tree_column,
300 GtkCellRenderer *cell,
303 EmpathyPersonaView *self)
307 gtk_tree_model_get (model, iter,
308 EMPATHY_PERSONA_STORE_COL_IS_ACTIVE, &is_active,
311 cell_set_background (self, cell, is_active);
315 empathy_persona_view_init (EmpathyPersonaView *self)
317 EmpathyPersonaViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
318 EMPATHY_TYPE_PERSONA_VIEW, EmpathyPersonaViewPriv);
322 /* Connect to tree view signals rather than override. */
323 g_signal_connect (self, "query-tooltip", (GCallback) query_tooltip_cb, NULL);
327 constructed (GObject *object)
329 EmpathyPersonaView *self = EMPATHY_PERSONA_VIEW (object);
330 GtkCellRenderer *cell;
331 GtkTreeViewColumn *col;
335 "headers-visible", FALSE,
336 "show-expanders", FALSE,
339 col = gtk_tree_view_column_new ();
342 cell = gtk_cell_renderer_pixbuf_new ();
343 gtk_tree_view_column_pack_start (col, cell, FALSE);
344 gtk_tree_view_column_set_cell_data_func (col, cell,
345 (GtkTreeCellDataFunc) pixbuf_cell_data_func, self, NULL);
354 cell = empathy_cell_renderer_text_new ();
355 gtk_tree_view_column_pack_start (col, cell, TRUE);
356 gtk_tree_view_column_set_cell_data_func (col, cell,
357 (GtkTreeCellDataFunc) text_cell_data_func, self, NULL);
359 gtk_tree_view_column_add_attribute (col, cell,
360 "name", EMPATHY_PERSONA_STORE_COL_DISPLAY_ID);
361 gtk_tree_view_column_add_attribute (col, cell,
362 "text", EMPATHY_PERSONA_STORE_COL_DISPLAY_ID);
363 gtk_tree_view_column_add_attribute (col, cell,
364 "presence-type", EMPATHY_PERSONA_STORE_COL_PRESENCE_TYPE);
365 gtk_tree_view_column_add_attribute (col, cell,
366 "status", EMPATHY_PERSONA_STORE_COL_STATUS);
368 /* Audio Call Icon */
369 cell = empathy_cell_renderer_activatable_new ();
370 gtk_tree_view_column_pack_start (col, cell, FALSE);
371 gtk_tree_view_column_set_cell_data_func (col, cell,
372 (GtkTreeCellDataFunc) audio_call_cell_data_func, self, NULL);
379 cell = gtk_cell_renderer_pixbuf_new ();
380 gtk_tree_view_column_pack_start (col, cell, FALSE);
381 gtk_tree_view_column_set_cell_data_func (col, cell,
382 (GtkTreeCellDataFunc) avatar_cell_data_func, self, NULL);
392 /* Actually add the column now we have added all cell renderers */
393 gtk_tree_view_append_column (GTK_TREE_VIEW (self), col);
397 get_property (GObject *object,
402 EmpathyPersonaViewPriv *priv = GET_PRIV (object);
407 g_value_set_object (value, priv->filter);
409 case PROP_SHOW_OFFLINE:
410 g_value_set_boolean (value, priv->show_offline);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
419 set_property (GObject *object,
424 EmpathyPersonaView *self = EMPATHY_PERSONA_VIEW (object);
429 set_model (self, g_value_get_object (value));
431 case PROP_SHOW_OFFLINE:
432 empathy_persona_view_set_show_offline (self,
433 g_value_get_boolean (value));
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
442 dispose (GObject *object)
444 EmpathyPersonaView *self = EMPATHY_PERSONA_VIEW (object);
445 EmpathyPersonaViewPriv *priv = GET_PRIV (self);
447 tp_clear_object (&priv->filter);
449 if (priv->tooltip_widget)
450 gtk_widget_destroy (priv->tooltip_widget);
451 priv->tooltip_widget = NULL;
453 G_OBJECT_CLASS (empathy_persona_view_parent_class)->dispose (object);
457 empathy_persona_view_class_init (EmpathyPersonaViewClass *klass)
459 GObjectClass *object_class = G_OBJECT_CLASS (klass);
461 object_class->constructed = constructed;
462 object_class->dispose = dispose;
463 object_class->get_property = get_property;
464 object_class->set_property = set_property;
466 /* We override the "model" property so that we can wrap it in a
467 * GtkTreeModelFilter for showing/hiding offline personas. */
468 g_object_class_override_property (object_class, PROP_MODEL, "model");
471 * EmpathyPersonaStore:show-offline:
473 * Whether to display offline personas.
475 g_object_class_install_property (object_class, PROP_SHOW_OFFLINE,
476 g_param_spec_boolean ("show-offline",
478 "Whether to display offline personas.",
480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
482 g_type_class_add_private (object_class, sizeof (EmpathyPersonaViewPriv));
486 * empathy_persona_view_new:
487 * @store: an #EmpathyPersonaStore
489 * Create a new #EmpathyPersonaView displaying the personas in
490 * #EmpathyPersonaStore.
492 * Return value: a new #EmpathyPersonaView
495 empathy_persona_view_new (EmpathyPersonaStore *store)
497 g_return_val_if_fail (EMPATHY_IS_PERSONA_STORE (store), NULL);
499 return g_object_new (EMPATHY_TYPE_PERSONA_VIEW, "model", store, NULL);
503 * empathy_persona_view_dup_selected:
504 * @self: an #EmpathyPersonaView
506 * Return the #FolksPersona associated with the currently selected row. The
507 * persona is referenced before being returned. If no row is selected, %NULL is
510 * Return value: the currently selected #FolksPersona, or %NULL; unref with
514 empathy_persona_view_dup_selected (EmpathyPersonaView *self)
516 EmpathyPersonaViewPriv *priv;
517 GtkTreeSelection *selection;
520 FolksPersona *persona;
522 g_return_val_if_fail (EMPATHY_IS_PERSONA_VIEW (self), NULL);
524 priv = GET_PRIV (self);
526 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
527 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
530 gtk_tree_model_get (model, &iter,
531 EMPATHY_PERSONA_STORE_COL_PERSONA, &persona,
538 * empathy_persona_view_get_show_offline:
539 * @self: an #EmpathyPersonaView
541 * Get the value of the #EmpathyPersonaView:show-offline property.
543 * Return value: %TRUE if offline personas are being shown, %FALSE otherwise
546 empathy_persona_view_get_show_offline (EmpathyPersonaView *self)
548 g_return_val_if_fail (EMPATHY_IS_PERSONA_VIEW (self), FALSE);
550 return GET_PRIV (self)->show_offline;
554 * empathy_persona_view_set_show_offline:
555 * @self: an #EmpathyPersonaView
556 * @show_offline: %TRUE to show personas which are offline, %FALSE otherwise
558 * Set the #EmpathyPersonaView:show-offline property to @show_offline.
561 empathy_persona_view_set_show_offline (EmpathyPersonaView *self,
562 gboolean show_offline)
564 EmpathyPersonaViewPriv *priv;
566 g_return_if_fail (EMPATHY_IS_PERSONA_VIEW (self));
568 priv = GET_PRIV (self);
569 priv->show_offline = show_offline;
571 gtk_tree_model_filter_refilter (priv->filter);
573 g_object_notify (G_OBJECT (self), "show-offline");