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