from PyQt6.QtWidgets import QLabel, QPushButton, QTextEdit from core.controllers.PolicyController import PolicyController from core.Errors import ( CommandNotFoundError, PolicyAssignmentError, PolicyInstatementError, ) from gui.v2.ui.pages.Page import Page class PolicySuggestionPage(Page): def __init__(self, page_stack, main_window=None, parent=None, policy_type='capability'): super().__init__("PolicySuggestion", page_stack, main_window, parent) self.btn_path = main_window.btn_path self.update_status = main_window self.policy_type = policy_type self.policy = PolicyController.get(policy_type) self.button_back.setVisible(False) self.button_next.setVisible(False) self.button_go.setVisible(False) self.status_label = QLabel(self) self.status_label.setGeometry(80, 430, 640, 40) self.status_label.setStyleSheet("font-size: 14px; color: cyan;") self.setup_ui() def setup_ui(self): policy_name = "Capability" if self.policy_type == 'capability' else "Privilege" self.title.setGeometry(20, 50, 760, 40) self.title.setText(f"Policy Suggestion: {policy_name} Policy") description = QLabel(self) description.setGeometry(80, 100, 640, 60) description.setWordWrap(True) description.setStyleSheet("font-size: 14px; color: cyan;") description.setText( f"A {policy_name.lower()} policy is available and can be instated to improve functionality. Review the policy details below before proceeding.") preview_label = QLabel(self) preview_label.setGeometry(80, 170, 640, 30) preview_label.setStyleSheet( "font-size: 13px; color: white; font-weight: bold;") preview_label.setText("Policy Preview:") preview_text = QTextEdit(self) preview_text.setGeometry(80, 200, 640, 150) preview_text.setReadOnly(True) preview_text.setStyleSheet(""" QTextEdit { background-color: #1a1a1a; color: #00ffff; border: 1px solid #333; border-radius: 4px; font-family: monospace; font-size: 11px; padding: 5px; } """) try: preview_content = PolicyController.preview(self.policy) preview_text.setText(preview_content) except Exception as e: preview_text.setText(f"Error loading preview: {str(e)}") yes_button = QPushButton(f"Instate {policy_name} Policy", self) yes_button.setGeometry(170, 370, 250, 50) yes_button.setStyleSheet(""" QPushButton { background: #007AFF; color: white; border: none; border-radius: 4px; font-size: 13px; font-weight: bold; } QPushButton:hover { background: #0056CC; } """) yes_button.clicked.connect(self.instate_policy) no_button = QPushButton("Skip", self) no_button.setGeometry(440, 370, 120, 50) no_button.setStyleSheet(""" QPushButton { background: #666; color: white; border: none; border-radius: 4px; font-size: 13px; font-weight: bold; } QPushButton:hover { background: #555; } """) no_button.clicked.connect(self.skip_policy) self.refresh_status() def refresh_status(self): try: if PolicyController.is_instated(self.policy): self.status_label.setText( f"Current status: {self.policy_type} policy is instated.") self.status_label.setStyleSheet( "font-size: 14px; color: #2ecc71;") else: self.status_label.setText( f"Current status: {self.policy_type} policy is not instated.") self.status_label.setStyleSheet( "font-size: 14px; color: #e67e22;") except Exception: self.status_label.setText("Unable to determine policy status.") self.status_label.setStyleSheet("font-size: 14px; color: #e67e22;") def instate_policy(self): try: PolicyController.instate(self.policy) self.refresh_status() policy_name = "Capability" if self.policy_type == 'capability' else "Privilege" self.update_status.update_status(f"{policy_name} policy instated") self.custom_window.navigator.navigate("menu") except CommandNotFoundError as e: self.status_label.setText(str(e)) self.status_label.setStyleSheet("font-size: 14px; color: red;") except (PolicyAssignmentError, PolicyInstatementError) as e: self.status_label.setText(str(e)) self.status_label.setStyleSheet("font-size: 14px; color: red;") except Exception as e: self.status_label.setText(f"Failed to instate policy: {str(e)}") self.status_label.setStyleSheet("font-size: 14px; color: red;") def skip_policy(self): self.custom_window.navigator.navigate("menu")