2 * Handle console GUI selection of files
3 * Kern Sibbald, March, 2004
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);
19 /* Forward referenced subroutines */
20 void FillDirectory(char *path, Window *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);
30 static GdkPixmap *check_pixmap = NULL;
31 static GdkPixmap *check_trans = NULL;
32 static GdkPixmap *blank_pixmap = NULL;
33 static GdkPixmap *blank_trans = NULL;
36 static GtkWidget *restore_dir; /* current directory edit box */
37 static GtkWidget *scrolled; /* select files scrolled window */
38 static Window *restore;
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;
48 * Read Director output and discard it until next prompt
50 static void discard_to_prompt()
52 while (bnet_recv(UA_sock) > 0) {
53 set_text(UA_sock->msg, UA_sock->msglen);
58 * Move up one directory
61 on_restore_up_button_clicked(GtkButton *button, gpointer user_data)
63 split_path_and_filename(restore->fname, &restore->path, &restore->pnl,
64 &restore->file, &restore->fnl);
65 FillDirectory(restore->path, restore);
68 static void mark_row(int row, bool mark)
74 gtk_clist_get_text(restore->list, row, FILE_COLUMN, &file);
76 bstrncpy(new_mark, "x", sizeof(new_mark));
77 len = Mmsg(&restore->buf, "mark %s", file);
79 bstrncpy(new_mark, " ", sizeof(new_mark));
80 len = Mmsg(&restore->buf, "unmark %s", file);
82 gtk_clist_set_text(restore->list, row, CHECK_COLUMN, new_mark);
83 /* strip trailing slash from directory name */
84 while (len > 1 && restore->buf[len-1] == '/') {
85 restore->buf[len-1] = 0;
87 write_director(restore->buf);
92 on_restore_add_button_clicked(GtkButton *button, gpointer user_data)
94 int num_selected = g_list_length(restore->list->selection);
97 for (int i=0; i < num_selected; i++) {
98 row = (int)g_list_nth_data(restore->list->selection, i);
105 on_restore_remove_button_clicked(GtkButton *button, gpointer user_data)
107 int num_selected = g_list_length(restore->list->selection);
110 for (int i=0; i < num_selected; i++) {
111 row = (int)g_list_nth_data(restore->list->selection, i);
112 mark_row(row, false);
117 * Called once at the beginning of the program for setup
119 void select_restore_setup()
121 gchar *title[NUM_COLUMNS] = {"Mark", "File", "Mode", "User", "Group", "Size", "Date"};
123 restore_file_selection = create_restore_file_selection();
124 if (!restore_file_selection) {
125 Dmsg0(000, "Restore_files not setup.\n");
127 restore_dir = lookup_widget(restore_file_selection, "restore_dir");
128 scrolled = lookup_widget(restore_file_selection, "scrolled");
130 Dmsg0(000, "Scrolled not setup.\n");
133 restore = new_window();
136 check_pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
137 gdk_colormap_get_system(), &check_trans, NULL,
139 blank_pixmap = gdk_pixmap_colormap_create_from_xpm(NULL,
140 gdk_colormap_get_system(), &blank_trans, NULL,
144 restore->list = (GtkCList *)gtk_clist_new_with_titles(NUM_COLUMNS, title);
145 gtk_clist_set_selection_mode(restore->list, GTK_SELECTION_EXTENDED);
146 gtk_clist_set_sort_column(restore->list, FILE_COLUMN);
147 gtk_clist_set_auto_sort(restore->list, true);
148 gtk_signal_connect(GTK_OBJECT(restore->list), "click_column",
149 G_CALLBACK(click_column_cb), restore);
150 gtk_signal_connect(GTK_OBJECT(restore->list), "select_row",
151 G_CALLBACK(select_row_cb), restore);
153 gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET(restore->list));
154 restore->buf = get_pool_memory(PM_FNAME);
155 restore->fname = get_pool_memory(PM_FNAME);
156 restore->path = get_pool_memory(PM_NAME);
157 restore->file = get_pool_memory(PM_NAME);
161 * Select files dialog called
163 void select_restore_files()
166 set_restore_dialog_defaults();
167 gtk_widget_show(GTK_WIDGET(restore->list));
168 FillDirectory("/", restore);
174 * Fill the CList box with files at path
176 void FillDirectory(char *path, Window *restore)
178 char pathbuf[MAXSTRING];
179 char modes[20], user[20], group[20], size[20], date[30];
182 gchar *text[NUM_COLUMNS] = {marked, file, modes, user, group, size, date};
183 GtkCList *list = restore->list;
186 stop_director_reader(NULL);
187 pm_strcpy(&restore->fname, path);
188 gtk_entry_set_text(GTK_ENTRY(restore_dir), restore->fname);
189 gtk_clist_freeze(list);
190 gtk_clist_clear(list);
192 bsnprintf(pathbuf, sizeof(pathbuf), "cd %s", path);
193 Dmsg1(100, "%s\n", pathbuf);
194 write_director(pathbuf);
197 write_director("dir");
198 while (bnet_recv(UA_sock) > 0) {
199 char *p = UA_sock->msg;
201 strip_trailing_junk(UA_sock->msg);
205 Dmsg1(200, "Got: %s\n", p);
210 skip_nonspaces(&p); /* permissions */
212 bstrncpy(modes, l, sizeof(modes));
214 skip_nonspaces(&p); /* link count */
218 skip_nonspaces(&p); /* user */
221 bstrncpy(user, l, sizeof(user));
223 skip_nonspaces(&p); /* group */
225 bstrncpy(group, l, sizeof(group));
228 skip_nonspaces(&p); /* size */
230 bstrncpy(size, l, sizeof(size));
233 skip_nonspaces(&p); /* date/time */
237 bstrncpy(date, l, sizeof(date));
240 bstrncpy(marked, "x", sizeof(marked));
243 bstrncpy(marked, " ", sizeof(marked));
245 split_path_and_filename(p, &restore->path, &restore->pnl,
246 &restore->file, &restore->fnl);
248 // Dmsg1(000, "restore->fname=%s\n", restore->fname);
249 bstrncpy(file, restore->file, sizeof(file));
250 // printf("modes=%s user=%s group=%s size=%s date=%s file=%s\n",
251 // modes, user, group, size, date, file);
253 gtk_clist_append(list, text);
258 /* Fix up length of file column */
259 gtk_clist_set_column_width(list, FILE_COLUMN, gtk_clist_optimal_column_width(list, FILE_COLUMN));
260 gtk_clist_set_column_width(list, MODES_COLUMN, gtk_clist_optimal_column_width(list, MODES_COLUMN));
261 gtk_clist_thaw(list);
262 start_director_reader(NULL);
267 Window *window = (Window *)malloc(sizeof(Window));
268 memset(window, 0, sizeof(window));
274 * User clicked a column title
276 static void click_column_cb(GtkCList *item, gint column, Window *restore)
281 * User selected a row
283 static void select_row_cb(GtkCList *item, gint row, gint column,
284 GdkEventButton *event, Window *restore)
288 /* Column non-negative => double click */
290 gtk_clist_unselect_row(item, row, column);
291 /* Double click on column 0 means to mark or unmark */
293 gtk_clist_get_text(restore->list, row, CHECK_COLUMN, &marked);
294 Dmsg1(200, "Marked=%s\n", marked);
295 if (!marked || strcmp(marked, "x") != 0) {
298 mark_row(row, false);
301 /* Double clicking on directory means to move to it */
303 gtk_clist_get_text(item, row, FILE_COLUMN, &file);
305 if (len > 0 && file[len-1] == '/') {
306 /* Change to new directory */
307 pm_strcpy(&restore->path, restore->fname);
309 Mmsg(&restore->fname, "%s%s", restore->path, file+1);
311 Mmsg(&restore->fname, "%s%s", restore->path, file);
313 FillDirectory(restore->fname, restore);
320 * CList row is being destroyed get rid of our data
322 void row_data_destroy_cb(gpointer data)
330 GdkPixmap *pixmap, *trans;
331 utf8_mark = g_locale_to_utf8(new_mark, -1, NULL, NULL, NULL);
332 gtk_clist_get_pixmap(restore->list, row, CHECK_COLUMN, &pixmap, &trans);
333 if (pixmap == blank_pixmap) {
334 bstrncpy(new_mark, "x", sizeof(new_mark));
335 // gtk_clist_set_pixmap(item, row, CHECK_COLUMN, check_pixmap, check_trans);
338 static void window_delete_cb(GtkWidget *item, GdkEvent *event, Window *restore)
340 gtk_widget_destroy(restore->window);
342 free_pool_memory(restore->buf);
343 free_pool_memory(restore->fname);
344 free_pool_memory(restore->path);
345 free_pool_memory(restore->file);
351 gtk_clist_set_pixmap(list, row, CHECK_COLUMN, check_pixmap, check_trans);
353 gtk_clist_set_pixmap(list, row, CHECK_COLUMN, blank_pixmap, blank_trans);