Merge branch 'trivia'
[empathy-mirror.git] / libempathy-gtk / empathy-individual-edit-dialog.c
blob4b5e2dfd7bee42b4c26a71a8f8e79624d2599f21
1 /*
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>
23 #include <config.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #include <gtk/gtk.h>
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)
44 typedef struct {
45 FolksIndividual *individual; /* owned */
46 } EmpathyIndividualEditDialogPriv;
48 enum {
49 PROP_INDIVIDUAL = 1,
52 /* Edit dialogs currently open.
53 * Each dialog contains a referenced pointer to its Individual */
54 static GList *edit_dialogs = NULL;
56 G_DEFINE_TYPE (EmpathyIndividualEditDialog, empathy_individual_edit_dialog,
57 GTK_TYPE_DIALOG);
59 /* Fairly arbitrary response ID for the "Unlink" button */
60 #define RESPONSE_UNLINK 5
62 static void
63 individual_dialogs_response_cb (GtkDialog *dialog,
64 gint response,
65 GList **dialogs)
67 if (response == RESPONSE_UNLINK)
69 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (dialog);
70 EmpathyIndividualManager *manager =
71 empathy_individual_manager_dup_singleton ();
73 empathy_individual_manager_unlink_individual (manager, priv->individual);
75 g_object_unref (manager);
78 *dialogs = g_list_remove (*dialogs, dialog);
79 gtk_widget_destroy (GTK_WIDGET (dialog));
82 static gint
83 individual_dialogs_find (GObject *object,
84 FolksIndividual *individual)
86 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
88 return individual != priv->individual;
91 void
92 empathy_individual_edit_dialog_show (FolksIndividual *individual,
93 GtkWindow *parent)
95 GtkWidget *dialog;
96 GList *l;
98 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
99 g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
101 l = g_list_find_custom (edit_dialogs, individual,
102 (GCompareFunc) individual_dialogs_find);
104 if (l != NULL)
106 gtk_window_present (GTK_WINDOW (l->data));
107 return;
110 /* Create dialog */
111 dialog = g_object_new (EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
112 "individual", individual,
113 NULL);
115 edit_dialogs = g_list_prepend (edit_dialogs, dialog);
116 gtk_widget_show (dialog);
119 static void
120 individual_edit_dialog_set_individual (
121 EmpathyIndividualEditDialog *dialog,
122 FolksIndividual *individual)
124 EmpathyIndividualEditDialogPriv *priv;
125 GtkWidget *individual_widget;
126 GList *personas, *l;
127 guint num_personas = 0;
129 g_return_if_fail (EMPATHY_INDIVIDUAL_EDIT_DIALOG (dialog));
130 g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
132 priv = GET_PRIV (dialog);
134 /* Individual info widget */
135 individual_widget = empathy_individual_widget_new (individual,
136 EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
137 EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
138 EMPATHY_INDIVIDUAL_WIDGET_EDIT_FAVOURITE);
139 gtk_container_set_border_width (GTK_CONTAINER (individual_widget), 8);
140 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
141 GTK_DIALOG (dialog))), individual_widget, TRUE, TRUE, 0);
142 gtk_widget_show (individual_widget);
144 /* Count how many Telepathy personas we have, to see whether we can
145 * unlink */
146 personas = folks_individual_get_personas (individual);
147 for (l = personas; l != NULL; l = l->next)
149 if (TPF_IS_PERSONA (l->data))
150 num_personas++;
153 priv->individual = g_object_ref (individual);
155 /* Only make the "Unlink" button sensitive if we have enough personas */
156 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), RESPONSE_UNLINK,
157 (num_personas > 1) ? TRUE : FALSE);
160 static void
161 individual_edit_dialog_get_property (GObject *object,
162 guint param_id,
163 GValue *value,
164 GParamSpec *pspec)
166 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
168 switch (param_id) {
169 case PROP_INDIVIDUAL:
170 g_value_set_object (value, priv->individual);
171 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
174 break;
178 static void
179 individual_edit_dialog_set_property (GObject *object,
180 guint param_id,
181 const GValue *value,
182 GParamSpec *pspec)
184 EmpathyIndividualEditDialog *dialog =
185 EMPATHY_INDIVIDUAL_EDIT_DIALOG (object);
187 switch (param_id) {
188 case PROP_INDIVIDUAL:
189 individual_edit_dialog_set_individual (dialog,
190 FOLKS_INDIVIDUAL (g_value_get_object (value)));
191 break;
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
194 break;
198 static void
199 individual_edit_dialog_dispose (GObject *object)
201 EmpathyIndividualEditDialogPriv *priv = GET_PRIV (object);
203 if (priv->individual != NULL)
204 g_object_unref (priv->individual);
205 priv->individual = NULL;
207 G_OBJECT_CLASS (
208 empathy_individual_edit_dialog_parent_class)->dispose (object);
211 static void
212 empathy_individual_edit_dialog_class_init (
213 EmpathyIndividualEditDialogClass *klass)
215 GObjectClass *object_class = G_OBJECT_CLASS (klass);
217 object_class->get_property = individual_edit_dialog_get_property;
218 object_class->set_property = individual_edit_dialog_set_property;
219 object_class->dispose = individual_edit_dialog_dispose;
221 g_object_class_install_property (object_class,
222 PROP_INDIVIDUAL,
223 g_param_spec_object ("individual",
224 "Folks Individual",
225 "Folks Individual to edit using the dialog.",
226 FOLKS_TYPE_INDIVIDUAL,
227 G_PARAM_CONSTRUCT_ONLY |
228 G_PARAM_READWRITE |
229 G_PARAM_STATIC_STRINGS));
231 g_type_class_add_private (object_class,
232 sizeof (EmpathyIndividualEditDialogPriv));
235 static void
236 empathy_individual_edit_dialog_init (
237 EmpathyIndividualEditDialog *dialog)
239 GtkWidget *button, *action_area;
240 EmpathyIndividualEditDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (
241 dialog, EMPATHY_TYPE_INDIVIDUAL_EDIT_DIALOG,
242 EmpathyIndividualEditDialogPriv);
244 dialog->priv = priv;
245 priv->individual = NULL;
247 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
248 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
249 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
251 /* Unlink button */
252 button = gtk_button_new_with_mnemonic (
253 C_("Unlink individual (button)", "_Unlink"));
254 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, RESPONSE_UNLINK);
256 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
257 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), button,
258 TRUE);
260 gtk_widget_show (button);
262 /* Close button */
263 button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
264 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
265 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
266 GTK_RESPONSE_CLOSE);
267 gtk_widget_set_can_default (button, TRUE);
268 gtk_window_set_default (GTK_WINDOW (dialog), button);
269 gtk_widget_show (button);
271 g_signal_connect (dialog, "response",
272 G_CALLBACK (individual_dialogs_response_cb), &edit_dialogs);