• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = "policycoreutils"
26try:
27    import gettext
28    kwargs = {}
29    if sys.version_info < (3,):
30        kwargs['unicode'] = True
31    gettext.install(PROGNAME,
32                    localedir="/usr/share/locale",
33                    codeset='utf-8',
34                    **kwargs)
35except:
36    try:
37        import builtins
38        builtins.__dict__['_'] = str
39    except ImportError:
40        import __builtin__
41        __builtin__.__dict__['_'] = unicode
42
43
44def idle_func():
45    while Gtk.events_pending():
46        Gtk.main_iteration()
47
48
49class semanagePage:
50
51    def __init__(self, xml, name, description):
52        self.xml = xml
53        self.window = self.xml.get_object("mainWindow").get_root_window()
54        self.busy_cursor = Gdk.Cursor.new(Gdk.CursorType.WATCH)
55        self.ready_cursor = Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR)
56
57        self.local = False
58        self.view = xml.get_object("%sView" % name)
59        self.dialog = xml.get_object("%sDialog" % name)
60        self.filter_entry = xml.get_object("%sFilterEntry" % name)
61        self.filter_entry.connect("focus_out_event", self.filter_changed)
62        self.filter_entry.connect("activate", self.filter_changed)
63        self.filter_entry.connect("changed", self.filter_changed)
64
65        self.view.connect("row_activated", self.rowActivated)
66        self.view.get_selection().connect("changed", self.itemSelected)
67        self.description = description
68
69    def wait(self):
70        self.window.set_cursor(self.busy_cursor)
71        idle_func()
72
73    def ready(self):
74        self.window.set_cursor(self.ready_cursor)
75        idle_func()
76
77    def get_description(self):
78        return self.description
79
80    def itemSelected(self, selection):
81        return
82
83    def filter_changed(self, *arg):
84        filter = arg[0].get_text()
85        if filter != self.filter:
86            self.load(filter)
87
88    def search(self, model, col, key, i):
89        sort_col = self.store.get_sort_column_id()[0]
90        val = model.get_value(i, sort_col)
91        if val.lower().startswith(key.lower()):
92            return False
93        return True
94
95    def match(self, target, filter):
96        try:
97            f = filter.lower()
98            t = target.lower()
99            if t.find(f) >= 0:
100                return True
101        except:
102            pass
103        return False
104
105    def rowActivated(self, view, row, Column):
106        self.propertiesDialog()
107
108    def verify(self, message, title=""):
109        dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
110                                Gtk.ButtonsType.YES_NO,
111                                message)
112        dlg.set_title(title)
113        dlg.set_position(Gtk.WindowPosition.MOUSE)
114        dlg.show_all()
115        rc = dlg.run()
116        dlg.destroy()
117        return rc
118
119    def error(self, message):
120        dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR,
121                                Gtk.ButtonsType.CLOSE,
122                                message)
123        dlg.set_position(Gtk.WindowPosition.MOUSE)
124        dlg.show_all()
125        dlg.run()
126        dlg.destroy()
127
128    def deleteDialog(self):
129        store, it = self.view.get_selection().get_selected()
130        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):
131            self.delete()
132
133    def use_menus(self):
134        return True
135
136    def addDialog(self):
137        self.dialogClear()
138        self.dialog.set_title(_("Add %s" % self.description))
139        self.dialog.set_position(Gtk.WindowPosition.MOUSE)
140
141        while self.dialog.run() == Gtk.ResponseType.OK:
142            try:
143                if self.add() is False:
144                    continue
145                break
146            except ValueError as e:
147                self.error(e.args[0])
148        self.dialog.hide()
149
150    def propertiesDialog(self):
151        self.dialogInit()
152        self.dialog.set_title(_("Modify %s" % self.description))
153        self.dialog.set_position(Gtk.WindowPosition.MOUSE)
154        while self.dialog.run() == Gtk.ResponseType.OK:
155            try:
156                if self.modify() is False:
157                    continue
158                break
159            except ValueError as e:
160                self.error(e.args[0])
161        self.dialog.hide()
162
163    def on_local_clicked(self, button):
164        self.local = not self.local
165        if self.local:
166            button.set_label(_("all"))
167        else:
168            button.set_label(_("Customized"))
169
170        self.load(self.filter)
171        return True
172