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