]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
Show information for all an individual's personas in the information dialogue
[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-contact-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 } EmpathyIndividualInformationDialogPriv;
47
48 enum {
49   PROP_0,
50   PROP_INDIVIDUAL,
51 };
52
53 /* Info dialogs currently open.
54  * Each dialog contains a referenced pointer to its Individual */
55 static GList *information_dialogs = NULL;
56
57 G_DEFINE_TYPE (EmpathyIndividualInformationDialog,
58     empathy_individual_information_dialog, GTK_TYPE_DIALOG);
59
60 static void
61 individual_dialogs_response_cb (GtkDialog *dialog,
62     gint response,
63     GList **dialogs)
64 {
65   *dialogs = g_list_remove (*dialogs, dialog);
66   gtk_widget_destroy (GTK_WIDGET (dialog));
67 }
68
69 static gint
70 individual_dialogs_find (GObject *object,
71     FolksIndividual *individual)
72 {
73   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
74
75   return individual != priv->individual;
76 }
77
78 void
79 empathy_individual_information_dialog_show (FolksIndividual *individual,
80     GtkWindow *parent)
81 {
82   GtkWidget *dialog;
83   GList *l;
84
85   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
86   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
87
88   l = g_list_find_custom (information_dialogs, individual,
89       (GCompareFunc) individual_dialogs_find);
90
91   if (l != NULL)
92     {
93       gtk_window_present (GTK_WINDOW (l->data));
94       return;
95     }
96
97   /* Create dialog */
98   dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
99       "individual", individual,
100       NULL);
101
102   information_dialogs = g_list_prepend (information_dialogs, dialog);
103   gtk_widget_show (dialog);
104 }
105
106 static void
107 individual_information_dialog_set_individual (
108     EmpathyIndividualInformationDialog *dialog,
109     FolksIndividual *individual)
110 {
111   EmpathyIndividualInformationDialogPriv *priv;
112   GList *personas, *l;
113
114   g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
115   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
116
117   priv = GET_PRIV (dialog);
118
119   gtk_window_set_title (GTK_WINDOW (dialog),
120       folks_individual_get_alias (individual));
121
122   personas = folks_individual_get_personas (individual);
123
124   for (l = personas; l != NULL; l = l->next)
125     {
126       GtkWidget *contact_widget;
127       TpContact *tp_contact;
128       EmpathyContact *contact;
129       TpfPersona *persona = l->data;
130
131       if (!TPF_IS_PERSONA (persona))
132         continue;
133
134       tp_contact = tpf_persona_get_contact (persona);
135       contact = empathy_contact_dup_from_tp_contact (tp_contact);
136
137       /* Contact info widget */
138       contact_widget = empathy_contact_widget_new (contact,
139           EMPATHY_CONTACT_WIDGET_SHOW_LOCATION |
140           EMPATHY_CONTACT_WIDGET_SHOW_DETAILS);
141       gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
142       gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
143           GTK_DIALOG (dialog))), contact_widget, TRUE, TRUE, 0);
144       gtk_widget_show (contact_widget);
145
146       g_object_unref (contact);
147     }
148
149   priv->individual = g_object_ref (individual);
150 }
151
152 static void
153 individual_information_dialog_get_property (GObject *object,
154     guint param_id,
155     GValue *value,
156     GParamSpec *pspec)
157 {
158   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
159
160   switch (param_id) {
161   case PROP_INDIVIDUAL:
162     g_value_set_object (value, priv->individual);
163     break;
164   default:
165     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
166     break;
167   };
168 }
169
170 static void
171 individual_information_dialog_set_property (GObject *object,
172   guint param_id,
173   const GValue *value,
174   GParamSpec   *pspec)
175 {
176   EmpathyIndividualInformationDialog *dialog =
177     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
178
179   switch (param_id) {
180   case PROP_INDIVIDUAL:
181     individual_information_dialog_set_individual (dialog,
182         FOLKS_INDIVIDUAL (g_value_get_object (value)));
183     break;
184   default:
185     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
186     break;
187   };
188 }
189
190 static void
191 individual_information_dialog_constructed (GObject *object)
192 {
193   GtkDialog *dialog = GTK_DIALOG (object);
194   GtkWidget *button;
195
196   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
197   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
198
199   /* Close button */
200   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
201   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
202   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
203       GTK_RESPONSE_CLOSE);
204   gtk_widget_set_can_default (button, TRUE);
205   gtk_window_set_default (GTK_WINDOW (dialog), button);
206   gtk_widget_show (button);
207
208   g_signal_connect (dialog, "response",
209       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
210 }
211
212 static void
213 individual_information_dialog_finalize (GObject *object)
214 {
215   EmpathyIndividualInformationDialog *dialog;
216   EmpathyIndividualInformationDialogPriv *priv;
217
218   dialog = EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
219   priv = GET_PRIV (dialog);
220
221   g_object_unref (priv->individual);
222
223   G_OBJECT_CLASS (
224       empathy_individual_information_dialog_parent_class)->finalize (object);
225 }
226
227 static void
228 empathy_individual_information_dialog_class_init (
229     EmpathyIndividualInformationDialogClass *klass)
230 {
231   GObjectClass *object_class = G_OBJECT_CLASS (klass);
232
233   object_class->finalize = individual_information_dialog_finalize;
234   object_class->get_property = individual_information_dialog_get_property;
235   object_class->set_property = individual_information_dialog_set_property;
236   object_class->constructed = individual_information_dialog_constructed;
237
238   g_object_class_install_property (object_class,
239       PROP_INDIVIDUAL,
240       g_param_spec_object ("individual",
241           "Folks Individual",
242           "Folks Individual to base the dialog upon",
243           FOLKS_TYPE_INDIVIDUAL,
244           G_PARAM_CONSTRUCT |
245           G_PARAM_READWRITE |
246           G_PARAM_STATIC_STRINGS));
247
248   g_type_class_add_private (object_class,
249       sizeof (EmpathyIndividualInformationDialogPriv));
250 }
251
252 static void
253 empathy_individual_information_dialog_init (
254     EmpathyIndividualInformationDialog *dialog)
255 {
256   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
257       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
258       EmpathyIndividualInformationDialogPriv);
259
260   dialog->priv = priv;
261   priv->individual = NULL;
262 }