r5987: Add credentials callback for gtk+. The gtk+ apps now no longer
[samba.git] / source4 / gtk / common / select.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB-related GTK+ functions
4    
5    Copyright (C) Jelmer Vernooij 2004
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 "librpc/gen_ndr/ndr_samr.h"
24 #include "gtk/common/select.h"
25 #include "gtk/common/gtk-smb.h"
26
27 /* GtkSelectDomainDialog */
28
29 const char *gtk_select_domain_dialog_get_domain(GtkSelectDomainDialog *d)
30 {
31         return gtk_entry_get_text(GTK_ENTRY(d->entry_domain));
32 }
33
34 void gtk_select_domain_dialog_init (GtkSelectDomainDialog *select_domain_dialog)
35 {
36         GtkWidget *dialog_vbox1;
37         GtkWidget *hbox1;
38         GtkWidget *label1;
39         GtkWidget *scrolledwindow1;
40         GtkWidget *dialog_action_area1;
41         GtkWidget *cancelbutton1;
42         GtkWidget *okbutton1;
43         GtkCellRenderer *renderer;
44         GtkTreeViewColumn *curcol;
45
46         gtk_window_set_title (GTK_WINDOW (select_domain_dialog), "Select Domain");
47
48         dialog_vbox1 = GTK_DIALOG (select_domain_dialog)->vbox;
49
50         hbox1 = gtk_hbox_new (FALSE, 0);
51         gtk_box_pack_start (GTK_BOX (dialog_vbox1), hbox1, TRUE, TRUE, 0);
52
53         label1 = gtk_label_new ("Domain:");
54         gtk_box_pack_start (GTK_BOX (hbox1), label1, FALSE, FALSE, 0);
55
56         select_domain_dialog->entry_domain = gtk_entry_new ();
57         gtk_box_pack_start (GTK_BOX (hbox1), select_domain_dialog->entry_domain, TRUE, TRUE, 0);
58
59         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
60         gtk_box_pack_start (GTK_BOX (dialog_vbox1), scrolledwindow1, TRUE, TRUE, 0);
61
62         select_domain_dialog->list_domains = gtk_tree_view_new ();
63         gtk_container_add (GTK_CONTAINER (scrolledwindow1), select_domain_dialog->list_domains);
64
65         curcol = gtk_tree_view_column_new ();
66         gtk_tree_view_column_set_title(curcol, "Name");
67         renderer = gtk_cell_renderer_text_new();
68         gtk_tree_view_column_pack_start(curcol, renderer, True);
69         gtk_tree_view_append_column(GTK_TREE_VIEW(select_domain_dialog->list_domains), curcol);
70         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
71
72         select_domain_dialog->store_domains = gtk_list_store_new(1, G_TYPE_STRING);
73         gtk_tree_view_set_model(GTK_TREE_VIEW(select_domain_dialog->list_domains), GTK_TREE_MODEL(select_domain_dialog->store_domains));
74         g_object_unref(select_domain_dialog->store_domains);
75
76         dialog_action_area1 = GTK_DIALOG (select_domain_dialog)->action_area;
77         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
78
79         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
80         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
81         GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
82
83         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
84         gtk_dialog_add_action_widget (GTK_DIALOG (select_domain_dialog), okbutton1, GTK_RESPONSE_OK);
85         gtk_widget_show_all(dialog_vbox1);
86         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
87 }
88
89 struct policy_handle gtk_select_domain_dialog_get_handle(GtkSelectDomainDialog *d)
90 {
91         struct policy_handle h;
92         
93         
94         /* FIXME */
95         return h;
96 }
97
98 GType gtk_select_domain_dialog_get_type (void)
99 {
100         static GType mytype = 0;
101
102         if (!mytype)
103         {
104                 static const GTypeInfo myinfo =
105                 {
106                         sizeof (GtkSelectDomainDialogClass),
107                         NULL,
108                         NULL,
109                         NULL,
110                         NULL,
111                         NULL,
112                         sizeof(GtkSelectDomainDialog),
113                         0,
114                         (GInstanceInitFunc) gtk_select_domain_dialog_init,
115                 };
116
117                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
118                                                                                  "GtkSelectDomainDialog", &myinfo, 0);
119         }
120
121         return mytype;
122 }
123                                                                                                                              
124 GtkWidget *gtk_select_domain_dialog_new (struct dcerpc_pipe *sam_pipe)
125 {
126         GtkSelectDomainDialog *d = g_object_new(gtk_select_domain_dialog_get_type (), NULL);
127         NTSTATUS status;
128         struct samr_EnumDomains r;
129         struct samr_Connect cr;
130         struct samr_Close dr;
131         struct policy_handle handle;
132         uint32_t resume_handle = 0;
133         int i;
134         TALLOC_CTX *mem_ctx = talloc_init("gtk_select_domain_dialog_new");
135
136         d->sam_pipe = sam_pipe;
137
138         cr.in.system_name = 0;
139         cr.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
140         cr.out.connect_handle = &handle;
141
142         status = dcerpc_samr_Connect(sam_pipe, mem_ctx, &cr);
143         if (!NT_STATUS_IS_OK(status)) {
144                 gtk_show_ntstatus(NULL, "Running Connect on SAMR", status);
145                 talloc_free(mem_ctx);
146                 return GTK_WIDGET(d);
147         }
148
149         r.in.connect_handle = &handle;
150         r.in.resume_handle = &resume_handle;
151         r.in.buf_size = (uint32_t)-1;
152         r.out.resume_handle = &resume_handle;
153
154         status = dcerpc_samr_EnumDomains(sam_pipe, mem_ctx, &r);
155         if (!NT_STATUS_IS_OK(status)) {
156                 gtk_show_ntstatus(NULL, "Enumerating domains", status);
157         } else if (r.out.sam) {
158                 for (i=0;i<r.out.sam->count;i++) {
159                         GtkTreeIter iter;
160                         gtk_list_store_append(d->store_domains, &iter);
161                         gtk_list_store_set (d->store_domains, &iter, 0, r.out.sam->entries[i].name.string, -1);
162                 }
163         }
164
165         dr.in.handle = &handle;
166         dr.out.handle = &handle;
167
168         status = dcerpc_samr_Close(sam_pipe, mem_ctx, &dr);
169         if (!NT_STATUS_IS_OK(status)) {
170                 gtk_show_ntstatus(NULL, "Closing SAMR connection", status);
171                 talloc_free(mem_ctx);
172                 return GTK_WIDGET ( d );
173         }
174
175         talloc_free(mem_ctx);
176
177         return GTK_WIDGET ( d );
178 }
179
180
181 /* GtkSelectHostDialog */
182 const char *gtk_select_host_dialog_get_host (GtkSelectHostDialog *d)
183 {
184         return gtk_entry_get_text(GTK_ENTRY(d->entry_host));
185 }
186
187 void gtk_select_host_dialog_init (GtkSelectHostDialog *select_host_dialog)
188 {
189         GtkWidget *dialog_vbox2;
190         GtkWidget *hbox2;
191         GtkWidget *label2;
192         GtkWidget *scrolledwindow2;
193         GtkWidget *dialog_action_area2;
194         GtkWidget *cancelbutton2;
195         GtkWidget *okbutton2;
196
197         gtk_window_set_title (GTK_WINDOW (select_host_dialog), "Select Host");
198
199         dialog_vbox2 = GTK_DIALOG (select_host_dialog)->vbox;
200
201         hbox2 = gtk_hbox_new (FALSE, 0);
202         gtk_box_pack_start (GTK_BOX (dialog_vbox2), hbox2, TRUE, TRUE, 0);
203
204         label2 = gtk_label_new ("Host");
205         gtk_box_pack_start (GTK_BOX (hbox2), label2, FALSE, FALSE, 0);
206
207         select_host_dialog->entry_host = gtk_entry_new ();
208         gtk_box_pack_start (GTK_BOX (hbox2), select_host_dialog->entry_host, TRUE, TRUE, 0);
209
210         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
211         gtk_box_pack_start (GTK_BOX (dialog_vbox2), scrolledwindow2, TRUE, TRUE, 0);
212
213         select_host_dialog->tree_host = gtk_tree_view_new ();
214         gtk_container_add (GTK_CONTAINER (scrolledwindow2), select_host_dialog->tree_host);
215
216         select_host_dialog->store_host = gtk_tree_store_new(1, G_TYPE_STRING);
217         gtk_tree_view_set_model(GTK_TREE_VIEW(select_host_dialog->tree_host), GTK_TREE_MODEL(select_host_dialog->store_host));
218         g_object_unref(select_host_dialog->store_host); 
219
220         dialog_action_area2 = GTK_DIALOG (select_host_dialog)->action_area;
221         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area2), GTK_BUTTONBOX_END);
222
223         cancelbutton2 = gtk_button_new_from_stock ("gtk-cancel");
224         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), cancelbutton2, GTK_RESPONSE_CANCEL);
225         GTK_WIDGET_SET_FLAGS (cancelbutton2, GTK_CAN_DEFAULT);
226
227         okbutton2 = gtk_button_new_from_stock ("gtk-ok");
228         gtk_widget_show_all (dialog_vbox2);
229         gtk_dialog_add_action_widget (GTK_DIALOG (select_host_dialog), okbutton2, GTK_RESPONSE_OK);
230         GTK_WIDGET_SET_FLAGS (okbutton2, GTK_CAN_DEFAULT);
231 }
232
233 GType gtk_select_host_dialog_get_type (void)
234 {
235         static GType mytype = 0;
236
237         if (!mytype)
238         {
239                 static const GTypeInfo myinfo =
240                 {
241                         sizeof (GtkSelectHostDialogClass),
242                         NULL,
243                         NULL,
244                         NULL,
245                         NULL,
246                         NULL,
247                         sizeof(GtkSelectHostDialog),
248                         0,
249                         (GInstanceInitFunc) gtk_select_host_dialog_init,
250                 };
251
252                 mytype = g_type_register_static (GTK_TYPE_DIALOG,
253                                                                                  "GtkSelectHostDialog", &myinfo, 0);
254         }
255
256         return mytype;
257 }
258                                                                                                                              
259 GtkWidget *gtk_select_host_dialog_new (struct dcerpc_pipe *sam_pipe, BOOL nocredentials)
260 {
261         return GTK_WIDGET ( g_object_new (gtk_select_host_dialog_get_type (), NULL ));
262 }