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