]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/restore.c
Add code for GUI Add and Remove buttons
[bacula/bacula] / bacula / src / gnome2-console / restore.c
1 /*
2  * Handle console GUI selection of files
3  *  Kern Sibbald, March, 2004
4  *
5 */
6
7 #include "bacula.h"
8 #include "console.h"
9 #include "interface.h"
10 #include "support.h"
11 #include "restore.h"
12
13 extern BSOCK *UA_sock;
14 void write_director(gchar *msg);
15 void start_director_reader(gpointer data);
16 void stop_director_reader(gpointer data);
17
18
19 /* Forward referenced subroutines */
20 void FillDirectory(char *path, Window *window);
21 Window *new_window();
22 static void click_column_cb(GtkCList *item, gint column, Window *restore);
23 static void select_row_cb(GtkCList *item, gint row, gint column, 
24              GdkEventButton *event, Window *restore);
25 void row_data_destroy_cb(gpointer data);
26 void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
27         POOLMEM **file, int *fnl);
28
29 #ifdef needed
30 static GdkPixmap *check_pixmap = NULL;
31 static GdkPixmap *check_trans = NULL;
32 static GdkPixmap *blank_pixmap = NULL;
33 static GdkPixmap *blank_trans = NULL;
34 #endif
35          
36 static GtkWidget *restore_dir;        /* current directory edit box */
37 static GtkWidget *scrolled;           /* select files scrolled window */
38 static Window *restore;
39
40 const int NUM_COLUMNS = 7;
41 const int CHECK_COLUMN = 0;
42 const int FILE_COLUMN = 1;
43 const int MODES_COLUMN = 2;
44 const int DATE_COLUMN = 6;
45
46
47 /*
48  * Read Director output and discard it until next prompt
49  */
50 static void discard_to_prompt()
51 {
52    while (bnet_recv(UA_sock) > 0) {
53    }
54 }
55
56 /*
57  * Move up one directory   
58  */
59 void
60 on_restore_up_button_clicked(GtkButton *button, gpointer user_data)
61 {
62    split_path_and_filename(restore->fname, &restore->path, &restore->pnl,
63                               &restore->file, &restore->fnl);
64    FillDirectory(restore->path, restore);
65 }
66
67 static void mark_row(int row, bool mark)
68 {
69    char *file;
70    int len;
71    char new_mark[10];
72
73    gtk_clist_get_text(restore->list, row, FILE_COLUMN, &file);
74    if (mark) {
75       bstrncpy(new_mark, "x", sizeof(new_mark));
76       len = Mmsg(&restore->buf, "mark %s", file);
77    } else {
78       bstrncpy(new_mark, " ", sizeof(new_mark));
79       len = Mmsg(&restore->buf, "unmark %s", file);
80    }
81    gtk_clist_set_text(restore->list, row, CHECK_COLUMN, new_mark);
82     /* strip trailing slash from directory name */
83    while (len > 1 && restore->buf[len-1] == '/') {
84       restore->buf[len-1] = 0;
85    }
86    write_director(restore->buf);
87    discard_to_prompt();
88 }
89
90 void
91 on_restore_add_button_clicked(GtkButton *button, gpointer user_data)
92 {
93    int num_selected = g_list_length(restore->list->selection);
94    int row;
95
96    for (int i=0; i < num_selected; i++) {
97       row = (int)g_list_nth_data(restore->list->selection, i);
98       mark_row(row, true);
99    }
100 }
101
102
103 void
104 on_restore_remove_button_clicked(GtkButton *button, gpointer user_data)
105 {
106    int num_selected = g_list_length(restore->list->selection);
107    int row;
108
109    for (int i=0; i < num_selected; i++) {
110       row = (int)g_list_nth_data(restore->list->selection, i);
111       mark_row(row, false);
112    }
113 }
114
115 /*
116  * Called once at the beginning of the program for setup
117  */
118 void select_restore_setup()
119 {
120    gchar *title[NUM_COLUMNS] = {"Mark", "File", "Mode", "User", "Group", "Size", "Date"};
121
122    restore_file_selection = create_restore_file_selection();
123    if (!restore_file_selection) {
124       Dmsg0(000, "Restore_files not setup.\n");
125    }
126    restore_dir = lookup_widget(restore_file_selection, "restore_dir");
127    scrolled = lookup_widget(restore_file_selection, "scrolled");
128    if (!scrolled) {
129       Dmsg0(000, "Scrolled not setup.\n");
130    }
131
132    restore = new_window();
133
134 #ifdef needed
135    check_pixmap = gdk_pixmap_colormap_create_from_xpm(NULL, 
136                   gdk_colormap_get_system(), &check_trans, NULL,
137                   "check.xpm");
138    blank_pixmap = gdk_pixmap_colormap_create_from_xpm(NULL, 
139                   gdk_colormap_get_system(), &blank_trans, NULL,
140                   "blank.xpm");
141 #endif
142
143    restore->list = (GtkCList *)gtk_clist_new_with_titles(NUM_COLUMNS, title);
144    gtk_clist_set_selection_mode(restore->list, GTK_SELECTION_EXTENDED);
145    gtk_clist_set_sort_column(restore->list, FILE_COLUMN);
146    gtk_clist_set_auto_sort(restore->list, true);
147    gtk_signal_connect(GTK_OBJECT(restore->list), "click_column", 
148                       G_CALLBACK(click_column_cb), restore);
149    gtk_signal_connect(GTK_OBJECT(restore->list), "select_row", 
150                       G_CALLBACK(select_row_cb), restore);
151
152    gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET(restore->list));
153    restore->buf   = get_pool_memory(PM_FNAME);
154    restore->fname = get_pool_memory(PM_FNAME);
155    restore->path  = get_pool_memory(PM_NAME);
156    restore->file  = get_pool_memory(PM_NAME);
157 }
158
159 /*
160  * Select files dialog called
161  */
162 void select_restore_files()
163 {
164    discard_to_prompt();
165    set_restore_dialog_defaults();
166    gtk_widget_show(GTK_WIDGET(restore->list));
167    FillDirectory("/", restore);
168    gtk_main();
169 }
170
171
172 /*
173  * Fill the CList box with files at path
174  */
175 void FillDirectory(char *path, Window *restore)
176 {
177    char pathbuf[MAXSTRING];
178    char modes[20], user[20], group[20], size[20], date[30];
179    char file[1000];
180    char marked[10];
181    gchar *text[NUM_COLUMNS] = {marked, file, modes, user, group, size, date};
182    GtkCList *list = restore->list;
183    int row = 0;
184    
185    stop_director_reader(NULL);         
186    pm_strcpy(&restore->fname, path);
187    gtk_entry_set_text(GTK_ENTRY(restore_dir), restore->fname);
188    gtk_clist_freeze(list);
189    gtk_clist_clear(list); 
190
191    bsnprintf(pathbuf, sizeof(pathbuf), "cd %s", path);
192    Dmsg1(100, "%s\n", pathbuf);
193    write_director(pathbuf);
194    discard_to_prompt();
195
196    write_director("dir");
197    while (bnet_recv(UA_sock) > 0) {
198       char *p = UA_sock->msg;
199       char *l;
200       strip_trailing_junk(UA_sock->msg);
201       if (*p == '$') {
202          break;
203       }
204       Dmsg1(200, "Got: %s\n", p);
205       if (!*p) {
206          continue;
207       }
208       l = p;
209       skip_nonspaces(&p);             /* permissions */
210       *p++ = 0;
211       bstrncpy(modes, l, sizeof(modes));
212       skip_spaces(&p);
213       skip_nonspaces(&p);             /* link count */
214       *p++ = 0;
215       skip_spaces(&p);
216       l = p;
217       skip_nonspaces(&p);             /* user */
218       *p++ = 0;
219       skip_spaces(&p);
220       bstrncpy(user, l, sizeof(user));
221       l = p;
222       skip_nonspaces(&p);             /* group */
223       *p++ = 0;
224       bstrncpy(group, l, sizeof(group));
225       skip_spaces(&p);
226       l = p;
227       skip_nonspaces(&p);             /* size */
228       *p++ = 0;
229       bstrncpy(size, l, sizeof(size));
230       skip_spaces(&p);
231       l = p;
232       skip_nonspaces(&p);             /* date/time */
233       skip_spaces(&p);
234       skip_nonspaces(&p);  
235       *p++ = 0;
236       bstrncpy(date, l, sizeof(date));
237       skip_spaces(&p);
238       if (*p == '*') {
239          bstrncpy(marked, "x", sizeof(marked));
240          p++;
241       } else {
242          bstrncpy(marked, " ", sizeof(marked));
243       }
244       split_path_and_filename(p, &restore->path, &restore->pnl,
245                               &restore->file, &restore->fnl);
246
247 //    Dmsg1(000, "restore->fname=%s\n", restore->fname);
248       bstrncpy(file, restore->file, sizeof(file));
249 //    printf("modes=%s user=%s group=%s size=%s date=%s file=%s\n",
250 //       modes, user, group, size, date, file);
251
252       gtk_clist_append(list, text);
253
254       row++;
255    }
256
257    /* Fix up length of file column */
258    gtk_clist_set_column_width(list, FILE_COLUMN, gtk_clist_optimal_column_width(list, FILE_COLUMN));
259    gtk_clist_set_column_width(list, MODES_COLUMN, gtk_clist_optimal_column_width(list, MODES_COLUMN));
260    gtk_clist_thaw(list);
261    start_director_reader(NULL);
262 }
263
264 Window *new_window()
265 {
266    Window *window = (Window *)malloc(sizeof(Window));
267    memset(window, 0, sizeof(window));
268    return window;
269 }
270
271
272 /*
273  * User clicked a column title
274  */
275 static void click_column_cb(GtkCList *item, gint column, Window *restore)
276 {
277 }
278
279 /*
280  * User selected a row
281  */
282 static void select_row_cb(GtkCList *item, gint row, gint column, 
283              GdkEventButton *event, Window *restore)
284 {
285    char *file;
286    char *marked = NULL;
287    /* Column non-negative => double click */
288    if (column >= 0) {
289       gtk_clist_unselect_row(item, row, column);
290       /* Double click on column 0 means to mark or unmark */
291       if (column == 0) {
292          gtk_clist_get_text(restore->list, row, CHECK_COLUMN, &marked);
293          Dmsg1(200, "Marked=%s\n", marked);
294          if (!marked || strcmp(marked, "x") != 0) {
295             mark_row(row, true);
296          } else {
297             mark_row(row, false);
298          }
299       } else {
300       /* Double clicking on directory means to move to it */
301          int len;
302          gtk_clist_get_text(item, row, FILE_COLUMN, &file);
303          len = strlen(file);
304          if (len > 0 && file[len-1] == '/') {
305             /* Change to new directory */
306             pm_strcpy(&restore->path, restore->fname);
307             if (*file == '*') {
308                Mmsg(&restore->fname, "%s%s", restore->path, file+1);
309             } else {
310                Mmsg(&restore->fname, "%s%s", restore->path, file);
311             }
312             FillDirectory(restore->fname, restore);
313          }
314       }
315    }  
316 }
317
318 /*
319  * CList row is being destroyed get rid of our data
320  */
321 void row_data_destroy_cb(gpointer data)
322 {
323    if (data) {
324       free(data);
325    }
326 }
327
328 #ifdef xxx
329    GdkPixmap *pixmap, *trans;
330          utf8_mark = g_locale_to_utf8(new_mark, -1, NULL, NULL, NULL);
331          gtk_clist_get_pixmap(restore->list, row, CHECK_COLUMN, &pixmap, &trans);
332          if (pixmap == blank_pixmap) {
333             bstrncpy(new_mark, "x", sizeof(new_mark));
334 //          gtk_clist_set_pixmap(item, row, CHECK_COLUMN, check_pixmap, check_trans);
335 #endif
336 #ifdef xxx
337 static void window_delete_cb(GtkWidget *item, GdkEvent *event, Window *restore)
338 {
339    gtk_widget_destroy(restore->window);
340    gtk_main_quit();
341    free_pool_memory(restore->buf);
342    free_pool_memory(restore->fname);
343    free_pool_memory(restore->path);
344    free_pool_memory(restore->file);
345    free(restore);
346 }
347 #endif
348 #ifdef xxx
349       if (marked) {
350          gtk_clist_set_pixmap(list, row, CHECK_COLUMN, check_pixmap, check_trans);
351       } else {
352          gtk_clist_set_pixmap(list, row, CHECK_COLUMN, blank_pixmap, blank_trans);
353       }
354 #endif