/*
 * Nautilus extensions: Submenu test case
 */

/* -- beggining of nautilus-test.h -- */

#ifndef NAUTILUS_TEST_H
#define NAUTILUS_TEST_H

#include <glib-object.h>

G_BEGIN_DECLS

#define NAUTILUS_TYPE_TEST  (nautilus_test_get_type ())
#define NAUTILUS_TEST(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), NAUTILUS_TYPE_TEST, NautilusTest))
#define NAUTILUS_IS_TEST(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), NAUTILUS_TYPE_TEST))

typedef struct _NautilusTest      NautilusTest;
typedef struct _NautilusTestClass NautilusTestClass;

struct _NautilusTest {
	GObject __parent;
};

struct _NautilusTestClass {
	GObjectClass __parent;
};

GType nautilus_test_get_type      (void);
void  nautilus_test_register_type (GTypeModule *module);

G_END_DECLS

#endif /* NAUTILUS_TEST_H */
/* -- end nautilus-test.h -- */


/* -- Module initialization -- */
#include <libnautilus-extension/nautilus-extension-types.h>

void
nautilus_module_initialize (GTypeModule*module)
{
	nautilus_test_register_type (module);
}


void
nautilus_module_shutdown (void)
{
}


void 
nautilus_module_list_types (const GType **types,
			    int          *num_types)
{
	static GType type_list[1];
	
	type_list[0] = NAUTILUS_TYPE_TEST;
	*types = type_list;

	*num_types = 1;
}
/* -- End Module initialization -- */


/* -- The extension itself -- nautilus-test.c -- */

#include <libnautilus-extension/nautilus-file-info.h>
#include <libnautilus-extension/nautilus-menu-provider.h>

static GObjectClass *parent_class;

static void
test_callback (NautilusMenuItem *item,
	      gpointer          user_data)
{
	g_print("my_callback\n");
}	 

static GList *
nautilus_test_get_file_items (NautilusMenuProvider *provider,
			      GtkWidget            *window,
			      GList                *files)
{
	GList    *items = NULL;
	NautilusMenuItem *item, *item_main, *item_work;
	NautilusMenu     *submenu;

	if (files == NULL)
		return NULL;

	/* This menu is shown and it works as expected */
	item_work = nautilus_menu_item_new ("NautilusTest::fileactionwork",
					   "File action work",
					   "This menu is activated...",
					   NULL);

	g_signal_connect (item_work, "activate", G_CALLBACK (test_callback), provider);

	/* This menu is shown in a submenu and it doesn't work */
	item = nautilus_menu_item_new ("NautilusTest::fileaction",
					   "File action...",
					   "This menu should be activated...",
					   NULL);

	g_signal_connect (item, "activate", G_CALLBACK (test_callback), provider);

	/* This menu works as expected */
	item_main = nautilus_menu_item_new ("NautilusTest::filesubmenu",
					   "Test file submenu...",
					   "This is a submenu from get_file_items",
					   NULL);

	submenu = nautilus_menu_new();
	nautilus_menu_item_set_submenu(item_main, submenu);

	nautilus_menu_append_item(submenu, item);

	items = g_list_append (items, item_main);
	items = g_list_append (items, item_work);

	return items;
}

static GList *
nautilus_test_get_background_items (NautilusMenuProvider *provider,
			      GtkWidget            *window,
			      NautilusFileInfo     *file)
{
	GList    *items = NULL;
	NautilusMenuItem *item, *item_main, *item_work;
	NautilusMenu     *submenu;

	/* This menu is shown and it works as expected */
	item_work = nautilus_menu_item_new ("NautilusTest::fileactionwork",
					   "File action work",
					   "This menu is activated...",
					   NULL);

	g_signal_connect (item_work, "activate", G_CALLBACK (test_callback), provider);

	/* This menu is never shown */
	item = nautilus_menu_item_new ("NautilusTest::backgroundaction",
					   "Background action...",
					   "This menu should be activated...",
					   NULL);

	g_signal_connect (item, "activate", G_CALLBACK (test_callback), provider);

	/* This menu is shown but it doesn't work as expected */
	item_main = nautilus_menu_item_new ("NautilusTest::backgroundsubmenu",
					   "Test background submenu...",
					   "This is a submenu from get_background_items",
					   NULL);

	submenu = nautilus_menu_new();
	nautilus_menu_item_set_submenu(item_main, submenu);

	nautilus_menu_append_item(submenu, item);

	items = g_list_append (items, item_main);
	items = g_list_append (items, item_work);

	return items;
}

static void 
nautilus_test_menu_provider_iface_init (NautilusMenuProviderIface *iface)
{
	iface->get_file_items = nautilus_test_get_file_items;
	iface->get_background_items = nautilus_test_get_background_items;
}


static void 
nautilus_test_instance_init (NautilusTest *test)
{
}


static void
nautilus_test_class_init (NautilusTestClass *class)
{
	parent_class = g_type_class_peek_parent (class);
}


static GType test_type = 0;


GType
nautilus_test_get_type (void) 
{
	return test_type;
}


void
nautilus_test_register_type (GTypeModule *module)
{
	static const GTypeInfo info = {
		sizeof (NautilusTestClass),
		(GBaseInitFunc) NULL,
		(GBaseFinalizeFunc) NULL,
		(GClassInitFunc) nautilus_test_class_init,
		NULL, 
		NULL,
		sizeof (NautilusTest),
		0,
		(GInstanceInitFunc) nautilus_test_instance_init,
	};

	static const GInterfaceInfo menu_provider_iface_info = {
		(GInterfaceInitFunc) nautilus_test_menu_provider_iface_init,
		NULL,
		NULL
	};

	test_type = g_type_module_register_type (module,
					         G_TYPE_OBJECT,
					         "NautilusTest",
					         &info, 0);

	g_type_module_add_interface (module,
				     test_type,
				     NAUTILUS_TYPE_MENU_PROVIDER,
				     &menu_provider_iface_info);
}
