| 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/db.js" as KeysDB |
| 26 |
|
| 27 |
Page { |
| 28 |
id: pageKeysResume |
| 29 |
anchors.fill: parent |
| 30 |
|
| 31 |
signal timerTicked(int time) |
| 32 |
|
| 33 |
property bool isListBusy: false |
| 34 |
property bool searching: false |
| 35 |
property var filterPattern: new RegExp("") |
| 36 |
|
| 37 |
//TODO: Do we want to show the hint only when the Flickable is at the top? |
| 38 |
//property alias isAtTop: keysListView.atYBeginning |
| 39 |
|
| 40 |
header: HeaderMain { |
| 41 |
id: searchHeader |
| 42 |
title: i18n.tr('2FA Manager') |
| 43 |
flickable: keysListView |
| 44 |
|
| 45 |
onIsSearchingChanged: pageKeysResume.searching = isSearching |
| 46 |
onSearchText: searchKeysModel.search(typedText); |
| 47 |
} |
| 48 |
|
| 49 |
ScrollView { |
| 50 |
width: parent.width |
| 51 |
height: parent.height |
| 52 |
contentItem: keysListView |
| 53 |
} |
| 54 |
|
| 55 |
ListView { |
| 56 |
id: keysListView |
| 57 |
anchors.fill: parent |
| 58 |
model: searching ? searchKeysModel : storedKeys |
| 59 |
delegate: keysDelegate |
| 60 |
} |
| 61 |
|
| 62 |
ListModel { |
| 63 |
id: storedKeys |
| 64 |
|
| 65 |
function populate(data) { |
| 66 |
storedKeys.clear(); |
| 67 |
if (data.length > 0) { |
| 68 |
for (var i = 0; i < data.length; i++) { |
| 69 |
var checkedUri = root.checkUri(data.item(i).keyJSON) |
| 70 |
|
| 71 |
try { |
| 72 |
var parsedKey = OTPAuth.URI.parse(checkedUri); |
| 73 |
var checkedDesc = data.item(i).description || ""; |
| 74 |
|
| 75 |
storedKeys.append({ |
| 76 |
desc: checkedDesc, |
| 77 |
authkey: parsedKey, |
| 78 |
secret: parsedKey.secret.b32, |
| 79 |
//We use id to delete the proper db entry |
| 80 |
id: data.item(i).identifier, |
| 81 |
url: data.item(i).keyJSON, |
| 82 |
searchablestring: data.item(i).description + " " + parsedKey.issuer + " " + parsedKey.label |
| 83 |
}); |
| 84 |
} catch(e) { |
| 85 |
console.log("Error",e); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
SortFilterModel { |
| 93 |
id: searchKeysModel |
| 94 |
model: storedKeys |
| 95 |
|
| 96 |
sort { |
| 97 |
property: "desc" |
| 98 |
order: Qt.DescendingOrder |
| 99 |
} |
| 100 |
|
| 101 |
filter { |
| 102 |
property: "searchablestring" |
| 103 |
pattern: pageKeysResume.filterPattern |
| 104 |
} |
| 105 |
|
| 106 |
function search(text) { |
| 107 |
pageKeysResume.filterPattern = text ? new RegExp(text, 'i') : new RegExp("") |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
Loader { |
| 112 |
id: emptyStateLoader |
| 113 |
anchors.fill: parent |
| 114 |
active: storedKeys.count === 0 && !isListBusy |
| 115 |
source: Qt.resolvedUrl("EmptyDocument.qml") |
| 116 |
} |
| 117 |
|
| 118 |
Component.onCompleted: { |
| 119 |
initialize(); |
| 120 |
|
| 121 |
if (appsettins.runs == 50 || appsettins.runs == 100 || appsettins.runs == 200) { |
| 122 |
PopupUtils.open(Qt.resolvedUrl("DialogDonate.qml"), pageKeysResume, { 'runs': appsettins.runs } ) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
Connections { |
| 127 |
target: root |
| 128 |
|
| 129 |
onInitDB: { |
| 130 |
console.log('Received initDB signal'); |
| 131 |
initialize(); |
| 132 |
} |
| 133 |
|
| 134 |
//When tapping an element in the ListView, close the search view |
| 135 |
onLeftSearchView: { |
| 136 |
//Signal to clear the text in the SearchHeader |
| 137 |
searchHeader.closeHeader() |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
function initialize() { |
| 142 |
isListBusy = true; |
| 143 |
var allKeys = KeysDB.getKeys(); |
| 144 |
storedKeys.populate(allKeys.rows); |
| 145 |
isListBusy = false; |
| 146 |
} |
| 147 |
} |