I have added a GtkFileChooserButton to my app via Glade. It is set to folder selection mode. When the widget is clicked it shows a dropdown list of Nautilus bookmarks with an 'other' option in the end. A new file chooser dialog is launched when 'other' is clicked. The button is not connected to any other custom file chooser dialog.
The connecting signal I am using is 'file-set' and I am retrieving the fullpath of user selected folder using Gtk.FileChooser.get_current_folder ().
This setup works fine if user selects a folder from 'other' option. But if a user selects a bookmark from dropdown list, no path is returned. How to solve this?
Is there a way to disable this dropdown list and directly go to the filechooser dialog? I want to use GtkFileChooserButton only so that the user can get a preview of his selected folder.
Because the file-set signal is only sent when the user changes the file, you will need to actually do the get_current_folder() call when you need to know the path, and not when the file-set signal is called. From the documentation, I see no way to get a notification when the user selects a path from the bookmarks, in folder selection mode.
As an alternative, though, you could use the FileChooserButton in normal mode, and simply use
get_current_folder
when thefile-set
signal is sent. This should give you the directory which the user is in, when a file is selected. Unfortunately, the UI will also look weird in this case, as the button will show the filename, while you're actually using the directory.Also, you should probably file a bug against upstream GTK+ about this use case and how it isn't usable. The file-set signal really should be deprecated and renamed to path-set, and used in both modes.
I used the
current_folder_changed
signal inherited fromGtkFileChooser
and it seems to work even with the bookmarks.GtkFileChooserButton in Folder Select mode can return the folder URI. Use
urlparse
module to convert URI to path. This works even with bookmarks. Hope this solves your issue.With GtkFileChooserButton, 'folder selection' mode. The following code is good enough:
We get directory path on every selection of the dropdown menu, even bookmarks.
Caution with w.get_current_folder(). It returns the LAST selected folder when user chose a folder from 'other' option, not the selected folder in the dropdown menu.