|
| 1 | +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 | + |
| 3 | +/* |
| 4 | + * nemo-preview-details.c - Widget for displaying file details in preview pane |
| 5 | + * |
| 6 | + * Copyright (C) 2025 Linux Mint |
| 7 | + * |
| 8 | + * Nemo is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU General Public License as |
| 10 | + * published by the Free Software Foundation; either version 2 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * Nemo is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public |
| 19 | + * License along with this program; see the file COPYING. If not, |
| 20 | + * write to the Free Software Foundation, Inc., 51 Franklin Street - Suite 500, |
| 21 | + * Boston, MA 02110-1335, USA. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "nemo-preview-details.h" |
| 25 | +#include <glib/gi18n.h> |
| 26 | + |
| 27 | +struct _NemoPreviewDetails { |
| 28 | + GtkBox parent; |
| 29 | +}; |
| 30 | + |
| 31 | +typedef struct { |
| 32 | + GtkWidget *grid; |
| 33 | + GtkWidget *name_value_label; |
| 34 | + GtkWidget *size_value_label; |
| 35 | + GtkWidget *type_value_label; |
| 36 | + GtkWidget *modified_value_label; |
| 37 | + GtkWidget *permissions_value_label; |
| 38 | + GtkWidget *location_value_label; |
| 39 | + |
| 40 | + NemoFile *file; |
| 41 | +} NemoPreviewDetailsPrivate; |
| 42 | + |
| 43 | +G_DEFINE_TYPE_WITH_PRIVATE (NemoPreviewDetails, nemo_preview_details, GTK_TYPE_BOX) |
| 44 | + |
| 45 | +static void |
| 46 | +nemo_preview_details_finalize (GObject *object) |
| 47 | +{ |
| 48 | + NemoPreviewDetails *details; |
| 49 | + NemoPreviewDetailsPrivate *priv; |
| 50 | + |
| 51 | + details = NEMO_PREVIEW_DETAILS (object); |
| 52 | + priv = nemo_preview_details_get_instance_private (details); |
| 53 | + |
| 54 | + if (priv->file != NULL) { |
| 55 | + nemo_file_unref (priv->file); |
| 56 | + priv->file = NULL; |
| 57 | + } |
| 58 | + |
| 59 | + G_OBJECT_CLASS (nemo_preview_details_parent_class)->finalize (object); |
| 60 | +} |
| 61 | + |
| 62 | +static GtkWidget * |
| 63 | +create_label_pair (GtkGrid *grid, const gchar *label_text, gint row) |
| 64 | +{ |
| 65 | + GtkWidget *label; |
| 66 | + GtkWidget *value; |
| 67 | + |
| 68 | + /* Create the label (left column) */ |
| 69 | + label = gtk_label_new (label_text); |
| 70 | + gtk_widget_set_halign (label, GTK_ALIGN_END); |
| 71 | + gtk_widget_set_valign (label, GTK_ALIGN_START); |
| 72 | + gtk_style_context_add_class (gtk_widget_get_style_context (label), "dim-label"); |
| 73 | + gtk_grid_attach (grid, label, 0, row, 1, 1); |
| 74 | + gtk_widget_show (label); |
| 75 | + |
| 76 | + /* Create the value label (right column) */ |
| 77 | + value = gtk_label_new (""); |
| 78 | + gtk_widget_set_halign (value, GTK_ALIGN_START); |
| 79 | + gtk_widget_set_valign (value, GTK_ALIGN_START); |
| 80 | + gtk_label_set_selectable (GTK_LABEL (value), TRUE); |
| 81 | + gtk_label_set_ellipsize (GTK_LABEL (value), PANGO_ELLIPSIZE_MIDDLE); |
| 82 | + gtk_label_set_max_width_chars (GTK_LABEL (value), 30); |
| 83 | + gtk_grid_attach (grid, value, 1, row, 1, 1); |
| 84 | + gtk_widget_show (value); |
| 85 | + |
| 86 | + return value; |
| 87 | +} |
| 88 | + |
| 89 | +static void |
| 90 | +nemo_preview_details_init (NemoPreviewDetails *details) |
| 91 | +{ |
| 92 | + NemoPreviewDetailsPrivate *priv; |
| 93 | + GtkGrid *grid; |
| 94 | + |
| 95 | + priv = nemo_preview_details_get_instance_private (details); |
| 96 | + |
| 97 | + /* Create the grid for label pairs */ |
| 98 | + grid = GTK_GRID (gtk_grid_new ()); |
| 99 | + gtk_grid_set_row_spacing (grid, 6); |
| 100 | + gtk_grid_set_column_spacing (grid, 12); |
| 101 | + gtk_widget_set_margin_start (GTK_WIDGET (grid), 12); |
| 102 | + gtk_widget_set_margin_end (GTK_WIDGET (grid), 12); |
| 103 | + gtk_widget_set_margin_top (GTK_WIDGET (grid), 12); |
| 104 | + gtk_widget_set_margin_bottom (GTK_WIDGET (grid), 12); |
| 105 | + priv->grid = GTK_WIDGET (grid); |
| 106 | + |
| 107 | + /* Create all the label pairs */ |
| 108 | + priv->name_value_label = create_label_pair (grid, _("Name:"), 0); |
| 109 | + priv->size_value_label = create_label_pair (grid, _("Size:"), 1); |
| 110 | + priv->type_value_label = create_label_pair (grid, _("Type:"), 2); |
| 111 | + priv->modified_value_label = create_label_pair (grid, _("Modified:"), 3); |
| 112 | + priv->permissions_value_label = create_label_pair (grid, _("Permissions:"), 4); |
| 113 | + priv->location_value_label = create_label_pair (grid, _("Location:"), 5); |
| 114 | + |
| 115 | + gtk_box_pack_start (GTK_BOX (details), GTK_WIDGET (grid), FALSE, FALSE, 0); |
| 116 | + gtk_widget_show (GTK_WIDGET (grid)); |
| 117 | +} |
| 118 | + |
| 119 | +static void |
| 120 | +nemo_preview_details_class_init (NemoPreviewDetailsClass *klass) |
| 121 | +{ |
| 122 | + GObjectClass *object_class; |
| 123 | + |
| 124 | + object_class = G_OBJECT_CLASS (klass); |
| 125 | + object_class->finalize = nemo_preview_details_finalize; |
| 126 | +} |
| 127 | + |
| 128 | +GtkWidget * |
| 129 | +nemo_preview_details_new (void) |
| 130 | +{ |
| 131 | + return g_object_new (NEMO_TYPE_PREVIEW_DETAILS, |
| 132 | + "orientation", GTK_ORIENTATION_VERTICAL, |
| 133 | + "spacing", 0, |
| 134 | + NULL); |
| 135 | +} |
| 136 | + |
| 137 | +void |
| 138 | +nemo_preview_details_set_file (NemoPreviewDetails *widget, |
| 139 | + NemoFile *file) |
| 140 | +{ |
| 141 | + NemoPreviewDetailsPrivate *priv; |
| 142 | + gchar *str; |
| 143 | + GFile *location; |
| 144 | + GFile *parent; |
| 145 | + gchar *parent_path; |
| 146 | + |
| 147 | + g_return_if_fail (NEMO_IS_PREVIEW_DETAILS (widget)); |
| 148 | + |
| 149 | + priv = nemo_preview_details_get_instance_private (widget); |
| 150 | + |
| 151 | + if (priv->file == file) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (priv->file != NULL) { |
| 156 | + nemo_file_unref (priv->file); |
| 157 | + } |
| 158 | + |
| 159 | + priv->file = file; |
| 160 | + |
| 161 | + if (file != NULL) { |
| 162 | + nemo_file_ref (file); |
| 163 | + |
| 164 | + /* Name */ |
| 165 | + str = nemo_file_get_display_name (file); |
| 166 | + gtk_label_set_text (GTK_LABEL (priv->name_value_label), str); |
| 167 | + g_free (str); |
| 168 | + |
| 169 | + /* Size */ |
| 170 | + str = nemo_file_get_string_attribute (file, "size"); |
| 171 | + if (str != NULL) { |
| 172 | + gtk_label_set_text (GTK_LABEL (priv->size_value_label), str); |
| 173 | + g_free (str); |
| 174 | + } else { |
| 175 | + gtk_label_set_text (GTK_LABEL (priv->size_value_label), "—"); |
| 176 | + } |
| 177 | + |
| 178 | + /* Type */ |
| 179 | + str = nemo_file_get_string_attribute (file, "type"); |
| 180 | + if (str != NULL) { |
| 181 | + gtk_label_set_text (GTK_LABEL (priv->type_value_label), str); |
| 182 | + g_free (str); |
| 183 | + } else { |
| 184 | + gtk_label_set_text (GTK_LABEL (priv->type_value_label), "—"); |
| 185 | + } |
| 186 | + |
| 187 | + /* Modified */ |
| 188 | + str = nemo_file_get_string_attribute (file, "date_modified"); |
| 189 | + if (str != NULL) { |
| 190 | + gtk_label_set_text (GTK_LABEL (priv->modified_value_label), str); |
| 191 | + g_free (str); |
| 192 | + } else { |
| 193 | + gtk_label_set_text (GTK_LABEL (priv->modified_value_label), "—"); |
| 194 | + } |
| 195 | + |
| 196 | + /* Permissions */ |
| 197 | + str = nemo_file_get_string_attribute (file, "permissions"); |
| 198 | + if (str != NULL) { |
| 199 | + gtk_label_set_text (GTK_LABEL (priv->permissions_value_label), str); |
| 200 | + g_free (str); |
| 201 | + } else { |
| 202 | + gtk_label_set_text (GTK_LABEL (priv->permissions_value_label), "—"); |
| 203 | + } |
| 204 | + |
| 205 | + /* Location */ |
| 206 | + location = nemo_file_get_location (file); |
| 207 | + parent = g_file_get_parent (location); |
| 208 | + if (parent != NULL) { |
| 209 | + parent_path = g_file_get_parse_name (parent); |
| 210 | + gtk_label_set_text (GTK_LABEL (priv->location_value_label), parent_path); |
| 211 | + g_free (parent_path); |
| 212 | + g_object_unref (parent); |
| 213 | + } else { |
| 214 | + gtk_label_set_text (GTK_LABEL (priv->location_value_label), "—"); |
| 215 | + } |
| 216 | + g_object_unref (location); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +void |
| 221 | +nemo_preview_details_clear (NemoPreviewDetails *widget) |
| 222 | +{ |
| 223 | + NemoPreviewDetailsPrivate *priv; |
| 224 | + |
| 225 | + g_return_if_fail (NEMO_IS_PREVIEW_DETAILS (widget)); |
| 226 | + |
| 227 | + priv = nemo_preview_details_get_instance_private (widget); |
| 228 | + |
| 229 | + if (priv->file != NULL) { |
| 230 | + nemo_file_unref (priv->file); |
| 231 | + priv->file = NULL; |
| 232 | + } |
| 233 | + |
| 234 | + gtk_label_set_text (GTK_LABEL (priv->name_value_label), ""); |
| 235 | + gtk_label_set_text (GTK_LABEL (priv->size_value_label), ""); |
| 236 | + gtk_label_set_text (GTK_LABEL (priv->type_value_label), ""); |
| 237 | + gtk_label_set_text (GTK_LABEL (priv->modified_value_label), ""); |
| 238 | + gtk_label_set_text (GTK_LABEL (priv->permissions_value_label), ""); |
| 239 | + gtk_label_set_text (GTK_LABEL (priv->location_value_label), ""); |
| 240 | +} |
0 commit comments