Skip to content

Commit

Permalink
handle signals and card states
Browse files Browse the repository at this point in the history
  • Loading branch information
bitgamma authored and iurimatias committed Oct 6, 2021
1 parent a22d936 commit ce1be69
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/app/keycard/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import NimQml, chronicles, std/wrapnils
import status/[signals, status, keycard]
import view

logScope:
topics = "keycard-model"

type KeycardController* = ref object
view*: KeycardView
variant*: QVariant
Expand Down Expand Up @@ -29,18 +32,22 @@ proc getCardState(self: KeycardController) =

if not appInfo.installed:
self.view.cardState = NotKeycard
self.view.cardNotKeycard()
elif not appInfo.initialized:
self.view.cardState = PreInit
self.view.cardPreInit()
elif self.attemptOpenSecureChannel():
# here we will also be able to check if the card is Frozen/Blocked
self.view.cardState = Paired
self.view.cardPaired()
elif appInfo.availableSlots > 0:
self.view.cardState = Unpaired
self.view.cardUnpaired()
else:
self.view.cardState = NoFreeSlots
self.view.cardNoFreeSlots()

proc init*(self: KeycardController) =
discard """
self.status.events.on(SignalType.KeycardConnected.event) do(e:Args):
getCardState()
self.view.cardConnected()
"""
self.getCardState()
self.view.cardConnected()
21 changes: 15 additions & 6 deletions src/app/keycard/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ QtObject:
result.setup

proc cardConnected*(self: KeycardView) {.signal.}

proc cardDisconnected*(self: KeycardView) {.signal.}

proc cardStateChanged*(self: KeycardView, cardState: int) {.signal.}
proc cardNotKeycard*(self: KeycardView) {.signal.}
proc cardPreInit*(self: KeycardView) {.signal.}
proc cardUnpaired*(self: KeycardView) {.signal.}
proc cardNoFreeSlots*(self: KeycardView) {.signal.}
proc cardPaired*(self: KeycardView) {.signal.}
proc cardFrozen*(self: KeycardView) {.signal.}
proc cardBlocked*(self: KeycardView) {.signal.}
proc cardAuthenticated*(self: KeycardView) {.signal.}

proc startConnection*(self: KeycardView) {.slot.} =
discard self.status.keycard.start()
Expand All @@ -47,6 +52,10 @@ QtObject:
self.cardState = Disconnected
discard self.status.keycard.stop()

proc `cardState=`*(self: KeycardView, cardState: CardState) =
self.cardState = cardState
self.cardStateChanged(int(cardState))
proc pair*(self: KeycardView, password: string) {.slot.} =
discard """
on succesful pairing, save and change card state
otherwise throw error
self.status.keycard.pair(password)
"""
52 changes: 49 additions & 3 deletions ui/onboarding/Keycard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ Item {
CreatePINModal {
id: createPinModal
onClosed: function () {
pairingModal.open()
keycardView.onClosed()
}
}

PairingModal {
id: pairingModal
onClosed: function () {
pinModal.open()
if (!pairingModal.submitted) {
keycardView.onClosed()
}
}
}

PINModal {
id: pinModal
onClosed: function () {
keycardView.open()
keycardView.onClosed()
}
}

Expand All @@ -41,4 +43,48 @@ Item {
keycardView.onClosed()
}
}

Connections {
id: connection
target: keycardModel
ignoreUnknownSignals: true

onCardUnpaired: {
pairingModal.open()
}

onCardPaired: {

}

//TODO: support the states below

onCardPreInit: {
keycardView.onClosed()
}

onCardFrozen: {
keycardView.onClosed()

}

onCardBlocked: {
keycardView.onClosed()
}

// TODO: handle these by showing an error an prompting for another card
// later add factory reset option for the NoFreeSlots case

onCardNoFreeSlots: {
//status-lib currently always returns availableSlots = 0 so we end up here
//keycardView.onClosed()
pairingModal.open()
}

onCardNotKeycard: {
keycardView.onClosed()

}

}
}
5 changes: 4 additions & 1 deletion ui/shared/keycard/PairingModal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "../../shared"

ModalPopup {
property bool pairingPasswordFieldValid: false
property bool submitted: false

id: popup
title: qsTr("Insert pairing code")
Expand Down Expand Up @@ -57,7 +58,9 @@ ModalPopup {
enabled: pairingPasswordFieldValid

onClicked: {

submitted = true
keycardModel.pair(pairingPasswordField.text)
popup.close()
}
}
}
Expand Down

0 comments on commit ce1be69

Please sign in to comment.