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