/* Chrysalide - Outil d'analyse de fichiers binaires
 * comment.c - équivalent Python du fichier "analysis/db/items/comment.c"
 *
 * Copyright (C) 2014-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide 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 General Public License for more details.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "comment.h"


#include <malloc.h>
#include <pygobject.h>


#include <i18n.h>
#include <analysis/db/items/comment.h>


#include "../item.h"
#include "../../../access.h"
#include "../../../helpers.h"
#include "../../../arch/vmpa.h"



/* Crée un nouvel objet Python de type 'DbComment'. */
static PyObject *py_db_comment_new(PyTypeObject *, PyObject *, PyObject *);

/* Fournit le commentaire associé à un commentaire. */
static PyObject *py_db_comment_get_text(PyObject *, void *);

/* Définit le commentaire associé à un commentaire. */
static int py_db_comment_set_text(PyObject *, PyObject *, void *);



/******************************************************************************
*                                                                             *
*  Paramètres  : type = type de l'objet à instancier.                         *
*                args = arguments fournis à l'appel.                          *
*                kwds = arguments de type key=val fournis.                    *
*                                                                             *
*  Description : Crée un nouvel objet Python de type 'DbComment'.             *
*                                                                             *
*  Retour      : Instance Python mise en place.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_db_comment_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *result;                       /* Instance à retourner        */
    PyObject *py_vmpa;                      /* Localisation version Python */
    const char *text;                       /* Texte à associer            */
    int is_volatile;                        /* Conservation en mémoire     */
    int ret;                                /* Bilan de lecture des args.  */
    vmpa2t *addr;                           /* Localisation version C      */
    GDbComment *comment;                    /* Version GLib du commentaire */

    ret = PyArg_ParseTuple(args, "Osp", &py_vmpa, &text, &is_volatile);
    if (!ret) Py_RETURN_NONE;

    ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type());
    if (!ret) return NULL;

    addr = get_internal_vmpa(py_vmpa);
    if (py_vmpa == NULL) Py_RETURN_NONE;

    return NULL;/* FIXME */
    //comment = g_db_comment_new(addr, text, is_volatile);

    result = pygobject_new(G_OBJECT(comment));
    g_object_unref(comment);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Fournit le commentaire associé à un commentaire.             *
*                                                                             *
*  Retour      : Texte manipulable en Python.                                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static PyObject *py_db_comment_get_text(PyObject *self, void *closure)
{
    PyObject *result;                       /* Résultat à retourner        */
    GDbComment *comment;                    /* Commentaire à consulter     */
    char *text;                             /* Contenu textuel associé     */

    comment = G_DB_COMMENT(pygobject_get(self));
    text = g_db_comment_get_text(comment);

    if (text == NULL)
    {
        result = Py_None;
        Py_INCREF(result);
    }
    else
    {
        result = PyUnicode_FromString(text);
        free(text);
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : self    = objet Python concerné par l'appel.                 *
*                value   = valeur fournie à intégrer ou prendre en compte.    *
*                closure = non utilisé ici.                                   *
*                                                                             *
*  Description : Définit le commentaire associé à un commentaire.             *
*                                                                             *
*  Retour      : Bilan de l'opération pour Python.                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int py_db_comment_set_text(PyObject *self, PyObject *value, void *closure)
{
    GDbComment *comment;                    /* Commentaire à consulter     */

    if (!PyUnicode_Check(value))
    {
        PyErr_SetString(PyExc_TypeError, _("The attribute value must be a string."));
        return -1;
    }

    comment = G_DB_COMMENT(pygobject_get(self));
    //g_db_comment_set_text(comment, PyUnicode_DATA(value));

    return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Fournit un accès à une définition de type à diffuser.        *
*                                                                             *
*  Retour      : Définition d'objet pour Python.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

PyTypeObject *get_python_db_comment_type(void)
{
    static PyMethodDef py_db_comment_methods[] = {
        { NULL }
    };

    static PyGetSetDef py_db_comment_getseters[] = {
        {
            "text", py_db_comment_get_text, py_db_comment_set_text,
            "Give access to the content of a given comment.", NULL
        },
        { NULL }
    };

    static PyTypeObject py_db_comment_type = {

        PyVarObject_HEAD_INIT(NULL, 0)

        .tp_name        = "pychrysalide.analysis.db.items.DbComment",
        .tp_basicsize   = sizeof(PyGObject),

        .tp_flags       = Py_TPFLAGS_DEFAULT,

        .tp_doc         = "PyChrysalide comment for edited binary",

        .tp_methods     = py_db_comment_methods,
        .tp_getset      = py_db_comment_getseters,
        .tp_new         = py_db_comment_new

    };

    return &py_db_comment_type;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : module = module dont la définition est à compléter.          *
*                                                                             *
*  Description : Prend en charge l'objet 'pychrysalide....db.items.DbComment'.*
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool ensure_python_db_comment_is_registered(void)
{
    PyTypeObject *type;                     /* Type Python 'DbComment'     */
    PyObject *module;                       /* Module à recompléter        */
    PyObject *dict;                         /* Dictionnaire du module      */

    type = get_python_db_comment_type();

    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
    {
        module = get_access_to_python_module("pychrysalide.analysis.db.items");

        dict = PyModule_GetDict(module);

        if (!ensure_python_db_item_is_registered())
            return false;

        if (!register_class_for_pygobject(dict, G_TYPE_DB_COMMENT, type, get_python_db_item_type()))
            return false;

    }

    return true;

}