]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
f4f8bda928c46161804b3a6c8ad005a054980c33
[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 <glib/gi18n-lib.h>
27
28 #include "empathy-individual-manager.h"
29 #include "empathy-utils.h"
30 #include "empathy-pkg-kit.h"
31
32 #include "empathy-individual-information-dialog.h"
33 #include "empathy-individual-widget.h"
34 #include "empathy-ui-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include "empathy-debug.h"
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualInformationDialog)
40 typedef struct {
41   FolksIndividual *individual;
42   GtkWidget *individual_widget; /* child widget */
43   GtkWidget *label; /* child widget */
44 } EmpathyIndividualInformationDialogPriv;
45
46 enum {
47   PROP_0,
48   PROP_INDIVIDUAL,
49 };
50
51 /* Info dialogs currently open.
52  * Each dialog contains a referenced pointer to its Individual */
53 static GList *information_dialogs = NULL;
54
55 static void individual_information_dialog_set_individual (
56     EmpathyIndividualInformationDialog *dialog,
57     FolksIndividual *individual);
58
59 G_DEFINE_TYPE (EmpathyIndividualInformationDialog,
60     empathy_individual_information_dialog, GTK_TYPE_DIALOG);
61
62 static void
63 individual_dialogs_response_cb (GtkDialog *dialog,
64     gint response,
65     GList **dialogs)
66 {
67   *dialogs = g_list_remove (*dialogs, dialog);
68   gtk_widget_destroy (GTK_WIDGET (dialog));
69 }
70
71 static gint
72 individual_dialogs_find (GObject *object,
73     FolksIndividual *individual)
74 {
75   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
76
77   return individual != priv->individual;
78 }
79
80 void
81 empathy_individual_information_dialog_show (FolksIndividual *individual,
82     GtkWindow *parent)
83 {
84   GtkWidget *dialog;
85   GList *l;
86
87   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
88   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
89
90   l = g_list_find_custom (information_dialogs, individual,
91       (GCompareFunc) individual_dialogs_find);
92
93   if (l != NULL)
94     {
95       gtk_window_present (GTK_WINDOW (l->data));
96       return;
97     }
98
99   /* Create dialog */
100   dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
101       "individual", individual,
102       NULL);
103
104   information_dialogs = g_list_prepend (information_dialogs, dialog);
105   gtk_widget_show (dialog);
106 }
107
108 static void
109 individual_removed_cb (FolksIndividual *individual,
110     FolksIndividual *replacement_individual,
111     EmpathyIndividualInformationDialog *self)
112 {
113   individual_information_dialog_set_individual (self,
114       replacement_individual);
115
116   /* Destroy the dialogue */
117   if (replacement_individual == NULL)
118     {
119       individual_dialogs_response_cb (GTK_DIALOG (self),
120           GTK_RESPONSE_DELETE_EVENT, &information_dialogs);
121     }
122 }
123
124 static void
125 set_label_visibility (EmpathyIndividualInformationDialog *dialog)
126 {
127   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (dialog);
128   guint num_personas = 0;
129
130   /* Count how many Telepathy personas we have, to see whether we can
131    * unlink */
132   if (priv->individual != NULL)
133     {
134       GeeSet *personas;
135       GeeIterator *iter;
136
137       personas = folks_individual_get_personas (priv->individual);
138       iter = gee_iterable_iterator (GEE_ITERABLE (personas));
139       while (gee_iterator_next (iter))
140         {
141           FolksPersona *persona = gee_iterator_get (iter);
142           if (empathy_folks_persona_is_interesting (persona))
143             num_personas++;
144
145           g_clear_object (&persona);
146         }
147       g_clear_object (&iter);
148     }
149
150   /* Only make the label visible if we have enough personas */
151   gtk_widget_set_visible (priv->label, (num_personas > 1) ? TRUE : FALSE);
152 }
153
154 static void
155 individual_information_dialog_set_individual (
156     EmpathyIndividualInformationDialog *dialog,
157     FolksIndividual *individual)
158 {
159   EmpathyIndividualInformationDialogPriv *priv;
160
161   g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
162   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
163
164   priv = GET_PRIV (dialog);
165
166   /* Remove the old Individual */
167   if (priv->individual != NULL)
168     {
169       g_signal_handlers_disconnect_by_func (priv->individual,
170           (GCallback) individual_removed_cb, dialog);
171     }
172
173   tp_clear_object (&priv->individual);
174
175   /* Add the new Individual */
176   priv->individual = individual;
177
178   if (individual != NULL)
179     {
180       g_object_ref (individual);
181       g_signal_connect (individual, "removed",
182           (GCallback) individual_removed_cb, dialog);
183
184       /* Update the UI */
185       gtk_window_set_title (GTK_WINDOW (dialog),
186           folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
187       empathy_individual_widget_set_individual (
188           EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
189       set_label_visibility (dialog);
190     }
191 }
192
193 static void
194 individual_information_dialog_get_property (GObject *object,
195     guint param_id,
196     GValue *value,
197     GParamSpec *pspec)
198 {
199   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
200
201   switch (param_id) {
202   case PROP_INDIVIDUAL:
203     g_value_set_object (value, priv->individual);
204     break;
205   default:
206     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
207     break;
208   };
209 }
210
211 static void
212 individual_information_dialog_set_property (GObject *object,
213   guint param_id,
214   const GValue *value,
215   GParamSpec   *pspec)
216 {
217   EmpathyIndividualInformationDialog *dialog =
218     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
219
220   switch (param_id) {
221   case PROP_INDIVIDUAL:
222     individual_information_dialog_set_individual (dialog,
223         FOLKS_INDIVIDUAL (g_value_get_object (value)));
224     break;
225   default:
226     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
227     break;
228   };
229 }
230
231 static void
232 individual_information_dialog_dispose (GObject *object)
233 {
234   individual_information_dialog_set_individual (
235       EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object), NULL);
236
237   G_OBJECT_CLASS (
238       empathy_individual_information_dialog_parent_class)->dispose (object);
239 }
240
241 static void
242 empathy_individual_information_dialog_class_init (
243     EmpathyIndividualInformationDialogClass *klass)
244 {
245   GObjectClass *object_class = G_OBJECT_CLASS (klass);
246
247   object_class->dispose = individual_information_dialog_dispose;
248   object_class->get_property = individual_information_dialog_get_property;
249   object_class->set_property = individual_information_dialog_set_property;
250
251   g_object_class_install_property (object_class,
252       PROP_INDIVIDUAL,
253       g_param_spec_object ("individual",
254           "Folks Individual",
255           "Folks Individual to base the dialog upon",
256           FOLKS_TYPE_INDIVIDUAL,
257           G_PARAM_CONSTRUCT |
258           G_PARAM_READWRITE |
259           G_PARAM_STATIC_STRINGS));
260
261   g_type_class_add_private (object_class,
262       sizeof (EmpathyIndividualInformationDialogPriv));
263 }
264
265 static void
266 empathy_individual_information_dialog_init (
267     EmpathyIndividualInformationDialog *dialog)
268 {
269   GtkWidget *button;
270   GtkBox *content_area;
271   gchar *label_string;
272   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
273       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
274       EmpathyIndividualInformationDialogPriv);
275
276   dialog->priv = priv;
277   priv->individual = NULL;
278
279   gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
280
281   content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
282
283   /* Translators: the heading at the top of the Information dialogue */
284   label_string = g_strdup_printf ("<b>%s</b>", _("Linked Contacts"));
285   priv->label = gtk_label_new (NULL);
286   gtk_label_set_markup (GTK_LABEL (priv->label), label_string);
287   g_free (label_string);
288
289   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
290   gtk_misc_set_padding (GTK_MISC (priv->label), 6, 6);
291   gtk_box_pack_start (content_area, priv->label, FALSE, TRUE, 0);
292   gtk_widget_show (priv->label);
293
294   /* Individual widget */
295   priv->individual_widget = empathy_individual_widget_new (priv->individual,
296       EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
297       EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS |
298       EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS);
299   gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 6);
300   gtk_box_pack_start (content_area, priv->individual_widget, TRUE, TRUE, 0);
301   gtk_widget_show (priv->individual_widget);
302
303   /* Close button */
304   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
305   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
306   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
307       GTK_RESPONSE_CLOSE);
308   gtk_widget_set_can_default (button, TRUE);
309   gtk_window_set_default (GTK_WINDOW (dialog), button);
310   gtk_widget_show (button);
311
312   g_signal_connect (dialog, "response",
313       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
314 }
315
316 static void
317 show_gnome_contacts_error_dialog (void)
318 {
319   GtkWidget *dialog;
320
321   dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
322       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
323       _("gnome-contacts not installed"));
324
325   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
326       _("Please install gnome-contacts to access contacts details."));
327
328   g_signal_connect_swapped (dialog, "response",
329           G_CALLBACK (gtk_widget_destroy), dialog);
330
331   gtk_widget_show (dialog);
332 }
333
334 static void
335 start_gnome_contacts (FolksIndividual *individual,
336     gboolean try_installing);
337
338 static void
339 install_gnome_contacts_cb (GObject *source,
340     GAsyncResult *result,
341     gpointer user_data)
342 {
343   FolksIndividual *individual = user_data;
344   GError *error = NULL;
345
346   if (!empathy_pkg_kit_install_packages_finish (result, &error))
347     {
348       DEBUG ("Failed to install gnome-contacts: %s", error->message);
349       g_error_free (error);
350
351       show_gnome_contacts_error_dialog ();
352       goto out;
353     }
354
355   DEBUG ("gnome-contacts installed");
356
357   start_gnome_contacts (individual, FALSE);
358
359 out:
360   g_object_unref (individual);
361 }
362
363 static void
364 start_gnome_contacts (FolksIndividual *individual,
365     gboolean try_installing)
366 {
367   gchar *args;
368   GError *error = NULL;
369
370   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
371
372   args = g_strdup_printf ("-i %s", folks_individual_get_id (individual));
373
374   if (!empathy_launch_external_app ("gnome-contacts.desktop", args, &error))
375     {
376       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
377         {
378           if (try_installing)
379             {
380               const gchar *packages[] = { "gnome-contacts", NULL };
381
382               DEBUG ("gnome-contacts not installed; try to install it");
383
384               empathy_pkg_kit_install_packages_async (0, packages, NULL,
385                   NULL, install_gnome_contacts_cb, g_object_ref (individual));
386             }
387           else
388             {
389               show_gnome_contacts_error_dialog ();
390             }
391         }
392     }
393
394   g_free (args);
395 }
396
397 /* Use gnome-contacts to display @individual or fallback to
398  * EmpathyIndividualInformationDialog if user is not not in Folks.
399  */
400 void
401 empathy_display_individual_info (FolksIndividual *individual)
402 {
403   EmpathyIndividualManager *mgr;
404
405   mgr = empathy_individual_manager_dup_singleton ();
406
407   /* Only use gnome-contacts if that's a 'real' individual we got from
408    * Folks (and so the individual manager knows about it). If not that's a
409    * MUC contact and we use the simple dialog. */
410   if (empathy_individual_manager_lookup_member (mgr,
411         folks_individual_get_id (individual)) != NULL)
412     {
413       start_gnome_contacts (individual, TRUE);
414     }
415   else
416     {
417       empathy_individual_information_dialog_show (individual, NULL);
418     }
419
420   g_object_unref (mgr);
421 }