summaryrefslogtreecommitdiff
path: root/src/netmonitor/front/scanner_profiles_tab.py
blob: 8e8610cf45138b1af31dee03d6908ca34644e383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from textual.widgets import Button, Label, Switch 
from textual import on
from textual.app import ComposeResult
from textual.containers import Vertical, Horizontal, VerticalScroll

from ..back.scanner_profiles_manager import ScannerProfilesManager
from .scanner_profiles_tab_pushscreens import ScanNowPushScreen, SetSchedulerPushScreen, ShowLogsPushScreen, SetNotifacationOptionsPushScreen, ShowProfilePushScreen, ConfirmDeletePushScreen

class ScannerProfilesTab(Vertical):
    def __init__(self, manager: ScannerProfilesManager, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.manager = manager
        self.manager.on_refresh = self.refresh_profiles
        self.manager.on_message = self.on_manager_message

    def compose(self) -> ComposeResult:
        yield VerticalScroll(id="profiles-list")

    def on_mount(self) -> None:
        self.refresh_profiles()

    def refresh_profiles(self) -> None:
        profiles_list = self.query_one("#profiles-list", VerticalScroll)
        profiles_list.remove_children()

        if not self.manager.profiles:
            return

        for profile in self.manager.profiles:
            row = Horizontal(
                Switch(id=f"switch-{profile.profile_name}", value=profile.is_active),
                Button("Scan Now", id=f"scan-now-button-{profile.profile_name}", classes="profile-action", variant="success"),
                Label(f"{profile.profile_name}", classes="profile-profile_name"),
                Button("Show Profile", id=f"show-profile-button-{profile.profile_name}", classes="profile-action", variant="primary"),
                Button("Show logs", id=f"show-logs-button-{profile.profile_name}", classes="profile-action", variant="default"),
                Button("Set scheduler", id=f"set-scheduler-button-{profile.profile_name}", classes="profile-action", variant="default"),
                Button("Notifications", id=f"set-notifications-button-{profile.profile_name}", classes="profile-action", variant="default"),
                Button("Delete", id=f"delete-{profile.profile_name}", classes="profile-action", variant="error"),
                classes="profile-row"
            )
            profiles_list.mount(row)

    @on(Switch.Changed)
    def switch_changed(self, event: Switch.Changed) -> None:
        switch_id = event.switch.id
        if not switch_id:
            return
        profile_name = switch_id.removeprefix("switch-")

        if event.switch.value:
            is_turned_on = self.manager.turn_on_profile(profile_name)
            if not is_turned_on:
                event.switch.value = False
        else:
            is_turned_off = self.manager.turn_off_profile(profile_name)
            if not is_turned_off:
                event.switch.value = False

    @on(Button.Pressed)
    def on_any_button_pressed(self, event: Button.Pressed) -> None:
        button_id = event.button.id
        if not button_id:
            return
        if button_id.startswith("scan-now-button-"):
            profile_name = button_id.removeprefix("scan-now-button-")
            self.app.push_screen(ScanNowPushScreen(self.manager, profile_name))
        elif button_id.startswith("show-profile-button-"):
            profile_name = button_id.removeprefix("show-profile-button-")
            self.app.push_screen(ShowProfilePushScreen(self.manager, profile_name))
        elif button_id.startswith("set-scheduler-button-"):
            profile_name = button_id.removeprefix("set-scheduler-button-")
            self.app.push_screen(SetSchedulerPushScreen(self.manager, profile_name))
        elif button_id.startswith("set-notifications-button-"):
            profile_name = button_id.removeprefix("set-notifications-button-")
            self.app.push_screen(SetNotifacationOptionsPushScreen(self.manager, profile_name))
        elif button_id.startswith("delete-"):
            profile_name = button_id.removeprefix("delete-")
            self.app.push_screen(ConfirmDeletePushScreen(self.manager, profile_name))
        elif button_id.startswith("show-logs-button-"):
            profile_name = button_id.removeprefix("show-logs-button-")
            self.app.push_screen(ShowLogsPushScreen(self.manager, profile_name))

    def on_manager_message(self, msg: str, title: str, severity):
        self.app.notify(message=msg, title=title, severity=severity)