6e33dc1566f37fd9114c084c63a69ee94e61af6e
[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, 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-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_USERNAME,
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_USERNAME:
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_USERNAME:
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_USERNAME, 
172                     g_param_spec_string ("username",
173                             "Username",
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     g_object_set (priv->nameEntry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL);
225     caption = HILDON_CAPTION(hildon_caption_new
226                                      (group,
227                                       _(HILDON_NAME_PASSWORD_DIALOG_NAME ),
228                                       GTK_WIDGET(priv->nameEntry), NULL,
229                                       HILDON_CAPTION_OPTIONAL));
230     hildon_caption_set_separator(caption, "");
231     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
232                        GTK_WIDGET(caption), FALSE, FALSE, 0);
233
234     /* Setup password entry */
235     priv->passwordEntry = GTK_ENTRY(gtk_entry_new());
236     g_object_set (priv->passwordEntry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
237     gtk_entry_set_visibility(GTK_ENTRY(priv->passwordEntry), FALSE);
238     caption =
239       HILDON_CAPTION(hildon_caption_new(group,
240                                         _(HILDON_NAME_PASSWORD_DIALOG_PASSWORD),
241                                         GTK_WIDGET(priv->passwordEntry),
242                                         NULL,
243                                         HILDON_CAPTION_OPTIONAL));
244     hildon_caption_set_separator(caption, "");
245     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
246                        GTK_WIDGET(caption), FALSE, FALSE, 0);
247
248     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
249     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
250     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
251     
252     /* Ensure group is freed when all its contents have been removed */
253     g_object_unref(group);
254 }
255
256 GType hildon_name_password_dialog_get_type(void)
257 {
258     static GType dialog_type = 0;
259
260     if (!dialog_type) {
261         static const GTypeInfo dialog_info = {
262             sizeof(HildonNamePasswordDialogClass),
263             NULL,       /* base_init */
264             NULL,       /* base_finalize */
265             (GClassInitFunc) hildon_name_password_dialog_class_init,
266             NULL,       /* class_finalize */
267             NULL,       /* class_data */
268             sizeof(HildonNamePasswordDialog),
269             0,  /* n_preallocs */
270             (GInstanceInitFunc) hildon_name_password_dialog_init
271         };
272         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
273                                              "HildonNamePasswordDialog",
274                                              &dialog_info, 0);
275     }
276
277     return dialog_type;
278 }
279
280 /**
281  * hildon_name_password_dialog_new:
282  * @parent: the parent window of the dialog
283  *
284  * Creates a new #HildonNamePasswordDialog widget with Ok and Close
285  * buttons.
286  *
287  * Returns: the newly created #HildonNamePasswordDialog
288  */
289 GtkWidget *hildon_name_password_dialog_new(GtkWindow * parent)
290 {
291     GtkWidget *self = g_object_new(HILDON_TYPE_NAME_PASSWORD_DIALOG,NULL);
292
293     if (parent)
294         gtk_window_set_transient_for(GTK_WINDOW(self), parent);
295
296     return self;
297 }
298
299 /**
300  * hildon_name_password_dialog_new_with_default:
301  * @parent: the parent window of the dialog
302  * @name: default username, NULL if unset
303  * @password: default password, NULL if unset
304  * 
305  * Same as #hildon_name_password_dialog_new, but with a 
306  * default name and password.
307  *
308  * Returns: the newly created #HildonNamePasswordDialog
309  */
310 GtkWidget *hildon_name_password_dialog_new_with_default(GtkWindow   *parent,
311                                                         const gchar *name,
312                                                         const gchar *password)
313 {
314     GtkWidget *self = hildon_name_password_dialog_new(parent);
315
316     if(name != NULL)
317       g_object_set(G_OBJECT(self), "username", name, NULL);
318     if(password != NULL)
319       g_object_set(G_OBJECT(self), "password", password, NULL);
320
321     return self;
322 }
323
324 /**
325  * hildon_name_password_dialog_get_name:
326  * @dialog: the dialog
327  *
328  * Gets the text that's in the name entry.
329  *
330  * Returns: a pointer to the name string.
331  */
332 const gchar *hildon_name_password_dialog_get_name(HildonNamePasswordDialog
333                                                   * dialog)
334 {
335     HildonNamePasswordDialogPrivate *priv;
336
337     g_return_val_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog), NULL);
338
339     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
340
341     return gtk_entry_get_text(priv->nameEntry);
342 }
343
344 /**
345  * hildon_name_password_dialog_get_password:
346  * @dialog: the dialog
347  * 
348  * Gets the text that's in the password entry.
349  * 
350  * Returns: a pointer to the password string
351  */
352 const gchar *hildon_name_password_dialog_get_password(HildonNamePasswordDialog
353                                                       * dialog)
354 {
355     HildonNamePasswordDialogPrivate *priv;
356
357     g_return_val_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog), NULL);
358
359     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
360
361     return gtk_entry_get_text(priv->passwordEntry);
362 }
363
364 /**
365  * hildon_name_password_dialog_set_domain(GtkWidget *dialog, 
366  * @dialog: the dialog
367  * @domain: the domain or some other descriptive text to be set
368  * 
369  * sets the optional descriptive text
370  */
371
372 void hildon_name_password_dialog_set_domain(HildonNamePasswordDialog *dialog, 
373                                             const gchar *domain)
374 {
375   HildonNamePasswordDialogPrivate *priv;
376
377   g_return_if_fail(HILDON_IS_NAME_PASSWORD_DIALOG(dialog));
378
379   priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
380   gtk_label_set_text(priv->domainLabel, domain);
381 }