/* gcc -o gtk-test `pkg-config --libs --cflags gtk+-2.0` gtk-test.c */
#include <gtk/gtk.h>

static gboolean
on_timeout ()
{
	GError *err = NULL;

	/* This is the standard way gnome-keyring-ask is run: config piped into it on stdin */

	const char *request = "[general]\nflags=16641\ntitle=Test Ask Dialog\n" \
	                      "primary=Primary prompt text.\n" \
	                      "secondary=Some secondary prompt text is here.";

	g_printerr ("run gnome-keyring-ask\n");

	if (!g_file_set_contents ("/tmp/test-case-screen-lock", request, -1, &err))
		g_error ("%s", err->message);

	if (!g_spawn_command_line_async ("sh -c \"/usr/lib/gnome-keyring/gnome-keyring-ask < /tmp/test-case-screen-lock\"", &err))
		g_error ("%s", err->message);

	return FALSE; /* Don't run timeout again */
}

gboolean
on_button_press(GtkWidget *widget, GdkEventButton *event, GtkUIManager *ui)
{
	GtkWidget *popup;

	if(event->button != 3)
		return FALSE;

	g_printerr ("show menu\n");

	popup = gtk_ui_manager_get_widget (ui, "/popup");
	g_return_val_if_fail (popup, FALSE);

	gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, 3, event->time);
	gtk_widget_show (popup);

	g_timeout_add (1000, on_timeout, NULL);
	
	return TRUE;
}

static const GtkActionEntry ACTIONS[] = {
	{ "test", NULL, "Test", NULL, NULL, NULL },
	{ "test1", NULL, "Test 1", NULL, NULL, NULL },
	{ "test2", NULL, "Test 2", NULL, NULL, NULL },
	{ "test3", NULL, "Test 3", NULL, NULL, NULL },
};

void 
test()
{
	GtkWidget *win;
	GtkWidget *label;
	GtkUIManager *ui;
	GtkActionGroup *actions;

	actions = gtk_action_group_new ("blah");
	gtk_action_group_add_actions (actions, ACTIONS, 4, NULL);

	ui = gtk_ui_manager_new ();
	gtk_ui_manager_insert_action_group (ui, actions, 0);
	gtk_ui_manager_add_ui_from_string (ui, "<ui><popup><menuitem action='test'/><menuitem action='test1'/>" \
	                                       "<menuitem action='test2'/><menuitem action='test3'/></popup></ui>", 
	                                   -1, NULL);

	label = gtk_label_new ("Right click and wait");
	gtk_widget_show (label);

	win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (win), 200, 200);
	gtk_widget_add_events (win, GDK_BUTTON_PRESS_MASK);
	gtk_container_add (GTK_CONTAINER (win), label);
	g_signal_connect (win, "button_press_event", G_CALLBACK (on_button_press), ui);
	g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
	gtk_widget_show_all (win);
	gtk_main();
}

int
main (int argc, char **argv)
{
	gtk_init(&argc, &argv);
	test();
	return 0;
}

