1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
* Copyright (C) 2022 The GNOME project contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "nautilus-name-cell.h"
#include "nautilus-file-utilities.h"
#define LOADING_TIMEOUT_SECONDS 1
struct _NautilusNameCell
{
NautilusViewCell parent_instance;
GSignalGroup *item_signal_group;
GQuark path_attribute_q;
GFile *file_path_base_location;
GtkWidget *expander;
GtkWidget *content;
GtkWidget *fixed_height_box;
GtkWidget *spinner;
GtkWidget *icon;
GtkWidget *label;
GtkWidget *emblems_box;
GtkWidget *snippet_button;
GtkWidget *snippet;
GtkWidget *path;
gboolean show_snippet;
guint loading_timeout_id;
};
G_DEFINE_TYPE (NautilusNameCell, nautilus_name_cell, NAUTILUS_TYPE_VIEW_CELL)
static gchar *
get_path_text (NautilusFile *file,
GQuark path_attribute_q,
GFile *base_location)
{
g_autofree gchar *path = NULL;
g_autoptr (GFile) dir_location = NULL;
g_autoptr (GFile) home_location = g_file_new_for_path (g_get_home_dir ());
g_autoptr (GFile) root_location = g_file_new_for_path ("/");
GFile *relative_location_base;
if (path_attribute_q == 0)
{
return NULL;
}
path = nautilus_file_get_string_attribute_q (file, path_attribute_q);
dir_location = g_file_new_for_commandline_arg (path);
if (base_location != NULL && g_file_equal (base_location, dir_location))
{
/* Only occurs when search result is
* a direct child of the base location
*/
return NULL;
}
if (g_file_equal (dir_location, home_location))
{
return nautilus_compute_title_for_location (home_location);
}
relative_location_base = base_location;
if (relative_location_base == NULL)
{
/* Only occurs in Recent, Starred and Trash. */
relative_location_base = home_location;
}
if (!g_file_equal (relative_location_base, root_location) &&
g_file_has_prefix (dir_location, relative_location_base))
{
g_autofree gchar *relative_path = NULL;
g_autofree gchar *display_name = NULL;
relative_path = g_file_get_relative_path (relative_location_base, dir_location);
display_name = g_filename_display_name (relative_path);
/* Ensure a trailing slash to emphasize it is a directory */
if (g_str_has_suffix (display_name, G_DIR_SEPARATOR_S))
{
return g_steal_pointer (&display_name);
}
return g_strconcat (display_name, G_DIR_SEPARATOR_S, NULL);
}
return g_steal_pointer (&path);
}
static void
update_labels (NautilusNameCell *self)
{
g_autoptr (NautilusViewItem) item = NULL;
NautilusFile *file;
g_autofree gchar *display_name = NULL;
g_autofree gchar *path_text = NULL;
const gchar *fts_snippet = NULL;
item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_return_if_fail (item != NULL);
file = nautilus_view_item_get_file (item);
display_name = nautilus_file_get_display_name (file);
path_text = get_path_text (file,
self->path_attribute_q,
self->file_path_base_location);
if (self->show_snippet)
{
fts_snippet = nautilus_file_get_search_fts_snippet (file);
}
gtk_label_set_text (GTK_LABEL (self->label), display_name);
gtk_label_set_text (GTK_LABEL (self->path), path_text);
gtk_label_set_markup (GTK_LABEL (self->snippet), fts_snippet);
gtk_widget_set_visible (self->path, (path_text != NULL));
gtk_widget_set_visible (self->snippet_button, (fts_snippet != NULL));
}
static void
update_icon (NautilusNameCell *self)
{
NautilusFileIconFlags flags;
g_autoptr (GdkPaintable) icon_paintable = NULL;
g_autoptr (NautilusViewItem) item = NULL;
NautilusFile *file;
guint icon_size;
gint scale_factor;
int icon_height;
int extra_margin;
g_autofree gchar *thumbnail_path = NULL;
item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_return_if_fail (item != NULL);
file = nautilus_view_item_get_file (item);
icon_size = nautilus_view_item_get_icon_size (item);
scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (self));
flags = NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS;
icon_paintable = nautilus_file_get_icon_paintable (file, icon_size, scale_factor, flags);
gtk_picture_set_paintable (GTK_PICTURE (self->icon), icon_paintable);
/* Set the same width for all icons regardless of aspect ratio.
* Don't set the width here because it would get GtkPicture w4h confused.
*/
gtk_widget_set_size_request (self->fixed_height_box, icon_size, -1);
/* Give all items the same minimum width. This cannot be done by setting the
* width request directly, as above, because it would get mess up with
* height for width calculations.
*
* Instead we must add margins on both sides of the icon which, summed up
* with the icon's actual width, equal the desired item width. */
icon_height = gdk_paintable_get_intrinsic_height (icon_paintable);
extra_margin = (icon_size - icon_height) / 2;
gtk_widget_set_margin_top (self->fixed_height_box, extra_margin);
gtk_widget_set_margin_bottom (self->fixed_height_box, extra_margin);
thumbnail_path = nautilus_file_get_thumbnail_path (file);
if (icon_size >= NAUTILUS_THUMBNAIL_MINIMUM_ICON_SIZE &&
thumbnail_path != NULL &&
nautilus_file_should_show_thumbnail (file))
{
gtk_widget_add_css_class (self->icon, "thumbnail");
}
else
{
gtk_widget_remove_css_class (self->icon, "thumbnail");
}
}
static void
update_emblems (NautilusNameCell *self)
{
g_autoptr (NautilusViewItem) item = NULL;
NautilusFile *file;
GtkWidget *child;
GtkIconTheme *theme;
g_autolist (GIcon) emblems = NULL;
item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_return_if_fail (item != NULL);
file = nautilus_view_item_get_file (item);
/* Remove old emblems. */
while ((child = gtk_widget_get_first_child (self->emblems_box)) != NULL)
{
gtk_box_remove (GTK_BOX (self->emblems_box), child);
}
theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
emblems = nautilus_file_get_emblem_icons (file);
for (GList *l = emblems; l != NULL; l = l->next)
{
if (!gtk_icon_theme_has_gicon (theme, l->data))
{
g_autofree gchar *icon_string = g_icon_to_string (l->data);
g_warning ("Failed to add emblem. ā%sā not found in the icon theme",
icon_string);
continue;
}
gtk_box_append (GTK_BOX (self->emblems_box),
gtk_image_new_from_gicon (l->data));
}
}
static void
on_file_changed (NautilusNameCell *self)
{
update_icon (self);
update_labels (self);
update_emblems (self);
}
static void
on_item_size_changed (NautilusNameCell *self)
{
update_icon (self);
}
static void
on_item_drag_accept_changed (NautilusNameCell *self)
{
gboolean drag_accept;
g_autoptr (NautilusViewItem) item = NULL;
GtkWidget *list_row = gtk_widget_get_parent (gtk_widget_get_parent (GTK_WIDGET (self)));
item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_object_get (item, "drag-accept", &drag_accept, NULL);
if (drag_accept)
{
gtk_widget_set_state_flags (list_row, GTK_STATE_FLAG_DROP_ACTIVE, FALSE);
}
else
{
gtk_widget_unset_state_flags (list_row, GTK_STATE_FLAG_DROP_ACTIVE);
}
}
static void
on_item_is_cut_changed (NautilusNameCell *self)
{
gboolean is_cut;
g_autoptr (NautilusViewItem) item = NULL;
item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_object_get (item,
"is-cut", &is_cut,
NULL);
if (is_cut)
{
gtk_widget_add_css_class (self->icon, "cut");
}
else
{
gtk_widget_remove_css_class (self->icon, "cut");
}
}
static gboolean
on_loading_timeout (gpointer user_data)
{
NautilusNameCell *self = NAUTILUS_NAME_CELL (user_data);
gboolean is_loading;
self->loading_timeout_id = 0;
g_object_get (nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self)),
"is-loading", &is_loading,
NULL);
if (is_loading)
{
gtk_widget_set_visible (self->spinner, TRUE);
gtk_spinner_start (GTK_SPINNER (self->spinner));
}
return G_SOURCE_REMOVE;
}
static void
on_item_is_loading_changed (NautilusNameCell *self)
{
gboolean is_loading;
NautilusViewItem *item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));
g_clear_handle_id (&self->loading_timeout_id, g_source_remove);
g_object_get (item, "is-loading", &is_loading, NULL);
if (is_loading)
{
self->loading_timeout_id = g_timeout_add_seconds (LOADING_TIMEOUT_SECONDS,
G_SOURCE_FUNC (on_loading_timeout),
self);
}
else
{
gtk_widget_set_visible (self->spinner, FALSE);
gtk_spinner_stop (GTK_SPINNER (self->spinner));
}
}
static void
nautilus_name_cell_init (NautilusNameCell *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
/* Connect automatically to an item. */
self->item_signal_group = g_signal_group_new (NAUTILUS_TYPE_VIEW_ITEM);
g_signal_group_connect_swapped (self->item_signal_group, "notify::icon-size",
(GCallback) on_item_size_changed, self);
g_signal_group_connect_swapped (self->item_signal_group, "notify::drag-accept",
(GCallback) on_item_drag_accept_changed, self);
g_signal_group_connect_swapped (self->item_signal_group, "notify::is-cut",
(GCallback) on_item_is_cut_changed, self);
g_signal_group_connect_swapped (self->item_signal_group, "notify::is-loading",
(GCallback) on_item_is_loading_changed, self);
g_signal_group_connect_swapped (self->item_signal_group, "file-changed",
(GCallback) on_file_changed, self);
g_signal_connect_object (self->item_signal_group, "bind",
(GCallback) on_file_changed, self,
G_CONNECT_SWAPPED);
g_object_bind_property (self, "item",
self->item_signal_group, "target",
G_BINDING_SYNC_CREATE);
}
static void
nautilus_name_cell_finalize (GObject *object)
{
NautilusNameCell *self = (NautilusNameCell *) object;
g_clear_object (&self->item_signal_group);
g_clear_object (&self->file_path_base_location);
G_OBJECT_CLASS (nautilus_name_cell_parent_class)->finalize (object);
}
static void
nautilus_name_cell_class_init (NautilusNameCellClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->finalize = nautilus_name_cell_finalize;
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/nautilus/ui/nautilus-name-cell.ui");
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, expander);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, content);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, fixed_height_box);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, spinner);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, icon);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, label);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, emblems_box);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, snippet_button);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, snippet);
gtk_widget_class_bind_template_child (widget_class, NautilusNameCell, path);
}
NautilusViewCell *
nautilus_name_cell_new (NautilusListBase *view)
{
return NAUTILUS_VIEW_CELL (g_object_new (NAUTILUS_TYPE_NAME_CELL,
"view", view,
NULL));
}
void
nautilus_name_cell_set_path (NautilusNameCell *self,
GQuark path_attribute_q,
GFile *base_location)
{
self->path_attribute_q = path_attribute_q;
g_set_object (&self->file_path_base_location, base_location);
}
void
nautilus_name_cell_show_snippet (NautilusNameCell *self)
{
self->show_snippet = TRUE;
}
GtkTreeExpander *
nautilus_name_cell_get_expander (NautilusNameCell *self)
{
return GTK_TREE_EXPANDER (self->expander);
}
GtkWidget *
nautilus_name_cell_get_content (NautilusNameCell *self)
{
return self->content;
}
|