]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-roster-contact.c
include telepathy-glib.h
[empathy.git] / libempathy-gtk / empathy-roster-contact.c
1 #include "config.h"
2
3 #include "empathy-roster-contact.h"
4
5 #include <telepathy-glib/telepathy-glib.h>
6
7 #include <libempathy/empathy-utils.h>
8
9 #include <libempathy-gtk/empathy-images.h>
10 #include <libempathy-gtk/empathy-ui-utils.h>
11
12 G_DEFINE_TYPE (EmpathyRosterContact, empathy_roster_contact, GTK_TYPE_ALIGNMENT)
13
14 #define AVATAR_SIZE 48
15
16 enum
17 {
18   PROP_INDIVIDIUAL = 1,
19   PROP_GROUP,
20   PROP_ONLINE,
21   PROP_ALIAS,
22   N_PROPS
23 };
24
25 /*
26 enum
27 {
28   LAST_SIGNAL
29 };
30
31 static guint signals[LAST_SIGNAL];
32 */
33
34 struct _EmpathyRosterContactPriv
35 {
36   FolksIndividual *individual;
37   gchar *group;
38
39   GtkWidget *avatar;
40   GtkWidget *first_line_alig;
41   GtkWidget *alias;
42   GtkWidget *presence_msg;
43   GtkWidget *presence_icon;
44   GtkWidget *phone_icon;
45
46   /* If not NULL, used instead of the individual's presence icon */
47   gchar *event_icon;
48
49   gboolean online;
50 };
51
52 static const gchar *
53 get_alias (EmpathyRosterContact *self)
54 {
55   return folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (
56         self->priv->individual));
57 }
58
59 static void
60 empathy_roster_contact_get_property (GObject *object,
61     guint property_id,
62     GValue *value,
63     GParamSpec *pspec)
64 {
65   EmpathyRosterContact *self = EMPATHY_ROSTER_CONTACT (object);
66
67   switch (property_id)
68     {
69       case PROP_INDIVIDIUAL:
70         g_value_set_object (value, self->priv->individual);
71         break;
72       case PROP_GROUP:
73         g_value_set_string (value, self->priv->group);
74         break;
75       case PROP_ONLINE:
76         g_value_set_boolean (value, self->priv->online);
77         break;
78       case PROP_ALIAS:
79         g_value_set_string (value, get_alias (self));
80         break;
81       default:
82         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83         break;
84     }
85 }
86
87 static void
88 empathy_roster_contact_set_property (GObject *object,
89     guint property_id,
90     const GValue *value,
91     GParamSpec *pspec)
92 {
93   EmpathyRosterContact *self = EMPATHY_ROSTER_CONTACT (object);
94
95   switch (property_id)
96     {
97       case PROP_INDIVIDIUAL:
98         g_assert (self->priv->individual == NULL); /* construct only */
99         self->priv->individual = g_value_dup_object (value);
100         break;
101       case PROP_GROUP:
102         g_assert (self->priv->group == NULL); /* construct only */
103         self->priv->group = g_value_dup_string (value);
104         break;
105       default:
106         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107         break;
108     }
109 }
110
111 static void
112 avatar_loaded_cb (GObject *source,
113     GAsyncResult *result,
114     gpointer user_data)
115 {
116   TpWeakRef *wr = user_data;
117   EmpathyRosterContact *self;
118   GdkPixbuf *pixbuf;
119
120   self = tp_weak_ref_dup_object (wr);
121   if (self == NULL)
122     goto out;
123
124   pixbuf = empathy_pixbuf_avatar_from_individual_scaled_finish (
125       FOLKS_INDIVIDUAL (source), result, NULL);
126
127   if (pixbuf == NULL)
128     {
129       pixbuf = empathy_pixbuf_from_icon_name_sized (
130           EMPATHY_IMAGE_AVATAR_DEFAULT, AVATAR_SIZE);
131     }
132
133   gtk_image_set_from_pixbuf (GTK_IMAGE (self->priv->avatar), pixbuf);
134   g_object_unref (pixbuf);
135
136   g_object_unref (self);
137
138 out:
139   tp_weak_ref_destroy (wr);
140 }
141
142 static void
143 update_avatar (EmpathyRosterContact *self)
144 {
145   empathy_pixbuf_avatar_from_individual_scaled_async (self->priv->individual,
146       AVATAR_SIZE, AVATAR_SIZE, NULL, avatar_loaded_cb,
147       tp_weak_ref_new (self, NULL, NULL));
148 }
149
150 static void
151 avatar_changed_cb (FolksIndividual *individual,
152     GParamSpec *spec,
153     EmpathyRosterContact *self)
154 {
155   update_avatar (self);
156 }
157
158 static void
159 update_alias (EmpathyRosterContact *self)
160 {
161   gtk_label_set_text (GTK_LABEL (self->priv->alias), get_alias (self));
162
163   g_object_notify (G_OBJECT (self), "alias");
164 }
165
166 static void
167 alias_changed_cb (FolksIndividual *individual,
168     GParamSpec *spec,
169     EmpathyRosterContact *self)
170 {
171   update_alias (self);
172 }
173
174 static gboolean
175 is_phone (FolksIndividual *individual)
176 {
177   const gchar * const *types;
178
179   types = empathy_individual_get_client_types (individual);
180   if (types == NULL)
181     return FALSE;
182
183   if (g_strv_length ((GStrv) types) <= 0)
184     return FALSE;
185
186   return !tp_strdiff (types[0], "phone");
187 }
188
189 static void
190 update_presence_msg (EmpathyRosterContact *self)
191 {
192   const gchar *msg;
193
194   msg = folks_presence_details_get_presence_message (
195       FOLKS_PRESENCE_DETAILS (self->priv->individual));
196
197   if (tp_str_empty (msg))
198     {
199       /* Just display the alias in the center of the row */
200       gtk_alignment_set (GTK_ALIGNMENT (self->priv->first_line_alig),
201           0, 0.5, 1, 1);
202
203       gtk_widget_hide (self->priv->presence_msg);
204     }
205   else
206     {
207       gtk_label_set_text (GTK_LABEL (self->priv->presence_msg), msg);
208
209       gtk_alignment_set (GTK_ALIGNMENT (self->priv->first_line_alig),
210           0, 0.75, 1, 1);
211       gtk_misc_set_alignment (GTK_MISC (self->priv->presence_msg), 0, 0.25);
212
213       gtk_widget_show (self->priv->presence_msg);
214     }
215
216   gtk_widget_set_visible (self->priv->phone_icon,
217       is_phone (self->priv->individual));
218 }
219
220 static void
221 presence_message_changed_cb (FolksIndividual *individual,
222     GParamSpec *spec,
223     EmpathyRosterContact *self)
224 {
225   update_presence_msg (self);
226 }
227
228 static void
229 update_presence_icon (EmpathyRosterContact *self)
230 {
231   const gchar *icon;
232
233   if (self->priv->event_icon == NULL)
234     icon = empathy_icon_name_for_individual (self->priv->individual);
235   else
236     icon = self->priv->event_icon;
237
238   gtk_image_set_from_icon_name (GTK_IMAGE (self->priv->presence_icon), icon,
239       GTK_ICON_SIZE_MENU);
240 }
241
242 static void
243 update_online (EmpathyRosterContact *self)
244 {
245   FolksPresenceType presence;
246   gboolean online;
247
248   presence = folks_presence_details_get_presence_type (
249       FOLKS_PRESENCE_DETAILS (self->priv->individual));
250
251   switch (presence)
252     {
253       case FOLKS_PRESENCE_TYPE_UNSET:
254       case FOLKS_PRESENCE_TYPE_OFFLINE:
255       case FOLKS_PRESENCE_TYPE_UNKNOWN:
256       case FOLKS_PRESENCE_TYPE_ERROR:
257         online = FALSE;
258         break;
259
260       case FOLKS_PRESENCE_TYPE_AVAILABLE:
261       case FOLKS_PRESENCE_TYPE_AWAY:
262       case FOLKS_PRESENCE_TYPE_EXTENDED_AWAY:
263       case FOLKS_PRESENCE_TYPE_HIDDEN:
264       case FOLKS_PRESENCE_TYPE_BUSY:
265         online = TRUE;
266         break;
267
268       default:
269         g_warning ("Unknown FolksPresenceType: %d", presence);
270         online = FALSE;
271     }
272
273   if (self->priv->online == online)
274     return;
275
276   self->priv->online = online;
277   g_object_notify (G_OBJECT (self), "online");
278 }
279
280 static void
281 presence_status_changed_cb (FolksIndividual *individual,
282     GParamSpec *spec,
283     EmpathyRosterContact *self)
284 {
285   update_presence_icon (self);
286   update_online (self);
287 }
288
289 static void
290 empathy_roster_contact_constructed (GObject *object)
291 {
292   EmpathyRosterContact *self = EMPATHY_ROSTER_CONTACT (object);
293   void (*chain_up) (GObject *) =
294       ((GObjectClass *) empathy_roster_contact_parent_class)->constructed;
295
296   if (chain_up != NULL)
297     chain_up (object);
298
299   g_assert (FOLKS_IS_INDIVIDUAL (self->priv->individual));
300
301   tp_g_signal_connect_object (self->priv->individual, "notify::avatar",
302       G_CALLBACK (avatar_changed_cb), self, 0);
303   tp_g_signal_connect_object (self->priv->individual, "notify::alias",
304       G_CALLBACK (alias_changed_cb), self, 0);
305   tp_g_signal_connect_object (self->priv->individual,
306       "notify::presence-message",
307       G_CALLBACK (presence_message_changed_cb), self, 0);
308   tp_g_signal_connect_object (self->priv->individual, "notify::presence-status",
309       G_CALLBACK (presence_status_changed_cb), self, 0);
310
311   update_avatar (self);
312   update_alias (self);
313   update_presence_msg (self);
314   update_presence_icon (self);
315
316   update_online (self);
317 }
318
319 static void
320 empathy_roster_contact_dispose (GObject *object)
321 {
322   EmpathyRosterContact *self = EMPATHY_ROSTER_CONTACT (object);
323   void (*chain_up) (GObject *) =
324       ((GObjectClass *) empathy_roster_contact_parent_class)->dispose;
325
326   g_clear_object (&self->priv->individual);
327
328   if (chain_up != NULL)
329     chain_up (object);
330 }
331
332 static void
333 empathy_roster_contact_finalize (GObject *object)
334 {
335   EmpathyRosterContact *self = EMPATHY_ROSTER_CONTACT (object);
336   void (*chain_up) (GObject *) =
337       ((GObjectClass *) empathy_roster_contact_parent_class)->finalize;
338
339   g_free (self->priv->group);
340   g_free (self->priv->event_icon);
341
342   if (chain_up != NULL)
343     chain_up (object);
344 }
345
346 static void
347 empathy_roster_contact_class_init (
348     EmpathyRosterContactClass *klass)
349 {
350   GObjectClass *oclass = G_OBJECT_CLASS (klass);
351   GParamSpec *spec;
352
353   oclass->get_property = empathy_roster_contact_get_property;
354   oclass->set_property = empathy_roster_contact_set_property;
355   oclass->constructed = empathy_roster_contact_constructed;
356   oclass->dispose = empathy_roster_contact_dispose;
357   oclass->finalize = empathy_roster_contact_finalize;
358
359   spec = g_param_spec_object ("individual", "Individual",
360       "FolksIndividual",
361       FOLKS_TYPE_INDIVIDUAL,
362       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
363   g_object_class_install_property (oclass, PROP_INDIVIDIUAL, spec);
364
365   spec = g_param_spec_string ("group", "Group",
366       "Group of this widget, or NULL",
367       NULL,
368       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
369   g_object_class_install_property (oclass, PROP_GROUP, spec);
370
371   spec = g_param_spec_boolean ("online", "Online",
372       "TRUE if Individual is online",
373       FALSE,
374       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
375   g_object_class_install_property (oclass, PROP_ONLINE, spec);
376
377   spec = g_param_spec_string ("alias", "Alias",
378       "The Alias of the individual displayed in the widget",
379       NULL,
380       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
381   g_object_class_install_property (oclass, PROP_ALIAS, spec);
382
383   g_type_class_add_private (klass, sizeof (EmpathyRosterContactPriv));
384 }
385
386 static void
387 empathy_roster_contact_init (EmpathyRosterContact *self)
388 {
389   GtkWidget *main_box, *box, *first_line_box;
390   GtkStyleContext *context;
391
392   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
393       EMPATHY_TYPE_ROSTER_CONTACT, EmpathyRosterContactPriv);
394
395   main_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
396
397   /* Avatar */
398   self->priv->avatar = gtk_image_new ();
399
400   gtk_widget_set_size_request (self->priv->avatar, AVATAR_SIZE, AVATAR_SIZE);
401
402   gtk_box_pack_start (GTK_BOX (main_box), self->priv->avatar, FALSE, FALSE, 0);
403   gtk_widget_show (self->priv->avatar);
404
405   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
406
407   /* Alias and phone icon */
408   self->priv->first_line_alig = gtk_alignment_new (0, 0.5, 1, 1);
409   first_line_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
410
411   self->priv->alias = gtk_label_new (NULL);
412   gtk_label_set_ellipsize (GTK_LABEL (self->priv->alias), PANGO_ELLIPSIZE_END);
413   gtk_box_pack_start (GTK_BOX (first_line_box), self->priv->alias,
414       FALSE, FALSE, 0);
415   gtk_misc_set_alignment (GTK_MISC (self->priv->alias), 0, 0.5);
416   gtk_widget_show (self->priv->alias);
417
418   self->priv->phone_icon = gtk_image_new_from_icon_name ("phone-symbolic",
419       GTK_ICON_SIZE_MENU);
420   gtk_misc_set_alignment (GTK_MISC (self->priv->phone_icon), 0, 0.5);
421   gtk_box_pack_start (GTK_BOX (first_line_box), self->priv->phone_icon,
422       TRUE, TRUE, 0);
423
424   gtk_container_add (GTK_CONTAINER (self->priv->first_line_alig),
425       first_line_box);
426   gtk_widget_show (self->priv->first_line_alig);
427
428   gtk_box_pack_start (GTK_BOX (box), self->priv->first_line_alig,
429       TRUE, TRUE, 0);
430   gtk_widget_show (first_line_box);
431
432   gtk_box_pack_start (GTK_BOX (main_box), box, TRUE, TRUE, 0);
433   gtk_widget_show (box);
434
435   /* Presence */
436   self->priv->presence_msg = gtk_label_new (NULL);
437   gtk_label_set_ellipsize (GTK_LABEL (self->priv->presence_msg),
438       PANGO_ELLIPSIZE_END);
439   gtk_box_pack_start (GTK_BOX (box), self->priv->presence_msg, TRUE, TRUE, 0);
440   gtk_widget_show (self->priv->presence_msg);
441
442   context = gtk_widget_get_style_context (self->priv->presence_msg);
443   gtk_style_context_add_class (context, GTK_STYLE_CLASS_DIM_LABEL);
444
445   /* Presence icon */
446   self->priv->presence_icon = gtk_image_new ();
447
448   gtk_box_pack_start (GTK_BOX (main_box), self->priv->presence_icon,
449       FALSE, FALSE, 0);
450   gtk_widget_show (self->priv->presence_icon);
451
452   gtk_container_add (GTK_CONTAINER (self), main_box);
453   gtk_widget_show (main_box);
454 }
455
456 GtkWidget *
457 empathy_roster_contact_new (FolksIndividual *individual,
458     const gchar *group)
459 {
460   g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
461
462   return g_object_new (EMPATHY_TYPE_ROSTER_CONTACT,
463       "individual", individual,
464       "group", group,
465       "bottom-padding", 4,
466       "top-padding", 4,
467       "left-padding", 4,
468       "right-padding", 12,
469       NULL);
470 }
471
472 FolksIndividual *
473 empathy_roster_contact_get_individual (EmpathyRosterContact *self)
474 {
475   return self->priv->individual;
476 }
477
478 gboolean
479 empathy_roster_contact_is_online (EmpathyRosterContact *self)
480 {
481   return self->priv->online;
482 }
483
484 const gchar *
485 empathy_roster_contact_get_group (EmpathyRosterContact *self)
486 {
487   return self->priv->group;
488 }
489
490 void
491 empathy_roster_contact_set_event_icon (EmpathyRosterContact *self,
492     const gchar *icon)
493 {
494   if (!tp_strdiff (self->priv->event_icon, icon))
495     return;
496
497   g_free (self->priv->event_icon);
498   self->priv->event_icon = g_strdup (icon);
499
500   update_presence_icon (self);
501 }
502
503 GdkPixbuf *
504 empathy_roster_contact_get_avatar_pixbuf (EmpathyRosterContact *self)
505 {
506   return gtk_image_get_pixbuf (GTK_IMAGE (self->priv->avatar));
507 }