r9772: Make credentials callbacks more consistant with the abstraction
[samba.git] / source / gtk / common / gtk-smb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB-related GTK+ functions
4    
5    Copyright (C) Jelmer Vernooij 2004-2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "gtk/common/gtk-smb.h"
24 #include "gtk/common/select.h"
25
26 void gtk_show_werror(GtkWidget *win, const char *message, WERROR err) 
27 {
28         GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(win), 
29                                                     GTK_DIALOG_DESTROY_WITH_PARENT,
30                                                     GTK_MESSAGE_ERROR,
31                                                     GTK_BUTTONS_CLOSE,
32                                                     "%s: %s\n", message?message: "Windows error",
33                                                     win_errstr(err));
34         gtk_dialog_run (GTK_DIALOG (dialog));
35         gtk_widget_destroy (dialog);
36 }
37                    
38 void gtk_show_ntstatus(GtkWidget *win, const char *message, NTSTATUS status) 
39 {
40         GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(win), 
41                                                     GTK_DIALOG_DESTROY_WITH_PARENT,
42                                                     GTK_MESSAGE_ERROR,
43                                                     GTK_BUTTONS_CLOSE,
44                                                     "%s: %s\n", message?message:"Windows error",
45                                                     nt_errstr(status));
46         gtk_dialog_run (GTK_DIALOG (dialog));
47         gtk_widget_destroy (dialog);
48 }
49
50 static void on_browse_activate  (GtkButton     *button,  gpointer         user_data)
51 {
52         GtkRpcBindingDialog *rbd = user_data;
53         GtkWidget *shd = gtk_select_host_dialog_new(rbd->sam_pipe);
54         if(gtk_dialog_run(GTK_DIALOG(shd)) == GTK_RESPONSE_ACCEPT) {
55                 gtk_entry_set_text(GTK_ENTRY(rbd->entry_host), gtk_select_host_dialog_get_host(GTK_SELECT_HOST_DIALOG(shd)));
56         }
57         
58         gtk_widget_destroy(GTK_WIDGET(shd));
59 }
60
61 static void on_ncalrpc_toggled(GtkToggleButton *tb, GtkRpcBindingDialog *d)
62 {
63         gtk_widget_set_sensitive(d->frame_host, !gtk_toggle_button_get_active(tb));
64 }
65
66 static void gtk_rpc_binding_dialog_init (GtkRpcBindingDialog *gtk_rpc_binding_dialog)
67 {
68         GtkWidget *dialog_vbox1;
69         GtkWidget *vbox1;
70         GtkWidget *vbox6;
71         GtkWidget *frame_transport;
72         GtkWidget *label1;
73         GtkWidget *hbox1;
74         GtkWidget *lbl_name;
75         GtkWidget *label2;
76         GtkWidget *label3;
77         GtkWidget *frame_security;
78         GtkWidget *vbox2;
79         GtkWidget *btn_browse;
80         GtkWidget *dialog_action_area1;
81         GtkWidget *btn_cancel;
82         GtkWidget *btn_connect;
83         GSList *transport_smb_group = NULL;
84
85         gtk_rpc_binding_dialog->mem_ctx = talloc_init("gtk_rcp_binding_dialog");
86
87         gtk_window_set_title (GTK_WINDOW (gtk_rpc_binding_dialog), "Connect");
88
89         dialog_vbox1 = GTK_DIALOG (gtk_rpc_binding_dialog)->vbox;
90
91         vbox1 = gtk_vbox_new (FALSE, 0);
92         gtk_box_pack_start (GTK_BOX (dialog_vbox1), vbox1, TRUE, TRUE, 0);
93
94         frame_transport = gtk_frame_new (NULL);
95         gtk_box_pack_start (GTK_BOX (vbox1), frame_transport, TRUE, TRUE, 0);
96
97         vbox6 = gtk_vbox_new (FALSE, 0);
98         gtk_container_add (GTK_CONTAINER (frame_transport), vbox6);
99
100         gtk_rpc_binding_dialog->transport_ncalrpc = gtk_radio_button_new_with_mnemonic (NULL, "Local Host");
101         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_ncalrpc, FALSE, FALSE, 0);
102         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_ncalrpc), transport_smb_group);
103         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_ncalrpc));
104
105
106         gtk_rpc_binding_dialog->transport_smb = gtk_radio_button_new_with_mnemonic (NULL, "RPC over SMB over TCP/IP");
107         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_smb, FALSE, FALSE, 0);
108         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_smb), transport_smb_group);
109         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_smb));
110         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_rpc_binding_dialog->transport_smb), TRUE);
111
112         gtk_rpc_binding_dialog->transport_tcp_ip = gtk_radio_button_new_with_mnemonic (NULL, "RPC over TCP/IP");
113         gtk_box_pack_start (GTK_BOX (vbox6), gtk_rpc_binding_dialog->transport_tcp_ip, FALSE, FALSE, 0);
114         gtk_radio_button_set_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_tcp_ip), transport_smb_group);
115         transport_smb_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (gtk_rpc_binding_dialog->transport_tcp_ip));
116
117
118
119         label1 = gtk_label_new ("Transport");
120         gtk_frame_set_label_widget (GTK_FRAME (frame_transport), label1);
121
122         gtk_rpc_binding_dialog->frame_host = gtk_frame_new (NULL);
123         gtk_box_pack_start (GTK_BOX (vbox1), gtk_rpc_binding_dialog->frame_host, TRUE, TRUE, 0);
124
125         hbox1 = gtk_hbox_new (FALSE, 0);
126         gtk_container_add (GTK_CONTAINER (gtk_rpc_binding_dialog->frame_host), hbox1);
127
128         lbl_name = gtk_label_new ("Name");
129         gtk_box_pack_start (GTK_BOX (hbox1), lbl_name, TRUE, TRUE, 0);
130
131         gtk_rpc_binding_dialog->entry_host = gtk_entry_new ();
132         gtk_box_pack_start (GTK_BOX (hbox1), gtk_rpc_binding_dialog->entry_host, TRUE, TRUE, 0);
133
134         if(gtk_rpc_binding_dialog->sam_pipe)
135         {
136                 btn_browse = gtk_button_new_with_label ("Browse");
137                 gtk_box_pack_start (GTK_BOX (hbox1), btn_browse, TRUE, TRUE, 0);
138
139                 g_signal_connect ((gpointer) btn_browse, "pressed",
140                                                   G_CALLBACK (on_browse_activate),
141                                                   gtk_rpc_binding_dialog);
142         }
143
144         label2 = gtk_label_new ("Host");
145         gtk_frame_set_label_widget (GTK_FRAME (gtk_rpc_binding_dialog->frame_host), label2);
146
147         frame_security = gtk_frame_new (NULL);
148
149         label3 = gtk_label_new ("Security");
150         gtk_frame_set_label_widget (GTK_FRAME (frame_security), label3);
151
152         gtk_box_pack_start (GTK_BOX (vbox1), frame_security, TRUE, TRUE, 0);
153
154         vbox2 = gtk_vbox_new (FALSE, 0);
155         gtk_container_add (GTK_CONTAINER (frame_security), vbox2);
156
157         gtk_rpc_binding_dialog->chk_sign = gtk_check_button_new_with_mnemonic ("S_ign");
158         gtk_box_pack_start (GTK_BOX (vbox2), gtk_rpc_binding_dialog->chk_sign, FALSE, FALSE, 0);
159
160         gtk_rpc_binding_dialog->chk_seal = gtk_check_button_new_with_mnemonic ("_Seal");
161         gtk_box_pack_start (GTK_BOX (vbox2), gtk_rpc_binding_dialog->chk_seal, FALSE, FALSE, 0);
162
163         dialog_action_area1 = GTK_DIALOG (gtk_rpc_binding_dialog)->action_area;
164         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
165
166         btn_cancel = gtk_button_new_from_stock ("gtk-cancel");
167         gtk_dialog_add_action_widget (GTK_DIALOG (gtk_rpc_binding_dialog), btn_cancel, GTK_RESPONSE_CANCEL);
168         GTK_WIDGET_SET_FLAGS (btn_cancel, GTK_CAN_DEFAULT);
169
170         btn_connect = gtk_button_new_with_mnemonic ("_Connect");
171         gtk_dialog_add_action_widget (GTK_DIALOG (gtk_rpc_binding_dialog), btn_connect, GTK_RESPONSE_ACCEPT);
172         gtk_container_set_border_width (GTK_CONTAINER (btn_connect), 1);
173         GTK_WIDGET_SET_FLAGS (btn_connect, GTK_CAN_DEFAULT);
174
175         g_signal_connect ((gpointer) gtk_rpc_binding_dialog->transport_ncalrpc, "toggled",
176                                                   G_CALLBACK (on_ncalrpc_toggled),
177                                                   gtk_rpc_binding_dialog);
178
179
180         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gtk_rpc_binding_dialog->transport_ncalrpc), TRUE);
181         gtk_widget_show_all(dialog_vbox1);
182
183         gtk_widget_grab_focus (btn_connect);
184         gtk_widget_grab_default (btn_connect);
185 }
186
187 GType gtk_rpc_binding_dialog_get_type (void)
188 {
189   static GType mytype = 0;
190
191   if (!mytype)
192     {
193       static const GTypeInfo myinfo =
194       {
195         sizeof (GtkRpcBindingDialogClass),
196         NULL,
197         NULL,
198         NULL,
199         NULL,
200         NULL,
201         sizeof(GtkRpcBindingDialog),
202         0,
203         (GInstanceInitFunc) gtk_rpc_binding_dialog_init,
204       };
205
206       mytype = g_type_register_static (GTK_TYPE_DIALOG, 
207                 "GtkRpcBindingDialog", &myinfo, 0);
208     }
209
210   return mytype;
211 }
212
213 GtkWidget *gtk_rpc_binding_dialog_new (struct dcerpc_pipe *sam_pipe)
214 {
215         GtkRpcBindingDialog *d = GTK_RPC_BINDING_DIALOG ( g_object_new (gtk_rpc_binding_dialog_get_type (), NULL));
216         d->sam_pipe = sam_pipe;
217         return GTK_WIDGET(d);
218 }
219
220 const char *gtk_rpc_binding_dialog_get_host(GtkRpcBindingDialog *d)
221 {
222         return gtk_entry_get_text(GTK_ENTRY(d->entry_host));
223 }
224
225 struct dcerpc_binding *gtk_rpc_binding_dialog_get_binding(GtkRpcBindingDialog *d, TALLOC_CTX *mem_ctx)
226 {
227         struct dcerpc_binding *binding = talloc(mem_ctx, struct dcerpc_binding);
228
229         ZERO_STRUCT(binding->object);
230
231         /* Format: TRANSPORT:host[\pipe\foo,foo,foo] */
232
233         binding->host = talloc_strdup(mem_ctx, gtk_entry_get_text(GTK_ENTRY(d->entry_host)));
234         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->transport_tcp_ip))) {
235                 binding->transport = NCACN_IP_TCP;
236         } else if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->transport_ncalrpc))) {
237                 binding->transport = NCALRPC;
238                 binding->host = NULL;
239         } else {
240                 binding->transport = NCACN_NP;
241         }
242
243         binding->options = NULL;
244         binding->flags = 0;
245         binding->endpoint = NULL;
246
247         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->chk_seal))) {
248                 binding->flags |= DCERPC_SEAL;
249         }
250
251         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->chk_sign))) {
252                 binding->flags |= DCERPC_SIGN;
253         }
254
255         return binding;
256 }
257
258 const char *gtk_rpc_binding_dialog_get_binding_string(GtkRpcBindingDialog *d, TALLOC_CTX *mem_ctx)
259 {
260         return dcerpc_binding_string(mem_ctx, gtk_rpc_binding_dialog_get_binding(d, mem_ctx));
261 }
262
263 GtkWidget *create_gtk_samba_about_dialog (const char *appname)
264 {
265         GtkWidget *samba_about_dialog;
266         GtkWidget *dialog_vbox1;
267         GtkWidget *label1;
268         GtkWidget *label2;
269         GtkWidget *label3;
270         GtkWidget *label4;
271         GtkWidget *dialog_action_area1;
272         GtkWidget *okbutton1;
273
274         samba_about_dialog = gtk_dialog_new ();
275         gtk_window_set_title (GTK_WINDOW (samba_about_dialog), "About");
276
277         dialog_vbox1 = GTK_DIALOG (samba_about_dialog)->vbox;
278
279         /* FIXME image1 = create_pixmap (samba_about_dialog, "slmed.png");
280            gtk_box_pack_start (GTK_BOX (dialog_vbox1), image1, TRUE, TRUE, 0);*/
281
282         label1 = gtk_label_new (appname);
283         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label1, FALSE, FALSE, 0);
284
285         label2 = gtk_label_new (samba_version_string());
286         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label2, FALSE, FALSE, 0);
287
288         label3 = gtk_label_new_with_mnemonic ("Part of Samba <http://www.samba.org/>");
289         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label3, FALSE, FALSE, 0);
290
291         label4 = gtk_label_new ("\302\251 1992-2005 The Samba Team");
292         gtk_box_pack_start (GTK_BOX (dialog_vbox1), label4, FALSE, FALSE, 0);
293
294         dialog_action_area1 = GTK_DIALOG (samba_about_dialog)->action_area;
295         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
296
297         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
298         gtk_dialog_add_action_widget (GTK_DIALOG (samba_about_dialog), okbutton1, GTK_RESPONSE_OK);
299         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
300         gtk_widget_show_all(dialog_vbox1);
301
302         return samba_about_dialog;
303 }