]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
Use gnome-contacts instead of the information dialog
[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   guint num_personas = 0;
133
134   /* Count how many Telepathy personas we have, to see whether we can
135    * unlink */
136   if (priv->individual != NULL)
137     {
138       GeeSet *personas;
139       GeeIterator *iter;
140
141       personas = folks_individual_get_personas (priv->individual);
142       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
143       while (gee_iterator_next (iter))
144         {
145           FolksPersona *persona = gee_iterator_get (iter);
146           if (empathy_folks_persona_is_interesting (persona))
147             num_personas++;
148
149           g_clear_object (&persona);
150         }
151       g_clear_object (&iter);
152     }
153
154   /* Only make the label visible if we have enough personas */
155   gtk_widget_set_visible (priv->label, (num_personas > 1) ? TRUE : FALSE);
156 }
157
158 static void
159 individual_information_dialog_set_individual (
160     EmpathyIndividualInformationDialog *dialog,
161     FolksIndividual *individual)
162 {
163   EmpathyIndividualInformationDialogPriv *priv;
164
165   g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
166   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
167
168   priv = GET_PRIV (dialog);
169
170   /* Remove the old Individual */
171   if (priv->individual != NULL)
172     {
173       g_signal_handlers_disconnect_by_func (priv->individual,
174           (GCallback) individual_removed_cb, dialog);
175     }
176
177   tp_clear_object (&priv->individual);
178
179   /* Add the new Individual */
180   priv->individual = individual;
181
182   if (individual != NULL)
183     {
184       g_object_ref (individual);
185       g_signal_connect (individual, "removed",
186           (GCallback) individual_removed_cb, dialog);
187
188       /* Update the UI */
189       gtk_window_set_title (GTK_WINDOW (dialog),
190           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
191       empathy_individual_widget_set_individual (
192           EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
193       set_label_visibility (dialog);
194     }
195 }
196
197 static void
198 individual_information_dialog_get_property (GObject *object,
199     guint param_id,
200     GValue *value,
201     GParamSpec *pspec)
202 {
203   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
204
205   switch (param_id) {
206   case PROP_INDIVIDUAL:
207     g_value_set_object (value, priv->individual);
208     break;
209   default:
210     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
211     break;
212   };
213 }
214
215 static void
216 individual_information_dialog_set_property (GObject *object,
217   guint param_id,
218   const GValue *value,
219   GParamSpec   *pspec)
220 {
221   EmpathyIndividualInformationDialog *dialog =
222     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
223
224   switch (param_id) {
225   case PROP_INDIVIDUAL:
226     individual_information_dialog_set_individual (dialog,
227         FOLKS_INDIVIDUAL (g_value_get_object (value)));
228     break;
229   default:
230     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
231     break;
232   };
233 }
234
235 static void
236 individual_information_dialog_dispose (GObject *object)
237 {
238   individual_information_dialog_set_individual (
239       EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object), NULL);
240
241   G_OBJECT_CLASS (
242       empathy_individual_information_dialog_parent_class)->dispose (object);
243 }
244
245 static void
246 empathy_individual_information_dialog_class_init (
247     EmpathyIndividualInformationDialogClass *klass)
248 {
249   GObjectClass *object_class = G_OBJECT_CLASS (klass);
250
251   object_class->dispose = individual_information_dialog_dispose;
252   object_class->get_property = individual_information_dialog_get_property;
253   object_class->set_property = individual_information_dialog_set_property;
254
255   g_object_class_install_property (object_class,
256       PROP_INDIVIDUAL,
257       g_param_spec_object ("individual",
258           "Folks Individual",
259           "Folks Individual to base the dialog upon",
260           FOLKS_TYPE_INDIVIDUAL,
261           G_PARAM_CONSTRUCT |
262           G_PARAM_READWRITE |
263           G_PARAM_STATIC_STRINGS));
264
265   g_type_class_add_private (object_class,
266       sizeof (EmpathyIndividualInformationDialogPriv));
267 }
268
269 static void
270 empathy_individual_information_dialog_init (
271     EmpathyIndividualInformationDialog *dialog)
272 {
273   GtkWidget *button;
274   GtkBox *content_area;
275   gchar *label_string;
276   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
277       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
278       EmpathyIndividualInformationDialogPriv);
279
280   dialog->priv = priv;
281   priv->individual = NULL;
282
283   gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
284
285   content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
286
287   /* Translators: the heading at the top of the Information dialogue */
288   label_string = g_strdup_printf ("<b>%s</b>", _("Linked Contacts"));
289   priv->label = gtk_label_new (NULL);
290   gtk_label_set_markup (GTK_LABEL (priv->label), label_string);
291   g_free (label_string);
292
293   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
294   gtk_misc_set_padding (GTK_MISC (priv->label), 6, 6);
295   gtk_box_pack_start (content_area, priv->label, FALSE, TRUE, 0);
296   gtk_widget_show (priv->label);
297
298   /* Individual widget */
299   priv->individual_widget = empathy_individual_widget_new (priv->individual,
300       EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
301       EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS |
302       EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS);
303   gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 6);
304   gtk_box_pack_start (content_area, priv->individual_widget, TRUE, TRUE, 0);
305   gtk_widget_show (priv->individual_widget);
306
307   /* Close button */
308   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
309   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
310   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
311       GTK_RESPONSE_CLOSE);
312   gtk_widget_set_can_default (button, TRUE);
313   gtk_window_set_default (GTK_WINDOW (dialog), button);
314   gtk_widget_show (button);
315
316   g_signal_connect (dialog, "response",
317       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
318 }