97e71c329c1e19c23c76e67deaa6bd36e8700e4f
[metze/samba/wip.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 #include "auth/credentials/credentials.h"
24
25 static void gtk_get_credentials(struct cli_credentials *credentials)
26 {
27         const char *ret;
28         GtkWidget *dialog;
29         GtkWidget *label;
30         GtkWidget *table;
31         GtkWidget *entry_username;
32         GtkWidget *entry_password;
33         GtkWidget *dialog_action_area1;
34         GtkWidget *cancelbutton1;
35         GtkWidget *okbutton1;
36         GtkWidget *anonymous;
37         const char *username;
38
39         dialog = gtk_dialog_new ();
40         gtk_window_set_title (GTK_WINDOW (dialog), "Credentials");
41         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
42         gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
43
44         table = gtk_table_new(4, 2, FALSE);
45         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), table);
46
47         label = gtk_label_new ("Username:");
48
49         gtk_table_attach(GTK_TABLE(table),label,0,1,1,2,GTK_FILL,0,0,0);
50
51         entry_username = gtk_entry_new ();
52         gtk_table_attach(GTK_TABLE(table),entry_username,1,2,1,2,GTK_FILL,0,0,0);
53         gtk_entry_set_activates_default (GTK_ENTRY (entry_username), TRUE);
54
55         username = cli_credentials_get_unparsed_name(credentials, credentials);
56
57         if (credentials->username_obtained != CRED_UNINITIALISED && 
58             username) {
59                 gtk_entry_set_text(GTK_ENTRY(entry_username), username);
60         }
61
62         label = gtk_label_new ("Password:");
63
64         gtk_table_attach(GTK_TABLE(table),label,0,1,3,4,GTK_FILL,0,0,0);
65
66         entry_password = gtk_entry_new ();
67         gtk_table_attach(GTK_TABLE(table),entry_password,1,2,3,4,GTK_FILL,0,0,0);
68         gtk_entry_set_visibility (GTK_ENTRY (entry_password), FALSE);
69         gtk_entry_set_activates_default (GTK_ENTRY (entry_password), TRUE);
70         if (credentials->password_obtained != CRED_UNINITIALISED &&
71             credentials->password_obtained != CRED_CALLBACK &&
72             credentials->password) {
73                 gtk_entry_set_text(GTK_ENTRY(entry_password), credentials->password);
74         }
75
76         anonymous = gtk_check_button_new_with_mnemonic("_Anonymous");
77         gtk_table_attach(GTK_TABLE(table),anonymous,0,2,4,5,GTK_FILL,0,0,0);
78
79         dialog_action_area1 = GTK_DIALOG (dialog)->action_area;
80         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
81
82         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
83         gtk_dialog_add_action_widget (GTK_DIALOG (dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
84         GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
85
86         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
87         gtk_dialog_add_action_widget (GTK_DIALOG (dialog), okbutton1, GTK_RESPONSE_OK);
88         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
89
90         gtk_widget_show_all (dialog);
91
92     switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
93         case GTK_RESPONSE_OK:
94                 cli_credentials_parse_string(credentials, gtk_entry_get_text(GTK_ENTRY(entry_username)), CRED_CALLBACK_RESULT);
95                 cli_credentials_set_password(credentials, gtk_entry_get_text(GTK_ENTRY(entry_password)), CRED_CALLBACK_RESULT);
96
97                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(anonymous))) {
98                         cli_credentials_set_anonymous(credentials);
99                 }
100                 break;
101         default:
102                 ret = NULL;
103                 break;
104         }
105
106         gtk_widget_destroy (dialog);
107 }
108
109 static const char *gtk_get_username(struct cli_credentials *credentials)
110 {
111         gtk_get_credentials(credentials);
112         return credentials->username;
113 }
114
115 static const char *gtk_get_userpassword(struct cli_credentials *credentials)
116 {
117         gtk_get_credentials(credentials);
118         return credentials->password;
119 }
120
121 static const char *gtk_get_domain(struct cli_credentials *credentials)
122 {
123         gtk_get_credentials(credentials);
124         return credentials->domain;
125 }
126
127 void cli_credentials_set_gtk_callbacks(struct cli_credentials *cred)
128 {
129         cli_credentials_set_username_callback(cred, gtk_get_username);
130         cli_credentials_set_domain_callback(cred, gtk_get_domain);
131         cli_credentials_set_password_callback(cred, gtk_get_userpassword);
132 }