How to Set My Application's Desktop Icon for Linux: Kde, Gnome etc

How to set my application's desktop icon for Linux: KDE, Gnome etc?

For Gnome and Kde, you would probably want to include a desktop file with your app that defines how it will be launched. The specification can be found here. If you have an installer included with your app, you would probably want to have it generate this desktop file and put it in the right places to make menu entries and whatnot

Programmatically set custom folder/directory icon in Linux

If you are using the KDE desktop, then simply create a .directory file inside the directory.

E.g. I have a custom icon for my directory named "dev.sparc". I created this using the KDE desktop. All it does is create a file which I can view.

$ cat dev.sparc/.directory
[Desktop Entry]
Icon=applications-engineering

Et voila. That's all there is to it. If you or your users are using Gnome, this will be similar but probably subtly different. Just create an empty directory, use Gnome to set an icon, then $ ls -al in the directory to see what file it created.

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 to programmatically set custom folder icon in GNOME?

I finally figured out how to do this! Here's a Python script that works in the standard Gnome environment:

#!/usr/bin/env python

import sys
from gi.repository import Gio

if len(sys.argv) not in (2, 3):
print 'Usage: {} FOLDER [ICON]'.format(sys.argv[0])
print 'Leave out ICON to unset'
sys.exit(0)

folder = Gio.File.new_for_path(sys.argv[1])
icon_file = Gio.File.new_for_path(sys.argv[2]) if len(sys.argv) == 3 else None

# Get a file info object
info = folder.query_info('metadata::custom-icon', 0, None)

if icon_file is not None:
icon_uri = icon_file.get_uri()
info.set_attribute_string('metadata::custom-icon', icon_uri)
else:
# Change the attribute type to INVALID to unset it
info.set_attribute('metadata::custom-icon',
Gio.FileAttributeType.INVALID, '')

# Write the changes back to the file
folder.set_attributes_from_info(info, 0, None)

Can we develop kde applications using a machine that runs gnome?

Yes. You just have to make sure you have the KDE libraries and development headers installed. (Should be a package named something like kdelibs-devel in your package manager.) You may find it difficult to test your application properly, though, for obvious reasons.



Related Topics



Leave a reply



Submit