0

I’m writing a command line program to get the Gtk3 icon file name associated with a mime type... the idea’s to be able to do this:

$ ./gtk-mimetype-icon text/html
Mime type: text/html
Icon file: /usr/share/icons/gnome/48x48/mimetypes/text-html.png

Unfortunately, this is happening instead:

$ ./gtk-mimetype-icon text/html

(process:30041): Gtk-CRITICAL **: gtk_icon_theme_get_for_screen: assertion `GDK_IS_SCREEN (screen)' failed

(process:30041): Gtk-CRITICAL **: gtk_icon_theme_lookup_by_gicon: assertion `GTK_IS_ICON_THEME (icon_theme)' failed    
Unable to load icon info, bailing!

After searching through the docs & googling for solutions, I’m bewildered by how to proceed. I am running this in an X terminal, with the DISPLAY properly exported:

$ env |grep DISPLAY
DISPLAY=:0.0

The program’s source is below; I’m building against glib2-2.30.3 and gtk3-3.2.4. Any insight’s appreciated!

#include <gio/gio.h>
#include <gtk/gtk.h>
#include <stdio.h>
// save as gtk-mimetype.c and build w/:
//   cc -o gtk-mimetype-icon `pkg-config --cflags --libs glib-2.0 gtk+-3.0` gtk-mimetype.c
// cf. http://unix.stackexchange.com/questions/11152/how-to-get-the-icon-for-a-mime-type
int
main (int argc, char **argv)
{
    g_type_init ();
    if (argc < 2) {
        fprintf (stderr, "Usage: %s <mimetype\n", argv[0]);
        return -1;
    }

    GIcon *icon = g_content_type_get_icon (argv[1]);

    GtkIconInfo *icon_info = gtk_icon_theme_lookup_by_gicon (
         gtk_icon_theme_get_default (),
         icon,
         48,
         GTK_ICON_LOOKUP_GENERIC_FALLBACK);

    const char *filename;
    if (icon_info != NULL)
        filename = gtk_icon_info_get_filename (icon_info);
    else {
        fprintf (stderr, "Unable to load icon info, bailing!\n");
        return -1;
    }

    printf ("Mime type: %s\nIcon file: %s\n",
            argv[1],
            filename);

    return 0;
}
Honore Doktorr
  • 1,585
  • 1
  • 13
  • 20

1 Answers1

2

you need to initialize the gtk framework, replace g_type_init (); with gtk_init(&argc, &argv);

Kwariz
  • 1,306
  • 8
  • 11