How can i limit GtkFileChooser to a specific file and directories?

Hello everyone
I’m new here. I just want to restrict gtk filechooser to show only path and directory that I want.
how can I achieve this?
I guess that I should change something in the source code and compile it again. but I don’t know how.

Any help and advice will be gratefully appreciated.
Thank you.

No, there is no way to do that from within the file chooser.

The recommended way to limit the file system visibility is to use a sandbox around your application; you should look at Flatpak.

thanks for your reply.
I’m running an application inside a docker container and I want to restrict files and directories from there.
so I cant use Flatpak.
I searched about this issue and if find your answer in stackoverflow.
is it still working?
thank you again.

Running a GTK app from within a Docker container is… unorthodox, to say the least. That’s why I recommended using Flatpak.

The places to modify are still those, for GTK3. What I wrote six years ago is still very much true:

Modifying the GtkFileChooserWidget is not for the faint of heart; you’ll have to change the way the widget determines the current directory, and navigates through the parent directories, both from a user interaction standpoint and from an API standpoint.

and:

This is probably on of the most complex sites inside the [GTK] code base

If you don’t know your way around GTK already it is very unlikely you’ll be able to implement this.

1 Like

Running a GTK app from within a Docker container is… unorthodox, to say the least. That’s why I recommended using Flatpak

yes, you are right.but to be more clear I have to run a browser( e.g: firefox, chrome) inside the docker container and I want to restrict GTK filechooser in them. so as you said modifying GTK source code is the only way.

If you don’t know your way around GTK already it is very unlikely you’ll be able to implement this.

unfortunately, I’m new here. but I have to do that.
could you please give me more specific clues to tackle this problem?

Thank you for taking the time.

This is going to get extra complicated, because the file chooser is an API, which means the application around it might decide to set a specific path; or it might not cope very well with the file chooser not returning a path if you try going outside of the bounds you set up. You’ll have to modify GTK and likely the application itself.

Not really, sorry. At the very basic, you’d have to modify gtkfilesystem.c and gtkfilesystemmodel.c to reject navigating into locations outside a specific “root”. What happens when the user or the application tries to do that is very complicated, and nobody has really attempted at doing that.

1 Like

really appreciate your help.
I hope I can handle that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.