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