From 07d7ebbf84da4c3165c3cdcfdf0f00fa93568153 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 19 Oct 2026 16:09:26 +0200 Subject: [PATCH] filechooser: more about the file beside the preview The panel beside the files lists, as the file managers do: the type, the size, where the file is, the size of a picture (from its thumbnail), when it was changed, read and made, its owner and group, and where a link points. What the list does not know yet is asked of GIO in the background, so a slow disk or network share holds nothing up. The name of a picture format the system has no name for is now found from its MIME type; before, the file itself was read for it, on the main thread. --- gtk/gtkfilechooserdefault.c | 205 +++++++++++++++++++++++++++++++----- 1 file changed, 176 insertions(+), 29 deletions(-) diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 6b945eb4f..5181a0b08 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -453,6 +453,9 @@ static void set_current_filter (GtkFileChooserDefault *impl, GtkFileFilter *filter); static void check_preview_change (GtkFileChooserDefault *impl); static void preview_panel_sync (GtkFileChooserDefault *impl); +static void preview_panel_update (GtkFileChooserDefault *impl); +static gchar *file_list_format_size (goffset size); +static gchar *my_g_format_time_for_display (gint64 secs); static void filter_combo_changed (GtkComboBox *combo_box, GtkFileChooserDefault *impl); @@ -1429,6 +1432,135 @@ preview_panel_stop (GtkFileChooserDefault *impl) } } +/* A line "Key: value" more in the table of the panel */ +static void +preview_panel_add_row (GtkFileChooserDefault *impl, + const gchar *key, + const gchar *value) +{ + GtkTable *table = GTK_TABLE (impl->preview_panel_info); + GtkWidget *label; + guint rows; + + if (value == NULL || value[0] == '\0') + return; + + g_object_get (table, "n-rows", &rows, NULL); + if (g_object_get_data (G_OBJECT (table), "gtk-used")) + gtk_table_resize (table, ++rows, 2); + g_object_set_data (G_OBJECT (table), I_("gtk-used"), GINT_TO_POINTER (TRUE)); + + label = gtk_label_new (key); + gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.0); + gtk_table_attach (table, label, 0, 1, rows - 1, rows, GTK_FILL, GTK_FILL, 0, 0); + + label = gtk_label_new (value); + gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); + gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR); + /* wrapped within the panel, beside the keys */ + gtk_widget_set_size_request (label, PREVIEW_PANEL_WIDTH * 3 / 5, -1); + gtk_label_set_selectable (GTK_LABEL (label), TRUE); + gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0); + gtk_table_attach (table, label, 1, 2, rows - 1, rows, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0); + + gtk_widget_show_all (GTK_WIDGET (table)); +} + +static void +preview_panel_clear_rows (GtkFileChooserDefault *impl) +{ + GList *children = gtk_container_get_children (GTK_CONTAINER (impl->preview_panel_info)), *l; + + for (l = children; l; l = l->next) + gtk_widget_destroy (l->data); + g_list_free (children); + gtk_table_resize (GTK_TABLE (impl->preview_panel_info), 1, 2); + g_object_set_data (G_OBJECT (impl->preview_panel_info), I_("gtk-used"), NULL); +} + +/* What the panel shows of a file beyond what the list has: asked of + * GIO in the background, so that a slow disk or share holds up nothing */ +#define PREVIEW_PANEL_ATTRIBUTES "standard::symlink-target,time::access,time::created,owner::user,owner::group" + +static void +preview_panel_info_done (GObject *object, + GAsyncResult *result, + gpointer data) +{ + GtkFileChooserDefault *impl = data; + GFileInfo *info = g_file_query_info_finish (G_FILE (object), result, NULL); + static const struct { const gchar *attribute, *key; } times[] = { + { G_FILE_ATTRIBUTE_TIME_ACCESS, N_("Accessed:") }, + { G_FILE_ATTRIBUTE_TIME_CREATED, N_("Created:") } + }; + gint i; + + /* given up meanwhile: the panel may be gone too */ + if (info == NULL) + { + g_object_unref (impl); + return; + } + + if (impl->preview_panel && impl->preview_file && + g_file_equal (impl->preview_file, G_FILE (object))) + { + preview_panel_add_row (impl, _("Link target:"), + g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET)); + for (i = 0; i < G_N_ELEMENTS (times); i++) + if (g_file_info_has_attribute (info, times[i].attribute)) + { + gchar *date = my_g_format_time_for_display (g_file_info_get_attribute_uint64 (info, times[i].attribute)); + + preview_panel_add_row (impl, _(times[i].key), date); + g_free (date); + } + preview_panel_add_row (impl, _("Owner:"), g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_USER)); + preview_panel_add_row (impl, _("Group:"), g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP)); + } + + g_object_unref (info); + g_object_unref (impl); +} + +/* The name of the type, without reading the file: where the system knows + * none (GIO then makes one of the type itself, ".jpg filetype" on Windows + * without that type registered, "image/x-foo" elsewhere), the one of the + * picture format gdk-pixbuf has for it */ +static gchar * +preview_panel_type_name (const gchar *type) +{ + gchar *description = g_content_type_get_description (type); + gchar *mime; + GSList *formats, *l; + + if (description && strstr (description, type) == NULL) + return description; + + mime = g_content_type_get_mime_type (type); + formats = mime ? gdk_pixbuf_get_formats () : NULL; + for (l = formats; l; l = l->next) + { + gchar **types = gdk_pixbuf_format_get_mime_types (l->data); + gboolean match = FALSE; + gint i; + + for (i = 0; types && types[i] && !match; i++) + match = g_ascii_strcasecmp (types[i], mime) == 0; + g_strfreev (types); + if (match) + { + g_free (description); + description = gdk_pixbuf_format_get_description (l->data); + break; + } + } + g_slist_free (formats); + g_free (mime); + + return description; +} + /* Shows the thumbnail at @path, never made bigger; FALSE if it cannot * be read. *@small tells whether it is smaller than the panel, and a * bigger one could be made. */ @@ -1461,6 +1593,22 @@ preview_panel_set_picture (GtkFileChooserDefault *impl, *small = FALSE; } + /* the size of the picture, as the thumbnail tells */ + if (!g_object_get_data (G_OBJECT (impl->preview_panel_info), "gtk-dimensions")) + { + const gchar *w = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Width"); + const gchar *h = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Height"); + + if (w && h && atoi (w) > 0 && atoi (h) > 0) + { + gchar *size = g_strdup_printf ("%d \303\227 %d", atoi (w), atoi (h)); + + preview_panel_add_row (impl, _("Dimensions:"), size); + g_free (size); + g_object_set_data (G_OBJECT (impl->preview_panel_info), I_("gtk-dimensions"), GINT_TO_POINTER (TRUE)); + } + } + gtk_image_set_from_pixbuf (GTK_IMAGE (impl->preview_panel_image), pixbuf); g_object_unref (pixbuf); @@ -1485,7 +1633,6 @@ preview_panel_update (GtkFileChooserDefault *impl) { GFileInfo *info = NULL; GtkTreeIter iter; - GString *text; const gchar *type, *thumbnail; gboolean small = TRUE; @@ -1496,40 +1643,24 @@ preview_panel_update (GtkFileChooserDefault *impl) &iter, impl->preview_file)) info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (impl->current_model), &iter); + preview_panel_clear_rows (impl); + g_object_set_data (G_OBJECT (impl->preview_panel_info), I_("gtk-dimensions"), NULL); + if (info == NULL) { gtk_image_clear (GTK_IMAGE (impl->preview_panel_image)); gtk_label_set_text (GTK_LABEL (impl->preview_panel_name), ""); - gtk_label_set_text (GTK_LABEL (impl->preview_panel_info), ""); return; } gtk_label_set_text (GTK_LABEL (impl->preview_panel_name), g_file_info_get_display_name (info)); - text = g_string_new (NULL); type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE); if (type) { - gchar *description = g_content_type_get_description (type); + gchar *description = preview_panel_type_name (type); - /* Where the system knows no name for the type, GIO makes one of - * the type itself (".jpg filetype" on Windows without that file - * type registered, "image/x-foo" elsewhere); an image gdk-pixbuf - * can read has the name of its format instead */ - if (description == NULL || strstr (description, type) != NULL) - { - gchar *path = g_file_get_path (impl->preview_file); - GdkPixbufFormat *format = path ? gdk_pixbuf_get_file_info (path, NULL, NULL) : NULL; - - if (format != NULL) - { - g_free (description); - description = gdk_pixbuf_format_get_description (format); - } - g_free (path); - } - if (description) - g_string_append (text, description); + preview_panel_add_row (impl, _("Type:"), description); g_free (description); } if (!_gtk_file_info_consider_as_directory (info) && @@ -1537,18 +1668,34 @@ preview_panel_update (GtkFileChooserDefault *impl) { gchar *size = file_list_format_size (g_file_info_get_size (info)); - g_string_append_printf (text, "\n%s", size); + preview_panel_add_row (impl, _("Size:"), size); g_free (size); } + { + GFile *parent = g_file_get_parent (impl->preview_file); + + if (parent) + { + gchar *location = g_file_get_parse_name (parent); + + preview_panel_add_row (impl, _("Location:"), location); + g_free (location); + g_object_unref (parent); + } + } if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED)) { gchar *date = my_g_format_time_for_display (g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED)); - g_string_append_printf (text, "\n%s", date); + preview_panel_add_row (impl, _("Modified:"), date); g_free (date); } - gtk_label_set_text (GTK_LABEL (impl->preview_panel_info), text->str); - g_string_free (text, TRUE); + + impl->preview_panel_cancellable = g_cancellable_new (); + g_file_query_info_async (impl->preview_file, PREVIEW_PANEL_ATTRIBUTES, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, G_PRIORITY_DEFAULT, + impl->preview_panel_cancellable, + preview_panel_info_done, g_object_ref (impl)); /* The thumbnail there is, else the icon; a large thumbnail is made * when the one there is smaller than the panel */ @@ -1566,7 +1713,6 @@ preview_panel_update (GtkFileChooserDefault *impl) if (small && impl->show_thumbnails && _gtk_file_thumbnail_wanted (impl->preview_file, info, MEGABYTES (impl->thumbnail_max_size))) { - impl->preview_panel_cancellable = g_cancellable_new (); _gtk_file_thumbnail_request (impl->preview_file, info, 256, MEGABYTES (impl->thumbnail_max_size_remote), impl->preview_panel_cancellable, @@ -1635,8 +1781,9 @@ preview_panel_sync (GtkFileChooserDefault *impl) gtk_widget_set_size_request (impl->preview_panel_name, PREVIEW_PANEL_WIDTH, -1); gtk_box_pack_start (GTK_BOX (impl->preview_panel), impl->preview_panel_name, FALSE, FALSE, 0); - impl->preview_panel_info = gtk_label_new (NULL); - gtk_misc_set_alignment (GTK_MISC (impl->preview_panel_info), 0.0, 0.0); + impl->preview_panel_info = gtk_table_new (1, 2, FALSE); + gtk_table_set_col_spacings (GTK_TABLE (impl->preview_panel_info), 6); + gtk_table_set_row_spacings (GTK_TABLE (impl->preview_panel_info), 2); gtk_box_pack_start (GTK_BOX (impl->preview_panel), impl->preview_panel_info, FALSE, FALSE, 0); box = gtk_widget_get_parent (impl->preview_box);