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
|
/*
* Copyright (C) 2024 GNOME Foundation Inc.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Original Author: António Fernandes <antoniof@gnome.org>
*/
#include "nautilus-internal-place-file.h"
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "nautilus-file-private.h"
#include "nautilus-scheme.h"
#include "nautilus-network-directory.h"
#include "nautilus-starred-directory.h"
struct _NautilusInternalPlaceFile
{
NautilusFile parent_instance;
GList *callbacks;
GCancellable *network_mount_cancellable;
};
G_DEFINE_FINAL_TYPE (NautilusInternalPlaceFile, nautilus_internal_place_file, NAUTILUS_TYPE_FILE);
static void
real_monitor_add (NautilusFile *file,
gconstpointer client,
NautilusAttributes attributes)
{
/* Internal place attributes are static, so there is nothing to monitor. */
}
static void
real_monitor_remove (NautilusFile *file,
gconstpointer client)
{
}
typedef struct
{
NautilusFileCallback callback;
gpointer callback_data;
} InternalPlaceFileCallback;
static void
network_mount_callback (GObject *source_object,
GAsyncResult *result,
gpointer data)
{
g_autoptr (GError) error = NULL;
if (!g_file_mount_enclosing_volume_finish (G_FILE (source_object), result, &error) &&
!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED))
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_warning ("Could not mount '%s': %s", SCHEME_NETWORK ":///", error->message);
}
return;
}
NautilusInternalPlaceFile *self = NAUTILUS_INTERNAL_PLACE_FILE (data);
/* Clear cancellable and steal callbacks list because call_when_ready() may
* be called again inside the `for` loop. */
g_clear_object (&self->network_mount_cancellable);
GList *callbacks = g_steal_pointer (&self->callbacks);
for (GList *l = callbacks; l != NULL; l = l->next)
{
InternalPlaceFileCallback *callback = l->data;
(*callback->callback)(NAUTILUS_FILE (self), callback->callback_data);
}
g_list_free_full (callbacks, g_free);
}
static void
real_call_when_ready (NautilusFile *file,
NautilusAttributes attributes,
NautilusFileCallback callback,
gpointer callback_data)
{
NautilusInternalPlaceFile *self = NAUTILUS_INTERNAL_PLACE_FILE (file);
if (NAUTILUS_IS_NETWORK_DIRECTORY (file->details->directory) &&
!g_test_initialized ())
{
/* WORKAROUND:
* We must ensure network:/// is mounted before calling it "ready",
* otherwise GDaemonFile makes sync D-Bus calls to mount network://
* when the view tries do add a monitor, blocking the main thread and
* freezing the UI.
* See: https://gitlab.gnome.org/GNOME/gvfs/-/issues/455
*/
if (callback != NULL)
{
InternalPlaceFileCallback data = {callback, callback_data};
self->callbacks = g_list_prepend (self->callbacks,
g_memdup2 (&data, sizeof (data)));
}
/* If there is a cancellable, we are already mounting */
if (self->network_mount_cancellable == NULL)
{
g_autoptr (GFile) network_backend = g_file_new_for_uri (SCHEME_NETWORK ":///");
self->network_mount_cancellable = g_cancellable_new ();
g_file_mount_enclosing_volume (network_backend,
G_MOUNT_MOUNT_NONE,
NULL,
self->network_mount_cancellable,
network_mount_callback, self);
}
}
else if (callback != NULL)
{
/* Internal place attributes are static, so its always ready. */
(*callback)(file, callback_data);
}
}
static void
real_cancel_call_when_ready (NautilusFile *file,
NautilusFileCallback callback,
gpointer callback_data)
{
NautilusInternalPlaceFile *self = NAUTILUS_INTERNAL_PLACE_FILE (file);
g_clear_list (&self->callbacks, g_free);
g_cancellable_cancel (self->network_mount_cancellable);
g_clear_object (&self->network_mount_cancellable);
}
static gboolean
real_check_if_ready (NautilusFile *file,
NautilusAttributes attributes)
{
/* Internal place attributes are static, so its always ready. */
return TRUE;
}
static void
nautilus_internal_place_file_init (NautilusInternalPlaceFile *self)
{
}
static void
nautilus_internal_place_file_constructed (GObject *object)
{
G_OBJECT_CLASS (nautilus_internal_place_file_parent_class)->constructed (object);
NautilusInternalPlaceFile *self = NAUTILUS_INTERNAL_PLACE_FILE (object);
NautilusFile *file = NAUTILUS_FILE (self);
file->details->mime_type = g_ref_string_new_intern ("inode/directory");
file->details->size = 0;
if (NAUTILUS_IS_NETWORK_DIRECTORY (file->details->directory))
{
nautilus_file_set_display_name (file, _("Network"), NULL, TRUE);
}
else if (NAUTILUS_IS_STARRED_DIRECTORY (file->details->directory))
{
nautilus_file_set_display_name (file, _("Starred"), NULL, TRUE);
}
file->details->got_file_info = TRUE;
file->details->file_info_is_up_to_date = TRUE;
}
static void
nautilus_internal_place_file_dispose (GObject *object)
{
NautilusInternalPlaceFile *self = NAUTILUS_INTERNAL_PLACE_FILE (object);
g_clear_list (&self->callbacks, g_free);
g_cancellable_cancel (self->network_mount_cancellable);
g_clear_object (&self->network_mount_cancellable);
G_OBJECT_CLASS (nautilus_internal_place_file_parent_class)->dispose (object);
}
static void
nautilus_internal_place_file_class_init (NautilusInternalPlaceFileClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NautilusFileClass *file_class = NAUTILUS_FILE_CLASS (klass);
/* We need to know the parent directory, which is a construction property.*/
object_class->constructed = nautilus_internal_place_file_constructed;
object_class->dispose = nautilus_internal_place_file_dispose;
file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
file_class->monitor_add = real_monitor_add;
file_class->monitor_remove = real_monitor_remove;
file_class->call_when_ready = real_call_when_ready;
file_class->cancel_call_when_ready = real_cancel_call_when_ready;
file_class->check_if_ready = real_check_if_ready;
}
|