/*---[ menus.c ]------------------------------------------------------
 * 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.
 *
 * Menu and toolbar related functions
 *
 * Based on firestarter code by Tomas Junnonen (majix@sci.fi)
 *--------------------------------------------------------------------*/

#include "menus.h"
#include "razorback.h"
#include "savelog.h"
#include "preferences.h"
#include "parse.h"
#include "lookup.h"

#include "xpm/clear.xpm"
#include "xpm/refresh.xpm"
#include "xpm/save.xpm"

static GnomeUIInfo docklet_menu [] = {
	GNOMEUIINFO_MENU_PREFERENCES_ITEM (preferences_cb, NULL),
	GNOMEUIINFO_SEPARATOR,
	GNOMEUIINFO_MENU_ABOUT_ITEM (show_about, NULL),
	GNOMEUIINFO_MENU_EXIT_ITEM (exit_razorback, NULL),
	GNOMEUIINFO_END
};

static GnomeUIInfo razorback_menu [] = {
	GNOMEUIINFO_MENU_PREFERENCES_ITEM (preferences_cb, NULL),
	GNOMEUIINFO_SEPARATOR,
	GNOMEUIINFO_MENU_EXIT_ITEM (exit_razorback, NULL),
	GNOMEUIINFO_END
};

static GnomeUIInfo log_menu [] = {
	GNOMEUIINFO_ITEM (N_("_Clear snort hit list"), NULL, clear_hitlist, clear_xpm),
	GNOMEUIINFO_ITEM (N_("R_eload entire snort hit list"), NULL, reload_hitlist, refresh_xpm),
	GNOMEUIINFO_ITEM (N_("_Save snort hit list to file"), NULL, create_savelog_filesel, save_xpm),
	GNOMEUIINFO_END
};

static GnomeUIInfo help_menu [] = {
	GNOMEUIINFO_MENU_ABOUT_ITEM (show_about, NULL),
	GNOMEUIINFO_END
};

static GnomeUIInfo menu [] = {
	GNOMEUIINFO_SUBTREE (N_("_Razorback"), razorback_menu),
	GNOMEUIINFO_SUBTREE (N_("_Hit list"), log_menu),
	GNOMEUIINFO_MENU_HELP_TREE (help_menu),
	GNOMEUIINFO_END
};

static GnomeUIInfo hitlogtoolbar [] = {
	GNOMEUIINFO_ITEM (NULL, N_("Clear snort hit list"), clear_hitlist, clear_xpm),
  	GNOMEUIINFO_ITEM (NULL, N_("Reload entire snort hit list"), reload_hitlist, refresh_xpm),
  	GNOMEUIINFO_ITEM (NULL, N_("Save snort hit list to file"), create_savelog_filesel, save_xpm),
	GNOMEUIINFO_END
};

static GnomeUIInfo hitlog_menu [] = {
	GNOMEUIINFO_ITEM (N_("Lookup hostname"), NULL, lookup, NULL),
	GNOMEUIINFO_END
};

/* [ install_menus_and_toolbar ]
 * Install the menus and the toolbar in *window
 */
void
install_menus_and_toolbar (GtkWidget *window)
{
	gnome_app_create_toolbar_with_data (GNOME_APP (window), hitlogtoolbar, window);
	gnome_app_create_menus_with_data (GNOME_APP (window), menu, window);

	appbar = gnome_appbar_new (FALSE, TRUE, GNOME_PREFERENCES_USER);
	gnome_app_set_statusbar (GNOME_APP (window), appbar);
}

/* [ popup_menu ]
 * Pops up a menu and sets the sensitivity of the menu items
 */
void
popup_menu (GtkWidget *clist, GdkEventButton* bevent, GtkWidget *popup)
{
	GList* l = NULL;
	gint i, row;

	if (bevent->button == 3) {
		l = GTK_MENU_SHELL (popup)->children;
		row = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (clist), "row"));

		for (i = 0; i < 7 && l != NULL; i++) {			
			gtk_widget_set_sensitive (GTK_WIDGET (l->data), row);
			l = l->next;
		}

	    /* The two last menu items of the dynclists are always sensitive*/
		if (clist == denyallclist || clist == allowallclist ||
		    clist == allowsmclist || clist == allowsaclist) {
			l = GTK_MENU_SHELL (popup)->children;
			l = l->next;
			gtk_widget_set_sensitive (GTK_WIDGET (l->data), TRUE);
			l = l->next;
			gtk_widget_set_sensitive (GTK_WIDGET (l->data), TRUE);
			l = l->next;
			l = l->next;
			gtk_widget_set_sensitive (GTK_WIDGET (l->data), TRUE);
			dynclist = (gpointer *)clist;
		}

		gtk_menu_popup (GTK_MENU (popup), NULL, NULL,
				NULL, NULL,
				bevent->button, bevent->time);
	}
}

/* [ install_clist_popup ]
 * Create a popup menu for a clist
 */
void
install_clist_popup (GtkWidget *clist)
{
	hitpopup = gnome_popup_menu_new (hitlog_menu);
}


/* [ install_docklet_menu ]
 * Attach a popup to the status button docklet
 */
void
install_docklet_menu (GtkWidget *eventbox)
{
	GtkWidget *menu;

	menu = gnome_popup_menu_new (docklet_menu);
	gnome_popup_menu_attach (menu, eventbox, NULL);
}
