如何在Linux桌面中获取与文件相关联的图标,MIME类型和应用程序? [英] How do you get the icon, MIME type, and application associated with a file in the Linux Desktop?

查看:388
本文介绍了如何在Linux桌面中获取与文件相关联的图标,MIME类型和应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux桌面上使用C ++,获取图标,文档描述和应用程序与任意文件/文件路径关联的最佳方式是什么?

Using C++ on the Linux desktop, what is the best way to get the icon, the document description and the application "associated" with an arbitrary file/file path?

我想使用最规范的方式在KDE和gnome上查找图标,mime类型/文件类型描述和相关应用程序,喜欢避免任何shelling out到命令行和低级例程,以及避免自己重新创建wheel(无需解析mime类型文件等)。

I'd like to use the most "canonical" way to find icons, mime-type/file type descriptions and associated applications on both KDE and gnome and I'd like to avoid any "shelling out" to the command line and "low-level" routines as well as avoiding re-inventing the wheel myself (no parsing the mime-types file and such).

编辑和记事:

QT文件信息对象和答案没有明确的答案似乎是正确的,只要它去。但是这是一个烂掉的情况,我打开这个问题寻找更多的信息。

Hey, I originally asked this question about the QT file info object and the answer that "there is no clear answer" seems to be correct as far as it goes. BUT this is such a screwed-up situation that I am opening the question looking for more information.

我不再在乎QT,我只是寻找大炮的方式来找到mime类型通过C ++ / c函数调用KDE和gnome(特别是Gnome,因为这是最让我困惑的地方)。我想能够显示符合Nautilus的图标和描述在Gnome和Konquerer /任何对KDE,以及适当地打开文件等。

I don't care about QT in particular any more, I'm just looking for the most cannonical way to find the mime type via C++/c function calls on both KDE and gnome (especially Gnome, since that's where things confuse me most). I want to be able show icons and descriptions matching Nautilus in Gnome and Konquerer/whatever on KDE as well as opening files appropriately, etc.

我想,这个分别为KDE和Gnome。最大的问题是什么是最常见/最好/大炮的方式来获得所有这些信息为Linux桌面? Gnome文档特别不透明。 gnome-vsf有mime例程,但它已被弃用,我找不到一个mime例程为GIO / GFS,gnome-vsf的替换。有一个模糊的暗示,应该使用打开的桌面应用程序,但使用哪个是模糊的。 libmagic和xdg在哪里适合?

I suppose it's OK that I get this separately for KDE and Gnome. The big question is what's the most common/best/cannonical way to get all this information for the Linux desktop? Gnome documentation is especially opaque. gnome-vsf has mime routines but it's deprecated and I can't find a mime routine for GIO/GFS, gnome-vsf's replacement. There's a vague implication that one should use the open desktop applications but which one to use is obscure. And where does libmagic and xdg fit in?

指向总结高兴接受的问题的文章的指针。再次,我知道三行答案是没有这样的动物,但我正在寻找答案。

Pointers to an essay summarizing the issues gladly accepted. Again, I know the three line answer is "no such animal" but I'm looking for the long answer.

推荐答案

以下是使用GLib / GIO获取所需信息的示例。

Here is an example of using GLib/GIO to get the information you want.

#include <gio/gio.h>
#include <stdio.h>

int
main (int argc, char **argv)
{
    g_thread_init (NULL);
    g_type_init ();

    if (argc < 2)
        return -1;

    GError *error;
    GFile *file = g_file_new_for_path (argv[1]);
    GFileInfo *file_info = g_file_query_info (file,
                                              "standard::*",
                                              0,
                                              NULL,
                                              &error);

    const char *content_type = g_file_info_get_content_type (file_info);
    char *desc = g_content_type_get_description (content_type);
    GAppInfo *app_info = g_app_info_get_default_for_type (
                                  content_type,
                                  FALSE);

    /* you'd have to use g_loadable_icon_load to get the actual icon */
    GIcon *icon = g_file_info_get_icon (file_info);

    printf ("File: %s\nDescription: %s\nDefault Application: %s\n",
            argv[1],
            desc,
            g_app_info_get_executable (app_info));

    return 0;
}

这篇关于如何在Linux桌面中获取与文件相关联的图标,MIME类型和应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆