/* Chrysalide - Outil d'analyse de fichiers binaires
 * source.c - sélection du fichier ciblé lors d'une décompilation
 *
 * Copyright (C) 2010-2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 "source.h"


#include <i18n.h>


#include "tbitem-int.h"
#include "../../format/format.h"
#include "../../gtkext/easygtk.h"
#include "../../gtkext/gtkbufferview.h"
#include "../../gtkext/gtksourceview.h"



/* Elément réactif listant les fichiers décompilés (instance) */
struct _GSourceTbItem
{
    GToolbarItem parent;                    /* A laisser en premier        */

};


/* Elément réactif listant les fichiers décompilés (classe) */
struct _GSourceTbItemClass
{
    GToolbarItemClass parent;               /* A laisser en premier        */

};



/* Initialise la classe des éléments réactifs de l'éditeur. */
static void g_source_tbitem_class_init(GSourceTbItemClass *);

/* Initialise une instance d'élément réactif pour l'éditeur. */
static void g_source_tbitem_init(GSourceTbItem *);

/* Supprime toutes les références externes. */
static void g_source_tbitem_dispose(GSourceTbItem *);

/* Procède à la libération totale de la mémoire. */
static void g_source_tbitem_finalize(GSourceTbItem *);

/* Réagit à un changement du binaire courant. */
static void update_source_item_binary(GEditorItem *, GLoadedBinary *);

/* Réagit à un changement de panneau d'affichage courant. */
static void update_source_item_view(GEditorItem *, GtkViewPanel *);

/* Réagit à un changement de sélection de la source courante. */
static void change_selected_source(GtkComboBox *, GSourceTbItem *);



/* Indique le type défini pour une liste de fichiers destinée à une barre d'outils. */
G_DEFINE_TYPE(GSourceTbItem, g_source_tbitem, G_TYPE_TOOLBAR_ITEM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des éléments réactifs de l'éditeur.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_source_tbitem_class_init(GSourceTbItemClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GEditorItemClass *item;                 /* Encore une autre vision     */

    object = G_OBJECT_CLASS(klass);
    item = G_EDITOR_ITEM_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_source_tbitem_dispose;
    object->finalize = (GObjectFinalizeFunc)g_source_tbitem_finalize;

    item->update_binary = update_source_item_binary;
    item->update_view = update_source_item_view;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise une instance d'élément réactif pour l'éditeur.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_source_tbitem_init(GSourceTbItem *item)
{
    GtkWidget *widget;                      /* Composant principal         */
    GtkWidget *hbox;                        /* Support interne             */
    GtkWidget *label;                       /* Etiquette d'introduction    */
    GtkWidget *comboboxentry;               /* Liste de fichiers           */

    widget = GTK_WIDGET(gtk_tool_item_new());
    gtk_widget_show(widget);

    G_EDITOR_ITEM(item)->widget = widget;

    hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
    gtk_widget_show(hbox);
    gtk_container_add(GTK_CONTAINER(widget), hbox);

    label = qck_create_label(G_OBJECT(widget), "label", _(" Source: "));
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

    comboboxentry = qck_create_combobox2(G_OBJECT(widget), "combo",
                                        G_CALLBACK(change_selected_source), item);
    gtk_box_pack_start(GTK_BOX(hbox), comboboxentry, TRUE, TRUE, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_source_tbitem_dispose(GSourceTbItem *item)
{
    G_OBJECT_CLASS(g_source_tbitem_parent_class)->dispose(G_OBJECT(item));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_source_tbitem_finalize(GSourceTbItem *item)
{
    G_OBJECT_CLASS(g_source_tbitem_parent_class)->finalize(G_OBJECT(item));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref = espace de référencement global.                        *
*                                                                             *
*  Description : Crée un élément réactif présentant des source de binaire.  *
*                                                                             *
*  Retour      : Adresse de la structure d'encadrement mise en place.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GEditorItem *create_source_tb_item(GObject *ref)
{
    GSourceTbItem *result;                /* Structure à retourner       */

    result = g_object_new(G_TYPE_SOURCE_TBITEM, NULL);

    return g_toolbar_item_setup(G_TOOLBAR_ITEM(result), ref, "source", _("Source files"));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item   = élément réactif sollicité.                          *
*                binary = binaire chargé nouvellement affiché.                *
*                                                                             *
*  Description : Réagit à un changement du binaire courant.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void update_source_item_binary(GEditorItem *item, GLoadedBinary *binary)
{
    GtkComboBoxText *combo;                 /* Liste de sélection          */
    GtkTreeModel *model;                    /* Gestionnaire d'éléments     */
    GtkTreeIter iter;                       /* Boucle de parcours #2       */
    GExeFormat *format;                     /* Format d'exécutable associé */
    size_t count;                           /* Quantité de fichiers        */
    size_t defsrc;                          /* Fichier source principal    */
    const char * const *files;              /* Liste de fichiers source    */
    size_t i;                               /* Boucle de parcours #2       */

    combo = GTK_COMBO_BOX_TEXT(g_object_get_data(G_OBJECT(item->widget), "combo"));

    /* Réinitialisation */

    g_signal_handlers_block_by_func(combo, G_CALLBACK(change_selected_source), item);

    /* FIXME : 3.0 */
    //gtk_combo_box_text_remove_all(combo);

    model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));

    while (gtk_tree_model_get_iter_first(model, &iter))
        gtk_combo_box_text_remove(combo, 0);

    /* Inscriptions */

    format = g_loaded_binary_get_format(binary);
    files = g_binary_format_get_source_files(G_BIN_FORMAT(format), &count, &defsrc);

    for (i = 0; i < count; i++)
        gtk_combo_box_text_append_text(combo, files[i]);

    /* Réactivation */

    g_signal_handlers_unblock_by_func(combo, G_CALLBACK(change_selected_source), item);

    //gtk_combo_box_set_active(combo, defsrc);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item = élément réactif sollicité.                            *
*                view = nouveau panneau d'affichage actif.                    *
*                                                                             *
*  Description : Réagit à un changement de panneau d'affichage courant.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void update_source_item_view(GEditorItem *item, GtkViewPanel *view)
{
    gtk_widget_set_sensitive(item->widget, GTK_IS_SOURCE_VIEW(view));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : widget = composant GTK en cause.                             *
*                item   = élément réactif associé.                            *
*                                                                             *
*  Description : Réagit à un changement de sélection de la source courante.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void change_selected_source(GtkComboBox *widget, GSourceTbItem *item)
{
#if 0
    gint index;                             /* Nouvelle source ciblée      */
    GLoadedBinary *binary;                  /* Binaire chargé courant      */
    GCodeBuffer *buffer;                    /* Nouveau tampon à présenter  */
    GtkBufferView *view;                    /* Afficheur de tampons        */

    index = gtk_combo_box_get_active(widget);

    binary = G_LOADED_BINARY(g_object_get_data(ref, "current_binary"));
    buffer = g_loaded_binary_get_decompiled_buffer(binary, index);

    view = GTK_BUFFER_VIEW(g_object_get_data(ref, /*"current_view"*/)); /* TODO : utiliser g_editor_item_get_current_view() */
    if (GTK_IS_BUFFER_VIEW(view))
        gtk_buffer_view_attach_buffer(view, buffer,
                                      g_loaded_binary_display_decomp_lines(binary), NULL);
#endif
}