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