Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sandbox.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
8#include "sandbox.h"
9
10#include <giomm/file.h>
11#include <glibmm/convert.h>
12#include <glibmm/miscutils.h>
13#include <optional>
14
16
18{
19 // Simplification: Whenever xdg portal is active, we assume that the full filesystem is hidden behind the portal.
20 // In reality, it may be more complex, e.g., we could have access to the home directory but not to external media.
21
22 // Linux (flatpak/snap):
23 return !Glib::getenv("GTK_USE_PORTAL").empty();
24 // FUTURE: Add MacOS App Sandbox?
25}
26
27Glib::ustring filesystem_get_display_path(std::optional<Glib::RefPtr<Gio::File const>> path,
28 Glib::ustring placeholder_if_empty)
29{
30 // Note: If File points to a URL, get_path() may be empty even if get_parse_name() is not.
31 // Therefore, to test for emptiness we use get_parse_name().
32 if (!path.has_value() || path.value()->get_parse_name().empty()) {
33 return placeholder_if_empty;
34 }
35 auto path_native = path.value()->get_path();
37 /* FUTURE: Try to get the true path. For xdg-portal, see https://gitlab.gnome.org/GNOME/gtk/-/issues/7102 .
38 * Extra care is needed to avoid that IO operations freeze Inkscape if the path has become
39 * inaccessible. Otherwise we would cause bugs such as:
40 * https://gitlab.com/inkscape/inkscape/-/merge_requests/6294
41 *
42 * WORKAROUND: We just display the last part of the path, i.e.,
43 * the filename or the name of the lowest directory.
44 *
45 * FUTURE: This method should be moved upstream to Glib::filename_display_name or some property of Gio::File
46 */
47 return Glib::filename_display_basename(path_native);
48 }
49 // FUTURE: Improve display, e.g. "My Documents" instead of /home/user/Documents
50 return path.value()->get_parse_name();
51}
52
53} // namespace Inkscape::IO::Sandbox
bool filesystem_is_sandboxed()
Query if the filesystem is "sandboxed", e.g., by using xdg-portal in flatpak/snap.
Definition sandbox.cpp:17
Glib::ustring filesystem_get_display_path(std::optional< Glib::RefPtr< Gio::File const > > path, Glib::ustring placeholder_if_empty)
Translate raw filesystem path to a path suitable for display.
Definition sandbox.cpp:27
Inkscape::IO::Sandbox.