]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
Merge branch 'tls-connection'
[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 } 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   GtkWidget *individual_widget;
113   GtkBox *content_area;
114   GList *personas, *l;
115   guint num_personas = 0;
116
117   g_return_if_fail (EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (dialog));
118   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
119
120   priv = GET_PRIV (dialog);
121
122   gtk_window_set_title (GTK_WINDOW (dialog),
123       folks_individual_get_alias (individual));
124
125   content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
126
127   /* Determine how many personas we have, because we only want to display the
128    * label if we have more than one persona. */
129   personas = folks_individual_get_personas (individual);
130   for (l = personas; l != NULL; l = l->next)
131     {
132       if (TPF_IS_PERSONA (l->data))
133         num_personas++;
134     }
135
136   /* Label */
137   if (num_personas > 1)
138     {
139       gchar *label_string;
140       GtkWidget *label;
141
142       /* Translators: the heading at the top of the Information dialogue */
143       label_string = g_strdup_printf ("<b>%s</b>", _("Linked Contacts"));
144       label = gtk_label_new (NULL);
145       gtk_label_set_markup (GTK_LABEL (label), label_string);
146       g_free (label_string);
147
148       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
149       gtk_misc_set_padding (GTK_MISC (label), 6, 6);
150       gtk_box_pack_start (content_area, label, FALSE, TRUE, 0);
151       gtk_widget_show (label);
152     }
153
154   /* Individual widget */
155   individual_widget = empathy_individual_widget_new (individual,
156       EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
157       EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS |
158       EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS);
159   gtk_container_set_border_width (GTK_CONTAINER (individual_widget), 6);
160   gtk_box_pack_start (content_area, individual_widget, TRUE, TRUE, 0);
161   gtk_widget_show (individual_widget);
162
163   priv->individual = g_object_ref (individual);
164 }
165
166 static void
167 individual_information_dialog_get_property (GObject *object,
168     guint param_id,
169     GValue *value,
170     GParamSpec *pspec)
171 {
172   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
173
174   switch (param_id) {
175   case PROP_INDIVIDUAL:
176     g_value_set_object (value, priv->individual);
177     break;
178   default:
179     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
180     break;
181   };
182 }
183
184 static void
185 individual_information_dialog_set_property (GObject *object,
186   guint param_id,
187   const GValue *value,
188   GParamSpec   *pspec)
189 {
190   EmpathyIndividualInformationDialog *dialog =
191     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
192
193   switch (param_id) {
194   case PROP_INDIVIDUAL:
195     individual_information_dialog_set_individual (dialog,
196         FOLKS_INDIVIDUAL (g_value_get_object (value)));
197     break;
198   default:
199     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
200     break;
201   };
202 }
203
204 static void
205 individual_information_dialog_constructed (GObject *object)
206 {
207   GtkDialog *dialog = GTK_DIALOG (object);
208   GtkWidget *button;
209
210   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
211   gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
212
213   /* Close button */
214   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
215   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
216   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
217       GTK_RESPONSE_CLOSE);
218   gtk_widget_set_can_default (button, TRUE);
219   gtk_window_set_default (GTK_WINDOW (dialog), button);
220   gtk_widget_show (button);
221
222   g_signal_connect (dialog, "response",
223       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
224 }
225
226 static void
227 individual_information_dialog_finalize (GObject *object)
228 {
229   EmpathyIndividualInformationDialog *dialog;
230   EmpathyIndividualInformationDialogPriv *priv;
231
232   dialog = EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
233   priv = GET_PRIV (dialog);
234
235   g_object_unref (priv->individual);
236
237   G_OBJECT_CLASS (
238       empathy_individual_information_dialog_parent_class)->finalize (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->finalize = individual_information_dialog_finalize;
248   object_class->get_property = individual_information_dialog_get_property;
249   object_class->set_property = individual_information_dialog_set_property;
250   object_class->constructed = individual_information_dialog_constructed;
251
252   g_object_class_install_property (object_class,
253       PROP_INDIVIDUAL,
254       g_param_spec_object ("individual",
255           "Folks Individual",
256           "Folks Individual to base the dialog upon",
257           FOLKS_TYPE_INDIVIDUAL,
258           G_PARAM_CONSTRUCT |
259           G_PARAM_READWRITE |
260           G_PARAM_STATIC_STRINGS));
261
262   g_type_class_add_private (object_class,
263       sizeof (EmpathyIndividualInformationDialogPriv));
264 }
265
266 static void
267 empathy_individual_information_dialog_init (
268     EmpathyIndividualInformationDialog *dialog)
269 {
270   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
271       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
272       EmpathyIndividualInformationDialogPriv);
273
274   dialog->priv = priv;
275   priv->individual = NULL;
276 }