[gnome-boxes/housekeeping-autumn-2021: 6/8] app: Add runtime check for whether we are running in a Flatpak




commit 52a844301401e04249b3b83cc0ce8ddf7213f42a
Author: Felipe Borges <felipeborges gnome org>
Date:   Wed Oct 13 15:26:29 2021 +0200

    app: Add runtime check for whether we are running in a Flatpak
    
    Boxes has a build option used to exclude some code paths from
    Flatpak builds. This is useful for excluding code path such as
    the methods that use udev to obtain media information (not available
    in Flatpak). But we were abusing this build option for runtime-specific
    logic too.
    
    This adds an "App.is_running_in_flatpak ()" utility to the App
    singleton that allows for easily conditioning code based on
    whether we are in a Flatpak sandbox.

 src/app.vala                        | 17 +++++++++++++----
 src/libvirt-machine-properties.vala |  5 ++---
 src/vm-configurator.vala            |  6 +++---
 3 files changed, 18 insertions(+), 10 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index 75b88392..f562f30e 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -41,6 +41,8 @@ public virtual async void add_source (CollectionSource source) throws GLib.Error
         get; private set;
     }
 
+    private static bool? is_flatpak;
+
     // A callback to notify that deletion of machines was undone by user.
     public delegate void UndoNotifyCallback ();
 
@@ -548,13 +550,10 @@ private void keep_on_running_on_background () {
 
             var keep_vm_running = (machine.run_in_bg && machine.is_running);
             if (keep_vm_running) {
-#if FLATPAK
-                run_in_bg = true;
-#endif
+                run_in_bg = is_flatpak;
 
                 notify_vm_is_running_in_background (machine);
                 debug ("Keep running %s in the background", machine.name);
-
             }
         });
     }
@@ -714,4 +713,14 @@ public new void uninhibit () {
             base.uninhibit (inhibit_cookie);
         }
     }
+
+    public static bool is_running_in_flatpak () {
+        if (is_flatpak != null)
+            return is_flatpak;
+
+        var file = File.new_for_path ("/.flatpak-info");
+        is_flatpak = file.query_exists ();
+
+        return is_flatpak;
+    }
 }
diff --git a/src/libvirt-machine-properties.vala b/src/libvirt-machine-properties.vala
index 117bbf0f..44918ec5 100644
--- a/src/libvirt-machine-properties.vala
+++ b/src/libvirt-machine-properties.vala
@@ -748,10 +748,9 @@ private void on_vcpus_changed (Boxes.Property property, int vcpus) {
         toggle.notify["active"].connect ((tooltip) => {
             box.tooltip_text = toggle.active? _("ā€œ%sā€ will not be paused automatically.").printf (name) :
                                               _("ā€œ%sā€ will be paused automatically to save 
resources.").printf (name);
-#if FLATPAK
-            if (toggle.get_active ())
+
+            if (App.is_running_in_flatpak () && toggle.get_active ())
                 on_run_in_bg ();
-#endif
         });
 
         return box;
diff --git a/src/vm-configurator.vala b/src/vm-configurator.vala
index b9b515d1..7f5d0d37 100644
--- a/src/vm-configurator.vala
+++ b/src/vm-configurator.vala
@@ -95,9 +95,9 @@ public static Domain create_domain_config (InstallerMedia install_media, string
         install_media.setup_domain_config (domain);
 
         add_usb_support (domain, install_media);
-#if !FLATPAK
-        add_smartcard_support (domain);
-#endif
+
+        if (!App.is_running_in_flatpak ())
+            add_smartcard_support (domain);
 
         set_video_config (domain, install_media);
         set_sound_config (domain, install_media);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]