import SDK release
[hildon] / hildon-widgets / hildon-get-password-dialog.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@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; either 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  * @file hildon-get-password-dialog.c
27  *
28  * This file contains the API implementation for Hildon Get Password
29  * dialog.
30  *  Get password dialog:
31  *   @desc: Get Password Dialog is used to enter a password when accessing
32  *   a password protected function 
33  * 
34  * Get old password dialog:
35  *   @desc: Get Old Password is used to enter the current password 
36  *          in order to either change or remove the existing password.
37  *
38  *  @seealso: #HildonSetPasswordDialog
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include <gtk/gtkentry.h>
46 #include <gtk/gtkbox.h>
47 #include <gtk/gtkeditable.h>
48 #include <gtk/gtksignal.h>
49 #include <gdk/gdkkeysyms.h>
50
51 #include <hildon-widgets/hildon-caption.h>
52 #include "hildon-get-password-dialog.h"
53
54 #include <libintl.h>
55 #define _(String) dgettext(PACKAGE, String)
56
57 static GtkDialogClass *parent_class;
58
59
60 #define PASSWORD_DIALOG_WIDTH       370
61 #define PASSWORD_DIALOG_HEIGHT      100
62
63 #define ENTER_PASSWORD_CHAR         _("ecdg_fi_verify_pwd_enter_pwd")
64 #define OK_CHAR                     _("ecdg_bd_verify_password_dialog_ok")
65 #define CANCEL_CHAR                 _("ecdg_bd_verify_password_dialog_cancel")
66 #define PASSWORD_PROTECTED_CHAR     _("ecdg_bd_verify_password")
67 #define ENTER_CURRENT_PASSWORD_CHAR _("ecdg_ti_get_old_password")
68
69 #define HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(obj) \
70  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
71   HILDON_TYPE_GET_PASSWORD_DIALOG, HildonGetPasswordDialogPrivate));
72
73 typedef struct _HildonGetPasswordDialogPrivate
74     HildonGetPasswordDialogPrivate;
75
76 static void
77 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass *
78                                       class);
79 static void hildon_get_password_dialog_init(HildonGetPasswordDialog *
80                                             widget);
81 static gboolean hildon_get_password_dialog_released(GtkWidget * widget,
82                                                     GdkEventButton * event,
83                                                     gpointer user_data);
84 static gboolean special_key_listener(GtkWidget * widget,
85                                      GdkEventKey * event,
86                                      gpointer user_data);
87
88 static void hildon_get_password_set_property(GObject * object,
89                                              guint prop_id,
90                                              const GValue * value,
91                                              GParamSpec * pspec);
92 static void hildon_get_password_get_property(GObject * object,
93                                              guint prop_id, GValue * value,
94                                              GParamSpec * pspec);
95
96 /* private struct */
97 struct _HildonGetPasswordDialogPrivate {
98     /* Tab one */
99     GtkWidget *entry1;
100     GtkWidget *caption1;
101
102     /* OK/Cancel buttons */
103     GtkWidget *okButton;
104     GtkWidget *cancelButton;
105 };
106
107 enum{
108     PROP_NONE = 0,
109     PROP_PASSWORD
110 };
111
112 /* Private functions */
113 static void
114 hildon_get_password_set_property(GObject * object,
115                                  guint prop_id,
116                                  const GValue * value, GParamSpec * pspec)
117 {
118     HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
119     HildonGetPasswordDialogPrivate *priv;
120
121     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
122     
123     switch (prop_id) {
124     case PROP_PASSWORD:
125         gtk_entry_set_text(GTK_ENTRY(priv->entry1), g_value_get_string(value));
126         break;
127     default:
128         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
129         break;
130     }
131 }
132
133 static void
134 hildon_get_password_get_property(GObject * object,
135                                  guint prop_id,
136                                  GValue * value, GParamSpec * pspec)
137 {
138     HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
139     HildonGetPasswordDialogPrivate *priv;
140     const gchar *string;
141
142     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
143     
144     switch (prop_id) {
145     case PROP_PASSWORD:
146         string = gtk_entry_get_text(GTK_ENTRY(priv->entry1));
147         g_value_set_string(value, string);
148         break;
149     default:
150         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
151         break;
152     }
153 }
154 static void
155 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
156 {
157     GParamSpec *pspec1;
158     GObjectClass *object_class = G_OBJECT_CLASS(class);
159   
160     parent_class = g_type_class_peek_parent(class);
161
162     object_class->set_property = hildon_get_password_set_property;
163     object_class->get_property = hildon_get_password_get_property;
164
165     pspec1 = g_param_spec_string("password",
166                                  "Password content",
167                                  "Set content to dialog",
168                                  "DEFAULT",
169                                  G_PARAM_READWRITE);
170
171     g_object_class_install_property (object_class,
172                                      PROP_PASSWORD,pspec1);
173     
174     g_type_class_add_private(class,
175                              sizeof(HildonGetPasswordDialogPrivate));
176 }
177
178 static void
179 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
180 {
181     HildonGetPasswordDialogPrivate *priv;
182     GtkSizeGroup *group;
183
184     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
185
186     group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
187
188     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
189
190     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
191
192     /* create tab */
193     priv->entry1 = gtk_entry_new();
194     gtk_widget_show(priv->entry1);
195
196     priv->caption1 = hildon_caption_new(group,
197                                         ENTER_PASSWORD_CHAR,
198                                         priv->entry1,
199                                         NULL, HILDON_CAPTION_OPTIONAL);
200
201     gtk_entry_set_visibility(GTK_ENTRY(priv->entry1), FALSE);
202
203     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
204                        priv->caption1, TRUE, TRUE, 0);
205     gtk_widget_show(priv->caption1);
206
207     /* Callback functions */
208     g_signal_connect(G_OBJECT(priv->entry1), "key-press-event",
209                      G_CALLBACK(special_key_listener), dialog);
210
211     g_signal_connect(priv->entry1, "button-release-event",
212                      G_CALLBACK(hildon_get_password_dialog_released),
213                      NULL);
214
215     /* Create the OK/CANCEL buttons */
216     priv->okButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
217                                            OK_CHAR, GTK_RESPONSE_OK);
218     priv->cancelButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
219                                                CANCEL_CHAR,
220                                                GTK_RESPONSE_CANCEL);
221     gtk_window_resize(GTK_WINDOW(dialog),
222                       PASSWORD_DIALOG_WIDTH, PASSWORD_DIALOG_HEIGHT);
223 }
224
225 static gboolean
226 hildon_get_password_dialog_released(GtkWidget * widget,
227                                     GdkEventButton * event,
228                                     gpointer user_data)
229 {
230     GtkEditable *editable = GTK_EDITABLE(widget);
231
232     gtk_editable_select_region(GTK_EDITABLE(editable), 0, -1);
233     gtk_widget_grab_focus(GTK_WIDGET(editable));
234
235     return FALSE;
236 }
237
238 static gboolean
239 special_key_listener(GtkWidget * widget,
240                      GdkEventKey * event, gpointer user_data)
241 {
242     HildonGetPasswordDialog *dialog =
243         HILDON_GET_PASSWORD_DIALOG(user_data);
244
245     if ((event->keyval == GDK_Return) || (event->keyval == GDK_KP_Enter)) {
246         gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
247     }
248
249     return FALSE;
250 }
251
252 /* Public functions */
253
254 /**
255  * hildon_get_password_dialog_get_type:
256  *
257  * Returns GType for HildonGetPasswordDialog as produced by
258  * g_type_register_static().
259  *
260  * Return value: HildonGetPasswordDialog type
261  **/
262 GType hildon_get_password_dialog_get_type()
263 {
264     static GType dialog_type = 0;
265
266     if (!dialog_type) {
267         static const GTypeInfo dialog_info = {
268             sizeof(HildonGetPasswordDialogClass),
269             NULL,       /* base_init */
270             NULL,       /* base_finalize */
271             (GClassInitFunc) hildon_get_password_dialog_class_init,
272             NULL,       /* class_finalize */
273             NULL,       /* class_data */
274             sizeof(HildonGetPasswordDialog),
275             0,  /* n_preallocs */
276             (GInstanceInitFunc) hildon_get_password_dialog_init
277         };
278
279         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
280                                              "HildonGetPasswordDialog",
281                                              &dialog_info, 0);
282     }
283     return dialog_type;
284 }
285
286 /**
287  * hildon_get_password_dialog_new:
288  * @parent: parent window; can be NULL
289  * @get_old_password_title: FALSE creates a new get password dialog and
290  *                     TRUE creates a new get old password dialog 
291  * 
292  * Construct a new HildonGetPasswordDialog.
293  *
294  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
295  **/
296 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
297                                           gboolean get_old_password_title)
298 {
299     HildonGetPasswordDialog *dialog = g_object_new
300         (HILDON_TYPE_GET_PASSWORD_DIALOG,
301          NULL);
302
303     if (get_old_password_title == FALSE) {
304         gtk_window_set_title(GTK_WINDOW(dialog), PASSWORD_PROTECTED_CHAR);
305     } else {
306         gtk_window_set_title(GTK_WINDOW(dialog),
307                              ENTER_CURRENT_PASSWORD_CHAR);
308     }
309
310     if (parent != NULL) {
311         gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
312     }
313
314     return GTK_WIDGET(dialog);
315 }
316
317 /**
318  * hildon_get_password_dialog_new:
319  * @parent: parent window; can be NULL
320  * @password: a default password to be shown in password field.
321  * @get_old_password_title: FALSE creates a new get password dialog and
322  *                     TRUE creates a new get old password dialog 
323  * 
324  * Same as #hildon_get_password_dialog_new but with a default password
325  * in password field.
326  *
327  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
328  **/
329 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
330                                                         gchar *password,
331                                                gboolean get_old_password_title)
332 {
333     GtkWidget *dialog;
334
335     dialog = hildon_get_password_dialog_new(parent, get_old_password_title);
336     if(password != NULL)
337         g_object_set(G_OBJECT(dialog), "password", password, NULL);
338
339     return GTK_WIDGET(dialog);
340 }
341
342 /**
343  * hildon_get_password_dialog_get_password:
344  * @dialog: pointer to HildonSetPasswordDialog
345  * 
346  * Gets the currently inputted password.
347  *
348  * Return value: current password ( if the dialog is successfully 
349  * accepted with 'OK'  )
350  **/
351 const gchar
352     *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
353                                              dialog)
354 {
355     GtkEntry *entry1;
356     gchar *text1;
357
358     HildonGetPasswordDialogPrivate *priv;
359
360     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
361
362     entry1 = GTK_ENTRY(hildon_caption_get_control
363                        (HILDON_CAPTION(priv->caption1)));
364
365     text1 = GTK_ENTRY(entry1)->text;
366
367     return text1;
368 }