Gtk3:获取mime类型的图标文件名

时间:2012-09-20 23:38:29

标签: mime-types gtk3 gio command-line-tool

我正在编写一个命令行程序来获取与mime类型相关联的Gtk3图标文件名...这个想法能够做到这一点:

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

不幸的是,发生了这种情况:

$ ./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!

搜索文档&谷歌搜索解决方案,我对如何继续感到困惑。我在X终端中运行它,并正确导出DISPLAY:

$ env |grep DISPLAY
DISPLAY=:0.0

该计划的来源如下;我正在建立对抗glib2-2.30.3和gtk3-3.2.4。任何见解都值得赞赏!

#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;
}

1 个答案:

答案 0 :(得分:2)

您需要初始化gtk框架,将g_type_init ();替换为gtk_init(&argc, &argv);