Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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 |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 19 | import sys |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 20 | from gi.repository import Gdk, Gtk |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 21 | |
| 22 | ## |
| 23 | ## I18N |
| 24 | ## |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 25 | PROGNAME = "policycoreutils" |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 26 | try: |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 27 | import gettext |
| 28 | kwargs = {} |
| 29 | if sys.version_info < (3,): |
| 30 | kwargs['unicode'] = True |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 31 | gettext.install(PROGNAME, |
| 32 | localedir="/usr/share/locale", |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 33 | codeset='utf-8', |
| 34 | **kwargs) |
| 35 | except: |
| 36 | try: |
| 37 | import builtins |
| 38 | builtins.__dict__['_'] = str |
| 39 | except ImportError: |
| 40 | import __builtin__ |
| 41 | __builtin__.__dict__['_'] = unicode |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 42 | |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 43 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 44 | def idle_func(): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 45 | while Gtk.events_pending(): |
| 46 | Gtk.main_iteration() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 47 | |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 48 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 49 | class semanagePage: |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 50 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 51 | def __init__(self, xml, name, description): |
| 52 | self.xml = xml |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 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) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 56 | |
| 57 | self.local = False |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 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) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 61 | self.filter_entry.connect("focus_out_event", self.filter_changed) |
| 62 | self.filter_entry.connect("activate", self.filter_changed) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 63 | self.filter_entry.connect("changed", self.filter_changed) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 64 | |
| 65 | self.view.connect("row_activated", self.rowActivated) |
| 66 | self.view.get_selection().connect("changed", self.itemSelected) |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 67 | self.description = description |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 80 | def itemSelected(self, selection): |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 81 | return |
| 82 | |
| 83 | def filter_changed(self, *arg): |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 84 | filter = arg[0].get_text() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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] |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 90 | val = model.get_value(i, sort_col) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 91 | if val.lower().startswith(key.lower()): |
| 92 | return False |
| 93 | return True |
| 94 | |
| 95 | def match(self, target, filter): |
| 96 | try: |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 97 | f = filter.lower() |
| 98 | t = target.lower() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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 | |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 108 | def verify(self, message, title=""): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 109 | dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, |
| 110 | Gtk.ButtonsType.YES_NO, |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 111 | message) |
| 112 | dlg.set_title(title) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 113 | dlg.set_position(Gtk.WindowPosition.MOUSE) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 114 | dlg.show_all() |
| 115 | rc = dlg.run() |
| 116 | dlg.destroy() |
| 117 | return rc |
| 118 | |
| 119 | def error(self, message): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 120 | dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, |
| 121 | Gtk.ButtonsType.CLOSE, |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 122 | message) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 123 | dlg.set_position(Gtk.WindowPosition.MOUSE) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 124 | dlg.show_all() |
| 125 | dlg.run() |
| 126 | dlg.destroy() |
| 127 | |
| 128 | def deleteDialog(self): |
Vit Mojzis | 530904e | 2016-10-19 14:36:03 +0200 | [diff] [blame] | 129 | store, it = self.view.get_selection().get_selected() |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 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): |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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)) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 139 | self.dialog.set_position(Gtk.WindowPosition.MOUSE) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 140 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 141 | while self.dialog.run() == Gtk.ResponseType.OK: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 142 | try: |
Vit Mojzis | 6f4b1bb | 2018-02-22 14:29:33 +0100 | [diff] [blame] | 143 | if self.add() is False: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 144 | continue |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 145 | break |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 146 | except ValueError as e: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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)) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 153 | self.dialog.set_position(Gtk.WindowPosition.MOUSE) |
| 154 | while self.dialog.run() == Gtk.ResponseType.OK: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 155 | try: |
Vit Mojzis | 6f4b1bb | 2018-02-22 14:29:33 +0100 | [diff] [blame] | 156 | if self.modify() is False: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 157 | continue |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 158 | break |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 159 | except ValueError as e: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 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 |