Fix gtk-doc packaging
[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.
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     g_object_set (priv->username_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
278
279     caption = HILDON_CAPTION (hildon_caption_new
280             (group,
281              _(HILDON_LOGIN_DIALOG_USERNAME),
282              GTK_WIDGET (priv->username_entry), NULL,
283              HILDON_CAPTION_OPTIONAL));
284
285     hildon_caption_set_separator (caption, "");
286     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
287             GTK_WIDGET (caption), FALSE, FALSE, 0);
288
289     /* Setup password entry */
290     priv->password_entry = GTK_ENTRY (gtk_entry_new ());
291     g_object_set (priv->password_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
292     gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
293
294     caption = HILDON_CAPTION (hildon_caption_new (group,
295                     _(HILDON_LOGIN_DIALOG_PASSWORD),
296                     GTK_WIDGET (priv->password_entry),
297                     NULL,
298                     HILDON_CAPTION_OPTIONAL));
299
300     hildon_caption_set_separator (caption, "");
301     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
302             GTK_WIDGET (caption), FALSE, FALSE, 0);
303
304     gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
305     gtk_widget_show_all (GTK_DIALOG (dialog)->action_area);
306     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
307
308     /* Ensure group is freed when all its contents have been removed */
309     g_object_unref (group);
310 }
311
312 /**
313  * hildon_login_dialog_new:
314  * @parent: the parent window of the dialog
315  *
316  * Creates a new #HildonLoginDialog widget with Ok and Close
317  * buttons.
318  *
319  * Returns: the newly created #HildonLoginDialog
320  */
321 GtkWidget*
322 hildon_login_dialog_new                         (GtkWindow *parent)
323 {
324     GtkWidget *self = g_object_new (HILDON_TYPE_LOGIN_DIALOG, NULL);
325
326     if (parent)
327         gtk_window_set_transient_for (GTK_WINDOW (self), parent);
328
329     return self;
330 }
331
332 /**
333  * hildon_login_dialog_new_with_default:
334  * @parent: the parent window of the dialog
335  * @name: default username, NULL if unset
336  * @password: default password, NULL if unset
337  * 
338  * Same as #hildon_login_dialog_new but with a 
339  * default username and password.
340  *
341  * Returns: the newly created #HildonLoginDialog
342  */
343 GtkWidget*
344 hildon_login_dialog_new_with_default            (GtkWindow *parent,
345                                                  const gchar *name,
346                                                  const gchar *password)
347 {
348     GtkWidget *self = hildon_login_dialog_new(parent);
349
350     if (name != NULL)
351         g_object_set (G_OBJECT (self), "username", name, NULL);
352
353     if (password != NULL)
354         g_object_set (G_OBJECT (self), "password", password, NULL);
355
356     return self;
357 }
358
359 /**
360  * hildon_login_dialog_get_username:
361  * @dialog: the dialog
362  *
363  * Gets the text that's in the username entry.
364  *
365  * Returns: a pointer to the name string. You should not modify it.
366  */
367 const gchar*
368 hildon_login_dialog_get_username                (HildonLoginDialog *dialog)
369 {
370     HildonLoginDialogPrivate *priv;
371
372     g_return_val_if_fail (HILDON_IS_LOGIN_DIALOG (dialog), NULL);
373
374     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
375     g_assert (priv);
376
377     return gtk_entry_get_text (priv->username_entry);
378 }
379
380 /**
381  * hildon_login_dialog_get_password:
382  * @dialog: the dialog
383  * 
384  * Gets the text that's in the password entry.
385  * 
386  * Returns: a pointer to the password string. You should not modify it.
387  */
388 const gchar*
389 hildon_login_dialog_get_password                (HildonLoginDialog *dialog)
390 {
391     HildonLoginDialogPrivate *priv;
392
393     g_return_val_if_fail (HILDON_IS_LOGIN_DIALOG (dialog), NULL);
394
395     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
396     g_assert (priv);
397
398     return gtk_entry_get_text (priv->password_entry);
399 }
400
401 /**
402  * hildon_login_dialog_set_message:
403  * @dialog: the dialog
404  * @msg: the message or some other descriptive text to be set
405  * 
406  * Sets the optional descriptive text that is displayed on the top 
407  * of the dialog. 
408  */
409 void 
410 hildon_login_dialog_set_message                 (HildonLoginDialog *dialog, 
411                                                  const gchar *msg)
412 {
413     HildonLoginDialogPrivate *priv;
414
415     g_return_if_fail (HILDON_IS_LOGIN_DIALOG (dialog));
416
417     priv = HILDON_LOGIN_DIALOG_GET_PRIVATE (dialog);
418     g_assert (priv);
419
420     gtk_label_set_text (priv->message_label, msg);
421 }
422
423