r3313: Updated French manual translation (Arnaud Calvo).
[rox-filer.git] / ROX-Filer / src / infobox.c
blobdc2f46cd80a36d8453c2b85a17bf5aab31410e8b
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* infobox.c - code for showing a file's attributes */
24 #include "config.h"
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/param.h>
30 #include <signal.h>
31 #include <libxml/parser.h>
33 #include <gtk/gtk.h>
35 #include "global.h"
37 #include "support.h"
38 #include "main.h"
39 #include "gui_support.h"
40 #include "diritem.h"
41 #include "type.h"
42 #include "infobox.h"
43 #include "appinfo.h"
44 #include "dnd.h" /* For xa_string */
45 #include "run.h" /* For show_help_files() */
46 #include "xml.h"
47 #include "mount.h"
48 #include "pixmaps.h"
50 typedef struct _FileStatus FileStatus;
52 /* This is for the 'file(1) says...' thing */
53 struct _FileStatus
55 int fd; /* FD to read from, -1 if closed */
56 int input; /* Input watcher tag if fd valid */
57 GtkLabel *label; /* Widget to output to */
58 gchar *text; /* String so far */
61 typedef struct du {
62 gchar *path;
63 GtkListStore *store;
64 guint watch;
65 GIOChannel *chan;
66 gint child;
67 } DU;
69 typedef struct _Permissions Permissions;
71 struct _Permissions
73 gchar *path;
74 DirItem *item;
75 GtkWidget *bits[12];
78 /* Static prototypes */
79 static void refresh_info(GObject *window);
80 static GtkWidget *make_vbox(const guchar *path, GObject *window);
81 static GtkWidget *make_details(const guchar *path, DirItem *item,
82 GObject *window);
83 static GtkWidget *make_about(const guchar *path, XMLwrapper *ai);
84 static GtkWidget *make_file_says(const guchar *path);
85 static GtkWidget *make_permissions(const gchar *path, DirItem *item);
86 static void add_file_output(FileStatus *fs,
87 gint source, GdkInputCondition condition);
88 static const gchar *pretty_type(DirItem *file, const guchar *path);
89 static void got_response(GObject *window, gint response, gpointer data);
90 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs);
92 /****************************************************************
93 * EXTERNAL INTERFACE *
94 ****************************************************************/
96 /* Open each item in a new infobox. Confirms if there are a large
97 * number of items to show.
99 void infobox_show_list(GList *paths)
101 int n;
103 n = g_list_length(paths);
105 if (n >= 10)
107 gchar *message;
108 gboolean ok;
110 message = g_strdup_printf(
111 _("Are you sure you want to open %d windows?"), n);
112 ok = confirm(message, GTK_STOCK_YES, _("Show Info"));
113 g_free(message);
114 if (!ok)
115 return;
118 g_list_foreach(paths, (GFunc) infobox_new, NULL);
121 /* Create and display a new info box showing details about this item */
122 void infobox_new(const gchar *pathname)
124 GtkWidget *window, *details;
125 gchar *path;
126 GObject *owindow;
128 g_return_if_fail(pathname != NULL);
130 path = g_strdup(pathname); /* Gets attached to window & freed later */
132 window = gtk_dialog_new_with_buttons(
133 g_utf8_validate(path, -1, NULL) ? path
134 : _("(bad utf-8)"),
135 NULL, GTK_DIALOG_NO_SEPARATOR,
136 GTK_STOCK_CANCEL, GTK_RESPONSE_DELETE_EVENT,
137 GTK_STOCK_REFRESH, GTK_RESPONSE_APPLY,
138 NULL);
140 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
142 owindow = G_OBJECT(window);
143 details = make_vbox(path, owindow);
144 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(window)->vbox),
145 details);
147 g_object_set_data(owindow, "details", details);
148 g_object_set_data_full(owindow, "path", path, g_free);
150 g_signal_connect(window, "response", G_CALLBACK(got_response), NULL);
152 number_of_windows++;
153 gtk_widget_show_all(window);
156 /****************************************************************
157 * INTERNAL FUNCTIONS *
158 ****************************************************************/
160 static void got_response(GObject *window, gint response, gpointer data)
162 if (response == GTK_RESPONSE_APPLY)
163 refresh_info(window);
164 else
166 gtk_widget_destroy(GTK_WIDGET(window));
167 one_less_window();
171 static void refresh_info(GObject *window)
173 GtkWidget *details, *vbox;
174 guchar *path;
176 path = g_object_get_data(window, "path");
177 details = g_object_get_data(window, "details");
178 g_return_if_fail(details != NULL);
179 g_return_if_fail(path != NULL);
181 vbox = details->parent;
182 gtk_widget_destroy(details);
184 details = make_vbox(path, window);
185 g_object_set_data(window, "details", details);
186 gtk_box_pack_start_defaults(GTK_BOX(vbox), details);
187 gtk_widget_show_all(details);
190 static void add_frame(GtkBox *vbox, GtkWidget *list)
192 GtkWidget *frame;
194 frame = gtk_frame_new(NULL);
195 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
196 gtk_container_add(GTK_CONTAINER(frame), list);
197 gtk_box_pack_start_defaults(vbox, frame);
200 /* Create the VBox widget that contains the details.
201 * Note that 'path' must not be freed until the vbox is destroyed.
203 static GtkWidget *make_vbox(const guchar *path, GObject *window)
205 DirItem *item;
206 GtkBox *vbox;
207 XMLwrapper *ai;
208 xmlNode *about = NULL;
209 gchar *help_dir;
210 GtkWidget *hbox, *name, *label;
212 g_return_val_if_fail(path[0] == '/', NULL);
214 item = diritem_new(g_basename(path));
215 diritem_restat(path, item, NULL);
217 ai = appinfo_get(path, item);
218 if (ai)
219 about = xml_get_section(ai, NULL, "About");
221 vbox = GTK_BOX(gtk_vbox_new(FALSE, 4));
222 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
224 /* Heading, with icon and name */
225 hbox = gtk_hbox_new(FALSE, 4);
226 gtk_box_pack_start(vbox, hbox, FALSE, TRUE, 0);
227 gtk_box_pack_start(GTK_BOX(hbox),
228 gtk_image_new_from_pixbuf(item->image->pixbuf),
229 FALSE, FALSE, 4);
231 if (g_utf8_validate(item->leafname, -1, NULL))
232 name = gtk_label_new(item->leafname);
233 else
235 guchar *u8;
237 u8 = to_utf8(item->leafname);
238 name = gtk_label_new(u8);
239 g_free(u8);
241 gtk_label_set_selectable(GTK_LABEL(name), TRUE);
242 gtk_box_pack_start(GTK_BOX(hbox), name, FALSE, TRUE, 4);
244 /* Make the name bolder and larger */
246 PangoAttribute *attr;
247 PangoAttrList *list;
249 list = pango_attr_list_new();
251 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
252 attr->start_index = 0;
253 attr->end_index = -1;
254 pango_attr_list_insert(list, attr);
256 attr = pango_attr_scale_new(PANGO_SCALE_X_LARGE);
257 attr->start_index = 0;
258 attr->end_index = -1;
259 pango_attr_list_insert(list, attr);
261 gtk_label_set_attributes(GTK_LABEL(name), list);
264 /* List of file attributes */
265 add_frame(vbox, make_details(path, item, window));
267 help_dir = g_strconcat(path, "/Help", NULL);
269 if (access(help_dir, F_OK) == 0)
271 GtkWidget *button, *align;
273 align = gtk_alignment_new(0.5, 0.5, 0, 0);
275 button = button_new_mixed(GTK_STOCK_JUMP_TO,
276 _("Show _Help Files"));
277 gtk_box_pack_start(vbox, align, FALSE, TRUE, 0);
278 gtk_container_add(GTK_CONTAINER(align), button);
279 g_signal_connect_swapped(button, "clicked",
280 G_CALLBACK(show_help_files),
281 (gpointer) path);
283 g_free(help_dir);
285 label = gtk_label_new(NULL);
286 gtk_label_set_markup(GTK_LABEL(label), _("<b>Permissions</b>"));
287 gtk_misc_set_alignment(GTK_MISC(label), 0, 1);
288 gtk_box_pack_start(vbox, label, FALSE, TRUE, 2);
290 gtk_box_pack_start(vbox, make_permissions(path, item),
291 FALSE, TRUE, 0);
293 if (about)
294 add_frame(vbox, make_about(path, ai));
295 else if (item->base_type == TYPE_FILE)
297 label = gtk_label_new(NULL);
298 gtk_label_set_markup(GTK_LABEL(label),
299 _("<b>Contents indicate...</b>"));
300 gtk_misc_set_alignment(GTK_MISC(label), 0, 1);
301 gtk_box_pack_start(vbox, label, FALSE, TRUE, 2);
303 gtk_box_pack_start_defaults(vbox, make_file_says(path));
306 if (ai)
307 g_object_unref(ai);
309 diritem_free(item);
311 return (GtkWidget *) vbox;
314 /* The selection has changed - grab or release the primary selection */
315 static void set_selection(GtkTreeView *view, gpointer data)
317 static GtkClipboard *primary = NULL;
318 GtkTreeModel *model;
319 GtkTreePath *path = NULL;
320 GtkTreeIter iter;
321 gchar *text;
323 gtk_tree_view_get_cursor(view, &path, NULL);
324 if (!path)
325 return;
327 if (!primary)
328 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
330 model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
332 gtk_tree_model_get_iter(model, &iter, path);
333 gtk_tree_path_free(path);
335 gtk_tree_model_get(model, &iter, 1, &text, -1);
337 gtk_clipboard_set_text(primary, text, -1);
339 g_free(text);
342 /* Returns a GtkTreePath for the item */
343 static const gchar *add_row(GtkListStore *store, const gchar *label,
344 const gchar *data)
346 GtkTreeIter iter;
347 gchar *u8 = NULL;
348 GtkTreePath *tpath;
349 static gchar *last = NULL;
351 if (!g_utf8_validate(data, -1, NULL))
352 u8 = to_utf8(data);
354 gtk_list_store_append(store, &iter);
355 gtk_list_store_set(store, &iter, 0, label, 1, u8 ? u8 : data, -1);
357 g_free(u8);
359 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
360 if (last)
361 g_free(last);
362 last = gtk_tree_path_to_string(tpath);
363 gtk_tree_path_free(tpath);
365 return last;
368 static void add_row_and_free(GtkListStore *store,
369 const gchar *label, gchar *data)
371 add_row(store, label, data);
372 g_free(data);
375 /* Create an empty list view, ready to place some data in */
376 static void make_list(GtkListStore **list_store, GtkWidget **list_view,
377 GCallback cell_edited)
379 GtkListStore *store;
380 GtkTreeView *view;
381 GtkCellRenderer *cell_renderer;
383 /* Field name, value, editable */
384 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING,
385 G_TYPE_BOOLEAN);
386 view = GTK_TREE_VIEW(
387 gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
388 g_object_unref(G_OBJECT(store));
389 gtk_tree_view_set_headers_visible(view, FALSE);
391 cell_renderer = gtk_cell_renderer_text_new();
392 g_object_set(G_OBJECT(cell_renderer), "xalign", 1.0, NULL);
393 gtk_tree_view_insert_column_with_attributes(view,
394 0, NULL, cell_renderer, "text", 0, NULL);
396 cell_renderer = gtk_cell_renderer_text_new();
397 gtk_tree_view_insert_column_with_attributes(view,
398 1, NULL, cell_renderer, "text", 1, "editable", 2, NULL);
400 if (cell_edited) {
401 g_signal_connect(G_OBJECT(cell_renderer), "edited",
402 G_CALLBACK(cell_edited), store);
405 g_signal_connect(view, "cursor_changed",
406 G_CALLBACK(set_selection), NULL);
408 *list_store = store;
409 *list_view = (GtkWidget *) view;
412 static void set_cell(GtkListStore *store, const gchar *path,
413 const gchar *ctext)
415 GtkTreeIter iter;
417 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store),
418 &iter, path);
419 gtk_list_store_set(store, &iter, 1, ctext, -1);
422 static void insert_size(DU *du, const char *line)
424 off_t size;
425 gchar *cell;
427 #ifdef LARGE_FILE_SUPPORT
428 size = strtoll(line, NULL, 10);
429 #else
430 size = strtol(line, NULL, 10);
431 #endif
432 size <<= 10; /* Because du reports in K */
433 cell = (size >= PRETTY_SIZE_LIMIT)
434 ? g_strdup_printf("%s (%" SIZE_FMT " %s)",
435 format_size(size),
436 size, _("bytes"))
437 : g_strdup(format_size(size));
439 set_cell(du->store, du->path, cell);
441 g_free(cell);
444 static gboolean read_du_output(GIOChannel *source, GIOCondition cond, DU *du)
446 GString *line;
447 GIOStatus stat;
448 GError *err = NULL;
450 line = g_string_new("");
451 stat = g_io_channel_read_line_string(source, line, NULL, &err);
452 switch (stat)
454 case G_IO_STATUS_NORMAL:
455 insert_size(du, line->str);
456 break;
457 case G_IO_STATUS_EOF:
458 break;
459 case G_IO_STATUS_AGAIN:
460 g_string_free(line, TRUE);
461 return TRUE;
462 case G_IO_STATUS_ERROR:
463 set_cell(du->store, du->path, err->message);
464 break;
466 g_string_free(line, TRUE);
468 return FALSE;
471 static void kill_du_output(GtkWidget *widget, DU *du)
473 g_io_channel_shutdown(du->chan, FALSE, NULL);
474 g_io_channel_unref(du->chan);
475 kill((pid_t) du->child, SIGTERM);
476 g_object_unref(G_OBJECT(du->store));
477 g_free(du->path);
478 g_free(du);
481 static gboolean refresh_info_idle(gpointer data)
483 GObject *window = G_OBJECT(data);
485 refresh_info(window);
486 g_object_unref(window);
487 return FALSE;
490 static void cell_edited(GtkCellRendererText *cell,
491 const gchar *path_string,
492 const gchar *new_text,
493 gpointer data)
495 GtkTreeModel *model = (GtkTreeModel *) data;
496 GtkTreePath *path;
497 GtkTreeIter iter;
498 GObject *window;
499 const char *fullpath;
500 char *oldlink;
502 window = g_object_get_data(G_OBJECT(model), "rox_window");
503 g_return_if_fail(window != NULL);
505 fullpath = g_object_get_data(window, "path");
506 g_return_if_fail(fullpath != NULL);
508 path = gtk_tree_path_new_from_string(path_string);
509 gtk_tree_model_get_iter(model, &iter, path);
510 gtk_tree_path_free(path);
512 oldlink = readlink_dup(fullpath);
513 if (!oldlink) {
514 report_error(_("'%s' is no longer a symlink"), fullpath);
515 return;
517 if (strcmp(oldlink, new_text) == 0)
518 return; /* No change */
519 g_free(oldlink);
520 if (unlink(fullpath)) {
521 report_error(_("Failed to unlink '%s':\n%s"),
522 fullpath, g_strerror(errno));
523 return;
525 if (symlink(new_text, fullpath)) {
526 report_error(_("Failed to create symlink from '%s':\n%s\n"
527 "(note: old link has been deleted)"));
528 return;
531 g_object_ref(window);
532 gtk_idle_add(refresh_info_idle, window);
535 /* Create the TreeView widget with the file's details */
536 static GtkWidget *make_details(const guchar *path, DirItem *item,
537 GObject *window)
539 GtkListStore *store;
540 GtkWidget *view;
541 gchar *tmp, *tmp2;
543 make_list(&store, &view, G_CALLBACK(cell_edited));
544 g_object_set_data(G_OBJECT(store), "rox_window", window);
546 /* For a symlink to an error, don't show the error */
547 if (item->base_type == TYPE_ERROR && item->lstat_errno)
549 add_row(store, _("Error:"), g_strerror(item->lstat_errno));
550 return view;
553 tmp = g_path_get_dirname(path);
554 tmp2 = pathdup(tmp);
555 if (strcmp(tmp, tmp2) != 0)
556 add_row_and_free(store, _("Real directory:"), tmp2);
557 g_free(tmp);
559 add_row_and_free(store, _("Owner, Group:"),
560 g_strdup_printf("%s, %s",
561 user_name(item->uid),
562 group_name(item->gid)));
564 if (item->base_type != TYPE_DIRECTORY)
566 add_row_and_free(store, _("Size:"),
567 item->size >= PRETTY_SIZE_LIMIT
568 ? g_strdup_printf("%s (%" SIZE_FMT " %s)",
569 format_size(item->size),
570 item->size, _("bytes"))
571 : g_strdup(format_size(item->size)));
573 else
575 DU *du;
576 int out;
577 gchar *args[] = {"du", "-sk", "", NULL};
579 du = g_new(DU, 1);
580 du->store = store;
581 du->path = g_strdup(add_row(store, _("Size:"), _("Scanning")));
583 args[2] = (gchar *) path;
584 if (g_spawn_async_with_pipes(NULL, args, NULL,
585 G_SPAWN_SEARCH_PATH,
586 NULL, NULL, &du->child,
587 NULL, &out, NULL,
588 NULL))
590 du->chan = g_io_channel_unix_new(out);
591 du->watch = g_io_add_watch(du->chan, G_IO_IN,
592 (GIOFunc) read_du_output, du);
593 g_object_ref(G_OBJECT(du->store));
594 g_signal_connect(G_OBJECT(view), "destroy",
595 G_CALLBACK(kill_du_output), du);
597 else
599 set_cell(store, du->path, _("Failed to scan"));
600 g_free(du->path);
601 g_free(du);
605 add_row_and_free(store, _("Change time:"), pretty_time(&item->ctime));
607 add_row_and_free(store, _("Modify time:"), pretty_time(&item->mtime));
609 add_row_and_free(store, _("Access time:"), pretty_time(&item->atime));
611 add_row(store, _("Type:"), pretty_type(item, path));
613 if (item->mime_type)
614 add_row(store, "", mime_type_comment(item->mime_type));
616 if (item->flags & ITEM_FLAG_SYMLINK)
618 GtkTreeIter iter;
619 GtkTreeModel *model = GTK_TREE_MODEL(store);
620 char *target;
622 target = readlink_dup(path);
623 if (!target)
624 target = g_strdup(g_strerror(errno));
625 add_row_and_free(store, _("Link target:"), target);
627 /* Make cell editable */
628 gtk_tree_model_iter_nth_child(model, &iter,
629 NULL, gtk_tree_model_iter_n_children(model, NULL) - 1);
631 gtk_list_store_set(store, &iter, 2, TRUE, -1);
634 if (item->base_type != TYPE_DIRECTORY)
635 add_row_and_free(store, _("Run action:"),
636 describe_current_command(item->mime_type));
638 return view;
641 /* Create the TreeView widget with the application's details */
642 static GtkWidget *make_about(const guchar *path, XMLwrapper *ai)
644 GtkListStore *store;
645 GtkWidget *view;
646 xmlNode *prop;
647 xmlNode *about, *about_trans;
648 GHashTable *translate;
650 g_return_val_if_fail(ai != NULL, NULL);
652 about_trans = xml_get_section(ai, NULL, "About");
654 about = xmlDocGetRootElement(ai->doc)->xmlChildrenNode;
655 for (; about; about = about->next)
657 if (about->type != XML_ELEMENT_NODE)
658 continue;
659 if (about->ns == NULL && strcmp(about->name, "About") == 0)
660 break;
663 g_return_val_if_fail(about != NULL, NULL);
665 make_list(&store, &view, NULL);
667 /* Add each field in about to the list, but overriding each element
668 * with about_trans if a translation is supplied.
670 translate = g_hash_table_new(g_str_hash, g_str_equal);
671 if (about_trans != about)
673 xmlNode *p;
674 for (p = about_trans->xmlChildrenNode; p; p = p->next)
676 if (p->type != XML_ELEMENT_NODE)
677 continue;
678 g_hash_table_insert(translate, (char *) p->name, p);
681 for (prop = about->xmlChildrenNode; prop; prop = prop->next)
683 if (prop->type == XML_ELEMENT_NODE)
685 char *label = NULL;
686 char *value = NULL;
687 char *tmp = NULL;
688 xmlNode *trans;
690 trans = g_hash_table_lookup(translate, prop->name);
691 if (!trans)
692 trans = prop;
694 tmp = xmlGetProp(trans, "label");
695 label = g_strconcat(tmp ? tmp
696 : (char *) trans->name,
697 ":", NULL);
698 g_free(tmp);
699 value = xmlNodeListGetString(trans->doc,
700 trans->xmlChildrenNode, 1);
701 if (!value)
702 value = xmlNodeListGetString(prop->doc,
703 prop->xmlChildrenNode, 1);
704 if (!value)
705 value = g_strdup("-");
706 add_row_and_free(store, label, value);
707 g_free(label);
711 g_hash_table_destroy(translate);
713 return view;
716 static GtkWidget *make_file_says(const guchar *path)
718 GtkWidget *w_file_label;
719 GtkLabel *l_file_label;
720 int file_data[2];
721 char *argv[] = {"file", "-b", NULL, NULL};
722 FileStatus *fs = NULL;
723 guchar *tmp;
725 w_file_label = gtk_label_new(_("<nothing yet>"));
726 l_file_label = GTK_LABEL(w_file_label);
727 gtk_label_set_line_wrap(l_file_label, TRUE);
729 if (pipe(file_data))
731 tmp = g_strdup_printf("pipe(): %s", g_strerror(errno));
732 gtk_label_set_text(l_file_label, tmp);
733 g_free(tmp);
734 return w_file_label;
737 switch (fork())
739 case -1:
740 tmp = g_strdup_printf("pipe(): %s", g_strerror(errno));
741 gtk_label_set_text(l_file_label, tmp);
742 g_free(tmp);
743 close(file_data[0]);
744 close(file_data[1]);
745 break;
746 case 0:
747 /* We are the child */
748 close(file_data[0]);
749 dup2(file_data[1], STDOUT_FILENO);
750 dup2(file_data[1], STDERR_FILENO);
751 #ifdef FILE_B_FLAG
752 argv[2] = (char *) path;
753 #else
754 argv[1] = (char *) g_basename(path);
755 chdir(g_path_get_dirname(path));
756 #endif
757 if (execvp(argv[0], argv))
758 fprintf(stderr, "execvp() error: %s\n",
759 g_strerror(errno));
760 _exit(0);
761 default:
762 /* We are the parent */
763 close(file_data[1]);
764 fs = g_new(FileStatus, 1);
765 fs->label = l_file_label;
766 fs->fd = file_data[0];
767 fs->text = g_strdup("");
768 fs->input = gtk_input_add_full(fs->fd, GDK_INPUT_READ,
769 (GdkInputFunction) add_file_output,
770 NULL, fs, NULL);
771 g_signal_connect(w_file_label, "destroy",
772 G_CALLBACK(file_info_destroyed), fs);
773 break;
776 return w_file_label;
779 /* Got some data from file(1) - stick it in the window. */
780 static void add_file_output(FileStatus *fs,
781 gint source, GdkInputCondition condition)
783 char buffer[20];
784 char *str;
785 int got;
787 got = read(source, buffer, sizeof(buffer) - 1);
788 if (got <= 0)
790 int err = errno;
791 gtk_input_remove(fs->input);
792 close(source);
793 fs->fd = -1;
794 if (got < 0)
795 delayed_error(_("file(1) says... %s"),
796 g_strerror(err));
797 return;
799 buffer[got] = '\0';
801 str = g_strconcat(fs->text, buffer, NULL);
802 g_free(fs->text);
803 fs->text = str;
805 str = to_utf8(fs->text);
806 g_strstrip(str);
807 gtk_label_set_text(fs->label, str);
808 g_free(str);
811 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
813 if (fs->fd != -1)
815 gtk_input_remove(fs->input);
816 close(fs->fd);
819 g_free(fs->text);
820 g_free(fs);
823 static void permissions_destroyed(GtkWidget *widget, Permissions *perm)
825 g_free(perm->path);
826 diritem_free(perm->item);
828 g_free(perm);
831 static void permissions_apply(GtkWidget *widget, Permissions *perm)
833 mode_t nmode;
834 int i;
836 nmode=0;
838 for (i = 0; i < 9; i++)
840 GtkToggleButton *bit = GTK_TOGGLE_BUTTON(perm->bits[i]);
841 if (gtk_toggle_button_get_active(bit))
842 nmode |= 1 << i;
844 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[9])))
845 nmode |= S_ISUID;
846 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[10])))
847 nmode |= S_ISGID;
848 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[11])))
849 nmode |= S_ISVTX;
851 if (chmod(perm->path, nmode))
852 report_error(_("Could not change permissions: %s"),
853 g_strerror(errno));
856 static GtkWidget *make_permissions(const gchar *path, DirItem *item)
858 Permissions *perm;
859 GtkWidget *table;
860 GtkWidget *tick, *label;
861 int i, x, y;
863 perm = g_new(Permissions, 1);
865 perm->path = g_strdup(path);
866 perm->item = diritem_new(path);
868 table = gtk_table_new(4, 5, TRUE);
870 label = gtk_label_new(_("Owner"));
871 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
872 label = gtk_label_new(_("Group"));
873 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
874 label = gtk_label_new(_("World"));
875 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
877 label = gtk_label_new(_("Read"));
878 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
879 label = gtk_label_new(_("Write"));
880 gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 0, 1);
881 label = gtk_label_new(_("Exec"));
882 gtk_table_attach_defaults(GTK_TABLE(table), label, 3, 4, 0, 1);
884 for (i = 0; i < 9; i++)
886 x = 1 + 2 - i % 3;
887 y = 1 + 2 - i / 3;
888 perm->bits[i] = tick = gtk_check_button_new();
889 gtk_table_attach_defaults(GTK_TABLE(table), tick,
890 x, x + 1, y, y + 1);
891 if (item->mode & (1 << i))
892 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick),
893 TRUE);
894 g_signal_connect(tick, "toggled",
895 G_CALLBACK(permissions_apply), perm);
898 tick = gtk_check_button_new_with_label(_("SUID"));
899 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 1, 2);
900 if (item->mode & S_ISUID)
901 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
902 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
903 perm->bits[9] = tick;
905 tick = gtk_check_button_new_with_label(_("SGID"));
906 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 2, 3);
907 if (item->mode & S_ISGID)
908 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
909 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
910 perm->bits[10] = tick;
912 tick = gtk_check_button_new_with_label(_("Sticky"));
913 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 3, 4);
914 if (item->mode & S_ISVTX)
915 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
916 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
917 perm->bits[11] = tick;
919 g_signal_connect(table, "destroy",
920 G_CALLBACK(permissions_destroyed), perm);
922 gtk_widget_show_all(table);
923 return table;
926 /* Don't g_free() the result */
927 static const gchar *pretty_type(DirItem *file, const guchar *path)
929 static gchar *text = NULL;
931 null_g_free(&text);
933 if (file->flags & ITEM_FLAG_SYMLINK)
934 return _("Symbolic link");
936 if (file->flags & ITEM_FLAG_APPDIR)
937 return _("ROX application");
939 if (file->flags & ITEM_FLAG_MOUNT_POINT)
941 MountPoint *mp;
942 const gchar *mounted;
944 mounted = mount_is_mounted(path, NULL, NULL)
945 ? _("mounted") : _("unmounted");
947 mp = g_hash_table_lookup(fstab_mounts, path);
948 if (mp)
949 text = g_strdup_printf(_("Mount point for %s (%s)"),
950 mp->name, mounted);
951 else
952 text = g_strdup_printf(_("Mount point (%s)"), mounted);
953 return text;
956 if (file->mime_type)
958 text = g_strconcat(file->mime_type->media_type, "/",
959 file->mime_type->subtype, NULL);
960 return text;
963 return "-";