Italian translation update.
[rox-filer.git] / ROX-Filer / src / infobox.c
blob954b833855b73faa483cc514e737015cc1fc15a6
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* infobox.c - code for showing a file's attributes */
22 #include "config.h"
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/param.h>
28 #include <signal.h>
29 #include <libxml/parser.h>
31 #include <gtk/gtk.h>
33 #include "global.h"
35 #include "support.h"
36 #include "main.h"
37 #include "gui_support.h"
38 #include "diritem.h"
39 #include "type.h"
40 #include "infobox.h"
41 #include "appinfo.h"
42 #include "dnd.h" /* For xa_string */
43 #include "run.h" /* For show_help_files() */
44 #include "xml.h"
45 #include "mount.h"
46 #include "pixmaps.h"
47 #include "xtypes.h"
49 typedef struct _FileStatus FileStatus;
51 /* This is for the 'file(1) says...' thing */
52 struct _FileStatus
54 int fd; /* FD to read from, -1 if closed */
55 int input; /* Input watcher tag if fd valid */
56 GtkLabel *label; /* Widget to output to */
57 gchar *text; /* String so far */
60 typedef struct du {
61 gchar *path;
62 GtkListStore *store;
63 guint watch;
64 GIOChannel *chan;
65 gint child;
66 } DU;
68 typedef struct _Permissions Permissions;
70 struct _Permissions
72 gchar *path;
73 DirItem *item;
74 GtkWidget *bits[12];
77 /* Static prototypes */
78 static void refresh_info(GObject *window);
79 static GtkWidget *make_vbox(const guchar *path, GObject *window);
80 static GtkWidget *make_details(const guchar *path, DirItem *item,
81 GObject *window);
82 static GtkWidget *make_about(const guchar *path, XMLwrapper *ai);
83 static GtkWidget *make_about_desktop(const gchar *path);
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_CLOSE, GTK_RESPONSE_CANCEL,
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(di_image(item)->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_label_set_line_wrap(GTK_LABEL(name), TRUE);
243 gtk_box_pack_start(GTK_BOX(hbox), name, FALSE, TRUE, 4);
245 make_heading(name, PANGO_SCALE_X_LARGE);
247 /* List of file attributes */
248 add_frame(vbox, make_details(path, item, window));
250 help_dir = g_strconcat(path, "/Help", NULL);
252 if (access(help_dir, F_OK) == 0)
254 GtkWidget *button, *align;
256 align = gtk_alignment_new(0.5, 0.5, 0, 0);
258 button = button_new_mixed(GTK_STOCK_JUMP_TO,
259 _("Show _Help Files"));
260 gtk_box_pack_start(vbox, align, FALSE, TRUE, 0);
261 gtk_container_add(GTK_CONTAINER(align), button);
262 g_signal_connect_swapped(button, "clicked",
263 G_CALLBACK(show_help_files),
264 (gpointer) path);
266 g_free(help_dir);
268 if (!(item->flags & ITEM_FLAG_SYMLINK))
270 label = gtk_label_new(NULL);
271 gtk_label_set_markup(GTK_LABEL(label),
272 _("<b>Permissions</b>"));
273 gtk_misc_set_alignment(GTK_MISC(label), 0, 1);
274 gtk_box_pack_start(vbox, label, FALSE, TRUE, 2);
276 gtk_box_pack_start(vbox, make_permissions(path, item),
277 FALSE, TRUE, 0);
280 if (about)
281 add_frame(vbox, make_about(path, ai));
282 else if (item->mime_type == application_x_desktop)
284 add_frame(vbox, make_about_desktop(path));
286 else if (item->base_type == TYPE_FILE)
288 label = gtk_label_new(NULL);
289 gtk_label_set_markup(GTK_LABEL(label),
290 _("<b>Contents indicate...</b>"));
291 gtk_misc_set_alignment(GTK_MISC(label), 0, 1);
292 gtk_box_pack_start(vbox, label, FALSE, TRUE, 2);
294 gtk_box_pack_start_defaults(vbox, make_file_says(path));
297 if (ai)
298 g_object_unref(ai);
300 diritem_free(item);
302 return (GtkWidget *) vbox;
305 /* The selection has changed - grab or release the primary selection */
306 static void set_selection(GtkTreeView *view, gpointer data)
308 static GtkClipboard *primary = NULL;
309 GtkTreeModel *model;
310 GtkTreePath *path = NULL;
311 GtkTreeIter iter;
312 gchar *text;
314 gtk_tree_view_get_cursor(view, &path, NULL);
315 if (!path)
316 return;
318 if (!primary)
319 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
321 model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
323 gtk_tree_model_get_iter(model, &iter, path);
324 gtk_tree_path_free(path);
326 gtk_tree_model_get(model, &iter, 1, &text, -1);
328 gtk_clipboard_set_text(primary, text, -1);
330 g_free(text);
333 /* Returns a GtkTreePath for the item */
334 static const gchar *add_row(GtkListStore *store, const gchar *label,
335 const gchar *data)
337 GtkTreeIter iter;
338 gchar *u8 = NULL;
339 GtkTreePath *tpath;
340 static gchar *last = NULL;
342 if (!g_utf8_validate(data, -1, NULL))
343 u8 = to_utf8(data);
345 gtk_list_store_append(store, &iter);
346 gtk_list_store_set(store, &iter, 0, label, 1, u8 ? u8 : data, -1);
348 g_free(u8);
350 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
351 if (last)
352 g_free(last);
353 last = gtk_tree_path_to_string(tpath);
354 gtk_tree_path_free(tpath);
356 return last;
359 static void add_row_and_free(GtkListStore *store,
360 const gchar *label, gchar *data)
362 add_row(store, label, data);
363 g_free(data);
366 /* Create an empty list view, ready to place some data in */
367 static void make_list(GtkListStore **list_store, GtkWidget **list_view,
368 GCallback cell_edited)
370 GtkListStore *store;
371 GtkTreeView *view;
372 GtkCellRenderer *cell_renderer;
374 /* Field name, value, editable */
375 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING,
376 G_TYPE_BOOLEAN);
377 view = GTK_TREE_VIEW(
378 gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
379 g_object_unref(G_OBJECT(store));
380 gtk_tree_view_set_headers_visible(view, FALSE);
382 cell_renderer = gtk_cell_renderer_text_new();
383 g_object_set(G_OBJECT(cell_renderer), "xalign", 1.0, NULL);
384 gtk_tree_view_insert_column_with_attributes(view,
385 0, NULL, cell_renderer, "text", 0, NULL);
387 cell_renderer = gtk_cell_renderer_text_new();
388 gtk_tree_view_insert_column_with_attributes(view,
389 1, NULL, cell_renderer, "text", 1, "editable", 2, NULL);
391 if (cell_edited) {
392 g_signal_connect(G_OBJECT(cell_renderer), "edited",
393 G_CALLBACK(cell_edited), store);
396 g_signal_connect(view, "cursor_changed",
397 G_CALLBACK(set_selection), NULL);
399 *list_store = store;
400 *list_view = (GtkWidget *) view;
403 static void set_cell(GtkListStore *store, const gchar *path,
404 const gchar *ctext)
406 GtkTreeIter iter;
408 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store),
409 &iter, path);
410 gtk_list_store_set(store, &iter, 1, ctext, -1);
413 static void insert_size(DU *du, const char *line)
415 off_t size;
416 gchar *cell;
418 #ifdef LARGE_FILE_SUPPORT
419 size = strtoll(line, NULL, 10);
420 #else
421 size = strtol(line, NULL, 10);
422 #endif
423 size <<= 10; /* Because du reports in K */
424 cell = (size >= PRETTY_SIZE_LIMIT)
425 ? g_strdup_printf("%s (%" SIZE_FMT " %s)",
426 format_size(size),
427 size, _("bytes"))
428 : g_strdup(format_size(size));
430 set_cell(du->store, du->path, cell);
432 g_free(cell);
435 static gboolean read_du_output(GIOChannel *source, GIOCondition cond, DU *du)
437 GString *line;
438 GIOStatus stat;
439 GError *err = NULL;
441 line = g_string_new("");
442 stat = g_io_channel_read_line_string(source, line, NULL, &err);
443 switch (stat)
445 case G_IO_STATUS_NORMAL:
446 insert_size(du, line->str);
447 break;
448 case G_IO_STATUS_EOF:
449 set_cell(du->store, du->path,
450 _("Failed to read size"));
451 break;
452 case G_IO_STATUS_AGAIN:
453 g_string_free(line, TRUE);
454 return TRUE;
455 case G_IO_STATUS_ERROR:
456 set_cell(du->store, du->path, err->message);
457 break;
459 g_string_free(line, TRUE);
461 return FALSE;
464 static void kill_du_output(GtkWidget *widget, DU *du)
466 g_source_remove(du->watch);
467 g_io_channel_shutdown(du->chan, FALSE, NULL);
468 g_io_channel_unref(du->chan);
469 kill((pid_t) du->child, SIGTERM);
470 g_object_unref(G_OBJECT(du->store));
471 g_free(du->path);
472 g_free(du);
475 static gboolean refresh_info_idle(gpointer data)
477 GObject *window = G_OBJECT(data);
479 refresh_info(window);
480 g_object_unref(window);
481 return FALSE;
484 static void cell_edited(GtkCellRendererText *cell,
485 const gchar *path_string,
486 const gchar *new_text,
487 gpointer data)
489 GtkTreeModel *model = (GtkTreeModel *) data;
490 GtkTreePath *path;
491 GtkTreeIter iter;
492 GObject *window;
493 const char *fullpath;
494 char *oldlink;
496 window = g_object_get_data(G_OBJECT(model), "rox_window");
497 g_return_if_fail(window != NULL);
499 fullpath = g_object_get_data(window, "path");
500 g_return_if_fail(fullpath != NULL);
502 path = gtk_tree_path_new_from_string(path_string);
503 gtk_tree_model_get_iter(model, &iter, path);
504 gtk_tree_path_free(path);
506 oldlink = readlink_dup(fullpath);
507 if (!oldlink) {
508 /* Must use delayed_error(), as this can be called
509 * from a focus-out event (causes a crash).
511 delayed_error(_("'%s' is no longer a symlink"), fullpath);
512 return;
514 if (strcmp(oldlink, new_text) == 0)
515 return; /* No change */
516 g_free(oldlink);
517 if (unlink(fullpath)) {
518 delayed_error(_("Failed to unlink '%s':\n%s"),
519 fullpath, g_strerror(errno));
520 return;
522 if (symlink(new_text, fullpath)) {
523 delayed_error(_("Failed to create symlink from '%s':\n%s\n"
524 "(note: old link has been deleted)"),
525 fullpath, g_strerror(errno));
526 return;
529 g_object_ref(window);
530 g_idle_add(refresh_info_idle, window);
533 /* Create the TreeView widget with the file's details */
534 static GtkWidget *make_details(const guchar *path, DirItem *item,
535 GObject *window)
537 GtkListStore *store;
538 GtkWidget *view;
539 gchar *tmp, *tmp2;
541 make_list(&store, &view, G_CALLBACK(cell_edited));
542 g_object_set_data(G_OBJECT(store), "rox_window", window);
544 /* For a symlink to an error, don't show the error */
545 if (item->base_type == TYPE_ERROR && item->lstat_errno)
547 add_row(store, _("Error:"), g_strerror(item->lstat_errno));
548 return view;
551 tmp = g_path_get_dirname(path);
552 tmp2 = pathdup(tmp);
553 if (strcmp(tmp, tmp2) != 0)
554 add_row_and_free(store, _("Real directory:"), tmp2);
555 g_free(tmp);
557 add_row_and_free(store, _("Owner, Group:"),
558 g_strdup_printf("%s, %s",
559 user_name(item->uid),
560 group_name(item->gid)));
562 if (item->base_type != TYPE_DIRECTORY)
564 add_row_and_free(store, _("Size:"),
565 item->size >= PRETTY_SIZE_LIMIT
566 ? g_strdup_printf("%s (%" SIZE_FMT " %s)",
567 format_size(item->size),
568 item->size, _("bytes"))
569 : g_strdup(format_size(item->size)));
571 else
573 gchar *stt=NULL;
575 if(item->flags & ITEM_FLAG_MOUNTED)
576 stt=mount_get_fs_size(path);
578 if(stt) {
579 add_row_and_free(store, _("Size:"), stt);
580 } else {
581 DU *du;
582 int out;
584 gchar *args[] = {"du", "-sk", "", NULL};
586 du = g_new(DU, 1);
587 du->store = store;
588 du->path = g_strdup(add_row(store, _("Size:"),
589 _("Scanning")));
591 args[2] = (gchar *) path;
592 if (g_spawn_async_with_pipes(NULL, args, NULL,
593 G_SPAWN_SEARCH_PATH,
594 NULL, NULL, &du->child,
595 NULL, &out, NULL,
596 NULL))
598 du->chan = g_io_channel_unix_new(out);
599 /* Select binary encoding so we don't get an
600 * error with non-UTF-8 filenames.
602 g_io_channel_set_encoding(du->chan, NULL, NULL);
603 du->watch = g_io_add_watch(du->chan,
604 G_IO_IN|G_IO_ERR|G_IO_HUP,
605 (GIOFunc) read_du_output, du);
606 g_object_ref(G_OBJECT(du->store));
607 g_signal_connect(G_OBJECT(view),
608 "destroy",
609 G_CALLBACK(kill_du_output),
610 du);
612 else
614 set_cell(store, du->path, _("Failed to scan"));
615 g_free(du->path);
616 g_free(du);
621 add_row_and_free(store, _("Change time:"), pretty_time(&item->ctime));
623 add_row_and_free(store, _("Modify time:"), pretty_time(&item->mtime));
625 add_row_and_free(store, _("Access time:"), pretty_time(&item->atime));
627 add_row(store, _("Type:"), pretty_type(item, path));
629 if (item->mime_type)
630 add_row(store, "", mime_type_comment(item->mime_type));
632 if (xattr_supported(NULL)) {
633 add_row(store, _("Extended attributes:"),
634 (item->flags & ITEM_FLAG_HAS_XATTR)
635 ? _("Present")
636 : xattr_supported(path) ? _("None")
637 : _("Not supported"));
640 if (item->flags & ITEM_FLAG_SYMLINK)
642 GtkTreeIter iter;
643 GtkTreeModel *model = GTK_TREE_MODEL(store);
644 char *target;
646 target = readlink_dup(path);
647 if (!target)
648 target = g_strdup(g_strerror(errno));
649 add_row_and_free(store, _("Link target:"), target);
651 /* Make cell editable */
652 gtk_tree_model_iter_nth_child(model, &iter,
653 NULL, gtk_tree_model_iter_n_children(model, NULL) - 1);
655 gtk_list_store_set(store, &iter, 2, TRUE, -1);
658 if (item->base_type != TYPE_DIRECTORY)
660 if (EXECUTABLE_FILE(item))
661 add_row(store, _("Run action:"), _("Execute file"));
662 else
664 add_row_and_free(store, _("Run action:"),
665 describe_current_command(item->mime_type));
669 return view;
672 /* Create the TreeView widget with the application's details */
673 static GtkWidget *make_about(const guchar *path, XMLwrapper *ai)
675 GtkListStore *store;
676 GtkWidget *view;
677 xmlNode *prop;
678 xmlNode *about, *about_trans;
679 GHashTable *translate;
681 g_return_val_if_fail(ai != NULL, NULL);
683 about_trans = xml_get_section(ai, NULL, "About");
685 about = xmlDocGetRootElement(ai->doc)->xmlChildrenNode;
686 for (; about; about = about->next)
688 if (about->type != XML_ELEMENT_NODE)
689 continue;
690 if (about->ns == NULL && strcmp(about->name, "About") == 0)
691 break;
694 g_return_val_if_fail(about != NULL, NULL);
696 make_list(&store, &view, NULL);
698 /* Add each field in about to the list, but overriding each element
699 * with about_trans if a translation is supplied.
701 translate = g_hash_table_new(g_str_hash, g_str_equal);
702 if (about_trans != about)
704 xmlNode *p;
705 for (p = about_trans->xmlChildrenNode; p; p = p->next)
707 if (p->type != XML_ELEMENT_NODE)
708 continue;
709 g_hash_table_insert(translate, (char *) p->name, p);
712 for (prop = about->xmlChildrenNode; prop; prop = prop->next)
714 if (prop->type == XML_ELEMENT_NODE)
716 char *label = NULL;
717 char *value = NULL;
718 char *tmp = NULL;
719 xmlNode *trans;
721 trans = g_hash_table_lookup(translate, prop->name);
722 if (!trans)
723 trans = prop;
725 tmp = xmlGetProp(trans, "label");
726 label = g_strconcat(tmp ? tmp
727 : (char *) trans->name,
728 ":", NULL);
729 g_free(tmp);
730 value = xmlNodeListGetString(trans->doc,
731 trans->xmlChildrenNode, 1);
732 if (!value)
733 value = xmlNodeListGetString(prop->doc,
734 prop->xmlChildrenNode, 1);
735 if (!value)
736 value = g_strdup("-");
737 add_row_and_free(store, label, value);
738 g_free(label);
742 g_hash_table_destroy(translate);
744 return view;
747 /* Create the TreeView widget with the desktop entry's details */
748 static GtkWidget *make_about_desktop(const gchar *path)
750 GtkListStore *store;
751 GtkWidget *view;
752 GError *error=NULL;
753 gchar *name=NULL, *comment=NULL, *exec=NULL;
755 make_list(&store, &view, NULL);
757 if(!get_values_from_desktop_file(path, &error,
758 "Desktop Entry", "Name", &name,
759 "Desktop Entry", "Comment", &comment,
760 "Desktop Entry", "Exec", &exec,
761 NULL))
763 /* Report it? */
764 delayed_error("%s", error->message);
765 if(error)
766 g_error_free(error);
767 return view;
770 if(name)
771 add_row_and_free(store, _("Name"), name);
772 if(comment)
773 add_row_and_free(store, _("Comment"), comment);
774 if(exec)
775 add_row_and_free(store, _("Execute"), exec);
777 return view;
780 static GtkWidget *make_file_says(const guchar *path)
782 GtkWidget *w_file_label;
783 GtkLabel *l_file_label;
784 int file_data[2];
785 char *argv[] = {"file", "-b", NULL, NULL};
786 FileStatus *fs = NULL;
787 guchar *tmp;
789 w_file_label = gtk_label_new(_("<nothing yet>"));
790 l_file_label = GTK_LABEL(w_file_label);
791 gtk_label_set_line_wrap(l_file_label, TRUE);
792 gtk_label_set_selectable(l_file_label, TRUE);
794 if (pipe(file_data))
796 tmp = g_strdup_printf("pipe(): %s", g_strerror(errno));
797 gtk_label_set_text(l_file_label, tmp);
798 g_free(tmp);
799 return w_file_label;
802 switch (fork())
804 case -1:
805 tmp = g_strdup_printf("pipe(): %s", g_strerror(errno));
806 gtk_label_set_text(l_file_label, tmp);
807 g_free(tmp);
808 close(file_data[0]);
809 close(file_data[1]);
810 break;
811 case 0:
812 /* We are the child */
813 close(file_data[0]);
814 dup2(file_data[1], STDOUT_FILENO);
815 dup2(file_data[1], STDERR_FILENO);
816 #ifdef FILE_B_FLAG
817 argv[2] = (char *) path;
818 #else
819 argv[1] = (char *) g_basename(path);
820 chdir(g_path_get_dirname(path));
821 #endif
822 if (execvp(argv[0], argv))
823 fprintf(stderr, "execvp() error: %s\n",
824 g_strerror(errno));
825 _exit(0);
826 default:
827 /* We are the parent */
828 close(file_data[1]);
829 fs = g_new(FileStatus, 1);
830 fs->label = l_file_label;
831 fs->fd = file_data[0];
832 fs->text = g_strdup("");
833 fs->input = gdk_input_add_full(fs->fd, GDK_INPUT_READ,
834 (GdkInputFunction) add_file_output,
835 fs, NULL);
836 g_signal_connect(w_file_label, "destroy",
837 G_CALLBACK(file_info_destroyed), fs);
838 break;
841 return w_file_label;
844 /* Got some data from file(1) - stick it in the window. */
845 static void add_file_output(FileStatus *fs,
846 gint source, GdkInputCondition condition)
848 char buffer[20];
849 char *str;
850 int got;
852 got = read(source, buffer, sizeof(buffer) - 1);
853 if (got <= 0)
855 int err = errno;
856 g_source_remove(fs->input);
857 close(source);
858 fs->fd = -1;
859 if (got < 0)
860 delayed_error(_("file(1) says... %s"),
861 g_strerror(err));
862 return;
864 buffer[got] = '\0';
866 str = g_strconcat(fs->text, buffer, NULL);
867 g_free(fs->text);
868 fs->text = str;
870 str = to_utf8(fs->text);
871 g_strstrip(str);
872 gtk_label_set_text(fs->label, str);
873 g_free(str);
876 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
878 if (fs->fd != -1)
880 g_source_remove(fs->input);
881 close(fs->fd);
884 g_free(fs->text);
885 g_free(fs);
888 static void permissions_destroyed(GtkWidget *widget, Permissions *perm)
890 g_free(perm->path);
891 diritem_free(perm->item);
893 g_free(perm);
896 static void permissions_apply(GtkWidget *widget, Permissions *perm)
898 mode_t nmode;
899 int i;
901 nmode=0;
903 for (i = 0; i < 9; i++)
905 GtkToggleButton *bit = GTK_TOGGLE_BUTTON(perm->bits[i]);
906 if (gtk_toggle_button_get_active(bit))
907 nmode |= 1 << i;
909 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[9])))
910 nmode |= S_ISUID;
911 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[10])))
912 nmode |= S_ISGID;
913 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(perm->bits[11])))
914 nmode |= S_ISVTX;
916 if (chmod(perm->path, nmode))
917 report_error(_("Could not change permissions: %s"),
918 g_strerror(errno));
921 static GtkWidget *make_permissions(const gchar *path, DirItem *item)
923 Permissions *perm;
924 GtkWidget *table;
925 GtkWidget *tick, *label;
926 int i, x, y;
928 perm = g_new(Permissions, 1);
930 perm->path = g_strdup(path);
931 perm->item = diritem_new(path);
933 table = gtk_table_new(4, 5, TRUE);
935 label = gtk_label_new(_("Owner"));
936 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
937 label = gtk_label_new(_("Group"));
938 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
939 label = gtk_label_new(_("World"));
940 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
942 label = gtk_label_new(_("Read"));
943 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
944 label = gtk_label_new(_("Write"));
945 gtk_table_attach_defaults(GTK_TABLE(table), label, 2, 3, 0, 1);
946 label = gtk_label_new(_("Exec"));
947 gtk_table_attach_defaults(GTK_TABLE(table), label, 3, 4, 0, 1);
949 for (i = 0; i < 9; i++)
951 x = 1 + 2 - i % 3;
952 y = 1 + 2 - i / 3;
953 perm->bits[i] = tick = gtk_check_button_new();
954 gtk_table_attach_defaults(GTK_TABLE(table), tick,
955 x, x + 1, y, y + 1);
956 if (item->mode & (1 << i))
957 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick),
958 TRUE);
959 g_signal_connect(tick, "toggled",
960 G_CALLBACK(permissions_apply), perm);
963 tick = gtk_check_button_new_with_label(_("SUID"));
964 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 1, 2);
965 if (item->mode & S_ISUID)
966 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
967 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
968 perm->bits[9] = tick;
970 tick = gtk_check_button_new_with_label(_("SGID"));
971 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 2, 3);
972 if (item->mode & S_ISGID)
973 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
974 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
975 perm->bits[10] = tick;
977 tick = gtk_check_button_new_with_label(_("Sticky"));
978 gtk_table_attach_defaults(GTK_TABLE(table), tick, 4, 5, 3, 4);
979 if (item->mode & S_ISVTX)
980 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tick), TRUE);
981 g_signal_connect(tick, "toggled", G_CALLBACK(permissions_apply), perm);
982 perm->bits[11] = tick;
984 g_signal_connect(table, "destroy",
985 G_CALLBACK(permissions_destroyed), perm);
987 gtk_widget_show_all(table);
988 return table;
991 /* Don't g_free() the result */
992 static const gchar *pretty_type(DirItem *file, const guchar *path)
994 static gchar *text = NULL;
996 null_g_free(&text);
998 if (file->flags & ITEM_FLAG_SYMLINK)
999 return _("Symbolic link");
1001 if (file->flags & ITEM_FLAG_APPDIR)
1002 return _("ROX application");
1004 if (file->flags & ITEM_FLAG_MOUNT_POINT)
1006 MountPoint *mp;
1007 const gchar *mounted;
1009 mounted = mount_is_mounted(path, NULL, NULL)
1010 ? _("mounted") : _("unmounted");
1012 mp = g_hash_table_lookup(fstab_mounts, path);
1013 if (mp)
1014 text = g_strdup_printf(_("Mount point for %s (%s)"),
1015 mp->name, mounted);
1016 else
1017 text = g_strdup_printf(_("Mount point (%s)"), mounted);
1018 return text;
1021 if (file->mime_type)
1023 text = g_strconcat(file->mime_type->media_type, "/",
1024 file->mime_type->subtype, NULL);
1025 return text;
1028 return "-";