📄 prefs_plugins_tree.c
字号:
GtkTreeViewColumn *column; int colnum; gboolean event_was_handled = FALSE; if (!gtk_tree_view_get_path_at_pos(tree, event->x, event->y, &path, &column, NULL, NULL)) return FALSE; colnum = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "colnum")); if (colnum == 1) { if (should_show_warning(gtk_tree_view_get_model(tree), path, NULL)) { show_info("%s", HLP_WARNING); } event_was_handled = TRUE; } else if (colnum == 2) { gtk_tree_view_set_cursor(tree, path, column, TRUE); event_was_handled = TRUE; } gtk_tree_path_free(path); return event_was_handled;}/* Set the "active" property of the toggle cell renderer */static voidactive_data_func(tree_column, cell, model, iter, data) GtkTreeViewColumn *tree_column; GtkCellRenderer *cell; GtkTreeModel *model; GtkTreeIter *iter; gpointer data;{ struct nessus_plugin *plugin; gboolean is_active = FALSE; if (gtk_tree_model_iter_has_child(model, iter)) { is_active = is_family_enabled(model, iter) == 0 ? 0:1; } else { gtk_tree_model_get(model, iter, COL_PLUGIN, &plugin, -1); is_active = plugin->enabled == 0 ? 0:1; } g_object_set(G_OBJECT(cell), "active", is_active, NULL);}/* Set the "pixbuf" property of the warning cell renderer */static voidwarning_data_func(tree_column, cell, model, iter, data) GtkTreeViewColumn *tree_column; GtkCellRenderer *cell; GtkTreeModel *model; GtkTreeIter *iter; gpointer data;{ static GdkPixbuf* warning_pixbuf = NULL; GdkPixbuf *value = NULL; if (should_show_warning(model, NULL, iter)) { if (!warning_pixbuf) warning_pixbuf = gdk_pixbuf_new_from_xpm_data( (const char**)warning_small_xpm); value = warning_pixbuf; } g_object_set(G_OBJECT(cell), "pixbuf", value, NULL);}/* Create a new plugin tree and model */GtkWidget *prefs_create_plugins_tree(context, ctrls) struct context *context; struct arglist *ctrls;{ GtkWidget *tree; GtkTreeSelection *selection; GtkTreeViewColumn *column; GtkCellRenderer *renderer; /* make sure the "statistics-changed" signal exists */ create_statistics_changed_signal(); /* Create the tree and its columns*/ tree = gtk_tree_view_new(); arg_add_value(ctrls, "PLUGINS_TREE_VIEW", ARG_PTR, -1, tree); renderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes(_("Name"), renderer, "text", COL_NAME, NULL); gtk_tree_view_column_set_resizable(column, TRUE); gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); g_object_set_data(G_OBJECT(column), "colnum", GINT_TO_POINTER(0)); renderer = gtk_cell_renderer_pixbuf_new(); column = gtk_tree_view_column_new_with_attributes(_("Warning"), renderer, NULL); gtk_tree_view_column_set_cell_data_func(column, renderer, warning_data_func, NULL, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); g_object_set_data(G_OBJECT(column), "colnum", GINT_TO_POINTER(1)); renderer = gtk_cell_renderer_toggle_new(); g_signal_connect(renderer, "toggled", G_CALLBACK(active_toggled), tree); column = gtk_tree_view_column_new_with_attributes(_("Active"), renderer, NULL); gtk_tree_view_column_set_cell_data_func(column, renderer, active_data_func, NULL, NULL); gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column); g_object_set_data(G_OBJECT(column), "colnum", GINT_TO_POINTER(2)); /* selection settings */ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); g_signal_connect(G_OBJECT(tree), "row-activated", G_CALLBACK(row_activated), NULL); g_signal_connect(G_OBJECT(tree), "button-press-event", G_CALLBACK(button_press_event), NULL); return tree;}/* Fill the context's tree model with the plugins, applying the filter * from ctrls if one is set. */static voidfill_plugin_tree_store(context, ctrls) struct context *context; struct arglist *ctrls;{ struct nessus_plugin *plugs = context->plugins; GtkTreeModel *store = GTK_TREE_MODEL(context->plugin_tree_store); struct plugin_filter *filter = arg_get_value(ctrls, "FILTER"); GHashTable* family_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)gtk_tree_path_free); /* add the plugins and families. The family nodes are created when * the first plugin of that family is encountered */ while(plugs != NULL ) { GtkTreeIter family_iter, insert_iter; char *plug_family; GtkTreePath * family_path; /* char *summary = arg_get_value(plugs->value, "SUMMARY"); */ if(filter && filter_plugin(filter, plugs)) { plugs = plugs->next; continue; } /* Determine the iter of the family node for the current plugin. If * the node doesn't exist yet, create it. */ plug_family = plugs->family; family_path = g_hash_table_lookup(family_hash, plug_family); if (family_path == NULL ) { gtk_tree_store_append(GTK_TREE_STORE(store), &family_iter, NULL); family_path = gtk_tree_model_get_path(store, &family_iter); g_hash_table_insert(family_hash, plug_family, family_path); gtk_tree_store_set(GTK_TREE_STORE(store), &family_iter, COL_NAME, plug_family, COL_PLUGIN, NULL, -1); } else { gtk_tree_model_get_iter(store, &family_iter, family_path); } /* Now family_iter should be set to the iter of the family the * current plugin belongs to. Append the plugin to that family. */ gtk_tree_store_append(GTK_TREE_STORE(store), &insert_iter, &family_iter); gtk_tree_store_set(GTK_TREE_STORE(store), &insert_iter, COL_NAME, plugs->name, COL_PLUGIN, plugs, -1); plugs = plugs->next; } g_hash_table_destroy(family_hash);}/* Update the plugin tree with a different context. The plugin-tree * maintains a tree store and a sorted model in the context. Make the * tree store of the new context the model shown in the plugin tree view * (taken from ctrls). If the store doesn't exist yet or if the * force_rebuild parameter is TRUE, this function creates a new store. * * Also, connect to some of the model's signals so that we can emit the * statistics_changed signal. */voidprefs_plugin_tree_context_changed(context, ctrls, force_rebuild) struct context *context; struct arglist *ctrls; gboolean force_rebuild;{ GtkTreeView *tree_view = arg_get_value(ctrls, "PLUGINS_TREE_VIEW"); GtkTreeModel *model = context->plugin_tree_model; GtkTreeModel *old_model = gtk_tree_view_get_model(tree_view); /* if the caller wants, force a rebuild of the store by deleting the * old one */ if (force_rebuild) context_reset_plugin_tree(context); /* if the context doesn't have a tree store yet, create it together * with the sort model and fill it */ if (context->plugin_tree_store == NULL) { context->plugin_tree_store = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_POINTER); model = gtk_tree_model_sort_new_with_model( GTK_TREE_MODEL(context->plugin_tree_store)); gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model), 0, GTK_SORT_ASCENDING); context->plugin_tree_model = model; fill_plugin_tree_store(context, ctrls); g_object_set_data(G_OBJECT(context->plugin_tree_store), "plugins", context->plugins); } /* Set the new model, reconnecting the signals that we need to emit * the statistics_changed signal */ if (old_model != NULL) { g_signal_handlers_disconnect_by_func(old_model, emit_statistics_when_row_deleted, tree_view); g_signal_handlers_disconnect_by_func(old_model, emit_statistics_when_row_changed, tree_view); } gtk_tree_view_set_model(tree_view, model); if (model != NULL) { g_signal_connect(model, "row-changed", emit_statistics_when_row_changed, tree_view); g_signal_connect(model, "row-deleted", emit_statistics_when_row_deleted, tree_view); } /* When a new model is set, the statistics will have changed: */ emit_statistics_changed(tree_view);}/* enable/disable all plugins in the tree. */voidprefs_plugin_tree_enable_all(tree, enable) GtkTreeView *tree; int enable;{ GtkTreeModel *model = gtk_tree_view_get_model(tree); GtkTreeIter iter, child_iter; struct nessus_plugin *plugin; if (gtk_tree_model_get_iter_first(model, &iter)) { do { if (gtk_tree_model_iter_children(model, &child_iter, &iter)) { do { gtk_tree_model_get(model, &child_iter, COL_PLUGIN, &plugin, -1); plugin->enabled = enable; trigger_row_update(model, &child_iter); } while (gtk_tree_model_iter_next(model, &child_iter)); trigger_row_update(model, &iter); } } while (gtk_tree_model_iter_next(model, &iter)); } }/* Compute some statistics for the plugin tree * * The tree_view parameter must be a tree view created by * prefs_create_plugins_tree and filled with * prefs_fill_plugin_tree_model. * * The function determines the total number of plugins and the number of * active plugins and stores these values is *total and *enabled. */voidprefs_plugin_tree_statistics(tree_view, total, enabled) GtkTreeView *tree_view; int *total; int *enabled;{ GtkTreeModel *tree_model = gtk_tree_view_get_model(tree_view); GtkTreeModel *real_model = gtk_tree_model_sort_get_model( GTK_TREE_MODEL_SORT(tree_model)); struct nessus_plugin *plugins = g_object_get_data(G_OBJECT(real_model), "plugins"); *total = 0; *enabled = 0; while (plugins != NULL) { *total += 1; if (plugins->enabled) *enabled += 1; plugins = plugins->next; }}#endif /* USE_GTK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -