Debian Codegraph @

qml/Components/PageImport.qml

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * This file is part of the 2FA Manager App for Ubuntu Touch
3 * Copyright (C) 2019 Joan CiberSheep
4 *
5 * This App is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 *
10 * This App is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 import QtQuick 2.9
21 import Lomiri.Components 1.3
22 import Lomiri.Components.Popups 1.3
23
24 import QtQuick.LocalStorage 2.0
•25 //import "../js/otpauth.js" as OTPAuth
26 import "../js/db.js" as KeysDB
27
28 Page {
29 id: keyInfoPage
30 objectName: "keyInfoPage"
31 anchors.fill: parent
32
33 property var key
34 property int id
35 property int reminingTime
36 property string secret
37 property string url
38 property string desc
39
40 property bool isTOTP
41 property string authType
42
43 header: HeaderInfo {
44 id: pageHeader
45 title: i18n.tr("%1 Key Information").arg(desc)
46 flickable: mainFlickable
47 }
48
49 ProgressBar {
50 id: bar
51 visible: isTOTP
52 width: parent.width
53 anchors.top: header.bottom
54
55 maximumValue: key.period || 0
56 minimumValue: 0
57 }
58
59 ScrollView {
60 width: parent.width
61 height: parent.height
62 contentItem: mainFlickable
63 }
64
65 Flickable {
66 id: mainFlickable
67 width: parent.width
68 height: parent.height
69 contentHeight: mainColumn.height
70 topMargin: units.gu(2)
71 bottomMargin: units.gu(4)
72
73 Column {
74 id: mainColumn
75 width: parent.width
76 anchors.top: parent.top
77 anchors.topMargin: units.gu(1)
78 spacing: units.gu(2)
79
80 Label {
81 text: i18n.tr("%1 generator: %2 at %32").arg(authType).arg(key.label).arg(key.issuer)
82 textSize: Label.Small
83 anchors.horizontalCenter: parent.horizontalCenter
84 }
85
86 LomiriShape {
87 id: contentShape
88 anchors.horizontalCenter: parent.horizontalCenter
89 width: Math.min(parent.width - units.gu(8), units.gu(68))
90 height: generatedKey.height + units.gu(8)
91 aspect: LomiriShape.Flat
92 color: root.highlightColor
93
94 onWidthChanged: generatedKey.adjustTextSize()
95
96 Label {
97 id: generatedKey
98 anchors.centerIn: parent
99
100 text: generateKey(isTOTP)
101
102 //TODO: This might be too slow for what it does. Consider
103 //From Dialer App
104 property double maximumFontSize: units.dp(35)
105 readonly property double minimumFontSize: FontUtils.sizeToPixels("medium")
106 property bool __adjusting: false
107
108 fontSizeMode: Text.HorizontalFit
109
110 function adjustTextSize(){
111 // avoid infinite recursion here
112 if (__adjusting) return;
113
114 __adjusting = true;
115
116 // start by resetting the font size to discover the scale that should be used
117 font.pixelSize = maximumFontSize
118
119 // check if it really needs to be scaled
120 if (contentShape.width < generatedKey.width) {
121 var factor = contentShape.width / generatedKey.width;
122 font.pixelSize = Math.max(font.pixelSize * factor, minimumFontSize);
123 }
124 __adjusting = false
125 }
126
127 Connections {
128 target: units
129 onGridUnitChanged: adjustTextSize()
130 }
131
132 Component.onCompleted: adjustTextSize()
133 }
134 }
135
136 Row {
137 width: parent.width - units.gu(8)
138 spacing: units.gu(4)
139 anchors.horizontalCenter: parent.horizontalCenter
140
141 Button {
142 id: regenBut
143 visible: !keyPeriod.visible
144 width: parent.width/2 - units.gu(2)
145 text: i18n.tr("Generate password")
146 onClicked: {
147 var difference = 1;
148 url = KeysDB.updateCounter(id, difference);
149 keyInfoPage.key = OTPAuth.URI.parse(url);
150 generatedKey.text = generateKey(isTOTP);
151 counterHtop.text = i18n.tr("Counter: %1").arg(key.counter)
152 root.initDB();
153 }
154 }
155
156 Button {
157 width: regenBut.visible ? parent.width/2 - units.gu(2) : parent.width
158 text: i18n.tr("Copy to clipboard")
159 onClicked: Clipboard.push(generatedKey.text)
160 color: theme.palette.normal.activity
161 }
162 }
163
164 Rectangle {
165 width: parent.width
166 height: units.gu(2)
167 color: "transparent"
168 }
169
170 LomiriShape {
171 anchors.horizontalCenter: parent.horizontalCenter
172 width: units.gu(21)
173 height: width
174 sourceFillMode: LomiriShape.Pad
175 aspect: LomiriShape.Flat
176 backgroundColor: "#ffffff"
177
178 source: QRCode {
179 id: qr
180 width: parent.width - units.gu(4)
181 height: width
182 anchors.bottom: parent.bottom
183 value : url
184 }
185 }
186
187 Text {
188 text: i18n.tr("Secret Key")
189 anchors.horizontalCenter: parent.horizontalCenter
190 height: units.gu(5)
191 verticalAlignment: Text.AlignBottom
192 color: theme.palette.normal.baseText
193 }
194
195 Label {
196 text: secret
197 textSize: Label.XSmall
198 anchors.horizontalCenter: parent.horizontalCenter
199 color: theme.palette.normal.baseText
200 }
201
202 Text {
203 text: i18n.tr("Algorithm: %1").arg(key.algorithm)
204 anchors.horizontalCenter: parent.horizontalCenter
205 color: theme.palette.normal.baseText
206 }
207
208 Text {
209 text: i18n.tr("Password Length: %1").arg(key.digits)
210 anchors.horizontalCenter: parent.horizontalCenter
211 color: theme.palette.normal.baseText
212 }
213
214 Text {
215 id: keyPeriod
216 visible: isTOTP
217 text: i18n.tr("Interval: %1").arg(key.period)
218 anchors.horizontalCenter: parent.horizontalCenter
219 color: theme.palette.normal.baseText
220 }
221
222 Text {
223 id: counterHtop
224 text: i18n.tr("Counter: %1").arg(key.counter)
225 visible: !keyPeriod.visible
226 anchors.horizontalCenter: parent.horizontalCenter
227 color: theme.palette.normal.baseText
228 }
229 }
230 }
231
232 //Let's connect with the main ticker
233 Connections {
234 target: timer
235
236 onTriggered: {
237 generatedKey.text = generateKey(isTOTP)
238
239 bar.value = timer.time % key.period
240 }
241 }
242
243 Component.onCompleted: {
244 isTOTP = key.counter == undefined;
245 bar.value = reminingTime;
246
247 authType = isTOTP
248 ? "TOTP"
249 : "HTOP"
250 }
251
252 function generateKey(boolVar) {
253 return boolVar
254 ? OTPAuth.TOTP.generate(key)
255 : OTPAuth.HOTP.generate(key)
256 }
257 }