gtkgtk3vala

How do I use Gio library in Vala?


I'm building a Gtk application in Vala and would like to use the Gio library and more specifically the g_app_info_get_all() function. I am able to achieve this in C as follows:

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>

int main(int argc, char *argv[])
{
  ...

  GDesktopAppInfo *app_info;
  GList *app_list, *l;

  gtk_init(&argc, &argv);

  ...

  app_list = g_app_info_get_all();
  
  ...

}

How do I achieve the same in Vala?

ChatGPT suggested me to use Gio.AppInfo.get_all() and even though pkg-config --modversion gio-2.0 returns 2.74.3 but using Gio; throws an error saying the namespace 'Gio' could not be found even when I'm compiling with --pkg gio-2.0 flag.


Solution

  • The namespace is GLib, this is shown as {} Glib in Valadoc. The GLib namespace is used by default, so the following will work:

    void main () {
        var result = AppInfo.get_all ();
        foreach (var app in result) {
            print (@"$(app.get_display_name())\n");
        }
    }
    

    Compile with:

    valac example.vala --pkg gio-2.0