1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
20 * Philip Withnall <philip.withnall@collabora.co.uk>
21 * Travis Reitter <travis.reitter@collabora.co.uk>
26 #include <glib/gi18n-lib.h>
28 #include <libempathy/empathy-individual-manager.h>
29 #include <libempathy/empathy-utils.h>
30 #include <libempathy/empathy-pkg-kit.h>
32 #include "empathy-individual-information-dialog.h"
33 #include "empathy-individual-widget.h"
34 #include "empathy-ui-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualInformationDialog)
41 FolksIndividual *individual;
42 GtkWidget *individual_widget; /* child widget */
43 GtkWidget *label; /* child widget */
44 } EmpathyIndividualInformationDialogPriv;
51 /* Info dialogs currently open.
52 * Each dialog contains a referenced pointer to its Individual */
53 static GList *information_dialogs = NULL;
55 static void individual_information_dialog_set_individual (
56 EmpathyIndividualInformationDialog *dialog,
57 FolksIndividual *individual);
59 G_DEFINE_TYPE (EmpathyIndividualInformationDialog,
60 empathy_individual_information_dialog, GTK_TYPE_DIALOG);
63 individual_dialogs_response_cb (GtkDialog *dialog,
67 *dialogs = g_list_remove (*dialogs, dialog);
68 gtk_widget_destroy (GTK_WIDGET (dialog));
72 individual_dialogs_find (GObject *object,
73 FolksIndividual *individual)
75 EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
77 return individual != priv->individual;
81 empathy_individual_information_dialog_show (FolksIndividual *individual,
87 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
88 g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
90 l = g_list_find_custom (information_dialogs, individual,
91 (GCompareFunc) individual_dialogs_find);
95 gtk_window_present (GTK_WINDOW (l->data));
100 dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
101 "individual", individual,
104 information_dialogs = g_list_prepend (information_dialogs, dialog);
105 gtk_widget_show (dialog);
109 individual_removed_cb (FolksIndividual *individual,
110 FolksIndividual *replacement_individual,
111 EmpathyIndividualInformationDialog *self)
113 individual_information_dialog_set_individual (self,
114 replacement_individual);
116 /* Destroy the dialogue */
117 if (replacement_individual == NULL)
119 individual_dialogs_response_cb (GTK_DIALOG (self),
120 GTK_RESPONSE_DELETE_EVENT, &information_dialogs);
125 set_label_visibility (EmpathyIndividualInformationDialog *dialog)
127 EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (dialog);
128 guint num_personas = 0;
130 /* Count how many Telepathy personas we have, to see whether we can
132 if (priv->individual != NULL)
137 personas = folks_individual_get_personas (priv->individual);
138 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
139 while (gee_iterator_next (iter))
141 FolksPersona *persona = gee_iterator_get (iter);
142 if (empathy_folks_persona_is_interesting (persona))
145 g_clear_object (&persona);
147 g_clear_object (&iter);
150 /* Only make the label visible if we have enough personas */
151 gtk_widget_set_visible (priv->label, (num_personas > 1) ? TRUE : FALSE);
155 individual_information_dialog_set_individual (
156 EmpathyIndividualInformationDialog *dialog,
157 FolksIndividual *individual)
159 EmpathyIndividualInformationDialogPriv *priv;
161 g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
162 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
164 priv = GET_PRIV (dialog);
166 /* Remove the old Individual */
167 if (priv->individual != NULL)
169 g_signal_handlers_disconnect_by_func (priv->individual,
170 (GCallback) individual_removed_cb, dialog);
173 tp_clear_object (&priv->individual);
175 /* Add the new Individual */
176 priv->individual = individual;
178 if (individual != NULL)
180 g_object_ref (individual);
181 g_signal_connect (individual, "removed",
182 (GCallback) individual_removed_cb, dialog);
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);
194 individual_information_dialog_get_property (GObject *object,
199 EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
202 case PROP_INDIVIDUAL:
203 g_value_set_object (value, priv->individual);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
212 individual_information_dialog_set_property (GObject *object,
217 EmpathyIndividualInformationDialog *dialog =
218 EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
221 case PROP_INDIVIDUAL:
222 individual_information_dialog_set_individual (dialog,
223 FOLKS_INDIVIDUAL (g_value_get_object (value)));
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
232 individual_information_dialog_dispose (GObject *object)
234 individual_information_dialog_set_individual (
235 EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object), NULL);
238 empathy_individual_information_dialog_parent_class)->dispose (object);
242 empathy_individual_information_dialog_class_init (
243 EmpathyIndividualInformationDialogClass *klass)
245 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
251 g_object_class_install_property (object_class,
253 g_param_spec_object ("individual",
255 "Folks Individual to base the dialog upon",
256 FOLKS_TYPE_INDIVIDUAL,
259 G_PARAM_STATIC_STRINGS));
261 g_type_class_add_private (object_class,
262 sizeof (EmpathyIndividualInformationDialogPriv));
266 empathy_individual_information_dialog_init (
267 EmpathyIndividualInformationDialog *dialog)
270 GtkBox *content_area;
272 EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
273 dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
274 EmpathyIndividualInformationDialogPriv);
277 priv->individual = NULL;
279 gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
281 content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
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);
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);
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);
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,
308 gtk_widget_set_can_default (button, TRUE);
309 gtk_window_set_default (GTK_WINDOW (dialog), button);
310 gtk_widget_show (button);
312 g_signal_connect (dialog, "response",
313 G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
317 show_gnome_contacts_error_dialog (void)
321 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
322 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
323 _("gnome-contacts not installed"));
325 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
326 _("Please install gnome-contacts to access contacts details."));
328 g_signal_connect_swapped (dialog, "response",
329 G_CALLBACK (gtk_widget_destroy), dialog);
331 gtk_widget_show (dialog);
335 start_gnome_contacts (FolksIndividual *individual,
336 gboolean try_installing);
339 install_gnome_contacts_cb (GObject *source,
340 GAsyncResult *result,
343 FolksIndividual *individual = user_data;
344 GError *error = NULL;
346 if (!empathy_pkg_kit_install_packages_finish (result, &error))
348 DEBUG ("Failed to install gnome-contacts: %s", error->message);
349 g_error_free (error);
351 show_gnome_contacts_error_dialog ();
355 DEBUG ("gnome-contacts installed");
357 start_gnome_contacts (individual, FALSE);
360 g_object_unref (individual);
364 start_gnome_contacts (FolksIndividual *individual,
365 gboolean try_installing)
368 GError *error = NULL;
370 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
372 args = g_strdup_printf ("-i %s", folks_individual_get_id (individual));
374 if (!empathy_launch_external_app ("gnome-contacts.desktop", args, &error))
376 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
380 const gchar *packages[] = { "gnome-contacts", NULL };
382 DEBUG ("gnome-contacts not installed; try to install it");
384 empathy_pkg_kit_install_packages_async (0, packages, NULL,
385 NULL, install_gnome_contacts_cb, g_object_ref (individual));
389 show_gnome_contacts_error_dialog ();
397 /* Use gnome-contacts to display @individual or fallback to
398 * EmpathyIndividualInformationDialog if user is not not in Folks.
401 empathy_display_individual_info (FolksIndividual *individual)
403 EmpathyIndividualManager *mgr;
405 mgr = empathy_individual_manager_dup_singleton ();
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)
413 start_gnome_contacts (individual, TRUE);
417 empathy_individual_information_dialog_show (individual, NULL);
420 g_object_unref (mgr);