]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-edit-dialog.c
Be more compatible with Facebook emoticon codes
[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 #include "empathy-individual-edit-dialog.h"
25
26 #include <glib/gi18n-lib.h>
27
28 #include "empathy-individual-widget.h"
29 #include "empathy-utils.h"
30
31 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualEditDialog)
32
33 typedef struct {
34   FolksIndividual *individual; /* owned */
35   GtkWidget *individual_widget; /* child widget */
36 } EmpathyIndividualEditDialogPriv;
37
38 enum {
39   PROP_INDIVIDUAL = 1,
40 };
41
42 /* Edit dialogs currently open.
43  * Each dialog contains a referenced pointer to its Individual */
44 static GList *edit_dialogs = NULL;
45
46 static void individual_edit_dialog_set_individual (
47     EmpathyIndividualEditDialog *dialog,
48     FolksIndividual *individual);
49
50 G_DEFINE_TYPE (EmpathyIndividualEditDialog, empathy_individual_edit_dialog,
51     GTK_TYPE_DIALOG);
52
53 static void
54 individual_dialogs_response_cb (GtkDialog *dialog,
55     gint response,
56     GList **dialogs)
57 {
58   *dialogs = g_list_remove (*dialogs, dialog);
59   gtk_widget_destroy (GTK_WIDGET (dialog));
60 }
61
62 static gint
63 individual_dialogs_find (GObject *object,
64     FolksIndividual *individual)
65 {
66   EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
67
68   return individual != priv->individual;
69 }
70
71 void
72 empathy_individual_edit_dialog_show (FolksIndividual *individual,
73     GtkWindow *parent)
74 {
75   GtkWidget *dialog;
76   GList *l;
77
78   g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
79   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
80
81   l = g_list_find_custom (edit_dialogs, individual,
82       (GCompareFunc) individual_dialogs_find);
83
84   if (l != NULL)
85     {
86       gtk_window_present (GTK_WINDOW (l->data));
87       return;
88     }
89
90   /* Create dialog */
91   dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
92       "individual", individual,
93       NULL);
94
95   edit_dialogs = g_list_prepend (edit_dialogs, dialog);
96   gtk_widget_show (dialog);
97 }
98
99 static void
100 individual_removed_cb (FolksIndividual *individual,
101     FolksIndividual *replacement_individual,
102     EmpathyIndividualEditDialog *self)
103 {
104   /* Update to show the new Individual (this will close the dialogue if there
105    * is no new Individual). */
106   individual_edit_dialog_set_individual (self, replacement_individual);
107
108   /* Destroy the dialogue */
109   if (replacement_individual == NULL)
110     {
111       individual_dialogs_response_cb (GTK_DIALOG (self),
112           GTK_RESPONSE_DELETE_EVENT, &edit_dialogs);
113     }
114 }
115
116 static void
117 individual_edit_dialog_set_individual (
118     EmpathyIndividualEditDialog *dialog,
119     FolksIndividual *individual)
120 {
121   EmpathyIndividualEditDialogPriv *priv;
122
123   g_return_if_fail (EMPATHY_INDIVIDUAL_EDIT_DIALOG (dialog));
124   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
125
126   priv = GET_PRIV (dialog);
127
128   /* Remove the old Individual */
129   if (priv->individual != NULL)
130     {
131       g_signal_handlers_disconnect_by_func (priv->individual,
132           (GCallback) individual_removed_cb, dialog);
133     }
134
135   tp_clear_object (&priv->individual);
136
137   /* Add the new Individual */
138   priv->individual = individual;
139
140   if (individual != NULL)
141     {
142       g_object_ref (individual);
143       g_signal_connect (individual, "removed",
144           (GCallback) individual_removed_cb, dialog);
145
146       /* Update the UI */
147       empathy_individual_widget_set_individual (
148           EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
149     }
150 }
151
152 static void
153 individual_edit_dialog_get_property (GObject *object,
154     guint param_id,
155     GValue *value,
156     GParamSpec *pspec)
157 {
158   EmpathyIndividualEditDialogPriv *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_edit_dialog_set_property (GObject *object,
172   guint param_id,
173   const GValue *value,
174   GParamSpec   *pspec)
175 {
176   EmpathyIndividualEditDialog *dialog =
177     EMPATHY_INDIVIDUAL_EDIT_DIALOG (object);
178
179   switch (param_id) {
180   case PROP_INDIVIDUAL:
181     individual_edit_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_edit_dialog_dispose (GObject *object)
192 {
193   individual_edit_dialog_set_individual (
194       EMPATHY_INDIVIDUAL_EDIT_DIALOG (object), NULL);
195
196   G_OBJECT_CLASS (
197       empathy_individual_edit_dialog_parent_class)->dispose (object);
198 }
199
200 static void
201 empathy_individual_edit_dialog_class_init (
202     EmpathyIndividualEditDialogClass *klass)
203 {
204   GObjectClass *object_class = G_OBJECT_CLASS (klass);
205
206   object_class->get_property = individual_edit_dialog_get_property;
207   object_class->set_property = individual_edit_dialog_set_property;
208   object_class->dispose = individual_edit_dialog_dispose;
209
210   g_object_class_install_property (object_class,
211       PROP_INDIVIDUAL,
212       g_param_spec_object ("individual",
213           "Folks Individual",
214           "Folks Individual to edit using the dialog.",
215           FOLKS_TYPE_INDIVIDUAL,
216           G_PARAM_CONSTRUCT_ONLY |
217           G_PARAM_READWRITE |
218           G_PARAM_STATIC_STRINGS));
219
220   g_type_class_add_private (object_class,
221       sizeof (EmpathyIndividualEditDialogPriv));
222 }
223
224 static void
225 empathy_individual_edit_dialog_init (
226     EmpathyIndividualEditDialog *dialog)
227 {
228   GtkWidget *button;
229   EmpathyIndividualEditDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
230       dialog, EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
231       EmpathyIndividualEditDialogPriv);
232
233   dialog->priv = priv;
234   priv->individual = NULL;
235
236   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
237   gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
238
239   /* Individual widget */
240   priv->individual_widget = empathy_individual_widget_new (priv->individual,
241       EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
242       EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
243       EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE);
244   gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 8);
245   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
246       GTK_DIALOG (dialog))), priv->individual_widget, TRUE, TRUE, 0);
247   gtk_widget_show (priv->individual_widget);
248
249   /* Close button */
250   button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
251   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
252   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
253       GTK_RESPONSE_CLOSE);
254   gtk_widget_set_can_default (button, TRUE);
255   gtk_window_set_default (GTK_WINDOW (dialog), button);
256   gtk_widget_show (button);
257
258   g_signal_connect (dialog, "response",
259       G_CALLBACK (individual_dialogs_response_cb), &edit_dialogs);
260 }