Web lists-archives.com

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')
>>     print(res)
>>
>> recent_manager = Gtk.RecentManager.get_default()
>>
>> GLib.timeout_add(5000, on_timeout, recent_manager)
>>
>> Gtk.main()
> 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))
>         recent_mgr.add_item(uri)
>
>     GLib.idle_add(Gtk.main_quit)
>     Gtk.main()

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 [1]

def on_timeout(manager, files):
    for path in files:
        f = Gio.File.new_for_path(path)
        exists = f.query_exists()
        if exists:
            res = manager.add_item(f.get_uri())
            print(res)
    Gtk.main_quit()


My timeout was five seconds, but it could be one milliseconds so it has
no noticeable delay when you run it.

~infirit

[1] https://lazka.github.io/pgi-docs/#Gio-2.0/interfaces/File.html

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list