Gazpacho support, added the following properties:
[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     PROP_CAPTION_LABEL,
100     PROP_MAX_CHARS
101 };
102
103 /* Private functions */
104 static void
105 hildon_get_password_set_property(GObject * object,
106                                  guint prop_id,
107                                  const GValue * value, GParamSpec * pspec)
108 {
109   HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
110   HildonGetPasswordDialogPrivate *priv;
111   
112   priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
113   
114   switch (prop_id) {
115   case PROP_DOMAIN: /* Implemented othervise, here just for completeness */
116     gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
117     break;
118   case PROP_PASSWORD:
119     gtk_entry_set_text(GTK_ENTRY
120                        (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
121                        g_value_get_string(value));
122     break;
123   case PROP_NUMBERS_ONLY:
124     g_object_set( G_OBJECT
125                   (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
126                   "input-mode", HILDON_INPUT_MODE_HINT_NUMERIC, NULL );
127     break;
128   case PROP_CAPTION_LABEL:
129     hildon_get_password_dialog_set_caption(dialog, g_value_get_string(value));
130     break;
131   case PROP_MAX_CHARS:
132     hildon_get_password_dialog_set_max_characters(dialog, 
133                                                   g_value_get_string(value));
134     break;
135   default:
136     G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
137     break;
138   }
139 }
140   
141 static void
142 hildon_get_password_get_property(GObject * object,
143                                  guint prop_id,
144                                  GValue * value, GParamSpec * pspec)
145 {
146     HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
147     HildonGetPasswordDialogPrivate *priv;
148     const gchar *string;
149     gint max_length; 
150
151     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
152     
153     switch (prop_id) {
154     case PROP_DOMAIN:
155       string = gtk_label_get_text(priv->domainLabel);
156       g_value_set_string(value, string);
157     case PROP_PASSWORD:
158       string = gtk_entry_get_text(GTK_ENTRY(priv->passwordEntry));
159       g_value_set_string(value, string);
160       break;
161     case PROP_CAPTION_LABEL:
162       string = hildon_caption_get_label(priv->passwordEntry);
163       g_value_set_string(value, string);
164       break;
165     case PROP_MAX_CHARS:
166       max_length = gtk_entry_get_max_length(
167               GTK_ENTRY (hildon_caption_get_control (priv->passwordEntry)));
168       g_value_set_int(value, max_length);
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
172       break;
173     }
174 }
175
176 static void
177 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
178 {
179
180   GObjectClass *object_class = G_OBJECT_CLASS(class);
181   
182   parent_class = g_type_class_peek_parent(class);
183   
184   object_class->set_property = hildon_get_password_set_property;
185   object_class->get_property = hildon_get_password_get_property;
186   
187   g_object_class_install_property 
188     (object_class, 
189      PROP_DOMAIN, 
190      g_param_spec_string ("domain",
191                           "Domain",
192                           "Set domain(content) for optional label.",
193                           NULL,
194                           G_PARAM_WRITABLE));
195   
196   
197   g_object_class_install_property
198     (object_class, 
199      PROP_PASSWORD,
200      g_param_spec_string ("password",
201                           "Password",
202                           "Set content for password entry",
203                           "DEFAULT",
204                           G_PARAM_READWRITE));
205
206   g_object_class_install_property
207     (object_class, 
208      PROP_NUMBERS_ONLY,
209      g_param_spec_boolean ("numbers_only",
210                           "NumbersOnly",
211                           "Set entry to accept only numeric values",
212                           FALSE,
213                           G_PARAM_WRITABLE));
214
215   g_object_class_install_property
216     (object_class, 
217      PROP_CAPTION_LABEL,
218      g_param_spec_string ("caption-label",
219                           "Caption Label",
220                           "The text to be set as the caption label",
221                           NULL,
222                           G_PARAM_READWRITE));
223
224   g_object_class_install_property
225     (object_class, 
226      PROP_MAX_CHARS,
227      g_param_spec_int ("max-characters",
228                        "Maximum Characters",
229                        "The maximum number of characters the password"
230                        " dialog accepts",
231                        G_MININT,
232                        G_MAXINT,
233                        0,
234                        G_PARAM_READWRITE));
235
236     g_type_class_add_private(class,
237                              sizeof(HildonGetPasswordDialogPrivate));
238 }
239
240 static void
241 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
242 {
243     GtkSizeGroup * group;
244     GtkWidget *control;
245   
246     HildonGetPasswordDialogPrivate *priv =
247       HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
248     group = GTK_SIZE_GROUP(gtk_size_group_new
249                            (GTK_SIZE_GROUP_HORIZONTAL));
250
251     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
252
253     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
254
255     
256     gtk_window_set_title(GTK_WINDOW(dialog), 
257                          _(HILDON_GET_PASSWORD_DIALOG_TITLE));
258
259     priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
260     
261     priv->okButton =
262       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
263                                        _(HILDON_GET_PASSWORD_DIALOG_OK),
264                                        GTK_RESPONSE_OK));
265     priv->cancelButton =
266       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
267                                        _(HILDON_GET_PASSWORD_DIALOG_CANCEL),
268                                        GTK_RESPONSE_CANCEL));
269
270     control = gtk_entry_new();
271     gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
272     priv->passwordEntry = HILDON_CAPTION
273       (hildon_caption_new(group,
274                           _(HILDON_GET_PASSWORD_DIALOG_PASSWORD ),
275                           control, NULL,
276                           HILDON_CAPTION_OPTIONAL));
277     hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
278     
279     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
280                        GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
281     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
282                        GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
283     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
284     
285     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
286   
287 }
288
289 /* Public functions */
290
291 /**
292  * hildon_get_password_dialog_get_type:
293  *
294  * Returns GType for HildonGetPasswordDialog as produced by
295  * g_type_register_static().
296  *
297  * Return value: HildonGetPasswordDialog type
298  **/
299 GType hildon_get_password_dialog_get_type()
300 {
301     static GType dialog_type = 0;
302
303     if (!dialog_type) {
304         static const GTypeInfo dialog_info = {
305             sizeof(HildonGetPasswordDialogClass),
306             NULL,       /* base_init */
307             NULL,       /* base_finalize */
308             (GClassInitFunc) hildon_get_password_dialog_class_init,
309             NULL,       /* class_finalize */
310             NULL,       /* class_data */
311             sizeof(HildonGetPasswordDialog),
312             0,  /* n_preallocs */
313             (GInstanceInitFunc) hildon_get_password_dialog_init
314         };
315
316         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
317                                              "HildonGetPasswordDialog",
318                                              &dialog_info, 0);
319     }
320     return dialog_type;
321 }
322
323 /**
324  * hildon_get_password_dialog_new:
325  * @parent: parent window; can be NULL
326  * @get_old_password_title: FALSE creates a new get password dialog and
327  *                     TRUE creates a new get old password dialog 
328  * 
329  * Construct a new HildonGetPasswordDialog.
330  *
331  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
332  **/
333 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
334                                           gboolean get_old_password_title)
335 {
336     HildonGetPasswordDialog *dialog = g_object_new
337         (HILDON_TYPE_GET_PASSWORD_DIALOG,
338          NULL);
339
340     if (get_old_password_title == FALSE) {
341       HildonGetPasswordDialogPrivate *priv;
342       gtk_window_set_title(GTK_WINDOW(dialog), 
343                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
344                 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
345         gtk_button_set_label(priv->okButton, 
346                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK));
347         gtk_button_set_label(priv->cancelButton, 
348                              _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL));
349         hildon_caption_set_label(priv->passwordEntry,
350                                  _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)
351                                  );
352     } /* The other logical strings are set in class init */
353
354     if (parent != NULL) {
355         gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
356     }
357
358     return GTK_WIDGET(dialog);
359 }
360
361 /**
362  * hildon_get_password_dialog_new:
363  * @parent: parent window; can be NULL
364  * @password: a default password to be shown in password field.
365  * @get_old_password_title: FALSE creates a new get password dialog and
366  *                     TRUE creates a new get old password dialog 
367  * 
368  * Same as #hildon_get_password_dialog_new but with a default password
369  * in password field.
370  *
371  * Return value: a new #GtkWidget of type HildonGetPasswordDialog
372  **/
373 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
374                                                         gchar *password,
375                                                gboolean get_old_password_title)
376 {
377     GtkWidget *dialog;
378
379     dialog = hildon_get_password_dialog_new(parent, get_old_password_title);
380     if(password != NULL)
381         g_object_set(G_OBJECT(dialog), "password", password, NULL);
382
383     return GTK_WIDGET(dialog);
384 }
385
386 /**
387  * hildon_get_password_dialog_get_password:
388  * @dialog: pointer to HildonSetPasswordDialog
389  * 
390  * Gets the currently inputted password.
391  *
392  * Return value: current password ( if the dialog is successfully 
393  * accepted with 'OK'  )
394  **/
395 const gchar
396     *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
397                                              dialog)
398 {
399     GtkEntry *entry1;
400     gchar *text1;
401
402     HildonGetPasswordDialogPrivate *priv;
403
404     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
405
406     entry1 = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(
407                        priv->passwordEntry)));
408
409     text1 = GTK_ENTRY(entry1)->text;
410
411     return text1;
412 }
413
414 /**
415  * hildon_get_password_dialog_set_domain(GtkWidget *dialog, 
416  * @dialog: the dialog
417  * @domain: the domain or some other descriptive text to be set.
418  * 
419  * sets the optional descriptive text
420  */
421
422 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog, 
423                                                 gchar *domain)
424 {
425   HildonGetPasswordDialogPrivate *priv =
426     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
427   gtk_label_set_text(priv->domainLabel, domain);
428   
429 }
430
431 /**
432  * hildon_get_password_dialog_set_dialog_title
433  *        (HildonGetPasswordDialog *dialog, gchar *new_title) 
434  * @dialog: the dialog
435  * @new_ title: the text to be set as the dialog title.
436  * 
437  * sets the dialog title
438  */
439 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
440                                           gchar *new_title)
441
442 {
443   g_return_if_fail (new_title !=NULL);
444   gtk_window_set_title(GTK_WINDOW(dialog), 
445                        new_title);
446
447 }
448
449 /**
450  * hildon_get_password_dialog_set_caption
451  *        (HildonGetPasswordDialog *dialog, gchar *new_caption) 
452  * @dialog: the dialog
453  * @new_caption: the text to be set as the caption label.
454  * 
455  * sets the password entry field's neigbouring label.
456  */
457
458
459 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
460                                                   gchar *new_caption)
461 {
462   
463
464   HildonGetPasswordDialogPrivate *priv =
465     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
466   g_return_if_fail (new_caption !=NULL);
467   hildon_caption_set_label(priv->passwordEntry, new_caption);
468
469 }
470
471 /**
472  * hildon_get_password_dialog_set_max_characters
473  *        (HildonGetPasswordDialog *dialog, gint max_characters,
474  *         gchar *error_ib_message) 
475  * @dialog: the dialog
476  * @max_characters: the maximum number of characters the password dialog
477  * accepts.
478  * @new_caption: the text to be set as the caption label.
479  * 
480  * sets the password entry field's neigbouring label.
481  */
482
483 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
484 {
485
486   HildonGetPasswordDialogPrivate *priv =
487     HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
488
489   g_return_if_fail(max_characters >0);
490   g_return_if_fail(dialog);
491
492   gtk_entry_set_max_length(GTK_ENTRY
493                            (hildon_caption_get_control
494                             (priv->passwordEntry)),
495                            max_characters);
496
497   g_signal_connect(GTK_ENTRY
498                    (hildon_caption_get_control
499                     (priv->passwordEntry)),
500                    "invalid_input",
501                    G_CALLBACK(_invalid_input),
502                    NULL
503                    );
504
505 }
506
507 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason, 
508                            gpointer user_data) 
509 {
510   if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
511     gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));
512   }
513
514 }