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 2010,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 static void terminate(GtkAction *action, transition_t *transition);
33 static void copy_line(GtkAction *action, transition_t *transition);
34 static void optimize_acl(GtkAction *action, transition_t *transition);
35 static void delete_transition(GtkAction *action, transition_t *transition);
36 static void insert_history_buffer(GtkWidget *view, gchar *entry);
37 static void append_transition(GtkAction *action, transition_t *transition);
38 static void set_transition(GtkAction *action, transition_t *transition);
39 static void manager_transition(GtkAction *action, transition_t *transition);
40 static void memory_transition(GtkAction *action, transition_t *transition);
41 static void show_about_dialog(void);
42 static void Process_state(GtkAction *action, transition_t *transition);
43 static void Detach_acl(GtkAction *action, transition_t *transition);
45 static GtkActionEntry entries[] = {
46 {"FileMenu", NULL, N_("_File")},
47 {"EditMenu", NULL, N_("_Edit")},
48 {"ViewMenu", NULL, N_("_View")},
49 {"HelpMenu", NULL, N_("_Help")},
51 {"Quit", GTK_STOCK_QUIT, N_("_Quit"), "<control>Q",
52 N_("Quit a program"), G_CALLBACK(terminate)},
54 {"Edit", GTK_STOCK_EDIT, N_("S_et"), "<control>E",
55 N_("Set or Edit the selected line"), G_CALLBACK(set_transition)},
56 {"Add", GTK_STOCK_ADD, N_("_Add"), "<control>I",
57 N_("Append line"), G_CALLBACK(append_transition)},
58 {"Delete", GTK_STOCK_DELETE, N_("_Delete"), "<control>D",
59 N_("Delete the selected line"), G_CALLBACK(delete_transition)},
60 {"Copy", GTK_STOCK_COPY, N_("_Copy"), "<control>C",
61 N_("Copy an entry at the cursor position to history buffer"),
62 G_CALLBACK(copy_line)},
63 {"OptimizationSupport", GTK_STOCK_CONVERT,
64 N_("_OptimizationSupport"), "<control>O",
65 N_("Extraction of redundant ACL entries"), G_CALLBACK(optimize_acl)},
67 {"Search", GTK_STOCK_FIND, N_("_Search"), "<control>F",
68 N_("Search for text"), G_CALLBACK(search_input)},
69 {"SearchBack", GTK_STOCK_GO_BACK, N_("Search_Backwards"), "<control><shift>G",
70 N_("Search backwards for the same text"), G_CALLBACK(search_back)},
71 {"SearchFoward", GTK_STOCK_GO_FORWARD, N_("SearchFor_wards"), "<control>G",
72 N_("Search forwards for the same text"), G_CALLBACK(search_forward)},
74 {"Refresh", GTK_STOCK_REFRESH, N_("_Refresh"), "<control>R",
75 N_("Refresh to the latest information"), G_CALLBACK(refresh_transition)},
76 {"Manager", GTK_STOCK_DND, N_("_Manager"), "<control>M",
77 N_("Manager Profile Editor"), G_CALLBACK(manager_transition)},
78 {"Memory", GTK_STOCK_DND, N_("_Statistics"), "<control>S",
79 N_("Statistics"), G_CALLBACK(memory_transition)},
81 {"About", GTK_STOCK_ABOUT, N_("_About"), "<alt>A",
82 N_("About a program"), show_about_dialog}
84 static guint n_entries = G_N_ELEMENTS(entries);
86 static GtkToggleActionEntry toggle_entries[] = {
87 { "Process", GTK_STOCK_MEDIA_RECORD, N_("Process"), "<control>at",
88 N_("Process State Viewer"), G_CALLBACK(Process_state), FALSE},
89 { "ACL", "", N_("Detach ACL"), "",
90 N_("Detach ACL window"), G_CALLBACK(Detach_acl), FALSE},
92 static guint n_toggle_entries = G_N_ELEMENTS(toggle_entries);
94 static const gchar *ui_info =
96 " <menubar name='MenuBar'>"
97 " <menu action='FileMenu'>"
98 " <menuitem action='Quit'/>"
101 " <menu action='EditMenu'>"
102 " <menuitem action='Edit'/>"
103 " <menuitem action='Add'/>"
104 " <menuitem action='Delete'/>"
106 " <menuitem action='Copy'/>"
107 " <menuitem action='OptimizationSupport'/>"
109 " <menuitem action='Search'/>"
110 " <menuitem action='SearchBack'/>"
111 " <menuitem action='SearchFoward'/>"
114 " <menu action='ViewMenu'>"
115 " <menuitem action='Refresh'/>"
117 " <menuitem action='Manager'/>"
118 " <menuitem action='Memory'/>"
120 " <menuitem action='Process'/>"
122 " <menuitem action='ACL'/>"
125 " <menu action='HelpMenu'>"
126 " <menuitem action='About'/>"
130 " <toolbar name='ToolBar'>"
131 " <toolitem action='Quit'/>"
133 " <toolitem action='Edit'/>"
134 " <toolitem action='Add'/>"
135 " <toolitem action='Delete'/>"
137 " <toolitem action='Copy'/>"
138 " <toolitem action='OptimizationSupport'/>"
140 " <toolitem action='Search'/>"
141 " <toolitem action='SearchBack'/>"
142 " <toolitem action='SearchFoward'/>"
144 " <toolitem action='Refresh'/>"
146 " <toolitem action='Process'/>"
148 " <toolitem action='About'/>"
151 " <popup name='PopUp'>"
152 " <menuitem action='Edit'/>"
153 " <menuitem action='Add'/>"
154 " <menuitem action='Delete'/>"
156 " <menuitem action='Copy'/>"
157 " <menuitem action='OptimizationSupport'/>"
161 GtkWidget *create_menu(GtkWidget *parent, transition_t *transition,
165 GtkActionGroup *actions;
166 GtkAccelGroup *accel_group;
167 GError *error = NULL;
170 actions = gtk_action_group_new("Actions");
171 gtk_action_group_set_translation_domain(actions, GETTEXT_PACKAGE);
172 gtk_action_group_add_actions(actions,
173 entries, n_entries, transition);
174 gtk_action_group_add_toggle_actions(actions,
175 toggle_entries, n_toggle_entries, transition);
177 ui = gtk_ui_manager_new();
178 gtk_ui_manager_insert_action_group(ui, actions, 0);
180 gtk_action_set_sensitive(
181 gtk_action_group_get_action(actions, "Search"), TRUE);
182 gtk_action_set_sensitive(
183 gtk_action_group_get_action(actions, "SearchBack"), FALSE);
184 gtk_action_set_sensitive(
185 gtk_action_group_get_action(actions, "SearchFoward"), FALSE);
186 gtk_action_set_sensitive(
187 gtk_action_group_get_action(actions, "Edit"), FALSE);
188 gtk_action_set_sensitive(gtk_action_group_get_action(
189 actions, "OptimizationSupport"), FALSE);
192 gtk_action_set_sensitive(
193 gtk_action_group_get_action(actions, "Process"), FALSE);
194 gtk_action_set_sensitive(
195 gtk_action_group_get_action(actions, "Memory"), FALSE);
198 transition->actions = actions;
200 // gtk_ui_manager_set_add_tearoffs(ui, TRUE);
201 accel_group = gtk_ui_manager_get_accel_group(ui);
202 gtk_window_add_accel_group(GTK_WINDOW(parent), accel_group);
203 if (!gtk_ui_manager_add_ui_from_string(ui, ui_info, -1, NULL)) {
204 g_message ("building menus failed: %s", error->message);
205 g_error_free (error);
208 *toolbar = gtk_ui_manager_get_widget(ui, "/ToolBar");
209 gtk_toolbar_set_style(GTK_TOOLBAR(*toolbar), GTK_TOOLBAR_ICONS);
211 /* to save gpet.c popup_menu() */
212 popup = gtk_ui_manager_get_widget(ui, "/PopUp");
213 g_object_set_data(G_OBJECT(transition->window), "popup", popup);
215 /* to save gpet.c gpet_main() */
216 g_object_set_data(G_OBJECT(transition->window),
217 "AccelGroup", accel_group);
219 return gtk_ui_manager_get_widget(ui, "/MenuBar");
221 /*-------+---------+---------+---------+---------+---------+---------+--------*/
222 void disp_statusbar(transition_t *transition, int scr)
224 gchar *status_str = NULL;
227 case CCS_SCREEN_EXCEPTION_LIST :
228 status_str = g_strdup_printf("Entry[%d]",
229 transition->exp.count);
231 case CCS_SCREEN_PROFILE_LIST :
232 status_str = g_strdup_printf("Entry[%d]",
233 transition->prf.count);
235 case CCS_SCREEN_DOMAIN_LIST :
236 case CCS_SCREEN_ACL_LIST :
237 if (transition->task_flag)
238 status_str = g_strdup_printf("Process[%d] Policy[%d]",
239 transition->tsk.count, transition->acl.count);
241 status_str = g_strdup_printf("Domain[%d] Policy[%d]",
242 transition->domain_count, transition->acl.count);
245 status_str = g_strdup_printf("Domain[%d]",
246 transition->domain_count);
249 g_warning("BUG: screen [%d] file(%s) line(%d)",
250 scr, __FILE__, __LINE__);
253 gtk_statusbar_pop(GTK_STATUSBAR(transition->statusbar),
254 transition->contextid);
255 gtk_statusbar_push(GTK_STATUSBAR(transition->statusbar),
256 transition->contextid, status_str);
259 /*-------+---------+---------+---------+---------+---------+---------+--------*/
260 static void terminate(GtkAction *action, transition_t *transition)
262 write_config(transition);
263 g_object_unref(transition->actions);
266 /*-------+---------+---------+---------+---------+---------+---------+--------*/
267 static gulong handler_id = 0UL;
269 static gboolean cb_destroy_acl(GtkWidget *widget, GdkEvent *event,
270 transition_t *transition)
272 GtkWidget *acl_container;
275 if (handler_id == 0UL)
278 /* get gpet.c main()*/
279 acl_container = g_object_get_data(G_OBJECT(transition->window), "acl_container");
280 paned = g_object_get_data(G_OBJECT(transition->window), "pane");
282 g_signal_handler_disconnect(
283 G_OBJECT(transition->acl_window), handler_id);
286 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(
287 gtk_action_group_get_action(transition->actions, "ACL")), FALSE);
288 transition->acl_detached = FALSE;
290 gtk_widget_reparent(GTK_WIDGET(transition->container),
293 gtk_widget_reparent(GTK_WIDGET(acl_container),
296 transition->current_page = CCS_SCREEN_DOMAIN_LIST;
297 set_sensitive(transition->actions, transition->task_flag,
298 transition->current_page);
299 refresh_transition(NULL, transition);
301 return gtk_widget_hide_on_delete(transition->acl_window);
304 static void Detach_acl(GtkAction *action, transition_t *transition)
306 GtkWidget *acl_container;
308 /* get gpet.c main()*/
309 acl_container = g_object_get_data(G_OBJECT(transition->window), "acl_container");
310 paned = g_object_get_data(G_OBJECT(transition->window), "pane");
312 if (transition->acl_detached) {
313 cb_destroy_acl(NULL, NULL, transition);
315 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(
316 gtk_action_group_get_action(transition->actions, "ACL")), TRUE);
317 transition->acl_detached = TRUE;
318 handler_id = g_signal_connect(
319 G_OBJECT(transition->acl_window), "delete_event",
320 G_CALLBACK(cb_destroy_acl), transition);
321 gtk_widget_reparent(GTK_WIDGET(acl_container),
322 transition->acl_window);
323 gtk_widget_show_all(transition->acl_window);
325 gtk_widget_reparent(GTK_WIDGET(transition->container),
327 transition->current_page = CCS_SCREEN_ACL_LIST;
328 set_sensitive(transition->actions, transition->task_flag,
329 transition->current_page);
330 refresh_transition(NULL, transition);
333 /*-------+---------+---------+---------+---------+---------+---------+--------*/
334 static void Process_state(GtkAction *action, transition_t *transition)
341 view = transition->task_flag ?
342 transition->tsk.treeview : transition->treeview;
343 gtk_container_remove(transition->container, view);
346 transition->task_flag = ~transition->task_flag & 1;
348 if (transition->task_flag) {
349 label_str = _("Process State");
350 view = transition->tsk.treeview;
352 label_str = _("Domain Transition");
353 view = transition->treeview;
356 /* from gpet.c:gpet_main()*/
357 notebook = g_object_get_data(
358 G_OBJECT(transition->window), "notebook");
359 tab1 = g_object_get_data(
360 G_OBJECT(transition->window), "tab1");
361 gtk_notebook_set_tab_label_text(
362 GTK_NOTEBOOK(notebook), tab1, label_str);
363 gtk_notebook_set_menu_label_text(
364 GTK_NOTEBOOK(notebook), tab1, label_str);
366 gtk_container_add(transition->container, view);
368 transition->current_page = CCS_SCREEN_DOMAIN_LIST;
369 set_sensitive(transition->actions, transition->task_flag,
370 transition->current_page);
371 if (transition->acl_detached)
372 g_object_set(G_OBJECT(notebook), "can-focus", FALSE, NULL);
374 g_object_set(G_OBJECT(notebook), "can-focus", TRUE, NULL);
375 refresh_transition(action, transition);
377 /*-------+---------+---------+---------+---------+---------+---------+--------*/
378 void view_cursor_set(GtkWidget *view,
379 GtkTreePath *path, GtkTreeViewColumn *column)
382 path = gtk_tree_path_new_from_indices(0, -1);
386 gchar *path_str = gtk_tree_path_to_string(path);
387 DEBUG_PRINT("<%s> TreePath[%s]\n",
388 g_type_name(G_OBJECT_TYPE(view)), path_str);
391 gtk_tree_view_set_cursor(
392 GTK_TREE_VIEW(view), path, column, FALSE);
393 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(view), path, NULL,
394 TRUE, 0.5, 0.0); // ksn
395 gtk_tree_path_free(path);
398 void refresh_transition(GtkAction *action, transition_t *transition)
400 GtkTreePath *path = NULL;
401 GtkTreeViewColumn *column = NULL;
402 GtkWidget *view = NULL;
404 DEBUG_PRINT("In Refresh Page[%d]\n", (int)transition->current_page);
405 switch((int)transition->current_page) {
406 case CCS_SCREEN_EXCEPTION_LIST :
407 view = transition->exp.listview;
408 gtk_tree_view_get_cursor(GTK_TREE_VIEW(view),
410 if (get_exception_policy(
411 &(transition->exp.list), &(transition->exp.count)))
413 add_list_data(&(transition->exp), TRUE);
414 set_position_addentry(transition, &path);
415 disp_statusbar(transition, CCS_SCREEN_EXCEPTION_LIST);
416 view_cursor_set(view, path, column);
417 gtk_widget_grab_focus(view);
419 case CCS_SCREEN_PROFILE_LIST :
420 view = transition->prf.listview;
421 gtk_tree_view_get_cursor(GTK_TREE_VIEW(view),
424 &(transition->prf.list), &(transition->prf.count)))
426 add_list_data(&(transition->prf), FALSE);
427 set_position_addentry(transition, &path);
428 disp_statusbar(transition, CCS_SCREEN_PROFILE_LIST);
429 view_cursor_set(view, path, column);
430 gtk_widget_grab_focus(view);
432 case CCS_SCREEN_DOMAIN_LIST :
434 view = transition->task_flag ?
435 transition->tsk.treeview : transition->treeview;
436 gtk_tree_view_get_cursor(
437 GTK_TREE_VIEW(view), &path, &column);
439 gtk_widget_hide(view);
440 if (transition->task_flag) {
441 if (get_task_list(&(transition->tsk.task),
442 &(transition->tsk.count)))
444 add_task_tree_data(GTK_TREE_VIEW(view), &(transition->tsk));
445 gtk_tree_view_expand_all(GTK_TREE_VIEW(view));
447 if (get_domain_policy(
448 transition->dp, &(transition->domain_count)))
450 add_tree_data(GTK_TREE_VIEW(view), transition->dp);
451 gtk_tree_view_expand_all(GTK_TREE_VIEW(view));
452 set_position_addentry(transition, &path);
455 view_cursor_set(view, path, column);
456 gtk_widget_show(view);
458 if (transition->acl_detached) { // get focus
459 gtk_widget_grab_focus(transition->acl.listview);
460 DEBUG_PRINT("☆Transition[%d] Acl[%d]\n",
461 gtk_window_has_toplevel_focus(GTK_WINDOW(transition->window)),
462 gtk_window_has_toplevel_focus(GTK_WINDOW(transition->acl_window)));
464 gtk_widget_grab_focus(view);
467 gtk_widget_grab_focus(view);
469 case CCS_SCREEN_ACL_LIST :
470 view = transition->task_flag ?
471 transition->tsk.treeview : transition->treeview;
472 gtk_tree_view_get_cursor(
473 GTK_TREE_VIEW(view), &path, &column);
474 view_cursor_set(view, path, column);
475 gtk_widget_grab_focus(transition->acl.listview);
479 if (transition->acl_detached) {
480 DEBUG_PRINT("★Transition[%d] Acl[%d]\n",
481 gtk_window_has_toplevel_focus(GTK_WINDOW(transition->window)),
482 gtk_window_has_toplevel_focus(GTK_WINDOW(transition->acl_window)));
484 DEBUG_PRINT("Out Refresh Page[%d]\n", (int)transition->current_page);
486 /*-------+---------+---------+---------+---------+---------+---------+--------*/
487 static void copy_line(GtkAction *action, transition_t *transition)
492 switch((int)transition->current_page) {
493 case CCS_SCREEN_DOMAIN_LIST :
494 index = get_current_domain_index(transition);
495 view = transition->treeview;
497 insert_history_buffer(view, g_strdup(
498 ccs_domain_name(transition->dp, index)));
500 case CCS_SCREEN_ACL_LIST :
501 view = transition->acl.listview;
502 insert_history_buffer(view, get_alias_and_operand(view));
504 case CCS_SCREEN_EXCEPTION_LIST :
505 view = transition->exp.listview;
506 insert_history_buffer(view, get_alias_and_operand(view));
512 /*-------+---------+---------+---------+---------+---------+---------+--------*/
513 static void optimize_acl(GtkAction *action, transition_t *transition)
515 GtkTreeSelection *selection;
519 if ((int)transition->current_page != CCS_SCREEN_ACL_LIST)
522 selection = gtk_tree_view_get_selection(
523 GTK_TREE_VIEW(transition->acl.listview));
524 if (!selection || !(count =
525 gtk_tree_selection_count_selected_rows(selection)))
527 DEBUG_PRINT("count[%d]\n", count);
530 current = select_list_line(&(transition->acl));
534 get_optimize_acl_list(current,
535 &(transition->acl.list), transition->acl.count);
537 model = gtk_tree_view_get_model(GTK_TREE_VIEW(transition->acl.listview));
538 gtk_tree_model_foreach(model,
539 (GtkTreeModelForeachFunc)disp_acl_line, &(transition->acl));
543 GtkTreePath *start_path, *end_path;
544 gint start_index, end_index;
546 start_path = end_path = gtk_tree_path_new_from_indices(current, -1);
547 gtk_tree_selection_unselect_range(selection, start_path, end_path);
549 for (index = 0; index < transition->acl.count; index++) {
550 if (transition->acl.list[index].selected)
554 start_path = gtk_tree_path_new_from_indices(index, -1);
555 start_index = end_index = index;
557 for ( ; index < transition->acl.count; index++) {
559 // 対象データ:ccs_gacl_list[index].selected == 1;
560 if (!transition->acl.list[index].selected)
564 if (index == (start_index + count)) {
565 end_path = gtk_tree_path_new_from_indices(index, -1);
569 end_path = start_path;
572 g_print("start[%d] end[%d]\n", start_index, end_index);
573 gtk_tree_selection_select_range(
574 selection, start_path, end_path);
576 start_path = gtk_tree_path_new_from_indices(index, -1);
584 g_print("start[%d] end[%d]\n", start_index, end_index);
585 gtk_tree_selection_select_range(selection, start_path, end_path);
589 /*-------+---------+---------+---------+---------+---------+---------+--------*/
590 static gint message_dialog(GtkWidget *parent, gchar *message)
595 dialog = gtk_message_dialog_new_with_markup(GTK_WINDOW(parent) ,
596 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
597 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
599 gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), message);
601 result = gtk_dialog_run(GTK_DIALOG(dialog));
602 gtk_widget_destroy(dialog);
607 static void delete_transition(GtkAction *action, transition_t *transition)
609 GtkTreeSelection *selection;
612 gchar *message = NULL;
615 DEBUG_PRINT("delete : current page[%d]\n", transition->current_page);
617 switch((int)transition->current_page) {
618 case CCS_SCREEN_EXCEPTION_LIST :
619 selection = gtk_tree_view_get_selection(
620 GTK_TREE_VIEW(transition->exp.listview));
621 if (!selection || !(count =
622 gtk_tree_selection_count_selected_rows(selection)))
624 DEBUG_PRINT("count[%d]\n", count);
625 message = count > 1 ?
627 "<span foreground='red' size='x-large'>"
628 "<b>Delete</b> the %d selected exception policies?</span>"), count) :
630 "<span foreground='red' size='x-large'>"
631 "<b>Delete</b> the selected exception policy?</span>"));
632 if (GTK_RESPONSE_YES ==
633 message_dialog(transition->window, message)) {
634 ret = delete_exp(transition, selection, count);
637 case CCS_SCREEN_DOMAIN_LIST :
639 selection = gtk_tree_view_get_selection(
640 GTK_TREE_VIEW(transition->treeview));
641 if (!selection || !(count =
642 gtk_tree_selection_count_selected_rows(selection)))
644 DEBUG_PRINT("count[%d]\n", count);
645 message = count > 1 ?
647 "<span foreground='red' size='x-large'>"
648 "<b>Delete</b> the %d selected domains?</span>"), count) :
650 "<span foreground='red' size='x-large'>"
651 "<b>Delete</b> the selected domain?</span>"));
652 if (GTK_RESPONSE_YES ==
653 message_dialog(transition->window, message)) {
654 ret = delete_domain(transition, selection, count);
657 case CCS_SCREEN_ACL_LIST :
658 DEBUG_PRINT("Delete ACL\n");
659 selection = gtk_tree_view_get_selection(
660 GTK_TREE_VIEW(transition->acl.listview));
661 if (!selection || !(count =
662 gtk_tree_selection_count_selected_rows(selection)))
664 DEBUG_PRINT("count[%d]\n", count);
665 parent = transition->acl_detached ?
666 transition->acl_window : transition->window;
667 message = count > 1 ?
669 "<span foreground='blue' size='x-large'>"
670 "<b>Delete</b> the %d selected policies?</span>"), count) :
672 "<span foreground='blue' size='x-large'>"
673 "<b>Delete</b> the selected policy?</span>"));
674 if (GTK_RESPONSE_YES ==
675 message_dialog(parent, message)) {
676 ret = delete_acl(transition, selection, count);
679 case CCS_SCREEN_MANAGER_LIST :
680 DEBUG_PRINT("Delete manager\n");
687 refresh_transition(NULL, transition);
689 /*-------+---------+---------+---------+---------+---------+---------+--------*/
690 #define COMBO_LIST_LIMIT 20
691 static GList *combolist = NULL;
692 static gchar *Last_entry_string = NULL;
694 gchar *get_combo_entry_last(void)
696 return Last_entry_string;
699 static void insert_history_buffer(GtkWidget *view, gchar *entry)
701 GtkClipboard *clipboard;
708 /* to system clipboard */
709 clipboard = gtk_widget_get_clipboard(view, GDK_SELECTION_CLIPBOARD);
710 gtk_clipboard_set_text(clipboard, entry, -1);
712 for (list = combolist; list; list = g_list_next(list)) {
713 if (strcmp(entry, (gchar *)list->data) == 0)
717 combolist = g_list_insert(combolist, entry, 0);
718 len = g_list_length(combolist);
719 if (len > COMBO_LIST_LIMIT) {
720 g_free(g_list_nth_data(combolist, len - 1));
721 combolist = g_list_delete_link(combolist,
722 g_list_nth(combolist, len - 1));
726 static gint combo_entry_activate(GtkWidget *combo, gchar **new_text)
728 GtkComboBox *combobox;
730 gboolean exist_flag = FALSE;
732 combobox = GTK_COMBO_BOX(combo);
733 (*new_text) = gtk_combo_box_get_active_text(combobox);
734 if(!(*new_text) || strcmp(*new_text, "") == 0)
737 g_free(Last_entry_string);
738 Last_entry_string = g_strdup(*new_text);
740 for (list = combolist; list; list = g_list_next(list)) {
741 if (strcmp(*new_text, (gchar *)list->data) == 0) {
749 combolist = insert_item(combobox, combolist, *new_text, 0);
750 len = g_list_length(combolist);
751 if (len > COMBO_LIST_LIMIT) {
752 combolist = remove_item(combobox, combolist, len - 1);
754 DEBUG_PRINT("Append '%s'.\n", *new_text);
757 return exist_flag ? 1 : 0;
760 static gint append_dialog(transition_t *transition,
761 gchar *title, gchar **input)
763 GtkWidget *dialog, *parent;
766 gint response, result = 1;
768 parent = (transition->acl_detached &&
769 (int)transition->current_page == CCS_SCREEN_ACL_LIST) ?
770 transition->acl_window : transition->window;
772 dialog = gtk_dialog_new_with_buttons(title,
774 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
775 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
776 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
779 combo = gtk_combo_box_entry_new_text();
780 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
782 for (list = combolist; list; list = g_list_next(list)) {
783 gtk_combo_box_append_text(
784 GTK_COMBO_BOX(combo), (gchar *)list->data);
788 GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), combo);
789 gtk_widget_set_size_request(dialog, 600, -1);
790 gtk_widget_show_all(dialog);
792 response = gtk_dialog_run(GTK_DIALOG(dialog));
793 if (response == GTK_RESPONSE_APPLY) {
794 combo_entry_activate(combo, input);
797 gtk_widget_destroy(dialog);
802 static void append_transition(GtkAction *action, transition_t *transition)
805 gint index, result = 1;
806 enum addentry_type type = ADDENTRY_NON;
807 char *err_buff = NULL;
809 switch((int)transition->current_page) {
810 case CCS_SCREEN_DOMAIN_LIST :
811 DEBUG_PRINT("append domain\n");
812 result = append_dialog(transition,
813 _("Add Domain"), &input);
815 result = add_domain(input, &err_buff);
816 type = ADDENTRY_DOMAIN_LIST;
818 case CCS_SCREEN_ACL_LIST :
819 DEBUG_PRINT("append acl\n");
820 result = append_dialog(transition, _("Add Acl"), &input);
822 index = get_current_domain_index(transition);
824 result = add_acl_list(transition->dp,
825 index, input, &err_buff);
827 type = ADDENTRY_ACL_LIST;
829 case CCS_SCREEN_EXCEPTION_LIST :
830 DEBUG_PRINT("append exception\n");
831 result = append_dialog(transition,
832 _("Add Exception"), &input);
834 result = add_exception_policy(input, &err_buff);
835 type = ADDENTRY_EXCEPTION_LIST;
837 case CCS_SCREEN_PROFILE_LIST :
838 DEBUG_PRINT("append profile\n");
839 result = append_dialog(transition,
840 _("Add Profile (0 - 255)"), &input);
842 result = add_profile(input, &err_buff);
843 type = ADDENTRY_PROFILE_LIST;
846 DEBUG_PRINT("append ???\n");
852 g_warning("%s", err_buff);
855 transition->addentry = ADDENTRY_NON;
857 transition->addentry = type;
858 refresh_transition(NULL, transition);
861 /*-------+---------+---------+---------+---------+---------+---------+--------*/
862 enum list_column_pos {
863 LIST_PROFILE, // display
864 PROFILE_NO, // hidden
867 static GtkWidget *create_list_profile(void)
870 GtkListStore *liststore;
871 GtkCellRenderer *renderer;
872 GtkTreeViewColumn *column;
874 liststore = gtk_list_store_new(N_COLUMNS_LIST,
875 G_TYPE_STRING, G_TYPE_UINT);
876 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));
877 g_object_unref(liststore);
879 renderer = gtk_cell_renderer_text_new();
880 column = gtk_tree_view_column_new();
881 gtk_tree_view_column_pack_start(column, renderer, FALSE);
882 column = gtk_tree_view_column_new_with_attributes("operand",
883 renderer, "text", LIST_PROFILE, NULL);
884 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
887 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
892 static void add_list_profile(GtkWidget *listview,
893 generic_list_t *generic)
900 store = GTK_LIST_STORE(gtk_tree_view_get_model(
901 GTK_TREE_VIEW(listview)));
902 gtk_list_store_clear(store);
904 DEBUG_PRINT("Count[%d]\n", generic->count);
905 for(i = 0; i < generic->count; i++){
906 if (generic->list[i].directive > 255 ||
907 !strstr(generic->list[i].operand, "COMMENT="))
910 DEBUG_PRINT("[%3d]:%3u-%s\n", i,
911 generic->list[i].directive, generic->list[i].operand);
913 gtk_list_store_append(store, &iter);
914 profile = g_strdup_printf("%3u-%s",
915 generic->list[i].directive,
916 generic->list[i].operand);
917 gtk_list_store_set(store, &iter,
918 LIST_PROFILE, profile,
919 PROFILE_NO, generic->list[i].directive,
925 static gboolean apply_profile(transition_t *transition,
926 GtkTreeSelection *domain_selection,
929 GtkTreeSelection *selection;
936 selection = gtk_tree_view_get_selection(
937 GTK_TREE_VIEW(listview));
939 gtk_tree_selection_count_selected_rows(selection) == 1) {
940 list = gtk_tree_selection_get_selected_rows(
942 model = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
943 gtk_tree_model_get_iter(model, &iter,
944 g_list_first(list)->data);
945 gtk_tree_model_get(model, &iter,
946 PROFILE_NO, &profile_no, -1);
947 DEBUG_PRINT("Selct Profile [%u]\n", profile_no);
948 result = set_domain_profile(transition,
949 domain_selection, profile_no);
951 refresh_transition(NULL, transition);
958 static void set_domain(transition_t *transition)
960 GtkTreeSelection *domain_selection;
963 gint count, response;
965 if (transition->task_flag)
966 domain_selection = gtk_tree_view_get_selection(
967 GTK_TREE_VIEW(transition->tsk.treeview));
969 domain_selection = gtk_tree_view_get_selection(
970 GTK_TREE_VIEW(transition->treeview));
972 if (!domain_selection || !(count =
973 gtk_tree_selection_count_selected_rows(domain_selection)))
976 dialog = gtk_dialog_new_with_buttons(_("Profile list"),
977 GTK_WINDOW(transition->window),
978 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
979 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
980 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
983 listview = create_list_profile();
984 add_list_profile(listview, &(transition->prf));
985 view_cursor_set(listview, NULL, NULL);
987 GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), listview);
988 gtk_widget_set_size_request(dialog, 400, 300);
989 gtk_widget_set_name(dialog, "GpetProfileSelectDialog"); // .gpetrc
990 gtk_widget_show_all(dialog);
993 response = gtk_dialog_run(GTK_DIALOG(dialog));
994 if (response == GTK_RESPONSE_APPLY) {
995 DEBUG_PRINT("Apply button was pressed.\n");
996 if (!apply_profile(transition,
997 domain_selection, listview))
999 } else if (response == GTK_RESPONSE_CANCEL) {
1000 DEBUG_PRINT("Cancel button was pressed.\n");
1002 DEBUG_PRINT("Another response was recieved.\n");
1004 gtk_widget_destroy(dialog);
1007 static void edit_profile(transition_t *transition)
1013 gchar *profile, *label_str, *ptr;
1014 gint index, response;
1016 if ((index = select_list_line(&(transition->prf))) < 0)
1019 entry = gtk_entry_new();
1021 profile = g_strdup(transition->prf.list[index].operand);
1022 ptr = strchr(profile, '=');
1026 gtk_entry_set_text(GTK_ENTRY(entry), ptr);
1030 if (transition->prf.list[index].directive < 256)
1031 label_str = g_strdup_printf("%3u-%s",
1032 transition->prf.list[index].directive, profile);
1034 label_str = g_strdup_printf("%s", profile);
1035 label = gtk_label_new(label_str);
1039 dialog = gtk_dialog_new_with_buttons(_("Profile Edit"),
1040 GTK_WINDOW(transition->window),
1041 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1042 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1043 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
1046 hbox = gtk_hbox_new(FALSE, 5);
1048 GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
1050 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1051 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1053 gtk_widget_set_size_request(dialog, 600, -1);
1054 gtk_widget_show_all(dialog);
1056 response = gtk_dialog_run(GTK_DIALOG(dialog));
1057 if (response == GTK_RESPONSE_APPLY) {
1059 char *err_buff = NULL;
1060 // transition->prf.list[index].selected = 1;
1061 input = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
1062 DEBUG_PRINT("Edit profile [%s] index[%d]\n", input, index);
1063 if (set_profile_level(index, input, &err_buff)) {
1064 g_warning("%s", err_buff);
1067 refresh_transition(NULL, transition);
1070 gtk_widget_destroy(dialog);
1073 static void set_transition(GtkAction *action, transition_t *transition)
1075 switch((int)transition->current_page) {
1076 case CCS_SCREEN_DOMAIN_LIST :
1077 DEBUG_PRINT("set profile\n");
1078 set_domain(transition);
1080 case CCS_SCREEN_PROFILE_LIST :
1081 DEBUG_PRINT("edit profile\n");
1082 edit_profile(transition);
1085 DEBUG_PRINT("edit ???\n");
1089 /*-------+---------+---------+---------+---------+---------+---------+--------*/
1090 static void manager_transition(GtkAction *action, transition_t *transition)
1092 manager_main(transition);
1095 static void memory_transition(GtkAction *action, transition_t *transition)
1097 memory_main(transition);
1099 /*-------+---------+---------+---------+---------+---------+---------+--------*/
1100 GdkPixbuf *get_png_file(void)
1105 pixbuf = gdk_pixbuf_new_from_file(PACKAGE_ICON_DIR "/tomoyo.png", &err);
1107 g_warning("%s", err->message);
1113 static void show_about_dialog(void)
1116 GtkAboutDialog *about;
1119 const gchar *authors[] = {
1120 _("Yoshihiro Kusuno <yocto@users.sourceforge.jp>"),
1121 _("ccstools --- kumaneko san"),
1123 const gchar *documenters[] = {_("Yoshihiro Kusuno"), NULL};
1124 // const gchar *translators = "none";
1126 dialog = gtk_about_dialog_new();
1127 about = GTK_ABOUT_DIALOG(dialog);
1128 gtk_about_dialog_set_name(about, "gpet");
1129 gtk_about_dialog_set_authors(about, authors);
1130 gtk_about_dialog_set_documenters(about, documenters);
1131 // gtk_about_dialog_set_translator_credits(about, translators);
1132 gtk_about_dialog_set_version(about, VERSION);
1133 gtk_about_dialog_set_copyright(about,
1134 "Copyright(C) 2010,2011 TOMOYO Linux Project");
1135 gtk_about_dialog_set_comments(about,
1136 "Gui Policy Editor for TOMOYO Linux 1.8"
1138 "\n(based on ccs-editpolicy:ccstools)");
1139 gtk_about_dialog_set_website(about, "http://sourceforge.jp/projects/gpet/");
1140 // gtk_about_dialog_set_website_label(about, "http://tomoyo.sourceforge.jp/");
1142 pixbuf = get_png_file();
1144 gtk_about_dialog_set_logo(about, pixbuf);
1145 g_object_unref(pixbuf);
1147 gtk_container_set_border_width(GTK_CONTAINER(dialog), 5);
1149 gtk_dialog_run(GTK_DIALOG(about));
1150 gtk_widget_destroy(GTK_WIDGET(about));