Debian Codegraph @

portal-test/gtk3/portal-test-win.c

raw
1 /*
2 * Copyright (C) 2018, Matthias Clasen
3 * Copyright (C) 2024 GNOME Foundation, Inc.
4 *
5 * This file is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.0 of the
8 * License.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-3.0-only
19 *
20 * Authors:
21 * Matthias Clasen <[email protected]>
22 * Hubert Figuière <[email protected]>
23 */
24
25 #include "config.h"
26
27 #include <gtk/gtk.h>
28 #include <gio/gdesktopappinfo.h>
29 #include <gio/gunixinputstream.h>
30 #include <gio/gunixoutputstream.h>
31 #include <gio/gunixfdlist.h>
32
33 #include <gst/gst.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <string.h>
39
40 #include "libportal/settings.h"
41 #include "libportal/portal-gtk3.h"
42
43 #include "portal-test-app.h"
44 #include "portal-test-win.h"
45
46 #ifdef GDK_WINDOWING_X11
47 #include <gdk/gdkx.h>
48 #endif
49
50 #ifdef GDK_WINDOWING_WAYLAND
51 #include <gdk/gdkwayland.h>
52 #endif
53
54 struct _PortalTestWin
55 {
56 GtkApplicationWindow parent;
57 GtkWidget *sandbox_status;
58 GtkWidget *network_status;
59 GtkWidget *monitor_name;
60 GtkWidget *resolver_name;
61 GtkWidget *proxies;
62 GtkWidget *encoding;
63 GtkWidget *ack_image;
64
65 XdpPortal *portal;
66 XdpSession *session;
67
68 XdpInputCaptureSession *input_capture_session;
69
70 GNetworkMonitor *monitor;
71 GProxyResolver *resolver;
72
73 GtkWidget *image;
74 GtkWidget *interactive;
75
76 GtkWidget *inhibit_idle;
77 GtkWidget *inhibit_logout;
78 GtkWidget *inhibit_suspend;
79 GtkWidget *inhibit_switch;
80 int inhibit_cookie;
81 GtkApplicationInhibitFlags inhibit_flags;
82
83 GtkWidget *username;
84 GtkWidget *realname;
85 GtkWidget *avatar;
86 GtkWidget *save_how;
87 GtkWidget *open_local_ask;
88 GtkWidget *open_local_dir;
89 GtkWidget *file_chooser_button;
90
91 XdpSettings *settings;
92 GtkWidget *settings_value;
93 GtkWidget *settings_count;
94
95 GtkWidget *screencast_label;
96 GtkWidget *screencast_toggle;
97
98 GtkWidget *remotedesktop_label;
99 GtkWidget *remotedesktop_toggle;
100
101 GtkWidget *inputcapture_label;
102 GtkWidget *inputcapture_toggle;
103
104 GFileMonitor *update_monitor;
105 GtkWidget *update_dialog;
106 GtkWidget *update_dialog2;
107 GtkWidget *update_progressbar;
108 GtkWidget *update_label;
109 GtkWidget *ok2;
110
111 GtkWidget *clipboard_label;
112 GtkWidget *clipboard_copy;
113 GtkWidget *clipboard_paste;
114 GtkWidget *clipboard_entry;
115
116 gulong selection_owner_changed;
117 gulong selection_transfer_handler_id;
118 };
119
120 struct _PortalTestWinClass
121 {
122 GtkApplicationWindowClass parent_class;
123 };
124
125 G_DEFINE_TYPE (PortalTestWin, portal_test_win, GTK_TYPE_APPLICATION_WINDOW);
126
127 #define TEST_FILE_PATH APPDATADIR ".Gtk3/test.txt"
128
129 static void update_clipboard (PortalTestWin *win);
130
131 static char *
132 file_path_in_source_dir (const char *file)
133 {
134 if (g_getenv ("MESON_SOURCE_ROOT") == NULL)
135 g_warning ("MESON_SOURCE_ROOT was not set, run this application using `ninja portal-test-gtk3`");
136
137 return g_build_filename (g_getenv ("MESON_SOURCE_ROOT"), "portal-test", "gtk3", file, NULL);
138 }
139
140 static void
141 update_network_status (PortalTestWin *win)
142 {
143 g_autoptr(GString) s = g_string_new ("");
144 GEnumClass *class;
145 GEnumValue *value;
146
147 if (g_network_monitor_get_network_available (win->monitor))
148 g_string_append (s, "available");
149 if (g_network_monitor_get_network_metered (win->monitor))
150 {
151 if (s->len > 0)
152 g_string_append (s, ", ");
153 g_string_append (s, "metered");
154 }
155 class = g_type_class_ref (G_TYPE_NETWORK_CONNECTIVITY);
156 value = g_enum_get_value (class, g_network_monitor_get_connectivity (win->monitor));
157 if (s->len > 0)
158 g_string_append (s, ", ");
159 g_string_append_printf (s, "connectivity=%s", value->value_nick);
160 g_type_class_unref (class);
161
162 gtk_label_set_label (GTK_LABEL (win->network_status), s->str);
163 }
164
165 static gboolean
166 show_restart_dialog (gpointer data)
167 {
168 PortalTestWin *win = data;
169
170 if (gtk_widget_get_visible (win->update_dialog2))
171 return G_SOURCE_CONTINUE;
172
173 gtk_window_present (GTK_WINDOW (win->update_dialog));
174 return G_SOURCE_REMOVE;
175 }
176
177 static void
178 update_monitor_changed (GFileMonitor *monitor,
179 GFile *file,
180 GFile *other,
181 GFileMonitorEvent event,
182 PortalTestWin *win)
183 {
184 if (event == G_FILE_MONITOR_EVENT_CREATED)
185 g_timeout_add_seconds (5, show_restart_dialog, win);
186 }
187
188 static void
189 update_dialog_response (GtkDialog *dialog,
190 int response,
191 PortalTestWin *win)
192 {
193 if (response == GTK_RESPONSE_OK)
194 portal_test_app_restart (PORTAL_TEST_APP (gtk_window_get_application (GTK_WINDOW (win))));
195
196 gtk_widget_hide (win->update_dialog);
197 }
198
199 static void
200 update_available (XdpPortal *portal,
201 const char *running,
202 const char *local,
203 const char *remote,
204 PortalTestWin *win)
205 {
206 g_message ("Update available");
207
208 gtk_label_set_label (GTK_LABEL (win->update_label), "Update available");
209 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (win->update_progressbar), 0.0);
210
211 gtk_window_present (GTK_WINDOW (win->update_dialog2));
212 }
213
214 static void
215 update_progress (XdpPortal *portal,
216 guint n_ops,
217 guint op,
218 guint progress,
219 int status,
220 const char *error,
221 const char *message,
222 PortalTestWin *win)
223 {
224 g_message ("Progress!");
225
226 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (win->update_progressbar), progress / 100.0);
227
228 if (status == XDP_UPDATE_STATUS_FAILED)
229 {
230 gtk_label_set_label (GTK_LABEL (win->update_label), "Something went wrong");
231 gtk_widget_hide (win->update_progressbar);
232 }
233 else if (status == XDP_UPDATE_STATUS_DONE)
234 {
235 gtk_label_set_label (GTK_LABEL (win->update_label), "Installed");
236 }
237
238 if (status != XDP_UPDATE_STATUS_RUNNING)
239 g_signal_handlers_disconnect_by_func (win->portal, update_progress, win);
240 }
241
242 static void
243 update_called (GObject *source,
244 GAsyncResult *result,
245 gpointer data)
246 {
247 PortalTestWin *win = data;
248 g_autoptr(GError) error = NULL;
249
250 if (!xdp_portal_update_install_finish (win->portal, result, &error))
251 {
252 g_warning ("Installation failed: %s", error->message);
253 gtk_widget_hide (win->update_dialog2);
254 }
255
256 gtk_label_set_label (GTK_LABEL (win->update_label), "Installing…");
257 gtk_widget_set_sensitive (win->ok2, FALSE);
258 g_signal_connect (win->portal, "update-progress", G_CALLBACK (update_progress), win);
259 }
260
261 static void
262 update_dialog2_response (GtkDialog *dialog,
263 int response,
264 PortalTestWin *win)
265 {
266 if (response == GTK_RESPONSE_OK)
267 {
268 XdpParent *parent = xdp_parent_new_gtk (GTK_WINDOW (win));
269 xdp_portal_update_install (win->portal, parent, 0, NULL, update_called, win);
270 xdp_parent_free (parent);
271 }
272 else
273 {
274 g_signal_handlers_disconnect_by_func (win->portal, update_available, win);
275 gtk_widget_hide (win->update_dialog2);
276 }
277 }
278
279 static void
280 reload_settings (PortalTestWin *win, GtkButton* button)
281 {
282 g_autoptr(GError) error = NULL;
283 const char *ns[] = { "org.freedesktop.appearance", NULL };
284 guint32 value = xdp_settings_read_uint (win->settings, ns[0], "contrast", NULL, &error);
285 g_autoptr(GVariant) value2 = NULL;
286
287 if (!error)
288 {
289 guint32 uvalue2;
290 g_autofree char *label = g_strdup_printf ("got %u", value);
291 gtk_label_set_text (GTK_LABEL (win->settings_value), label);
292
293 xdp_settings_read (win->settings, ns[0], "contrast", NULL, &error, "u", &uvalue2);
294 if (!error)
295 g_assert (uvalue2 == value);
296 }
297
298
299 value2 = xdp_settings_read_all_values (win->settings, ns, NULL, NULL);
300 if (value2)
301 {
302 gsize count = g_variant_n_children (value2);
303 g_autofree char *count_label = g_strdup_printf ("%lu", count);
304
305 gtk_label_set_text (GTK_LABEL (win->settings_count), count_label);
306 }
307 }
308
309 static void
310 settings_changed (XdpSettings* settings, const gchar *namespace, const gchar *key, GVariant *value, gpointer data)
311 {
312 PortalTestWin *win = data;
313 g_autofree char *label = g_strdup_printf ("chg %s %s", namespace, key);
314
315 gtk_label_set_text (GTK_LABEL (win->settings_value), label);
316 }
317
318 static void
319 portal_test_win_init (PortalTestWin *win)
320 {
321 const char *status;
322 g_auto(GStrv) proxies = NULL;
323 g_autofree char *proxy = NULL;
324 g_autofree char *path = NULL;
325 g_autofree char *filename = NULL;
326 g_autoptr(GError) error = NULL;
327 g_autoptr(GFile) file = NULL;
328 g_autoptr(GFile) dst = NULL;
329 g_autoptr(GFile) src = NULL;
330
331 win->portal = xdp_portal_new ();
332
333 gtk_widget_init_template (GTK_WIDGET (win));
334
335 path = g_build_filename (g_get_user_runtime_dir (), "flatpak-info", NULL);
336 if (g_file_test (path, G_FILE_TEST_EXISTS))
337 status = "confined";
338 else
339 status = "unconfined";
340 gtk_label_set_label (GTK_LABEL (win->sandbox_status), status);
341
342 win->monitor = g_network_monitor_get_default ();
343 gtk_label_set_label (GTK_LABEL (win->monitor_name), G_OBJECT_TYPE_NAME (win->monitor));
344 g_signal_connect_swapped (win->monitor, "notify", G_CALLBACK (update_network_status), win);
345 update_network_status (win);
346
347 win->resolver = g_proxy_resolver_get_default ();
348 gtk_label_set_label (GTK_LABEL (win->resolver_name), G_OBJECT_TYPE_NAME (win->resolver));
349
350 proxies = g_proxy_resolver_lookup (win->resolver, "http://www.flatpak.org", NULL, &error);
351 if (proxies == NULL)
352 {
353 g_print ("Failed to lookup proxies: %s\n", error->message);
354 proxy = g_strdup ("");
355 }
356 else
357 proxy = g_strjoinv (", ", proxies);
358 gtk_label_set_label (GTK_LABEL (win->proxies), proxy);
359
360 file = g_file_new_for_path ("/app/.updated");
361 win->update_monitor = g_file_monitor_file (file, 0, NULL, NULL);
362 g_signal_connect (win->update_monitor, "changed", G_CALLBACK (update_monitor_changed), win);
363
364 g_signal_connect (win->update_dialog, "response", G_CALLBACK (update_dialog_response), win);
365
366 filename = g_build_filename (g_get_user_data_dir (), "test.txt", NULL);
367 dst = g_file_new_for_path (filename);
368 src = g_file_new_for_path (TEST_FILE_PATH);
369 g_file_copy (src, dst, 0, NULL, NULL, NULL, NULL);
370
371 xdp_portal_update_monitor_start (win->portal, 0, NULL, NULL, NULL);
372 g_signal_connect (win->portal, "update-available", G_CALLBACK (update_available), win);
373 g_signal_connect (win->update_dialog2, "response", G_CALLBACK (update_dialog2_response), win);
374
375 win->settings = xdp_portal_get_settings (win->portal);
376 g_signal_connect (win->settings, "changed", G_CALLBACK (settings_changed), win);
377 }
378
379 static void
380 opened_uri (GObject *object,
381 GAsyncResult *result,
382 gpointer data)
383 {
384 XdpPortal *portal = XDP_PORTAL (object);
385 PortalTestWin *win = data;
386 g_autoptr(GError) error = NULL;
387 gboolean open_dir;
388 gboolean res;
389
390 open_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->open_local_dir));
391
392 if (open_dir)
393 res = xdp_portal_open_directory_finish (portal, result, &error);
394 else
395 res = xdp_portal_open_uri_finish (portal, result, &error);
396
397 if (!res)
398 g_warning ("%s", error->message);
399 }
400
401 static void
402 open_local (GtkWidget *button, PortalTestWin *win)
403 {
404 XdpParent *parent;
405 g_autofree char *filename = NULL;
406 g_autoptr(GFile) file = NULL;
407 g_autofree char *uri = NULL;
408 gboolean open_dir;
409 XdpOpenUriFlags flags = XDP_OPEN_URI_FLAG_NONE;
410
411 filename = g_build_filename (g_get_user_data_dir (), "test.txt", NULL);
412 file = g_file_new_for_path (filename);
413 uri = g_file_get_uri (file);
414
415 g_message ("Opening '%s'", uri);
416
417 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->open_local_ask)))
418 flags |= XDP_OPEN_URI_FLAG_ASK;
419
420 open_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->open_local_dir));
421
422 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
423
424 if (open_dir)
425 xdp_portal_open_directory (win->portal, parent, uri, flags, NULL, opened_uri, win);
426 else
427 xdp_portal_open_uri (win->portal, parent, uri, flags, NULL, opened_uri, win);
428 xdp_parent_free (parent);
429 }
430
431 static gboolean
432 write_file_atomically (const char *path, GError **error)
433 {
434 return g_file_set_contents (path, "test", -1, error);
435 }
436
437 static gboolean
438 write_file_gio (const char *path, GError **error)
439 {
440 g_autoptr(GFile) file = NULL;
441 g_autoptr(GOutputStream) stream = NULL;
442
443 file = g_file_new_for_path (path);
444 stream = G_OUTPUT_STREAM (g_file_create (file, 0, NULL, error));
445 if (stream == NULL)
446 return FALSE;
447
448 if (g_output_stream_write (stream, "test", strlen ("test"), NULL, error) < 0)
449 return FALSE;
450
451 if (!g_output_stream_close (stream, NULL, error))
452 return FALSE;
453
454 return TRUE;
455 }
456
457 static gboolean
458 write_file_posix (const char *path, GError **error)
459 {
460 int fd;
461
462 fd = creat (path, 0600);
463 if (fd < 0)
464 {
465 g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errno), "creat failed");
466 return FALSE;
467 }
468
469 if (write (fd, "test", strlen ("test")) < 0)
470 {
471 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "write failed");
472 return FALSE;
473 }
474
475 if (close (fd) < 0)
476 {
477 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "close failed");
478 return FALSE;
479 }
480
481 return TRUE;
482 }
483
484 static void
485 save_dialog (GtkWidget *button, PortalTestWin *win)
486 {
487 gint res;
488 GtkFileChooserNative *dialog;
489 GtkFileChooser *chooser;
490 GtkWindow *parent;
491 const char *options[] = {
492 "current",
493 "iso8859-15",
494 "utf-16",
495 NULL
496 };
497 const char *labels[] = {
498 "Current Locale (UTF-8)",
499 "Western (ISO-8859-15)",
500 "Unicode (UTF-16)",
501 NULL,
502 };
503 const char *encoding;
504 const char *label;
505 g_autofree char *text = NULL;
506 const char *canonicalize;
507 int i;
508
509 parent = GTK_WINDOW (gtk_widget_get_toplevel (button));
510 dialog = gtk_file_chooser_native_new ("File Chooser Portal",
511 parent,
512 GTK_FILE_CHOOSER_ACTION_SAVE,
513 "_Save",
514 "_Cancel");
515 chooser = GTK_FILE_CHOOSER (dialog);
516 #if GTK_CHECK_VERSION(3, 21, 4)
517 gtk_file_chooser_add_choice (chooser,
518 "encoding", "Character Encoding:",
519 options, labels);
520 gtk_file_chooser_set_choice (chooser, "encoding", "current");
521
522 gtk_file_chooser_add_choice (chooser, "canonicalize", "Canonicalize", NULL, NULL);
523 gtk_file_chooser_set_choice (chooser, "canonicalize", "true");
524 #endif
525
526 res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
527 g_message ("Saving file / Response: %d", res);
528 if (res == GTK_RESPONSE_ACCEPT)
529 {
530 const char *method;
531 char *filename;
532 gboolean res = TRUE;
533 g_autoptr(GError) error = NULL;
534
535 method = gtk_combo_box_get_active_id (GTK_COMBO_BOX (win->save_how));
536
537 filename = gtk_file_chooser_get_filename (chooser);
538 g_message ("Saving file: '%s'", filename);
539
540 if (strcmp (method, "atomically") == 0)
541 res = write_file_atomically (filename, &error);
542 else if (strcmp (method, "posix") == 0)
543 res = write_file_posix (filename, &error);
544 else if (strcmp (method, "gio") == 0)
545 res = write_file_gio (filename, &error);
546 else
547 g_message ("Not writing");
548
549 if (!res)
550 g_message ("Failed: %s", error->message);
551 g_free (filename);
552 }
553
554 #if GTK_CHECK_VERSION(3, 21, 4)
555 encoding = gtk_file_chooser_get_choice (chooser, "encoding");
556 canonicalize = gtk_file_chooser_get_choice (chooser, "canonicalize");
557 #endif
558
559 label = "";
560 for (i = 0; options[i]; i++)
561 {
562 if (g_strcmp0 (encoding, options[i]) == 0)
563 label = labels[i];
564 }
565
566 text = g_strdup_printf ("%s%s", label, g_str_equal (canonicalize, "true") ? " (canon)" : "");
567 gtk_label_set_label (GTK_LABEL (win->encoding), text);
568
569 g_object_unref (dialog);
570 }
571
572 static void
573 opened_directory (GObject *object,
574 GAsyncResult *result,
575 gpointer data)
576 {
577 XdpPortal *portal = XDP_PORTAL (object);
578 g_autoptr(GError) error = NULL;
579 gboolean res;
580
581 res = xdp_portal_open_directory_finish (portal, result, &error);
582
583 if (!res)
584 g_warning ("%s", error->message);
585 }
586
587 static void
588 open_directory (GtkWidget *button, PortalTestWin *win)
589 {
590 GtkFileChooser *file_chooser;
591 g_autofree gchar *uri = NULL;
592 XdpParent *parent;
593
594 file_chooser = GTK_FILE_CHOOSER (win->file_chooser_button);
595 uri = gtk_file_chooser_get_uri (file_chooser);
596 if (!uri)
597 return;
598
599 g_message ("Opening '%s'", uri);
600
601 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
602 xdp_portal_open_directory (win->portal,
603 parent,
604 uri,
605 XDP_OPEN_URI_FLAG_NONE,
606 NULL,
607 opened_directory,
608 win);
609 xdp_parent_free (parent);
610 }
611
612 static void
613 taken (GObject *source,
614 GAsyncResult *result,
615 gpointer data)
616 {
617 PortalTestWin *win = data;
618 g_autofree char *uri = NULL;
619 g_autoptr(GError) error = NULL;
620
621 uri = xdp_portal_take_screenshot_finish (win->portal, result, &error);
622 if (uri != NULL)
623 {
624 g_autofree char *path = NULL;
625 g_autoptr(GdkPixbuf) pixbuf = NULL;
626 g_autoptr(GError) error = NULL;
627
628 path = g_filename_from_uri (uri, NULL, NULL);
629 pixbuf = gdk_pixbuf_new_from_file_at_scale (path, 60, 40, TRUE, &error);
630 if (pixbuf == NULL)
631 g_warning ("Failed to load screenshot from %s: %s", uri, error->message);
632 else
633 gtk_image_set_from_pixbuf (GTK_IMAGE (win->image), pixbuf);
634 }
635 else
636 g_warning ("Failed to load screenshot: %s", error->message);
637 }
638
639 static void
640 take_screenshot (GtkButton *button,
641 PortalTestWin *win)
642 {
643 XdpParent *parent;
644 XdpScreenshotFlags flags = XDP_SCREENSHOT_FLAG_NONE;
645
646 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->interactive)))
647 flags = XDP_SCREENSHOT_FLAG_INTERACTIVE;
648
649 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
650 xdp_portal_take_screenshot (win->portal, parent, flags, NULL, taken, win);
651 xdp_parent_free (parent);
652 }
653
654 static void
655 on_selection_owner_changed (XdpSession *session,
656 GStrv mime_types,
657 gboolean session_is_owner,
658 PortalTestWin *win)
659 {
660 update_clipboard (win);
661 }
662
663 static void
664 on_selection_transfer (XdpSession *session,
665 const char *mime_type,
666 unsigned int serial,
667 PortalTestWin *win)
668 {
669 int fd;
670 g_autoptr(GOutputStream) output_stream = NULL;
671 g_autoptr(GError) error = NULL;
672 const char *text;
673
674 fd = xdp_session_selection_write (session, serial);
675 if (fd < 0)
676 return;
677
678 text = gtk_entry_get_text (GTK_ENTRY (win->clipboard_entry));
679
680 output_stream = g_unix_output_stream_new (g_steal_fd (&fd), TRUE);
681 if (!g_output_stream_write_all (output_stream,
682 text,
683 strlen (text),
684 NULL, NULL, &error))
685 g_warning ("Failed to write clipboard content: %s", error->message);
686
687 g_clear_object (&output_stream);
688 xdp_session_selection_write_done (session, serial, !error);
689 }
690
691 static void
692 update_clipboard (PortalTestWin *win)
693 {
694 if (win->session &&
695 xdp_session_is_clipboard_enabled (win->session))
696 {
697 const char **mime_types;
698
699 mime_types = xdp_session_get_selection_mime_types (win->session);
700 if (!xdp_session_is_selection_owned_by_session (win->session) &&
701 mime_types &&
702 g_strv_contains (mime_types, "text/plain"))
703 gtk_widget_set_sensitive (win->clipboard_paste, TRUE);
704 else
705 gtk_widget_set_sensitive (win->clipboard_paste, FALSE);
706
707 gtk_widget_set_sensitive (win->clipboard_copy, TRUE);
708
709 if (!win->selection_owner_changed)
710 {
711 win->selection_owner_changed =
712 g_signal_connect (win->session, "selection-owner-changed",
713 G_CALLBACK (on_selection_owner_changed), win);
714 win->selection_transfer_handler_id =
715 g_signal_connect (win->session, "selection-transfer",
716 G_CALLBACK (on_selection_transfer), win);
717 }
718 }
719 else
720 {
721 gtk_widget_set_sensitive (win->clipboard_copy, FALSE);
722 gtk_widget_set_sensitive (win->clipboard_paste, FALSE);
723 if (win->session)
724 {
725 g_clear_signal_handler (&win->selection_owner_changed,
726 win->session);
727 g_clear_signal_handler (&win->selection_transfer_handler_id,
728 win->session);
729 }
730 else
731 {
732 win->selection_transfer_handler_id = 0;
733 win->selection_owner_changed = 0;
734 }
735 }
736 }
737
738 static void
739 inputcapture_session_started (GObject *source,
740 GAsyncResult *result,
741 gpointer data)
742 {
743 XdpInputCaptureSession *ic = XDP_INPUT_CAPTURE_SESSION (source);
744 PortalTestWin *win = data;
745 g_autoptr(GError) error = NULL;
746 GList *zones;
747 g_autoptr (GString) s = NULL;
748
749 if (!xdp_input_capture_session_start_finish (ic, result, &error))
750 {
751 g_warning ("Failed to start inputcapture session: %s", error->message);
752 return;
753 }
754
755 zones = xdp_input_capture_session_get_zones (ic);
756 s = g_string_new ("");
757 for (GList *elem = g_list_first (zones); elem; elem = g_list_next (elem))
758 {
759 XdpInputCaptureZone *zone = elem->data;
760 guint w, h;
761 gint x, y;
762
763 g_object_get (zone,
764 "width", &w,
765 "height", &h,
766 "x", &x,
767 "y", &y,
768 NULL);
769
770 g_string_append_printf (s, "%ux%u@%d,%d ", w, h, x, y);
771 }
772 gtk_label_set_label (GTK_LABEL (win->inputcapture_label), s->str);
773
774 update_clipboard (win);
775 }
776
777 static void
778 inputcapture_session_created2 (GObject *source,
779 GAsyncResult *result,
780 gpointer data)
781 {
782 XdpPortal *portal = XDP_PORTAL (source);
783 PortalTestWin *win = data;
784 g_autoptr(GError) error = NULL;
785 g_autoptr (GString) s = NULL;
786 XdpInputCaptureSession *ic;
787
788 ic = xdp_portal_create_input_capture_session2_finish (portal, result, &error);
789 if (ic == NULL)
790 {
791 g_warning ("Failed to create inputcapture session: %s", error->message);
792 return;
793 }
794 win->session = xdp_input_capture_session_get_session (ic);
795 win->input_capture_session = ic;
796
797 xdp_session_request_clipboard (win->session);
798
799 xdp_input_capture_session_start (ic,
800 NULL,
801 XDP_INPUT_CAPABILITY_POINTER | XDP_INPUT_CAPABILITY_KEYBOARD,
802 NULL,
803 inputcapture_session_started,
804 win);
805 }
806
807 static void
808 inputcapture_session_created (GObject *source,
809 GAsyncResult *result,
810 gpointer data)
811 {
812 XdpPortal *portal = XDP_PORTAL (source);
813 PortalTestWin *win = data;
814 g_autoptr(GError) error = NULL;
815 GList *zones;
816 g_autoptr (GString) s = NULL;
817 XdpInputCaptureSession *ic;
818
819 ic = xdp_portal_create_input_capture_session_finish (portal, result, &error);
820 if (ic == NULL)
821 {
822 g_warning ("Failed to create inputcapture session: %s", error->message);
823 return;
824 }
825 win->session = xdp_input_capture_session_get_session (ic);
826 win->input_capture_session = ic;
827
828 zones = xdp_input_capture_session_get_zones (ic);
829 s = g_string_new ("");
830 for (GList *elem = g_list_first (zones); elem; elem = g_list_next (elem))
831 {
832 XdpInputCaptureZone *zone = elem->data;
833 guint w, h;
834 gint x, y;
835
836 g_object_get (zone,
837 "width", &w,
838 "height", &h,
839 "x", &x,
840 "y", &y,
841 NULL);
842
843 g_string_append_printf (s, "%ux%u@%d,%d ", w, h, x, y);
844 }
845 gtk_label_set_label (GTK_LABEL (win->inputcapture_label), s->str);
846 }
847
848 static void
849 start_input_capture (PortalTestWin *win)
850 {
851 g_clear_object (&win->session);
852
853 update_clipboard (win);
854
855 if (xdp_portal_get_input_capture_version_sync (win->portal, NULL, NULL) > 1)
856 {
857 xdp_portal_create_input_capture_session2 (win->portal,
858 NULL,
859 inputcapture_session_created2,
860 win);
861 }
862 else
863 {
864 xdp_portal_create_input_capture_session (win->portal,
865 NULL,
866 XDP_INPUT_CAPABILITY_POINTER | XDP_INPUT_CAPABILITY_KEYBOARD,
867 NULL,
868 inputcapture_session_created,
869 win);
870 }
871 }
872
873 static void
874 stop_input_capture (PortalTestWin *win)
875 {
876 if (win->session != NULL)
877 {
878 xdp_session_close (win->session);
879
880 if (xdp_session_get_session_type (win->session) == XDP_SESSION_INPUT_CAPTURE)
881 {
882 g_clear_object (&win->input_capture_session);
883 win->session = NULL;
884 }
885 else
886 {
887 g_clear_object (&win->session);
888 }
889
890 update_clipboard (win);
891
892 gtk_label_set_label (GTK_LABEL (win->inputcapture_label), "");
893 }
894 }
895
896 static void
897 capture_input (GtkButton *button,
898 PortalTestWin *win)
899 {
900 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
901 start_input_capture (win);
902 else
903 stop_input_capture (win);
904 }
905
906 static void
907 capture_input_release (GtkButton *button,
908 PortalTestWin *win)
909 {
910 /* FIXME */
911 }
912
913 static void
914 capture_input_toggle_enable (GtkButton *button,
915 PortalTestWin *win)
916 {
917 if (!win->input_capture_session)
918 return;
919
920 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
921 xdp_input_capture_session_enable (win->input_capture_session);
922 else
923 xdp_input_capture_session_disable (win->input_capture_session);
924 }
925
926 static void
927 session_started (GObject *source,
928 GAsyncResult *result,
929 gpointer data)
930 {
931 XdpSession *session = (XdpSession*) source;
932 PortalTestWin *win = data;
933 g_autoptr(GError) error = NULL;
934 guint id;
935 g_autoptr(GVariantIter) iter = NULL;
936 GVariant *props;
937 g_autoptr(GStrvBuilder) builder = g_strv_builder_new ();
938 g_auto(GStrv) strv = NULL;
939 g_autofree char *s = NULL;
940 XdpDeviceType device_types;
941
942 if (!xdp_session_start_finish (session, result, &error))
943 {
944 g_warning ("Failed to start session: %s", error->message);
945 g_clear_object (&win->session);
946
947 update_clipboard (win);
948
949 if (xdp_session_get_session_type (session) == XDP_SESSION_SCREENCAST)
950 {
951 gtk_label_set_label (GTK_LABEL (win->screencast_label), "");
952 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (win->screencast_toggle), FALSE);
953 }
954 else if (xdp_session_get_session_type (session) == XDP_SESSION_REMOTE_DESKTOP)
955 {
956 gtk_label_set_label (GTK_LABEL (win->remotedesktop_label), "");
957 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (win->remotedesktop_toggle), FALSE);
958 }
959 return;
960 }
961
962
963 update_clipboard (win);
964
965 iter = g_variant_iter_new (xdp_session_get_streams (session));
966 while (g_variant_iter_next (iter, "(u@a{sv})", &id, &props))
967 {
968 int x, y, w, h;
969 g_variant_lookup (props, "position", "(ii)", &x, &y);
970 g_variant_lookup (props, "size", "(ii)", &w, &h);
971 g_strv_builder_take (builder, g_strdup_printf ("Stream %d: %dx%d @ %d,%d", id, w, h, x, y));
972 g_variant_unref (props);
973 }
974
975 device_types = xdp_session_get_devices (session);
976 if (device_types)
977 {
978 g_autoptr(GStrvBuilder) b = g_strv_builder_new ();
979 g_auto(GStrv) devices = NULL;
980 g_autofree char* devices_s = NULL;
981
982 if (device_types & XDP_DEVICE_KEYBOARD)
983 g_strv_builder_add (b, "keyboard");
984 if (device_types & XDP_DEVICE_POINTER)
985 g_strv_builder_add (b, "pointer");
986 if (device_types & XDP_DEVICE_TOUCHSCREEN)
987 g_strv_builder_add (b, "touchscreen");
988
989 devices = g_strv_builder_end (b);
990 devices_s = g_strjoinv (", ", devices);
991
992 g_strv_builder_take (builder, g_strdup_printf("Devices: %s", devices_s));
993 }
994
995 g_strv_builder_take (builder,
996 g_strdup_printf ("Clipboard: %s",
997 xdp_session_is_clipboard_enabled (session) ? "yes" : "no"));
998 strv = g_strv_builder_end (builder);
999 s = g_strjoinv (", ", strv);
1000
1001 if (xdp_session_get_session_type (session) == XDP_SESSION_SCREENCAST)
1002 gtk_label_set_label (GTK_LABEL (win->screencast_label), s);
1003 else if (xdp_session_get_session_type (session) == XDP_SESSION_REMOTE_DESKTOP)
1004 gtk_label_set_label (GTK_LABEL (win->remotedesktop_label), s);
1005 }
1006
1007 static void
1008 screencast_session_created (GObject *source,
1009 GAsyncResult *result,
1010 gpointer data)
1011 {
1012 XdpPortal *portal = XDP_PORTAL (source);
1013 PortalTestWin *win = data;
1014 g_autoptr(GError) error = NULL;
1015 XdpParent *parent = NULL;
1016
1017 win->session = xdp_portal_create_screencast_session_finish (portal, result, &error);
1018 if (win->session == NULL)
1019 {
1020 g_warning ("Failed to create screencast session: %s", error->message);
1021 return;
1022 }
1023
1024 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1025 xdp_session_start (win->session, parent, NULL, session_started, win);
1026 xdp_parent_free (parent);
1027 }
1028
1029 static void
1030 start_screencast (PortalTestWin *win)
1031 {
1032 g_clear_object (&win->session);
1033
1034 update_clipboard (win);
1035
1036 xdp_portal_create_screencast_session (win->portal,
1037 XDP_OUTPUT_MONITOR | XDP_OUTPUT_WINDOW,
1038 XDP_SCREENCAST_FLAG_NONE,
1039 XDP_CURSOR_MODE_HIDDEN,
1040 XDP_PERSIST_MODE_NONE,
1041 NULL,
1042 NULL,
1043 screencast_session_created,
1044 win);
1045 }
1046
1047 static void
1048 stop_screencast (PortalTestWin *win)
1049 {
1050 if (win->session != NULL)
1051 {
1052 xdp_session_close (win->session);
1053 g_clear_object (&win->session);
1054 update_clipboard (win);
1055 gtk_label_set_label (GTK_LABEL (win->screencast_label), "");
1056 }
1057 }
1058
1059 static void
1060 screencast_toggled (GtkToggleButton *button,
1061 PortalTestWin *win)
1062 {
1063 if (gtk_toggle_button_get_active (button))
1064 start_screencast (win);
1065 else
1066 stop_screencast (win);
1067 }
1068
1069 static void
1070 remotedesktop_session_created (GObject *source,
1071 GAsyncResult *result,
1072 gpointer data)
1073 {
1074 XdpPortal *portal = XDP_PORTAL (source);
1075 PortalTestWin *win = data;
1076 g_autoptr(GError) error = NULL;
1077 XdpParent *parent = NULL;
1078
1079 win->session = xdp_portal_create_remote_desktop_session_finish (portal, result, &error);
1080 if (win->session == NULL)
1081 {
1082 g_warning ("Failed to create remotedesktop session: %s", error->message);
1083 return;
1084 }
1085
1086 xdp_session_request_clipboard (win->session);
1087
1088 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1089 xdp_session_start (win->session, parent, NULL, session_started, win);
1090 xdp_parent_free (parent);
1091 }
1092
1093 static void
1094 start_remotedesktop (PortalTestWin *win)
1095 {
1096 g_clear_object (&win->session);
1097
1098 update_clipboard (win);
1099
1100 xdp_portal_create_remote_desktop_session (win->portal,
1101 (XDP_DEVICE_KEYBOARD |
1102 XDP_DEVICE_POINTER |
1103 XDP_DEVICE_TOUCHSCREEN),
1104 XDP_OUTPUT_MONITOR | XDP_OUTPUT_WINDOW,
1105 XDP_REMOTE_DESKTOP_FLAG_NONE,
1106 XDP_CURSOR_MODE_HIDDEN,
1107 NULL,
1108 remotedesktop_session_created,
1109 win);
1110 }
1111
1112 static void
1113 stop_remotedesktop (PortalTestWin *win)
1114 {
1115 if (win->session != NULL)
1116 {
1117 xdp_session_close (win->session);
1118 g_clear_object (&win->session);
1119 update_clipboard (win);
1120 gtk_label_set_label (GTK_LABEL (win->remotedesktop_label), "");
1121 }
1122 }
1123
1124 static void
1125 remotedesktop_toggled (GtkToggleButton *button,
1126 PortalTestWin *win)
1127 {
1128 if (gtk_toggle_button_get_active (button))
1129 start_remotedesktop (win);
1130 else
1131 stop_remotedesktop (win);
1132 }
1133
1134 static void
1135 paste_clicked (GtkButton *button,
1136 PortalTestWin *win)
1137 {
1138 int fd;
1139 g_autoptr(GInputStream) input_stream = NULL;
1140 g_autoptr(GError) error = NULL;
1141 char clipboard_content[1024] = {};
1142
1143 fd = xdp_session_selection_read (win->session, "text/plain");
1144 if (fd < 0)
1145 return;
1146
1147 input_stream = g_unix_input_stream_new (g_steal_fd (&fd), TRUE);
1148 if (!g_input_stream_read_all (input_stream,
1149 clipboard_content,
1150 sizeof (clipboard_content) - 1,
1151 NULL, NULL, &error))
1152 {
1153 g_warning ("Failed to read clipboard content: %s", error->message);
1154 return;
1155 }
1156
1157 gtk_label_set_label (GTK_LABEL (win->clipboard_label), clipboard_content);
1158 }
1159
1160 static void
1161 copy_clicked (GtkButton *button,
1162 PortalTestWin *win)
1163 {
1164 const char *mime_types[] = {
1165 "text/plain",
1166 NULL,
1167 };
1168
1169 xdp_session_set_selection (win->session, mime_types);
1170 }
1171
1172 static void
1173 account_response (GObject *source,
1174 GAsyncResult *result,
1175 gpointer data)
1176 {
1177 PortalTestWin *win = data;
1178 g_autoptr(GVariant) ret = NULL;
1179 g_autoptr(GError) error = NULL;
1180
1181 ret = xdp_portal_get_user_information_finish (win->portal, result, &error);
1182
1183 if (ret)
1184 {
1185 const char *id;
1186 const char *name;
1187 const char *uri;
1188 g_autofree char *path = NULL;
1189
1190 g_variant_lookup (ret, "id", "&s", &id);
1191 g_variant_lookup (ret, "name", "&s", &name);
1192 g_variant_lookup (ret, "image", "&s", &uri);
1193
1194 gtk_label_set_label (GTK_LABEL (win->username), id);
1195 gtk_label_set_label (GTK_LABEL (win->realname), name);
1196
1197 if (uri && uri[0])
1198 {
1199 g_autoptr(GdkPixbuf) pixbuf = NULL;
1200
1201 path = g_filename_from_uri (uri, NULL, NULL);
1202 pixbuf = gdk_pixbuf_new_from_file_at_scale (path, 60, 40, TRUE, &error);
1203 if (error)
1204 g_warning ("Failed to load photo %s: %s", path, error->message);
1205 else
1206 gtk_image_set_from_pixbuf (GTK_IMAGE (win->avatar), pixbuf);
1207 }
1208 }
1209 else if (error)
1210 g_message ("Account error: %s", error->message);
1211 else
1212 g_message ("Account canceled");
1213 }
1214
1215 static void
1216 get_user_information (PortalTestWin *win)
1217 {
1218 XdpParent *parent;
1219
1220 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1221 xdp_portal_get_user_information (win->portal,
1222 parent,
1223 "Allows portal-test to test the Account portal.",
1224 XDP_USER_INFORMATION_FLAG_NONE,
1225 NULL,
1226 account_response,
1227 win);
1228 xdp_parent_free (parent);
1229 }
1230
1231 static void
1232 compose_email_called (GObject *source,
1233 GAsyncResult *result,
1234 gpointer data)
1235 {
1236 PortalTestWin *win = data;
1237 g_autoptr(GError) error = NULL;
1238
1239 if (!xdp_portal_compose_email_finish (win->portal, result, &error))
1240 {
1241 g_warning ("Email error: %s", error->message);
1242 return;
1243 }
1244
1245 g_print ("Email sent\n");
1246 }
1247
1248 static void
1249 compose_email (PortalTestWin *win)
1250 {
1251 XdpParent *parent;
1252 g_autofree char *filename = NULL;
1253 const char *addresses[2];
1254 const char *cc[3];
1255 const char *attachments[2];
1256
1257 filename = g_build_filename (g_get_user_data_dir (), "test.txt", NULL);
1258
1259 attachments[0] = filename;
1260 attachments[1] = NULL;
1261
1262 addresses[0] = "[email protected]";
1263 addresses[1] = NULL;
1264
1265 cc[0] = "[email protected]";
1266 cc[1] = "[email protected]";
1267 cc[2] = NULL;
1268
1269 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1270 xdp_portal_compose_email (win->portal,
1271 parent,
1272 addresses, cc, NULL,
1273 "Test subject",
1274 "Test body",
1275 attachments,
1276 XDP_EMAIL_FLAG_NONE,
1277 NULL,
1278 compose_email_called,
1279 win);
1280 xdp_parent_free (parent);
1281 }
1282
1283 static void
1284 request_background_called (GObject *source,
1285 GAsyncResult *result,
1286 gpointer data)
1287 {
1288 PortalTestWin *win = data;
1289 g_autoptr(GError) error = NULL;
1290 gboolean ret = xdp_portal_request_background_finish (win->portal, result, &error);
1291 if (!ret)
1292 {
1293 g_warning ("Background request failed: %s", error ? error->message : "Unknown error");
1294 return;
1295 }
1296
1297 g_message ("Background request successful!");
1298 }
1299
1300 static void
1301 request_background (PortalTestWin *win)
1302 {
1303 XdpParent *parent;
1304 g_autoptr(GPtrArray) commandline = g_ptr_array_new_with_free_func (g_free);
1305
1306 g_ptr_array_add (commandline, g_strdup ("/bin/true"));
1307
1308 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1309 xdp_portal_request_background (win->portal,
1310 parent,
1311 "Test reason",
1312 commandline,
1313 XDP_BACKGROUND_FLAG_AUTOSTART,
1314 NULL,
1315 request_background_called,
1316 win);
1317 xdp_parent_free (parent);
1318 }
1319
1320 static void
1321 notify_me (PortalTestWin *win)
1322 {
1323 GVariantBuilder button;
1324 GVariantBuilder buttons;
1325 GVariantBuilder notification;
1326
1327 gtk_widget_hide (win->ack_image);
1328
1329 g_variant_builder_init (&button, G_VARIANT_TYPE_VARDICT);
1330 g_variant_builder_add (&button, "{sv}", "label", g_variant_new_string ("Yup"));
1331 g_variant_builder_add (&button, "{sv}", "action", g_variant_new_string ("app.ack"));
1332
1333 g_variant_builder_init (&buttons, G_VARIANT_TYPE ("aa{sv}"));
1334 g_variant_builder_add (&buttons, "a{sv}", &button);
1335
1336 g_variant_builder_init (&notification, G_VARIANT_TYPE_VARDICT);
1337 g_variant_builder_add (&notification, "{sv}", "title", g_variant_new_string ("Notify me"));
1338 g_variant_builder_add (&notification, "{sv}", "body", g_variant_new_string ("Really important information would ordinarily appear here"));
1339 g_variant_builder_add (&notification, "{sv}", "buttons", g_variant_builder_end (&buttons));
1340
1341 xdp_portal_add_notification (win->portal, "notification", g_variant_builder_end (&notification), XDP_NOTIFICATION_FLAG_NONE, NULL, NULL, NULL);
1342 }
1343
1344 void
1345 portal_test_win_ack (PortalTestWin *win)
1346 {
1347 gtk_widget_show (win->ack_image);
1348 }
1349
1350 static GList *active_prints = NULL;
1351
1352 typedef struct {
1353 char *text;
1354 PangoLayout *layout;
1355 GList *page_breaks;
1356 char *font;
1357 } PrintData;
1358
1359 static void
1360 status_changed_cb (GtkPrintOperation *op,
1361 gpointer user_data)
1362 {
1363 if (gtk_print_operation_is_finished (op))
1364 {
1365 active_prints = g_list_remove (active_prints, op);
1366 g_object_unref (op);
1367 }
1368 }
1369
1370 static void
1371 begin_print (GtkPrintOperation *operation,
1372 GtkPrintContext *context,
1373 PrintData *print_data)
1374 {
1375 PangoFontDescription *desc;
1376 PangoLayoutLine *layout_line;
1377 double width, height;
1378 double page_height;
1379 GList *page_breaks;
1380 int num_lines;
1381 int line;
1382
1383 width = gtk_print_context_get_width (context);
1384 height = gtk_print_context_get_height (context);
1385
1386 print_data->layout = gtk_print_context_create_pango_layout (context);
1387
1388 desc = pango_font_description_from_string (print_data->font);
1389 pango_layout_set_font_description (print_data->layout, desc);
1390 pango_font_description_free (desc);
1391
1392 pango_layout_set_width (print_data->layout, width * PANGO_SCALE);
1393 pango_layout_set_text (print_data->layout, print_data->text, -1);
1394
1395 num_lines = pango_layout_get_line_count (print_data->layout);
1396
1397 page_breaks = NULL;
1398 page_height = 0;
1399
1400 for (line = 0; line < num_lines; line++)
1401 {
1402 PangoRectangle ink_rect, logical_rect;
1403 double line_height;
1404
1405 layout_line = pango_layout_get_line (print_data->layout, line);
1406 pango_layout_line_get_extents (layout_line, &ink_rect, &logical_rect);
1407
1408 line_height = logical_rect.height / 1024.0;
1409
1410 if (page_height + line_height > height)
1411 {
1412 page_breaks = g_list_prepend (page_breaks, GINT_TO_POINTER (line));
1413 page_height = 0;
1414 }
1415
1416 page_height += line_height;
1417 }
1418
1419 page_breaks = g_list_reverse (page_breaks);
1420 gtk_print_operation_set_n_pages (operation, g_list_length (page_breaks) + 1);
1421
1422 print_data->page_breaks = page_breaks;
1423 }
1424
1425 static void
1426 draw_page (GtkPrintOperation *operation,
1427 GtkPrintContext *context,
1428 int page_nr,
1429 PrintData *print_data)
1430 {
1431 cairo_t *cr;
1432 GList *pagebreak;
1433 int start, end, i;
1434 PangoLayoutIter *iter;
1435 double start_pos;
1436
1437 if (page_nr == 0)
1438 start = 0;
1439 else
1440 {
1441 pagebreak = g_list_nth (print_data->page_breaks, page_nr - 1);
1442 start = GPOINTER_TO_INT (pagebreak->data);
1443 }
1444
1445 pagebreak = g_list_nth (print_data->page_breaks, page_nr);
1446 if (pagebreak == NULL)
1447 end = pango_layout_get_line_count (print_data->layout);
1448 else
1449 end = GPOINTER_TO_INT (pagebreak->data);
1450
1451 cr = gtk_print_context_get_cairo_context (context);
1452
1453 cairo_set_source_rgb (cr, 0, 0, 0);
1454
1455 i = 0;
1456 start_pos = 0;
1457 iter = pango_layout_get_iter (print_data->layout);
1458 do
1459 {
1460 PangoRectangle logical_rect;
1461 PangoLayoutLine *line;
1462 int baseline;
1463
1464 if (i >= start)
1465 {
1466 line = pango_layout_iter_get_line (iter);
1467
1468 pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
1469 baseline = pango_layout_iter_get_baseline (iter);
1470
1471 if (i == start)
1472 start_pos = logical_rect.y / 1024.0;
1473
1474 cairo_move_to (cr, logical_rect.x / 1024.0, baseline / 1024.0 - start_pos);
1475 pango_cairo_show_layout_line (cr, line);
1476 }
1477 i++;
1478 }
1479 while (i < end &&
1480 pango_layout_iter_next_line (iter));
1481
1482 pango_layout_iter_free (iter);
1483 }
1484
1485 static void
1486 end_print (GtkPrintOperation *op,
1487 GtkPrintContext *context,
1488 PrintData *print_data)
1489 {
1490 g_list_free (print_data->page_breaks);
1491 print_data->page_breaks = NULL;
1492 g_object_unref (print_data->layout);
1493 print_data->layout = NULL;
1494 }
1495
1496 static void
1497 print_done (GtkPrintOperation *op,
1498 GtkPrintOperationResult res,
1499 PrintData *print_data)
1500 {
1501 GError *error = NULL;
1502
1503 if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
1504 {
1505
1506 GtkWidget *error_dialog;
1507
1508 gtk_print_operation_get_error (op, &error);
1509
1510 error_dialog = gtk_message_dialog_new (NULL,
1511 GTK_DIALOG_DESTROY_WITH_PARENT,
1512 GTK_MESSAGE_ERROR,
1513 GTK_BUTTONS_CLOSE,
1514 "Error printing file:\n%s",
1515 error ? error->message : "no details");
1516 g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
1517 gtk_widget_show (error_dialog);
1518 }
1519
1520 g_free (print_data->text);
1521 g_free (print_data->font);
1522 g_free (print_data);
1523 if (!gtk_print_operation_is_finished (op))
1524 {
1525 g_object_ref (op);
1526 active_prints = g_list_append (active_prints, op);
1527
1528 /* This ref is unref:ed when we get the final state change */
1529 g_signal_connect (op, "status-changed",
1530 G_CALLBACK (status_changed_cb), NULL);
1531 }
1532 }
1533
1534 static char *
1535 get_text (void)
1536 {
1537 char *text;
1538 g_autofree char* test_file_path = file_path_in_source_dir ("portal-test-win.c");
1539 g_autoptr(GError) error = NULL;
1540
1541 if (!g_file_get_contents (test_file_path, &text, NULL, &error))
1542 {
1543 g_warning ("Failed to load print text: %s", error->message);
1544 text = g_strdup (error->message);
1545 }
1546
1547 return text;
1548 }
1549
1550 static void
1551 print_cb (GtkButton *button, PortalTestWin *win)
1552 {
1553 GtkPrintOperation *print;
1554 PrintData *print_data;
1555
1556 print_data = g_new0 (PrintData, 1);
1557
1558 print_data->text = get_text ();
1559 print_data->font = g_strdup ("Sans 12");
1560
1561 print = gtk_print_operation_new ();
1562
1563 g_signal_connect (print, "begin-print", G_CALLBACK (begin_print), print_data);
1564 g_signal_connect (print, "end-print", G_CALLBACK (end_print), print_data);
1565 g_signal_connect (print, "draw-page", G_CALLBACK (draw_page), print_data);
1566 g_signal_connect (print, "done", G_CALLBACK (print_done), print_data);
1567
1568 //gtk_print_operation_set_allow_async (print, TRUE);
1569 gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1570 GTK_WINDOW (win), NULL);
1571
1572 g_object_unref (print);
1573 }
1574
1575 static void
1576 inhibit_finished (GObject *source,
1577 GAsyncResult *result,
1578 gpointer data)
1579 {
1580 g_autoptr(GError) error = NULL;
1581 PortalTestWin *win = data;
1582
1583 win->inhibit_cookie = xdp_portal_session_inhibit_finish (XDP_PORTAL (source), result, &error);
1584 }
1585
1586 static void
1587 inhibit_changed (GtkToggleButton *button, PortalTestWin *win)
1588 {
1589 GtkApplicationInhibitFlags flags = 0;
1590
1591 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->inhibit_logout)))
1592 flags |= GTK_APPLICATION_INHIBIT_LOGOUT;
1593 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->inhibit_switch)))
1594 flags |= GTK_APPLICATION_INHIBIT_SWITCH;
1595 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->inhibit_suspend)))
1596 flags |= GTK_APPLICATION_INHIBIT_SUSPEND;
1597 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (win->inhibit_idle)))
1598 flags |= GTK_APPLICATION_INHIBIT_IDLE;
1599
1600 if (win->inhibit_flags == flags)
1601 return;
1602
1603 if (win->inhibit_cookie != 0)
1604 {
1605 xdp_portal_session_uninhibit (win->portal, win->inhibit_cookie);
1606 win->inhibit_cookie = 0;
1607 }
1608
1609 win->inhibit_flags = flags;
1610
1611 if (win->inhibit_flags != 0)
1612 {
1613 XdpParent *parent;
1614
1615 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1616 xdp_portal_session_inhibit (win->portal,
1617 parent,
1618 "Portal Testing",
1619 (XdpInhibitFlags)win->inhibit_flags,
1620 NULL,
1621 inhibit_finished,
1622 win);
1623 xdp_parent_free (parent);
1624 }
1625 }
1626
1627 static gboolean
1628 pipeline_stop (GstElement *pipeline)
1629 {
1630 gst_element_set_state (pipeline, GST_STATE_NULL);
1631 g_object_unref (pipeline);
1632
1633 return FALSE;
1634 }
1635
1636 static gboolean
1637 watch_func (GstBus *bus,
1638 GstMessage *message,
1639 gpointer data)
1640 {
1641 GstElement *pipeline = data;
1642
1643 g_print ("message %d\n", GST_MESSAGE_TYPE (message));
1644
1645 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR)
1646 {
1647 g_autoptr(GError) err = NULL;
1648
1649 gst_message_parse_error (message, &err, NULL);
1650 g_print ("ERROR: %s\n", err->message);
1651
1652 gst_element_set_state (pipeline, GST_STATE_NULL);
1653 g_object_unref (pipeline);
1654 g_object_unref (bus);
1655
1656 return FALSE;
1657 }
1658 else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED)
1659 {
1660 GstStateChangeReturn change;
1661 GstState state;
1662 GstState pending;
1663
1664 change = gst_element_get_state (pipeline, &state, &pending, GST_MSECOND);
1665 if (change == GST_STATE_CHANGE_SUCCESS)
1666 g_print ("Pipeline state now %d\n", state);
1667 else if (change == GST_STATE_CHANGE_ASYNC)
1668 g_print ("Pipeline state now %d, pending %d\n", state, pending);
1669 else
1670 g_print ("Pipeline state change failed, now %d\n", state);
1671
1672 if (state == GST_STATE_PLAYING)
1673 {
1674 g_timeout_add (500, (GSourceFunc) pipeline_stop, pipeline);
1675 g_object_unref (bus);
1676
1677 return FALSE;
1678 }
1679 }
1680
1681 return TRUE;
1682 }
1683
1684 static void
1685 play_sound (gdouble frequency)
1686 {
1687 GstElement *source, *sink;
1688 GstElement *pipeline;
1689 GstBus *bus;
1690
1691 pipeline = gst_pipeline_new ("note");
1692 source = gst_element_factory_make ("audiotestsrc", "source");
1693 sink = gst_element_factory_make ("autoaudiosink", "output");
1694
1695 g_object_set (source, "freq", frequency, NULL);
1696
1697 gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
1698 gst_element_link (source, sink);
1699
1700 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1701 gst_bus_add_watch (bus, watch_func, pipeline);
1702
1703 g_print ("Set pipeline state to playing\n");
1704 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1705 }
1706
1707 static void
1708 play_clicked (GtkButton *button, PortalTestWin *win)
1709 {
1710 play_sound (440.0);
1711 }
1712
1713 static void
1714 set_wallpaper_called (GObject *source,
1715 GAsyncResult *result,
1716 gpointer data)
1717 {
1718 PortalTestWin *win = data;
1719 g_autoptr(GError) error = NULL;
1720 gboolean ret = xdp_portal_set_wallpaper_finish (win->portal, result, &error);
1721 if (!ret)
1722 {
1723 g_warning ("Wallpaper request failed: %s", error ? error->message : "Unknown error");
1724 return;
1725 }
1726
1727 g_message ("Wallpaper request successful!");
1728 }
1729
1730 static void
1731 set_wallpaper (PortalTestWin *win)
1732 {
1733 XdpParent *parent;
1734 // const char *uri = "https://gitlab.gnome.org/GNOME/gtk/raw/master/demos/gtk-demo/portland-rose.jpg";
1735 const char *uri = "file:///usr/share/backgrounds/gnome/adwaita-morning.jpg";
1736
1737 parent = xdp_parent_new_gtk (GTK_WINDOW (win));
1738 xdp_portal_set_wallpaper (win->portal,
1739 parent,
1740 uri,
1741 XDP_WALLPAPER_FLAG_BACKGROUND | XDP_WALLPAPER_FLAG_PREVIEW,
1742 NULL,
1743 set_wallpaper_called,
1744 win);
1745 xdp_parent_free (parent);
1746 }
1747
1748 static void
1749 portal_test_win_class_init (PortalTestWinClass *class)
1750 {
1751 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1752
1753 gtk_widget_class_set_template_from_resource (widget_class,
1754 "/org/gtk/portal-test/portal-test-win.ui");
1755
1756 gtk_widget_class_bind_template_callback (widget_class, save_dialog);
1757 gtk_widget_class_bind_template_callback (widget_class, open_directory);
1758 gtk_widget_class_bind_template_callback (widget_class, open_local);
1759 gtk_widget_class_bind_template_callback (widget_class, take_screenshot);
1760 gtk_widget_class_bind_template_callback (widget_class, capture_input);
1761 gtk_widget_class_bind_template_callback (widget_class, capture_input_release);
1762 gtk_widget_class_bind_template_callback (widget_class, capture_input_toggle_enable);
1763 gtk_widget_class_bind_template_callback (widget_class, screencast_toggled);
1764 gtk_widget_class_bind_template_callback (widget_class, remotedesktop_toggled);
1765 gtk_widget_class_bind_template_callback (widget_class, paste_clicked);
1766 gtk_widget_class_bind_template_callback (widget_class, copy_clicked);
1767 gtk_widget_class_bind_template_callback (widget_class, notify_me);
1768 gtk_widget_class_bind_template_callback (widget_class, print_cb);
1769 gtk_widget_class_bind_template_callback (widget_class, inhibit_changed);
1770 gtk_widget_class_bind_template_callback (widget_class, play_clicked);
1771 gtk_widget_class_bind_template_callback (widget_class, get_user_information);
1772 gtk_widget_class_bind_template_callback (widget_class, compose_email);
1773 gtk_widget_class_bind_template_callback (widget_class, reload_settings);
1774 gtk_widget_class_bind_template_callback (widget_class, request_background);
1775 gtk_widget_class_bind_template_callback (widget_class, set_wallpaper);
1776 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, sandbox_status);
1777 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, network_status);
1778 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, monitor_name);
1779 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, proxies);
1780 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, resolver_name);
1781 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, image);
1782 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, interactive);
1783 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, encoding);
1784 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, ack_image);
1785 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inhibit_idle);
1786 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inhibit_logout);
1787 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inhibit_suspend);
1788 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inhibit_switch);
1789 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inputcapture_label);
1790 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, inputcapture_toggle);
1791 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, username);
1792 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, realname);
1793 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, avatar);
1794 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, save_how);
1795 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, settings_value);
1796 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, settings_count);
1797 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, screencast_label);
1798 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, screencast_toggle);
1799 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, remotedesktop_label);
1800 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, remotedesktop_toggle);
1801 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, clipboard_label);
1802 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, clipboard_copy);
1803 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, clipboard_paste);
1804 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, clipboard_entry);
1805 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, update_dialog);
1806 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, update_dialog2);
1807 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, update_progressbar);
1808 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, update_label);
1809 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, ok2);
1810 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, open_local_dir);
1811 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, open_local_ask);
1812 gtk_widget_class_bind_template_child (widget_class, PortalTestWin, file_chooser_button);
1813 }
1814
1815 GtkApplicationWindow *
1816 portal_test_win_new (PortalTestApp *app)
1817 {
1818 return g_object_new (portal_test_win_get_type (), "application", app, NULL);
1819 }