/* -*- Mode: C; c-basic-offset: 4 -*- */
%%
headers
#include <Python.h>

#define NO_IMPORT_PYGOBJECT
#include "pygobject.h"
#include <libgnomeui/libgnomeui.h>
#include <libgnomeui/gnome-ice.h>
/* #include <libgnomeui/gnome-appbar.h> */
#include <bonobo/bonobo-ui-type-builtins.h>
#define GNOME_TYPE_APP_BAR GNOME_TYPE_APPBAR
#define GNOME_APP_BAR GNOME_APPBAR
#define NO_IMPORT_PYGNOMEVFS
#include "pygnomevfs.h"
#include "config.h"

%%
modulename gnome.ui
%%
import gobject.GObject as PyGObject_Type
import gtk.Object as PyGtkObject_Type
import gtk.Widget as PyGtkWidget_Type
import gtk.Bin as PyGtkBin_Type
import gtk.Container as PyGtkContainer_Type
import gtk.HBox as PyGtkHBox_Type
import gtk.VBox as PyGtkVBox_Type
import gtk.Window as PyGtkWindow_Type
import gtk.Dialog as PyGtkDialog_Type
import gtk.Button as PyGtkButton_Type
import gtk.Image as PyGtkImage_Type
import gtk.Menu as PyGtkMenu_Type
import gtk.MenuBar as PyGtkMenuBar_Type
import gtk.Toolbar as PyGtkToolbar_Type
import gtk.Combo as PyGtkCombo_Type
import gtk.Adjustment as PyGtkAdjustment_Type
import gtk.Editable as PyGtkEditable_Type
import gtk.gdk.Pixbuf as PyGdkPixbuf_Type
import gnomecanvas.Canvas as PyGnomeCanvas_Type
import gnomecanvas.CanvasItem as PyGnomeCanvasItem_Type
import bonobo.ui.DockItem as PyBonoboDockItem_Type
import gtk.gdk.Screen as PyGdkScreen_Type
import gtk.IconTheme as PyGtkIconTheme_Type
import gtk.HBox as PyGtkHBox_Type
%%
ignore-glob
  *_get_type
  *_construct

%%
ignore
  gnome_window_icon_init
  gnome_window_icon_set_from_file
  gnome_window_icon_set_from_file_list
  gnome_window_icon_set_default_from_file
  gnome_window_icon_set_default_from_file_list
  gnome_ok_dialog
  gnome_message_box_new
  gnome_icon_lookup_sync

%%
override gnome_about_new kwargs
static int
_wrap_gnome_about_new (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char  *kwlist[] = { "name", "version", "copyright", "comments",
			       "authors", "documenters", "translator_credits",
			       "logo_pixbuf", NULL };
    
    char       *name, *version, *copyright, *comments;
    PyObject   *pyauthors;
    PyObject   *pydocumenters = Py_None;
    char       *translator_credits = NULL;
    PyGObject  *pypixbuf = NULL;
    int         n_authors;
    char      **authors;
    int         i;
    int         n_documenters;
    char      **documenters;
    GdkPixbuf  *pixbuf;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "ssssO|OzO!:GnomeAbout.__init__", kwlist,
                                     &name, &version, &copyright, &comments,
				     &pyauthors, &pydocumenters,
				     &translator_credits, &PyGdkPixbuf_Type, &pypixbuf)) {
	return -1;
    }

    if (!PyList_Check(pyauthors) && !PyTuple_Check(pyauthors)) {
	PyErr_SetString(PyExc_TypeError, "5th argument not a tuple or list");
	return -1;
    }

    if (pydocumenters != Py_None && !PyList_Check(pydocumenters) && !PyTuple_Check(pydocumenters)) {
	PyErr_SetString(PyExc_TypeError, "6th argument not a tuple or list");
	return -1;
    }
    
    n_authors = PySequence_Length(pyauthors);
    authors = g_new(gchar *, n_authors + 1);
    for (i = 0; i < n_authors; i++) {
        PyObject *item = PySequence_GetItem(pyauthors, i);
        Py_DECREF(item); 
        authors[i] = PyString_AsString(item);
    }
    authors[n_authors] = NULL;
    
    if (pydocumenters != Py_None) {
	    n_documenters = PySequence_Length(pydocumenters);
	    documenters = g_new(gchar *, n_documenters + 1);
	    for (i = 0; i < n_documenters; i++) {
		    PyObject *item = PySequence_GetItem(pydocumenters, i);
		    Py_DECREF(item); 
		    documenters[i] = PyString_AsString(item);
	    }
	    documenters[n_documenters] = NULL;
    } else {
	    documenters = NULL;
    }

    /* if translator_credits is "translator_credits", set it to NULL */
    if (translator_credits &&
	!strcmp(translator_credits, "translator_credits"))
	translator_credits = NULL;

    if (pypixbuf != NULL) {
	pixbuf = GDK_PIXBUF(pypixbuf->obj);
    } else {
	pixbuf = NULL;
    }
    
    self->obj = (GObject*)gnome_about_new (name,
					   version,
					   copyright,
					   comments,
					   (const gchar**)authors,
					   (const gchar**)documenters,
					   translator_credits,
					   pixbuf);
    if (!self->obj) {
        PyErr_SetString(PyExc_RuntimeError,"could not create GnomeAbout object");
        return -1;
    }

    g_object_ref(self->obj); /* we don't own the first reference of windows */
    pygobject_register_wrapper((PyObject *)self);
    return 0;
}
%%
override gnome_icon_list_get_icon_data kwargs
static PyObject *
_wrap_gnome_icon_list_get_icon_data(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "idx", NULL };
    int idx;
    PyGObject *data;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "i:GnomeIconList.get_icon_data",
				     kwlist, &idx))
        return NULL;
    
    data = gnome_icon_list_get_icon_data(GNOME_ICON_LIST(self->obj), idx);
    if (!data) {
	Py_INCREF(Py_None);
	return Py_None;
    }
    
    Py_INCREF(data);
    return (PyObject*)data;
}
%%
override gnome_icon_list_set_icon_data kwargs
static PyObject *
_wrap_gnome_icon_list_set_icon_data(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "idx", "data", NULL };
    int idx;
    PyGObject *data;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "iO:GnomeIconList.set_icon_data",
				     kwlist, &idx, &data))
        return NULL;
    
    gnome_icon_list_set_icon_data(GNOME_ICON_LIST(self->obj),
				  idx, data);
    
    Py_INCREF(Py_None);
    return Py_None;
}
%%
override gnome_icon_list_get_selection
static PyObject *
_wrap_gnome_icon_list_get_selection(PyGObject *self)
{
    GList    *list, *l;
    PyObject *rv;
    int       len, i;

    list = gnome_icon_list_get_selection(GNOME_ICON_LIST(self->obj));
    len = g_list_length(list);
    rv = PyList_New(len);
    if (!rv) {
	return NULL;
    }
    for (i = 0, l = list; i < len; ++i, l = l->next)
	PyList_SET_ITEM(rv, i, PyInt_FromLong(GPOINTER_TO_INT(l->data)));
    g_list_free(list);
    return rv;
}
%%
override gnome_client_set_restart_command kwargs

typedef void (*clientfunc) (GnomeClient *, int, gchar*[]);

static PyObject*
generic_gnome_client_command (PyGObject *self, PyObject*args, PyObject* kwargs,
			      const char *name, clientfunc func)
{
    char **argv;
    int argc, i;
    PyObject *py_argv;
    char fmt[128];

    if (PySequence_Length(args) == 2) {
	static char *kwlist[] = { "argc", "argv", NULL };
	snprintf(fmt, 128, "iO:gome.ui.Client.%s", name);
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 fmt, kwlist, &argc, &py_argv))
	    return NULL;
	if (PyErr_Warn(PyExc_DeprecationWarning, "the 'argc' parameter"
		       " is optional and deprecated") < 0)
	    return NULL;
    } else {
	static char *kwlist[] = { "argv", NULL };
	snprintf(fmt, 128, "O:gome.ui.Client.%s", name);
	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
					 fmt, kwlist, &py_argv))
	    return NULL;
    }

    if (!PySequence_Check(py_argv)) {
	PyErr_SetString(PyExc_TypeError, "argv argument is not a sequence");
	return NULL;
    }

    argc = PySequence_Length(py_argv);
    argv = g_new(gchar *, argc + 1);
    for (i = 0; i < argc; i++) {
	PyObject *item = PySequence_GetItem(py_argv, i);
	if (!PyString_Check(item)) {
	    PyErr_SetString(PyExc_TypeError, "argv element is not a string");
	    Py_DECREF(item);
	    g_free(argv);
	    return NULL;
	}
	argv[i] = PyString_AsString(item);
	Py_DECREF(item);
    }
    argv[i] = NULL;
    func(GNOME_CLIENT(self->obj), argc, argv);
    g_free(argv);
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
_wrap_gnome_client_set_restart_command (PyGObject *self, PyObject *args,
					PyObject *kwargs)
{
    return generic_gnome_client_command(self, args, kwargs,
					"set_restart_command",
					gnome_client_set_restart_command);
}
%%
override gnome_client_set_clone_command kwargs
static PyObject*
_wrap_gnome_client_set_clone_command (PyGObject *self, PyObject *args,
				      PyObject *kwargs)
{
    return generic_gnome_client_command(self, args, kwargs,
					"set_clone_command",
					gnome_client_set_clone_command);
}
%%
override gnome_client_set_discard_command kwargs
static PyObject*
_wrap_gnome_client_set_discard_command (PyGObject *self, PyObject *args,
					PyObject *kwargs)
{
    return generic_gnome_client_command(self, args, kwargs,
					"set_discard_command",
					gnome_client_set_discard_command);
}
%%
override gnome_client_set_resign_command kwargs
static PyObject*
_wrap_gnome_client_set_resign_command (PyGObject *self, PyObject *args,
				       PyObject *kwargs)
{
    return generic_gnome_client_command(self, args, kwargs,
					"set_resign_command",
					gnome_client_set_resign_command);
}
%%
override gnome_client_set_shutdown_command kwargs
static PyObject*
_wrap_gnome_client_set_shutdown_command (PyGObject *self, PyObject *args,
					 PyObject *kwargs)
{
    return generic_gnome_client_command(self, args, kwargs,
					"set_shutdown_command",
					gnome_client_set_shutdown_command);
}
%%
override gnome_client_module_info_get noargs
static PyObject *
_wrap_gnome_client_module_info_get(PyObject *self)
{
    GnomeModuleInfo *ret;

    ret = (GnomeModuleInfo *) gnome_client_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(GNOME_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override libgnomeui_module_info_get noargs
static PyObject *
_wrap_libgnomeui_module_info_get(PyObject *self)
{
    GnomeModuleInfo *ret;

    ret = (GnomeModuleInfo *) libgnomeui_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(GNOME_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override gnome_gtk_module_info_get noargs
static PyObject *
_wrap_gnome_gtk_module_info_get(PyObject *self)
{
    GnomeModuleInfo *ret;

    ret = (GnomeModuleInfo *) gnome_gtk_module_info_get();
    /* pyg_boxed_new handles NULL checking */
    return pyg_boxed_new(GNOME_TYPE_MODULE_INFO, ret, TRUE, TRUE);
}
%%
override gnome_icon_lookup kwargs
static PyObject *
_wrap_gnome_icon_lookup(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "icon_theme", "thumbnail_factory", "file_uri",
                              "custom_icon", "flags", "mime_type", "file_info", NULL };
    PyGObject *icon_theme;
    char *file_uri, *custom_icon = NULL, *mime_type = NULL;
    gchar *ret;
    PyObject *file_info = NULL;
    PyObject *py_flags = NULL;
    PyObject *py_thumb_fact;
    GnomeThumbnailFactory *thumbnail_factory = NULL;
    GnomeIconLookupFlags flags;
    GnomeIconLookupResultFlags result_flags = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!Os|sOsO!:icon_lookup", kwlist,
                                     &PyGtkIconTheme_Type, &icon_theme,
                                     &py_thumb_fact,
                                     &file_uri, &custom_icon,
                                     &py_flags, &mime_type,
                                     &PyGnomeVFSFileInfo_Type, &file_info))
        return NULL;

    if ((file_info && !mime_type) || (!file_info && mime_type)) {
        PyErr_SetString(PyExc_TypeError, "file_info and mime_type mutually exclusive");
        return NULL;
    }

    if (py_flags) {
        if (pyg_flags_get_value(GNOME_TYPE_ICON_LOOKUP_FLAGS, py_flags, (gint *)&flags))
            return NULL;
    } else
        flags = GNOME_ICON_LOOKUP_FLAGS_NONE;
    
    if (pygobject_check(((PyGObject *) py_thumb_fact), &PyGnomeThumbnailFactory_Type))
        thumbnail_factory = GNOME_THUMBNAIL_FACTORY(((PyGObject *) py_thumb_fact)->obj);
    else if (py_thumb_fact == Py_None)
        thumbnail_factory = NULL;
    else {
        PyErr_SetString(PyExc_TypeError,
                        "thumbnail_factory has to be a GnomeThumbnailFactory or None");
        return NULL;
    }

    if (file_info)
        ret = gnome_icon_lookup(GTK_ICON_THEME(icon_theme->obj),
                                thumbnail_factory,
                                file_uri, custom_icon,
                                pygnome_vfs_file_info_get(file_info),
                                mime_type, flags, &result_flags);
    else
        ret = gnome_icon_lookup_sync(GTK_ICON_THEME(icon_theme->obj),
                                     thumbnail_factory,
                                     file_uri, custom_icon,
                                     flags, &result_flags);
    if (ret) {
        PyObject *py_ret = Py_BuildValue("si", ret, result_flags);
        g_free(ret);
        return py_ret;
    }
      /* The function should never return NULL; if it does, it's an internal bug */
    PyErr_SetString(PyExc_RuntimeError, "internal error");
    return NULL;
}
%%
override gnome_appbar_new kwargs
static int
_wrap_gnome_appbar_new(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    GType obj_type = pyg_type_from_object((PyObject *) self);
    GParameter params[3];
    PyObject *parsed_args[3] = {NULL, };
    char *arg_names[] = {"has_progress", "has_status", "interactivity", NULL };
    char *prop_names[] = {"has_progress", "has_status", "interactivity", NULL };
    guint nparams, i;

      /* http://bugzilla.gnome.org/show_bug.cgi?id=334836 */
    if ((args? PySequence_Size(args) : 0) + (kwargs? PyDict_Size(kwargs) : 0) < 3) {
        if (PyErr_Warn(PyExc_DeprecationWarning, "gnome.ui.AppBar constructor takes exactly 3 arguments,"
                       " but calling old gtk.HBox constructor for compatibility reasons.") < 0)
            return -1;
        return PyGtkHBox_Type.tp_init((PyObject *) self, args, kwargs);
    }

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO:gnome.ui.AppBar.__init__" , arg_names ,
                                     &parsed_args[0] , &parsed_args[1] , &parsed_args[2]))
        return -1;

    memset(params, 0, sizeof(GParameter)*3);
    if (!pyg_parse_constructor_args(obj_type, arg_names,
                                    prop_names, params, 
                                    &nparams, parsed_args))
        return -1;
    pygobject_constructv(self, nparams, params);
    for (i = 0; i < nparams; ++i)
        g_value_unset(&params[i].value);
    if (!self->obj) {
        PyErr_SetString(
            PyExc_RuntimeError, 
            "could not create gnome.ui.AppBar object");
        return -1;
    }
    return 0;
}
