/* ====================================================================
 * 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 "ftd_gui_main.h"
#include "ftd_gui_menu.h"
#include "ftd_gui_worksheet.h"
#include "ftd_gui_pixbuf.h"
#include "ftd_gui_center.h"
#include "ftd_gui_sidebar.h"
#include "ftd_gui_toolbar.h"
#include "ftd_gui_mail.h"

FtdGuiMain *gui = NULL;

static void ftd_gui_main_log_func (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data);

void
ftd_gui_main_new(void)
{
    GtkWidget *hbox;
#ifdef HAVE_DBUS
    OpenFTDDBusObject *obj;
#endif

    g_assert(gui == NULL);

    g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, ftd_gui_main_log_func, NULL);

    gui = (FtdGuiMain *) g_malloc0(sizeof(FtdGuiMain));

#ifdef HAVE_DBUS
    obj = ftd_dbus_get_object();
    if (obj) {
        g_signal_connect(G_OBJECT(obj), "bring_to_front", G_CALLBACK(ftd_gui_callback_bring_to_front), NULL);
        g_signal_connect(G_OBJECT(obj), "open_posting", G_CALLBACK(ftd_gui_callback_open_posting), NULL);
    }
#endif

    gui->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (gui->window, 850, 600);
    gtk_window_set_title (GTK_WINDOW (gui->window), "OpenFTD");
    gtk_window_set_position (GTK_WINDOW (gui->window), GTK_WIN_POS_CENTER);
    if (ftd_config_get()->startMaximized) {
        gtk_window_maximize (GTK_WINDOW (gui->window));
    }
    g_signal_connect(G_OBJECT(gui->window), "destroy",
                     G_CALLBACK(ftd_gui_callback_afsluiten), NULL);
    g_signal_connect(G_OBJECT(gui->window), "window_state_event",
                           G_CALLBACK(ftd_gui_main_window_state_changed), NULL);

    ftd_gui_pixbuf_init_pixbufs();

    gtk_window_set_icon(GTK_WINDOW(gui->window), ftd_gui_main_get_pixbuf("icon"));

    gui->vbox = gtk_vbox_new (FALSE, 0);
    gtk_widget_show(gui->vbox);
    gtk_container_add(GTK_CONTAINER(gui->window), gui->vbox);

    ftd_gui_menu_new();
    gtk_box_pack_start(GTK_BOX(gui->vbox), gui->menu->widget, FALSE, FALSE, 0);

    ftd_gui_toolbar_new();

    gtk_box_pack_start(GTK_BOX(gui->vbox), gui->toolbar->widget, FALSE, FALSE, 0);

#ifdef WIN32
    // Is nodig voor Internet Explorer/Mozilla integratie aangezien deze een geldige HWND nodig hebben
    gtk_widget_show(gui->window);
#endif

    ftd_gui_worksheet_new();

    hbox = gtk_hbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(gui->vbox), hbox, FALSE, FALSE, 3);
    gtk_widget_show(hbox);

    gui->progressbar = gtk_progress_bar_new();
    gtk_box_pack_start(GTK_BOX(hbox), gui->progressbar, FALSE, FALSE, 3);
    gtk_widget_show(gui->progressbar);

    gui->statusbar = gtk_statusbar_new();
    gtk_box_pack_start(GTK_BOX(hbox), gui->statusbar, TRUE, TRUE, 3);
    gtk_statusbar_push(GTK_STATUSBAR(gui->statusbar), 0, "Welkom bij OpenFTD!");
    gtk_widget_show(gui->statusbar);

    gtk_widget_grab_focus(gui->worksheet->center->widget);

#ifndef DARWIN
    if (ftd_config_get()->trayIcon) {
        ftd_gui_tray_create(NULL);
    }
#endif

    // Laad het FTD Mail venster in de achtergrond, maar toon het nog niet
    ftd_gui_mail_new();
}

gboolean
ftd_gui_main_window_state_changed(GtkWidget *caller, GdkEvent *event, gpointer data)
{
#ifndef DARWIN
    g_assert(gui);

    if (!ftd_config_get()->trayIcon || !gui->trayIconActivated) {
        return FALSE;
    }

    if (    (event->window_state.new_window_state & GDK_WINDOW_STATE_ICONIFIED) == GDK_WINDOW_STATE_ICONIFIED &&
            (event->window_state.changed_mask & GDK_WINDOW_STATE_ICONIFIED) == GDK_WINDOW_STATE_ICONIFIED) {
        gtk_window_set_skip_taskbar_hint(GTK_WINDOW(gui->window), TRUE);
    }

#endif

    return FALSE;
}

static void
ftd_gui_main_log_func (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
    ftd_gui_dialog_show(log_domain, message, gui->window, TRUE);
}
