Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use File chooser portal #1887

Open
rmnvgr opened this issue May 21, 2021 · 3 comments · May be fixed by #1894
Open

Use File chooser portal #1887

rmnvgr opened this issue May 21, 2021 · 3 comments · May be fixed by #1894
Labels

Comments

@rmnvgr
Copy link
Contributor

rmnvgr commented May 21, 2021

What feature or enhancement would you like to add?

Use the File chooser portal to improve the privacy of the Flatpak.

The theory

As you know, CorsixTH is now distributed as a Flatpak. Flatpaks run in a sandboxed environment, and this creates an issue for programs that need to access the host filesystem.

To allow CorsixTH users to choose their game data nonetheless, the Flatpak requires read access to the whole home directory. This is the easiest solution because it doesn't require any change from in the program, but some users have privacy concerns over this.

It is possible, however, to plug this hole and use portals to get access to the host. The File chooser portal spawns a system file chooser dialog on the host, and when the user selects files, they are mounted within the sandbox so the program can access them.

In practice

The program needs to make DBus calls to use the portals. However, as this is not the most practical, GUI toolkits have widgets that use these portals transparently.

Because CorsixTH doesn't use a GUI toolkit, there is another solution: spawn another program that will in turn call the portal and give back the result to CorsixTH. Such a program would be zenity, and this is what OpenRCT2 uses for its file dialogs.

To not make zenity a dependency of CorsixTH, it is possible to only use it if it is already installed and/or the game detects it is in a Flatpak, and fall back to the current in-game file chooser.

There are two downsides:

  • Because the dialog is part of the OS, it is not integrated into the game
  • File validation can only happen after the user chooses them, because the program can't see them beforehand

Conclusion

Although not mandatory for the Flatpak, using the File chooser portal would be a nice convenience and comfort users about the privacy of their data. If that is something you are interested in seeing in CorsixTH, I can look into working on it, even if I'm not familiar with the code base and haven't written Lua in a while!

@rmnvgr rmnvgr added the enhancement issue type label May 21, 2021
@lewri lewri added the OS:Linux OS label May 21, 2021
@TheCycoONE
Copy link
Member

TheCycoONE commented May 22, 2021

As a quick and dirty proof of concept:

diff --git a/CorsixTH/Lua/app.lua b/CorsixTH/Lua/app.lua
index 301c731a..1e484990 100644
--- a/CorsixTH/Lua/app.lua
+++ b/CorsixTH/Lua/app.lua
@@ -1283,6 +1283,16 @@ function App:checkInstallFolder()
         end
       end
     end
+
+    if not status then
+      -- unfortunately here we don't have translations yet.
+      local f = io.popen('zenity --file-selection --directory --title="Select Theme Hospital game directory"')
+      local dir = f:read('*l')
+      if dir ~= nil then
+        status, _ = self.fs:setRoot(dir)
+      end
+    end
+
     if not status then
       -- If the given directory didn't exist, then likely the config file hasn't
       -- been changed at all from the default, and we looked unsuccessfully in

Doing it for real one would want to know when to attempt to call zenity - I don't know what exists for flatpak detection but I wouldn't do it just because zenity was available. Maybe as a cmake argument if there's no good way to detect at runtime.
They might also want to handle the non-happy paths better than this proof of concept; wrong directory just falls through to regular selection with this, and I didn't test to see what cancel would do.

Anyway, I'm open to it.

Edit: changed diff to not explode on 'Cancel'

@rmnvgr
Copy link
Contributor Author

rmnvgr commented May 22, 2021

Nice!

To detect if the program is running in a Flatpak, it is possible to check if the file .flatpak-info exists at the root of the file system.

Since the file chooser is called in different places (first startup, options, folders options), I'd maybe look into adding a showFileChooser method to the app class that will either spawn zenity or add the UIDirectoryBrowser window.

@kparal
Copy link

kparal commented May 25, 2021

To detect if the program is running in a Flatpak, it is possible to check if the file .flatpak-info exists at the root of the file system.

There's also the FLATPAK_ID environment variable present, which can serve for detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants