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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Souk - device_permission.rs
// Copyright (C) 2022-2023  Felix Häcker <haeckerfelix@gnome.org>
//
// 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 3 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, see <https://www.gnu.org/licenses/>.

use gtk::glib;

use crate::main::context::{SkContextDetail, SkContextDetailKind, SkContextDetailLevel};
use crate::main::flatpak::permissions::{PermissionDetails, SkPermissionSummary};
use crate::main::i18n::i18n;

#[glib::flags(name = "SkDevicePermission")]
pub enum SkDevicePermission {
    #[flags_value(name = "none")]
    NONE = 1 << 0,
    #[flags_value(name = "unknown")]
    UNKNOWN = 1 << 1,
    #[flags_value(name = "dri")]
    DRI = 1 << 2,
    #[flags_value(name = "kvm")]
    KVM = 1 << 3,
    #[flags_value(name = "shm")]
    SHM = 1 << 4,
    #[flags_value(name = "all")]
    ALL = 1 << 5,
}

impl PermissionDetails for SkDevicePermission {
    fn summary(&self) -> SkPermissionSummary {
        let mut summary = SkPermissionSummary::empty();

        if self.contains(Self::ALL) {
            summary |= SkPermissionSummary::FULL_DEVICE_ACCESS;
        }

        if self.contains(Self::SHM) {
            summary |= SkPermissionSummary::READ_DATA;
        }

        if self.contains(Self::UNKNOWN) {
            summary |= SkPermissionSummary::UNKNOWN;
        }

        summary
    }

    fn context_details(&self) -> Vec<SkContextDetail> {
        let mut details = Vec::new();
        let kind = SkContextDetailKind::Icon;
        let icon_name = "drive-harddisk-usb-symbolic";

        if self == &Self::NONE || self == &SkDevicePermission::DRI {
            let level = SkContextDetailLevel::Good;
            let title = i18n("No Device Access");
            let description = i18n("Has no access to connected devices");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::ALL) {
            let level = SkContextDetailLevel::Bad;
            let title = i18n("Access to All Devices");
            let description =
                i18n("Can access all connected devices, e.g. webcams or game controllers");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if !self.contains(Self::ALL) && self.contains(Self::KVM) {
            let icon_name = "computer-symbolic";
            let level = SkContextDetailLevel::Neutral;
            let title = i18n("Access to Virtualization Subsystem");
            let description = i18n("Can access and run virtual machines");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::SHM) {
            let icon_name = "folder-symbolic";
            let level = SkContextDetailLevel::Warning;
            let title = i18n("Access to Shared Memory");
            let description = i18n("Can access memory which is shared with other applications");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::UNKNOWN) {
            let icon_name = "dialog-question-symbolic";
            let level = SkContextDetailLevel::Bad;
            let title = i18n("Access to Unknown Device");
            let description = i18n("Can access an unknown device");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        details
    }
}

impl From<&str> for SkDevicePermission {
    fn from(value: &str) -> Self {
        match value {
            "dri" => Self::DRI,
            "kvm" => Self::KVM,
            "shm" => Self::SHM,
            "all" => Self::ALL,
            _ => Self::UNKNOWN,
        }
    }
}