proper fix to #19468
[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  * HILDON DOC
27  * @shortdesc: GetPasswordDialog is a dialog for getting the user
28  * password.
29  * @longdesc: GetPasswordDialog implements two specified dialogs,
30  * which are pretty much functionally the same, but have 
31  * different outlooks.
32  */
33 #include <glib.h>
34
35 #include <errno.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <stdio.h>
40
41 #include <gtk/gtk.h>
42 #include <hildon-lgpl/hildon-widgets/gtk-infoprint.h>
43 #include <hildon-lgpl/hildon-widgets/hildon-input-mode-hint.h>
44
45 #include <hildon-widgets/hildon-caption.h>
46 #include <hildon-widgets/hildon-get-password-dialog.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 _HildonGetPasswordDialogPrivate 
58   HildonGetPasswordDialogPrivate;
59
60 struct _HildonGetPasswordDialogPrivate {
61   GtkButton *okButton;
62   GtkButton *cancelButton;
63   
64   GtkLabel *domainLabel;
65   HildonCaption *passwordEntry;
66 };
67
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 static void
74 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass *
75                                       class);
76
77 static void hildon_get_password_dialog_init(HildonGetPasswordDialog *
78                                             widget);
79
80 static void hildon_get_password_set_property(GObject * object,
81                                              guint prop_id,
82                                              const GValue * value,
83                                              GParamSpec * pspec);
84 static void hildon_get_password_get_property(GObject * object,
85                                              guint prop_id, GValue * value,
86                                              GParamSpec * pspec);
87
88 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog, 
89                                            gchar *domain);
90
91 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason, 
92                            gpointer user_data);
93
94 enum{
95     PROP_NONE = 0,
96     PROP_DOMAIN,
97     PROP_PASSWORD,
98     PROP_NUMBERS_ONLY  
99 };
100
101 /* Private functions */
102 static void
103 hildon_get_password_set_property(GObject * object,
104                                  guint prop_id,
105                                  const GValue * value, GParamSpec * pspec)
106 {
107   HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
108   HildonGetPasswordDialogPrivate *priv;
109   
110   priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
111   
112   switch (prop_id) {
113   case PROP_DOMAIN: /* Implemented othervise, here just for completeness */
114     gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
115     break;
116   case PROP_PASSWORD:
117     gtk_entry_set_text(GTK_ENTRY
118                        (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
119                        g_value_get_string(value));
120     break;
121   case PROP_NUMBERS_ONLY:
122     g_object_set( G_OBJECT
123                   (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
124                   "input-mode", HILDON_INPUT_MODE_HINT_NUMERIC, NULL );
125     break;
126   default:
127     G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
128     break;
129   }
130 }
131   
132 static void
133 hildon_get_password_get_property(GObject * object,
134                                  guint prop_id,
135                                  GValue * value, GParamSpec * pspec)
136 {
137     HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
138     HildonGetPasswordDialogPrivate *priv;
139     const gchar *string;
140
141     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
142     
143     switch (prop_id) {
144     case PROP_DOMAIN:
145       string = gtk_label_get_text(priv->domainLabel);
146       g_value_set_string(value, string);
147     case PROP_PASSWORD:
148       string = gtk_entry_get_text(GTK_ENTRY(priv->passwordEntry));
149       g_value_set_string(value, string);
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
153       break;
154     }
155 }
156 static void
157 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
158 {
159
160   GObjectClass *object_class = G_OBJECT_CLASS(class);
161   
162   parent_class = g_type_class_peek_parent(class);
163   
164   object_class->set_property = hildon_get_password_set_property;
165   object_class->get_property = hildon_get_password_get_property;
166   
167   g_object_class_install_property 
168     (object_class, 
169      PROP_DOMAIN, 
170      g_param_spec_string ("domain",
171                           "Domain",
172                           "Set domain(content) for optional label.",
173                           NULL,
174                           G_PARAM_WRITABLE));
175   
176   
177   g_object_class_install_property
178     (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   g_object_class_install_property
187     (object_class, 
188      PROP_NUMBERS_ONLY,
189      g_param_spec_boolean ("numbers_only",
190                           "NumbersOnly",
191                           "Set entry to accept only numeric values",
192                           FALSE,
193                           G_PARAM_WRITABLE));
194
195     g_type_class_add_private(class,
196                              sizeof(HildonGetPasswordDialogPrivate));
197 }
198
199 static void
200 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
201 {
202     GtkSizeGroup * group;
203     GtkWidget *control;
204   
205     HildonGetPasswordDialogPrivate *priv =
206       HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
207     group = GTK_SIZE_GROUP(gtk_size_group_new
208                            (GTK_SIZE_GROUP_HORIZONTAL));
209
210     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
211
212     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
213
214     
215     gtk_window_set_title(GTK_WINDOW(dialog), 
216                          _(HILDON_GET_PASSWORD_DIALOG_TITLE));
217
218     priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
219     
220     priv->okButton =
221       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
222                                        _(HILDON_GET_PASSWORD_DIALOG_OK),
223                                        GTK_RESPONSE_OK));
224     priv->cancelButton =
225       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
226                                        _(HILDON_GET_PASSWORD_DIALOG_CANCEL),
227                                        GTK_RESPONSE_CANCEL));
228
229     control = gtk_entry_new();
230     gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
231     priv->passwordEntry = HILDON_CAPTION
232       (hildon_caption_new(group,
233                           _(HILDON_GET_PASSWORD_DIALOG_PASSWORD ),
234                           control, NULL,
235                           HILDON_CAPTION_OPTIONAL));
236     hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
237     
238     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
239                        GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
240     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
241                        GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
242     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
243     
244     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
245   
246 }
247
248 /* Public functions */
249
250 /**
251  * hildon_get_password_dialog_get_type:
252  *
253  * Returns GType for HildonGetPasswordDialog as produced by
254  * g_type_register_static().
255  *
256  * Return value: HildonGetPasswordDialog type
257  **/
258 GType hildon_get_password_dialog_get_type()
259 {
260     static GType dialog_type = 0;
261
262     if (!dialog_type) {
263         static const GTypeInfo dialog_info = {
264             sizeof(HildonGetPasswordDialogClass),
265             NULL,       /* base_init */
266             NULL,       /* base_finalize */
267             (GClassInitFunc) hildon_get_password_dialog_class_init,
268             NULL,       /* class_finalize */
269             NULL,       /* class_data */
270             sizeof(HildonGetPasswordDialog),
271             0,  /* n_preallocs */
272             (GInstanceInitFunc) hildon_get_password_dialog_init
273         };
274
275         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
276                                              "HildonGetPasswordDialog",
277                                              &dialog_info, 0);
278     }
279     return dialog_type;
280 }
281
282 /**
283  * hildon_get_password_dialog_new:
284  * @parent: parent window; can be NULL
285  * @get_old_password_title: FALSE creates a new get password dialog and
286  *                     TRUE creates a new get old password dialog 
287  * 
288  * Construct a new HildonGetPasswordDialog.
289  *
290  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
291  **/
292 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
293                                           gboolean get_old_password_title)
294 {
295     HildonGetPasswordDialog *dialog = g_object_new
296         (HILDON_TYPE_GET_PASSWORD_DIALOG,
297          NULL);
298
299     if (get_old_password_title == FALSE) {
300       HildonGetPasswordDialogPrivate *priv;
301       gtk_window_set_title(GTK_WINDOW(dialog), 
302                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
303                 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
304         gtk_button_set_label(priv->okButton, 
305                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK));
306         gtk_button_set_label(priv->cancelButton, 
307                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL));
308         hildon_caption_set_label(priv->passwordEntry,
309                                  _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)
310                                  );
311     } /* The other logical strings are set in class init */
312
313     if (parent != NULL) {
314         gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
315     }
316
317     return GTK_WIDGET(dialog);
318 }
319
320 /**
321  * hildon_get_password_dialog_new:
322  * @parent: parent window; can be NULL
323  * @password: a default password to be shown in password field.
324  * @get_old_password_title: FALSE creates a new get password dialog and
325  *                     TRUE creates a new get old password dialog 
326  * 
327  * Same as #hildon_get_password_dialog_new but with a default password
328  * in password field.
329  *
330  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
331  **/
332 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
333                                                         gchar *password,
334                                                gboolean get_old_password_title)
335 {
336     GtkWidget *dialog;
337
338     dialog = hildon_get_password_dialog_new(parent, get_old_password_title);
339     if(password != NULL)
340         g_object_set(G_OBJECT(dialog), "password", password, NULL);
341
342     return GTK_WIDGET(dialog);
343 }
344
345 /**
346  * hildon_get_password_dialog_get_password:
347  * @dialog: pointer to HildonSetPasswordDialog
348  * 
349  * Gets the currently inputted password.
350  *
351  * Return value: current password ( if the dialog is successfully 
352  * accepted with 'OK'  )
353  **/
354 const gchar
355     *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
356                                              dialog)
357 {
358     GtkEntry *entry1;
359     gchar *text1;
360
361     HildonGetPasswordDialogPrivate *priv;
362
363     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
364
365     entry1 = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(
366                        priv->passwordEntry)));
367
368     text1 = GTK_ENTRY(entry1)->text;
369
370     return text1;
371 }
372
373 /**
374  * hildon_get_password_dialog_set_domain(GtkWidget *dialog, 
375  * @dialog: the dialog
376  * @domain: the domain or some other descriptive text to be set.
377  * 
378  * sets the optional descriptive text
379  */
380
381 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog, 
382                                                 gchar *domain)
383 {
384   HildonGetPasswordDialogPrivate *priv =
385     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
386   gtk_label_set_text(priv->domainLabel, domain);
387   
388 }
389
390 /**
391  * hildon_get_password_dialog_set_dialog_title
392  *        (HildonGetPasswordDialog *dialog, gchar *new_title) 
393  * @dialog: the dialog
394  * @new_ title: the text to be set as the dialog title.
395  * 
396  * sets the dialog title
397  */
398 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
399                                           gchar *new_title)
400
401 {
402   g_return_if_fail (new_title !=NULL);
403   gtk_window_set_title(GTK_WINDOW(dialog), 
404                        new_title);
405
406 }
407
408 /**
409  * hildon_get_password_dialog_set_caption_label
410  *        (HildonGetPasswordDialog *dialog, gchar *new_caption) 
411  * @dialog: the dialog
412  * @new_caption: the text to be set as the caption label.
413  * 
414  * sets the password entry field's neigbouring label.
415  */
416
417
418 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
419                                                   gchar *new_caption)
420 {
421   
422
423   HildonGetPasswordDialogPrivate *priv =
424     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
425   g_return_if_fail (new_caption !=NULL);
426   hildon_caption_set_label(priv->passwordEntry, new_caption);
427
428 }
429
430 /**
431  * hildon_get_password_dialog_set_max_characters
432  *        (HildonGetPasswordDialog *dialog, gint max_characters,
433  *         gchar *error_ib_message) 
434  * @dialog: the dialog
435  * @max_characters: the maximum number of characters the password dialog
436  * accepts.
437  * @new_caption: the text to be set as the caption label.
438  * 
439  * sets the password entry field's neigbouring label.
440  */
441
442 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
443 {
444
445   HildonGetPasswordDialogPrivate *priv =
446     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
447
448   g_return_if_fail(max_characters >0);
449   g_return_if_fail(dialog);
450
451   gtk_entry_set_max_length(GTK_ENTRY
452                            (hildon_caption_get_control
453                             (priv->passwordEntry)),
454                            max_characters);
455
456   g_signal_connect(GTK_ENTRY
457                    (hildon_caption_get_control
458                     (priv->passwordEntry)),
459                    "invalid_input",
460                    G_CALLBACK(_invalid_input),
461                    NULL
462                    );
463
464 }
465
466 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason, 
467                            gpointer user_data) 
468 {
469   if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
470     gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));
471   }
472
473 }