Make sure that all timeouts in HildonBanner are removed
[hildon] / hildon / hildon-login-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006, 2009 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-login-dialog
27  * @short_description: A widget which allows a user to enter an username
28  * and a password.
29  * @see_also: #HildonGetPasswordDialog, #HildonSetPasswordDialog
30  *
31  * #HildonLoginDialog is used to enter a username and password
32  * when accessing a password protected area. The widget performs no
33  * input checking and is used only for retrieving the username and a
34  * password. 
35  */
36
37 #undef                                          HILDON_DISABLE_DEPRECATED
38
39 #ifdef                                          HAVE_CONFIG_H
40 #include                                        <config.h>
41 #endif
42
43 #include                                        <errno.h>
44 #include                                        <string.h>
45 #include                                        <strings.h>
46 #include                                        <unistd.h>
47 #include                                        <stdio.h>
48 #include                                        <libintl.h>
49 #include                                        <glib.h>
50
51 #include                                        "hildon-login-dialog.h"
52 #include                                        "hildon-caption.h"
53 #include                                        "hildon-login-dialog-private.h"
54 #include                                        "hildon-entry.h"
55
56 enum
57 {
58     PROP_0,
59     PROP_MESSAGE,
60     PROP_USERNAME,
61     PROP_PASSWORD
62 };
63
64 static void
65 hildon_login_dialog_class_init                  (HildonLoginDialogClass *class);
66
67 static void
68 hildon_login_dialog_init                        (HildonLoginDialog *widget);
69
70 static void
71 hildon_login_dialog_set_property                (GObject *object,
72                                                  guint prop_id,
73                                                  const GValue *value, 
74                                                  GParamSpec *pspec);
75
76 static void
77 hildon_login_dialog_get_property                (GObject *object,
78                                                  guint prop_id,
79                                                  GValue *value, 
80                                                  GParamSpec *pspec);
81
82 #define                                         HILDON_LOGIN_DIALOG_TITLE "frw_ti_get_user_name_and_pwd"
83
84 #define                                         HILDON_LOGIN_DIALOG_USERNAME "frw_ti_get_user_name_and_pwd_enter_user_name"
85
86 #define                                         HILDON_LOGIN_DIALOG_PASSWORD "frw_ti_get_user_name_and_pwd_enter_pwd"
87
88 #define                                         HILDON_LOGIN_DIALOG_OK "wdgt_bd_done"
89
90 #define                                         _(String) dgettext("hildon-libs", String)
91
92 static GtkDialogClass*                          parent_class;
93
94 /**
95  * hildon_login_dialog_get_type:
96  *
97  * Returns GType for HildonLoginDialog.
98  *
99  * Returns: HildonLoginDialog type
100  */
101 GType G_GNUC_CONST
102 hildon_login_dialog_get_type                    (void)
103 {
104     static GType dialog_type = 0;
105
106     if (! dialog_type) {
107         static const GTypeInfo dialog_info = {
108             sizeof (HildonLoginDialogClass),
109             NULL,       /* base_init */
110             NULL,       /* base_finalize */
111             (GClassInitFunc) hildon_login_dialog_class_init,
112             NULL,       /* class_finalize */
113             NULL,       /* class_data */
114             sizeof(HildonLoginDialog),
115             0,  /* n_preallocs */
116             (GInstanceInitFunc) hildon_login_dialog_init
117         };
118         dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
119                 "HildonLoginDialog",
120                 &dialog_info, 0);
121     }
122
123     return dialog_type;
124 }
125
126 static void
127 hildon_login_dialog_set_property                (GObject *object,
128                                                  guint prop_id,
129                                                  const GValue *value, 
130                                                  GParamSpec *pspec)
131 {
132     HildonLoginDialog *dialog = NULL;
133     HildonLoginDialogPrivate *priv   = NULL;
134
135     dialog = HILDON_LOGIN_DIALOG (object);
136     priv   = HILDON_LOGIN_DIALOG_GET_PRIVATE(dialog);
137     g_assert (priv);
138
139     switch (prop_id) {
140
141         case PROP_MESSAGE:
142             /* Set the password message text */
143             hildon_login_dialog_set_message (dialog, g_value_get_string (value));
144             break;
145
146         case PROP_USERNAME:
147             /* Set the current username displayed in the dialog */
148             gtk_entry_set_text (GTK_ENTRY (priv->username_entry), g_value_get_string (value));
149             break;
150
151         case PROP_PASSWORD:
152             /* Set the currently entered password */
153             gtk_entry_set_text (GTK_ENTRY (priv->password_entry), g_value_get_string (value));
154             break;
155
156         default:
157             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158             break;
159     }
160 }
161
162 static void
163 hildon_login_dialog_get_property                (GObject *object,
164                                                  guint prop_id,
165                                                  GValue *value, 
166                                                  GParamSpec *pspec)
167 {
168     HildonLoginDialog *dialog = NULL;
169     HildonLoginDialogPrivate *priv = NULL;
170
171     dialog = HILDON_LOGIN_DIALOG (object);
172     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
173     g_assert (priv);
174
175     switch (prop_id) {
176
177         case PROP_MESSAGE:
178             g_value_set_string (value, gtk_label_get_text (priv->message_label));
179             break;
180
181         case PROP_USERNAME:
182             g_value_set_string (value, hildon_login_dialog_get_username (dialog));
183             break;
184
185         case PROP_PASSWORD:
186             g_value_set_string (value, hildon_login_dialog_get_password (dialog));
187             break;
188
189         default:
190             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191             break;
192     }
193 }
194
195 static void
196 hildon_login_dialog_class_init                  (HildonLoginDialogClass *class)
197 {
198     GObjectClass *object_class = G_OBJECT_CLASS (class);
199
200     parent_class = g_type_class_peek_parent (class);
201
202     /* Override virtual functions */
203     object_class->set_property = hildon_login_dialog_set_property;
204     object_class->get_property = hildon_login_dialog_get_property;
205
206     /* Install new properties */
207
208     /**
209      * HildonLoginDialog:message:
210      *
211      * Optional message displayed to the user.
212      */
213     g_object_class_install_property (object_class, 
214             PROP_MESSAGE, 
215             g_param_spec_string ("message",
216                 "Message",
217                 "Message displayed by the dialog",
218                 NULL,
219                 G_PARAM_READWRITE));
220
221     /**
222      * HildonLoginDialog:username:
223      *
224      * Contents of the username field.
225      */
226     g_object_class_install_property (object_class,
227             PROP_USERNAME, 
228             g_param_spec_string ("username",
229                 "Username",
230                 "Username field",
231                 "DEFAULT",
232                 G_PARAM_READWRITE));
233     
234     /**
235      * HildonLoginDialog:password:
236      *
237      * Contents of the password field.
238      */
239     g_object_class_install_property (object_class, 
240             PROP_PASSWORD,
241             g_param_spec_string ("password",
242                 "Password",
243                 "Password field",
244                 "DEFAULT",
245                 G_PARAM_READWRITE));
246
247     /* Install private data structure */
248     g_type_class_add_private (class, sizeof (HildonLoginDialogPrivate));
249 }
250
251 static void
252 hildon_login_dialog_init                        (HildonLoginDialog *dialog)
253 {
254     /* Access private structure */
255     HildonLoginDialogPrivate *priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
256     g_assert (priv);
257
258     /* Size group for captions */
259     GtkSizeGroup *group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
260     HildonCaption *caption;
261
262     /* Initialize dialog */
263     gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
264     gtk_window_set_title (GTK_WINDOW(dialog), _(HILDON_LOGIN_DIALOG_TITLE));
265
266     /* Optional message label */    
267     /* FIXME Set the warpping for the message label */
268     priv->message_label = GTK_LABEL (gtk_label_new (NULL));
269     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
270             GTK_WIDGET (priv->message_label), FALSE, FALSE, 0);
271
272     /* Create buttons */    
273     gtk_dialog_add_button (GTK_DIALOG (dialog), _(HILDON_LOGIN_DIALOG_OK), GTK_RESPONSE_OK);
274
275     /* Setup username entry */
276     priv->username_entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
277
278 #ifdef MAEMO_GTK 
279     g_object_set (priv->username_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
280 #endif
281
282     caption = HILDON_CAPTION (hildon_caption_new
283             (group,
284              _(HILDON_LOGIN_DIALOG_USERNAME),
285              GTK_WIDGET (priv->username_entry), NULL,
286              HILDON_CAPTION_OPTIONAL));
287
288     hildon_caption_set_separator (caption, "");
289     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
290             GTK_WIDGET (caption), FALSE, FALSE, 0);
291
292     /* Setup password entry */
293     priv->password_entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
294
295 #ifdef MAEMO_GTK
296     g_object_set (priv->password_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
297 #endif
298
299     gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
300
301     caption = HILDON_CAPTION (hildon_caption_new (group,
302                     _(HILDON_LOGIN_DIALOG_PASSWORD),
303                     GTK_WIDGET (priv->password_entry),
304                     NULL,
305                     HILDON_CAPTION_OPTIONAL));
306
307     hildon_caption_set_separator (caption, "");
308     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
309             GTK_WIDGET (caption), FALSE, FALSE, 0);
310
311     gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
312     gtk_widget_show_all (GTK_DIALOG (dialog)->action_area);
313     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
314
315     /* Ensure group is freed when all its contents have been removed */
316     g_object_unref (group);
317 }
318
319 /**
320  * hildon_login_dialog_new:
321  * @parent: the parent window of the dialog
322  *
323  * Creates a new #HildonLoginDialog widget with Ok and Close
324  * buttons.
325  *
326  * Returns: the newly created #HildonLoginDialog
327  */
328 GtkWidget*
329 hildon_login_dialog_new                         (GtkWindow *parent)
330 {
331     GtkWidget *self = g_object_new (HILDON_TYPE_LOGIN_DIALOG, NULL);
332
333     if (parent)
334         gtk_window_set_transient_for (GTK_WINDOW (self), parent);
335
336     return self;
337 }
338
339 /**
340  * hildon_login_dialog_new_with_default:
341  * @parent: the parent window of the dialog
342  * @name: default username, NULL if unset
343  * @password: default password, NULL if unset
344  * 
345  * Same as #hildon_login_dialog_new but with a 
346  * default username and password.
347  *
348  * Returns: the newly created #HildonLoginDialog
349  */
350 GtkWidget*
351 hildon_login_dialog_new_with_default            (GtkWindow *parent,
352                                                  const gchar *name,
353                                                  const gchar *password)
354 {
355     GtkWidget *self = hildon_login_dialog_new(parent);
356
357     if (name != NULL)
358         g_object_set (G_OBJECT (self), "username", name, NULL);
359
360     if (password != NULL)
361         g_object_set (G_OBJECT (self), "password", password, NULL);
362
363     return self;
364 }
365
366 /**
367  * hildon_login_dialog_get_username:
368  * @dialog: the dialog
369  *
370  * Gets the text that's in the username entry.
371  *
372  * Returns: a pointer to the name string. You should not modify it.
373  */
374 const gchar*
375 hildon_login_dialog_get_username                (HildonLoginDialog *dialog)
376 {
377     HildonLoginDialogPrivate *priv;
378
379     g_return_val_if_fail (HILDON_IS_LOGIN_DIALOG (dialog), NULL);
380
381     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
382     g_assert (priv);
383
384     return gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
385 }
386
387 /**
388  * hildon_login_dialog_get_password:
389  * @dialog: the dialog
390  * 
391  * Gets the text that's in the password entry.
392  * 
393  * Returns: a pointer to the password string. You should not modify it.
394  */
395 const gchar*
396 hildon_login_dialog_get_password                (HildonLoginDialog *dialog)
397 {
398     HildonLoginDialogPrivate *priv;
399
400     g_return_val_if_fail (HILDON_IS_LOGIN_DIALOG (dialog), NULL);
401
402     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
403     g_assert (priv);
404
405     return gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
406 }
407
408 /**
409  * hildon_login_dialog_set_message:
410  * @dialog: the dialog
411  * @msg: the message or some other descriptive text to be set
412  * 
413  * Sets the optional descriptive text that is displayed on the top 
414  * of the dialog. 
415  */
416 void 
417 hildon_login_dialog_set_message                 (HildonLoginDialog *dialog, 
418                                                  const gchar *msg)
419 {
420     HildonLoginDialogPrivate *priv;
421
422     g_return_if_fail (HILDON_IS_LOGIN_DIALOG (dialog));
423
424     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
425     g_assert (priv);
426
427     gtk_label_set_text (priv->message_label, msg);
428 }
429
430