/***** * * Copyright (C) 2004, 2005 Pablo Belin * All Rights Reserved * * This file is part of the gprelude program. * * 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; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * *****/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "list.h" #include "db-interface.h" #include "gprelude-config.h" #include "db-selection.h" #include "db-selection-dialog.h" #include "idmef-criteria.h" #include "idmef-criteria-dialog.h" #include "alerts.h" #include "active-cnx.h" #include "active-cnx-widget.h" struct active_cnx_widget { GtkListStore *store; GtkWidget *view; list_t *list; active_cnx_t *current_cnx; alerts_layout_t *layout; GtkWidget *statusbar; int context_id; int message_id; }; void active_cnx_widget_disconnect ( active_cnx_widget_t *cnx ) { GtkTreeSelection *selection; GtkTreeModel *model; GtkTreeIter iter, tmp; GtkTreePath *path; list_t *list = NULL; selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW(cnx->view) ); model = gtk_tree_view_get_model ( GTK_TREE_VIEW(cnx->view) ); if ( gtk_tree_selection_get_selected ( selection, &model, &iter ) ) { gtk_tree_model_get ( model, &iter, ACTIVE_CNX_STORE_DATA, &list, -1 ); cnx->list = list_remove_with_data ( cnx->list, list, active_cnx_destroy ); cnx->current_cnx = NULL; path = gtk_tree_model_get_path ( model, &iter ); tmp = iter; if ( gtk_tree_path_prev ( path ) ) gtk_tree_selection_select_path ( selection, path ); else if ( gtk_tree_model_iter_next ( model, &tmp ) ) gtk_tree_selection_select_iter ( selection, &tmp ); else { set_alerts_layout_ctx ( cnx->layout, NULL ); if ( cnx->message_id != -1 ) { gtk_statusbar_remove ( GTK_STATUSBAR(cnx->statusbar), cnx->context_id, cnx->message_id ); cnx->message_id = -1; } } gtk_list_store_remove ( cnx->store, &iter ); gtk_tree_path_free ( path ); } } static GtkListStore *create_active_cnx_widget_store ( void ) { GtkListStore *store; store = gtk_list_store_new ( ACTIVE_CNX_STORE_NB, G_TYPE_STRING, G_TYPE_POINTER ); return store; } static void cb_active_cnx_widget_view_changed ( GtkTreeSelection *selection, active_cnx_widget_t *cnx ) { GtkTreeModel *model; GtkTreeIter iter; list_t *list = NULL; active_cnx_t *act; char *str; model = gtk_tree_view_get_model ( GTK_TREE_VIEW(cnx->view) ); if ( gtk_tree_selection_get_selected ( selection, &model, &iter ) ) { gtk_tree_model_get ( model, &iter, ACTIVE_CNX_STORE_DATA, &list, -1 ); act = (active_cnx_t *)list->data; cnx->current_cnx = act; set_alerts_layout_ctx ( cnx->layout, active_cnx_get_ctx ( act ) ); if ( cnx->message_id != -1 ) { gtk_statusbar_remove ( GTK_STATUSBAR(cnx->statusbar), cnx->context_id, cnx->message_id ); cnx->message_id = -1; } str = active_cnx_get_str ( act ); if ( str ) cnx->message_id = gtk_statusbar_push ( GTK_STATUSBAR(cnx->statusbar), cnx->context_id, str ); } } static GtkWidget *create_active_cnx_widget_view ( active_cnx_widget_t *cnx ) { GtkWidget *view; GtkTreeSelection *selection; GtkCellRenderer *renderer; GtkTreeViewColumn *column; view = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(cnx->store) ); gtk_tree_view_set_headers_visible ( GTK_TREE_VIEW(view), FALSE ); selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW(view) ); gtk_tree_selection_set_mode ( selection, GTK_SELECTION_SINGLE ); g_signal_connect ( G_OBJECT(selection), "changed", G_CALLBACK(cb_active_cnx_widget_view_changed), cnx ); renderer = gtk_cell_renderer_text_new (); column = gtk_tree_view_column_new_with_attributes ( "", renderer, "text", ACTIVE_CNX_STORE_NAME, NULL ); gtk_tree_view_append_column ( GTK_TREE_VIEW(view), column ); return view; } active_cnx_widget_t *create_active_cnx_widget ( alerts_layout_t *layout, GtkWidget *statusbar ) { active_cnx_widget_t *cnx; cnx = malloc ( sizeof(active_cnx_widget_t) ); if ( !cnx ) { log ( LOG_ERR, "memory exhausted.\n" ); return NULL; } cnx->list = NULL; cnx->current_cnx = NULL; cnx->layout = layout; cnx->store = create_active_cnx_widget_store (); cnx->view = create_active_cnx_widget_view ( cnx ); cnx->statusbar = statusbar; cnx->context_id = gtk_statusbar_get_context_id ( GTK_STATUSBAR(statusbar), "Interfaces info" ); cnx->message_id = -1; return cnx; } int destroy_active_cnx_widget ( active_cnx_widget_t *cnx ) { if ( !cnx ) return -1; list_destroy_recursive_with_data ( cnx->list, active_cnx_destroy ); free ( cnx ); return 0; } int set_active_cnx_widget ( active_cnx_widget_t *cnx, active_cnx_t *act ) { GtkTreeIter iter; GtkTreeSelection *selection; list_t *list; char *name; list = list_new ( act ); if ( !list ) { log ( LOG_ERR, "memory exhausted.\n" ); return -1; } cnx->list = list_append ( cnx->list, list ); cnx->current_cnx = act; name = active_cnx_get_name ( act ); gtk_list_store_append ( cnx->store, &iter ); gtk_list_store_set ( cnx->store, &iter, ACTIVE_CNX_STORE_NAME, name, ACTIVE_CNX_STORE_DATA, list, -1 ); selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW(cnx->view) ); gtk_tree_selection_select_iter ( selection, &iter ); return 0; } GtkWidget *create_active_cnx_widget_ornament ( active_cnx_widget_t *cnx ) { GtkWidget *scrolled; scrolled = gtk_scrolled_window_new ( NULL, NULL ); gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC ); gtk_container_add ( GTK_CONTAINER(scrolled), cnx->view ); gtk_widget_show ( cnx->view ); return scrolled; } list_t *get_active_cnx_widget_list ( active_cnx_widget_t *cnx ) { return cnx->list; }