]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-widget.c
Add EmpathyIndividualWidget
[empathy.git] / libempathy-gtk / empathy-individual-widget.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Philip Withnall <philip.withnall@collabora.co.uk>
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <gtk/gtk.h>
27 #include <glib/gi18n-lib.h>
28
29 #include <telepathy-glib/util.h>
30
31 #include <libempathy/empathy-utils.h>
32
33 #include <folks/folks-telepathy.h>
34
35 #include "empathy-individual-widget.h"
36 #include "empathy-gtk-enum-types.h"
37
38 /**
39  * SECTION:empathy-individual-widget
40  * @title:EmpathyIndividualWidget
41  * @short_description: A widget used to display and edit details about an
42  * individual
43  * @include: libempathy-empathy-individual-widget.h
44  *
45  * #EmpathyIndividualWidget is a widget which displays appropriate widgets
46  * with details about an individual, also allowing changing these details,
47  * if desired.
48  */
49
50 /**
51  * EmpathyIndividualWidget:
52  * @parent: parent object
53  *
54  * Widget which displays appropriate widgets with details about an individual,
55  * also allowing changing these details, if desired.
56  *
57  * Currently, it's just a thin wrapper around #EmpathyContactWidget, and
58  * displays the details of the first eligible persona found in the individual.
59  */
60
61 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualWidget)
62
63 typedef struct {
64   FolksIndividual *individual;
65   EmpathyIndividualWidgetFlags flags;
66 } EmpathyIndividualWidgetPriv;
67
68 G_DEFINE_TYPE (EmpathyIndividualWidget, empathy_individual_widget,
69     GTK_TYPE_BOX);
70
71 enum {
72   PROP_INDIVIDUAL = 1,
73   PROP_FLAGS
74 };
75
76 static void
77 empathy_individual_widget_init (EmpathyIndividualWidget *self)
78 {
79   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
80       EMPATHY_TYPE_INDIVIDUAL_WIDGET, EmpathyIndividualWidgetPriv);
81
82   gtk_orientable_set_orientation (GTK_ORIENTABLE (self),
83       GTK_ORIENTATION_VERTICAL);
84 }
85
86 static void
87 get_property (GObject *object,
88     guint param_id,
89     GValue *value,
90     GParamSpec *pspec)
91 {
92   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
93
94   switch (param_id)
95     {
96       case PROP_INDIVIDUAL:
97         g_value_set_object (value, priv->individual);
98         break;
99       case PROP_FLAGS:
100         g_value_set_flags (value, priv->flags);
101         break;
102       default:
103         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
104         break;
105     }
106 }
107
108 static void
109 set_property (GObject *object,
110     guint param_id,
111     const GValue *value,
112     GParamSpec *pspec)
113 {
114   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
115
116   switch (param_id)
117     {
118       case PROP_INDIVIDUAL:
119         empathy_individual_widget_set_individual (
120             EMPATHY_INDIVIDUAL_WIDGET (object), g_value_get_object (value));
121         break;
122       case PROP_FLAGS:
123         priv->flags = g_value_get_flags (value);
124         break;
125       default:
126         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
127         break;
128     }
129 }
130
131 static void
132 dispose (GObject *object)
133 {
134   EmpathyIndividualWidgetPriv *priv = GET_PRIV (object);
135
136   tp_clear_object (&priv->individual);
137
138   G_OBJECT_CLASS (empathy_individual_widget_parent_class)->dispose (object);
139 }
140
141 static void
142 empathy_individual_widget_class_init (EmpathyIndividualWidgetClass *klass)
143 {
144   GObjectClass *object_class = G_OBJECT_CLASS (klass);
145
146   object_class->get_property = get_property;
147   object_class->set_property = set_property;
148   object_class->dispose = dispose;
149
150   /**
151    * EmpathyIndividualWidget:individual:
152    *
153    * The #FolksIndividual to display in the widget.
154    */
155   g_object_class_install_property (object_class, PROP_INDIVIDUAL,
156       g_param_spec_object ("individual",
157           "Individual",
158           "The #FolksIndividual to display in the widget.",
159           FOLKS_TYPE_INDIVIDUAL,
160           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161
162   /**
163    * EmpathyIndividualWidget:flags:
164    *
165    * A set of flags which affect the widget's behaviour.
166    */
167   g_object_class_install_property (object_class, PROP_FLAGS,
168       g_param_spec_flags ("flags",
169           "Flags",
170           "A set of flags which affect the widget's behaviour.",
171           EMPATHY_TYPE_INDIVIDUAL_WIDGET_FLAGS,
172           EMPATHY_INDIVIDUAL_WIDGET_EDIT_NONE,
173           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
174
175   g_type_class_add_private (object_class, sizeof (EmpathyIndividualWidgetPriv));
176 }
177
178 /**
179  * empathy_individual_widget_new:
180  * @individual: the #FolksIndividual to display
181  * @flags: flags affecting how the widget behaves and what it displays
182  *
183  * Creates a new #EmpathyIndividualWidget.
184  *
185  * Return value: a new #EmpathyIndividualWidget
186  */
187 GtkWidget *
188 empathy_individual_widget_new (FolksIndividual *individual,
189     EmpathyIndividualWidgetFlags flags)
190 {
191   g_return_val_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual),
192       NULL);
193
194   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_WIDGET,
195       "individual", individual, "flags", flags, NULL);
196 }
197
198 FolksIndividual *
199 empathy_individual_widget_get_individual (EmpathyIndividualWidget *self)
200 {
201   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self), NULL);
202
203   return GET_PRIV (self)->individual;
204 }
205
206 void
207 empathy_individual_widget_set_individual (EmpathyIndividualWidget *self,
208     FolksIndividual *individual)
209 {
210   EmpathyIndividualWidgetPriv *priv;
211   GList *personas = NULL, *l;
212
213   g_return_if_fail (EMPATHY_IS_INDIVIDUAL_WIDGET (self));
214   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
215
216   priv = GET_PRIV (self);
217
218   /* Out with the old… */
219   gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback) gtk_widget_destroy,
220       NULL);
221   tp_clear_object (&priv->individual);
222
223   /* …and in with the new. */
224   priv->individual = individual;
225   if (individual != NULL)
226     {
227       g_object_ref (individual);
228       personas = folks_individual_get_personas (individual);
229     }
230
231   for (l = personas; l != NULL; l = l->next)
232     {
233       GtkWidget *contact_widget;
234       TpContact *tp_contact;
235       EmpathyContact *contact;
236       TpfPersona *persona = l->data;
237
238       if (!TPF_IS_PERSONA (persona))
239         continue;
240
241       tp_contact = tpf_persona_get_contact (persona);
242       contact = empathy_contact_dup_from_tp_contact (tp_contact);
243
244       /* Contact info widget */
245       contact_widget = empathy_contact_widget_new (contact, priv->flags);
246       gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
247       gtk_box_pack_start (GTK_BOX (self), contact_widget, TRUE, TRUE, 0);
248       gtk_widget_show (contact_widget);
249
250       g_object_unref (contact);
251
252       /* If we're not meant to display all of the personas, bail after the first
253        * one. */
254       if (!(priv->flags & EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS))
255         break;
256     }
257 }