/* ====================================================================
 * 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 "ftd_gui_main.h"
#include "ftd_gui_center.h"
#include "ftd_gui_worksheet.h"
#include "ftd_gui_errhandler.h"

void         ftd_gui_center_vrienden_show (GtkWidget *, gpointer);
gboolean     ftd_gui_center_vrienden_open_uri (GtkWidget *, const char *, gpointer);

void
ftd_gui_center_vrienden_new(void)
{
    GtkWidget *label;
    FtdGuiCenterVrienden *vrienden;
    int teller;
    GtkWidget *tabby;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);

    // Hebben we reeds een 'vrienden van FTD' tabblad open staan ?
    if (gui->worksheet->center->vrienden) {
        // Zoek de tab waar 'vrienden van FTD' op vermeld staat en activeer deze
        for (teller = 0; teller < gtk_notebook_get_n_pages(GTK_NOTEBOOK (gui->worksheet->center->widget)); teller++) {
            if (gtk_notebook_get_nth_page(GTK_NOTEBOOK (gui->worksheet->center->widget), teller) == gui->worksheet->center->vrienden->widget) {
                gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), teller);

                return;
            }
        }
    }

    vrienden = (FtdGuiCenterVrienden *) g_malloc0(sizeof(FtdGuiCenterVrienden));
    gui->worksheet->center->vrienden = vrienden;

    /* Initialiseer de inhoud van het schermpie */
    vrienden->browser_data = ftd_gui_browser_new(gui->worksheet->center->widget, FALSE);
    vrienden->widget = ftd_gui_browser_get_widget(vrienden->browser_data);

    /* Voeg een pagina toe aan de notebook, met bijbehorend sluit-knopje */
    label = gtk_label_new ("Vrienden van FTD");
    gtk_widget_show (label);

    tabby = gtk_hbox_new(FALSE, 2);
    vrienden->cmdclose = gtk_button_new();
    gtk_widget_set_size_request(GTK_WIDGET(vrienden->cmdclose), 16, 16);
    gtk_container_add(GTK_CONTAINER(vrienden->cmdclose), gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
    gtk_widget_show_all (vrienden->cmdclose);
    gtk_button_set_relief(GTK_BUTTON(vrienden->cmdclose), GTK_RELIEF_NONE);

    gtk_box_pack_start(GTK_BOX(tabby), label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(tabby), vrienden->cmdclose, FALSE, FALSE, 0);

    gtk_notebook_set_tab_label (GTK_NOTEBOOK (gui->worksheet->center->widget), vrienden->widget, tabby);

    /* Signal handler om de url klikjes vanuit het componentje op te vangen */
    ftd_gui_browser_set_link_clicked_cb(vrienden->browser_data, ftd_gui_center_vrienden_open_uri, NULL);

    /* Signal handler voor het afsluiten knopje */
    g_signal_connect (GTK_WIDGET (vrienden->cmdclose), "clicked", G_CALLBACK(ftd_gui_center_vrienden_sluiten), NULL);

    gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), -1);

    ftd_gui_center_vrienden_show(NULL, NULL);
}

void
ftd_gui_center_vrienden_show (GtkWidget *caller, gpointer data)
{
    FtdGuiCenterVrienden *vrienden;
    char *url;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->vrienden);
    vrienden = gui->worksheet->center->vrienden;

    url = g_strdup_printf("http://www.ftd.nu/watch/index.php?login[name]=%s&login[pass]=%s", ftd_string_value(ftd_config_get()->username), ftd_string_value(ftd_config_get()->password));

    ftd_gui_browser_navigate(vrienden->browser_data, url);

    g_free(url);
}

void
ftd_gui_center_vrienden_sluiten (GtkWidget *caller, gpointer data)
{
    FtdGuiCenterVrienden *vrienden;
    int teller;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->vrienden);
    vrienden = gui->worksheet->center->vrienden;

    ftd_gui_browser_destroy(vrienden->browser_data);
    g_free(vrienden);
    gui->worksheet->center->vrienden = NULL;
}

gboolean
ftd_gui_center_vrienden_open_uri (GtkWidget *embed, const char *uri, gpointer data)
{
    FtdGuiCenterVrienden *vrienden;
    char *begin;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->vrienden);
    vrienden = gui->worksheet->center->vrienden;

    begin = strstr(uri, "ftd://");
    if (begin) {
        char url[16];
        begin += 6;
        snprintf(url, sizeof(url), "#post%s", begin);
        return ftd_gui_center_eventlist_open_uri(NULL, url, NULL);
    }

    return FALSE;
}
