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

#define NO_IMPORT_PYGOBJECT
#include "pygobject.h"
#include <libgnome/gnome-desktop-item.h>

#include "config.h"

/*
 This wrapper does not free the string in the list, just the list itself
 */
static PyObject *
_helper_wrap_string_glist (GList *list)
{
    GList *tmp;
    PyObject *py_list;

    if ((py_list = PyList_New(0)) == NULL) {
        g_list_free(list);
        return NULL;
    }
    for (tmp = list; tmp != NULL; tmp = tmp->next) {
        PyObject *str_obj =  PyString_FromString ((char*)tmp->data);

        if (str_obj == NULL) {
            g_list_free(list);
            Py_DECREF(py_list);
            return NULL;
        }
        PyList_Append(py_list, str_obj);
        Py_DECREF(str_obj);
    }
    g_list_free(list);
    return py_list;
}

static PyObject *
_helper_wrap_string_array (char **strings)
{
    int i;
    PyObject *py_list;

    if ((py_list = PyList_New(0)) == NULL) {
        return NULL;
    }
    
    for (i = 0; strings[i] != NULL; i++) {
        PyObject *str_obj =  PyString_FromString (strings[i]);

        if (str_obj == NULL) {
            Py_DECREF(py_list);
            return NULL;
        }
        PyList_Append(py_list, str_obj);
        Py_DECREF(str_obj);
    }
    return py_list;
}

%%
modulename gnomedesktop
%%
import gtk.IconTheme as PyGtkIconTheme_Type
import gtk.gdk.Screen as PyGdkScreen_Type
%%
ignore-glob
  *_get_type
  gnome_desktop_item_error_quark
  gnome_desktop_item_launch_with_env
  gnome_desktop_item_drop_uri_list_with_env
%%
override gnome_desktop_item_get_languages kwargs
static PyObject *
_wrap_gnome_desktop_item_get_languages (PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "attr", NULL };
    char        *attr;
    GList       *langs;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GnomeDesktopItem.get_languages", kwlist, &attr))
        return NULL;
    
    langs = gnome_desktop_item_get_languages (pyg_boxed_get(self, GnomeDesktopItem), attr);
    
    return _helper_wrap_string_glist (langs);
}
%%
override gnome_desktop_item_get_strings kwargs
static PyObject *
_wrap_gnome_desktop_item_get_strings (PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "attr", NULL };
    char        *attr;
    char       **strings;
    PyObject    *ret;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GnomeDesktopItem.get_strings", kwlist, &attr))
        return NULL;
    
    strings = gnome_desktop_item_get_strings (pyg_boxed_get(self, GnomeDesktopItem), attr);
    
    ret = _helper_wrap_string_array (strings);
    /* FIXME: The doc says to: gnome_desktop_item_free_string_list (strings);
       But this function does not exists, using g_strfreev.. */
    g_strfreev (strings);
    
    return ret;
}
%%
override gnome_desktop_item_set_strings kwargs
static PyObject *
_wrap_gnome_desktop_item_set_strings (PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "attr", "strings", NULL };
    char        *attr;
    PyObject   *pystrings;
    int         i;
    int         n_strings = 0;
    char       **strings;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO:GnomeDesktopItem.set_strings", kwlist, &attr, &pystrings))
        return NULL;
    
    if (!PyList_Check(pystrings) && !PyTuple_Check(pystrings)) {
        PyErr_SetString(PyExc_TypeError, "2nd argument not a tuple or list");
        return NULL;
    }

    n_strings = PySequence_Length(pystrings);
    strings = g_new(gchar *, n_strings + 1);
    for (i = 0; i < n_strings; i++) {
        PyObject *item = PySequence_GetItem(pystrings, i);
        Py_DECREF(item); 
        strings[i] = PyString_AsString(item);
    }
    strings[n_strings] = NULL;
        
    gnome_desktop_item_set_strings (pyg_boxed_get(self, GnomeDesktopItem), attr, strings);
    
    g_free (strings);
    
    Py_INCREF(Py_None);
    return Py_None;
}
%%
override gnome_desktop_item_drop_uri_list kwargs
static PyObject *
_wrap_gnome_desktop_item_drop_uri_list (PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "uri_list", "flags", "env", NULL };
    char       *uri_list;
    GnomeDesktopItemLaunchFlags flags = 0;
    PyObject   *pyenv = Py_None;
    int         n_env = 0;
    char      **envp = NULL;
    GError     *error = NULL;
    int i;
    int ret;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO:GnomeDesktopItem.drop_uri_list", kwlist,
                                                    &uri_list, &flags, &pyenv))
        return NULL;
    
    if (pyenv != Py_None) {
        if (!PyList_Check(pyenv) && !PyTuple_Check(pyenv)) {
            PyErr_SetString(PyExc_TypeError, "3rd argument not a tuple or list");
            return NULL;
        }
    
        n_env = PySequence_Length(pyenv);
        envp = g_new(gchar *, n_env + 1);
        for (i = 0; i < n_env; i++) {
            PyObject *item = PySequence_GetItem(pyenv, i);
            Py_DECREF(item); 
            envp[i] = PyString_AsString(item);
        }
        envp[n_env] = NULL;
        ret = gnome_desktop_item_drop_uri_list_with_env (pyg_boxed_get(self, GnomeDesktopItem), uri_list, flags, envp, &error);
    }
    else {
        ret = gnome_desktop_item_drop_uri_list (pyg_boxed_get(self, GnomeDesktopItem), uri_list, flags, &error);
    }
    g_free (envp);
    
    if (pyg_error_check(&error))
        return NULL;
    return PyInt_FromLong(ret);
}
%%
override gnome_desktop_item_launch kwargs
static PyObject *
_wrap_gnome_desktop_item_launch (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char  *kwlist[] = { "file_list", "flags", "env", NULL };
    
    PyObject   *pyfile_list;
    PyObject   *pyenv = Py_None;
    GnomeDesktopItemLaunchFlags flags = 0;
    int         i;
    int         n_files = 0;
    GList      *files = NULL;
    int         n_env = 0;
    char      **envp = NULL;
    GError     *error = NULL;
    int         result;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "O|iO:GnomeDesktopItem.launch", kwlist,
                                     &pyfile_list, &flags, &pyenv)) {
        return NULL;
    }

    if (!PyList_Check(pyfile_list) && !PyTuple_Check(pyfile_list)) {
        PyErr_SetString(PyExc_TypeError, "1st argument not a tuple or list");
        return NULL;
    }
           
    n_files = PySequence_Length(pyfile_list);
    for (i = 0; i < n_files; i++) {
        PyObject *item = PySequence_GetItem(pyfile_list, i);
        Py_DECREF(item); 
        files = g_list_append (files, PyString_AsString(item));
    }
    
    if (pyenv != Py_None) {
        if (!PyList_Check(pyenv) && !PyTuple_Check(pyenv)) {
            PyErr_SetString(PyExc_TypeError, "3rd argument not a tuple or list");
            return NULL;
        }
    
        n_env = PySequence_Length(pyenv);
        envp = g_new(gchar *, n_env + 1);
        for (i = 0; i < n_env; i++) {
            PyObject *item = PySequence_GetItem(pyenv, i);
            Py_DECREF(item); 
            envp[i] = PyString_AsString(item);
        }
        envp[n_env] = NULL;
        
        result = gnome_desktop_item_launch_with_env (
                            pyg_boxed_get(self, GnomeDesktopItem),
                            files,
                            flags,
                            envp,
                            &error);
    }
    else {
        result = gnome_desktop_item_launch (
                            pyg_boxed_get(self, GnomeDesktopItem),
                            files,
                            flags,
                            &error);
    }
    g_free (envp);
    g_list_free (files);
    
    if (pyg_error_check(&error))
        return NULL;
        
    return PyInt_FromLong(result);
}
%%
override gnome_desktop_item_launch_on_screen kwargs
static PyObject *
_wrap_gnome_desktop_item_launch_on_screen (PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char  *kwlist[] = { "file_list", "flags", "screen", "workspace", NULL };
    
    PyObject   *pyfile_list;
    PyObject   *pyscreen;
    GnomeDesktopItemLaunchFlags flags = 0;
    int workspace;
    
    int         i;
    int         n_files = 0;
    GList      *files = NULL;
    GError     *error = NULL;
    int         result;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "OiOi:GnomeDesktopItem.launch", kwlist,
                                     &pyfile_list, &flags, &PyGdkScreen_Type, &pyscreen, &workspace)) {
        return NULL;
    }

    if (!PyList_Check(pyfile_list) && !PyTuple_Check(pyfile_list)) {
        PyErr_SetString(PyExc_TypeError, "1st argument not a tuple or list");
        return NULL;
    }
            
    n_files = PySequence_Length(pyfile_list);
    for (i = 0; i < n_files; i++) {
        PyObject *item = PySequence_GetItem(pyfile_list, i);
        Py_DECREF(item); 
        files = g_list_append (files, PyString_AsString(item));
    }
        
    result = gnome_desktop_item_launch_on_screen (
                        pyg_boxed_get(self, GnomeDesktopItem),
                        files,
                        flags,
                        GDK_SCREEN (pygobject_get (pyscreen)),
                        workspace,
                        &error);
    
    g_list_free (files);
    if (pyg_error_check(&error))
        return NULL;
        
    return PyInt_FromLong(result);
}
