Store selected key instead of model and index

This commit is contained in:
Emil Lundberg 2017-11-29 14:45:53 +01:00
parent 6604569500
commit acf998481a
No known key found for this signature in database
GPG Key ID: 1342CC2B5CF84F44
4 changed files with 44 additions and 37 deletions

View File

@ -6,11 +6,11 @@ Item {
property var credRepeater
property int nCreds: credRepeater.model.length
property bool nothingSelected: selectedIndex === null
property bool nothingSelected: selectedKey === null
property var firstCred: credRepeater.model[0]
property var lastCred: credRepeater.model[nCreds - 1]
property bool lastCredSelected: selectedIndex === nCreds - 1
property bool firstCredSeleced: selectedIndex === 0
property bool lastCredSelected: lastCred !== undefined && selectedKey === lastCred.credential.key
property bool firstCredSeleced: firstCred !== undefined && selectedKey === firstCred.credential.key
signal goDown()
signal goUp()
@ -18,14 +18,18 @@ Item {
Keys.onDownPressed: goDown()
Keys.onUpPressed: goUp()
function findSelectedIndex() {
return credRepeater.model.findIndex(function(entry) {
return entry.credential.key === selectedKey
}) || null
}
onGoDown: {
flickable.flick(0, -300)
if (nothingSelected) {
selected = firstCred
selectedIndex = 0
selectedKey = firstCred.credential.key
} else if (!lastCredSelected) {
selected = credRepeater.model[selectedIndex + 1]
selectedIndex = selectedIndex + 1
selectedKey = credRepeater.model[findSelectedIndex() + 1].credential.key
}
}
@ -33,11 +37,9 @@ Item {
onGoUp: {
flickable.flick(0, 300)
if (nothingSelected) {
selected = lastCred
selectedIndex = nCreds - 1
selectedKey = lastCred.credential.key
} else if (!firstCredSeleced) {
selected = credRepeater.model[selectedIndex - 1]
selectedIndex = selectedIndex - 1
selectedKey = credRepeater.model[findSelectedIndex() - 1].credential.key
}
}

View File

@ -55,7 +55,7 @@ MenuBar {
MenuItem {
text: qsTr("\&Copy to clipboard")
shortcut: StandardKey.Copy
enabled: (selected != null) && (selected.code != null)
enabled: (getSelected() != null) && (getSelected().code != null)
onTriggered:copy()
}
@ -72,7 +72,7 @@ MenuBar {
MenuItem {
text: qsTr("\&Delete")
shortcut: StandardKey.Delete
enabled: (selected != null)
enabled: (getSelected() != null)
onTriggered: deleteCredential()
}
}

View File

@ -148,9 +148,9 @@ Python {
}
// The selected credential should still be selected,
// with an updated code.
if (selected != null) {
if (selected.credential.key === entry.credential.key) {
selected = entry
if (getSelected() != null) {
if (getSelected().credential.key === entry.credential.key) {
selectCredential(entry)
}
}
@ -248,7 +248,7 @@ Python {
// Update the selected credential
// after update, since the code now
// might be available.
selected = entry
selectCredential(entry)
if (copyAfterUpdate) {
copy()
}

View File

@ -23,8 +23,7 @@ ApplicationWindow {
property bool ccidModeMatch: (!settings.slotMode && device.hasCCID)
property var hotpCoolDowns: []
property var selected: null
property var selectedIndex: null
property var selectedKey: null
// Don't refresh credentials when window is minimized or hidden
// See http://doc.qt.io/qt-5/qwindow.html#Visibility-enum
@ -39,15 +38,15 @@ ApplicationWindow {
signal deleteCredential
onDeleteCredential: confirmDeleteCredential.open()
onGenerate: handleGenerate(selected, copyAfterGenerate)
onCopy: clipboard.setClipboard(selected.code.value)
onGenerate: handleGenerate(getSelected(), copyAfterGenerate)
onCopy: clipboard.setClipboard(getSelected().code.value)
onHasDeviceChanged: handleNewDevice()
menuBar: MainMenuBar {
slotMode: settings.slotMode
hasDevice: device.hasDevice
enableGenerate: enableManualGenerate(selected)
enableGenerate: enableManualGenerate(getSelected())
onOpenAddCredential: openClearAddCredential()
onOpenSetPassword: setPassword.open()
onOpenReset: reset.open()
@ -195,9 +194,9 @@ ApplicationWindow {
CredentialMenu {
id: credentialMenu
credential: selected
showGenerate: allowManualGenerate(selected)
enableGenerate: enableManualGenerate(selected)
credential: getSelected()
showGenerate: allowManualGenerate(credential)
enableGenerate: enableManualGenerate(credential)
}
DeleteCredentialConfirmation {
@ -290,7 +289,7 @@ ApplicationWindow {
// A double-click should select the credential,
// then generate if needed and copy the code.
selectCredential(modelData, index)
selectCredential(modelData)
generateOrCopy()
}
@ -304,13 +303,13 @@ ApplicationWindow {
if (appWindow.isSelected(modelData.credential)) {
deselectCredential()
} else {
selectCredential(modelData, index)
selectCredential(modelData)
}
}
// Right-click, select credential and open popup menu.
if (mouse.button & Qt.RightButton) {
selectCredential(modelData, index)
selectCredential(modelData)
credentialMenu.popup()
}
}
@ -377,17 +376,22 @@ ApplicationWindow {
id: noQr
}
function selectCredential(entry, index) {
selected = entry
selectedIndex = index
function selectCredential(entry) {
selectedKey = entry.credential.key
}
function deselectCredential() {
selected = null
selectedIndex = null
selectedKey = null
}
function getSelected() {
return credentials.find(function(entry) {
return entry.credential.key === selectedKey
}) || null
}
function isSelected(credential) {
var selected = getSelected()
return selected != null && selected.credential.key === credential.key
}
@ -491,10 +495,10 @@ ApplicationWindow {
// reset selected to avoid hidden selected creds.
deselectCredential()
}
if (selected === null) {
if (selectedKey === null) {
// If the search gave some results, and nothing is currently selected,
// the top credential should be selected.
selectCredential(searchResult[0], 0)
selectCredential(searchResult[0])
}
} else {
// If search was started but no result,
@ -598,13 +602,14 @@ ApplicationWindow {
function deleteSelectedCredential() {
if (settings.slotMode) {
device.deleteSlotCredential(getSlot(selected.credential.name))
device.deleteSlotCredential(getSlot(getSelected().credential.name))
} else {
device.deleteCredential(selected.credential)
device.deleteCredential(getSelected().credential)
}
}
function generateOrCopy() {
var selected = getSelected()
if (selected.code == null || isExpired(selected) || selected.credential.oath_type === 'HOTP') {
generate(true)
} else {