]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-password-dialog.c
Merge remote-tracking branch 'origin/gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-password-dialog.c
1 /*
2  * empathy-password-dialog.c - Source for EmpathyPasswordDialog
3  * Copyright (C) 2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21 #include "empathy-password-dialog.h"
22
23 #include <glib/gi18n-lib.h>
24
25 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
26 #include "empathy-debug.h"
27
28 G_DEFINE_TYPE (EmpathyPasswordDialog, empathy_password_dialog,
29     EMPATHY_TYPE_BASE_PASSWORD_DIALOG)
30
31 enum {
32   PROP_HANDLER = 1,
33
34   LAST_PROPERTY,
35 };
36
37 struct _EmpathyPasswordDialogPriv {
38   EmpathyServerSASLHandler *handler;
39 };
40
41 static void
42 empathy_password_dialog_get_property (GObject *object,
43     guint property_id,
44     GValue *value,
45     GParamSpec *pspec)
46 {
47   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
48
49   switch (property_id)
50     {
51     case PROP_HANDLER:
52       g_value_set_object (value, self->priv->handler);
53       break;
54     default:
55       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
56       break;
57     }
58 }
59
60 static void
61 empathy_password_dialog_set_property (GObject *object,
62     guint property_id,
63     const GValue *value,
64     GParamSpec *pspec)
65 {
66   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
67
68   switch (property_id)
69     {
70     case PROP_HANDLER:
71       g_assert (self->priv->handler == NULL); /* construct only */
72       self->priv->handler = g_value_dup_object (value);
73       break;
74     default:
75       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76       break;
77     }
78 }
79
80 static void
81 empathy_password_dialog_dispose (GObject *object)
82 {
83   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
84
85   tp_clear_object (&self->priv->handler);
86
87   G_OBJECT_CLASS (empathy_password_dialog_parent_class)->dispose (object);
88 }
89
90 static void
91 password_dialog_response_cb (GtkDialog *dialog,
92     gint response,
93     gpointer user_data)
94 {
95   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) dialog;
96   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) dialog;
97
98   if (response == GTK_RESPONSE_OK)
99     {
100       empathy_server_sasl_handler_provide_password (self->priv->handler,
101           gtk_entry_get_text (GTK_ENTRY (base->entry)),
102           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (base->ticky)));
103     }
104   else
105     {
106       empathy_server_sasl_handler_cancel (self->priv->handler);
107     }
108
109   gtk_widget_destroy (GTK_WIDGET (dialog));
110 }
111
112 static void
113 password_dialog_handler_invalidated_cb (EmpathyServerSASLHandler *handler,
114     EmpathyPasswordDialog *dialog)
115 {
116   gtk_widget_destroy (GTK_WIDGET (dialog));
117 }
118
119 static void
120 empathy_password_dialog_constructed (GObject *object)
121 {
122   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
123   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) object;
124   gchar *text;
125
126   G_OBJECT_CLASS (empathy_password_dialog_parent_class)->constructed (object);
127
128   tp_g_signal_connect_object (self->priv->handler, "invalidated",
129       G_CALLBACK (password_dialog_handler_invalidated_cb),
130       object, 0);
131
132   text = g_strdup_printf (_("Enter your password for account\n<b>%s</b>"),
133       tp_account_get_display_name (base->account));
134   gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (self), text);
135   g_free (text);
136
137   /* only show it if we actually support it */
138   if (empathy_server_sasl_handler_can_save_response_somewhere (
139         self->priv->handler))
140     gtk_widget_show (base->ticky);
141
142   g_signal_connect (self, "response",
143       G_CALLBACK (password_dialog_response_cb), self);
144 }
145
146 static void
147 empathy_password_dialog_init (EmpathyPasswordDialog *self)
148 {
149   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
150       EMPATHY_TYPE_PASSWORD_DIALOG, EmpathyPasswordDialogPriv);
151 }
152
153 static void
154 empathy_password_dialog_class_init (EmpathyPasswordDialogClass *klass)
155 {
156   GParamSpec *pspec;
157   GObjectClass *oclass = G_OBJECT_CLASS (klass);
158
159   g_type_class_add_private (klass, sizeof (EmpathyPasswordDialogPriv));
160
161   oclass->set_property = empathy_password_dialog_set_property;
162   oclass->get_property = empathy_password_dialog_get_property;
163   oclass->dispose = empathy_password_dialog_dispose;
164   oclass->constructed = empathy_password_dialog_constructed;
165
166   pspec = g_param_spec_object ("handler", "The EmpathyServerSASLHandler",
167       "The EmpathyServerSASLHandler to be used.",
168       EMPATHY_TYPE_SERVER_SASL_HANDLER,
169       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
170   g_object_class_install_property (oclass, PROP_HANDLER, pspec);
171 }
172
173 GtkWidget *
174 empathy_password_dialog_new (EmpathyServerSASLHandler *handler)
175 {
176   g_assert (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
177
178   return g_object_new (EMPATHY_TYPE_PASSWORD_DIALOG,
179       "handler", handler,
180       "account", empathy_server_sasl_handler_get_account (handler),
181       NULL);
182 }