2008-03-03 Sven Herzberg <sven@imendio.com>
[hildon] / src / hildon-login-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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 #ifdef                                          HAVE_CONFIG_H
38 #include                                        <config.h>
39 #endif
40
41 #include                                        "hildon-login-dialog.h"
42 #include                                        <glib.h>
43 #include                                        <gtk/gtk.h>
44 #include                                        <errno.h>
45 #include                                        <string.h>
46 #include                                        <strings.h>
47 #include                                        <unistd.h>
48 #include                                        <stdio.h>
49 #include                                        "hildon-caption.h"
50 #include                                        <libintl.h>
51 #include                                        "hildon-login-dialog-private.h"
52
53 enum
54 {
55     PROP_0,
56     PROP_MESSAGE,
57     PROP_USERNAME,
58     PROP_PASSWORD
59 };
60
61 static void
62 hildon_login_dialog_class_init                  (HildonLoginDialogClass *class);
63
64 static void
65 hildon_login_dialog_init                        (HildonLoginDialog *widget);
66
67 static void
68 hildon_login_dialog_set_property                (GObject *object,
69                                                  guint prop_id,
70                                                  const GValue *value, 
71                                                  GParamSpec *pspec);
72
73 static void
74 hildon_login_dialog_get_property                (GObject *object,
75                                                  guint prop_id,
76                                                  GValue *value, 
77                                                  GParamSpec *pspec);
78
79 #define                                         HILDON_LOGIN_DIALOG_TITLE "frw_ti_get_user_name_and_pwd"
80
81 #define                                         HILDON_LOGIN_DIALOG_USERNAME "frw_ti_get_user_name_and_pwd_enter_user_name"
82
83 #define                                         HILDON_LOGIN_DIALOG_PASSWORD "frw_ti_get_user_name_and_pwd_enter_pwd"
84
85 #define                                         HILDON_LOGIN_DIALOG_OK "frw_bd_get_user_name_and_pwd_ok"
86
87 #define                                         HILDON_LOGIN_DIALOG_CANCEL "frw_bd_get_user_name_and_pwd_cancel"
88
89 #define                                         _(String) dgettext("hildon-libs", String)
90
91 static GtkDialogClass*                          parent_class;
92
93 /**
94  * hildon_login_dialog_get_type:
95  *
96  * Returns GType for HildonLoginDialog.
97  *
98  * Returns: HildonLoginDialog type
99  */
100 GType G_GNUC_CONST
101 hildon_login_dialog_get_type                    (void)
102 {
103     static GType dialog_type = 0;
104
105     if (! dialog_type) {
106         static const GTypeInfo dialog_info = {
107             sizeof (HildonLoginDialogClass),
108             NULL,       /* base_init */
109             NULL,       /* base_finalize */
110             (GClassInitFunc) hildon_login_dialog_class_init,
111             NULL,       /* class_finalize */
112             NULL,       /* class_data */
113             sizeof(HildonLoginDialog),
114             0,  /* n_preallocs */
115             (GInstanceInitFunc) hildon_login_dialog_init
116         };
117         dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
118                 "HildonLoginDialog",
119                 &dialog_info, 0);
120     }
121
122     return dialog_type;
123 }
124
125 static void
126 hildon_login_dialog_set_property                (GObject *object,
127                                                  guint prop_id,
128                                                  const GValue *value, 
129                                                  GParamSpec *pspec)
130 {
131     HildonLoginDialog *dialog = NULL;
132     HildonLoginDialogPrivate *priv   = NULL;
133
134     dialog = HILDON_LOGIN_DIALOG (object);
135     priv   = HILDON_LOGIN_DIALOG_GET_PRIVATE(dialog);
136     g_assert (priv);
137
138     switch (prop_id) {
139
140         case PROP_MESSAGE:
141             /* Set the password message text */
142             hildon_login_dialog_set_message (dialog, g_value_get_string (value));
143             break;
144
145         case PROP_USERNAME:
146             /* Set the current username displayed in the dialog */
147             gtk_entry_set_text (priv->username_entry, g_value_get_string (value));
148             break;
149
150         case PROP_PASSWORD:
151             /* Set the currently entered password */
152             gtk_entry_set_text (priv->password_entry, g_value_get_string (value));
153             break;
154
155         default:
156             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157             break;
158     }
159 }
160
161 static void
162 hildon_login_dialog_get_property                (GObject *object,
163                                                  guint prop_id,
164                                                  GValue *value, 
165                                                  GParamSpec *pspec)
166 {
167     HildonLoginDialog *dialog = NULL;
168     HildonLoginDialogPrivate *priv = NULL;
169
170     dialog = HILDON_LOGIN_DIALOG (object);
171     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
172     g_assert (priv);
173
174     switch (prop_id) {
175
176         case PROP_MESSAGE:
177             g_value_set_string (value, gtk_label_get_text (priv->message_label));
178             break;
179
180         case PROP_USERNAME:
181             g_value_set_string (value, hildon_login_dialog_get_username (dialog));
182             break;
183
184         case PROP_PASSWORD:
185             g_value_set_string (value, hildon_login_dialog_get_password (dialog));
186             break;
187
188         default:
189             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190             break;
191     }
192 }
193
194 static void
195 hildon_login_dialog_class_init                  (HildonLoginDialogClass *class)
196 {
197     GObjectClass *object_class = G_OBJECT_CLASS (class);
198
199     parent_class = g_type_class_peek_parent (class);
200
201     /* Override virtual functions */
202     object_class->set_property = hildon_login_dialog_set_property;
203     object_class->get_property = hildon_login_dialog_get_property;
204
205     /* Install new properties */
206
207     /**
208      * HildonLoginDialog:message:
209      *
210      * Optional message displayed to the user.
211      */
212     g_object_class_install_property (object_class, 
213             PROP_MESSAGE, 
214             g_param_spec_string ("message",
215                 "Message",
216                 "Message displayed by the dialog",
217                 NULL,
218                 G_PARAM_READWRITE));
219
220     /**
221      * HildonLoginDialog:username:
222      *
223      * Contents of the username field.
224      */
225     g_object_class_install_property (object_class,
226             PROP_USERNAME, 
227             g_param_spec_string ("username",
228                 "Username",
229                 "Username field",
230                 "DEFAULT",
231                 G_PARAM_READWRITE));
232     
233     /**
234      * HildonLoginDialog:password:
235      *
236      * Contents of the password field.
237      */
238     g_object_class_install_property (object_class, 
239             PROP_PASSWORD,
240             g_param_spec_string ("password",
241                 "Password",
242                 "Password field",
243                 "DEFAULT",
244                 G_PARAM_READWRITE));
245
246     /* Install private data structure */
247     g_type_class_add_private (class, sizeof (HildonLoginDialogPrivate));
248 }
249
250 static void
251 hildon_login_dialog_init                        (HildonLoginDialog *dialog)
252 {
253     /* Access private structure */
254     HildonLoginDialogPrivate *priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
255     g_assert (priv);
256
257     /* Size group for captions */
258     GtkSizeGroup *group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
259     HildonCaption *caption;
260
261     /* Initialize dialog */
262     gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
263     gtk_window_set_title (GTK_WINDOW(dialog), _(HILDON_LOGIN_DIALOG_TITLE));
264
265     /* Optional message label */    
266     /* FIXME Set the warpping for the message label */
267     priv->message_label = GTK_LABEL (gtk_label_new (NULL));
268     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
269             GTK_WIDGET (priv->message_label), FALSE, FALSE, 0);
270
271     /* Create buttons */    
272     gtk_dialog_add_button (GTK_DIALOG (dialog), _(HILDON_LOGIN_DIALOG_OK), GTK_RESPONSE_OK);
273     gtk_dialog_add_button (GTK_DIALOG (dialog), _(HILDON_LOGIN_DIALOG_CANCEL), GTK_RESPONSE_CANCEL);
274
275     /* Setup username entry */
276     priv->username_entry = GTK_ENTRY (gtk_entry_new ());
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 = GTK_ENTRY (gtk_entry_new ());
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 (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 (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