]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-password-dialog.c
Move should_create_salut_account to local-xmpp-assistant-widget
[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
22 #include "empathy-password-dialog.h"
23
24 #include <glib/gi18n-lib.h>
25
26 #define DEBUG_FLAG EMPATHY_DEBUG_SASL
27 #include <libempathy/empathy-debug.h>
28 #include <libempathy/empathy-utils.h>
29
30 G_DEFINE_TYPE (EmpathyPasswordDialog, empathy_password_dialog,
31     EMPATHY_TYPE_BASE_PASSWORD_DIALOG)
32
33 enum {
34   PROP_HANDLER = 1,
35
36   LAST_PROPERTY,
37 };
38
39 struct _EmpathyPasswordDialogPriv {
40   EmpathyServerSASLHandler *handler;
41 };
42
43 static void
44 empathy_password_dialog_get_property (GObject *object,
45     guint property_id,
46     GValue *value,
47     GParamSpec *pspec)
48 {
49   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
50
51   switch (property_id)
52     {
53     case PROP_HANDLER:
54       g_value_set_object (value, self->priv->handler);
55       break;
56     default:
57       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
58       break;
59     }
60 }
61
62 static void
63 empathy_password_dialog_set_property (GObject *object,
64     guint property_id,
65     const GValue *value,
66     GParamSpec *pspec)
67 {
68   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
69
70   switch (property_id)
71     {
72     case PROP_HANDLER:
73       g_assert (self->priv->handler == NULL); /* construct only */
74       self->priv->handler = g_value_dup_object (value);
75       break;
76     default:
77       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78       break;
79     }
80 }
81
82 static void
83 empathy_password_dialog_dispose (GObject *object)
84 {
85   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
86
87   tp_clear_object (&self->priv->handler);
88
89   G_OBJECT_CLASS (empathy_password_dialog_parent_class)->dispose (object);
90 }
91
92 static void
93 password_dialog_response_cb (GtkDialog *dialog,
94     gint response,
95     gpointer user_data)
96 {
97   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) dialog;
98   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) dialog;
99
100   if (response == GTK_RESPONSE_OK)
101     {
102       empathy_server_sasl_handler_provide_password (self->priv->handler,
103           gtk_entry_get_text (GTK_ENTRY (base->entry)),
104           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (base->ticky)));
105     }
106   else
107     {
108       empathy_server_sasl_handler_cancel (self->priv->handler);
109     }
110
111   gtk_widget_destroy (GTK_WIDGET (dialog));
112 }
113
114 static void
115 password_dialog_handler_invalidated_cb (EmpathyServerSASLHandler *handler,
116     EmpathyPasswordDialog *dialog)
117 {
118   gtk_widget_destroy (GTK_WIDGET (dialog));
119 }
120
121 static void
122 empathy_password_dialog_constructed (GObject *object)
123 {
124   EmpathyPasswordDialog *self = (EmpathyPasswordDialog *) object;
125   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) object;
126   gchar *text;
127
128   G_OBJECT_CLASS (empathy_password_dialog_parent_class)->constructed (object);
129
130   tp_g_signal_connect_object (self->priv->handler, "invalidated",
131       G_CALLBACK (password_dialog_handler_invalidated_cb),
132       object, 0);
133
134   text = g_strdup_printf (_("Enter your password for account\n<b>%s</b>"),
135       tp_account_get_display_name (base->account));
136   gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (self), text);
137   g_free (text);
138
139   /* only show it if we actually support it */
140   if (empathy_server_sasl_handler_can_save_response_somewhere (
141         self->priv->handler))
142     gtk_widget_show (base->ticky);
143
144   g_signal_connect (self, "response",
145       G_CALLBACK (password_dialog_response_cb), self);
146 }
147
148 static void
149 empathy_password_dialog_init (EmpathyPasswordDialog *self)
150 {
151   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
152       EMPATHY_TYPE_PASSWORD_DIALOG, EmpathyPasswordDialogPriv);
153 }
154
155 static void
156 empathy_password_dialog_class_init (EmpathyPasswordDialogClass *klass)
157 {
158   GParamSpec *pspec;
159   GObjectClass *oclass = G_OBJECT_CLASS (klass);
160
161   g_type_class_add_private (klass, sizeof (EmpathyPasswordDialogPriv));
162
163   oclass->set_property = empathy_password_dialog_set_property;
164   oclass->get_property = empathy_password_dialog_get_property;
165   oclass->dispose = empathy_password_dialog_dispose;
166   oclass->constructed = empathy_password_dialog_constructed;
167
168   pspec = g_param_spec_object ("handler", "The EmpathyServerSASLHandler",
169       "The EmpathyServerSASLHandler to be used.",
170       EMPATHY_TYPE_SERVER_SASL_HANDLER,
171       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
172   g_object_class_install_property (oclass, PROP_HANDLER, pspec);
173 }
174
175 GtkWidget *
176 empathy_password_dialog_new (EmpathyServerSASLHandler *handler)
177 {
178   g_assert (EMPATHY_IS_SERVER_SASL_HANDLER (handler));
179
180   return g_object_new (EMPATHY_TYPE_PASSWORD_DIALOG,
181       "handler", handler,
182       "account", empathy_server_sasl_handler_get_account (handler),
183       NULL);
184 }