r5987: Add credentials callback for gtk+. The gtk+ apps now no longer
[samba.git] / source4 / gtk / common / credentials.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Jelmer Vernooij 2005
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "gtk/common/gtk-smb.h"
23
24 static const char *gtk_get_userpassword(struct cli_credentials *credentials)
25 {
26         char *prompt;
27         const char *ret;
28         GtkWidget *dialog;
29         GtkWidget *dialog_vbox1;
30         GtkWidget *hbox;
31         GtkWidget *label;
32         GtkWidget *entry_password;
33         GtkWidget *dialog_action_area1;
34         GtkWidget *cancelbutton1;
35         GtkWidget *okbutton1;
36
37         dialog = gtk_dialog_new ();
38         gtk_window_set_title (GTK_WINDOW (dialog), "Enter Password");
39         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
40         gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
41
42         dialog_vbox1 = GTK_DIALOG (dialog)->vbox;
43
44         hbox = gtk_hbox_new (FALSE, 0);
45         gtk_box_pack_start (GTK_BOX (dialog_vbox1), hbox, TRUE, TRUE, 0);
46
47         prompt = talloc_asprintf(NULL, "Password for [%s\\%s]:", 
48                                                          cli_credentials_get_domain(credentials),
49                                                          cli_credentials_get_username(credentials));
50
51         label = gtk_label_new (prompt);
52
53         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
54
55         entry_password = gtk_entry_new ();
56         gtk_box_pack_start (GTK_BOX (hbox), entry_password, TRUE, TRUE, 0);
57         gtk_entry_set_visibility (GTK_ENTRY (entry_password), FALSE);
58         gtk_entry_set_activates_default (GTK_ENTRY (entry_password), TRUE);
59
60         dialog_action_area1 = GTK_DIALOG (dialog)->action_area;
61         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
62
63         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
64         gtk_dialog_add_action_widget (GTK_DIALOG (dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
65         GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
66
67         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
68         gtk_dialog_add_action_widget (GTK_DIALOG (dialog), okbutton1, GTK_RESPONSE_OK);
69         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
70
71         gtk_widget_show_all (dialog);
72
73     switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
74         case GTK_RESPONSE_OK:
75                 ret = talloc_strdup(credentials, gtk_entry_get_text(GTK_ENTRY(entry_password)));
76                 break;
77         default:
78                 ret = NULL;
79                 break;
80         }
81
82         gtk_widget_destroy (dialog);
83
84         talloc_free(prompt);
85         
86         return ret;
87 }
88
89 void cli_credentials_set_gtk_callbacks(struct cli_credentials *cred)
90 {
91         if (cred->password_obtained <= CRED_CALLBACK) {
92                 cred->password_cb = gtk_get_userpassword;
93                 cred->password_obtained = CRED_CALLBACK;
94         }
95 }