xref: /aosp_15_r20/external/selinux/gui/semanagePage.py (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1## semanagePage.py - show selinux mappings
2## Copyright (C) 2006 Red Hat, Inc.
3
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18## Author: Dan Walsh
19import sys
20from gi.repository import Gdk, Gtk
21
22##
23## I18N
24##
25PROGNAME = "selinux-gui"
26try:
27    import gettext
28    kwargs = {}
29    if sys.version_info < (3,):
30        kwargs['unicode'] = True
31    t = gettext.translation(PROGNAME,
32                    localedir="/usr/share/locale",
33                    **kwargs,
34                    fallback=True)
35    _ = t.gettext
36except:
37    try:
38        import builtins
39        builtins.__dict__['_'] = str
40    except ImportError:
41        import __builtin__
42        __builtin__.__dict__['_'] = unicode
43
44
45def idle_func():
46    while Gtk.events_pending():
47        Gtk.main_iteration()
48
49
50class semanagePage:
51
52    def __init__(self, xml, name, description):
53        self.xml = xml
54        self.window = self.xml.get_object("mainWindow").get_root_window()
55        self.busy_cursor = Gdk.Cursor.new(Gdk.CursorType.WATCH)
56        self.ready_cursor = Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR)
57
58        self.local = False
59        self.view = xml.get_object("%sView" % name)
60        self.dialog = xml.get_object("%sDialog" % name)
61        self.filter_entry = xml.get_object("%sFilterEntry" % name)
62        self.filter_entry.connect("focus_out_event", self.filter_changed)
63        self.filter_entry.connect("activate", self.filter_changed)
64        self.filter_entry.connect("changed", self.filter_changed)
65
66        self.view.connect("row_activated", self.rowActivated)
67        self.view.get_selection().connect("changed", self.itemSelected)
68        self.description = description
69
70    def wait(self):
71        self.window.set_cursor(self.busy_cursor)
72        idle_func()
73
74    def ready(self):
75        self.window.set_cursor(self.ready_cursor)
76        idle_func()
77
78    def get_description(self):
79        return self.description
80
81    def itemSelected(self, selection):
82        return
83
84    def filter_changed(self, *arg):
85        filter = arg[0].get_text()
86        if filter != self.filter:
87            self.load(filter)
88
89    def search(self, model, col, key, i):
90        sort_col = self.store.get_sort_column_id()[0]
91        val = model.get_value(i, sort_col)
92        if val.lower().startswith(key.lower()):
93            return False
94        return True
95
96    def match(self, target, filter):
97        try:
98            f = filter.lower()
99            t = target.lower()
100            if t.find(f) >= 0:
101                return True
102        except:
103            pass
104        return False
105
106    def rowActivated(self, view, row, Column):
107        self.propertiesDialog()
108
109    def verify(self, message, title=""):
110        dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
111                                Gtk.ButtonsType.YES_NO,
112                                message)
113        dlg.set_title(title)
114        dlg.set_position(Gtk.WindowPosition.MOUSE)
115        dlg.show_all()
116        rc = dlg.run()
117        dlg.destroy()
118        return rc
119
120    def error(self, message):
121        dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR,
122                                Gtk.ButtonsType.CLOSE,
123                                message)
124        dlg.set_position(Gtk.WindowPosition.MOUSE)
125        dlg.show_all()
126        dlg.run()
127        dlg.destroy()
128
129    def deleteDialog(self):
130        store, it = self.view.get_selection().get_selected()
131        if (it is not None) and (self.verify(_("Are you sure you want to delete %s '%s'?" % (self.description, store.get_value(it, 0))), _("Delete %s" % self.description)) == Gtk.ResponseType.YES):
132            self.delete()
133
134    def use_menus(self):
135        return True
136
137    def addDialog(self):
138        self.dialogClear()
139        self.dialog.set_title(_("Add %s" % self.description))
140        self.dialog.set_position(Gtk.WindowPosition.MOUSE)
141
142        while self.dialog.run() == Gtk.ResponseType.OK:
143            try:
144                if self.add() is False:
145                    continue
146                break
147            except ValueError as e:
148                self.error(e.args[0])
149        self.dialog.hide()
150
151    def propertiesDialog(self):
152        self.dialogInit()
153        self.dialog.set_title(_("Modify %s" % self.description))
154        self.dialog.set_position(Gtk.WindowPosition.MOUSE)
155        while self.dialog.run() == Gtk.ResponseType.OK:
156            try:
157                if self.modify() is False:
158                    continue
159                break
160            except ValueError as e:
161                self.error(e.args[0])
162        self.dialog.hide()
163
164    def on_local_clicked(self, button):
165        self.local = not self.local
166        if self.local:
167            button.set_label(_("all"))
168        else:
169            button.set_label(_("Customized"))
170
171        self.load(self.filter)
172        return True
173