How do you get the icon, MIME type, and application associated with a file in the Linux Desktop?
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;
}
How do you make xdg-utils detect a file association for my application from a distribution package?
You don't need to run the mime-*
tools, but you do need to update the desktop and icon associations for the freedesktop environment after install like so:
update-mime-database /usr/share/mime
update-desktop-database
gtk-update-icon-cache /usr/share/icons/hicolor
These should be run after uninstall, too, to cleanup after removal. In RPMs this is the %postun
section.
Substitute /usr/share
with whatever deployment variable you might use like %{_datadir}
for RPM .spec's.
Also the icon name had an issue. It should have just been named 'xnec2c' not 'application-x-nec2' because the 'xnec2c' icon gets installed with the installer as follows:
%{_datadir}/icons/hicolor/scalable/apps/%{name}.svg
%{_datadir}/icons/hicolor/256x256/apps/%{name}.png
Get the default icon for specific file type in Ubuntu OS by python
GNU/Linux desktop environments do not assign icons to filename suffixes (extensions). Rather, they assign icons to Internet media types (MIME types), and a file’s type may or may not be determined by its filename suffix (content sniffing may also be involved). You can use the mimetypes
module from the standard library to guess the media type from the suffix. Then gio.content_type_get_icon
, and from then on it’s as in the question you linked.
import mimetypes
import gio
import gtk
def get_icon_path(extension, size=32):
type_, encoding = mimetypes.guess_type('x.' + extension)
if type_:
icon = gio.content_type_get_icon(type_)
theme = gtk.icon_theme_get_default()
info = theme.choose_icon(icon.get_names(), size, 0)
if info:
return info.get_filename()
Register file extensions / mime types in Linux
Use xdg-utils
from freedesktop.org Portland.
Register the icon for the MIME type:
xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype
Create a configuration file (freedesktop Shared MIME documentation):
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="application/x-mytype">
<comment>A witty comment</comment>
<comment xml:lang="it">Uno Commento</comment>
<glob pattern="*.myapp"/>
</mime-type>
</mime-info>
Install the configuration file:
xdg-mime install mytype-mime.xml
This gets your files recognized and associated with an icon. xdg-mime default
can be used for associating an application with the MIME type after you get a .desktop
file installed.
Where to install icon for a user-specific application while adhering to FreeDesktop standards?
Rereading the XDG Icon Specification, I noticed the specification references the XDG Base Directory Specification by including $XDG_DATA_DIRS
. This inclusion implies that that the Base Directory Specification is also in effect, which in turn mandates that
Lookups of the data file should search for ./subdir/filename relative to all base directories specified by $XDG_DATA_HOME and $XDG_DATA_DIRS . If an environment variable is either not set or empty, its default value as defined by this specification should be used instead. (src)
So, assuming my understanding is correct and XDG desktops adhere to specs, the user-specific icon location for your application is $XDG_DATA_HOME/icons/hicolor/48x48/apps/$YOURAPP.(png|xpm)
and $XDG_DATA_HOME/icons/hicolor/scalable/apps/$YOURAPP.svg
. File extensions are case-sensitive and only these three extensions are allowed. Also note that SVG support is optional.
If $XDG_DATA_HOME
is not set, you should default to $HOME/.local/share
as per the Base Directory Specification.
Related Topics
Why Do C++ Streams Use Char Instead of Unsigned Char
How to Typedef a Template Class
How to Define Sealed Class in C++
What Is the Right Approach When Using Stl Container for Median Calculation
Lru Implementation in Production Code
Simple Ipc Between C++ and Python (Cross Platform)
What Does "Typedef Void (*Something)()" Mean
How to Use New Std::Byte Type in Places Where Old-Style Unsigned Char Is Needed
Openmp Nested Parallel for Loops VS Inner Parallel For
How to Find the Name of the Calling Function
How to Call Function After Window Is Shown
What Is the Performance Impact of Using Int64_T Instead of Int32_T on 32-Bit Systems
How to Assign Multiple Values into a Struct at Once
How to Detect Region of Large # of White Pixels Using Opencv