3.12.12
[nijm-empathy.git] / libempathy-gtk / empathy-subscription-dialog.c
blobd7e70bfb3b10c76335652245ca7e54efeb92163a
1 /*
2 * Copyright (C) 2012 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: Guillaume Desmottes <guillaume.desmottes@collabora.com>
21 #include "config.h"
22 #include "empathy-subscription-dialog.h"
24 #include <glib/gi18n-lib.h>
25 #include <telepathy-glib/telepathy-glib-dbus.h>
27 #include "empathy-individual-widget.h"
28 #include "empathy-utils.h"
30 G_DEFINE_TYPE (EmpathySubscriptionDialog, empathy_subscription_dialog, GTK_TYPE_MESSAGE_DIALOG)
32 enum
34 PROP_INDIVIDUAL = 1,
35 PROP_MESSAGE,
36 N_PROPS
40 enum
42 LAST_SIGNAL
45 static guint signals[LAST_SIGNAL];
48 struct _EmpathySubscriptionDialogPriv
50 FolksIndividual *individual;
51 gchar *message;
54 static void
55 empathy_subscription_dialog_get_property (GObject *object,
56 guint property_id,
57 GValue *value,
58 GParamSpec *pspec)
60 EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
62 switch (property_id)
64 case PROP_INDIVIDUAL:
65 g_value_set_object (value, self->priv->individual);
66 break;
67 case PROP_MESSAGE:
68 g_value_set_string (value, self->priv->message);
69 break;
70 default:
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
72 break;
76 static void
77 empathy_subscription_dialog_set_property (GObject *object,
78 guint property_id,
79 const GValue *value,
80 GParamSpec *pspec)
82 EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
84 switch (property_id)
86 case PROP_INDIVIDUAL:
87 g_assert (self->priv->individual == NULL); /* construct only */
88 self->priv->individual = g_value_dup_object (value);
89 break;
90 case PROP_MESSAGE:
91 g_assert (self->priv->message == NULL); /* construct only */
92 self->priv->message = g_value_dup_string (value);
93 break;
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
96 break;
101 * block_contact_dialog_show:
102 * @parent: the parent of this dialog (or %NULL)
103 * @contact: the contact for this dialog
104 * @abusive: a pointer to store the value of the abusive contact check box
105 * (or %NULL)
107 * Returns: %TRUE if the user wishes to block the contact
109 static gboolean
110 block_contact_dialog_show (GtkWindow *parent,
111 EmpathyContact *contact,
112 GdkPixbuf *avatar,
113 gboolean *abusive)
115 GtkWidget *dialog;
116 GtkWidget *abusive_check = NULL;
117 int res;
118 TpConnection *conn;
120 dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
121 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Block %s?"),
122 empathy_contact_get_alias (contact));
124 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
125 _("Are you sure you want to block '%s' from contacting you again?"),
126 empathy_contact_get_alias (contact));
128 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
129 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
130 _("_Block"), GTK_RESPONSE_REJECT,
131 NULL);
133 if (avatar != NULL)
135 GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
136 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
137 gtk_widget_show (image);
140 conn = empathy_contact_get_connection (contact);
142 /* ask the user if they want to also report the contact as abusive */
143 if (tp_connection_can_report_abusive (conn))
145 GtkWidget *vbox;
147 vbox = gtk_message_dialog_get_message_area (
148 GTK_MESSAGE_DIALOG (dialog));
149 abusive_check = gtk_check_button_new_with_mnemonic (
150 _("_Report this contact as abusive"));
152 gtk_box_pack_start (GTK_BOX (vbox), abusive_check,
153 FALSE, TRUE, 0);
154 gtk_widget_show (abusive_check);
157 res = gtk_dialog_run (GTK_DIALOG (dialog));
158 if (abusive != NULL)
160 if (abusive_check != NULL)
161 *abusive = gtk_toggle_button_get_active (
162 GTK_TOGGLE_BUTTON (abusive_check));
163 else
164 *abusive = FALSE;
167 gtk_widget_destroy (dialog);
169 return res == GTK_RESPONSE_REJECT;
172 static void
173 response_cb (GtkDialog *dialog,
174 gint reponse,
175 EmpathySubscriptionDialog *self)
177 EmpathyContact *contact;
179 contact = empathy_contact_dup_from_folks_individual (self->priv->individual);
181 if (reponse == GTK_RESPONSE_YES)
183 empathy_contact_add_to_contact_list (contact, "");
185 else if (reponse == GTK_RESPONSE_NO)
187 empathy_contact_remove_from_contact_list (contact);
189 else if (reponse == GTK_RESPONSE_REJECT)
191 gboolean abusive;
193 /* confirm the blocking */
194 if (block_contact_dialog_show (GTK_WINDOW (dialog), contact,
195 NULL, &abusive))
197 TpContact *tp_contact;
199 empathy_contact_remove_from_contact_list (contact);
201 tp_contact = empathy_contact_get_tp_contact (contact);
203 tp_contact_block_async (tp_contact, abusive, NULL, NULL);
205 else
207 /* if they don't confirm, return back to the
208 * first dialog */
209 return;
213 gtk_widget_destroy (GTK_WIDGET (self));
216 static void
217 empathy_subscription_dialog_constructed (GObject *object)
219 EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
220 void (*chain_up) (GObject *) =
221 ((GObjectClass *) empathy_subscription_dialog_parent_class)->constructed;
222 GtkWidget *content;
223 GtkWidget *individual_widget;
224 const gchar *alias;
225 gchar *tmp;
226 EmpathyContact *contact;
227 TpConnection *conn;
229 if (chain_up != NULL)
230 chain_up (object);
232 g_assert (self->priv->individual != NULL);
234 gtk_window_set_title (GTK_WINDOW (self), _("Subscription Request"));
236 alias = folks_alias_details_get_alias (
237 FOLKS_ALIAS_DETAILS (self->priv->individual));
239 tmp = g_strdup_printf (
240 _("%s would like permission to see when you are online"), alias);
242 /* Why is there no gtk_message_dialog_set_text()? */
243 g_object_set (self, "text", tmp, NULL);
244 g_free (tmp);
246 if (self->priv->message != NULL)
248 tmp = g_strdup_printf ("<i>%s</i>", self->priv->message);
249 gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (self),
250 "%s", tmp);
251 g_free (tmp);
254 /* Individual widget */
255 individual_widget = empathy_individual_widget_new (self->priv->individual,
256 EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
257 EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
258 EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS);
260 gtk_container_set_border_width (GTK_CONTAINER (individual_widget), 8);
262 content = gtk_dialog_get_content_area (GTK_DIALOG (self));
264 gtk_box_pack_start (GTK_BOX (content), individual_widget, TRUE, TRUE, 0);
265 gtk_widget_show (individual_widget);
267 /* Add 'Block' button if supported */
268 contact = empathy_contact_dup_from_folks_individual (self->priv->individual);
269 conn = empathy_contact_get_connection (contact);
271 if (tp_proxy_has_interface_by_id (conn,
272 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
274 gtk_dialog_add_button (GTK_DIALOG (self),
275 _("_Block"), GTK_RESPONSE_REJECT);
278 g_object_unref (contact);
280 gtk_dialog_add_buttons (GTK_DIALOG (self),
281 _("_Decline"), GTK_RESPONSE_NO,
282 _("_Accept"), GTK_RESPONSE_YES,
283 NULL);
285 g_signal_connect (self, "response",
286 G_CALLBACK (response_cb), self);
289 static void
290 empathy_subscription_dialog_dispose (GObject *object)
292 EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
293 void (*chain_up) (GObject *) =
294 ((GObjectClass *) empathy_subscription_dialog_parent_class)->dispose;
296 g_clear_object (&self->priv->individual);
298 if (chain_up != NULL)
299 chain_up (object);
302 static void
303 empathy_subscription_dialog_finalize (GObject *object)
305 EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
306 void (*chain_up) (GObject *) =
307 ((GObjectClass *) empathy_subscription_dialog_parent_class)->finalize;
309 g_free (self->priv->message);
311 if (chain_up != NULL)
312 chain_up (object);
315 static void
316 empathy_subscription_dialog_class_init (
317 EmpathySubscriptionDialogClass *klass)
319 GObjectClass *oclass = G_OBJECT_CLASS (klass);
320 GParamSpec *spec;
322 oclass->get_property = empathy_subscription_dialog_get_property;
323 oclass->set_property = empathy_subscription_dialog_set_property;
324 oclass->constructed = empathy_subscription_dialog_constructed;
325 oclass->dispose = empathy_subscription_dialog_dispose;
326 oclass->finalize = empathy_subscription_dialog_finalize;
328 spec = g_param_spec_object ("individual", "individual",
329 "FolksIndividual",
330 FOLKS_TYPE_INDIVIDUAL,
331 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
332 g_object_class_install_property (oclass, PROP_INDIVIDUAL, spec);
334 spec = g_param_spec_string ("message", "message",
335 "Message",
336 NULL,
337 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
338 g_object_class_install_property (oclass, PROP_MESSAGE, spec);
340 g_type_class_add_private (klass, sizeof (EmpathySubscriptionDialogPriv));
343 static void
344 empathy_subscription_dialog_init (EmpathySubscriptionDialog *self)
346 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
347 EMPATHY_TYPE_SUBSCRIPTION_DIALOG, EmpathySubscriptionDialogPriv);
350 GtkWidget *
351 empathy_subscription_dialog_new (FolksIndividual *individual,
352 const gchar *message)
354 return g_object_new (EMPATHY_TYPE_SUBSCRIPTION_DIALOG,
355 "individual", individual,
356 "message", message,
357 "message-type", GTK_MESSAGE_QUESTION,
358 NULL);