r6600: Rework of the GTK credentials system; the credentials information is
[samba.git] / source / gtk / tools / gwcrontab.c
1 /* 
2    Unix SMB/CIFS implementation.
3    GTK+ Windows crontab frontend
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 "dynconfig.h"
24 #include "librpc/gen_ndr/ndr_atsvc.h"
25 #include "gtk/common/select.h"
26 #include "gtk/common/gtk-smb.h"
27
28 struct dcerpc_pipe *at_pipe = NULL;
29 GtkWidget *mainwin;
30 GtkListStore *store_jobs;
31 GtkWidget *tasks;
32 GtkWidget *entry_cmd;
33 GtkWidget *entry_repeat_weekly;
34 GtkWidget *entry_repeat_monthly;
35 GtkWidget *delete;
36
37 void update_joblist(void)
38 {
39         TALLOC_CTX *mem_ctx = talloc_init("update_joblist");
40         NTSTATUS status;
41         struct atsvc_JobEnum r;
42         struct atsvc_enum_ctr ctr;
43         int i;
44         uint32_t resume_handle = 0;
45
46         gtk_list_store_clear(store_jobs);
47
48         ctr.entries_read = 0;
49         ctr.first_entry = NULL;
50         r.in.servername = dcerpc_server_name(at_pipe);
51         r.in.ctr = r.out.ctr = &ctr;
52         r.in.preferred_max_len = 0xffffffff;
53         r.in.resume_handle = r.out.resume_handle = &resume_handle;
54         
55         status = dcerpc_atsvc_JobEnum(at_pipe, mem_ctx, &r);
56         if(!NT_STATUS_IS_OK(status)) {
57                 gtk_show_ntstatus(mainwin, "Error while enumerating first job", status);
58                 return;
59         }
60
61         for (i = 0; r.out.ctr && i < r.out.ctr->entries_read; i++) {
62                 GtkTreeIter iter;
63                 gtk_list_store_append(store_jobs, &iter);
64                 gtk_list_store_set (store_jobs, &iter, 
65                         0, r.out.ctr->first_entry[i].flags,
66                         1, r.out.ctr->first_entry[i].job_id, 
67                         2, r.out.ctr->first_entry[i].days_of_week, /*FIXME: Nicer format */
68                         3, r.out.ctr->first_entry[i].job_time, /* FIXME: Nicer format */
69                         4, r.out.ctr->first_entry[i].command,
70                         -1);
71
72         }
73         talloc_free(mem_ctx);
74         gtk_widget_set_sensitive(tasks, TRUE);
75 }
76
77 static void on_job_select(GtkTreeSelection *sel,
78                                              gpointer data)
79 {
80         gtk_widget_set_sensitive(delete, gtk_tree_selection_get_selected(sel, NULL, NULL));
81 }
82
83
84 static void
85 on_connect_activate                    (GtkMenuItem     *menuitem,
86                                         gpointer         user_data)
87 {
88         GtkRpcBindingDialog *d;
89         NTSTATUS status;
90         struct cli_credentials *credentials;
91         gint result;
92         TALLOC_CTX *mem_ctx;
93
94         d = GTK_RPC_BINDING_DIALOG(gtk_rpc_binding_dialog_new(NULL));
95         result = gtk_dialog_run(GTK_DIALOG(d));
96         switch(result) {
97                 case GTK_RESPONSE_ACCEPT:
98                         break;
99                 default: 
100                 gtk_widget_destroy(GTK_WIDGET(d));
101                 return;
102         }
103
104         mem_ctx = talloc_init("gwcrontab_connect");
105         /* If connected, get list of jobs */
106
107         credentials = cli_credentials_init(mem_ctx);
108         cli_credentials_guess(credentials);
109         cli_credentials_set_gtk_callbacks(credentials);
110         
111         status = dcerpc_pipe_connect_b(mem_ctx, &at_pipe,
112                                        gtk_rpc_binding_dialog_get_binding(d, mem_ctx),
113                                        DCERPC_ATSVC_UUID,
114                                        DCERPC_ATSVC_VERSION,
115                                        credentials);
116
117         if(!NT_STATUS_IS_OK(status)) {
118                 gtk_show_ntstatus(mainwin, "Error while connecting to at service", status);
119                 at_pipe = NULL;
120                 gtk_widget_destroy(GTK_WIDGET(d));
121                 talloc_free(mem_ctx);
122                 return;
123         }
124         gtk_widget_destroy(GTK_WIDGET(d));
125
126         at_pipe = talloc_reference(talloc_autofree_context(), at_pipe);
127         talloc_free(mem_ctx);
128         update_joblist();
129 }
130
131 void
132 on_quit_activate                      (GtkMenuItem     *menuitem,
133                                                                            gpointer         user_data)
134 {
135         talloc_free(at_pipe);
136         gtk_main_quit();
137 }
138
139
140 static GtkWidget* create_new_job_dialog (void);
141
142 void
143 on_new_activate                        (GtkMenuItem     *menuitem,
144                                                                                 gpointer         user_data)
145 {
146         GtkWidget *d = create_new_job_dialog();
147         gint result = gtk_dialog_run(GTK_DIALOG(d));
148         struct atsvc_JobAdd r;
149         struct atsvc_JobInfo job;
150         NTSTATUS status;
151         TALLOC_CTX *mem_ctx;
152         switch(result) {
153                 case GTK_RESPONSE_OK:
154                         break;
155                 default:
156                         gtk_widget_destroy(d);
157                         return;
158         }
159         mem_ctx = talloc_init("add_job");
160
161         job.job_time = 0; /* FIXME */
162         job.days_of_month = 0; /* FIXME */
163         job.days_of_week = 0; /* FIXME */
164         job.flags = 0; /* FIXME */
165         job.command = gtk_entry_get_text(GTK_ENTRY(entry_cmd));
166         r.in.servername = dcerpc_server_name(at_pipe);
167         r.in.job_info = &job;
168
169         status = dcerpc_atsvc_JobAdd(at_pipe, mem_ctx, &r);
170         if(!NT_STATUS_IS_OK(status)) {
171                 talloc_free(mem_ctx);
172                 gtk_show_ntstatus(mainwin, "Error while adding job", status);
173                 return;
174         }
175         
176         talloc_free(mem_ctx);
177         gtk_widget_destroy(d);
178         
179         d = gtk_message_dialog_new (GTK_WINDOW(mainwin), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Job Id: %d", r.out.job_id);
180         gtk_dialog_run(GTK_DIALOG(d));
181         gtk_widget_destroy(d);
182         update_joblist();
183 }
184
185
186 void
187 on_delete_activate                     (GtkMenuItem     *menuitem,
188                                         gpointer         user_data)
189 {
190         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tasks));
191         GtkTreeModel *model = GTK_TREE_MODEL(store_jobs);
192         GtkTreeIter iter;
193         gint id;
194
195         if (gtk_tree_selection_get_selected (sel, &model, &iter))
196         {
197                 struct atsvc_JobDel r;
198                 TALLOC_CTX *mem_ctx;
199                 NTSTATUS status;
200                 gtk_tree_model_get (model, &iter, 1, &id, -1);
201
202                 r.in.servername = dcerpc_server_name(at_pipe);
203                 r.in.min_job_id = r.in.max_job_id = id;
204
205                 mem_ctx = talloc_init("del_job");
206                 status = dcerpc_atsvc_JobDel(at_pipe, mem_ctx, &r);
207                 talloc_free(mem_ctx);
208                 if(!NT_STATUS_IS_OK(status)) {
209                         gtk_show_ntstatus(mainwin, "Error deleting job", status);
210                         return;
211                 }
212
213                 update_joblist();
214         }
215 }
216
217
218 void
219 on_about_activate                     (GtkMenuItem     *menuitem,
220                                                                            gpointer         user_data)
221 {
222         GtkDialog *aboutwin = GTK_DIALOG(create_gtk_samba_about_dialog("gwcrontab"));
223         gtk_dialog_run(aboutwin);
224         gtk_widget_destroy(GTK_WIDGET(aboutwin));
225 }
226
227 GtkWidget*
228 create_mainwindow (void)
229 {
230         GtkWidget *mainwindow;
231         GtkWidget *vbox;
232         GtkWidget *menubar;
233         GtkWidget *menuitem4;
234         GtkWidget *menuitem4_menu;
235         GtkWidget *mnu_connect;
236         GtkWidget *separatormenuitem1;
237         GtkWidget *quit;
238         GtkWidget *task;
239         GtkWidget *task_menu;
240         GtkWidget *new;
241         GtkCellRenderer *renderer;
242         GtkTreeViewColumn *curcol;
243         GtkWidget *menuitem7;
244         GtkWidget *menuitem7_menu;
245         GtkWidget *about;
246         GtkWidget *scrolledwindow;
247         GtkWidget *statusbar;
248         GtkAccelGroup *accel_group;
249
250         accel_group = gtk_accel_group_new ();
251
252         mainwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
253         gtk_window_set_default_size (GTK_WINDOW (mainwindow), 642, 562);
254         gtk_window_set_title (GTK_WINDOW (mainwindow), "Task Scheduler");
255
256         vbox = gtk_vbox_new (FALSE, 0);
257         gtk_container_add (GTK_CONTAINER (mainwindow), vbox);
258
259         menubar = gtk_menu_bar_new ();
260         gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
261
262         menuitem4 = gtk_menu_item_new_with_mnemonic ("_File");
263         gtk_container_add (GTK_CONTAINER (menubar), menuitem4);
264
265         menuitem4_menu = gtk_menu_new ();
266         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);
267
268         mnu_connect = gtk_menu_item_new_with_mnemonic ("_Connect");
269         gtk_container_add (GTK_CONTAINER (menuitem4_menu), mnu_connect);
270         g_signal_connect ((gpointer) mnu_connect, "activate",
271           G_CALLBACK (on_connect_activate), NULL);
272
273         separatormenuitem1 = gtk_separator_menu_item_new ();
274         gtk_container_add (GTK_CONTAINER (menuitem4_menu), separatormenuitem1);
275         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
276
277         quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
278         gtk_container_add (GTK_CONTAINER (menuitem4_menu), quit);
279
280         task = gtk_menu_item_new_with_mnemonic ("_Task");
281         gtk_container_add (GTK_CONTAINER (menubar), task);
282
283         task_menu = gtk_menu_new ();
284         gtk_menu_item_set_submenu (GTK_MENU_ITEM (task), task_menu);
285
286         new = gtk_menu_item_new_with_mnemonic ("_New");
287         gtk_container_add (GTK_CONTAINER (task_menu), new);
288
289         delete = gtk_menu_item_new_with_mnemonic ("_Delete");
290         gtk_widget_set_sensitive(delete, FALSE);
291         gtk_container_add (GTK_CONTAINER (task_menu), delete);
292
293         menuitem7 = gtk_menu_item_new_with_mnemonic ("_Help");
294         gtk_container_add (GTK_CONTAINER (menubar), menuitem7);
295
296         menuitem7_menu = gtk_menu_new ();
297         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem7), menuitem7_menu);
298
299         about = gtk_menu_item_new_with_mnemonic ("_About");
300         gtk_container_add (GTK_CONTAINER (menuitem7_menu), about);
301
302         scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
303         gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0);
304
305         tasks = gtk_tree_view_new ();
306
307         curcol = gtk_tree_view_column_new ();
308         gtk_tree_view_column_set_title(curcol, "Status");
309         renderer = gtk_cell_renderer_text_new();
310         gtk_tree_view_column_pack_start(curcol, renderer, True);
311         gtk_tree_view_append_column(GTK_TREE_VIEW(tasks), curcol);
312         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
313
314         curcol = gtk_tree_view_column_new ();
315         gtk_tree_view_column_set_title(curcol, "ID");
316         renderer = gtk_cell_renderer_text_new();
317         gtk_tree_view_column_pack_start(curcol, renderer, True);
318         gtk_tree_view_append_column(GTK_TREE_VIEW(tasks), curcol);
319         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 1);
320
321         curcol = gtk_tree_view_column_new ();
322         gtk_tree_view_column_set_title(curcol, "Day");
323         renderer = gtk_cell_renderer_text_new();
324         gtk_tree_view_column_pack_start(curcol, renderer, True);
325         gtk_tree_view_append_column(GTK_TREE_VIEW(tasks), curcol);
326         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 2);
327
328         curcol = gtk_tree_view_column_new ();
329         gtk_tree_view_column_set_title(curcol, "Time");
330         renderer = gtk_cell_renderer_text_new();
331         gtk_tree_view_column_pack_start(curcol, renderer, True);
332         gtk_tree_view_append_column(GTK_TREE_VIEW(tasks), curcol);
333         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 3);
334
335         curcol = gtk_tree_view_column_new ();
336         gtk_tree_view_column_set_title(curcol, "Command Line");
337         renderer = gtk_cell_renderer_text_new();
338         gtk_tree_view_column_pack_start(curcol, renderer, True);
339         gtk_tree_view_append_column(GTK_TREE_VIEW(tasks), curcol);
340         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 4);
341
342         store_jobs = gtk_list_store_new(5, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
343         gtk_tree_view_set_model(GTK_TREE_VIEW(tasks), GTK_TREE_MODEL(store_jobs));
344         g_object_unref(store_jobs);
345
346         gtk_container_add (GTK_CONTAINER (scrolledwindow), tasks);
347
348         g_signal_connect (gtk_tree_view_get_selection(GTK_TREE_VIEW(tasks)) , "changed", G_CALLBACK (on_job_select), NULL);
349
350         statusbar = gtk_statusbar_new ();
351         gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 0);
352
353
354         g_signal_connect ((gpointer) quit, "activate",
355           G_CALLBACK (on_quit_activate), NULL);
356         g_signal_connect ((gpointer) new, "activate",
357           G_CALLBACK (on_new_activate), NULL);
358         g_signal_connect ((gpointer) delete, "activate",
359           G_CALLBACK (on_delete_activate), NULL);
360         g_signal_connect ((gpointer) about, "activate",
361           G_CALLBACK (on_about_activate), NULL);
362
363         gtk_window_add_accel_group (GTK_WINDOW (mainwindow), accel_group);
364         gtk_widget_set_sensitive(tasks, FALSE);
365
366         return mainwindow;
367 }
368
369 void
370 on_chk_weekly_toggled                  (GtkToggleButton *togglebutton,
371                                                                                 gpointer         user_data)
372 {
373         gtk_widget_set_sensitive(entry_repeat_weekly, gtk_toggle_button_get_active(togglebutton));
374 }
375
376
377 void
378 on_chk_monthly_toggled                 (GtkToggleButton *togglebutton,
379                                         gpointer         user_data)
380 {
381         gtk_widget_set_sensitive(entry_repeat_monthly, gtk_toggle_button_get_active(togglebutton));
382 }
383
384
385 static GtkWidget*create_new_job_dialog (void)
386 {
387         GtkWidget *new_job_dialog;
388         GtkWidget *dialog_vbox1;
389         GtkWidget *frame1;
390         GtkWidget *table1;
391         GtkWidget *label4;
392         GtkWidget *cal_day;
393         GtkWidget *label3;
394         GtkWidget *entry_time;
395         GtkWidget *chk_weekly;
396         GtkWidget *chk_monthly;
397         GtkWidget *label1;
398         GtkWidget *frame2;
399         GtkWidget *hbox1;
400         GtkWidget *label5;
401         GtkWidget *label2;
402         GtkWidget *dialog_action_area1;
403         GtkWidget *cancelbutton1;
404         GtkWidget *okbutton1;
405
406         new_job_dialog = gtk_dialog_new ();
407         gtk_window_set_title (GTK_WINDOW (new_job_dialog), "New job");
408
409         dialog_vbox1 = GTK_DIALOG (new_job_dialog)->vbox;
410
411         frame1 = gtk_frame_new (NULL);
412         gtk_box_pack_start (GTK_BOX (dialog_vbox1), frame1, TRUE, TRUE, 0);
413
414         table1 = gtk_table_new (4, 2, FALSE);
415         gtk_container_add (GTK_CONTAINER (frame1), table1);
416
417         label4 = gtk_label_new ("Time:");
418         gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 1, 2,
419                                           (GtkAttachOptions) (GTK_FILL),
420                                           (GtkAttachOptions) (0), 0, 0);
421         gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
422
423         cal_day = gtk_calendar_new ();
424         gtk_table_attach (GTK_TABLE (table1), cal_day, 1, 2, 0, 1,
425                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
426                                           (GtkAttachOptions) (GTK_FILL), 0, 0);
427         gtk_calendar_set_display_options (GTK_CALENDAR (cal_day),
428                                           GTK_CALENDAR_SHOW_HEADING
429                                           | GTK_CALENDAR_SHOW_DAY_NAMES);
430
431         label3 = gtk_label_new ("Date");
432         gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 0, 1,
433                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
434                                           (GtkAttachOptions) (0), 0, 0);
435         gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
436
437         entry_time = gtk_entry_new ();
438         gtk_table_attach (GTK_TABLE (table1), entry_time, 1, 2, 1, 2,
439                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
440                                           (GtkAttachOptions) (0), 0, 0);
441
442         chk_weekly = gtk_check_button_new_with_mnemonic ("Repeat weekly");
443         gtk_table_attach (GTK_TABLE (table1), chk_weekly, 0, 1, 2, 3,
444                                           (GtkAttachOptions) (GTK_FILL),
445                                           (GtkAttachOptions) (0), 0, 0);
446
447         entry_repeat_weekly = gtk_entry_new ();
448         gtk_table_attach (GTK_TABLE (table1), entry_repeat_weekly, 1, 2, 2, 3,
449                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
450                                           (GtkAttachOptions) (0), 0, 0);
451
452         chk_monthly = gtk_check_button_new_with_mnemonic ("Repeat monthly");
453         gtk_table_attach (GTK_TABLE (table1), chk_monthly, 0, 1, 3, 4,
454                                           (GtkAttachOptions) (GTK_FILL),
455                                           (GtkAttachOptions) (0), 0, 0);
456
457         entry_repeat_monthly = gtk_entry_new ();
458         gtk_table_attach (GTK_TABLE (table1), entry_repeat_monthly, 1, 2, 3, 4,
459                                           (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
460                                           (GtkAttachOptions) (0), 0, 0);
461
462         label1 = gtk_label_new ("Moment");
463         gtk_frame_set_label_widget (GTK_FRAME (frame1), label1);
464
465         frame2 = gtk_frame_new (NULL);
466         gtk_box_pack_start (GTK_BOX (dialog_vbox1), frame2, TRUE, TRUE, 0);
467
468         hbox1 = gtk_hbox_new (FALSE, 0);
469         gtk_container_add (GTK_CONTAINER (frame2), hbox1);
470
471         label5 = gtk_label_new ("Command to execute");
472         gtk_box_pack_start (GTK_BOX (hbox1), label5, TRUE, TRUE, 0);
473
474         entry_cmd = gtk_entry_new ();
475         gtk_box_pack_start (GTK_BOX (hbox1), entry_cmd, TRUE, TRUE, 0);
476
477         label2 = gtk_label_new ("Command");
478         gtk_frame_set_label_widget (GTK_FRAME (frame2), label2);
479
480         dialog_action_area1 = GTK_DIALOG (new_job_dialog)->action_area;
481         gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
482
483         cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
484         gtk_dialog_add_action_widget (GTK_DIALOG (new_job_dialog), cancelbutton1, GTK_RESPONSE_CANCEL);
485         GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
486
487         okbutton1 = gtk_button_new_from_stock ("gtk-ok");
488         gtk_dialog_add_action_widget (GTK_DIALOG (new_job_dialog), okbutton1, GTK_RESPONSE_OK);
489         GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
490
491         g_signal_connect ((gpointer) chk_weekly, "toggled",
492                                           G_CALLBACK (on_chk_weekly_toggled),
493                                           NULL);
494         g_signal_connect ((gpointer) chk_monthly, "toggled",
495                                           G_CALLBACK (on_chk_monthly_toggled),
496                                           NULL);
497
498         return new_job_dialog;
499 }
500
501
502
503  int main(int argc, char **argv)
504 {
505         gwcrontab_init_subsystems;
506         lp_load(dyn_CONFIGFILE,True,False,False);
507         load_interfaces();
508         setup_logging(argv[0], DEBUG_STDERR);
509
510         gtk_init(&argc, &argv);
511         mainwin = create_mainwindow();
512         gtk_widget_show_all(mainwin);
513
514         return gtk_event_loop();
515 }