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