]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-edit-dialog.c
Use a flat namespace for internal includes
[empathy.git] / libempathy-gtk / empathy-individual-edit-dialog.c
1 /*
2  * Copyright (C) 2007-2010 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Philip Withnall <philip.withnall@collabora.co.uk>
20  *          Travis Reitter <travis.reitter@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <glib/gi18n-lib.h>
26
27 #include "empathy-utils.h"
28
29 #include "empathy-individual-edit-dialog.h"
30 #include "empathy-individual-widget.h"
31
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualEditDialog)
33
34 typedef struct {
35   FolksIndividual *individual; /* owned */
36   GtkWidget *individual_widget; /* child widget */
37 } EmpathyIndividualEditDialogPriv;
38
39 enum {
40   PROP_INDIVIDUAL = 1,
41 };
42
43 /* Edit dialogs currently open.
44  * Each dialog contains a referenced pointer to its Individual */
45 static GList *edit_dialogs = NULL;
46
47 static void individual_edit_dialog_set_individual (
48     EmpathyIndividualEditDialog *dialog,
49     FolksIndividual *individual);
50
51 G_DEFINE_TYPE (EmpathyIndividualEditDialog, empathy_individual_edit_dialog,
52     GTK_TYPE_DIALOG);
53
54 static void
55 individual_dialogs_response_cb (GtkDialog *dialog,
56     gint response,
57     GList **dialogs)
58 {
59   *dialogs = g_list_remove (*dialogs, dialog);
60   gtk_widget_destroy (GTK_WIDGET (dialog));
61 }
62
63 static gint
64 individual_dialogs_find (GObject *object,
65     FolksIndividual *individual)
66 {
67   EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
68
69   return individual != priv->individual;
70 }
71
72 void
73 empathy_individual_edit_dialog_show (FolksIndividual *individual,
74     GtkWindow *parent)
75 {
76   GtkWidget *dialog;
77   GList *l;
78
79   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
80   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
81
82   l = g_list_find_custom (edit_dialogs, individual,
83       (GCompareFunc) individual_dialogs_find);
84
85   if (l != NULL)
86     {
87       gtk_window_present (GTK_WINDOW (l->data));
88       return;
89     }
90
91   /* Create dialog */
92   dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
93       "individual", individual,
94       NULL);
95
96   edit_dialogs = g_list_prepend (edit_dialogs, dialog);
97   gtk_widget_show (dialog);
98 }
99
100 static void
101 individual_removed_cb (FolksIndividual *individual,
102     FolksIndividual *replacement_individual,
103     EmpathyIndividualEditDialog *self)
104 {
105   /* Update to show the new Individual (this will close the dialogue if there
106    * is no new Individual). */
107   individual_edit_dialog_set_individual (self, replacement_individual);
108
109   /* Destroy the dialogue */
110   if (replacement_individual == NULL)
111     {
112       individual_dialogs_response_cb (GTK_DIALOG (self),
113           GTK_RESPONSE_DELETE_EVENT, &edit_dialogs);
114     }
115 }
116
117 static void
118 individual_edit_dialog_set_individual (
119     EmpathyIndividualEditDialog *dialog,
120     FolksIndividual *individual)
121 {
122   EmpathyIndividualEditDialogPriv *priv;
123
124   g_return_if_fail (EMPATHY_INDIVIDUAL_EDIT_DIALOG (dialog));
125   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
126
127   priv = GET_PRIV (dialog);
128
129   /* Remove the old Individual */
130   if (priv->individual != NULL)
131     {
132       g_signal_handlers_disconnect_by_func (priv->individual,
133           (GCallback) individual_removed_cb, dialog);
134     }
135
136   tp_clear_object (&priv->individual);
137
138   /* Add the new Individual */
139   priv->individual = individual;
140
141   if (individual != NULL)
142     {
143       g_object_ref (individual);
144       g_signal_connect (individual, "removed",
145           (GCallback) individual_removed_cb, dialog);
146
147       /* Update the UI */
148       empathy_individual_widget_set_individual (
149           EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
150     }
151 }
152
153 static void
154 individual_edit_dialog_get_property (GObject *object,
155     guint param_id,
156     GValue *value,
157     GParamSpec *pspec)
158 {
159   EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
160
161   switch (param_id) {
162   case PROP_INDIVIDUAL:
163     g_value_set_object (value, priv->individual);
164     break;
165   default:
166     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
167     break;
168   };
169 }
170
171 static void
172 individual_edit_dialog_set_property (GObject *object,
173   guint param_id,
174   const GValue *value,
175   GParamSpec   *pspec)
176 {
177   EmpathyIndividualEditDialog *dialog =
178     EMPATHY_INDIVIDUAL_EDIT_DIALOG (object);
179
180   switch (param_id) {
181   case PROP_INDIVIDUAL:
182     individual_edit_dialog_set_individual (dialog,
183         FOLKS_INDIVIDUAL (g_value_get_object (value)));
184     break;
185   default:
186     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187     break;
188   };
189 }
190
191 static void
192 individual_edit_dialog_dispose (GObject *object)
193 {
194   individual_edit_dialog_set_individual (
195       EMPATHY_INDIVIDUAL_EDIT_DIALOG (object), NULL);
196
197   G_OBJECT_CLASS (
198       empathy_individual_edit_dialog_parent_class)->dispose (object);
199 }
200
201 static void
202 empathy_individual_edit_dialog_class_init (
203     EmpathyIndividualEditDialogClass *klass)
204 {
205   GObjectClass *object_class = G_OBJECT_CLASS (klass);
206
207   object_class->get_property = individual_edit_dialog_get_property;
208   object_class->set_property = individual_edit_dialog_set_property;
209   object_class->dispose = individual_edit_dialog_dispose;
210
211   g_object_class_install_property (object_class,
212       PROP_INDIVIDUAL,
213       g_param_spec_object ("individual",
214           "Folks Individual",
215           "Folks Individual to edit using the dialog.",
216           FOLKS_TYPE_INDIVIDUAL,
217           G_PARAM_CONSTRUCT_ONLY |
218           G_PARAM_READWRITE |
219           G_PARAM_STATIC_STRINGS));
220
221   g_type_class_add_private (object_class,
222       sizeof (EmpathyIndividualEditDialogPriv));
223 }
224
225 static void
226 empathy_individual_edit_dialog_init (
227     EmpathyIndividualEditDialog *dialog)
228 {
229   GtkWidget *button;
230   EmpathyIndividualEditDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
231       dialog, EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
232       EmpathyIndividualEditDialogPriv);
233
234   dialog->priv = priv;
235   priv->individual = NULL;
236
237   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
238   gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
239
240   /* Individual widget */
241   priv->individual_widget = empathy_individual_widget_new (priv->individual,
242       EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
243       EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
244       EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE);
245   gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 8);
246   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
247       GTK_DIALOG (dialog))), priv->individual_widget, TRUE, TRUE, 0);
248   gtk_widget_show (priv->individual_widget);
249
250   /* Close button */
251   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
252   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
253   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
254       GTK_RESPONSE_CLOSE);
255   gtk_widget_set_can_default (button, TRUE);
256   gtk_window_set_default (GTK_WINDOW (dialog), button);
257   gtk_widget_show (button);
258
259   g_signal_connect (dialog, "response",
260       G_CALLBACK (individual_dialogs_response_cb), &edit_dialogs);
261 }