1import QtQuick 2.9
2import QtQuick.Controls 2.2
3import QtQuick.Layouts 1.2
4import "utils.js" as Utils
5import QtQuick.Controls.Material 2.2
6
7ColumnLayout {
8    objectName: "interfaces"
9    StackView.onActivating: load()
10    function load() {
11        otp.checked = Utils.includes(yubiKey.usbInterfacesEnabled, 'OTP')
12        fido.checked = Utils.includes(yubiKey.usbInterfacesEnabled, 'FIDO')
13        ccid.checked = Utils.includes(yubiKey.usbInterfacesEnabled, 'CCID')
14    }
15
16    function getEnabledInterfaces() {
17        var interfaces = []
18        if (otp.checked) {
19            interfaces.push('OTP')
20        }
21        if (fido.checked) {
22            interfaces.push('FIDO')
23        }
24        if (ccid.checked) {
25            interfaces.push('CCID')
26        }
27        return interfaces
28    }
29
30    function configureInterfaces() {
31        yubiKey.setMode(getEnabledInterfaces(), function (resp) {
32            if (resp.success) {
33                if (!yubiKey.canWriteConfig) {
34                    reInsertYubiKey.open()
35                } else {
36                    views.pop()
37                }
38            } else {
39                snackbarError.show(
40                            qsTr("Failed to configure interfaces. Make sure the YubiKey does not have restricted access."))
41            }
42        })
43    }
44
45    function configurationHasChanged() {
46        var enabledYubiKeyUsbInterfaces = JSON.stringify(
47                    yubiKey.usbInterfacesEnabled.sort())
48        var enabledUiUsbInterfaces = JSON.stringify(
49                    getEnabledInterfaces().sort())
50        return enabledYubiKeyUsbInterfaces !== enabledUiUsbInterfaces
51    }
52
53    function validCombination() {
54        return otp.checked || fido.checked || ccid.checked
55    }
56
57    CustomContentColumn {
58
59        ViewHeader {
60            breadcrumbs: [qsTr("Interfaces")]
61        }
62
63        RowLayout {
64            Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
65            Layout.fillHeight: true
66            Layout.fillWidth: true
67
68            GridLayout {
69                columns: 3
70                RowLayout {
71                    Layout.columnSpan: 3
72                    Image {
73                        fillMode: Image.PreserveAspectCrop
74                        source: "../images/usb.svg"
75                        sourceSize.width: 24
76                        sourceSize.height: 24
77                    }
78                    Label {
79                        text: qsTr("USB")
80                        color: yubicoBlue
81                        font.pixelSize: constants.h2
82                    }
83                }
84                CheckBox {
85                    id: otp
86                    enabled: yubiKey.otpInterfaceSupported()
87                    text: qsTr("OTP")
88                    font.pixelSize: constants.h3
89                    checkable: true
90                    ToolTip.delay: 1000
91                    ToolTip.visible: hovered
92                    ToolTip.text: qsTr("Toggle OTP interface over USB")
93                    Material.foreground: yubicoBlue
94                }
95                CheckBox {
96                    id: fido
97                    enabled: yubiKey.fidoInterfaceSupported()
98                    text: qsTr("FIDO")
99                    font.pixelSize: constants.h3
100                    checkable: true
101                    ToolTip.delay: 1000
102                    ToolTip.visible: hovered
103                    ToolTip.text: qsTr("Toggle FIDO interface over USB")
104                    Material.foreground: yubicoBlue
105                }
106                CheckBox {
107                    id: ccid
108                    enabled: yubiKey.ccidInterfaceSupported()
109                    text: qsTr("CCID (Smart Card)")
110                    font.pixelSize: constants.h3
111                    checkable: true
112                    ToolTip.delay: 1000
113                    ToolTip.visible: hovered
114                    ToolTip.text: qsTr("Toggle CCID interface over USB")
115                    Material.foreground: yubicoBlue
116                }
117            }
118        }
119
120        ButtonsBar {
121            finishCallback: configureInterfaces
122            finishEnabled: configurationHasChanged() && validCombination()
123            finishText: qsTr("Save Interfaces")
124            finishTooltip: qsTr("Finish and save interfaces configuration to YubiKey")
125        }
126    }
127}
128