2006-08-30 Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
[hildon] / hildon-widgets / hildon-name-password-dialog.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation.
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-name-password-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  * #HildonNamePasswordDialog is used to enter a username and password
32  * when accessing a password protected function. The widget performs no
33  * input checking and is used only for retrieving a user name and a
34  * password. 
35  */
36
37 #include <glib.h>
38 #include <gtk/gtk.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <stdio.h>
44
45 #include <hildon-name-password-dialog.h>
46 #include <hildon-widgets/hildon-caption.h>
47
48 #ifdef HAVE_CONFIG_H
49 #include <config.h>
50 #endif
51
52 #include <libintl.h>
53 #define _(String) dgettext(PACKAGE, String)
54
55 static GtkDialogClass *parent_class;
56
57 typedef struct _HildonNamePasswordDialogPrivate 
58   HildonNamePasswordDialogPrivate;
59
60 struct _HildonNamePasswordDialogPrivate {
61   GtkButton *okButton;
62   GtkButton *closeButton;
63   
64   GtkLabel *domainLabel;
65   GtkEntry *nameEntry;
66   GtkEntry *passwordEntry;
67 };
68
69 /* Macro to access the private data of the object instance */
70 #define HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(o)  \
71    (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_NAME_PASSWORD_DIALOG,\
72     HildonNamePasswordDialogPrivate))
73
74 enum{
75     PROP_NONE = 0,
76     PROP_CONTENT,
77     PROP_NAME,
78     PROP_PASSWORD
79 };
80
81 static void
82 hildon_name_password_dialog_class_init(HildonNamePasswordDialogClass *class);
83 static void hildon_name_password_dialog_init(HildonNamePasswordDialog *widget);
84 static void hildon_name_password_dialog_set_property(GObject * object,
85                                                      guint prop_id,
86                                                      const GValue * value, 
87                                                      GParamSpec * pspec);
88 static void hildon_name_password_dialog_get_property(GObject * object,
89                                                      guint prop_id,
90                                                      GValue * value, 
91                                                      GParamSpec * pspec);
92
93 static void
94 hildon_name_password_dialog_set_property(GObject * object,
95                                          guint prop_id,
96                                          const GValue * value, GParamSpec * pspec)
97 {
98     HildonNamePasswordDialog        *dialog = NULL;
99     HildonNamePasswordDialogPrivate *priv   = NULL;
100
101     dialog = HILDON_NAME_PASSWORD_DIALOG(object);
102     priv   = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
103     
104     switch (prop_id) {
105     case PROP_CONTENT:
106       /* Set the password domain text */
107       hildon_name_password_dialog_set_domain(dialog, g_value_get_string(value));
108       break;
109     case PROP_NAME:
110       /* Set the current username displayed in the dialog */
111       gtk_entry_set_text(priv->nameEntry, g_value_get_string(value));
112       break;
113     case PROP_PASSWORD:
114       /* Set the currently entered password */
115       gtk_entry_set_text(priv->passwordEntry, g_value_get_string(value));
116       break;
117     default:
118       G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
119       break;
120     }
121 }
122
123 static void
124 hildon_name_password_dialog_get_property(GObject * object,
125                                          guint prop_id,
126                                          GValue * value, GParamSpec * pspec)
127 {
128     HildonNamePasswordDialog        *dialog = NULL;
129     HildonNamePasswordDialogPrivate *priv   = NULL;
130
131     dialog = HILDON_NAME_PASSWORD_DIALOG(object);
132     priv   = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
133     
134     switch (prop_id) {
135     case PROP_CONTENT:
136       g_value_set_string(value, gtk_label_get_text(priv->domainLabel));
137       break;
138     case PROP_NAME:
139       g_value_set_string(value, hildon_name_password_dialog_get_name(dialog));
140       break;
141     case PROP_PASSWORD:
142       g_value_set_string(value, hildon_name_password_dialog_get_password(dialog));
143      break;
144     default:
145       G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
146       break;
147     }
148 }
149
150 static void
151 hildon_name_password_dialog_class_init(HildonNamePasswordDialogClass *class)
152 {
153     GObjectClass *object_class = G_OBJECT_CLASS(class);
154     
155     parent_class = g_type_class_peek_parent(class);
156     
157     /* Override virtual functions */
158     object_class->set_property = hildon_name_password_dialog_set_property;
159     object_class->get_property = hildon_name_password_dialog_get_property;
160     
161     /* Install new properties */
162     g_object_class_install_property(object_class, 
163                     PROP_CONTENT, 
164                     g_param_spec_string ("content",
165                             "Content",
166                             "Set content for content label.",
167                             NULL,
168                             G_PARAM_READWRITE));
169
170     g_object_class_install_property(object_class,
171                     PROP_NAME, 
172                     g_param_spec_string ("name",
173                             "Name",
174                             "Set content for name entry.",
175                             "DEFAULT",
176                             G_PARAM_READWRITE));
177     
178     g_object_class_install_property(object_class, 
179                     PROP_PASSWORD,
180                     g_param_spec_string ("password",
181                             "Password",
182                             "Set content for password entry",
183                             "DEFAULT",
184                             G_PARAM_READWRITE));
185
186     /* Install private data structure */
187     g_type_class_add_private(class,
188                              sizeof(HildonNamePasswordDialogPrivate));
189 }
190
191 static void
192 hildon_name_password_dialog_init(HildonNamePasswordDialog * dialog)
193 {
194     /* Access private structure */
195     HildonNamePasswordDialogPrivate *priv =
196         HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
197
198     /* Size group for captions */
199     GtkSizeGroup *group =
200         GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
201     HildonCaption *caption;
202     
203     /* Initialize dialog */
204     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
205     gtk_window_set_title(GTK_WINDOW(dialog), _(HILDON_NAME_PASSWORD_DIALOG_TITLE));
206
207     /* Optional domain name label */    
208     priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
209     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
210           GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
211
212     /* Create buttons */    
213     priv->okButton =
214       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
215                                        _(HILDON_NAME_PASSWORD_DIALOG_OK),
216                                        GTK_RESPONSE_OK));
217     priv->closeButton =
218       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
219                                        _(HILDON_NAME_PASSWORD_DIALOG_CANCEL),
220                                        GTK_RESPONSE_CANCEL));
221
222     /* Setup user name entry */
223     priv->nameEntry = GTK_ENTRY(gtk_entry_new());
224     caption = HILDON_CAPTION(hildon_caption_new
225                                      (group,
226                                       _(HILDON_NAME_PASSWORD_DIALOG_NAME ),
227                                       GTK_WIDGET(priv->nameEntry), NULL,
228                                       HILDON_CAPTION_OPTIONAL));
229     hildon_caption_set_separator(caption, "");
230     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
231                        GTK_WIDGET(caption), FALSE, FALSE, 0);
232
233     /* Setup password entry */
234     priv->passwordEntry = GTK_ENTRY(gtk_entry_new());
235     gtk_entry_set_visibility(GTK_ENTRY(priv->passwordEntry), FALSE);
236     caption =
237       HILDON_CAPTION(hildon_caption_new(group,
238                                         _(HILDON_NAME_PASSWORD_DIALOG_PASSWORD),
239                                         GTK_WIDGET(priv->passwordEntry),
240                                         NULL,
241                                         HILDON_CAPTION_OPTIONAL));
242     hildon_caption_set_separator(caption, "");
243     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
244                        GTK_WIDGET(caption), FALSE, FALSE, 0);
245
246     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
247     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
248     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
249     
250     /* Ensure group is freed when all its contents have been removed */
251     g_object_unref(group);
252 }
253
254 GType hildon_name_password_dialog_get_type(void)
255 {
256     static GType dialog_type = 0;
257
258     if (!dialog_type) {
259         static const GTypeInfo dialog_info = {
260             sizeof(HildonNamePasswordDialogClass),
261             NULL,       /* base_init */
262             NULL,       /* base_finalize */
263             (GClassInitFunc) hildon_name_password_dialog_class_init,
264             NULL,       /* class_finalize */
265             NULL,       /* class_data */
266             sizeof(HildonNamePasswordDialog),
267             0,  /* n_preallocs */
268             (GInstanceInitFunc) hildon_name_password_dialog_init
269         };
270         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
271                                              "HildonNamePasswordDialog",
272                                              &dialog_info, 0);
273     }
274
275     return dialog_type;
276 }
277
278 /**
279  * hildon_name_password_dialog_new:
280  * @parent: the parent window of the dialog
281  *
282  * Creates a new #HildonNamePasswordDialog widget with Ok and Close
283  * buttons.
284  *
285  * Returns: the newly created #HildonNamePasswordDialog
286  */
287 GtkWidget *hildon_name_password_dialog_new(GtkWindow * parent)
288 {
289     GtkWidget *self = g_object_new(HILDON_TYPE_NAME_PASSWORD_DIALOG,NULL);
290
291     if (parent)
292         gtk_window_set_transient_for(GTK_WINDOW(self), parent);
293
294     return self;
295 }
296
297 /**
298  * hildon_name_password_dialog_new_with_default:
299  * @parent: the parent window of the dialog
300  * @name: default name, NULL if unset
301  * @password: default password, NULL if unset
302  * 
303  * Same as #hildon_name_password_dialog_new, but with a 
304  * default name and password.
305  *
306  * Returns: the newly created #HildonNamePasswordDialog
307  */
308 GtkWidget *hildon_name_password_dialog_new_with_default(GtkWindow   *parent,
309                                                         const gchar *name,
310                                                         const gchar *password)
311 {
312     GtkWidget *self = hildon_name_password_dialog_new(parent);
313
314     if(name != NULL)
315       g_object_set(G_OBJECT(self), "name", name, NULL);
316     if(password != NULL)
317       g_object_set(G_OBJECT(self), "password", password, NULL);
318
319     return self;
320 }
321
322 /**
323  * hildon_name_password_dialog_get_name:
324  * @dialog: the dialog
325  *
326  * Gets the text that's in the name entry.
327  *
328  * Returns: a pointer to the name string.
329  */
330 const gchar *hildon_name_password_dialog_get_name(HildonNamePasswordDialog
331                                                   * dialog)
332 {
333     HildonNamePasswordDialogPrivate *priv;
334
335     g_return_val_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog), NULL);
336
337     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
338
339     return gtk_entry_get_text(priv->nameEntry);
340 }
341
342 /**
343  * hildon_name_password_dialog_get_password:
344  * @dialog: the dialog
345  * 
346  * Gets the text that's in the password entry.
347  * 
348  * Returns: a pointer to the password string
349  */
350 const gchar *hildon_name_password_dialog_get_password(HildonNamePasswordDialog
351                                                       * dialog)
352 {
353     HildonNamePasswordDialogPrivate *priv;
354
355     g_return_val_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog), NULL);
356
357     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
358
359     return gtk_entry_get_text(priv->passwordEntry);
360 }
361
362 /**
363  * hildon_name_password_dialog_set_domain(GtkWidget *dialog, 
364  * @dialog: the dialog
365  * @domain: the domain or some other descriptive text to be set
366  * 
367  * sets the optional descriptive text
368  */
369
370 void hildon_name_password_dialog_set_domain(HildonNamePasswordDialog *dialog, 
371                                             const gchar *domain)
372 {
373   HildonNamePasswordDialogPrivate *priv;
374
375   g_return_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog));
376
377   priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
378   gtk_label_set_text(priv->domainLabel, domain);
379 }