How do I set an emblem with GTK/GIO?
Question
I'm trying to set an emblem using gio
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
GFile *gfile = NULL;
g_type_init();
gfile = g_file_new_for_path("./foo.txt");
if (g_file_set_attribute_string(gfile,
"metadata::emblems",
"favorite",
G_FILE_QUERY_INFO_NONE,
NULL, NULL) == TRUE) {
puts("Success");
} else {
puts("Fail");
}
return 0;
}
if the file exists, the function returns TRUE, which, according the docs means the metadata was set, but Nautilus (GNOME) doesn't display the favorite
emblem. There are not many example on the net, so I'm kind of stuck.
Solution
It looks like metadata::emblems needs an array of strings even if you are only setting one value. This seems to work:
char *value[] = {"favorite", '\0'};
[...]
g_file_set_attribute(file, "metadata::emblems",
G_FILE_ATTRIBUTE_TYPE_STRINGV,
&value[0],
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
OTHER TIPS
If you want Nautilus to show an emblem, you need to actually provide an extension to Nautilus to do so. Your extension should use the nautilus-info-provider interface, and in the nautilus_info_provider_update_file_info() function you can call the nautilus_file_info_add_emblem() function to add an emblem.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow