/*
 *  Copyright (C) 2004 Morten Fjord-Larsen
 *
 *  $Id: app.c,v 1.5 2004/05/03 07:49:36 fjord Exp $
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Library 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.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gnome.h>
#include <glade/glade.h>
#include <gconf/gconf-client.h>

#include "passwordlist.h"
#include "rundialog.h"
#include "preferences.h"

GladeXML *xml;

static GtkWidget *application;
static gchar *password_file;
extern GConfClient *gconf_client;
extern GnomeClient *gnome_client;

static void app_save_window_geometry()
{
    gint           width, height;
    gint           x, y;

    gtk_window_get_size (GTK_WINDOW(application), &width, &height);
    gtk_window_get_position (GTK_WINDOW(application), &x, &y);

    gconf_client_set_int (gconf_client,
	    GCONF_PATH "/ui/main_window_width",
	    width,
	    NULL);

    gconf_client_set_int (gconf_client,
	    GCONF_PATH "/ui/main_window_height",
	    height,
	    NULL);

    gconf_client_set_int (gconf_client,
	    GCONF_PATH "/ui/main_window_position_x",
	    x,
	    NULL);

    gconf_client_set_int (gconf_client,
	    GCONF_PATH "/ui/main_window_position_y",
	    y,
	    NULL);
}

static gboolean app_save_yourself(GnomeClient *client,
                                  gint phase,
			          GnomeSaveStyle arg2,
			          gboolean arg3,
			          GnomeInteractStyle arg4,
			          gboolean arg5,
			          gpointer user_data)
{
    app_save_window_geometry();
    return TRUE;
}


void app_init()
{
    GtkToolbar *toolbar;
    BonoboDockItem *dockitem;
    GtkWidget *treeview;
    gint width, height;
    gint x,y;
    gboolean hidden;

    /* Install 'save-yourself' handler */
    g_signal_connect(gnome_client, 
	             "save-yourself", 
	             G_CALLBACK(app_save_yourself), 
	             NULL);

    xml = glade_xml_new (PACKAGE_DATA_DIR"/main-window.glade", NULL, NULL);
    if (xml == NULL) {
	g_print(PACKAGE_NAME " does not seem to be properly installed.\n");
	exit(1);
    }
    glade_xml_signal_autoconnect(xml);

    toolbar = GTK_TOOLBAR(glade_xml_get_widget(xml, "toolbar1"));
    g_assert(toolbar != NULL);

    dockitem = BONOBO_DOCK_ITEM(glade_xml_get_widget(xml, "bonobodockitem3"));
    g_assert(dockitem != NULL);

    gnome_app_setup_toolbar(toolbar, dockitem);

    application = glade_xml_get_widget(xml, "application");
    gnome_app_enable_layout_config(GNOME_APP(application), TRUE);
    g_assert(application != NULL);

    treeview = glade_xml_get_widget(xml, "app_treeview");
    password_list_setup(treeview);

    /* Set window size */
    width = gconf_client_get_int (gconf_client,
	    GCONF_PATH "/ui/main_window_width",
	    NULL);

    height = gconf_client_get_int (gconf_client,
	    GCONF_PATH "/ui/main_window_height",
	    NULL);

    if (width > 0 && height > 0)
	gtk_window_set_default_size (GTK_WINDOW(application), width, height);

    /* Set window position */
    x = gconf_client_get_int (gconf_client, 
	    GCONF_PATH "/ui/main_window_position_x",
	    NULL);

    y = gconf_client_get_int (gconf_client, 
	    GCONF_PATH "/ui/main_window_position_y", 
	    NULL);

    if (x >= 0 && y >= 0) {
	gtk_window_move (GTK_WINDOW(application), x, y);
    }




    password_file = g_strdup_printf("%s/.gpass/gpass.dat", g_get_home_dir());
    gtk_widget_show(application);
    
    /* set toolbar visibility */
    hidden = gconf_client_get_bool(gconf_client,
	    GCONF_PATH "/ui/toolbar_hidden", 
	    NULL);

    if (hidden) {
	GtkWidget *menuitem;
	GtkWidget *toolbar = glade_xml_get_widget(xml, "bonobodockitem3");
	g_assert(toolbar != NULL);
	gtk_widget_hide(toolbar);

	menuitem = glade_xml_get_widget(xml, "toolbar2");
	g_assert(menuitem != NULL);
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),!hidden);
    }

    /* set statusbar visibility */
    hidden = gconf_client_get_bool(gconf_client,
	    GCONF_PATH "/ui/statusbar_hidden", 
	    NULL);

    if (hidden) {
	GtkWidget *menuitem;
	GtkWidget *statusbar = glade_xml_get_widget(xml, "appbar");
	g_assert(statusbar != NULL);
	gtk_widget_hide(statusbar);

	menuitem = glade_xml_get_widget(xml, "statusbar1");
	g_assert(menuitem != NULL);
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),!hidden);
    }

    password_list_load_password_file(password_file);
}


static void app_close_dialog_display()
{
    run_dialog(PACKAGE_DATA_DIR"/dirty-close-dialog.glade", 
	       "dirty_close_dialog");
}


gboolean app_save_quit() 
{
    if (password_list_is_dirty()) {
	password_list_write();
    } 
    gtk_main_quit();
    return TRUE;
}


gboolean app_quit()
{
    if (password_list_is_dirty()) {
	app_close_dialog_display();
    } else {
	gtk_main_quit();
    }
    app_save_window_geometry();
    return TRUE;
}


void app_start() 
{

}


void app_toggle_statusbar(GtkMenuItem *menuitem, gpointer user_data)
{
    GtkWidget *statusbar;
    gboolean active;
    active = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
    statusbar = glade_xml_get_widget(xml, "appbar");
    g_assert(statusbar != NULL);
    if (active)
	gtk_widget_show(statusbar);
    else
	gtk_widget_hide(statusbar);

    gconf_client_set_bool(gconf_client,
	    GCONF_PATH "/ui/statusbar_hidden",
	    !active,
	    NULL);

}


void app_toggle_toolbar(GtkMenuItem *menuitem, gpointer user_data)
{
    GtkWidget *toolbar;
    gboolean active;
    active = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
    toolbar = glade_xml_get_widget(xml, "bonobodockitem3");
    g_assert(toolbar != NULL);
    if (active)
	gtk_widget_show(toolbar);
    else
	gtk_widget_hide(toolbar);

    gconf_client_set_bool(gconf_client,
	    GCONF_PATH "/ui/toolbar_hidden",
	    !active,
	    NULL);
}


void app_status_say(const gchar *text, ...)
{
    va_list va;
    gchar *str;
    GnomeAppBar *appbar = GNOME_APPBAR(glade_xml_get_widget(xml, "appbar"));
    g_assert(appbar != NULL);

    va_start(va, text);
    str = g_strdup_vprintf(text,va);
    va_end(va);

    gnome_appbar_set_status(appbar, str);

    g_free(str);
}


void app_menu_edit_items_sensitive (gboolean sensitive)
{
    int i;

    static char *menu_edit_items[] = {
	"copy_password",
	"copy_username",
	"copy_username_password",
	"edit",
	"remove",
	"goto_url"
    };

    for (i = 0; i < sizeof(menu_edit_items)/sizeof(char *); i++) {
	GtkWidget *w = glade_xml_get_widget(xml, menu_edit_items[i]);
	g_assert(w != NULL);
	gtk_widget_set_sensitive(w, sensitive); 
    }
}

