/***** * * 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 "gprelude-mem.h" #include "idmef-value.h" #define TYPE_NUMERIC(vid,vmin,vmax,vdigits,vstep) \ case (vid): \ min = vmin; \ max = vmax; \ digits = vdigits; \ step = vstep; \ break GtkWidget *create_idmef_value_type_numeric ( idmef_value_type_id_t id ) { GtkAdjustment *adj; GtkWidget *spin; double min, max, step; int digits; switch ( id ) { TYPE_NUMERIC(type_int16, SHRT_MIN, SHRT_MAX, 0, 1); TYPE_NUMERIC(type_uint16, 0, USHRT_MAX, 0, 1); TYPE_NUMERIC(type_int32, INT_MIN, INT_MAX, 0, 1); TYPE_NUMERIC(type_uint32, 0, UINT_MAX, 0, 1); TYPE_NUMERIC(type_int64, LONG_MIN, LONG_MAX, 0, 1); TYPE_NUMERIC(type_uint64, 0, ULONG_MAX, 0, 1); TYPE_NUMERIC(type_float, -FLT_MAX, FLT_MAX, 2, 0.01); TYPE_NUMERIC(type_double, -DBL_MAX, DBL_MAX, 2, 0.01); default: return NULL; } adj = (GtkAdjustment *)gtk_adjustment_new ( 0.0, min, max, step, step * 10, step * 10 ); spin = gtk_spin_button_new ( adj, step, digits ); return spin; } void set_idmef_value_type_numeric ( GtkWidget *spin, double value ) { gtk_spin_button_set_value ( GTK_SPIN_BUTTON(spin), value ); } void clear_idmef_value_type_numeric ( GtkWidget *spin ) { set_idmef_value_type_numeric ( spin, 0.0 ); } #define GET_TYPE_NUMERIC_AS_INTEGER(vname) \ case type_##vname : \ value_as_int = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(spin) ); \ value = idmef_value_new_##vname ( value_as_int ); \ break #define GET_TYPE_NUMERIC_AS_DOUBLE(vname) \ case type_##vname : \ value_as_double = gtk_spin_button_get_value ( GTK_SPIN_BUTTON(spin) ); \ value = idmef_value_new_##vname ( value_as_double ); \ break idmef_criterion_value_t *get_idmef_value_type_numeric ( GtkWidget *spin, idmef_value_type_id_t id ) { idmef_value_t *value; idmef_criterion_value_t *criterion_value; int value_as_int; double value_as_double; switch ( id ) { GET_TYPE_NUMERIC_AS_INTEGER(int16); GET_TYPE_NUMERIC_AS_INTEGER(uint16); GET_TYPE_NUMERIC_AS_INTEGER(int32); GET_TYPE_NUMERIC_AS_INTEGER(uint32); GET_TYPE_NUMERIC_AS_INTEGER(int64); GET_TYPE_NUMERIC_AS_INTEGER(uint64); GET_TYPE_NUMERIC_AS_DOUBLE(float); GET_TYPE_NUMERIC_AS_DOUBLE(double); default: value = NULL; break; } if ( !value ) return NULL; criterion_value = idmef_criterion_value_new_fixed ( value ); if ( !criterion_value ) idmef_value_destroy ( value ); return criterion_value; } GtkWidget *create_idmef_value_type_string ( void ) { GtkWidget *entry; entry = gtk_entry_new (); return entry; } void set_idmef_value_type_string ( GtkWidget *entry, const char *str ) { gtk_entry_set_text ( GTK_ENTRY(entry), str ? str : "" ); } void clear_idmef_value_type_string ( GtkWidget *entry ) { set_idmef_value_type_string ( entry, NULL ); } idmef_criterion_value_t *get_idmef_value_type_string ( GtkWidget *entry ) { idmef_string_t *string; idmef_value_t *value; idmef_criterion_value_t *criterion_value; const char *buff; buff = gtk_entry_get_text (GTK_ENTRY(entry) ); string = idmef_string_new_dup ( buff ); if ( !string ) return NULL; value = idmef_value_new_string ( string ); if ( !value ) { idmef_string_destroy ( string ); return NULL; } criterion_value = idmef_criterion_value_new_fixed ( value ); if ( !criterion_value ) idmef_value_destroy ( value ); return criterion_value; } GList *create_idmef_value_type_enum ( idmef_type_t type ) { GList *items = NULL; idmef_child_t child = 0; const char *str; char *buff; while ( (str = idmef_type_enum_to_string ( type, child )) ) { if ( strcmp ( str, "NULL" ) != 0 ) { buff = gprelude_strdup ( str ); if ( buff ) items = g_list_append ( items, buff ); } child++; } return items; } GtkWidget *create_idmef_value_type_enum_ornament ( GList *list ) { GtkWidget *combo; combo = gtk_combo_new (); gtk_entry_set_editable ( GTK_ENTRY(GTK_COMBO(combo)->entry), FALSE ); if ( list ) gtk_combo_set_popdown_strings ( GTK_COMBO(combo), list ); return combo; } idmef_criterion_value_t *get_idmef_value_type_enum ( GtkWidget *combo, idmef_type_t type ) { idmef_value_t *value; idmef_criterion_value_t *criterion_value; const char *buff; buff = gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo)->entry) ); value = idmef_value_new_enum_string ( type, buff ); if ( !value ) return NULL; criterion_value = idmef_criterion_value_new_fixed ( value ); if ( !criterion_value ) idmef_value_destroy ( value ); return criterion_value; } gprelude_idmef_value_type_time_t *create_idmef_value_type_time ( void ) { gprelude_idmef_value_type_time_t *idmef_time; GtkAdjustment *adj; idmef_time = malloc ( sizeof(gprelude_idmef_value_type_time_t) ); if ( !idmef_time ) { log ( LOG_ERR, "Memory exhausted.\n" ); return NULL; } memset ( idmef_time, 0, sizeof(gprelude_idmef_value_type_time_t) ); idmef_time->calendar = gtk_calendar_new (); adj = (GtkAdjustment *)gtk_adjustment_new ( 0, 0, 23, 1, 10, 10 ); idmef_time->hour = gtk_spin_button_new ( adj, 1, 0 ); adj = (GtkAdjustment *)gtk_adjustment_new ( 0, 0, 59, 1, 10, 10 ); idmef_time->min= gtk_spin_button_new ( adj, 1, 0 ); adj = (GtkAdjustment *)gtk_adjustment_new ( 0, 0, 59, 1, 10, 10 ); idmef_time->sec = gtk_spin_button_new ( adj, 1, 0 ); return idmef_time; } GtkWidget *create_idmef_value_type_time_ornament ( gprelude_idmef_value_type_time_t *idmef_time ) { GtkWidget *table, *label; if ( !idmef_time ) return NULL; table = gtk_table_new ( 3, 3, FALSE ); gtk_table_set_col_spacings ( GTK_TABLE(table), 5 ); gtk_table_set_row_spacings ( GTK_TABLE(table), 5 ); gtk_table_attach_defaults ( GTK_TABLE(table), idmef_time->calendar, 0, 3, 0, 1 ); gtk_widget_show ( idmef_time->calendar ); label = gtk_label_new ( "Hour :" ); gtk_table_attach_defaults ( GTK_TABLE(table), label, 0, 1, 1, 2 ); gtk_table_attach_defaults ( GTK_TABLE(table), idmef_time->hour, 0, 1, 2, 3 ); gtk_widget_show ( label ); gtk_widget_show ( idmef_time->hour ); label = gtk_label_new ( "Minute :" ); gtk_table_attach_defaults ( GTK_TABLE(table), label, 1, 2, 1, 2 ); gtk_table_attach_defaults ( GTK_TABLE(table), idmef_time->min, 1, 2, 2, 3 ); gtk_widget_show ( label ); gtk_widget_show ( idmef_time->min ); label = gtk_label_new ( "Seconde :" ); gtk_table_attach_defaults ( GTK_TABLE(table), label, 2, 3, 1, 2 ); gtk_table_attach_defaults ( GTK_TABLE(table), idmef_time->sec, 2, 3, 2, 3 ); gtk_widget_show ( label ); gtk_widget_show ( idmef_time->sec ); return table; } #define GET_NON_LINEAR_TIME(vname) \ i = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(idmef_time->vname) ); \ if ( i ) \ idmef_criterion_value_non_linear_time_set_ ## vname ( value, i ); idmef_criterion_value_t *get_idmef_value_type_time ( gprelude_idmef_value_type_time_t *idmef_time ) { idmef_criterion_value_non_linear_time_t *value; idmef_criterion_value_t *criterion_value; unsigned int year, month, day; int i; if ( !idmef_time ) return NULL; value = idmef_criterion_value_non_linear_time_new (); if ( !value ) return NULL; gtk_calendar_get_date ( GTK_CALENDAR(idmef_time->calendar), &year, &month, &day ); idmef_criterion_value_non_linear_time_set_year ( value, year ); idmef_criterion_value_non_linear_time_set_month ( value, month ); idmef_criterion_value_non_linear_time_set_mday ( value, day ); GET_NON_LINEAR_TIME ( hour ); GET_NON_LINEAR_TIME ( min ); GET_NON_LINEAR_TIME ( sec ); criterion_value = idmef_criterion_value_new_non_linear_time ( value ); if ( !criterion_value ) idmef_criterion_value_non_linear_time_destroy ( value ); return criterion_value; } GtkWidget *create_idmef_value_type_data ( void ) { GtkWidget *view; view = gtk_text_view_new (); return view; } GtkWidget *create_idmef_value_type_data_ornament ( GtkWidget *view ) { GtkWidget *scrolled, *frame; scrolled = gtk_scrolled_window_new ( NULL, NULL ); gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); gtk_container_add ( GTK_CONTAINER(scrolled), view ); gtk_widget_show ( view ); frame = gtk_frame_new ( NULL ); gtk_container_add ( GTK_CONTAINER(frame), scrolled ); gtk_widget_show ( scrolled ); return frame; } idmef_criterion_value_t *get_idmef_value_type_data ( GtkWidget *view ) { idmef_data_t *data; idmef_value_t *value; idmef_criterion_value_t *criterion_value; GtkTextBuffer *buffer; GtkTextIter start, end; char *text; buffer = gtk_text_view_get_buffer ( GTK_TEXT_VIEW(view) ); gtk_text_buffer_get_start_iter ( buffer, &start ); gtk_text_buffer_get_end_iter ( buffer, &end ); text = gtk_text_buffer_get_text ( buffer, &start, &end, FALSE ); if ( !text ) return NULL; data = idmef_data_new_nodup ( text, strlen ( text ) ); if ( !data ) { free ( text ); return NULL; } value = idmef_value_new_data ( data ); if ( !value ) { idmef_data_destroy ( data ); return NULL; } criterion_value = idmef_criterion_value_new_fixed ( value ); if ( !criterion_value ) idmef_value_destroy ( value ); return criterion_value; }