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