/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gmodule.h>

#include "ftd4linux.h"
#include "ftd_gui_browser.h"
#include "ftd_gui_config.h"
#include "ftd_gui_errhandler.h"

static FtdGuiBrowserData *(*browser_new) (GtkWidget *, gboolean) = NULL;
static void (*browser_navigate) (FtdGuiBrowserData *, const char *) = NULL;
static void (*browser_destroy) (FtdGuiBrowserData *) = NULL;
static void (*browser_set_element_attribute) (FtdGuiBrowserData *, const char *, const char *, const char *) = NULL;
static void (*browser_set_load_finished_cb) (FtdGuiBrowserData *, void (*) (GtkWidget *, gpointer), gpointer) = NULL;
static void (*browser_set_link_clicked_cb) (FtdGuiBrowserData *, gboolean (*) (GtkWidget *, const char *, gpointer), gpointer) = NULL;
static GtkWidget *(*browser_get_widget) (FtdGuiBrowserData *) = NULL;

static gboolean initialized = FALSE;

static void
init_plugin(const char *filename)
{
    GModule *plugin = g_module_open(filename, G_MODULE_BIND_LOCAL);
    if (!plugin) {
        g_error("Openen van HTML renderer plugin mislukt: %s", g_module_error());
    }

    g_assert(g_module_symbol(plugin, "ftd_gui_browser_new", (gpointer *) &browser_new));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_navigate", (gpointer *) &browser_navigate));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_destroy", (gpointer *) &browser_destroy));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_set_element_attribute", (gpointer *) &browser_set_element_attribute));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_set_load_finished_cb", (gpointer *) &browser_set_load_finished_cb));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_set_link_clicked_cb", (gpointer *) &browser_set_link_clicked_cb));
    g_assert(g_module_symbol(plugin, "ftd_gui_browser_get_widget", (gpointer *) &browser_get_widget));
}

FtdGuiBrowserData *
ftd_gui_browser_new(GtkWidget *parent, gboolean is_paned)
{
    if (!initialized) {
        FtdList *plugins = ftd_gui_config_get_html_renderers();
        FtdList *list;

        list = plugins;
        while (list) {
            FtdGuiConfigHTMLRenderer *item = ftd_list_value(list);

            if (strcmp(item->name, ftd_string_value(ftd_config_get()->htmlRenderer)) == 0) {
                init_plugin(item->filename);
                break;
            }

            list = ftd_list_next(list);
        }

        if (browser_new == NULL) {
            FtdGuiConfigHTMLRenderer *item = ftd_list_value(plugins);
            char *msg;

            if (ftd_string_length(ftd_config_get()->htmlRenderer) > 0) {
                msg = g_strdup_printf("Opgegeven HTML renderer '%s' kon niet gevonden worden; HTML renderer '%s' wordt nu geladen", ftd_string_value(ftd_config_get()->htmlRenderer), item->name);

                ftd_gui_dialog_show(NULL, msg, NULL, FALSE);

                g_free(msg);
            }

            if (ftd_config_get()->htmlRenderer) {
                ftd_string_free(ftd_config_get()->htmlRenderer);
            }
            ftd_config_get()->htmlRenderer = ftd_string_new(item->name);
            ftd_config_save();

            init_plugin(item->filename);
        }

        ftd_gui_config_free_html_renderers(plugins);

        initialized = TRUE;
    }

    return browser_new(parent, is_paned);
}

void
ftd_gui_browser_navigate(FtdGuiBrowserData *browser_data, const char *url)
{
    g_assert(initialized);

    browser_navigate(browser_data, url);
}

void
ftd_gui_browser_destroy(FtdGuiBrowserData *browser_data)
{
    g_assert(initialized);

    browser_destroy(browser_data);
}

void
ftd_gui_browser_set_element_attribute(FtdGuiBrowserData *browser_data, const char *elementId, const char *attribute, const char *value)
{
    g_assert(initialized);

    browser_set_element_attribute(browser_data, elementId, attribute, value);
}

void
ftd_gui_browser_set_load_finished_cb(FtdGuiBrowserData *browser_data, void (*cb) (GtkWidget *caller, gpointer data), gpointer data)
{
    g_assert(initialized);

    browser_set_load_finished_cb(browser_data, cb, data);
}

void
ftd_gui_browser_set_link_clicked_cb(FtdGuiBrowserData *browser_data, gboolean (*cb) (GtkWidget *caller, const char *url, gpointer data), gpointer data)
{
    g_assert(initialized);

    browser_set_link_clicked_cb(browser_data, cb, data);
}

GtkWidget *
ftd_gui_browser_get_widget(FtdGuiBrowserData *browser_data)
{
    g_assert(initialized);

    return browser_get_widget(browser_data);
}

