I’m trying to create a Nautilus extension in C, but there are just Python examples and helps.
Almost no documentation and literally no examples, but just some complete extensions which are long and hard to understand for a beginner.
I need just a simple sample code that creates a new column in Nautilus’s list view. How to write and compile it.
The code I’ve tried is:
#include <libnautilus-extension/nautilus-column-provider.h>
typedef struct _FooExtension FooExtension;
typedef struct _FooExtensionClass FooExtensionClass;
struct _FooExtension
{
GObject parent_slot;
};
struct _FooExtensionClass
{
GObjectClass parent_slot;
};
static void foo_extension_class_init (FooExtensionClass *class);
static void foo_extension_instance_init (FooExtension *img);
static void foo_extension_class_init(FooExtensionClass *class)
{
}
static void foo_extension_instance_init(FooExtension *img)
{
}
static GType provider_types[1];
static GType foo_extension_type;
static void foo_extension_register_type(GTypeModule *module)
{
static const GTypeInfo info = {
sizeof(FooExtensionClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) foo_extension_class_init,
NULL,
NULL,
sizeof (FooExtension),
0,
(GInstanceInitFunc) foo_extension_instance_init,
};
foo_extension_type = g_type_module_register_type(module,
G_TYPE_OBJECT,
"FooExtension",
&info, 0);
/* ... add interfaces ... */
}
GType foo_extension_get_type(void)
{
return foo_extension_type;
}
static GList *foo_extension_get_columns(NautilusColumnProvider *provider)
{
NautilusColumn *column;
GList *ret;
column = nautilus_column_new("FooExtension::foo_data_column", "FooExtension::foo_data", "Foo Data", "Foo Description");
/* _("Foo Data"),
_("Information from the Foo Extension"));*/
ret = g_list_append(NULL, column);
return ret;
}
void nautilus_module_initialize (GTypeModule *module)
{
foo_extension_register_type(module);
provider_types[0] = foo_extension_get_type();
}
void nautilus_module_shutdown(void)
{
/* Any module-specific shutdown */
}
void nautilus_module_list_types (const GType **types, int *num_types)
{
*types = provider_types;
*num_types = G_N_ELEMENTS (provider_types);
}
and I’ve built it with:
gcc-c foo_extension.c -o foo_extension.o -fPIC $(pkg-config libnautilus-extension --libs --cflags)
gcc -shared foo_extension.o -o foo_extension.so $(pkg-config libnautilus-extension --libs --cflags)
and I copied it into /usr/lib/nautilus/extensions-2.0/, then I tried nautilus -q but it doesn’t work.
You can also retrieve the documentation pointed in Nautilus Extension’s wiki from the copy in archive.org. The copy in archive.org has examples in C.
EDIT: I added a complete working example, as well as as an explanation of the missing parts in your code.
You are missing two things:
Below you have a working example. Beware NautilusFileInfoProvider is part of Nautilus’ asynchronous IO system. Hence, if the operations are slow, you will block Nautilus. In the example below I just set a fixed string per file (“Foo“). However, if you need to collect other information, you should implement also the arguments update_complete and handle, and the cancel_update interface. Check the documentation whose copy is available in archive.org
To compile it:
For testing the extension, first you need to stop any running instance of nautilus and re-launch nautilus. That is:
Note that is without the option -q you used, which is for quit.
If you would like to check if Nautilus is loading your extension, you can use strace as follow:
And see what libraries and files Nautilus is loading/opening.
While working in your extension, instead of copying the extension file in [libdir]/nautilus/extensions-3.0, you can create a symlink to your working directory. If you are using Nautilus 2.x, use [libdir]/nautilus/extensions-2.0 instead.