/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  Copyright (C) 2003-2004 Hiroyuki Ikezoe
 *  Copyright (C) 2003 Takuro Ashie <ashie@homa.ne.jp>
 *
 *  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, 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 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.
 *
 *  $Id: kz-bookmark-base-menu-item.c 2251 2005-07-18 23:18:34Z ikezoe $
 */

#include "kz-bookmark-base-menu-item.h"

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "gobject-utils.h"
#include "utils.h"

enum {
	PROP_0,
	PROP_BOOKMARK,
	PROP_KZ_WINDOW
};

enum {
	TARGET_KAZEHAKASE_BOOKMARKS,
	TARGET_NETSCAPE_URL,
	TARGET_TEXT_URI_LIST
};

static GtkTargetEntry dnd_types[] = {
	{"_KAZEHAKASE_BOOKMARKS", 0, TARGET_KAZEHAKASE_BOOKMARKS},
};

static GtkTargetEntry url_drag_types [] = 
{
	{"_KAZEHAKASE_BOOKMARKS", 0, TARGET_KAZEHAKASE_BOOKMARKS},
        { "_NETSCAPE_URL",        0, TARGET_NETSCAPE_URL},
	{ "text/uri-list",        0, TARGET_TEXT_URI_LIST}
};

static const gint dnd_types_num = G_N_ELEMENTS(dnd_types);
static const gint n_url_drag_types = G_N_ELEMENTS(url_drag_types);

static void     kz_bookmark_base_menu_item_class_init       (KzBookmarkBaseMenuItemClass *klass);
static void     kz_bookmark_base_menu_item_init             (KzBookmarkBaseMenuItem *menu);

/* object class methods */
static GObject* kz_bookmark_base_menu_item_constructor      (GType           type,
						             guint           n_props,
						             GObjectConstructParam *props);
static void     kz_bookmark_base_menu_item_dispose          (GObject        *object);
static void     kz_bookmark_base_menu_item_set_property     (GObject        *object,
							     guint           prop_id,
							     const GValue   *value,
							     GParamSpec     *pspec);
static void     kz_bookmark_base_menu_item_get_property     (GObject        *object,
							     guint           prop_id,
							     GValue         *value,
							     GParamSpec     *pspec);

static void 	connect_bookmark_signals                    (KzBookmarkBaseMenuItem *item,
	       						     KzBookmark             *bookmark);
static void 	disconnect_bookmark_signals                 (KzBookmarkBaseMenuItem *item,
	       						     KzBookmark             *bookmark);

static GtkImageMenuItemClass *parent_class = NULL;


KZ_OBJECT_GET_TYPE(kz_bookmark_base_menu_item, "KzBookmarkBaseMenuItem", KzBookmarkBaseMenuItem,
		   kz_bookmark_base_menu_item_class_init, kz_bookmark_base_menu_item_init,
		   GTK_TYPE_IMAGE_MENU_ITEM)

static void
kz_bookmark_base_menu_item_class_init (KzBookmarkBaseMenuItemClass *klass)
{
	GObjectClass *gobject_class;

	parent_class = g_type_class_peek_parent (klass);

	gobject_class   = (GObjectClass *) klass;

	/* GObject signals */
	gobject_class->dispose      = kz_bookmark_base_menu_item_dispose;
	gobject_class->set_property = kz_bookmark_base_menu_item_set_property;
	gobject_class->get_property = kz_bookmark_base_menu_item_get_property;
	gobject_class->constructor  = kz_bookmark_base_menu_item_constructor;
	
	g_object_class_install_property
		(gobject_class,
		 PROP_BOOKMARK,
		 g_param_spec_object ("bookmark",
				      _("Bookmark"),
				      _("The boomark to show"),
				      KZ_TYPE_BOOKMARK,
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property
		(gobject_class,
		 PROP_KZ_WINDOW,
		 g_param_spec_object ("kz-window",
				      _("KzWindow"),
				      _("The KzWindow to add a home button"),
				      KZ_TYPE_WINDOW,
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT_ONLY));
}


static void
kz_bookmark_base_menu_item_init (KzBookmarkBaseMenuItem *menu)
{
	menu->kz            = NULL;
	menu->bookmark      = NULL;
	menu->tooltips      = NULL;
}


static GObject*
kz_bookmark_base_menu_item_constructor (GType                  type,
			                guint                  n_props,
			                GObjectConstructParam *props)
{
	KzBookmarkBaseMenuItem *item;
	GObject *object;
	const gchar *desc;
	gchar *tooltip_text = NULL;
	KzBookmark *bookmark;
	GObjectClass *klass = G_OBJECT_CLASS(parent_class);

	object = klass->constructor(type, n_props, props);
	item = KZ_BOOKMARK_BASE_MENU_ITEM(object);
	bookmark = item->bookmark;
	
	/* tooltip */
	desc = kz_bookmark_get_description(bookmark);
	if (desc)
		tooltip_text = remove_tag(desc, strlen(desc));
	
	item->tooltips = gtk_tooltips_new();
	if (tooltip_text)
	{
		gtk_tooltips_set_tip(item->tooltips, GTK_WIDGET(item), tooltip_text, NULL);
		g_free(tooltip_text);
	}

	connect_bookmark_signals(item, bookmark);

	return object;
}

static void
kz_bookmark_base_menu_item_dispose (GObject *object)
{
	KzBookmarkBaseMenuItem *menu;

	if (G_OBJECT_CLASS (parent_class)->dispose)
		G_OBJECT_CLASS (parent_class)->dispose(object);

	menu = KZ_BOOKMARK_BASE_MENU_ITEM(object);

	if (menu->bookmark)
	{
		disconnect_bookmark_signals(menu, menu->bookmark);
		g_object_unref(menu->bookmark);
	}

	if (menu->tooltips)
		gtk_object_sink(GTK_OBJECT(menu->tooltips));

	menu->tooltips = NULL;
	
	if (menu->kz)
		g_object_unref(menu->kz);

	menu->kz       = NULL;
	menu->bookmark = NULL;
	menu->tooltips = NULL;
}


static void
kz_bookmark_base_menu_item_set_property (GObject         *object,
			       guint            prop_id,
			       const GValue    *value,
			       GParamSpec      *pspec)
{
	KzBookmarkBaseMenuItem *menu = KZ_BOOKMARK_BASE_MENU_ITEM(object);
  
	switch (prop_id)
	{
	case PROP_BOOKMARK:
		menu->bookmark = g_object_ref(g_value_get_object(value));
		break;
	case PROP_KZ_WINDOW:
		menu->kz = g_object_ref(g_value_get_object(value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}


static void
kz_bookmark_base_menu_item_get_property (GObject         *object,
			       guint            prop_id,
			       GValue          *value,
			       GParamSpec      *pspec)
{
	KzBookmarkBaseMenuItem *item = KZ_BOOKMARK_BASE_MENU_ITEM(object);

	switch (prop_id)
	{
	case PROP_BOOKMARK:
		g_value_set_object(value, item->bookmark);
		break;
	case PROP_KZ_WINDOW:
		g_value_set_object(value, item->kz);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}


static void
cb_notify_title (GObject *object, GParamSpec *pspec,
		 GtkWidget *widget)
{
	const gchar *title;

	title = kz_bookmark_get_title(KZ_BOOKMARK(object));

	if (GTK_IS_LABEL(GTK_BIN(widget)->child))
		gtk_label_set_text(GTK_LABEL(GTK_BIN(widget)->child), title);
}


static void
cb_notify_desc (GObject *object, GParamSpec *pspec,
		GtkWidget *widget)
{
	GtkTooltips *tooltip;
	const gchar *desc;
	gchar *tooltip_text = NULL;
	
	desc = kz_bookmark_get_description(KZ_BOOKMARK(object));
	if (desc)
		tooltip_text = remove_tag(desc, strlen(desc));
	
	tooltip = KZ_BOOKMARK_BASE_MENU_ITEM(widget)->tooltips;
	if (GTK_IS_TOOLTIPS(tooltip) && tooltip_text)
	{
		gtk_tooltips_set_tip(tooltip, widget, tooltip_text, NULL);
		g_free(tooltip_text);
	}
}


static void
connect_bookmark_signals (KzBookmarkBaseMenuItem *item, KzBookmark *bookmark)
{
	g_signal_connect(bookmark, "notify::title",
			 G_CALLBACK(cb_notify_title),
			 item);
	g_signal_connect(bookmark, "notify::description",
			 G_CALLBACK(cb_notify_desc),
			 item);
}

static void
disconnect_bookmark_signals (KzBookmarkBaseMenuItem *item, KzBookmark *bookmark)
{
	g_signal_handlers_disconnect_by_func(bookmark,
					     G_CALLBACK(cb_notify_title),
					     item);
	g_signal_handlers_disconnect_by_func(bookmark,
					     G_CALLBACK(cb_notify_desc),
					     item);
}

