/* -*- Mode: C; c-basic-offset: 4; tab-width: 4 -*- */
%%
headers
#include <time.h>
#include <Python.h>
#include "config.h"
#define NO_IMPORT_PYGOBJECT
#include "pygobject.h"

#include "override_common.h"

#include "evo-ecal-environment.h"
#include "evo-calendar.h"

static PyObject *
_helper_wrap_glist_of_evo_locations(GList *locations)
{
	PyObject *result;
	int i;

	if ((result = PyList_New (g_list_length (locations))) == NULL)
		return NULL;

	i = 0;
	for (; locations != NULL; locations = locations->next) {
		evo_location_t *path = (evo_location_t *) locations->data;
		PyObject *t = PyTuple_New (2);
		if (path->name == NULL) {
			Py_INCREF (Py_None);
			PyTuple_SET_ITEM (t, 0, Py_None);
		} else {
			PyTuple_SET_ITEM (t, 0, PyString_FromString(path->name));
		}
		
		if (path->uri == NULL) {
			Py_INCREF (Py_None);
			PyTuple_SET_ITEM (t, 1, Py_None);
		} else {
			PyTuple_SET_ITEM (t, 1, PyString_FromString(path->uri));
		}

		PyList_SET_ITEM (result, i, t);
		i++;
	}
	return result;
}

%%
modulename ecal
%%
import gobject.GObject as PyGObject_Type  
%%
ignore-glob
	init
	free_*
	*_get_type
%%
override evo_environment_list_calendars noargs
static PyObject*
_wrap_evo_environment_list_calendars(PyGObject *self) 
{
    GList *list = NULL;
    list = evo_environment_list_cal_sources(E_CAL_SOURCE_TYPE_EVENT);
    return _helper_wrap_glist_of_evo_locations(list);
}
%%
override evo_environment_list_task_sources noargs 
static PyObject*
_wrap_evo_environment_list_task_sources(PyGObject *self) 
{
    GList *list = NULL;
    list = evo_environment_list_cal_sources(E_CAL_SOURCE_TYPE_TODO);
    return _helper_wrap_glist_of_evo_locations(list);
}
%%
override evo_environment_list_memo_sources noargs 
static PyObject*
_wrap_evo_environment_list_memo_sources(PyGObject *self) 
{
    GList *list = NULL;
    list = evo_environment_list_cal_sources(E_CAL_SOURCE_TYPE_JOURNAL);
    return _helper_wrap_glist_of_evo_locations(list);
}
%%
override evo_cal_source_get_all_objects noargs
static PyObject*
_wrap_evo_cal_source_get_all_objects(PyGObject *self) 
{
    GList *objects = NULL;
    GError *error = NULL;

    e_cal_get_object_list_as_comp(
                    E_CAL(self->obj), 
                    "#t", 
                    &objects, 
                    &error);

    //Fixme: Check error
    //evo_cal_source_print_all_objects(E_CAL(self->obj));

    return _helper_wrap_gobject_glist(objects);
}
%%
override e_cal_component_new kwargs
static int
_wrap_e_cal_component_new(PyGObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "ical", "type", NULL };
    gchar *ical = NULL;
    int type = E_CAL_COMPONENT_NO_TYPE;
    ECalComponent *comp = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|z:ECalComponent.__init__",
                                     kwlist, &type, &ical))
        return -1;

    if (ical) {
        comp = (ECalComponent *) e_cal_component_new_from_string(ical);
    } else {
        comp = (ECalComponent *) e_cal_component_new();
        e_cal_component_set_new_vtype(comp, type);
        
    }

    if (!comp) {
        PyErr_SetString(PyExc_RuntimeError, "could not create ECalComponent object");
        return -1;
    } else {
        self->obj = (GObject*) comp;
        return 0;
    }

}
%%
override e_cal_get_component_as_string args
static PyObject *
_wrap_e_cal_get_component_as_string(PyGObject *self, PyObject *args)
{
	char *s;
	PyGObject *obj;
	icalcomponent *ic;

	if (!PyArg_ParseTuple(args, "O!:ECal.get_component_as_string", &PyECalComponent_Type, (PyGObject **)&obj))
        return NULL;

	ic = e_cal_component_get_icalcomponent(E_CAL_COMPONENT(obj->obj));
	s = e_cal_get_component_as_string(E_CAL(self->obj),ic);

	return PyString_FromString(s);
}

%%
override evo_cal_component_get_due noargs
static PyObject *
_wrap_evo_cal_component_get_due(PyGObject *self)
{
    ECalComponent *calcomponent;
    ECalComponentDateTime dt;
    glong t;
    
    calcomponent = E_CAL_COMPONENT(self->obj);
    e_cal_component_get_due(calcomponent, &dt);

    if (dt.value == NULL) {
        e_cal_component_free_datetime(&dt);
        Py_INCREF(Py_None);
        return Py_None;
    }
    t = icaltime_as_timet_with_zone(*(dt.value),
                                    icaltimezone_get_utc_timezone());
    e_cal_component_free_datetime(&dt);
    return PyInt_FromLong(t);
}

%%
override evo_cal_component_set_due args
static PyObject *
_wrap_evo_cal_component_set_due(PyGObject *self, PyObject *args)
{
    ECalComponent *calcomponent;
    ECalComponentDateTime *dt;
    PyObject* due;

    if (PyTuple_Size(args) != 1) {
        PyErr_SetString(PyExc_RuntimeError, "ECalComponent.set_due takes exactly 1 argument");
        return NULL;
    }
    calcomponent = E_CAL_COMPONENT(self->obj);
    due =  PyTuple_GET_ITEM(args, 0);
    
    if (due == Py_None) {
        // TV-TODO: how to clear the due date do this? gnome bugzilla #452915
        //e_cal_component_set_due(calcomponent, NULL);
        icalcomponent *ic = e_cal_component_get_icalcomponent (calcomponent);
        icalproperty *ip = icalcomponent_get_first_property(ic, ICAL_DUE_PROPERTY);
        if (ip != NULL) {    
            icalcomponent_remove_property (ic, ip);
            icalproperty_free (ip);
        }
        e_cal_component_set_icalcomponent(calcomponent, ic);
        e_cal_component_rescan(calcomponent);
        
        Py_INCREF(Py_None);
        return Py_None;
    }
    else if (! PyInt_Check(due)) {
        PyErr_SetString(PyExc_RuntimeError, "due date must be int timestamp or None");
        return NULL;
    }
    dt = g_malloc0 (sizeof (ECalComponentDateTime));
    dt->value = g_malloc0 (sizeof (struct icaltimetype));
    (*dt->value) = icaltime_from_timet(PyInt_AsLong(due), TRUE);
    e_cal_component_set_due(calcomponent, dt);

    Py_INCREF(Py_None);
    return Py_None;    
}

%%
override evo_cal_component_get_categories_list noargs
static PyObject *
_wrap_evo_cal_component_get_categories_list(PyGObject *self)
{
    ECalComponent *calcomponent;
    GSList* categories;
    GSList* l;
    PyObject* res;
    int i = 0;

    calcomponent = E_CAL_COMPONENT(self->obj);
    e_cal_component_get_categories_list(calcomponent, &categories);
    if (categories == NULL)
        return PyList_New(0);
    res = PyList_New(g_slist_length(categories));
    for (l = categories; l; l = l->next, i++)
        PyList_SetItem(res, i, PyString_FromString(l->data));
        e_cal_component_free_categories_list(categories);
    return res;
}


%%
override evo_cal_component_set_categories_list args
static PyObject *
_wrap_evo_cal_component_set_categories_list(PyGObject *self, PyObject *args)
{
    ECalComponent *calcomponent;
    GSList* categories = NULL;
    PyObject* list;
    char* item;
    int i;

    // TV-TODO: allow tuple?
    if (!PyArg_ParseTuple(args, "O!:ECalComponent.set_categories_list",
                          &PyList_Type,&list))
        return NULL;

    for (i = PyList_Size(list)-1; i >= 0; i--) {
        item = PyString_AsString(PyList_GetItem(list, i));
        if (item == NULL) {
            g_slist_free(categories);
            return NULL;
        }
        categories = g_slist_prepend(categories, item);
    }

    calcomponent = E_CAL_COMPONENT(self->obj);
    e_cal_component_set_categories_list(calcomponent, categories);
    
    Py_INCREF(Py_None);
    return Py_None;
}

