nautilus-python-object.c 13 KB
Newer Older
Johan Dahlin's avatar
Johan Dahlin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 *  Copyright (C) 2004 Novell, Inc.
 *
 *  This library 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 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Dave Camp <dave@ximian.com>
 * 
 */

#include <config.h>

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

#include <libnautilus-extension/nautilus-extension-types.h>

#include <pygobject.h>

/* Nautilus extension headers */
#include <libnautilus-extension/nautilus-file-info.h>
#include <libnautilus-extension/nautilus-info-provider.h>
#include <libnautilus-extension/nautilus-column-provider.h>
#include <libnautilus-extension/nautilus-menu-item.h>
#include <libnautilus-extension/nautilus-menu-provider.h>
#include <libnautilus-extension/nautilus-property-page-provider.h>

#include <string.h>

#define METHOD_PREFIX ""

static GObjectClass *parent_class;

/* 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)                                        \
	if (!PyObject_HasAttrString(self, METHOD_NAME))                    \
		goto beach;
	
#define CONVERT_LIST(py_files, files)                                  \
	{                                                                  \
		GList *l;                                                      \
        py_files = PyList_New(0);                                      \
		for (l = files; l; l = l->next) {                              \
61 62 63
            PyObject *obj = pygobject_new((GObject*)l->data);          \
			PyList_Append(py_files, obj);							   \
			Py_DECREF(obj);                                            \
Johan Dahlin's avatar
Johan Dahlin committed
64 65 66 67 68
		}                                                              \
	}

#define HANDLE_RETVAL(py_ret)                                          \
    if (!py_ret) {                                                     \
69
		PyErr_Print();                                                 \
Johan Dahlin's avatar
Johan Dahlin committed
70
		goto beach;                                                    \
71 72
 	} else if (py_ret == Py_None) {                                    \
 		goto beach;                                                    \
Johan Dahlin's avatar
Johan Dahlin committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	}

#define HANDLE_LIST(py_ret, type, type_name)                           \
    {                                                                  \
        int i = 0;                                                     \
    	if (!PySequence_Check(py_ret) || PyString_Check(py_ret)) {     \
    		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;                                            \
    		}                                                          \
93 94
    		ret = g_list_append (ret, (type*) g_object_ref(py_item->obj)); \
            Py_DECREF(py_item);                                        \
Johan Dahlin's avatar
Johan Dahlin committed
95 96 97 98 99 100 101 102 103
    	}                                                              \
    }

#define METHOD_NAME "get_property_pages"
static GList *
nautilus_python_object_get_property_pages (NautilusPropertyPageProvider *provider,
										   GList *files)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
104
    PyObject *py_files, *py_ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
105
    GList *ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
106
	PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
107 108
	
  	debug_enter();
Johan Dahlin's avatar
Johan Dahlin committed
109

Johan Dahlin's avatar
Johan Dahlin committed
110 111 112 113 114
	CHECK_METHOD_NAME(object->instance);

	CONVERT_LIST(py_files, files);
	
    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
115
								 "(N)", py_files);
Johan Dahlin's avatar
Johan Dahlin committed
116 117 118 119 120
	HANDLE_RETVAL(py_ret);

	HANDLE_LIST(py_ret, NautilusPropertyPage, "nautilus.PropertyPage");
	
 beach:
121
	Py_XDECREF(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
122
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    return ret;
}
#undef METHOD_NAME


static void
nautilus_python_object_property_page_provider_iface_init (NautilusPropertyPageProviderIface *iface)
{
	iface->get_pages = nautilus_python_object_get_property_pages;
}

#define METHOD_NAME "get_file_items"
static GList *
nautilus_python_object_get_file_items (NautilusMenuProvider *provider,
									   GtkWidget *window,
									   GList *files)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
    GList *ret = NULL;
142 143
    PyObject *py_ret = NULL, *py_files;
	PyGILState_STATE state = pyg_gil_state_ensure();
Johan Dahlin's avatar
Johan Dahlin committed
144 145 146 147 148 149
	
  	debug_enter();

	CHECK_METHOD_NAME(object->instance);

	CONVERT_LIST(py_files, files);
150

Johan Dahlin's avatar
Johan Dahlin committed
151
    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
152
								 "(NN)", pygobject_new((GObject *)window), py_files);
Johan Dahlin's avatar
Johan Dahlin committed
153 154 155 156 157
	HANDLE_RETVAL(py_ret);

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

 beach:
158
	Py_XDECREF(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
159
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
160 161 162 163 164 165 166 167 168 169 170 171
    return ret;
}
#undef METHOD_NAME

#define METHOD_NAME "get_background_items"
static GList *
nautilus_python_object_get_background_items (NautilusMenuProvider *provider,
											 GtkWidget *window,
											 NautilusFileInfo *file)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
    GList *ret = NULL;
172
    PyObject *py_ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
173
	PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
174 175 176 177 178 179
	
  	debug_enter();
	
	CHECK_METHOD_NAME(object->instance);

    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
180
								 "(NN)",
Johan Dahlin's avatar
Johan Dahlin committed
181 182 183 184 185 186 187 188
								 pygobject_new((GObject *)window),
								 pygobject_new((GObject *)file));
								 
	HANDLE_RETVAL(py_ret);

	HANDLE_LIST(py_ret, NautilusMenuItem, "nautilus.MenuItem");
	
 beach:
189
	Py_XDECREF(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
190
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
191 192 193 194 195 196 197 198 199 200 201 202
    return ret;
}
#undef METHOD_NAME

#define METHOD_NAME "get_toolbar_items"
static GList *
nautilus_python_object_get_toolbar_items (NautilusMenuProvider *provider,
										  GtkWidget *window,
										  NautilusFileInfo *file)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
    GList *ret = NULL;
203
    PyObject *py_ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
204
	PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
205 206 207 208 209 210
	
  	debug_enter();

	CHECK_METHOD_NAME(object->instance);

    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
211
								 "(NN)",
Johan Dahlin's avatar
Johan Dahlin committed
212 213 214 215 216 217 218
								 pygobject_new((GObject *)window),
								 pygobject_new((GObject *)file));
	HANDLE_RETVAL(py_ret);

	HANDLE_LIST(py_ret, NautilusMenuItem, "nautilus.MenuItem");
	
 beach:
219
	Py_XDECREF(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
220
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    return ret;
}
#undef METHOD_NAME

static void
nautilus_python_object_menu_provider_iface_init (NautilusMenuProviderIface *iface)
{
	iface->get_background_items = nautilus_python_object_get_background_items;
	iface->get_toolbar_items = nautilus_python_object_get_toolbar_items;
	iface->get_file_items = nautilus_python_object_get_file_items;
}

#define METHOD_NAME "get_columns"
static GList *
nautilus_python_object_get_columns (NautilusColumnProvider *provider)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
    GList *ret = NULL;
    PyObject *py_ret;
Johan Dahlin's avatar
Johan Dahlin committed
240
	PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
241 242 243 244 245 246 247 248 249 250 251 252 253

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

    py_ret = PyObject_CallMethod(object->instance, METHOD_PREFIX METHOD_NAME,
								 NULL);

	HANDLE_RETVAL(py_ret);

	HANDLE_LIST(py_ret, NautilusColumn, "nautilus.Column");
	
 beach:
Johan Dahlin's avatar
Johan Dahlin committed
254
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    return ret;
}
#undef METHOD_NAME

static void
nautilus_python_object_column_provider_iface_init (NautilusColumnProviderIface *iface)
{
	iface->get_columns = nautilus_python_object_get_columns;
}


#define METHOD_NAME "cancel_update"
static void
nautilus_python_object_cancel_update (NautilusInfoProvider *provider,
									  NautilusOperationHandle *handle)
{
  	debug_enter();
}
#undef METHOD_NAME

#define METHOD_NAME "update_file_info"
static NautilusOperationResult
nautilus_python_object_update_file_info (NautilusInfoProvider *provider,
										 NautilusFile *file,
										 GClosure *update_complete,
										 NautilusOperationHandle **handle)
{
	NautilusPythonObject *object = (NautilusPythonObject*)provider;
    NautilusOperationResult ret = NAUTILUS_OPERATION_COMPLETE;
284
    PyObject *py_ret = NULL;
Johan Dahlin's avatar
Johan Dahlin committed
285
	PyGILState_STATE state = pyg_gil_state_ensure();                                    \
Johan Dahlin's avatar
Johan Dahlin committed
286 287 288 289 290 291
	
  	debug_enter();

	CHECK_METHOD_NAME(object->instance);

    py_ret = PyObject_CallMethod(object->instance,
292
								 METHOD_PREFIX METHOD_NAME, "(N)",
Johan Dahlin's avatar
Johan Dahlin committed
293 294 295 296 297 298 299 300 301 302 303 304 305
								 pygobject_new((GObject*)file));
	HANDLE_RETVAL(py_ret);


	if (!PyInt_Check(py_ret)) {
		PyErr_SetString(PyExc_TypeError,
						METHOD_NAME " must return None or a int");
		goto beach;
	}

	ret = PyInt_AsLong(py_ret);
	
 beach:
306
	Py_XDECREF(py_ret);
Johan Dahlin's avatar
Johan Dahlin committed
307
	pyg_gil_state_release(state);
Johan Dahlin's avatar
Johan Dahlin committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    return ret;
}
#undef METHOD_NAME

static void
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;
}

static void 
nautilus_python_object_instance_init (NautilusPythonObject *object)
{
	NautilusPythonObjectClass *class;
  	debug_enter();

	class = (NautilusPythonObjectClass*)(((GTypeInstance*)object)->g_class);

	object->instance = PyObject_CallObject(class->type, NULL);
328 329
	if (object->instance == NULL)
		PyErr_Print();
Johan Dahlin's avatar
Johan Dahlin committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

static void
nautilus_python_object_finalize (GObject *object)
{
  	debug_enter();

	Py_DECREF(((NautilusPythonObject *)object)->instance);
}

static void
nautilus_python_object_class_init (NautilusPythonObjectClass *class,
								   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;
}

GType 
nautilus_python_object_get_type (GTypeModule *module, 
								 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 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
	};

Johan Dahlin's avatar
Johan Dahlin committed
385
	debug_enter_args("type=%s", PyString_AsString(PyObject_GetAttrString(type, "__name__")));
Johan Dahlin's avatar
Johan Dahlin committed
386 387 388 389 390 391 392 393 394 395 396 397 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 424 425 426 427 428 429
	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",
								PyString_AsString(PyObject_GetAttrString(type, "__name__")));
		
	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*)&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;
}