How to create nautilus C extensions
Asked Answered
P

1

10

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.

Pegu answered 12/3, 2012 at 21:52 Comment(1)
pastebin.com/gyCU2sti <- That is the code. 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 putted it in "/usr/lib/nautilus/extensions-2.0/" so "nautilus -q" and it doesn't worked...Hoboken
L
8

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:

  1. 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.
  2. 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.

Liebknecht answered 4/5, 2012 at 6:21 Comment(9)
The first link has only Python examples, the second one is the one which I used for my code but without success. nautilus-sendto is not for ColumnView, just for context menu, so even if I learn from it reading the code probably it will be useless.Hoboken
You asked for writing and compiling. nautilus-sendto already has the toolchain set up, therefore, you can use it as a template for your project. Start with something you know it works and you can check. Once you have that set, then start to apply your changes.Liebknecht
I'll try giving a look, but probably (even if it's a simple plugin) it will be too complex to have a "quick feedback" about extensions and will be useless. Time ago I already tried with another simple project and it was too long to read and understand just for what I need.Hoboken
Fair enough. I edited my answer and now you have an explanation of the missing parts in your code as well as a working example.Liebknecht
Thank you very much, I'll try as soon as I can and let you know!Hoboken
Thanks, your example is working, later I'll give a better look to the code and to my mistakes, but there's just one mistake in your you answer: If you call "nautilus" without the -q option (while it's already running), it will neither reload itself nor the extension, it will just open the file manager in your home.Hoboken
strace -e trace=open nautilus 2>&1 | grep foo is not working either.Hoboken
I assumed you did not have nautilus running when trying the extension. In that case, first run 'nautilus -q' to stop nautilus. Once nautilus has stopped, run 'nautilus' without '-q'. I wonder how did you get my example working, because it should be in the strace output.Liebknecht
nautilus -q, stop nautilus, but something (gnome-session I think) run it again when stopped. So I can just use nautilus -q to make sure it restarts. Probably strace doesn't work because it's not "tracing" the nautilus istance which is loading the extension, it just trace the "new file manager windows" I think... I can't use "strace nautilus" for this purpose until I stop nautilus and prevent gnome-session from re-executing it.Hoboken

© 2022 - 2024 — McMap. All rights reserved.