]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
Merge branch 'bug-595305'
[empathy.git] / libempathy-gtk / empathy-individual-information-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 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: Xavier Claessens <xclaesse@gmail.com>
20  *          Philip Withnall <philip.withnall@collabora.co.uk>
21  *          Travis Reitter <travis.reitter@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <gtk/gtk.h>
30 #include <glib/gi18n-lib.h>
31
32 #include <telepathy-glib/util.h>
33 #include <folks/folks.h>
34 #include <folks/folks-telepathy.h>
35
36 #include <libempathy/empathy-individual-manager.h>
37 #include <libempathy/empathy-utils.h>
38
39 #include "empathy-individual-information-dialog.h"
40 #include "empathy-individual-widget.h"
41 #include "empathy-ui-utils.h"
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualInformationDialog)
44 typedef struct {
45   FolksIndividual *individual;
46   GtkWidget *individual_widget; /* child widget */
47   GtkWidget *label; /* child widget */
48 } EmpathyIndividualInformationDialogPriv;
49
50 enum {
51   PROP_0,
52   PROP_INDIVIDUAL,
53 };
54
55 /* Info dialogs currently open.
56  * Each dialog contains a referenced pointer to its Individual */
57 static GList *information_dialogs = NULL;
58
59 static void individual_information_dialog_set_individual (
60     EmpathyIndividualInformationDialog *dialog,
61     FolksIndividual *individual);
62
63 G_DEFINE_TYPE (EmpathyIndividualInformationDialog,
64     empathy_individual_information_dialog, GTK_TYPE_DIALOG);
65
66 static void
67 individual_dialogs_response_cb (GtkDialog *dialog,
68     gint response,
69     GList **dialogs)
70 {
71   *dialogs = g_list_remove (*dialogs, dialog);
72   gtk_widget_destroy (GTK_WIDGET (dialog));
73 }
74
75 static gint
76 individual_dialogs_find (GObject *object,
77     FolksIndividual *individual)
78 {
79   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
80
81   return individual != priv->individual;
82 }
83
84 void
85 empathy_individual_information_dialog_show (FolksIndividual *individual,
86     GtkWindow *parent)
87 {
88   GtkWidget *dialog;
89   GList *l;
90
91   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
92   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
93
94   l = g_list_find_custom (information_dialogs, individual,
95       (GCompareFunc) individual_dialogs_find);
96
97   if (l != NULL)
98     {
99       gtk_window_present (GTK_WINDOW (l->data));
100       return;
101     }
102
103   /* Create dialog */
104   dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
105       "individual", individual,
106       NULL);
107
108   information_dialogs = g_list_prepend (information_dialogs, dialog);
109   gtk_widget_show (dialog);
110 }
111
112 static void
113 individual_removed_cb (FolksIndividual *individual,
114     FolksIndividual *replacement_individual,
115     EmpathyIndividualInformationDialog *self)
116 {
117   individual_information_dialog_set_individual (self,
118       replacement_individual);
119
120   /* Destroy the dialogue */
121   if (replacement_individual == NULL)
122     {
123       individual_dialogs_response_cb (GTK_DIALOG (self),
124           GTK_RESPONSE_DELETE_EVENT, &information_dialogs);
125     }
126 }
127
128 static void
129 set_label_visibility (EmpathyIndividualInformationDialog *dialog)
130 {
131   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (dialog);
132   GList *personas, *l;
133   guint num_personas = 0;
134
135   /* Count how many Telepathy personas we have, to see whether we can
136    * unlink */
137   if (priv->individual != NULL)
138     {
139       personas = folks_individual_get_personas (priv->individual);
140       for (l = personas; l != NULL; l = l->next)
141         {
142           if (TPF_IS_PERSONA (l->data))
143             num_personas++;
144         }
145     }
146
147   /* Only make the label visible if we have enough personas */
148   gtk_widget_set_visible (priv->label, (num_personas > 1) ? TRUE : FALSE);
149 }
150
151 static void
152 individual_information_dialog_set_individual (
153     EmpathyIndividualInformationDialog *dialog,
154     FolksIndividual *individual)
155 {
156   EmpathyIndividualInformationDialogPriv *priv;
157
158   g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
159   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
160
161   priv = GET_PRIV (dialog);
162
163   /* Remove the old Individual */
164   if (priv->individual != NULL)
165     {
166       g_signal_handlers_disconnect_by_func (priv->individual,
167           (GCallback) individual_removed_cb, dialog);
168     }
169
170   tp_clear_object (&priv->individual);
171
172   /* Add the new Individual */
173   priv->individual = individual;
174
175   if (individual != NULL)
176     {
177       g_object_ref (individual);
178       g_signal_connect (individual, "removed",
179           (GCallback) individual_removed_cb, dialog);
180
181       /* Update the UI */
182       gtk_window_set_title (GTK_WINDOW (dialog),
183           folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
184       empathy_individual_widget_set_individual (
185           EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
186       set_label_visibility (dialog);
187     }
188 }
189
190 static void
191 individual_information_dialog_get_property (GObject *object,
192     guint param_id,
193     GValue *value,
194     GParamSpec *pspec)
195 {
196   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
197
198   switch (param_id) {
199   case PROP_INDIVIDUAL:
200     g_value_set_object (value, priv->individual);
201     break;
202   default:
203     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
204     break;
205   };
206 }
207
208 static void
209 individual_information_dialog_set_property (GObject *object,
210   guint param_id,
211   const GValue *value,
212   GParamSpec   *pspec)
213 {
214   EmpathyIndividualInformationDialog *dialog =
215     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
216
217   switch (param_id) {
218   case PROP_INDIVIDUAL:
219     individual_information_dialog_set_individual (dialog,
220         FOLKS_INDIVIDUAL (g_value_get_object (value)));
221     break;
222   default:
223     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
224     break;
225   };
226 }
227
228 static void
229 individual_information_dialog_dispose (GObject *object)
230 {
231   individual_information_dialog_set_individual (
232       EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object), NULL);
233
234   G_OBJECT_CLASS (
235       empathy_individual_information_dialog_parent_class)->dispose (object);
236 }
237
238 static void
239 empathy_individual_information_dialog_class_init (
240     EmpathyIndividualInformationDialogClass *klass)
241 {
242   GObjectClass *object_class = G_OBJECT_CLASS (klass);
243
244   object_class->dispose = individual_information_dialog_dispose;
245   object_class->get_property = individual_information_dialog_get_property;
246   object_class->set_property = individual_information_dialog_set_property;
247
248   g_object_class_install_property (object_class,
249       PROP_INDIVIDUAL,
250       g_param_spec_object ("individual",
251           "Folks Individual",
252           "Folks Individual to base the dialog upon",
253           FOLKS_TYPE_INDIVIDUAL,
254           G_PARAM_CONSTRUCT |
255           G_PARAM_READWRITE |
256           G_PARAM_STATIC_STRINGS));
257
258   g_type_class_add_private (object_class,
259       sizeof (EmpathyIndividualInformationDialogPriv));
260 }
261
262 static void
263 empathy_individual_information_dialog_init (
264     EmpathyIndividualInformationDialog *dialog)
265 {
266   GtkWidget *button;
267   GtkBox *content_area;
268   gchar *label_string;
269   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
270       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
271       EmpathyIndividualInformationDialogPriv);
272
273   dialog->priv = priv;
274   priv->individual = NULL;
275
276   gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
277
278   content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
279
280   /* Translators: the heading at the top of the Information dialogue */
281   label_string = g_strdup_printf ("<b>%s</b>", _("Linked Contacts"));
282   priv->label = gtk_label_new (NULL);
283   gtk_label_set_markup (GTK_LABEL (priv->label), label_string);
284   g_free (label_string);
285
286   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
287   gtk_misc_set_padding (GTK_MISC (priv->label), 6, 6);
288   gtk_box_pack_start (content_area, priv->label, FALSE, TRUE, 0);
289   gtk_widget_show (priv->label);
290
291   /* Individual widget */
292   priv->individual_widget = empathy_individual_widget_new (priv->individual,
293       EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
294       EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS |
295       EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS);
296   gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 6);
297   gtk_box_pack_start (content_area, priv->individual_widget, TRUE, TRUE, 0);
298   gtk_widget_show (priv->individual_widget);
299
300   /* Close button */
301   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
302   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
303   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
304       GTK_RESPONSE_CLOSE);
305   gtk_widget_set_can_default (button, TRUE);
306   gtk_window_set_default (GTK_WINDOW (dialog), button);
307   gtk_widget_show (button);
308
309   g_signal_connect (dialog, "response",
310       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
311 }