nautilus-python-object.c 20.1 KB
Newer Older
Johan Dahlin's avatar
Johan Dahlin committed
1 2
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
3
 *  Copyright (C) 2004,2005 Johan Dahlin
Johan Dahlin's avatar
Johan Dahlin committed
4
 *
5 6 7 8
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
Johan Dahlin's avatar
Johan Dahlin committed
9
 *
10
 *  This program is distributed in the hope that it will be useful,
Johan Dahlin's avatar
Johan Dahlin committed
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
Johan Dahlin's avatar
Johan Dahlin committed
14
 *
15 16 17
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
Johan Dahlin's avatar
Johan Dahlin committed
18 19 20 21 22 23 24
 */

#include <config.h>

#include "nautilus-python-object.h"
#include "nautilus-python.h"

25
#include <nautilus-extension.h>
Johan Dahlin's avatar
Johan Dahlin committed
26 27 28 29 30 31 32 33 34

#include <pygobject.h>

#include <string.h>

#define METHOD_PREFIX ""

static GObjectClass *parent_class;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
int __PyString_Check(PyObject *obj) {
#if PY_MAJOR_VERSION >= 3
    return PyUnicode_Check(obj);
#else
    return PyString_Check(obj);
#endif
}

char* __PyString_AsString(PyObject *obj) {
#if PY_MAJOR_VERSION >= 3
    return PyUnicode_AsUTF8(obj);
#else
    return PyString_AsString(obj);
#endif
}

PyObject* __PyString_FromString(const char *c) {
#if PY_MAJOR_VERSION >= 3
    return PyUnicode_FromString(c);
#else
    return PyString_FromString(c);
#endif
}

int __PyInt_Check(PyObject *obj) {
#if PY_MAJOR_VERSION >= 3
    return PyLong_Check(obj);
#else
    return PyInt_Check(obj);
#endif
}

Johan Dahlin's avatar
Johan Dahlin committed
67 68 69 70 71 72 73
/* These macros assumes the following things:
 *   a METHOD_NAME is defined with is a string
 *   a goto label called beach
 *   the return value is called ret
 */

#define CHECK_METHOD_NAME(self)                                        \
Adam Plumb's avatar
Adam Plumb committed
74 75
    if (!PyObject_HasAttrString(self, METHOD_NAME))                    \
        goto beach;
76

Adam Plumb's avatar
Adam Plumb committed
77 78 79 80 81 82
#define CHECK_OBJECT(object)                                           \
      if (object->instance == NULL)                                       \
      {                                                                   \
          g_object_unref (object);                                       \
          goto beach;                                                       \
      }                                                                   \
83

Johan Dahlin's avatar
Johan Dahlin committed
84
#define CONVERT_LIST(py_files, files)                                  \
Adam Plumb's avatar
Adam Plumb committed
85 86
    {                                                                  \
        GList *l;                                                      \
Johan Dahlin's avatar
Johan Dahlin committed
87
        py_files = PyList_New(0);                                      \
Adam Plumb's avatar
Adam Plumb committed
88 89 90 91 92
        for (l = files; l; l = l->next)                                \
        {                                                              \
            PyList_Append(py_files, pygobject_new((GObject*)l->data)); \
        }                                                              \
    }
Johan Dahlin's avatar
Johan Dahlin committed
93 94

#define HANDLE_RETVAL(py_ret)                                          \
95 96 97
    if (!py_ret)                                                       \
    {                                                                  \
        PyErr_Print();                                                 \
Adam Plumb's avatar
Adam Plumb committed
98 99 100 101 102 103
        goto beach;                                                    \
    }                                                                  \
    else if (py_ret == Py_None)                                        \
    {                                                                  \
        goto beach;                                                    \
    }
Johan Dahlin's avatar
Johan Dahlin committed
104 105 106

#define HANDLE_LIST(py_ret, type, type_name)                           \
    {                                                                  \
107
        Py_ssize_t i = 0;                                              \
108
        if (!PySequence_Check(py_ret) || __PyString_Check(py_ret))       \
Adam Plumb's avatar
Adam Plumb committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        {                                                              \
            PyErr_SetString(PyExc_TypeError,                           \
                            METHOD_NAME " must return a sequence");    \
            goto beach;                                                \
        }                                                              \
        for (i = 0; i < PySequence_Size (py_ret); i++)                 \
        {                                                              \
            PyGObject *py_item;                                        \
            py_item = (PyGObject*)PySequence_GetItem (py_ret, i);      \
            if (!pygobject_check(py_item, &Py##type##_Type))           \
            {                                                          \
                PyErr_SetString(PyExc_TypeError,                       \
                                METHOD_NAME                            \
                                " must return a sequence of "          \
                                type_name);                            \
                goto beach;                                            \
            }                                                          \
            ret = g_list_append (ret, (type*) g_object_ref(py_item->obj));  \
127
            Py_DECREF(py_item);                                        \
Adam Plumb's avatar
Adam Plumb committed
128
        }                                                              \
Johan Dahlin's avatar
Johan Dahlin committed
129
    }
130 131 132


static void
Adam Plumb's avatar
Adam Plumb committed
133 134 135 136 137 138
free_pygobject_data(gpointer data, gpointer user_data) {
    /* Some NautilusFile objects are cached and not freed until nautilus
        itself is closed.  Since PyGObject stores data that must be freed by
        the Python interpreter, we must always free it before the interpreter
        is finalized. */
    g_object_set_data((GObject *)data, "PyGObject::instance-data", NULL);
139 140 141
}

static void
Adam Plumb's avatar
Adam Plumb committed
142 143 144 145 146
free_pygobject_data_list(GList *list) {
    if (list == NULL)
        return;
        
    g_list_foreach(list, (GFunc)free_pygobject_data, NULL);
147
}
Johan Dahlin's avatar
Johan Dahlin committed
148

149
static PyObject *
Adam Plumb's avatar
Adam Plumb committed
150 151 152 153 154 155 156
nautilus_python_boxed_new (PyTypeObject *type, gpointer boxed, gboolean free_on_dealloc) {
    PyGBoxed *self = (PyGBoxed *) type->tp_alloc (type, 0);
    self->gtype = pyg_type_from_object ( (PyObject *) type);
    self->boxed = boxed;
    self->free_on_dealloc = free_on_dealloc;
    
    return (PyObject *) self;
157 158
}

Johan Dahlin's avatar
Johan Dahlin committed
159 160 161
#define METHOD_NAME "get_property_pages"
static GList *
nautilus_python_object_get_property_pages (NautilusPropertyPageProvider *provider,
Adam Plumb's avatar
Adam Plumb committed
162 163
                                           GList                         *files) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
164
    PyObject *py_files, *py_ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
165
    GList *ret = NULL;
Adam Plumb's avatar
Adam Plumb committed
166 167 168
    PyGILState_STATE state = pyg_gil_state_ensure();
    
    debug_enter();
Johan Dahlin's avatar
Johan Dahlin committed
169

Adam Plumb's avatar
Adam Plumb committed
170 171
    CHECK_OBJECT(object);
    CHECK_METHOD_NAME(object->instance);
Johan Dahlin's avatar
Johan Dahlin committed
172

Adam Plumb's avatar
Adam Plumb committed
173 174
    CONVERT_LIST(py_files, files);
    
Johan Dahlin's avatar
Johan Dahlin committed
175
    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
Adam Plumb's avatar
Adam Plumb committed
176 177 178 179 180 181 182 183
                                 "(N)", py_files);
    HANDLE_RETVAL(py_ret);

    HANDLE_LIST(py_ret, NautilusPropertyPage, "Nautilus.PropertyPage");
    
beach:
    Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
184 185 186 187 188
    return ret;
}
#undef METHOD_NAME

static void
Adam Plumb's avatar
Adam Plumb committed
189 190
nautilus_python_object_property_page_provider_iface_init (NautilusPropertyPageProviderIface *iface) {
    iface->get_pages = nautilus_python_object_get_property_pages;
Johan Dahlin's avatar
Johan Dahlin committed
191 192
}

193 194 195
#define METHOD_NAME "get_widget"
static GtkWidget *
nautilus_python_object_get_widget (NautilusLocationWidgetProvider *provider,
Adam Plumb's avatar
Adam Plumb committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209
                                   const char                        *uri,
                                   GtkWidget                       *window) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
    GtkWidget *ret = NULL;
    PyObject *py_ret = NULL;
    PyGObject *py_ret_gobj;
    PyObject *py_uri = NULL;
    PyGILState_STATE state = pyg_gil_state_ensure();

    debug_enter();

    CHECK_OBJECT(object);
    CHECK_METHOD_NAME(object->instance);

210
    py_uri = __PyString_FromString(uri);
Adam Plumb's avatar
Adam Plumb committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
                                 "(NN)", py_uri,
                                 pygobject_new((GObject *)window));
    HANDLE_RETVAL(py_ret);

    py_ret_gobj = (PyGObject *)py_ret;
    if (!pygobject_check(py_ret_gobj, &PyGtkWidget_Type)) {
        PyErr_SetString(PyExc_TypeError,
                        METHOD_NAME "should return a gtk.Widget");
        goto beach;
    }

    ret = (GtkWidget *)g_object_ref(py_ret_gobj->obj);

beach:
    Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
    return ret;
230 231 232 233
}
#undef METHOD_NAME

static void
Adam Plumb's avatar
Adam Plumb committed
234 235
nautilus_python_object_location_widget_provider_iface_init (NautilusLocationWidgetProviderIface *iface) {
    iface->get_widget = nautilus_python_object_get_widget;
236 237
}

Johan Dahlin's avatar
Johan Dahlin committed
238 239 240
#define METHOD_NAME "get_file_items"
static GList *
nautilus_python_object_get_file_items (NautilusMenuProvider *provider,
Adam Plumb's avatar
Adam Plumb committed
241 242 243
                                       GtkWidget             *window,
                                       GList                 *files) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
Johan Dahlin's avatar
Johan Dahlin committed
244
    GList *ret = NULL;
245
    PyObject *py_ret = NULL, *py_files;
Adam Plumb's avatar
Adam Plumb committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    PyGILState_STATE state = pyg_gil_state_ensure();
    
    debug_enter();

    CHECK_OBJECT(object);    

    if (PyObject_HasAttrString(object->instance, "get_file_items_full")) {
        CONVERT_LIST(py_files, files);
        py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX "get_file_items_full",
                                     "(NNN)",
                                     pygobject_new((GObject *)provider), 
                                     pygobject_new((GObject *)window), 
                                     py_files);
    }
    else if (PyObject_HasAttrString(object->instance, "get_file_items")) {
        CONVERT_LIST(py_files, files);
        py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
                                     "(NN)", 
                                     pygobject_new((GObject *)window), 
                                     py_files);
    }
    else {
        goto beach;
    }

    HANDLE_RETVAL(py_ret);

    HANDLE_LIST(py_ret, NautilusMenuItem, "Nautilus.MenuItem");

beach:
	free_pygobject_data_list(files);
    Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
279 280 281 282 283 284 285
    return ret;
}
#undef METHOD_NAME

#define METHOD_NAME "get_background_items"
static GList *
nautilus_python_object_get_background_items (NautilusMenuProvider *provider,
Adam Plumb's avatar
Adam Plumb committed
286 287 288
                                             GtkWidget               *window,
                                             NautilusFileInfo       *file) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
Johan Dahlin's avatar
Johan Dahlin committed
289
    GList *ret = NULL;
290
    PyObject *py_ret = NULL;
Adam Plumb's avatar
Adam Plumb committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    PyGILState_STATE state = pyg_gil_state_ensure();
    
    debug_enter();

    CHECK_OBJECT(object);

    if (PyObject_HasAttrString(object->instance, "get_background_items_full")) {
        py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX "get_background_items_full",
                                     "(NNN)",
                                     pygobject_new((GObject *)provider),
                                     pygobject_new((GObject *)window),
                                     pygobject_new((GObject *)file));
    }
    else if (PyObject_HasAttrString(object->instance, "get_background_items")) {
        py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
                                     "(NN)",
                                     pygobject_new((GObject *)window),
                                     pygobject_new((GObject *)file));
    }
    else {
        goto beach;
    }

    HANDLE_RETVAL(py_ret);

    HANDLE_LIST(py_ret, NautilusMenuItem, "Nautilus.MenuItem");
    
beach:
   free_pygobject_data(file, NULL);
    Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
322 323 324 325 326
    return ret;
}
#undef METHOD_NAME

static void
Adam Plumb's avatar
Adam Plumb committed
327 328 329
nautilus_python_object_menu_provider_iface_init (NautilusMenuProviderIface *iface) {
    iface->get_background_items = nautilus_python_object_get_background_items;
    iface->get_file_items = nautilus_python_object_get_file_items;
Johan Dahlin's avatar
Johan Dahlin committed
330 331 332 333
}

#define METHOD_NAME "get_columns"
static GList *
Adam Plumb's avatar
Adam Plumb committed
334 335
nautilus_python_object_get_columns (NautilusColumnProvider *provider) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
Johan Dahlin's avatar
Johan Dahlin committed
336
    GList *ret = NULL;
337
    PyObject *py_ret = NULL;
Adam Plumb's avatar
Adam Plumb committed
338
    PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
339

Adam Plumb's avatar
Adam Plumb committed
340 341 342 343
    debug_enter();
        
    CHECK_OBJECT(object);
    CHECK_METHOD_NAME(object->instance);
Johan Dahlin's avatar
Johan Dahlin committed
344 345

    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
Adam Plumb's avatar
Adam Plumb committed
346
                                 NULL);
Johan Dahlin's avatar
Johan Dahlin committed
347

Adam Plumb's avatar
Adam Plumb committed
348
    HANDLE_RETVAL(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
349

Adam Plumb's avatar
Adam Plumb committed
350 351 352 353 354 355
    HANDLE_LIST(py_ret, NautilusColumn, "Nautilus.Column");
    
beach:
    if (py_ret != NULL)
        Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
356 357 358 359 360
    return ret;
}
#undef METHOD_NAME

static void
Adam Plumb's avatar
Adam Plumb committed
361 362
nautilus_python_object_column_provider_iface_init (NautilusColumnProviderIface *iface) {
    iface->get_columns = nautilus_python_object_get_columns;
Johan Dahlin's avatar
Johan Dahlin committed
363 364 365 366 367
}


#define METHOD_NAME "cancel_update"
static void
Adam Plumb's avatar
Adam Plumb committed
368 369 370 371 372
nautilus_python_object_cancel_update (NautilusInfoProvider         *provider,
                                      NautilusOperationHandle     *handle) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
    PyGILState_STATE state = pyg_gil_state_ensure();
    PyObject *py_handle = nautilus_python_boxed_new (_PyNautilusOperationHandle_Type, handle, FALSE);
373

Adam Plumb's avatar
Adam Plumb committed
374
    debug_enter();
375

Adam Plumb's avatar
Adam Plumb committed
376 377
    CHECK_OBJECT(object);
    CHECK_METHOD_NAME(object->instance);
378 379

    PyObject_CallMethod(object->instance,
Adam Plumb's avatar
Adam Plumb committed
380 381 382
                                 METHOD_PREFIX METHOD_NAME, "(NN)",
                                 pygobject_new((GObject*)provider),
                                 py_handle);
383

Adam Plumb's avatar
Adam Plumb committed
384 385
beach:
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
386 387 388 389 390
}
#undef METHOD_NAME

#define METHOD_NAME "update_file_info"
static NautilusOperationResult
Adam Plumb's avatar
Adam Plumb committed
391 392 393 394 395
nautilus_python_object_update_file_info (NautilusInfoProvider         *provider,
                                         NautilusFile                 *file,
                                         GClosure                     *update_complete,
                                         NautilusOperationHandle   **handle) {
    NautilusPythonObject *object = (NautilusPythonObject*)provider;
Johan Dahlin's avatar
Johan Dahlin committed
396
    NautilusOperationResult ret = NAUTILUS_OPERATION_COMPLETE;
397
    PyObject *py_ret = NULL;
Adam Plumb's avatar
Adam Plumb committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    PyGILState_STATE state = pyg_gil_state_ensure();
    PyObject *py_handle = nautilus_python_boxed_new (_PyNautilusOperationHandle_Type, *handle, FALSE);

    debug_enter();

    CHECK_OBJECT(object);

    if (PyObject_HasAttrString(object->instance, "update_file_info_full")) {
        py_ret = PyObject_CallMethod(object->instance,
                                     METHOD_PREFIX "update_file_info_full", "(NNNN)",
                                     pygobject_new((GObject*)provider),
                                     py_handle,
                                     pyg_boxed_new(G_TYPE_CLOSURE, update_complete, TRUE, TRUE),
                                     pygobject_new((GObject*)file));
    }
    else if (PyObject_HasAttrString(object->instance, "update_file_info")) {
        py_ret = PyObject_CallMethod(object->instance,
                                     METHOD_PREFIX METHOD_NAME, "(N)",
                                     pygobject_new((GObject*)file));
    }
    else {
        goto beach;
    }
    
    HANDLE_RETVAL(py_ret);

424
    if (!__PyInt_Check(py_ret)) {
Adam Plumb's avatar
Adam Plumb committed
425 426 427 428 429
        PyErr_SetString(PyExc_TypeError,
                        METHOD_NAME " must return None or a int");
        goto beach;
    }

430 431 432
#if PY_MAJOR_VERSION >= 3
    ret = PyLong_AsLong(py_ret);
#else
Adam Plumb's avatar
Adam Plumb committed
433
    ret = PyInt_AsLong(py_ret);
434 435
#endif

Adam Plumb's avatar
Adam Plumb committed
436 437 438 439
beach:
    free_pygobject_data(file, NULL);
    Py_XDECREF(py_ret);
    pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
440 441 442 443 444
    return ret;
}
#undef METHOD_NAME

static void
Adam Plumb's avatar
Adam Plumb committed
445 446 447
nautilus_python_object_info_provider_iface_init (NautilusInfoProviderIface *iface) {
    iface->cancel_update = nautilus_python_object_cancel_update;
    iface->update_file_info = nautilus_python_object_update_file_info;
Johan Dahlin's avatar
Johan Dahlin committed
448 449 450
}

static void 
Adam Plumb's avatar
Adam Plumb committed
451 452 453
nautilus_python_object_instance_init (NautilusPythonObject *object) {
    NautilusPythonObjectClass *class;
    debug_enter();
Johan Dahlin's avatar
Johan Dahlin committed
454

Adam Plumb's avatar
Adam Plumb committed
455
    class = (NautilusPythonObjectClass*)(((GTypeInstance*)object)->g_class);
Johan Dahlin's avatar
Johan Dahlin committed
456

Adam Plumb's avatar
Adam Plumb committed
457 458 459
    object->instance = PyObject_CallObject(class->type, NULL);
    if (object->instance == NULL)
        PyErr_Print();
Johan Dahlin's avatar
Johan Dahlin committed
460 461 462
}

static void
Adam Plumb's avatar
Adam Plumb committed
463 464
nautilus_python_object_finalize (GObject *object) {
    debug_enter();
Johan Dahlin's avatar
Johan Dahlin committed
465

Adam Plumb's avatar
Adam Plumb committed
466 467
    if (((NautilusPythonObject *)object)->instance != NULL)
        Py_DECREF(((NautilusPythonObject *)object)->instance);
Johan Dahlin's avatar
Johan Dahlin committed
468 469 470 471
}

static void
nautilus_python_object_class_init (NautilusPythonObjectClass *class,
Adam Plumb's avatar
Adam Plumb committed
472 473 474 475 476 477 478 479
                                   gpointer                   class_data) {
    debug_enter();

    parent_class = g_type_class_peek_parent (class);
    
    class->type = (PyObject*)class_data;
    
    G_OBJECT_CLASS (class)->finalize = nautilus_python_object_finalize;
Johan Dahlin's avatar
Johan Dahlin committed
480 481 482 483
}

GType 
nautilus_python_object_get_type (GTypeModule *module, 
Adam Plumb's avatar
Adam Plumb committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
                                 PyObject     *type) {
    GTypeInfo *info;
    const char *type_name;
    GType gtype;
      
    static const GInterfaceInfo property_page_provider_iface_info = {
        (GInterfaceInitFunc) nautilus_python_object_property_page_provider_iface_init,
        NULL,
        NULL
    };

    static const GInterfaceInfo location_widget_provider_iface_info = {
        (GInterfaceInitFunc) nautilus_python_object_location_widget_provider_iface_init,
        NULL,
        NULL
    };

    static const GInterfaceInfo menu_provider_iface_info = {
        (GInterfaceInitFunc) nautilus_python_object_menu_provider_iface_init,
        NULL,
        NULL
    };

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

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

519
    debug_enter_args("type=%s", __PyString_AsString(PyObject_GetAttrString(type, "__name__")));
Adam Plumb's avatar
Adam Plumb committed
520 521 522 523 524 525 526 527 528 529 530
    info = g_new0 (GTypeInfo, 1);
    
    info->class_size = sizeof (NautilusPythonObjectClass);
    info->class_init = (GClassInitFunc)nautilus_python_object_class_init;
    info->instance_size = sizeof (NautilusPythonObject);
    info->instance_init = (GInstanceInitFunc)nautilus_python_object_instance_init;

    info->class_data = type;
    Py_INCREF(type);

    type_name = g_strdup_printf("%s+NautilusPython",
531
                                __PyString_AsString(PyObject_GetAttrString(type, "__name__")));
Adam Plumb's avatar
Adam Plumb committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
        
    gtype = g_type_module_register_type (module, 
                                         G_TYPE_OBJECT,
                                         type_name,
                                         info, 0);

    if (PyObject_IsSubclass(type, (PyObject*)&PyNautilusPropertyPageProvider_Type)) {
        g_type_module_add_interface (module, gtype, 
                                     NAUTILUS_TYPE_PROPERTY_PAGE_PROVIDER,
                                     &property_page_provider_iface_info);
    }

    if (PyObject_IsSubclass(type, (PyObject*)&PyNautilusLocationWidgetProvider_Type)) {
        g_type_module_add_interface (module, gtype,
                                     NAUTILUS_TYPE_LOCATION_WIDGET_PROVIDER,
                                     &location_widget_provider_iface_info);
    }
    
    if (PyObject_IsSubclass(type, (PyObject*)&PyNautilusMenuProvider_Type)) {
        g_type_module_add_interface (module, gtype, 
                                     NAUTILUS_TYPE_MENU_PROVIDER,
                                     &menu_provider_iface_info);
    }

    if (PyObject_IsSubclass(type, (PyObject*)&PyNautilusColumnProvider_Type)) {
        g_type_module_add_interface (module, gtype, 
                                     NAUTILUS_TYPE_COLUMN_PROVIDER,
                                     &column_provider_iface_info);
    }
    
    if (PyObject_IsSubclass(type, (PyObject*)&PyNautilusInfoProvider_Type)) {
        g_type_module_add_interface (module, gtype, 
                                     NAUTILUS_TYPE_INFO_PROVIDER,
                                     &info_provider_iface_info);
    }
    
    return gtype;
Johan Dahlin's avatar
Johan Dahlin committed
569
}