/* $Id$ * Written by Sebastien Tricaud * * 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, version 2 of the License. * * 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. */ /* This file contains all the GTK+ code for brouette: * - tray icon (status icon in gtk+ jargon) * - preference window * - about window * - popup menu */ #include #include #include "brouette-gtk-status.h" #include "config.h" #define ICON_OK STATUS_ICONS "/brouette-ok-icon.png" #define ICON_ALERT STATUS_ICONS "/brouette-alert-icon.png" static GtkStatusIcon *icon; static gboolean preferences_window_open = FALSE; static GtkWidget *entry_profile; static GtkWidget *entry_filter; static GtkWidget *entry_manager_addr; static GtkWidget *entry_prewikka_addr; static GtkWidget *entry_timeout; /* Signals */ static void brouette_gtk_status_activate(GtkStatusIcon *status_icon, gpointer user_data); static void brouette_gtk_status_popup(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data); static void brouette_gtk_status_preferences_window(void); static void brouette_gtk_status_about_window(void); static void brouette_gtk_status_exit(void); /* Popup menu code taken from http://developer.imendio.com/files/developer/demo-statusicon.c.txt */ static void popup_urgent_cb (GtkAction *action, gpointer data); static void popup_really_urgent_cb (GtkAction *action, gpointer data); static GtkActionEntry menu_items [] = { { "Preferences", NULL, "Preferences...", NULL, NULL, G_CALLBACK (brouette_gtk_status_preferences_window) }, { "About", NULL, "About", NULL, NULL, G_CALLBACK (brouette_gtk_status_about_window) }, { "Quit", NULL, "Quit", NULL, NULL, G_CALLBACK (exit) }, }; const gchar *ui_description = "" " " " " " " " " " " " " ""; static GtkWidget* create_popup (GtkStatusIcon *icon) { GtkWidget *popup; GtkActionGroup *action_group; GtkUIManager *ui_manager; action_group = gtk_action_group_new ("PopupActions"); gtk_action_group_set_translation_domain (action_group, NULL); gtk_action_group_add_actions (action_group, menu_items, G_N_ELEMENTS (menu_items), icon); ui_manager = gtk_ui_manager_new (); gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); if (gtk_ui_manager_add_ui_from_string (ui_manager, ui_description, -1, NULL)) { popup = gtk_ui_manager_get_widget (ui_manager, "/ui/PopupMenu"); } return popup; } extern void brouette_gtk_status_init(int argc, char **argv) { gtk_init(&argc, &argv); } extern void brouette_gtk_status_main(void) { gtk_main(); } extern void brouette_gtk_status_run(void) { GtkWidget *popup; icon = gtk_status_icon_new(); gtk_status_icon_set_from_file(icon, ICON_OK); popup = create_popup (icon); gtk_status_icon_set_tooltip(icon, "Brouette prelude monitoring"); g_signal_connect(GTK_STATUS_ICON(icon), "activate", G_CALLBACK(brouette_gtk_status_activate), NULL); g_signal_connect(GTK_STATUS_ICON(icon), "popup-menu", G_CALLBACK(brouette_gtk_status_popup), popup); } static void brouette_gtk_status_preferences_ok(GtkButton *button, gpointer user_data) { const gchar *buffer; preferences_window_open = FALSE; buffer = gtk_entry_get_text(GTK_ENTRY(entry_profile)); config_set_val("idmef","profile",(char *)buffer); buffer = gtk_entry_get_text(GTK_ENTRY(entry_filter)); config_set_val("idmef","filter",(char *)buffer); buffer = gtk_entry_get_text(GTK_ENTRY(entry_manager_addr)); config_set_val("manager","address",(char *)buffer); buffer = gtk_entry_get_text(GTK_ENTRY(entry_prewikka_addr)); config_set_val("prewikka","url",(char *)buffer); buffer = gtk_entry_get_text(GTK_ENTRY(entry_timeout)); config_set_val("general","timeout",(char *)buffer); gtk_widget_destroy(GTK_WIDGET(user_data)); } static void brouette_gtk_status_preferences_cancel(GtkButton *button, gpointer user_data) { preferences_window_open = FALSE; gtk_widget_destroy(GTK_WIDGET(user_data)); } static gboolean destroy_window(GtkWidget *widget, GdkEvent *event, gpointer user_data) { preferences_window_open = FALSE; gtk_widget_destroy(widget); } static void brouette_gtk_status_about_window(void) { GtkWidget *about; about = gtk_about_dialog_new(); gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about), "http://www.wallinfire.net/brouette/"); gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(about), "Event collector for prelude manager"); gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(about), "Sebastien Tricaud 2007"); gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about), APPLICATION_VERSION); gtk_dialog_run(GTK_DIALOG(about)); gtk_widget_destroy(about); } static void brouette_gtk_status_exit(void) { exit(0); } static void brouette_gtk_status_preferences_window(void) { GtkWidget *window; GtkWidget *vbox; GtkWidget *hbox; GtkWidget *frame; GtkWidget *frame_vbox; GtkWidget *frame_hbox; GtkWidget *label; GtkWidget *combo_box; GtkWidget *button; GtkWidget *separator; GDir *themes_dir; const gchar *buffer; if ( ! preferences_window_open ) { themes_dir = g_dir_open(ICONS, 0, NULL); preferences_window_open = TRUE; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Preferences..."); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); g_signal_connect(GTK_WINDOW(window), "delete-event", G_CALLBACK(destroy_window), NULL); g_signal_connect(GTK_WINDOW(window), "destroy-event", G_CALLBACK(destroy_window), NULL); vbox = gtk_vbox_new(FALSE, 5); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox)); hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 5); /* IDMEF Frame */ frame = gtk_frame_new("IDMEF"); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(frame), FALSE, FALSE, 5); frame_vbox = gtk_vbox_new(TRUE, 5); gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(frame_vbox)); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Analyzer profile: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); entry_profile = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(entry_profile), FALSE, FALSE, 5); gtk_entry_set_text(GTK_ENTRY(entry_profile), config_get_val("idmef", "profile")); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Message filter: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); entry_filter = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(entry_filter), FALSE, FALSE, 5); gtk_entry_set_text(GTK_ENTRY(entry_filter), config_get_val("idmef", "filter")); /* Manager frame */ frame = gtk_frame_new("Prelude Manager"); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(frame), FALSE, FALSE, 5); frame_vbox = gtk_vbox_new(TRUE, 5); gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(frame_vbox)); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Manager address: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); entry_manager_addr = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(entry_manager_addr), FALSE, FALSE, 5); gtk_entry_set_text(GTK_ENTRY(entry_manager_addr), config_get_val("manager", "address")); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); /* Now things get underneath */ hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 5); /* Prewikka frame */ frame = gtk_frame_new("Frontend"); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(frame), FALSE, FALSE, 5); frame_vbox = gtk_vbox_new(TRUE, 5); gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(frame_vbox)); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Prewikka URL: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); entry_prewikka_addr = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(entry_prewikka_addr), FALSE, FALSE, 5); gtk_entry_set_text(GTK_ENTRY(entry_prewikka_addr), config_get_val("prewikka", "url")); /* Theme frame */ frame = gtk_frame_new("Theme"); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(frame), FALSE, FALSE, 5); frame_vbox = gtk_vbox_new(TRUE, 5); gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(frame_vbox)); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Use this theme: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); combo_box = gtk_combo_box_new_text(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(combo_box), FALSE, FALSE, 5); while ( buffer = g_dir_read_name(themes_dir) ) { gtk_combo_box_append_text(GTK_COMBO_BOX(combo_box), buffer); } gtk_combo_box_set_active(GTK_COMBO_BOX(combo_box), 0); /* Now things get underneath */ hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 5); /* Timeout frame */ frame = gtk_frame_new("Timeout"); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(frame), FALSE, FALSE, 5); frame_vbox = gtk_vbox_new(TRUE, 5); gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(frame_vbox)); frame_hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(frame_vbox), GTK_WIDGET(frame_hbox), FALSE, FALSE, 5); label = gtk_label_new("Refresh: "); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(label), FALSE, FALSE, 5); entry_timeout = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(frame_hbox), GTK_WIDGET(entry_timeout), FALSE, FALSE, 5); buffer = config_get_val("general", "timeout"); if ( ! buffer ) buffer = "2"; gtk_entry_set_text(GTK_ENTRY(entry_timeout), buffer); /* Now things get underneath */ separator = gtk_hseparator_new(); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(separator), FALSE, FALSE, 5); hbox = gtk_hbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 5); button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(button), FALSE, FALSE, 5); g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(brouette_gtk_status_preferences_cancel), window); button = gtk_button_new_from_stock(GTK_STOCK_OK); gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(button), FALSE, FALSE, 5); g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(brouette_gtk_status_preferences_ok), window); g_dir_close(themes_dir); gtk_widget_show_all(window); } } /* Signals */ /* activate: Left click */ static void brouette_gtk_status_activate(GtkStatusIcon *status_icon, gpointer user_data) { brouette_gtk_status_preferences_window(); } /* popup-menu: Right click */ static void brouette_gtk_status_popup(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data) { GtkWidget *popup; popup = GTK_WIDGET (user_data); gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, button, activate_time); }