2012-04-13 15:04:45 +00:00
## semanagePage.py - show selinux mappings
## Copyright (C) 2006 Red Hat, Inc.
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
## Author: Dan Walsh
import sys
2017-09-20 06:56:54 +00:00
from gi . repository import Gdk , Gtk
2012-04-13 15:04:45 +00:00
##
## I18N
##
2015-07-24 08:07:13 +00:00
PROGNAME = " policycoreutils "
2012-04-13 15:04:45 +00:00
try :
2016-08-04 18:34:02 +00:00
import gettext
kwargs = { }
if sys . version_info < ( 3 , ) :
kwargs [ ' unicode ' ] = True
2012-04-13 15:04:45 +00:00
gettext . install ( PROGNAME ,
localedir = " /usr/share/locale " ,
2016-08-04 18:34:02 +00:00
codeset = ' utf-8 ' ,
* * kwargs )
except :
try :
import builtins
builtins . __dict__ [ ' _ ' ] = str
except ImportError :
import __builtin__
__builtin__ . __dict__ [ ' _ ' ] = unicode
2012-04-13 15:04:45 +00:00
2015-07-24 08:07:13 +00:00
2012-04-13 15:04:45 +00:00
def idle_func ( ) :
2017-09-20 06:56:54 +00:00
while Gtk . events_pending ( ) :
Gtk . main_iteration ( )
2012-04-13 15:04:45 +00:00
2015-07-24 08:07:13 +00:00
2012-04-13 15:04:45 +00:00
class semanagePage :
2015-07-24 08:07:13 +00:00
2012-04-13 15:04:45 +00:00
def __init__ ( self , xml , name , description ) :
self . xml = xml
2017-09-20 06:56:54 +00:00
self . window = self . xml . get_object ( " mainWindow " ) . get_root_window ( )
self . busy_cursor = Gdk . Cursor . new ( Gdk . CursorType . WATCH )
self . ready_cursor = Gdk . Cursor . new ( Gdk . CursorType . LEFT_PTR )
2012-04-13 15:04:45 +00:00
self . local = False
2017-09-20 06:56:54 +00:00
self . view = xml . get_object ( " %s View " % name )
self . dialog = xml . get_object ( " %s Dialog " % name )
self . filter_entry = xml . get_object ( " %s FilterEntry " % name )
2012-04-13 15:04:45 +00:00
self . filter_entry . connect ( " focus_out_event " , self . filter_changed )
self . filter_entry . connect ( " activate " , self . filter_changed )
2017-09-20 06:56:54 +00:00
self . filter_entry . connect ( " changed " , self . filter_changed )
2012-04-13 15:04:45 +00:00
self . view . connect ( " row_activated " , self . rowActivated )
self . view . get_selection ( ) . connect ( " changed " , self . itemSelected )
2015-07-24 08:07:13 +00:00
self . description = description
2012-04-13 15:04:45 +00:00
def wait ( self ) :
self . window . set_cursor ( self . busy_cursor )
idle_func ( )
def ready ( self ) :
self . window . set_cursor ( self . ready_cursor )
idle_func ( )
def get_description ( self ) :
return self . description
2017-09-20 06:56:54 +00:00
def itemSelected ( self , selection ) :
2012-04-13 15:04:45 +00:00
return
def filter_changed ( self , * arg ) :
2015-07-24 08:07:13 +00:00
filter = arg [ 0 ] . get_text ( )
2012-04-13 15:04:45 +00:00
if filter != self . filter :
self . load ( filter )
def search ( self , model , col , key , i ) :
sort_col = self . store . get_sort_column_id ( ) [ 0 ]
2015-07-24 08:07:13 +00:00
val = model . get_value ( i , sort_col )
2012-04-13 15:04:45 +00:00
if val . lower ( ) . startswith ( key . lower ( ) ) :
return False
return True
def match ( self , target , filter ) :
try :
2015-07-24 08:07:13 +00:00
f = filter . lower ( )
t = target . lower ( )
2012-04-13 15:04:45 +00:00
if t . find ( f ) > = 0 :
return True
except :
pass
return False
def rowActivated ( self , view , row , Column ) :
self . propertiesDialog ( )
2015-07-24 08:07:13 +00:00
def verify ( self , message , title = " " ) :
2017-09-20 06:56:54 +00:00
dlg = Gtk . MessageDialog ( None , 0 , Gtk . MessageType . INFO ,
Gtk . ButtonsType . YES_NO ,
2012-04-13 15:04:45 +00:00
message )
dlg . set_title ( title )
2017-09-20 06:56:54 +00:00
dlg . set_position ( Gtk . WindowPosition . MOUSE )
2012-04-13 15:04:45 +00:00
dlg . show_all ( )
rc = dlg . run ( )
dlg . destroy ( )
return rc
def error ( self , message ) :
2017-09-20 06:56:54 +00:00
dlg = Gtk . MessageDialog ( None , 0 , Gtk . MessageType . ERROR ,
Gtk . ButtonsType . CLOSE ,
2012-04-13 15:04:45 +00:00
message )
2017-09-20 06:56:54 +00:00
dlg . set_position ( Gtk . WindowPosition . MOUSE )
2012-04-13 15:04:45 +00:00
dlg . show_all ( )
dlg . run ( )
dlg . destroy ( )
def deleteDialog ( self ) :
2016-10-19 12:36:03 +00:00
store , it = self . view . get_selection ( ) . get_selected ( )
2017-09-20 06:56:54 +00:00
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 ) :
2012-04-13 15:04:45 +00:00
self . delete ( )
def use_menus ( self ) :
return True
def addDialog ( self ) :
self . dialogClear ( )
self . dialog . set_title ( _ ( " Add %s " % self . description ) )
2017-09-20 06:56:54 +00:00
self . dialog . set_position ( Gtk . WindowPosition . MOUSE )
2012-04-13 15:04:45 +00:00
2017-09-20 06:56:54 +00:00
while self . dialog . run ( ) == Gtk . ResponseType . OK :
2012-04-13 15:04:45 +00:00
try :
2017-09-20 06:56:54 +00:00
if not self . add ( ) :
2012-04-13 15:04:45 +00:00
continue
2015-07-24 08:07:13 +00:00
break
2016-08-04 18:34:03 +00:00
except ValueError as e :
2012-04-13 15:04:45 +00:00
self . error ( e . args [ 0 ] )
self . dialog . hide ( )
def propertiesDialog ( self ) :
self . dialogInit ( )
self . dialog . set_title ( _ ( " Modify %s " % self . description ) )
2017-09-20 06:56:54 +00:00
self . dialog . set_position ( Gtk . WindowPosition . MOUSE )
while self . dialog . run ( ) == Gtk . ResponseType . OK :
2012-04-13 15:04:45 +00:00
try :
2017-09-20 06:56:54 +00:00
if not self . modify ( ) :
2012-04-13 15:04:45 +00:00
continue
2015-07-24 08:07:13 +00:00
break
2016-08-04 18:34:03 +00:00
except ValueError as e :
2012-04-13 15:04:45 +00:00
self . error ( e . args [ 0 ] )
self . dialog . hide ( )
def on_local_clicked ( self , button ) :
self . local = not self . local
if self . local :
button . set_label ( _ ( " all " ) )
else :
button . set_label ( _ ( " Customized " ) )
self . load ( self . filter )
return True