Custom cell renderer for custom list view column in a nautilus-extension?



Hi folks,

Been knocking up a simple nautilus extension in C to add a new
column to the list view that will show mtime's as a fuller string such
as 

        Wed Jun 30 21:49:08 1993

This works fine, until you come to sort that column, it of course
gets sorted in string order rather than chronological order.

So OK, what I'd do here would be to store the mtime as a time_t in
the list store and then add a cell renderer to format that as
required. I knocked up a simple app to check that works and it does.
When you click the column heading it sorts in chronological order.

However, I just can't see how to do this within the nautilus-extension
framework.

I've looked at the nautilus source, to see if I can see how it does its
"Modified" column, but so far can't really find it.

Below is the bulk of the code I've got so far, largely
knocked together from bits I found on the net.

I'm adding two columns here, one with the above date string format and
one in, YYYY-MM-DD hh:mm:ss format (which of course does sort
chronologically).

I should add this is with nautilus-3.6.3

Any hints would be greatly appreciated.

Cheers,
Andrew

void nautilus_module_initialize(GTypeModule *module)
{
        mtime_extension_register_type(module);
        provider_types[0] = mtime_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);
}

static void mtime_extension_column_provider_iface_init(
                NautilusColumnProviderIface *iface)
{
        iface->get_columns = mtime_extension_get_columns;
}

static void mtime_extension_info_provider_iface_init(
                NautilusInfoProviderIface *iface)
{
        iface->update_file_info = mtime_extension_update_file_info;
}

static void mtime_extension_register_type(GTypeModule *module)
{
        static const GTypeInfo info = {
                sizeof(MTimeExtClass),
                (GBaseInitFunc)NULL,
                (GBaseFinalizeFunc)NULL,
                NULL,
                NULL,
                NULL,
                sizeof(MTimeExt),
                0,
                NULL,
        };

        static const GInterfaceInfo column_provider_iface_info = {
                (GInterfaceInitFunc)mtime_extension_column_provider_iface_init,
                NULL,
                NULL
        };

        static const GInterfaceInfo info_provider_iface_info = {
                (GInterfaceInitFunc)mtime_extension_info_provider_iface_init,
                NULL,
                NULL
        };

        mtime_extension_type = g_type_module_register_type(module,
                        G_TYPE_OBJECT,
                        "MTimeExt",
                        &info, 0);

        /* ... add interfaces ... */
        g_type_module_add_interface(module,
                        mtime_extension_type,
                        NAUTILUS_TYPE_COLUMN_PROVIDER,
                        &column_provider_iface_info);

        g_type_module_add_interface(module,
                        mtime_extension_type,
                        NAUTILUS_TYPE_INFO_PROVIDER,
                        &info_provider_iface_info);
}

GType mtime_extension_get_type(void)
{
        return mtime_extension_type;
}

/* Column interfaces */
static GList *mtime_extension_get_columns(NautilusColumnProvider *provider)
{
        NautilusColumn *column;
        GList *clist;

        column = nautilus_column_new("MTimeExt::mtime_data_column",
                        "MTimeExt::mtime_data",
                        "Modified Time",
                        "Show full mtime");
        clist = g_list_append(NULL, column);

        column = nautilus_column_new("MTimeExt::mtime_data_ts_column",
                        "MTimeExt::mtime_ts_data",
                        "Modified Time",
                        "Show full mtime");
        clist = g_list_append(clist, column);

        return clist;
}

/* Info interfaces */
static NautilusOperationResult mtime_extension_update_file_info(
                NautilusInfoProvider *provider,
                NautilusFileInfo *file,
                GClosure *update_complete,
                NautilusOperationHandle **handle)
{
        char *data;
        char datetime[32];
        struct stat sb;
        struct tm *tm;
        GFile *loc;

        loc = nautilus_file_info_get_location(file);
        stat(g_file_get_path(loc), &sb);

        g_snprintf(datetime, sizeof(datetime), "%s", ctime(&sb.st_mtime));
        /* Loose the '\n' added by ctime(3) */
        datetime[strlen(datetime) - 1] = '\0';
        nautilus_file_info_add_string_attribute(file, "MTimeExt::mtime_data",
                        datetime);

        tm = localtime(&sb.st_mtime);
        g_snprintf(datetime, sizeof(datetime), "%d-%02d:%02d %02d:%02d:%02d",
                        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
                        tm->tm_hour, tm->tm_min, tm->tm_sec);
        nautilus_file_info_add_string_attribute(file,
                        "MTimeExt::mtime_ts_data", datetime);

        return NAUTILUS_OPERATION_COMPLETE;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]