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:
- Add the interfaces. For the column provider would be foo_extension_column_provider_iface_init, and there you need to associate the link the interfaces expected with your implementation. In this particular case get_columns.
- With the previous one, you would get only a column but with unknown value for each file. Therefore, you have to use the InfoProvider, too. In particular, the interface update_file_info. In that interface you can associate the attribute for your column with each file through nautilus_file_info_add_string_attribute.
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
#include <libnautilus-extension/nautilus-column-provider.h>
#include <libnautilus-extension/nautilus-info-provider.h>
typedef struct _FooExtension FooExtension;
typedef struct _FooExtensionClass FooExtensionClass;
typedef struct {
GClosure *update_complete;
NautilusInfoProvider *provider;
NautilusFileInfo *file;
int operation_handle;
gboolean cancelled;
} UpdateHandle;
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 GList *foo_extension_get_columns (NautilusColumnProvider *provider);
static NautilusOperationResult foo_extension_update_file_info (
NautilusInfoProvider *provider,
NautilusFileInfo *file,
GClosure *update_complete,
NautilusOperationHandle **handle);
/* Interfaces */
static void
foo_extension_column_provider_iface_init (NautilusColumnProviderIface *iface) {
iface->get_columns = foo_extension_get_columns;
return;
}
static void
foo_extension_info_provider_iface_init (NautilusInfoProviderIface *iface) {
iface->update_file_info = foo_extension_update_file_info;
return;
}
/* Extension */
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,
};
static const GInterfaceInfo column_provider_iface_info = {
(GInterfaceInitFunc) foo_extension_column_provider_iface_init,
NULL,
NULL
};
static const GInterfaceInfo info_provider_iface_info = {
(GInterfaceInitFunc) foo_extension_info_provider_iface_init,
NULL,
NULL
};
foo_extension_type = g_type_module_register_type(module,
G_TYPE_OBJECT,
"FooExtension",
&info, 0);
/* ... add interfaces ... */
g_type_module_add_interface (module,
foo_extension_type,
NAUTILUS_TYPE_COLUMN_PROVIDER,
&column_provider_iface_info);
g_type_module_add_interface (module,
foo_extension_type,
NAUTILUS_TYPE_INFO_PROVIDER,
&info_provider_iface_info);
}
GType foo_extension_get_type(void)
{
return foo_extension_type;
}
/* Column interfaces */
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");
ret = g_list_append(NULL, column);
return ret;
}
/* Info interfaces */
static NautilusOperationResult
foo_extension_update_file_info (NautilusInfoProvider *provider,
NautilusFileInfo *file,
GClosure *update_complete,
NautilusOperationHandle **handle)
{
char *data;
/* Check if we've previously cached the file info */
data = g_object_get_data (G_OBJECT (file), "foo_extension_data");
/* get and provide the information associated with the column.
If the operation is not fast enough, we should use the arguments
update_complete and handle for asyncrhnous operation. */
if (!data) {
data = g_strdup ("Foo");
}
nautilus_file_info_add_string_attribute (file,
"FooExtension::foo_data",
data);
return NAUTILUS_OPERATION_COMPLETE;
}
/* Extension initialization */
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);
}
To compile it:
$ gcc -c foo-extension.c -o foo-extension.o -fPIC $(pkg-config libnautilus-extension --cflags)
$ gcc -shared foo-extension.o -o foo-extension.so $(pkg-config libnautilus-extension --libs)
For testing the extension, first you need to stop any running instance of nautilus and re-launch nautilus. That is:
$ nautilus -q
$ nautilus
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:
$ strace -e trace=open nautilus
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.