]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/callbacks.c
Fix bat to automatically use installed bat.conf
[bacula/bacula] / bacula / src / gnome2-console / callbacks.c
1 /*
2  *    Version $Id$
3  */
4 /*
5    Bacula® - The Network Backup Solution
6
7    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
8
9    The main author of Bacula is Kern Sibbald, with contributions from
10    many others, a complete list can be found in the file AUTHORS.
11    This program is Free Software; you can redistribute it and/or
12    modify it under the terms of version two of the GNU General Public
13    License as published by the Free Software Foundation and included
14    in the file LICENSE.
15
16    This program is distributed in the hope that it will be useful, but
17    WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24    02110-1301, USA.
25
26    Bacula® is a registered trademark of Kern Sibbald.
27    The licensor of Bacula is the Free Software Foundation Europe
28    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
29    Switzerland, email:ftf@fsfeurope.org.
30 */
31
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #include "bacula.h"
38 #include "console.h"
39
40 #include "callbacks.h"
41 #include "interface.h"
42 #include "support.h"
43
44 #define KEY_Enter 65293
45 #define KEY_Up    65362
46 #define KEY_Down  65364
47 #define KEY_Left  65361
48 #define KEY_Right 65363
49
50 void terminate_console(int sig);
51
52 extern "C" gint compare_func(const void *data1, const void *data2);
53
54 gboolean
55 on_console_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
56 {
57    gtk_main_quit();
58    terminate_console(0);
59    return TRUE;
60 }
61
62 void
63 on_connect_activate(GtkMenuItem *menuitem, gpointer user_data)
64 {
65    if (connect_to_director(user_data)) {
66       start_director_reader(user_data);
67    }
68 }
69
70
71 void
72 on_disconnect_activate(GtkMenuItem *menuitem, gpointer user_data)
73 {
74    if (disconnect_from_director(user_data)) {
75       stop_director_reader(user_data);
76    }
77 }
78
79
80 void
81 on_exit_activate(GtkMenuItem *menuitem, gpointer user_data)
82 {
83    gtk_main_quit();
84 }
85
86
87 void
88 on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
89 {
90
91 }
92
93
94 void
95 on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
96 {
97
98 }
99
100
101 void
102 on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
103 {
104
105 }
106
107
108 void
109 on_clear1_activate(GtkMenuItem *menuitem, gpointer user_data)
110 {
111
112 }
113
114 void
115 on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
116 {
117 }
118
119
120 void
121 on_about_activate(GtkMenuItem *menuitem, gpointer user_data)
122 {
123    gtk_widget_show(about1);
124    gtk_main();
125 }
126
127
128 void
129 on_connect_button_clicked(GtkButton *button, gpointer user_data)
130 {
131    if (connect_to_director(user_data)) {
132       start_director_reader(user_data);
133    }
134 }
135
136 /* Define max length of history and how many to delete when it gets there.
137  * If you make HIST_DEL > HIST_MAX, you shoot yourself in the foot.
138  */
139 #define HIST_MAX 2500
140 #define HIST_DEL  500
141
142 static GList *hist = NULL;
143 static GList *hc = NULL;
144 static int hist_len = 0;
145
146 static void add_to_history(gchar *ecmd)
147 {
148    int len = strlen(ecmd);
149
150    if (len > 0) {
151       hist = g_list_append(hist, bstrdup(ecmd));
152       hist_len++;
153    }
154    if (hist_len >= HIST_MAX) {
155       int i;
156       GList *hp;
157       for (i=0; i<HIST_DEL; i++) {
158          hp = g_list_next(hist);
159          free(hp->data);
160          hist = g_list_remove(hist, hp->data);
161       }
162       hist_len -= HIST_DEL;
163    }
164    hc = NULL;
165 }
166
167 /*
168  * Note, apparently there is a bug in GNOME where some of the
169  *  key press events are lost -- at least it loses every other
170  *  up arrow!
171  */
172 gboolean
173 on_entry1_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
174 {
175    if (event->keyval == KEY_Enter) {
176       char *ecmd = (char *)gtk_entry_get_text((GtkEntry *)entry1);
177       set_text(ecmd, strlen(ecmd));
178       set_text("\n", 1);
179       add_to_history(ecmd);
180       write_director(ecmd);
181       gtk_entry_set_text((GtkEntry *)entry1, "");
182    } else if (event->keyval == KEY_Up) {
183       if (!hc) {
184          if (!hist) {
185             return FALSE;
186          }
187          hc = g_list_last(hist);
188       } else {
189          hc = g_list_previous(hc);
190       }
191       if (!hc) {
192          if (!hist) {
193             return FALSE;
194          }
195          hc = g_list_first(hist);
196       }
197       gtk_entry_set_text((GtkEntry *)entry1, (gchar *)hc->data);
198    } else if (event->keyval == KEY_Down) {
199       if (!hc) {
200          if (!hist) {
201             return FALSE;
202          }
203          hc = g_list_first(hist);
204       } else {
205          hc = g_list_next(hc);
206       }
207       if (!hc) {
208          if (!hist) {
209             return FALSE;
210          }
211          hc = g_list_last(hist);
212       }
213       gtk_entry_set_text((GtkEntry *)entry1, (gchar *)hc->data);
214    }
215 // gtk_entry_set_position((GtkEntry *)entry1, -1);
216 // gtk_window_set_focus((GtkWindow *)app1, entry1);
217    return FALSE;
218 }
219
220
221 void
222 on_app1_show(GtkWidget *widget, gpointer user_data)
223 {
224 }
225
226
227
228 void
229 on_msgs_button_clicked(GtkButton *button, gpointer user_data)
230 {
231    write_director("messages");
232 }
233
234 void
235 on_msgs_activate(GtkMenuItem *menuitem, gpointer user_data)
236 {
237    write_director("messages");
238 }
239
240 void
241 on_about_button_clicked(GtkButton *button, gpointer user_data)
242 {
243    gtk_widget_hide(about1);
244    gtk_main_quit();
245    set_status_ready();
246 }
247
248 void
249 on_select_director_OK_clicked(GtkButton *button, gpointer user_data)
250 {
251    reply = OK;
252    gtk_widget_hide(dir_dialog);
253    gtk_main_quit();
254    set_status_ready();
255 }
256
257
258 void
259 on_select_director_cancel_clicked(GtkButton *button, gpointer user_data)
260 {
261    reply = CANCEL;
262    gtk_widget_hide(dir_dialog);
263    gtk_main_quit();
264    set_status_ready();
265 }
266
267 /*
268  * Compare list string items
269  */
270 extern "C"
271 gint compare_func(const void *data1, const void *data2)
272 {
273    return strcmp((const char *)data1, (const char *)data2);
274 }
275
276 static GList *find_combo_list(char *name)
277 {
278    if (strcmp(name, "job") == 0) {
279       return job_list;
280    }
281    if (strcmp(name, "pool") == 0) {
282       return pool_list;
283    }
284    if (strcmp(name, "client") == 0) {
285       return client_list;
286    }
287    if (strcmp(name, "storage") == 0) {
288       return storage_list;
289    }
290    if (strcmp(name, "fileset") == 0) {
291       return fileset_list;
292    }
293    if (strcmp(name, "messages") == 0) {
294       return messages_list;
295    }
296    if (strcmp(name, "type") == 0) {
297       return type_list;
298    }
299    if (strcmp(name, "level") == 0) {
300       return level_list;
301    }
302    return NULL;
303 }
304
305 /*
306  * Set correct default values in the Run dialog box
307  */
308 static void set_run_defaults()
309 {
310    GtkWidget *combo, *entry;
311    char *job, *def;
312    GList *item, *list;
313    char cmd[1000];
314    int pos;
315
316    stop_director_reader(NULL);
317
318    combo = lookup_widget(run_dialog, "combo_job");
319    job = (char *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
320    bsnprintf(cmd, sizeof(cmd), ".defaults job=\"%s\"", job);
321    write_director(cmd);
322    while (bnet_recv(UA_sock) > 0) {
323       def = strchr(UA_sock->msg, '=');
324       if (!def) {
325          continue;
326       }
327       *def++ = 0;
328       if (strcmp(UA_sock->msg, "job") == 0 ||
329           strcmp(UA_sock->msg, "when") == 0) {
330          continue;
331       }
332       /* Where is an entry box */
333       if (strcmp(UA_sock->msg, "where") == 0) {
334          entry = lookup_widget(run_dialog, "entry_where");
335          gtk_entry_set_text(GTK_ENTRY(entry), def);
336          continue;
337       }
338
339       /* Now handle combo boxes */
340       list = find_combo_list(UA_sock->msg);
341       if (!list) {
342          continue;
343       }
344       item = g_list_find_custom(list, def, compare_func);
345       bsnprintf(cmd, sizeof(cmd), "combo_%s", UA_sock->msg);
346       combo = lookup_widget(run_dialog, cmd);
347       if (!combo) {
348          continue;
349       }
350       pos = g_list_position(list, item);
351       gtk_list_select_item(GTK_LIST(GTK_COMBO(combo)->list), pos);
352    }
353    start_director_reader(NULL);
354 }
355
356 void
357 on_entry_job_changed(GtkEditable *editable, gpointer user_data)
358 {
359    set_run_defaults();
360 }
361
362
363 void
364 on_run_button_clicked(GtkButton *button, gpointer user_data)
365 {
366    char dt[50];
367    GtkWidget *entry;
368
369    bstrutime(dt, sizeof(dt), time(NULL));
370    entry = lookup_widget(run_dialog, "entry_when");
371    gtk_entry_set_text(GTK_ENTRY(entry), dt);
372    set_run_defaults();
373    gtk_widget_show(run_dialog);
374    gtk_main();
375 }
376
377
378 static char *get_combo_text(GtkWidget *dialog, const char *combo_name)
379 {
380    GtkWidget *combo;
381    combo = lookup_widget(dialog, combo_name);
382    if (!combo) {
383       return NULL;
384    }
385    return (char *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
386 }
387
388 static char *get_entry_text(GtkWidget *dialog, const char *entry_name)
389 {
390    GtkWidget *entry;
391    entry = lookup_widget(dialog, entry_name);
392    if (!entry) {
393       return NULL;
394    }
395    return (char *)gtk_entry_get_text(GTK_ENTRY(entry));
396 }
397
398 static char *get_spin_text(GtkWidget *dialog, const char *spin_name)
399 {
400    GtkSpinButton *spin;
401    spin = (GtkSpinButton *)lookup_widget(dialog, spin_name);
402    if (!spin) {
403       return NULL;
404    }
405    return (char *)gtk_entry_get_text(GTK_ENTRY(&spin->entry));
406 }
407
408
409
410
411 void
412 on_run_ok_clicked(GtkButton *button, gpointer user_data)
413 {
414    char *job, *fileset, *level, *client, *pool, *when, *where, *storage, *priority;
415    
416    gtk_widget_hide(run_dialog);
417    gtk_main_quit();
418
419    job     = get_combo_text(run_dialog, "combo_job");
420    fileset = get_combo_text(run_dialog, "combo_fileset");
421    client  = get_combo_text(run_dialog, "combo_client");
422    pool    = get_combo_text(run_dialog, "combo_pool");
423    storage = get_combo_text(run_dialog, "combo_storage");
424    level   = get_combo_text(run_dialog, "combo_level");
425    priority = get_spin_text(run_dialog, "spinbutton1");
426    when    = get_entry_text(run_dialog, "entry_when");
427    where   = get_entry_text(run_dialog, "entry_where");
428
429    if (!job || !fileset || !client || !pool || !storage ||
430        !level || !priority || !when || !where) {
431       set_status_ready();
432       return;
433    }
434
435    bsnprintf(cmd, sizeof(cmd),
436              "run job=\"%s\" fileset=\"%s\" level=%s client=\"%s\" pool=\"%s\" "
437              "when=\"%s\" where=\"%s\" storage=\"%s\" priority=\"%s\"\n",
438              job, fileset, level, client, pool, when, where, storage, priority);
439    write_director(cmd);
440    set_text(cmd, strlen(cmd));
441    write_director("yes");
442    return;
443 }
444
445
446 void
447 on_run_cancel_clicked(GtkButton *button, gpointer user_data)
448 {
449    gtk_widget_hide(run_dialog);
450    gtk_main_quit();
451    set_status_ready();
452 }
453
454 gboolean
455 on_entry1_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
456 {
457   return FALSE;
458 }
459
460 void
461 on_restore_button_clicked(GtkButton *button, gpointer user_data)
462 {
463    gtk_widget_show(restore_dialog);
464    gtk_main();
465 }
466
467 void
468 on_view_fileset_clicked(GtkButton *button, gpointer user_data)
469 {
470 }
471
472
473 void
474 on_restore_cancel_clicked(GtkButton *button, gpointer user_data)
475 {
476    gtk_widget_hide(restore_dialog);
477    gtk_main_quit();
478    set_status_ready();
479 }
480
481
482 void
483 on_label_button_clicked(GtkButton *button, gpointer user_data)
484 {
485    gtk_widget_show(label_dialog);
486    gtk_main();
487 }
488
489 void
490 on_label_ok_clicked(GtkButton *button, gpointer user_data)
491 {
492    char *pool, *volume, *storage, *slot;
493
494    gtk_widget_hide(label_dialog);
495    gtk_main_quit();
496
497    pool    = get_combo_text(label_dialog, "label_combo_pool");
498    storage = get_combo_text(label_dialog, "label_combo_storage");
499
500    volume  = get_entry_text(label_dialog, "label_entry_volume");
501
502    slot    = get_spin_text(label_dialog, "label_slot");
503
504    if (!pool || !storage || !volume || !(*volume)) {
505       set_status_ready();
506       return;
507    }
508
509    bsnprintf(cmd, sizeof(cmd),
510              "label volume=\"%s\" pool=\"%s\" storage=\"%s\" slot=%s\n", 
511              volume, pool, storage, slot);
512    write_director(cmd);
513    set_text(cmd, strlen(cmd));
514 }
515
516
517 void
518 on_label_cancel_clicked(GtkButton *button, gpointer user_data)
519 {
520    gtk_widget_hide(label_dialog);
521    gtk_main_quit();
522    set_status_ready();
523 }
524
525
526 void
527 on_select_files_button_clicked(GtkButton *button, gpointer user_data)
528 {
529    char *job, *fileset, *client, *pool, *before, *storage;
530
531    gtk_widget_hide(restore_dialog);
532
533    job     = get_combo_text(restore_dialog, "combo_restore_job");
534    fileset = get_combo_text(restore_dialog, "combo_restore_fileset");
535    client  = get_combo_text(restore_dialog, "combo_restore_client");
536    pool    = get_combo_text(restore_dialog, "combo_restore_pool");
537    storage = get_combo_text(restore_dialog, "combo_restore_storage");
538
539    before  = get_entry_text(restore_dialog, "restore_before_entry");
540
541    if (!job || !fileset || !client || !pool || !storage || !before) {
542       set_status_ready();
543       return;
544    }
545
546    bsnprintf(cmd, sizeof(cmd),
547              "restore select current fileset=\"%s\" client=\"%s\" pool=\"%s\" "
548              "storage=\"%s\"\n", fileset, client, pool, storage);
549    write_director(cmd);
550    set_text(cmd, strlen(cmd));
551    gtk_widget_show(restore_file_selection);
552    select_restore_files();            /* put up select files dialog */
553 }
554
555 void
556 on_restore_select_ok_clicked(GtkButton *button, gpointer user_data)
557 {
558    gtk_widget_hide(restore_file_selection);
559    write_director("done");
560    gtk_main_quit();
561    set_status_ready();
562 }
563
564
565 void
566 on_restore_select_cancel_clicked(GtkButton *button, gpointer user_data)
567 {
568    gtk_widget_hide(restore_file_selection);
569    write_director("quit");
570    gtk_main_quit();
571    set_status_ready();
572 }
573
574 gboolean
575 on_restore_files_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
576 {
577    gtk_widget_hide(restore_file_selection);
578    gtk_main_quit();
579    set_status_ready();
580    return FALSE;
581 }
582
583
584 void
585 on_new1_activate                       (GtkMenuItem     *menuitem,
586                                         gpointer         user_data)
587 {
588
589 }
590
591
592 void
593 on_open1_activate                      (GtkMenuItem     *menuitem,
594                                         gpointer         user_data)
595 {
596
597 }
598
599
600 void
601 on_save1_activate                      (GtkMenuItem     *menuitem,
602                                         gpointer         user_data)
603 {
604
605 }
606
607
608 void
609 on_save_as1_activate                   (GtkMenuItem     *menuitem,
610                                         gpointer         user_data)
611 {
612
613 }
614
615
616 void
617 on_quit1_activate                      (GtkMenuItem     *menuitem,
618                                         gpointer         user_data)
619 {
620
621 }
622
623
624 void
625 on_cut2_activate                       (GtkMenuItem     *menuitem,
626                                         gpointer         user_data)
627 {
628
629 }
630
631
632 void
633 on_copy2_activate                      (GtkMenuItem     *menuitem,
634                                         gpointer         user_data)
635 {
636
637 }
638
639
640 void
641 on_paste2_activate                     (GtkMenuItem     *menuitem,
642                                         gpointer         user_data)
643 {
644
645 }
646
647
648 void
649 on_clear2_activate                     (GtkMenuItem     *menuitem,
650                                         gpointer         user_data)
651 {
652
653 }
654
655
656 void
657 on_properties1_activate                (GtkMenuItem     *menuitem,
658                                         gpointer         user_data)
659 {
660
661 }
662
663
664 void
665 on_preferences2_activate               (GtkMenuItem     *menuitem,
666                                         gpointer         user_data)
667 {
668
669 }
670
671
672 void
673 on_about2_activate                     (GtkMenuItem     *menuitem,
674                                         gpointer         user_data)
675 {
676
677 }
678
679 /*
680  * Set correct default values in the Restore dialog box
681  */
682 void set_restore_dialog_defaults()
683 {
684    GtkWidget *combo;
685    char *job, *def;
686    GList *item, *list;
687    char cmd[1000];
688    int pos;
689
690    stop_director_reader(NULL);
691
692    combo = lookup_widget(restore_dialog, "combo_restore_job");
693    job = (char *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
694    bsnprintf(cmd, sizeof(cmd), ".defaults job=\"%s\"", job);
695    write_director(cmd);
696    while (bnet_recv(UA_sock) > 0) {
697       def = strchr(UA_sock->msg, '=');
698       if (!def) {
699          continue;
700       }
701       *def++ = 0;
702       if (strcmp(UA_sock->msg, "job") == 0 ||
703           strcmp(UA_sock->msg, "when") == 0 ||
704           strcmp(UA_sock->msg, "where") == 0 ||
705           strcmp(UA_sock->msg, "messages") == 0 ||
706           strcmp(UA_sock->msg, "level") == 0 ||
707           strcmp(UA_sock->msg, "type") == 0) {
708          continue;
709       }
710
711       /* Now handle combo boxes */
712       list = find_combo_list(UA_sock->msg);
713       if (!list) {
714          continue;
715       }
716       item = g_list_find_custom(list, def, compare_func);
717       bsnprintf(cmd, sizeof(cmd), "combo_restore_%s", UA_sock->msg);
718       combo = lookup_widget(restore_dialog, cmd);
719       if (!combo) {
720          continue;
721       }
722       pos = g_list_position(list, item);
723       gtk_list_select_item(GTK_LIST(GTK_COMBO(combo)->list), pos);
724    }
725    start_director_reader(NULL);
726 }
727
728
729 void
730 on_restore_job_entry_changed(GtkEditable *editable, gpointer user_data)
731 {
732    /* Set defaults that correspond to new job selection */
733    set_restore_dialog_defaults();
734 }
735
736 void
737 on_dir_button_clicked(GtkButton *toolbutton, gpointer user_data)
738 {
739    write_director("status dir");
740 }