/* ====================================================================
 * 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>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#else
#include <getopt.h>
#include <sys/time.h>
#include <unistd.h>
#endif /* WIN32 */
#ifdef HAVE_GNOME
#include <libgnome/libgnome.h>
#include <libgnomeui/libgnomeui.h>
#endif

#include "../../config.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "ftd_gui_main.h"
#include "ftd_gui_config.h"
#include "ftd_gui_errhandler.h"
#include "ftd_gui_xslt.h"
#include "ftd_gui_parse.h"

#ifdef __cplusplus
}
#endif /* __cplusplus */

gboolean
main_init(gpointer data)
{
    // Moet nog netter kunnen
    if (!ftd_group_list_ready()) {
        char *errmsg;
        ftd_group_fetch(&errmsg);
        ftd_gui_status_push();
    }

    ftd_gui_parse_init();

    ftd_gui_main_new();
    gtk_widget_show(gui->window);
    ftd_gui_status_set_parent(gui->window);
    ftd_gui_status_push();

    if (ftd_config_get()->updatenBijOpstarten) {
        ftd_gui_callback_updaten(NULL, (gpointer) 1);
    } else {
        ftd_gui_callback_nieuw(NULL, (gpointer) 1);
    }

    return FALSE;
}

int main(int argc, char *argv[])
{
    FtdList *tasks = NULL;
    FtdStatusTask *task;
    char *errmsg = NULL;
    gboolean dbus_init_success;

#ifdef HAVE_GNOME
    GnomeProgram *my_app;

    // Met deze instructie wordt bug buddy gelanceerd indien er een sigsegv optreedt
    my_app = gnome_program_init("OpenFTD", VERSION, LIBGNOMEUI_MODULE,
                                argc, argv, GNOME_PARAM_POPT_TABLE, NULL,
                                GNOME_PARAM_NONE);
#endif

    if (!g_thread_supported ()) g_thread_init (NULL);
    gtk_set_locale ();
    gtk_init (&argc, &argv);
    g_set_prgname("OpenFTD");

    if (!ftd_app_core_init(g_getenv("FTD_DATA_DIR"), NULL, g_getenv("FTD_LIB_DIR"),  "GTK-GUI", TRUE, &errmsg, &dbus_init_success)) {
        ftd_gui_dialog_show(NULL, "ftd_app_core_init() mislukt!", NULL, TRUE);
        ftd_app_core_cleanup();
        return 2;
    }

    if (ftd_app_core_is_first_time()) {
        /* Dit is de eerste keer dat we opgestart worden.
           Toon eerst het configvenster waar de gebruiker
           een FTD Username en wachtwoord moet instellen */
///#ifdef NO_EXTERNAL_BROWSER
#if 0
        ftd_gui_config_new(NULL, TRUE, FALSE);
#else
        ftd_gui_config_new(NULL, TRUE, TRUE);
#endif

        gtk_main();
    }

    /* Voer de controle nogmaals uit. Indien de gebruiker
       geen of geen geldige gegevens heeft ingevoerd heeft
       het totaal geen zin om het programma verder te starten. */
    if (ftd_app_core_is_first_time()) {
        ftd_gui_dialog_show(NULL, "First time setup mislukt!", NULL, TRUE);
        ftd_app_core_cleanup();
        return 3;
    }

#ifdef HAVE_DBUS
#ifndef DARWIN
    if (!dbus_init_success) {
        char *msg = g_strdup_printf("%sFirefox integratie is uitgeschakeld", errmsg);
        ftd_gui_dialog_show(NULL, msg, NULL, FALSE);
        g_free(msg);
        g_free(errmsg);
    }
#endif
#endif

///#ifndef NO_EXTERNAL_BROWSER
#if 1
    ftd_gui_xslt_init();
#endif

    task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
    task->tekst = ftd_string_new("Aanmaken GUI");
    tasks = ftd_list_append(tasks, task);

    if (ftd_config_get()->updatenBijOpstarten) {
        task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
        task->tekst = ftd_string_new("Ophalen nieuwe FTD gegevens");
        tasks = ftd_list_append(tasks, task);

        task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
        task->tekst = ftd_string_new("Samenvoegen ontvangen gegevens");
        tasks = ftd_list_append(tasks, task);
    }

    task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
    task->tekst = ftd_string_new("Samenstellen van lijst");
    tasks = ftd_list_append(tasks, task);

    task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
    task->tekst = ftd_string_new("Laden van lijst");
    tasks = ftd_list_append(tasks, task);

    ftd_gui_status_new(tasks, NULL);

    g_idle_add(main_init, NULL);

    gtk_main();

///#ifndef NO_EXTERNAL_BROWSER
#if 1
    ftd_gui_xslt_free();
#endif

    ftd_gui_pixbuf_destroy();
    ftd_gui_parse_free();

    ftd_app_core_cleanup();

    return 0;
}

#ifdef WIN32
static HINSTANCE instance = NULL;

HINSTANCE main_getinstance(void)
{
    return instance;
}

int APIENTRY WinMain( HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow)
{
    GMemVTable vtable;

    memset(&vtable, 0, sizeof(vtable));
    vtable.malloc = malloc;
    vtable.realloc = realloc;
    vtable.free = free;
    g_mem_set_vtable(&vtable);

    instance = hInstance;
    return main( __argc, __argv );
}
#endif
