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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* nautilus-previewer: nautilus previewer DBus wrapper
*
* Copyright (C) 2011, Red Hat, Inc.
*
* Nautilus is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Nautilus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Cosimo Cecchi <cosimoc@redhat.com>
*
*/
#define G_LOG_DOMAIN "nautilus-previewer"
#include "config.h"
#include "nautilus-previewer.h"
#include "nautilus-file.h"
#include "nautilus-files-view.h"
#include "nautilus-window.h"
#include "nautilus-window-slot.h"
#include <gio/gio.h>
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/wayland/gdkwayland.h>
#endif
#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#endif
#define PREVIEWER2_DBUS_IFACE "org.gnome.NautilusPreviewer2"
#define PREVIEWER_DBUS_NAME "org.gnome.NautilusPreviewer"
#define PREVIEWER_DBUS_PATH "/org/gnome/NautilusPreviewer"
static const char *previewer_dbus_name = PREVIEWER_DBUS_NAME PROFILE;
static const char *previewer_dbus_path = PREVIEWER_DBUS_PATH PROFILE;
static gboolean tried_alternative_previewer_dbus_name = FALSE;
static gboolean previewer_ready = FALSE;
static gboolean fetching_bus = FALSE;
static GDBusProxy *previewer_proxy = NULL;
enum
{
DBUS_SUBSCRIPTION_NAVIGATE_TO,
DBUS_SUBSCRIPTION_RENAME,
DBUS_SUBSCRIPTION_SELECTION,
DBUS_SUBSCRIPTION_TRASH,
NUM_DBUS_SUBSCRIPTIONS
};
static guint dbus_subscriptions[NUM_DBUS_SUBSCRIPTIONS];
static GCancellable *cancellable = NULL;
static NautilusWindowSlot *current_slot = NULL; /* weak ref */
static GtkRoot *current_window = NULL; /* weak ref */
static gchar *exported_window_handle = NULL;
static void real_call_show_file (const gchar *uri,
const gchar *window_handle,
gboolean close_if_already_visible,
const char *activation_token);
static void previewer_call_close (void);
static void create_new_bus (void);
#ifdef GDK_WINDOWING_WAYLAND
typedef struct
{
gchar *uri;
gboolean close_if_already_visible;
char *activation_token;
} PreviewExportData;
static void
preview_export_data_free (gpointer _data)
{
PreviewExportData *data = _data;
g_free (data->uri);
g_free (data->activation_token);
g_free (data);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PreviewExportData, preview_export_data_free)
static void
wayland_window_handle_exported (GdkToplevel *toplevel,
const char *wayland_handle_str,
gpointer user_data)
{
PreviewExportData *data = user_data;
g_autofree char *wayland_handle = g_strdup_printf ("wayland:%s", wayland_handle_str);
real_call_show_file (data->uri, wayland_handle, data->close_if_already_visible, data->activation_token);
}
#endif
static void
clear_exported_window_handle (void)
{
if (exported_window_handle == NULL)
{
return;
}
#ifdef GDK_WINDOWING_WAYLAND
if (current_window != NULL &&
GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (GTK_WIDGET (current_window))))
{
GdkSurface *gdk_surface = gtk_native_get_surface (GTK_NATIVE (current_window));
if (GDK_IS_WAYLAND_TOPLEVEL (gdk_surface) &&
g_str_has_prefix (exported_window_handle, "wayland:"))
{
gdk_wayland_toplevel_drop_exported_handle (GDK_WAYLAND_TOPLEVEL (gdk_surface),
exported_window_handle + strlen ("wayland:"));
}
}
#endif
g_free (exported_window_handle);
exported_window_handle = NULL;
}
static gboolean
is_uri_selected (NautilusFilesView *files_view,
const char *uri)
{
g_autolist (NautilusFile) selection = nautilus_files_view_get_selection (files_view);
if (g_list_length (selection) != 1)
{
return FALSE;
}
g_autoptr (NautilusFile) file = nautilus_file_get_by_uri (uri);
return file == selection->data;
}
typedef void (*PreviewerEventCallback) (NautilusFilesView *files_view,
GVariant *parameters);
static void
previewer_navigate_to_event (NautilusFilesView *files_view,
GVariant *parameters)
{
const char *uri;
g_variant_get (parameters, "(s)", &uri);
g_autoptr (NautilusFile) file = nautilus_file_get_by_uri (uri);
if (!nautilus_file_is_directory (file) || !is_uri_selected (files_view, uri))
{
g_warning ("Ignoring previewer NavigateTo request for uri %s", uri);
return;
}
nautilus_files_view_activate_file (files_view, file, NAUTILUS_OPEN_FLAG_NORMAL);
}
static void
previewer_rename_event (NautilusFilesView *files_view,
GVariant *parameters)
{
const char *uri;
g_variant_get (parameters, "(s)", &uri);
if (!is_uri_selected (files_view, uri))
{
g_warning ("Ignoring previewer Rename request for uri %s", uri);
return;
}
gtk_widget_activate_action (GTK_WIDGET (files_view), "view.rename", NULL);
}
static void
previewer_selection_event (NautilusFilesView *files_view,
GVariant *parameters)
{
GtkDirectionType direction;
g_variant_get (parameters, "(u)", &direction);
nautilus_files_view_preview_selection_event (files_view, direction);
}
static void
previewer_trash_event (NautilusFilesView *files_view,
GVariant *parameters)
{
const char *uri;
g_variant_get (parameters, "(s)", &uri);
if (!is_uri_selected (files_view, uri))
{
g_warning ("Ignoring previewer Trash request for uri %s", uri);
return;
}
gtk_widget_activate_action (GTK_WIDGET (files_view), "view.move-to-trash", NULL);
}
static void
handle_previewer_event (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
PreviewerEventCallback callback = user_data;
NautilusFilesView *files_view = (current_slot != NULL) ?
nautilus_window_slot_get_current_view (current_slot) : NULL;
if (files_view == NULL)
{
return;
}
callback (files_view, parameters);
}
static guint
setup_dbus_connection (GDBusConnection *connection,
const char *event_name,
PreviewerEventCallback callback)
{
return g_dbus_connection_signal_subscribe (connection,
previewer_dbus_name,
PREVIEWER2_DBUS_IFACE,
event_name,
previewer_dbus_path,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
handle_previewer_event,
callback,
NULL);
}
static void
switch_to_alternative_previewer_dbus_name (void)
{
tried_alternative_previewer_dbus_name = TRUE;
if (g_str_has_suffix (previewer_dbus_name, "Devel"))
{
previewer_dbus_name = PREVIEWER_DBUS_NAME;
previewer_dbus_path = PREVIEWER_DBUS_PATH;
}
else
{
previewer_dbus_name = PREVIEWER_DBUS_NAME "Devel";
previewer_dbus_path = PREVIEWER_DBUS_PATH "Devel";
}
}
static void
on_ping_finished (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
g_autoptr (GError) error = NULL;
g_autoptr (GVariant) variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (object), res, &error);
if (error == NULL)
{
GDBusConnection *connection = g_dbus_proxy_get_connection (previewer_proxy);
previewer_ready = TRUE;
fetching_bus = FALSE;
dbus_subscriptions[DBUS_SUBSCRIPTION_NAVIGATE_TO] =
setup_dbus_connection (connection, "NavigateTo", previewer_navigate_to_event);
dbus_subscriptions[DBUS_SUBSCRIPTION_RENAME] =
setup_dbus_connection (connection, "Rename", previewer_rename_event);
dbus_subscriptions[DBUS_SUBSCRIPTION_SELECTION] =
setup_dbus_connection (connection, "SelectionEvent", previewer_selection_event);
dbus_subscriptions[DBUS_SUBSCRIPTION_TRASH] =
setup_dbus_connection (connection, "Trash", previewer_trash_event);
}
else if (!tried_alternative_previewer_dbus_name)
{
switch_to_alternative_previewer_dbus_name ();
create_new_bus ();
}
else
{
fetching_bus = FALSE;
g_debug ("Unable to create NautilusPreviewer2 proxy: %s", error->message);
}
}
static void
on_bus_ready (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
g_autoptr (GError) error = NULL;
g_clear_object (&previewer_proxy);
previewer_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
if (error == NULL)
{
g_dbus_proxy_call (previewer_proxy,
"org.freedesktop.DBus.Peer.Ping", NULL,
G_DBUS_CALL_FLAGS_NONE, G_MAXINT,
cancellable, on_ping_finished, NULL);
}
else if (!tried_alternative_previewer_dbus_name)
{
switch_to_alternative_previewer_dbus_name ();
create_new_bus ();
}
else
{
fetching_bus = FALSE;
g_debug ("Unable to create NautilusPreviewer2 proxy: %s", error->message);
}
}
static void
create_new_bus (void)
{
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION,
NULL,
previewer_dbus_name,
previewer_dbus_path,
PREVIEWER2_DBUS_IFACE,
cancellable,
on_bus_ready,
NULL);
}
static gboolean
ensure_previewer_proxy (void)
{
if (previewer_ready)
{
return TRUE;
}
if (fetching_bus)
{
return FALSE;
}
fetching_bus = TRUE;
create_new_bus ();
return FALSE;
}
static void
previewer2_method_ready_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GDBusProxy *proxy = G_DBUS_PROXY (source);
g_autoptr (GError) error = NULL;
g_dbus_proxy_call_finish (proxy, res, &error);
if (error != NULL)
{
g_debug ("Unable to call method on NautilusPreviewer: %s", error->message);
}
}
void
nautilus_previewer_call_show_file (const gchar *uri,
NautilusWindowSlot *slot,
gboolean close_if_already_visible)
{
g_set_weak_pointer (¤t_slot, slot);
GtkRoot *window = gtk_widget_get_root (GTK_WIDGET (slot));
g_autoptr (GdkAppLaunchContext) launch_context = gdk_display_get_app_launch_context (
gtk_root_get_display (window));
g_autofree char *activation_token = g_app_launch_context_get_startup_notify_id (
G_APP_LAUNCH_CONTEXT (launch_context),
NULL,
NULL);
/* Reuse existing handle if called again for the same window. */
if (current_window == window &&
exported_window_handle != NULL)
{
real_call_show_file (uri, exported_window_handle, close_if_already_visible, activation_token);
return;
}
/* Otherwise, obtain a new window handle. */
clear_exported_window_handle ();
g_set_weak_pointer (¤t_window, window);
g_signal_connect_object (g_application_get_default (), "last-window-closed",
G_CALLBACK (previewer_call_close), window, 0);
GdkSurface *gdk_surface = gtk_native_get_surface (GTK_NATIVE (window));
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_DISPLAY (gtk_widget_get_display (GTK_WIDGET (window))))
{
guint xid = (guint) gdk_x11_surface_get_xid (gdk_surface);
g_autofree char *window_handle = g_strdup_printf ("x11:%x", xid);
real_call_show_file (uri, window_handle, close_if_already_visible, activation_token);
return;
}
#endif
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (GTK_WIDGET (window))))
{
g_autoptr (PreviewExportData) data = g_new0 (PreviewExportData, 1);
data->uri = g_strdup (uri);
data->close_if_already_visible = close_if_already_visible;
data->activation_token = g_strdup (activation_token);
if (gdk_wayland_toplevel_export_handle (GDK_WAYLAND_TOPLEVEL (gdk_surface),
wayland_window_handle_exported,
data,
preview_export_data_free))
{
/* Don't let autoptr free data successfully taken by the call. */
(void) g_steal_pointer (&data);
return;
}
}
#endif
g_warning ("Couldn't export handle, unsupported windowing system");
/* Let's use a fallback, so at least a preview will be displayed */
real_call_show_file (uri, "x11:0", close_if_already_visible, activation_token);
}
static void
real_call_show_file (const gchar *uri,
const gchar *window_handle,
gboolean close_if_already_visible,
const char *activation_token)
{
g_set_str (&exported_window_handle, window_handle);
if (!ensure_previewer_proxy ())
{
return;
}
if (activation_token == NULL)
{
activation_token = "";
}
GVariant *parameters = g_variant_new (
"(ssbs)",
uri, window_handle, close_if_already_visible, activation_token);
g_dbus_proxy_call (previewer_proxy,
"ShowFile",
parameters,
G_DBUS_CALL_FLAGS_NONE,
-1,
cancellable,
previewer2_method_ready_cb,
NULL);
}
static void
previewer_call_close (void)
{
if (!ensure_previewer_proxy ())
{
return;
}
/* don't autostart the previewer if it's not running */
g_dbus_proxy_call (previewer_proxy,
"Close",
NULL,
G_DBUS_CALL_FLAGS_NO_AUTO_START,
-1,
cancellable,
previewer2_method_ready_cb,
NULL);
}
void
nautilus_previewer_setup (void)
{
ensure_previewer_proxy ();
}
void
nautilus_previewer_teardown (GDBusConnection *connection)
{
for (guint i = 0; i < NUM_DBUS_SUBSCRIPTIONS; i += 1)
{
if (dbus_subscriptions[i] != 0)
{
g_dbus_connection_signal_unsubscribe (connection, dbus_subscriptions[i]);
}
}
g_cancellable_cancel (cancellable);
g_clear_object (&cancellable);
g_clear_object (&previewer_proxy);
clear_exported_window_handle ();
g_clear_weak_pointer (¤t_window);
g_clear_weak_pointer (¤t_slot);
}
gboolean
nautilus_previewer_is_visible (void)
{
if (!ensure_previewer_proxy ())
{
return FALSE;
}
g_autoptr (GVariant) variant = g_dbus_proxy_get_cached_property (previewer_proxy, "Visible");
if (variant)
{
return g_variant_get_boolean (variant);
}
return FALSE;
}
|