2 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Philip Withnall <philip.withnall@collabora.co.uk>
20 * Travis Reitter <travis.reitter@collabora.co.uk>
29 #include <glib/gi18n-lib.h>
31 #include <telepathy-glib/util.h>
32 #include <folks/folks.h>
33 #include <folks/folks-telepathy.h>
35 #include <libempathy/empathy-individual-manager.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-individual-edit-dialog.h"
39 #include "empathy-individual-widget.h"
40 #include "empathy-ui-utils.h"
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualEditDialog)
45 FolksIndividual *individual; /* owned */
46 GtkWidget *individual_widget; /* child widget */
47 } EmpathyIndividualEditDialogPriv;
53 /* Edit dialogs currently open.
54 * Each dialog contains a referenced pointer to its Individual */
55 static GList *edit_dialogs = NULL;
57 static void individual_edit_dialog_set_individual (
58 EmpathyIndividualEditDialog *dialog,
59 FolksIndividual *individual);
61 G_DEFINE_TYPE (EmpathyIndividualEditDialog, empathy_individual_edit_dialog,
64 /* Fairly arbitrary response ID for the "Unlink" button */
65 #define RESPONSE_UNLINK 5
68 individual_dialogs_response_cb (GtkDialog *dialog,
72 if (response == RESPONSE_UNLINK)
74 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (dialog);
75 EmpathyIndividualManager *manager =
76 empathy_individual_manager_dup_singleton ();
78 empathy_individual_manager_unlink_individual (manager, priv->individual);
80 g_object_unref (manager);
83 *dialogs = g_list_remove (*dialogs, dialog);
84 gtk_widget_destroy (GTK_WIDGET (dialog));
88 individual_dialogs_find (GObject *object,
89 FolksIndividual *individual)
91 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
93 return individual != priv->individual;
97 empathy_individual_edit_dialog_show (FolksIndividual *individual,
103 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
104 g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
106 l = g_list_find_custom (edit_dialogs, individual,
107 (GCompareFunc) individual_dialogs_find);
111 gtk_window_present (GTK_WINDOW (l->data));
116 dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
117 "individual", individual,
120 edit_dialogs = g_list_prepend (edit_dialogs, dialog);
121 gtk_widget_show (dialog);
125 set_unlink_button_sensitivity (EmpathyIndividualEditDialog *self)
127 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (self);
129 guint num_personas = 0;
131 /* Count how many Telepathy personas we have, to see whether we can
133 if (priv->individual != NULL)
135 personas = folks_individual_get_personas (priv->individual);
136 for (l = personas; l != NULL; l = l->next)
138 if (TPF_IS_PERSONA (l->data))
143 /* Only make the "Unlink" button sensitive if we have enough personas */
144 gtk_dialog_set_response_sensitive (GTK_DIALOG (self), RESPONSE_UNLINK,
145 (num_personas > 1) ? TRUE : FALSE);
149 individual_removed_cb (FolksIndividual *individual,
150 FolksIndividual *replacement_individual,
151 EmpathyIndividualEditDialog *self)
153 /* Update to show the new Individual (this will close the dialogue if there
154 * is no new Individual). */
155 individual_edit_dialog_set_individual (self, replacement_individual);
157 /* Destroy the dialogue */
158 if (replacement_individual == NULL)
160 individual_dialogs_response_cb (GTK_DIALOG (self),
161 GTK_RESPONSE_DELETE_EVENT, &edit_dialogs);
166 individual_edit_dialog_set_individual (
167 EmpathyIndividualEditDialog *dialog,
168 FolksIndividual *individual)
170 EmpathyIndividualEditDialogPriv *priv;
172 g_return_if_fail (EMPATHY_INDIVIDUAL_EDIT_DIALOG (dialog));
173 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
175 priv = GET_PRIV (dialog);
177 /* Remove the old Individual */
178 if (priv->individual != NULL)
180 g_signal_handlers_disconnect_by_func (priv->individual,
181 (GCallback) individual_removed_cb, dialog);
184 tp_clear_object (&priv->individual);
186 /* Add the new Individual */
187 priv->individual = individual;
189 if (individual != NULL)
191 g_object_ref (individual);
192 g_signal_connect (individual, "removed",
193 (GCallback) individual_removed_cb, dialog);
196 empathy_individual_widget_set_individual (
197 EMPATHY_INDIVIDUAL_WIDGET (priv->individual_widget), individual);
198 set_unlink_button_sensitivity (dialog);
203 individual_edit_dialog_get_property (GObject *object,
208 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
211 case PROP_INDIVIDUAL:
212 g_value_set_object (value, priv->individual);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
221 individual_edit_dialog_set_property (GObject *object,
226 EmpathyIndividualEditDialog *dialog =
227 EMPATHY_INDIVIDUAL_EDIT_DIALOG (object);
230 case PROP_INDIVIDUAL:
231 individual_edit_dialog_set_individual (dialog,
232 FOLKS_INDIVIDUAL (g_value_get_object (value)));
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
241 individual_edit_dialog_dispose (GObject *object)
243 individual_edit_dialog_set_individual (
244 EMPATHY_INDIVIDUAL_EDIT_DIALOG (object), NULL);
247 empathy_individual_edit_dialog_parent_class)->dispose (object);
251 empathy_individual_edit_dialog_class_init (
252 EmpathyIndividualEditDialogClass *klass)
254 GObjectClass *object_class = G_OBJECT_CLASS (klass);
256 object_class->get_property = individual_edit_dialog_get_property;
257 object_class->set_property = individual_edit_dialog_set_property;
258 object_class->dispose = individual_edit_dialog_dispose;
260 g_object_class_install_property (object_class,
262 g_param_spec_object ("individual",
264 "Folks Individual to edit using the dialog.",
265 FOLKS_TYPE_INDIVIDUAL,
266 G_PARAM_CONSTRUCT_ONLY |
268 G_PARAM_STATIC_STRINGS));
270 g_type_class_add_private (object_class,
271 sizeof (EmpathyIndividualEditDialogPriv));
275 empathy_individual_edit_dialog_init (
276 EmpathyIndividualEditDialog *dialog)
278 GtkWidget *button, *action_area;
279 EmpathyIndividualEditDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
280 dialog, EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
281 EmpathyIndividualEditDialogPriv);
284 priv->individual = NULL;
286 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
287 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
288 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
290 /* Individual widget */
291 priv->individual_widget = empathy_individual_widget_new (priv->individual,
292 EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
293 EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
294 EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE);
295 gtk_container_set_border_width (GTK_CONTAINER (priv->individual_widget), 8);
296 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
297 GTK_DIALOG (dialog))), priv->individual_widget, TRUE, TRUE, 0);
298 gtk_widget_show (priv->individual_widget);
301 button = gtk_button_new_with_mnemonic (
302 C_("Unlink individual (button)", "_Unlink"));
303 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, RESPONSE_UNLINK);
305 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
306 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), button,
309 gtk_widget_show (button);
312 button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
313 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
314 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
316 gtk_widget_set_can_default (button, TRUE);
317 gtk_window_set_default (GTK_WINDOW (dialog), button);
318 gtk_widget_show (button);
320 g_signal_connect (dialog, "response",
321 G_CALLBACK (individual_dialogs_response_cb), &edit_dialogs);