1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 * Gui Policy Editor for TOMOYO Linux
6 * Copyright (C) Yoshihiro Kusuno 2011 <yocto@users.sourceforge.jp>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
28 #include <glib/gi18n.h>
32 typedef struct _search_t {
37 #define COMBO_LIST_LIMIT 10
38 static GList *combolist = NULL;
39 static gchar *S_entry = NULL;
45 SCREEN_EXCEPTION_LIST,
51 static gint locate_index[MAXSCREEN+1] = {0, };
53 static gint get_locate_index(transition_t *transition)
55 enum screen_type_e screen;
57 switch((int)transition->current_page) {
58 case CCS_SCREEN_DOMAIN_LIST :
59 if (transition->task_flag) {
60 screen = SCREEN_PROCESS_LIST;
62 screen = SCREEN_DOMAIN_LIST;
65 case CCS_SCREEN_ACL_LIST :
66 screen = SCREEN_ACL_LIST;
68 case CCS_SCREEN_EXCEPTION_LIST :
69 screen = SCREEN_EXCEPTION_LIST;
71 case CCS_SCREEN_PROFILE_LIST :
72 screen = SCREEN_PROFILE_LIST;
74 case CCS_SCREEN_MANAGER_LIST :
80 return locate_index[screen];
83 static void put_locate_index(transition_t *transition, gint index)
85 enum screen_type_e screen;
87 switch((int)transition->current_page) {
88 case CCS_SCREEN_DOMAIN_LIST :
89 if (transition->task_flag) {
90 screen = SCREEN_PROCESS_LIST;
92 screen = SCREEN_DOMAIN_LIST;
95 case CCS_SCREEN_ACL_LIST :
96 screen = SCREEN_ACL_LIST;
98 case CCS_SCREEN_EXCEPTION_LIST :
99 screen = SCREEN_EXCEPTION_LIST;
101 case CCS_SCREEN_PROFILE_LIST :
102 screen = SCREEN_PROFILE_LIST;
104 case CCS_SCREEN_MANAGER_LIST :
110 locate_index[screen] = index;
113 static gboolean search_pos_list(GtkTreeModel *model, GtkTreePath *path,
114 GtkTreeIter *iter, transition_t *transition)
116 GtkWidget *view = NULL;
117 GtkTreeSelection *selection;
119 gchar *number = NULL;
122 switch((int)transition->current_page) {
123 case CCS_SCREEN_DOMAIN_LIST :
124 if (transition->task_flag) {
125 view = transition->tsk.treeview;
127 view = transition->treeview;
129 gtk_tree_model_get(model, iter, 0, &index, -1);
131 case CCS_SCREEN_ACL_LIST :
132 view = transition->acl.listview;
133 gtk_tree_model_get(model, iter, 0, &number, -1);
134 index = atoi(number);
136 case CCS_SCREEN_EXCEPTION_LIST :
137 view = transition->exp.listview;
138 gtk_tree_model_get(model, iter, 0, &number, -1);
139 index = atoi(number);
141 case CCS_SCREEN_PROFILE_LIST :
142 view = transition->prf.listview;
143 gtk_tree_model_get(model, iter, 0, &number, -1);
144 index = atoi(number);
149 if (index == get_locate_index(transition)) {
150 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
151 gtk_tree_selection_select_path(selection, path);
152 view_cursor_set(view, gtk_tree_path_copy(path), NULL);
159 static gint get_list_count(transition_t *transition)
163 switch((int)transition->current_page) {
164 case CCS_SCREEN_DOMAIN_LIST :
165 if (transition->task_flag) {
166 count = transition->tsk.count;
168 count = transition->dp->list_len;
171 case CCS_SCREEN_ACL_LIST :
172 count = transition->acl.count;
174 case CCS_SCREEN_EXCEPTION_LIST :
175 count = transition->exp.count;
177 case CCS_SCREEN_PROFILE_LIST :
178 count = transition->prf.count;
180 case CCS_SCREEN_MANAGER_LIST :
188 static gint get_current_index(transition_t *transition)
192 switch((int)transition->current_page) {
193 case CCS_SCREEN_DOMAIN_LIST :
194 if (transition->task_flag) {
195 index = get_current_process_index(&(transition->tsk));
197 index = get_current_domain_index(transition);
200 case CCS_SCREEN_ACL_LIST :
201 index = select_list_line(&(transition->acl));
203 case CCS_SCREEN_EXCEPTION_LIST :
204 index = select_list_line(&(transition->exp));
206 case CCS_SCREEN_PROFILE_LIST :
207 index = select_list_line(&(transition->prf));
209 case CCS_SCREEN_MANAGER_LIST :
217 static void search(transition_t *transition, gboolean forward)
219 GtkWidget *view = NULL;
223 index = get_current_index(transition);
224 count = get_list_count(transition);
226 forward ? index++ : index--;
227 if (index < 0 || count <= index)
230 switch((int)transition->current_page) {
231 case CCS_SCREEN_DOMAIN_LIST :
232 if (transition->task_flag) {
233 view = transition->tsk.treeview;
234 str_p = transition->tsk.task[index].name;
236 view = transition->treeview;
237 str_p = (gchar *)get_domain_last_name(index);
240 case CCS_SCREEN_ACL_LIST :
241 view = transition->acl.listview;
242 str_p = g_strdup_printf("%s %s",
243 ccs_directives[transition->acl.list
244 [index].directive].alias,
245 transition->acl.list[index].operand);
247 case CCS_SCREEN_EXCEPTION_LIST :
248 view = transition->exp.listview;
249 str_p = g_strdup_printf("%s %s",
250 ccs_directives[transition->exp.list
251 [index].directive].alias,
252 transition->exp.list[index].operand);
254 case CCS_SCREEN_PROFILE_LIST :
255 view = transition->prf.listview;
256 str_p = g_strdup_printf("%u-%s",
257 transition->prf.list[index].directive,
258 transition->prf.list[index].operand);
260 case CCS_SCREEN_MANAGER_LIST :
266 if(g_strrstr(str_p, S_entry)) {
268 put_locate_index(transition, index);
269 model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
270 gtk_tree_model_foreach(model,
271 (GtkTreeModelForeachFunc)search_pos_list, transition);
276 if (transition->current_page != CCS_SCREEN_DOMAIN_LIST)
279 gtk_action_set_sensitive(gtk_action_group_get_action(
280 transition->actions, "SearchBack"), TRUE);
281 gtk_action_set_sensitive(gtk_action_group_get_action(
282 transition->actions, "SearchFoward"), TRUE);
285 /*-------+---------+---------+---------+---------+---------+---------+--------*/
286 static void cb_combo_changed(GtkComboBox *combobox, gpointer nothing)
292 model = gtk_combo_box_get_model(combobox);
293 if (gtk_combo_box_get_active_iter(combobox, &iter)) {
294 gtk_tree_model_get(model, &iter, 0, &text, -1);
297 // g_print("cb_combo_changed[%s]\n", text);
301 static void cb_combo_entry_activate(GtkEntry *entry, search_t *srch)
303 GtkComboBox *combobox;
306 gboolean exist_flag = FALSE;
308 combobox = GTK_COMBO_BOX(srch->combo);
309 new_text = gtk_combo_box_get_active_text(combobox);
310 if(!new_text || strcmp(new_text, "") == 0)
314 S_entry = g_strdup(new_text);
315 // g_print("cb_combo_entry_activate[%s]\n", new_text);
317 for (list = combolist; list; list = g_list_next(list)) {
318 if (strcmp(new_text, (gchar *)list->data) == 0) {
326 combolist = insert_item(combobox, combolist, new_text, 0);
327 len = g_list_length(combolist);
328 if (len > COMBO_LIST_LIMIT) {
329 combolist = remove_item(combobox, combolist, len - 1);
334 search(srch->tran, TRUE);
337 static void cb_btn_prev_clicked(GtkButton *widget , search_t *srch)
339 cb_combo_entry_activate(NULL, srch);
340 search(srch->tran, FALSE);
343 static void cb_btn_next_clicked(GtkButton *widget , search_t *srch)
345 cb_combo_entry_activate(NULL, srch);
346 search(srch->tran, TRUE);
348 /*-------+---------+---------+---------+---------+---------+---------+--------*/
349 GList *insert_item(GtkComboBox *combobox,
350 GList *cmblist, const gchar *item_label, gint index)
352 gtk_combo_box_insert_text(combobox, index, g_strdup(item_label));
353 return g_list_insert(cmblist, g_strdup(item_label), index);
356 GList *remove_item(GtkComboBox *combobox, GList *cmblist, gint index)
358 gtk_combo_box_remove_text(combobox, index);
359 g_free(g_list_nth_data(cmblist, index));
360 return g_list_delete_link(cmblist, g_list_nth(cmblist, index));
363 static gchar *search_title(transition_t *transition)
367 switch((int)transition->current_page) {
368 case CCS_SCREEN_DOMAIN_LIST :
369 if (transition->task_flag) {
370 title = _("Process Search for :");
372 title = _("Domain Search for :");
375 case CCS_SCREEN_ACL_LIST :
376 title = _("ACL Search for :");
378 case CCS_SCREEN_EXCEPTION_LIST :
379 title = _("Exception Search for :");
381 case CCS_SCREEN_PROFILE_LIST :
382 title = _("Profile Search for :");
384 case CCS_SCREEN_MANAGER_LIST :
386 title = _("Search for :");
392 void search_input(GtkAction *action, transition_t *transition)
394 GtkWidget *dialog, *parent;
395 GtkWidget *hbox, *vbox_l, *vbox_r;
398 GtkWidget *btn_next, *btn_prev;
403 parent = (transition->acl_detached &&
404 (int)transition->current_page == CCS_SCREEN_ACL_LIST) ?
405 transition->acl_window : transition->window;
407 dialog = gtk_dialog_new_with_buttons(_("Find"),
409 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
410 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
413 hbox = gtk_hbox_new(FALSE, 5);
414 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
415 hbox, TRUE, TRUE, 0);
416 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
418 vbox_l = gtk_vbox_new(FALSE, 5);
419 gtk_box_pack_start(GTK_BOX(hbox), vbox_l, TRUE, TRUE, 0);
421 label = gtk_label_new(NULL);
422 gtk_label_set_markup(GTK_LABEL(label), search_title(transition));
423 gtk_box_pack_start(GTK_BOX(vbox_l), label, FALSE, FALSE, 0);
425 entry = gtk_combo_box_entry_new_text();
426 srch.tran = transition;
428 g_signal_connect(G_OBJECT(entry) , "changed",
429 G_CALLBACK(cb_combo_changed), NULL);
430 g_signal_connect(G_OBJECT(GTK_BIN(entry)->child) , "activate" ,
431 G_CALLBACK(cb_combo_entry_activate), &srch);
432 gtk_box_pack_start(GTK_BOX(vbox_l), entry, FALSE, FALSE, 0);
434 for (list = combolist; list; list = g_list_next(list)) {
435 gtk_combo_box_append_text(
436 GTK_COMBO_BOX(entry), (gchar *)list->data);
438 gtk_combo_box_set_active(GTK_COMBO_BOX(entry), 0);
440 vbox_r = gtk_vbox_new(FALSE, 5);
441 gtk_box_pack_start(GTK_BOX(hbox), vbox_r, FALSE, FALSE, 0);
443 btn_prev = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
444 gtk_box_pack_start(GTK_BOX(vbox_r), btn_prev, FALSE, FALSE, 0);
445 g_signal_connect(G_OBJECT(btn_prev) , "clicked" ,
446 G_CALLBACK (cb_btn_prev_clicked) , &srch);
448 btn_next = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
449 gtk_box_pack_start(GTK_BOX(vbox_r), btn_next, FALSE, FALSE, 0);
450 g_signal_connect(G_OBJECT(btn_next) , "clicked" ,
451 G_CALLBACK (cb_btn_next_clicked) , &srch);
453 gtk_widget_set_size_request(dialog, 400, -1);
454 put_locate_index(transition, get_current_index(transition));
455 gtk_widget_show_all(dialog);
457 response = gtk_dialog_run(GTK_DIALOG(dialog));
458 gtk_widget_destroy(dialog);
461 void search_back(GtkAction *action, transition_t *transition)
463 search(transition, FALSE);
466 void search_forward(GtkAction *action, transition_t *transition)
468 search(transition, TRUE);
470 /*-------+---------+---------+---------+---------+---------+---------+--------*/