Re: How does one add an item to the “recently used” file list (from Python)?
- Date: Sun, 9 Oct 2016 00:33:07 +0200
- From: infirit <infirit@xxxxxxxxx>
- Subject: Re: How does one add an item to the “recently used” file list (from Python)?
Op 10/08/2016 om 11:52 PM schreef Laurence Gonsalves:
> On Fri, Oct 7, 2016 at 3:13 PM, infirit <infirit@xxxxxxxxx> wrote:
>> That is how you do it, however you _require_ a main-loop running for
>> this to work. Working example below.
>> from gi.repository import Gtk, GLib
>> def on_timeout(manager):
>> res = manager.add_item('file:///home/user/example.txt')
>> recent_manager = Gtk.RecentManager.get_default()
>> GLib.timeout_add(5000, on_timeout, recent_manager)
> Thanks! Is it necessary to use timeout_add? I noticed that I can call
> add_item before starting the main loop, and it seems to work once the
> main loop is started. I'm not sure if that's relying on undocumented
> behavior, however.
There is no undocumented behaviour, many parts of Gtk and GLib require a
mainloop to function properly, GtkRecentManager is one of them and
sending/receiving signals is another.
A timeout is necessary (or any other way to schedule something) as
calling Gtk.main is a blocking operation.
> I also want to quit the process right after adding the items. This
> seems to work:
> recent_mgr = Gtk.RecentManager.get_default()
> for file in args.files:
> uri = GLib.filename_to_uri(os.path.abspath(file))
You could do that but it relies, imo, on implicit behaviour that if no
higher tasks are running it should quit. My approach would be that you
explicitly call Gtk.main_quit when you are done processing the files.
Modifying my on_timeout function it would look something like below.
Notice also the usage of GFile 
def on_timeout(manager, files):
for path in files:
f = Gio.File.new_for_path(path)
exists = f.query_exists()
res = manager.add_item(f.get_uri())
My timeout was five seconds, but it could be one milliseconds so it has
no noticeable delay when you run it.
gtk-list mailing list