blob: 4127804fbbeec2598c965db66afe1e123d3f324b [file] [log] [blame]
Dan Walsh514af852012-04-13 11:04:45 -04001## 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 Walsh514af852012-04-13 11:04:45 -040019import sys
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020020from gi.repository import Gdk, Gtk
Dan Walsh514af852012-04-13 11:04:45 -040021
22##
23## I18N
24##
Jason Zaman789d0eb2015-07-24 16:07:13 +080025PROGNAME = "policycoreutils"
Dan Walsh514af852012-04-13 11:04:45 -040026try:
Jason Zamanaf595442016-08-05 02:34:02 +080027 import gettext
28 kwargs = {}
29 if sys.version_info < (3,):
30 kwargs['unicode'] = True
Dan Walsh514af852012-04-13 11:04:45 -040031 gettext.install(PROGNAME,
32 localedir="/usr/share/locale",
Jason Zamanaf595442016-08-05 02:34:02 +080033 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
Dan Walsh514af852012-04-13 11:04:45 -040042
Jason Zaman789d0eb2015-07-24 16:07:13 +080043
Dan Walsh514af852012-04-13 11:04:45 -040044def idle_func():
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020045 while Gtk.events_pending():
46 Gtk.main_iteration()
Dan Walsh514af852012-04-13 11:04:45 -040047
Jason Zaman789d0eb2015-07-24 16:07:13 +080048
Dan Walsh514af852012-04-13 11:04:45 -040049class semanagePage:
Jason Zaman789d0eb2015-07-24 16:07:13 +080050
Dan Walsh514af852012-04-13 11:04:45 -040051 def __init__(self, xml, name, description):
52 self.xml = xml
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020053 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 Walsh514af852012-04-13 11:04:45 -040056
57 self.local = False
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020058 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 Walsh514af852012-04-13 11:04:45 -040061 self.filter_entry.connect("focus_out_event", self.filter_changed)
62 self.filter_entry.connect("activate", self.filter_changed)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020063 self.filter_entry.connect("changed", self.filter_changed)
Dan Walsh514af852012-04-13 11:04:45 -040064
65 self.view.connect("row_activated", self.rowActivated)
66 self.view.get_selection().connect("changed", self.itemSelected)
Jason Zaman789d0eb2015-07-24 16:07:13 +080067 self.description = description
Dan Walsh514af852012-04-13 11:04:45 -040068
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 Iooss0f3beeb2017-09-20 08:56:54 +020080 def itemSelected(self, selection):
Dan Walsh514af852012-04-13 11:04:45 -040081 return
82
83 def filter_changed(self, *arg):
Jason Zaman789d0eb2015-07-24 16:07:13 +080084 filter = arg[0].get_text()
Dan Walsh514af852012-04-13 11:04:45 -040085 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 Zaman789d0eb2015-07-24 16:07:13 +080090 val = model.get_value(i, sort_col)
Dan Walsh514af852012-04-13 11:04:45 -040091 if val.lower().startswith(key.lower()):
92 return False
93 return True
94
95 def match(self, target, filter):
96 try:
Jason Zaman789d0eb2015-07-24 16:07:13 +080097 f = filter.lower()
98 t = target.lower()
Dan Walsh514af852012-04-13 11:04:45 -040099 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 Zaman789d0eb2015-07-24 16:07:13 +0800108 def verify(self, message, title=""):
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200109 dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
110 Gtk.ButtonsType.YES_NO,
Dan Walsh514af852012-04-13 11:04:45 -0400111 message)
112 dlg.set_title(title)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200113 dlg.set_position(Gtk.WindowPosition.MOUSE)
Dan Walsh514af852012-04-13 11:04:45 -0400114 dlg.show_all()
115 rc = dlg.run()
116 dlg.destroy()
117 return rc
118
119 def error(self, message):
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200120 dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR,
121 Gtk.ButtonsType.CLOSE,
Dan Walsh514af852012-04-13 11:04:45 -0400122 message)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200123 dlg.set_position(Gtk.WindowPosition.MOUSE)
Dan Walsh514af852012-04-13 11:04:45 -0400124 dlg.show_all()
125 dlg.run()
126 dlg.destroy()
127
128 def deleteDialog(self):
Vit Mojzis530904e2016-10-19 14:36:03 +0200129 store, it = self.view.get_selection().get_selected()
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200130 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 Walsh514af852012-04-13 11:04:45 -0400131 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 Iooss0f3beeb2017-09-20 08:56:54 +0200139 self.dialog.set_position(Gtk.WindowPosition.MOUSE)
Dan Walsh514af852012-04-13 11:04:45 -0400140
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200141 while self.dialog.run() == Gtk.ResponseType.OK:
Dan Walsh514af852012-04-13 11:04:45 -0400142 try:
Vit Mojzis6f4b1bb2018-02-22 14:29:33 +0100143 if self.add() is False:
Dan Walsh514af852012-04-13 11:04:45 -0400144 continue
Jason Zaman789d0eb2015-07-24 16:07:13 +0800145 break
Jason Zaman4d340e42016-08-05 02:34:03 +0800146 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400147 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 Iooss0f3beeb2017-09-20 08:56:54 +0200153 self.dialog.set_position(Gtk.WindowPosition.MOUSE)
154 while self.dialog.run() == Gtk.ResponseType.OK:
Dan Walsh514af852012-04-13 11:04:45 -0400155 try:
Vit Mojzis6f4b1bb2018-02-22 14:29:33 +0100156 if self.modify() is False:
Dan Walsh514af852012-04-13 11:04:45 -0400157 continue
Jason Zaman789d0eb2015-07-24 16:07:13 +0800158 break
Jason Zaman4d340e42016-08-05 02:34:03 +0800159 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400160 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