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