]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-information-dialog.c
Use EmpathyIndividualWidget in EmpathyIndividualInformationDialog
[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
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   individual_widget = empathy_individual_widget_new (individual,
123       EMPATHY_INDIVIDUAL_WIDGET_SHOW_LOCATION |
124       EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS |
125       EMPATHY_INDIVIDUAL_WIDGET_SHOW_PERSONAS);
126   gtk_container_set_border_width (GTK_CONTAINER (individual_widget), 8);
127   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
128       GTK_DIALOG (dialog))), individual_widget, TRUE, TRUE, 0);
129   gtk_widget_show (individual_widget);
130
131   priv->individual = g_object_ref (individual);
132 }
133
134 static void
135 individual_information_dialog_get_property (GObject *object,
136     guint param_id,
137     GValue *value,
138     GParamSpec *pspec)
139 {
140   EmpathyIndividualInformationDialogPriv *priv = GET_PRIV (object);
141
142   switch (param_id) {
143   case PROP_INDIVIDUAL:
144     g_value_set_object (value, priv->individual);
145     break;
146   default:
147     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
148     break;
149   };
150 }
151
152 static void
153 individual_information_dialog_set_property (GObject *object,
154   guint param_id,
155   const GValue *value,
156   GParamSpec   *pspec)
157 {
158   EmpathyIndividualInformationDialog *dialog =
159     EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
160
161   switch (param_id) {
162   case PROP_INDIVIDUAL:
163     individual_information_dialog_set_individual (dialog,
164         FOLKS_INDIVIDUAL (g_value_get_object (value)));
165     break;
166   default:
167     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
168     break;
169   };
170 }
171
172 static void
173 individual_information_dialog_constructed (GObject *object)
174 {
175   GtkDialog *dialog = GTK_DIALOG (object);
176   GtkWidget *button;
177
178   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
179   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
180
181   /* Close button */
182   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
183   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
184   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
185       GTK_RESPONSE_CLOSE);
186   gtk_widget_set_can_default (button, TRUE);
187   gtk_window_set_default (GTK_WINDOW (dialog), button);
188   gtk_widget_show (button);
189
190   g_signal_connect (dialog, "response",
191       G_CALLBACK (individual_dialogs_response_cb), &information_dialogs);
192 }
193
194 static void
195 individual_information_dialog_finalize (GObject *object)
196 {
197   EmpathyIndividualInformationDialog *dialog;
198   EmpathyIndividualInformationDialogPriv *priv;
199
200   dialog = EMPATHY_INDIVIDUAL_INFORMATION_DIALOG (object);
201   priv = GET_PRIV (dialog);
202
203   g_object_unref (priv->individual);
204
205   G_OBJECT_CLASS (
206       empathy_individual_information_dialog_parent_class)->finalize (object);
207 }
208
209 static void
210 empathy_individual_information_dialog_class_init (
211     EmpathyIndividualInformationDialogClass *klass)
212 {
213   GObjectClass *object_class = G_OBJECT_CLASS (klass);
214
215   object_class->finalize = individual_information_dialog_finalize;
216   object_class->get_property = individual_information_dialog_get_property;
217   object_class->set_property = individual_information_dialog_set_property;
218   object_class->constructed = individual_information_dialog_constructed;
219
220   g_object_class_install_property (object_class,
221       PROP_INDIVIDUAL,
222       g_param_spec_object ("individual",
223           "Folks Individual",
224           "Folks Individual to base the dialog upon",
225           FOLKS_TYPE_INDIVIDUAL,
226           G_PARAM_CONSTRUCT |
227           G_PARAM_READWRITE |
228           G_PARAM_STATIC_STRINGS));
229
230   g_type_class_add_private (object_class,
231       sizeof (EmpathyIndividualInformationDialogPriv));
232 }
233
234 static void
235 empathy_individual_information_dialog_init (
236     EmpathyIndividualInformationDialog *dialog)
237 {
238   EmpathyIndividualInformationDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
239       dialog, EMPATHY_TYPE_INDIVIDUAL_INFORMATION_DIALOG,
240       EmpathyIndividualInformationDialogPriv);
241
242   dialog->priv = priv;
243   priv->individual = NULL;
244 }