latest update
[hildon] / hildon-widgets / hildon-name-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 /* HILDON DOC
26  * @shortdesc: NamePasswordDialog is a dialog for getting the user name
27  * and password.
28  * @longdesc: NamePasswordDialog is a simple dummy dialog for getting
29  * name&password from the user.
30  */
31
32 #include <glib.h>
33 #include <gtk/gtk.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <unistd.h>
38 #include <stdio.h>
39
40 #include <hildon-name-password-dialog.h>
41 #include <hildon-widgets/hildon-caption.h>
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include <libintl.h>
48 #define _(String) dgettext(PACKAGE, String)
49
50 static GtkDialogClass *parent_class;
51
52 typedef struct _HildonNamePasswordDialogPrivate 
53   HildonNamePasswordDialogPrivate;
54
55 struct _HildonNamePasswordDialogPrivate {
56   GtkButton *okButton;
57   GtkButton *closeButton;
58   
59   GtkLabel *domainLabel;
60   HildonCaption *nameEntry;
61   HildonCaption *passwordEntry;
62 };
63
64 #define HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(o)  \
65    (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_NAME_PASSWORD_DIALOG,\
66     HildonNamePasswordDialogPrivate))
67
68 enum{
69     PROP_NONE = 0,
70     PROP_CONTENT,
71     PROP_NAME,
72     PROP_PASSWORD
73 };
74
75 static void
76 hildon_name_password_dialog_class_init(HildonNamePasswordDialogClass *class);
77 static void hildon_name_password_dialog_init(HildonNamePasswordDialog *widget);
78 static void hildon_name_password_dialog_set_property(GObject * object,
79                                                      guint prop_id,
80                                                      const GValue * value, 
81                                                      GParamSpec * pspec);
82 static void hildon_name_password_dialog_get_property(GObject * object,
83                                                      guint prop_id,
84                                                      GValue * value, 
85                                                      GParamSpec * pspec);
86 static void
87 hildon_name_password_dialog_set_property(GObject * object,
88                                          guint prop_id,
89                                          const GValue * value, GParamSpec * pspec)
90 {
91     HildonNamePasswordDialog *dialog = HILDON_NAME_PASSWORD_DIALOG(object);
92     HildonNamePasswordDialogPrivate *priv;
93
94     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
95     
96     switch (prop_id) {
97     case PROP_CONTENT:
98         gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
99         break;
100     case PROP_NAME:
101       gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child
102                                    (GTK_BIN(
103                                             priv->nameEntry))), 
104                          g_value_get_string(value));
105       break;
106     case PROP_PASSWORD:
107       gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child
108                                    (GTK_BIN(
109                                             priv->passwordEntry))), 
110                          g_value_get_string(value));
111         break;
112     default:
113         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
114         break;
115     }
116 }
117
118 static void
119 hildon_name_password_dialog_get_property(GObject * object,
120                                          guint prop_id,
121                                          GValue * value, GParamSpec * pspec)
122 {
123     HildonNamePasswordDialog *dialog = HILDON_NAME_PASSWORD_DIALOG(object);
124     HildonNamePasswordDialogPrivate *priv;
125     const gchar *string;
126
127     priv = HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
128     
129     switch (prop_id) {
130     case PROP_CONTENT:
131       string = gtk_label_get_text(priv->domainLabel);
132       g_value_set_string(value, string);
133       break;
134     case PROP_NAME:
135       string = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child
136                                             (GTK_BIN(
137                                                      priv->nameEntry))));
138
139       g_value_set_string(value, string);
140       break;
141     case PROP_PASSWORD:
142      string = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child
143                                             (GTK_BIN(
144                                                      priv->passwordEntry))));
145         g_value_set_string(value, string);
146         break;
147     default:
148         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
149         break;
150     }
151 }
152 static void
153 hildon_name_password_dialog_class_init(HildonNamePasswordDialogClass *class)
154 {
155     GObjectClass *object_class = G_OBJECT_CLASS(class);
156     
157     parent_class = g_type_class_peek_parent(class);
158     
159     object_class->set_property = hildon_name_password_dialog_set_property;
160     object_class->get_property = hildon_name_password_dialog_get_property;
161     
162     /* properties */
163     g_object_class_install_property(object_class, 
164                     PROP_CONTENT, 
165                     g_param_spec_string ("content",
166                             "Content",
167                             "Set content for content label.",
168                             NULL,
169                             G_PARAM_READWRITE));
170
171     g_object_class_install_property(object_class,
172                     PROP_NAME, 
173                     g_param_spec_string ("name",
174                             "Name",
175                             "Set content for name entry.",
176                             "DEFAULT",
177                             G_PARAM_READWRITE));
178     
179     g_object_class_install_property(object_class, 
180                     PROP_PASSWORD,
181                     g_param_spec_string ("password",
182                             "Password",
183                             "Set content for password entry",
184                             "DEFAULT",
185                             G_PARAM_READWRITE));
186
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     HildonNamePasswordDialogPrivate *priv =
195         HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
196     GtkSizeGroup *group =
197         GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
198     GtkWidget *control;
199     
200     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
201     gtk_window_set_title(GTK_WINDOW(dialog), _(HILDON_NAME_PASSWORD_DIALOG_TITLE));
202     
203     priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
204     
205     priv->okButton =
206       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
207                                        _(HILDON_NAME_PASSWORD_DIALOG_OK),
208                                        GTK_RESPONSE_OK));
209     priv->closeButton =
210       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
211                                        _(HILDON_NAME_PASSWORD_DIALOG_CANCEL),
212                                        GTK_RESPONSE_CANCEL));
213
214     control = gtk_entry_new();
215     priv->nameEntry = HILDON_CAPTION(hildon_caption_new
216                                      (group,
217                                       _(HILDON_NAME_PASSWORD_DIALOG_NAME ),
218                                       control, NULL,
219                                       HILDON_CAPTION_OPTIONAL));
220     hildon_caption_set_separator(priv->nameEntry, "");
221
222     control = gtk_entry_new();
223     gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
224     priv->passwordEntry =
225       HILDON_CAPTION(hildon_caption_new(group,
226                                         _(HILDON_NAME_PASSWORD_DIALOG_PASSWORD),
227                                         control,
228                                         NULL,
229                                         HILDON_CAPTION_OPTIONAL));
230     hildon_caption_set_separator(priv->passwordEntry, "");
231
232     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
233           GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
234     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), 
235                        GTK_WIDGET(priv->nameEntry), FALSE, FALSE, 0);
236     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
237                        GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
238
239     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
240     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
241     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
242     
243 }
244 GType hildon_name_password_dialog_get_type(void)
245 {
246     static GType dialog_type = 0;
247
248     if (!dialog_type) {
249         static const GTypeInfo dialog_info = {
250             sizeof(HildonNamePasswordDialogClass),
251             NULL,       /* base_init */
252             NULL,       /* base_finalize */
253             (GClassInitFunc) hildon_name_password_dialog_class_init,
254             NULL,       /* class_finalize */
255             NULL,       /* class_data */
256             sizeof(HildonNamePasswordDialog),
257             0,  /* n_preallocs */
258             (GInstanceInitFunc) hildon_name_password_dialog_init
259         };
260         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
261                                              "HildonNamePasswordDialog",
262                                              &dialog_info, 0);
263     }
264
265     return dialog_type;
266 }
267
268 /**
269  * hildon_name_password_dialog_new:
270  * @parent: the parent window of the dialog.
271  *
272  * Creates a new #HildonNamePasswordDialog widget with Ok and Close
273  * buttons.
274  *
275  * Return value: the newly created #HildonNamePasswordDialog
276  */
277 GtkWidget *hildon_name_password_dialog_new(GtkWindow * parent)
278 {
279     GtkWidget *self = g_object_new(HILDON_TYPE_NAME_PASSWORD_DIALOG,NULL);
280
281     if (parent)
282         gtk_window_set_transient_for(GTK_WINDOW(self), parent);
283
284     return self;
285 }
286
287 /**
288  * hildon_name_password_dialog_new_with_default:
289  * @parent: the parent window of the dialog.
290  * @name: default name, nul-terminated string, if you want
291  * to leave it unset, pass a NULL pointer.
292  * @password: default password, nul-terminated string, 
293  * if you want to leave it unset, pass a NULL pointer.
294  * 
295  * Same as #hildon_name_password_dialog_new, but with a 
296  * default name and password.
297  *
298  * Return value: the newly created #HildonNamePasswordDialog
299  */
300 GtkWidget *hildon_name_password_dialog_new_with_default
301                                           (GtkWindow * parent,
302                                            gchar *name,
303                                            gchar *password)
304 {
305     GtkWidget *self = hildon_name_password_dialog_new(parent);
306
307     if(name != NULL)
308       g_object_set(G_OBJECT(self), "name", name, NULL);
309     if(password != NULL)
310       g_object_set(G_OBJECT(self), "password", password, NULL);
311
312     return self;
313 }
314
315 /**
316  * hildon_name_password_dialog_get_name:
317  * @dialog: the dialog.
318  *
319  * Gets the text that's in the name entry.
320  *
321  * Return value: a pointer to the name string.
322  */
323 const gchar *hildon_name_password_dialog_get_name(HildonNamePasswordDialog
324                                                   * dialog)
325 {
326     HildonNamePasswordDialogPrivate *priv =
327         HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
328     return gtk_entry_get_text(GTK_ENTRY(
329                               (gtk_bin_get_child
330                                (GTK_BIN(
331                                 priv->nameEntry)))));
332 }
333
334 /**
335  * hildon_name_password_dialog_get_password:
336  * @dialog: the dialog.
337  * 
338  * Gets the text that's in the password entry.
339  * 
340  * Return value: a pointer to the password string.
341  */
342 const gchar *hildon_name_password_dialog_get_password(HildonNamePasswordDialog
343                                                       * dialog)
344 {
345     HildonNamePasswordDialogPrivate *priv =
346         HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
347     return gtk_entry_get_text(GTK_ENTRY
348                               (gtk_bin_get_child
349                                (GTK_BIN(
350                                 (priv->passwordEntry)))));
351 }
352
353 /**
354  * hildon_name_password_dialog_set_domain(GtkWidget *dialog, 
355  * @dialog: the dialog
356  * @domain: the domain or some other descriptive text to be set.
357  * 
358  * sets the optional descriptive text
359  */
360
361 void hildon_name_password_dialog_set_domain(HildonNamePasswordDialog *dialog, 
362                                                 gchar *domain)
363 {
364   HildonNamePasswordDialogPrivate *priv =
365     HILDON_NAME_PASSWORD_DIALOG_GET_PRIVATE(dialog);
366   gtk_label_set_text(priv->domainLabel, domain);
367   
368 }