]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/callbacks.c
Add Gnome 2.0 files
[bacula/bacula] / bacula / src / gnome2-console / callbacks.c
1 /* 
2  *    Version $Id$
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #  include <config.h>
7 #endif
8
9 #include "bacula.h"
10 #include "console.h"
11
12 #include "callbacks.h"
13 #include "interface.h"
14 #include "support.h"
15
16 #define KEY_Enter 65293
17 #define KEY_Up    65362
18 #define KEY_Down  65364
19 #define KEY_Left  65361
20 #define KEY_Right 65363
21
22 gboolean
23 on_app1_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
24 {
25    gtk_main_quit();
26    return FALSE;
27 }
28
29 void
30 on_connect_activate(GtkMenuItem *menuitem, gpointer user_data)
31 {
32    if (connect_to_director(user_data)) {
33       start_director_reader(user_data);
34    }
35 }
36
37
38 void
39 on_disconnect_activate(GtkMenuItem *menuitem, gpointer user_data)
40 {
41    if (disconnect_from_director(user_data)) {
42       stop_director_reader(user_data);
43    }
44 }
45
46
47 void
48 on_exit_activate(GtkMenuItem *menuitem, gpointer user_data)
49 {
50    gtk_main_quit();
51 }
52
53
54 void
55 on_cut1_activate(GtkMenuItem *menuitem, gpointer user_data)
56 {
57
58 }
59
60
61 void
62 on_copy1_activate(GtkMenuItem *menuitem, gpointer user_data)
63 {
64
65 }
66
67
68 void
69 on_paste1_activate(GtkMenuItem *menuitem, gpointer user_data)
70 {
71
72 }
73
74
75 void
76 on_clear1_activate(GtkMenuItem *menuitem, gpointer user_data)
77 {
78
79 }
80
81
82 void
83 on_properties1_activate(GtkMenuItem *menuitem, gpointer user_data)
84 {
85
86 }
87
88
89 void
90 on_preferences1_activate(GtkMenuItem *menuitem, gpointer user_data)
91 {
92 }
93
94
95 void
96 on_about_activate(GtkMenuItem *menuitem, gpointer user_data)
97 {
98    gtk_widget_show(about1);
99    gtk_main();
100 }
101
102
103 void
104 on_connect_button_clicked(GtkButton *button, gpointer user_data)
105 {
106    if (connect_to_director(user_data)) {
107       start_director_reader(user_data);
108    }
109 }
110
111 /* Define max length of history and how many to delete when it gets there.  
112  * If you make HIST_DEL > HIST_MAX, you shoot yourself in the foot. 
113  */
114 #define HIST_MAX 2500
115 #define HIST_DEL  500
116
117 static GList *hist = NULL;
118 static GList *hc = NULL;
119 static int hist_len = 0;
120
121 static void add_to_history(gchar *ecmd)
122 {
123    int len = strlen(ecmd);
124
125    if (len > 0) {
126       hist = g_list_append(hist, bstrdup(ecmd));
127       hist_len++;
128    }
129    if (hist_len >= HIST_MAX) {
130       int i;
131       GList *hp;
132       for (i=0; i<HIST_DEL; i++) {
133          hp = g_list_next(hist);
134          free(hp->data);
135          hist = g_list_remove(hist, hp->data);
136       }
137       hist_len -= HIST_DEL;
138    }
139    hc = NULL;             
140 }
141
142 /*
143  * Note, apparently there is a bug in GNOME where some of the
144  *  key press events are lost -- at least it loses every other
145  *  up arrow!
146  */
147 gboolean
148 on_entry1_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
149 {
150    if (event->keyval == KEY_Enter) {
151       char *ecmd = (char *)gtk_entry_get_text((GtkEntry *)entry1);
152       set_text(ecmd, strlen(ecmd));
153       set_text("\n", 1);
154       add_to_history(ecmd);
155       write_director(ecmd);
156       gtk_entry_set_text((GtkEntry *)entry1, "");
157    } else if (event->keyval == KEY_Up) {
158       if (!hc) {
159          hc = g_list_last(hist);
160       } else {
161          hc = g_list_previous(hc);
162       }
163       if (!hc) {
164          hc = g_list_first(hist);
165       }
166       gtk_entry_set_text((GtkEntry *)entry1, (gchar *)hc->data);
167    } else if (event->keyval == KEY_Down) {
168       if (!hc) {
169          hc = g_list_first(hist);
170       } else {
171          hc = g_list_next(hc);
172       }
173       if (!hc) {
174          hc = g_list_last(hist);
175       }
176       gtk_entry_set_text((GtkEntry *)entry1, (gchar *)hc->data);
177    }
178    gtk_window_set_focus((GtkWindow *)app1, entry1);
179    return FALSE;
180 }
181
182
183 void
184 on_app1_show(GtkWidget *widget, gpointer user_data)
185 {
186 }
187
188
189
190 void
191 on_msgs_button_clicked(GtkButton *button, gpointer user_data)
192 {
193    write_director("messages");
194 }
195
196 void
197 on_msgs_activate(GtkMenuItem *menuitem, gpointer user_data)
198 {
199    write_director("messages");
200 }
201
202 void
203 on_about_button_clicked(GtkButton *button, gpointer user_data)
204 {
205    gtk_widget_hide(about1);
206    gtk_main_quit();
207    set_status_ready();
208 }
209
210 void
211 on_select_director_OK_clicked(GtkButton *button, gpointer user_data)
212 {
213    reply = OK;
214    gtk_widget_hide(dir_dialog);
215    gtk_main_quit();
216    set_status_ready();
217 }
218
219
220 void
221 on_select_director_cancel_clicked(GtkButton *button, gpointer user_data)
222 {
223    reply = CANCEL;
224    gtk_widget_hide(dir_dialog);
225    gtk_main_quit();
226    set_status_ready();
227 }
228
229 /*
230  * Compare list string items
231  */
232 static gint compare_func(const void *data1, const void *data2)
233 {
234    return strcmp((const char *)data1, (const char *)data2);
235 }
236
237 static GList *find_combo_list(char *name)
238 {
239    if (strcmp(name, "job") == 0) {
240       return job_list;
241    }
242    if (strcmp(name, "pool") == 0) {
243       return pool_list;
244    }
245    if (strcmp(name, "client") == 0) {
246       return client_list;
247    }
248    if (strcmp(name, "storage") == 0) {
249       return storage_list;
250    }
251    if (strcmp(name, "fileset") == 0) {
252       return fileset_list;
253    }
254    if (strcmp(name, "messages") == 0) {
255       return messages_list;
256    }
257    if (strcmp(name, "type") == 0) {
258       return type_list;
259    }
260    if (strcmp(name, "level") == 0) {
261       return level_list;
262    }
263    return NULL;
264 }
265
266 /*
267  * Set correct default values in the Run dialog box
268  */
269 static void set_run_defaults()
270 {
271    GtkWidget *combo, *entry;
272    char *job, *def;
273    GList *item, *list;
274    char cmd[1000];
275    int pos;
276
277    stop_director_reader(NULL);
278
279    combo = lookup_widget(run_dialog, "combo_job");
280    job = (char *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
281    bsnprintf(cmd, sizeof(cmd), ".defaults job=\"%s\"", job);
282    write_director(cmd);
283    while (bnet_recv(UA_sock) > 0) {
284       def = strchr(UA_sock->msg, '=');
285       if (!def) {
286          continue;
287       }
288       *def++ = 0;
289       if (strcmp(UA_sock->msg, "job") == 0 ||
290           strcmp(UA_sock->msg, "when") == 0) {
291          continue;
292       }
293       /* Where is an entry box */
294       if (strcmp(UA_sock->msg, "where") == 0) {
295          entry = lookup_widget(run_dialog, "entry_where");
296          gtk_entry_set_text(GTK_ENTRY(entry), def);
297          continue;
298       }
299
300       /* Now handle combo boxes */
301       list = find_combo_list(UA_sock->msg);
302       if (!list) {
303          continue;
304       }
305       item = g_list_find_custom(list, def, compare_func);
306       bsnprintf(cmd, sizeof(cmd), "combo_%s", UA_sock->msg);
307       combo = lookup_widget(run_dialog, cmd);
308       if (!combo) {
309          continue;
310       }
311       pos = g_list_position(list, item);
312       gtk_list_select_item(GTK_LIST(GTK_COMBO(combo)->list), pos);
313    }
314    start_director_reader(NULL);
315 }
316
317 void
318 on_entry_job_changed(GtkEditable *editable, gpointer user_data)
319 {
320    set_run_defaults();
321 }
322
323
324 void
325 on_run_button_clicked(GtkButton *button, gpointer user_data)
326 {
327    char dt[50];
328    GtkWidget *entry;
329
330    bstrutime(dt, sizeof(dt), time(NULL));
331    entry = lookup_widget(run_dialog, "entry_when");
332    gtk_entry_set_text(GTK_ENTRY(entry), dt);
333    set_run_defaults();
334    gtk_widget_show(run_dialog);
335    gtk_main();
336 }
337
338
339 static char *get_combo_text(GtkWidget *dialog, char *combo_name)
340 {
341    GtkWidget *combo;
342    combo = lookup_widget(dialog, combo_name);
343    if (!combo) {
344       return NULL;
345    }
346    return (char *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry));
347 }
348
349 static char *get_entry_text(GtkWidget *dialog, char *entry_name)
350 {
351    GtkWidget *entry;
352    entry = lookup_widget(dialog, entry_name);
353    if (!entry) {
354       return NULL;
355    }
356    return (char *)gtk_entry_get_text(GTK_ENTRY(entry));
357 }
358
359 static char *get_spin_text(GtkWidget *dialog, char *spin_name)
360 {
361    GtkSpinButton *spin;
362    spin = (GtkSpinButton *)lookup_widget(dialog, spin_name);
363    if (!spin) {
364       return NULL;
365    }
366    return (char *)gtk_entry_get_text(GTK_ENTRY(&spin->entry));
367 }
368
369
370
371
372 void
373 on_run_ok_clicked(GtkButton *button, gpointer user_data)
374 {
375    char *job, *fileset, *level, *client, *pool, *when, *where, *storage;
376
377    gtk_widget_hide(run_dialog);
378    gtk_main_quit();
379
380    job     = get_combo_text(run_dialog, "combo_job");
381    fileset = get_combo_text(run_dialog, "combo_fileset");
382    client  = get_combo_text(run_dialog, "combo_client");
383    pool    = get_combo_text(run_dialog, "combo_pool");
384    storage = get_combo_text(run_dialog, "combo_storage");
385    level   = get_combo_text(run_dialog, "combo_level");
386
387    when    = get_entry_text(run_dialog, "entry_when");
388    where   = get_entry_text(run_dialog, "entry_where");
389
390    if (!job || !fileset || !client || !pool || !storage ||
391        !level || !when || !where) {
392       set_status_ready();
393       return;
394    }
395       
396    bsnprintf(cmd, sizeof(cmd), 
397              "run job=\"%s\" fileset=\"%s\" level=%s client=\"%s\" pool=\"%s\" "
398              "when=\"%s\" where=\"%s\" storage=\"%s\"", 
399              job, fileset, level, client, pool, when, where, storage);
400    write_director(cmd);
401    write_director("yes");
402    return;
403 }
404
405
406 void
407 on_run_cancel_clicked(GtkButton *button, gpointer user_data)
408 {
409    gtk_widget_hide(run_dialog);
410    gtk_main_quit();
411    set_status_ready();
412 }
413
414 gboolean
415 on_entry1_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
416 {
417   return FALSE;
418 }
419
420 void
421 on_restore_button_clicked(GtkButton *button, gpointer user_data)
422 {
423    gtk_widget_show(restore_dialog);
424    gtk_main();
425 }
426
427 void
428 on_view_fileset_clicked(GtkButton *button, gpointer user_data)
429 {
430 }
431
432 void
433 on_restore_ok_clicked(GtkButton *button, gpointer user_data)
434 {
435    gtk_widget_hide(restore_dialog);
436    gtk_main_quit();
437    set_status_ready();
438 }
439
440
441 void
442 on_restore_cancel_clicked(GtkButton *button, gpointer user_data)
443 {
444    gtk_widget_hide(restore_dialog);
445    gtk_main_quit();
446    set_status_ready();
447 }
448
449 void
450 on_apply_button_clicked(GtkButton *button, gpointer user_data)
451 {
452    gtk_widget_show(restore_files);
453    gtk_main();
454 }
455
456 void
457 on_restore_file_clicked(GtkButton *button, gpointer user_data)
458 {
459    gtk_widget_hide(restore_files);
460    gtk_main_quit();
461 }
462
463 void
464 on_label_button_clicked(GtkButton *button, gpointer user_data)
465 {
466    gtk_widget_show(label_dialog);
467    gtk_main();
468 }
469
470 void
471 on_label_ok_clicked(GtkButton *button, gpointer user_data)
472 {
473    char *pool, *volume, *storage, *slot;
474
475    gtk_widget_hide(label_dialog);
476    gtk_main_quit();
477
478    pool    = get_combo_text(label_dialog, "label_combo_pool");
479    storage = get_combo_text(label_dialog, "label_combo_storage");
480
481    volume  = get_entry_text(label_dialog, "label_entry_volume");
482
483    slot    = get_spin_text(label_dialog, "label_slot");
484
485    if (!pool || !storage || !volume || !(*volume)) {
486       set_status_ready();
487       return;
488    }
489       
490    bsnprintf(cmd, sizeof(cmd), 
491              "label name=\"%s\" pool=\"%s\" storage=\"%s\" slot=%s", 
492              volume, pool, storage, slot);
493    write_director(cmd);
494 }
495
496
497 void
498 on_label_cancel_clicked(GtkButton *button, gpointer user_data)
499 {
500    gtk_widget_hide(label_dialog);
501    gtk_main_quit();
502    set_status_ready();
503 }