What the rigth way to cross-platform open file with an associated program

I am writing a file manager as a pet project and can’t find anything suitable in GLib. Now my solution is to Process.spawn_command_line_async("xdg-open ...");, but it seems very hacky to me , and I will have to do ifdef for windows.

The relevant API is in the GAppInfo class:

There’s much more for that, depending on what you want to achieve.

Additionally, if you’re writing a GUI application, you may want to check gtk_show_uri_on_window(), and the GdkAppLaunchContext.


Thanks, Gtk.show_uri_on_window works great.
By the way, I noticed that DateTime.to_unix() returns int64, and Gtk.show_uri_on_window accepts uint32. so I need to cast it.

void main(string[] args){
    File file = File.new_for_path("/home/gavr/...");
    Gtk.init(ref args);
    var window = new Gtk.Window();

    try {
            (uint32)new DateTime.now().to_unix()
    } catch (Error e) {error(e.message);}

    window.destroy.connect (Gtk.main_quit);

No, you don’t need to cast it, you must not use it at all.

Gtk.show_uri_on_window() does not document any specifics about the timestamp for very good reasons, it’s display-system specific and you shouldn’t make any assumptions about it. For example GNOME’s wayland session uses the system monotonic time in milliseconds (that is, not calendar time in seconds).

What you should pass instead is either the timestamp of a corresponding event, or Gdk.CURRENT_TIME.

1 Like

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