Debian Codegraph @

qml/Components/PageEditAddKey.qml

raw
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 import QtQuick.LocalStorage 2.0
24 import "../js/db.js" as KeysDB
25
26 Page {
27 id: editAddKeyPage
28
29 property var uriToEdit
30 property alias descriptionToEdit: info.userDesc
31 property bool editMode: false
32 property int idToUpdate
33
34 header: HeaderBase {
35 id: pageHeader
36 title: i18n.tr("Key Information")
37
38 trailingActionBar {
39 actions: Action {
40 iconName: "ok"
41 text: editMode
42 ? i18n.tr("Edit")
43 : i18n.tr("Add")
44
45 enabled: !info.isInfoEmpty
46 onTriggered: addKey()
47 }
48 }
49 }
50
51 Flickable {
52 id: editAddFlickable
53 width: parent.width
54 height: parent.height
55 clip: true
56 contentHeight: info.height
57 topMargin: pageHeader.height + units.gu(2)
58 bottomMargin: Qt.inputMethod.visible && !editMode
59 ? Qt.inputMethod.keyboardRectangle.height + units.gu(2)
60 : units.gu(4)
61
62 KeyToEdit {
63 id: info
64 width: parent.width * .85
65 anchors.horizontalCenter: parent.horizontalCenter
66 editMode: editAddKeyPage.editMode
67 }
68 }
69
70 function addKey() {
71 var editedKey = info.checkFields();
72
73 if (info.isTOTP) {
74 editedKey = new OTPAuth.TOTP(editedKey);
75 } else {
76 editedKey = new OTPAuth.HOTP(editedKey);
77 }
78
79 if (editMode) {
80 KeysDB.updateStoredKey(idToUpdate, Date(), editedKey.toString(), info.userDesc);
81 } else {
82 KeysDB.storeKey(Date(), editedKey.toString(), info.userDesc);
83 }
84
85 root.initDB();
86 closingPop();
87 }
88
89 function closingPop() {
90 if (editMode) {
91 mainStack.pop();
92 } else {
93 bottomEdge.collapse();
94 }
95 }
96
97 Component.onCompleted: {
98 if (editMode) {
99 try {
100 info.isTOTP = uriToEdit.match("totp") !== null;
101 info.uriJson = OTPAuth.URI.parse(uriToEdit);
102 } catch(e) {
103 console.log("Error",e);
104 }
105 }
106 }
107 }