]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-bad-password-dialog.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[empathy.git] / libempathy-gtk / empathy-bad-password-dialog.c
1 /*
2  * empathy-bad-password-dialog.c - Source for EmpathyBadPasswordDialog
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-bad-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
29 G_DEFINE_TYPE (EmpathyBadPasswordDialog, empathy_bad_password_dialog,
30     EMPATHY_TYPE_BASE_PASSWORD_DIALOG)
31
32 enum {
33   PROP_PASSWORD = 1,
34
35   LAST_PROPERTY,
36 };
37
38 /* signal enum */
39 enum {
40   RETRY,
41   LAST_SIGNAL,
42 };
43
44 static guint signals[LAST_SIGNAL] = {0};
45
46 struct _EmpathyBadPasswordDialogPriv {
47   gchar *password;
48 };
49
50 static void
51 empathy_bad_password_dialog_get_property (GObject *object,
52     guint property_id,
53     GValue *value,
54     GParamSpec *pspec)
55 {
56   EmpathyBadPasswordDialog *self = (EmpathyBadPasswordDialog *) object;
57
58   switch (property_id)
59     {
60     case PROP_PASSWORD:
61       g_value_set_string (value, self->priv->password);
62       break;
63     default:
64       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
65       break;
66     }
67 }
68
69 static void
70 empathy_bad_password_dialog_set_property (GObject *object,
71     guint property_id,
72     const GValue *value,
73     GParamSpec *pspec)
74 {
75   EmpathyBadPasswordDialog *self = (EmpathyBadPasswordDialog *) object;
76
77   switch (property_id)
78     {
79     case PROP_PASSWORD:
80       g_assert (self->priv->password == NULL); /* construct only */
81       self->priv->password = g_value_dup_string (value);
82       break;
83     default:
84       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85       break;
86     }
87 }
88
89 static void
90 empathy_bad_password_dialog_finalize (GObject *object)
91 {
92   EmpathyBadPasswordDialog *self = (EmpathyBadPasswordDialog *) object;
93
94   tp_clear_pointer (&self->priv->password, g_free);
95
96   G_OBJECT_CLASS (empathy_bad_password_dialog_parent_class)->finalize (object);
97 }
98
99 static void
100 bad_password_dialog_response_cb (GtkDialog *dialog,
101     gint response,
102     gpointer user_data)
103 {
104   EmpathyBadPasswordDialog *self = (EmpathyBadPasswordDialog *) dialog;
105   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) dialog;
106
107   if (response == GTK_RESPONSE_OK)
108     {
109       const gchar *password;
110
111       password = gtk_entry_get_text (GTK_ENTRY (base->entry));
112
113       g_signal_emit (self, signals[RETRY], 0, base->account, password);
114     }
115
116   gtk_widget_destroy (GTK_WIDGET (dialog));
117 }
118
119 static void
120 empathy_bad_password_dialog_constructed (GObject *object)
121 {
122   EmpathyBadPasswordDialog *self = (EmpathyBadPasswordDialog *) object;
123   EmpathyBasePasswordDialog *base = (EmpathyBasePasswordDialog *) object;
124   gchar *text;
125
126   G_OBJECT_CLASS (empathy_bad_password_dialog_parent_class)->constructed (
127       object);
128
129   text = g_strdup_printf (_("Authentication failed for account <b>%s</b>"),
130       tp_account_get_display_name (base->account));
131   gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (self), text);
132   g_free (text);
133
134   if (self->priv->password != NULL)
135     {
136       gtk_entry_set_text (GTK_ENTRY (base->entry), self->priv->password);
137
138       gtk_editable_select_region (GTK_EDITABLE (base->entry), 0, -1);
139     }
140
141   gtk_button_set_label (GTK_BUTTON (base->ok_button), _("Retry"));
142
143   g_signal_connect (self, "response",
144       G_CALLBACK (bad_password_dialog_response_cb), self);
145 }
146
147 static void
148 empathy_bad_password_dialog_init (EmpathyBadPasswordDialog *self)
149 {
150   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
151       EMPATHY_TYPE_BAD_PASSWORD_DIALOG, EmpathyBadPasswordDialogPriv);
152 }
153
154 static void
155 empathy_bad_password_dialog_class_init (EmpathyBadPasswordDialogClass *klass)
156 {
157   GParamSpec *pspec;
158   GObjectClass *oclass = G_OBJECT_CLASS (klass);
159
160   g_type_class_add_private (klass, sizeof (EmpathyBadPasswordDialogPriv));
161
162   oclass->set_property = empathy_bad_password_dialog_set_property;
163   oclass->get_property = empathy_bad_password_dialog_get_property;
164   oclass->finalize = empathy_bad_password_dialog_finalize;
165   oclass->constructed = empathy_bad_password_dialog_constructed;
166
167   pspec = g_param_spec_string ("password", "Password",
168       "The wrong password",
169       NULL,
170       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
171   g_object_class_install_property (oclass, PROP_PASSWORD, pspec);
172
173   signals[RETRY] = g_signal_new ("retry",
174       G_TYPE_FROM_CLASS (klass),
175       G_SIGNAL_RUN_LAST, 0,
176       NULL, NULL,
177       g_cclosure_marshal_generic,
178       G_TYPE_NONE, 2, TP_TYPE_ACCOUNT, G_TYPE_STRING);
179 }
180
181 GtkWidget *
182 empathy_bad_password_dialog_new (TpAccount *account,
183     const gchar *password)
184 {
185   g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
186
187   return g_object_new (EMPATHY_TYPE_BAD_PASSWORD_DIALOG,
188       "account", account,
189       "password", password,
190       NULL);
191 }