| 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 Qt.labs.settings 1.0 |
| 23 |
import Lomiri.Components.Popups 1.3 |
| 24 |
import QtQuick.LocalStorage 2.0 |
| 25 |
import Lomiri.Content 1.3 |
| 26 |
|
| 27 |
|
| 28 |
//Only need to import OTPAuth once |
| •29 |
import "js/otpauth.js" as OTPAuth |
| 30 |
import "js/db.js" as KeysDB |
| 31 |
import "Components" |
| 32 |
|
| 33 |
MainView { |
| 34 |
id: root |
| 35 |
objectName: 'mainView' |
| 36 |
anchorToKeyboard: false |
| 37 |
|
| 38 |
/* If applicationName starts by a number the UrlDispatcher |
| 39 |
* ignores the incoming link |
| 40 |
* See: https://github.com/ubports/url-dispatcher/issues/7 |
| 41 |
*/ |
| 42 |
applicationName: 'tfamanager.cibersheep' |
| 43 |
automaticOrientation: true |
| 44 |
|
| 45 |
property string uriToRender |
| 46 |
property bool resumeIsMainPage: true |
| 47 |
property bool isResumeFlicking: false |
| 48 |
|
| 49 |
//App Color Properties |
| 50 |
readonly property color mainColor: "#594092" |
| 51 |
readonly property color secondaryColor: "#7b66b5" |
| 52 |
readonly property color highlightColor: "#447b66b5" |
| 53 |
readonly property color titleColor: LomiriColors.porcelain |
| 54 |
|
| 55 |
signal initDB() |
| 56 |
signal leftSearchView() |
| 57 |
|
| 58 |
width: units.gu(45) |
| 59 |
height: units.gu(75) |
| 60 |
|
| 61 |
Settings { |
| 62 |
id: theming |
| 63 |
category: "Theming Settings" |
| 64 |
property string themeName: "" |
| 65 |
|
| 66 |
onThemeNameChanged: { |
| 67 |
setTheme(); |
| 68 |
bottomEdge.refresh(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
Settings { |
| 73 |
id: appsettins |
| 74 |
category: "General" |
| 75 |
|
| 76 |
property real runs: 0 |
| 77 |
} |
| 78 |
|
| 79 |
PageStack { |
| 80 |
id: mainStack |
| 81 |
anchors.fill: parent |
| 82 |
|
| 83 |
onCurrentPageChanged: { |
| 84 |
console.log("Current PageStack Page: " + currentPage.objectName) |
| 85 |
resumeIsMainPage = currentPage.objectName == "resumePage" |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
BottomEdge { |
| 90 |
id: bottomEdge |
| 91 |
enabled: resumeIsMainPage && !isResumeFlicking && !Qt.inputMethod.visible |
| 92 |
//Workaround. On Focal dev BottomEdge covers the PageHeader (?) |
| 93 |
height: status.Hidden ? 0 : root.height |
| 94 |
hint.visible: enabled |
| 95 |
hint.text: i18n.tr("Add Key") |
| 96 |
preloadContent: false |
| 97 |
|
| 98 |
contentUrl: Qt.resolvedUrl("Components/PageEditAddKey.qml") |
| 99 |
|
| 100 |
//Workaround to refresh BottomEdge when theme changes |
| 101 |
function refresh() { |
| 102 |
commit(); |
| 103 |
collapse(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
//Handle incoming otpauth:// url |
| 108 |
Connections { |
| 109 |
target: UriHandler |
| 110 |
|
| 111 |
onOpened: { |
| 112 |
if (uris.length > 0) { |
| 113 |
for (var i = 0; i < uris.length; i++) { |
| 114 |
if (uris[i].match(/^otpauth/)) { |
| 115 |
console.log('URI from UriHandler'); |
| 116 |
renderOTPuri(uris[i]); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Connections { |
| 124 |
target: ContentHub |
| 125 |
|
| 126 |
onImportRequested: { |
| 127 |
console.log('Import from ContentHub'); |
| 128 |
for (var i = 0; i < transfer.items.length; i++) { |
| 129 |
renderOTPuri(transfer.items[i].text) |
| 130 |
} |
| 131 |
|
| 132 |
transfer.finalize() |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
Component.onCompleted: { |
| 137 |
//Check if opened the app because we have an incoming uri |
| 138 |
if (Qt.application.arguments && Qt.application.arguments.length > 0) { |
| 139 |
for (var i = 0; i < Qt.application.arguments.length; i++) { |
| 140 |
|
| 141 |
if (Qt.application.arguments[i].match(/^otpauth/)) { |
| 142 |
console.log("Received otpauth URIs as Qt.application.arguments") |
| 143 |
renderOTPuri(Qt.application.arguments[i]); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
setTheme(); |
| 149 |
appsettins.runs = appsettins.runs + 1; |
| 150 |
mainStack.push(storedKeysComponent); |
| 151 |
} |
| 152 |
|
| 153 |
Component { |
| 154 |
//Pop up to add a key |
| 155 |
id: renderUriComponent |
| 156 |
|
| 157 |
RenderUriPopUp {} |
| 158 |
} |
| 159 |
|
| 160 |
Component { |
| 161 |
//Delegate of ListItem of codes |
| 162 |
id: keysDelegate |
| 163 |
|
| 164 |
KeysDelegate {} |
| 165 |
} |
| 166 |
|
| 167 |
Component { |
| 168 |
//ListItem of codes |
| 169 |
id: storedKeysComponent |
| 170 |
|
| 171 |
PageKeysResume { |
| 172 |
id: storedKeysPage |
| 173 |
objectName: "resumePage" |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
Timer { |
| 178 |
//General ticker. It ticks every second |
| 179 |
id: timer |
| 180 |
running: true |
| 181 |
repeat: true |
| 182 |
|
| 183 |
property int time |
| 184 |
|
| 185 |
onTriggered: time = Math.round(new Date().getTime() / 1000.0) |
| 186 |
} |
| 187 |
|
| 188 |
Timer { |
| 189 |
//Wait 250 miliseconds to load the BottomEdge content. Should improve app start time |
| 190 |
id: botEdgeTimer |
| 191 |
interval: 250 |
| 192 |
running: true |
| 193 |
repeat: false |
| 194 |
|
| 195 |
onTriggered: bottomEdge.preloadContent = true |
| 196 |
} |
| 197 |
|
| 198 |
function renderOTPuri(uri) { |
| 199 |
/* uriToRender is a general var, otherwise if apps opens from UrlDispatcher, |
| 200 |
* PopUtils.open(renderUriComponent, caller, var) complains about caller not being ready |
| 201 |
* See issue: https://github.com/ubports/ubuntu-ui-toolkit/issues/43 |
| 202 |
*/ |
| 203 |
uriToRender = uri |
| 204 |
|
| 205 |
PopupUtils.open(renderUriComponent) |
| 206 |
} |
| 207 |
|
| 208 |
function checkUri(uri) { |
| 209 |
console.log("Checking URI") |
| 210 |
var checkIssuer = findParam("issuer=", uri, "&"); |
| 211 |
var checkFirstIssuer; |
| 212 |
|
| 213 |
checkFirstIssuer = findParam("", uri.slice(15), ":"); |
| 214 |
|
| 215 |
if (!checkFirstIssuer) { |
| 216 |
//We have no issuer and ":[user]" |
| 217 |
console.log("Checking problem with 'issuer'. We have no issuer and ':[user]'") |
| 218 |
return uri.replace("/:","/") |
| 219 |
} |
| 220 |
|
| 221 |
if (checkIssuer) { |
| 222 |
console.log("Checking problem with issuer (2)...") |
| 223 |
if (checkFirstIssuer !== checkIssuer) { |
| 224 |
console.log(checkFirstIssuer,checkIssuer) |
| 225 |
uri = uri.replace("issuer=" + checkIssuer,"") |
| 226 |
console.log("uri",uri,uri.replace("?&","?")) |
| 227 |
return uri.replace("?&","?") |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return uri; |
| 232 |
} |
| 233 |
|
| 234 |
function findParam(thisTerm, inText, withSeparator) { |
| 235 |
//Returns xx in [thisTerm]xx[separator or end of inText] |
| 236 |
if (inText.indexOf(withSeparator) == -1) { |
| 237 |
return "" |
| 238 |
} else { |
| 239 |
return inText.slice(inText.indexOf(thisTerm) + thisTerm.length, inText.indexOf(withSeparator) || inText.length); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
function formatCode(code, split) { |
| 244 |
//Format the code to add spaces only for viewing it |
| 245 |
if (split) { |
| 246 |
if (code.length > 6) { |
| 247 |
return code.replace(/(\d{3})(\d{3})(\d)/, '$1 $2 $3'); |
| 248 |
} else { |
| 249 |
return code.replace(/(\d{3})(\d{3})/, '$1 $2'); |
| 250 |
} |
| 251 |
} else { |
| 252 |
return code.replace(/ /g, ""); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
function setTheme() { |
| 257 |
theme.name = theming.themeName |
| 258 |
} |
| 259 |
} |