2020-12-18 20:55:33 +00:00
import QtQuick 2.12
import QtQuick . Controls 2.3
2022-11-28 14:57:56 +00:00
import QtQuick . Layouts 1.14
2020-12-18 20:55:33 +00:00
import QtGraphicalEffects 1.13
import QtQuick . Dialogs 1.3
2022-11-28 14:57:56 +00:00
import QtQml . Models 2.14
2021-09-28 15:04:06 +00:00
import utils 1.0
2021-10-27 21:27:49 +00:00
import shared . controls 1.0
2021-10-19 08:51:11 +00:00
import StatusQ . Core 0.1
import StatusQ . Core . Theme 0.1
2022-11-28 14:57:56 +00:00
import StatusQ . Popups . Dialog 0.1
import StatusQ . Controls 0.1
2021-10-19 08:51:11 +00:00
2022-11-28 14:57:56 +00:00
StatusDialog {
2021-10-22 20:49:47 +00:00
id: root
2021-11-30 14:49:36 +00:00
property var store
2022-11-28 14:57:56 +00:00
width: 640
title: qsTr ( "Import Community" )
2021-10-22 20:49:47 +00:00
2023-07-21 17:21:11 +00:00
signal joinCommunity ( string communityId , var communityDetails )
2023-10-12 14:35:59 +00:00
2022-11-28 14:57:56 +00:00
QtObject {
id: d
property string importErrorMessage
2023-07-31 15:56:00 +00:00
2023-10-12 14:35:59 +00:00
property bool loading
property bool communityFound: ( d . communityDetails !== null && ! ! d . communityDetails . name )
property var communityDetails: {
if ( ! isInputValid ) {
loading = false
return null
2023-07-31 15:56:00 +00:00
}
2023-10-12 14:35:59 +00:00
loading = true
const key = isPublicKey ? Utils . getCompressedPk ( publicKey ) :
root . store . getCommunityPublicKeyFromPrivateKey ( inputKey , true /*importing*/ ) ;
const details = root . store . getCommunityDetails ( key )
if ( ! ! details ) // the above can return `null` in which case we continue loading
loading = false
return details
2023-07-31 15:56:00 +00:00
}
2022-11-28 14:57:56 +00:00
readonly property string inputErrorMessage: isInputValid ? "" : qsTr ( "Invalid key" )
readonly property string errorMessage: importErrorMessage || inputErrorMessage
2023-05-15 10:31:23 +00:00
readonly property string inputKey: keyInput . text . trim ( )
2023-07-21 17:21:11 +00:00
readonly property bool isPrivateKey: ( Utils . isPrivateKey ( inputKey ) )
readonly property bool isPublicKey: ( publicKey !== "" )
2023-07-31 15:56:00 +00:00
readonly property string privateKey: inputKey
2023-05-15 10:31:23 +00:00
readonly property string publicKey: {
2023-07-21 17:21:11 +00:00
if ( ! Utils . isStatusDeepLink ( inputKey ) ) {
const key = Utils . dropCommunityLinkPrefix ( inputKey )
if ( ! Utils . isCommunityPublicKey ( key ) )
return ""
if ( ! Utils . isCompressedPubKey ( key ) )
return key
return Utils . changeCommunityKeyCompression ( key )
} else {
return Utils . getCommunityDataFromSharedLink ( inputKey ) . communityId ;
}
2023-05-15 10:31:23 +00:00
}
2022-11-28 14:57:56 +00:00
readonly property bool isInputValid: isPrivateKey || isPublicKey
2021-03-10 14:25:06 +00:00
}
2023-10-12 14:35:59 +00:00
Timer {
interval: 20000 // 20s
running: d . loading
onTriggered: {
d . loading = false
d . importErrorMessage = qsTr ( "Timeout reached while getting community info" )
}
}
Connections {
target: root . store
function onImportingCommunityStateChanged ( communityId , state , errorMsg ) {
switch ( state )
{
case Constants.communityImported:
const community = root . store . getCommunityDetailsAsJson ( communityId )
d . loading = false
d . communityFound = true
d . communityDetails = community
d . importErrorMessage = ""
break
case Constants.communityImportingInProgress:
d . loading = true
break
case Constants.communityImportingError:
d . loading = false
d . communityFound = false
d . communityDetails = null
d . importErrorMessage = errorMsg
break
default:
const msg = qsTr ( "Error state '%1' while importing community: %2" ) . arg ( state ) . arg ( communityId )
console . error ( msg )
d . loading = false
d . communityFound = false
d . communityDetails = null
d . importErrorMessage = msg
return
}
}
}
2022-11-28 14:57:56 +00:00
footer: StatusDialogFooter {
rightButtons: ObjectModel {
StatusButton {
2023-10-12 14:35:59 +00:00
enabled: d . communityFound && ( ( d . isPublicKey ) || ( d . isPrivateKey && agreeToKeepOnline . checked ) )
loading: d . loading
text: d . isPrivateKey && d . communityFound ? qsTr ( "Make this device the control node for %1" ) . arg ( d . communityDetails . name )
: qsTr ( "Import" )
2023-05-04 15:37:44 +00:00
onClicked: {
if ( d . isPrivateKey ) {
2023-07-31 15:56:00 +00:00
root . store . importCommunity ( d . privateKey ) ;
2023-05-04 15:37:44 +00:00
root . close ( ) ;
2023-07-31 15:56:00 +00:00
} else if ( d . isPublicKey ) {
2023-07-21 17:21:11 +00:00
root . joinCommunity ( d . publicKey , d . communityDetails ) ;
2023-05-04 15:37:44 +00:00
}
}
2022-11-28 14:57:56 +00:00
}
}
2020-12-18 20:55:33 +00:00
}
2023-07-31 15:56:00 +00:00
StatusScrollView {
id: scrollContent
anchors.fill: parent
anchors.leftMargin: Style . current . halfPadding
contentWidth: ( root . width - Style . current . bigPadding - Style . current . padding )
padding: 0
2022-03-23 10:56:25 +00:00
2023-07-31 15:56:00 +00:00
ColumnLayout {
width: ( scrollContent . width - Style . current . padding )
spacing: Style . current . halfPadding
2022-11-28 14:57:56 +00:00
2023-07-21 17:21:11 +00:00
StatusBaseText {
2023-07-31 15:56:00 +00:00
id: infoText1
Layout.fillWidth: true
text: qsTr ( "Enter the public key of the community you wish to access, or enter the private key of a community you own. Remember to always keep any private key safe and never share a private key with anyone else." )
wrapMode: Text . WordWrap
2023-10-12 14:35:59 +00:00
font.pixelSize: Style . current . additionalTextSize
2023-07-31 15:56:00 +00:00
color: Theme . palette . baseColor1
}
StatusBaseText {
id: inputLabel
text: qsTr ( "Community key" )
color: Theme . palette . directColor1
}
StatusTextArea {
id: keyInput
Layout.fillWidth: true
2023-10-02 12:20:15 +00:00
Layout.preferredHeight: 108
2023-07-31 15:56:00 +00:00
placeholderText: "0x0..."
wrapMode: TextEdit . WrapAtWordBoundaryOrAnywhere
onTextChanged: d . importErrorMessage = ""
}
RowLayout {
Layout.fillWidth: true
Layout.minimumHeight: 46
Layout.maximumHeight: 46
StatusChatInfoButton {
visible: d . communityFound
title: visible ? d.communityDetails.name : ""
subTitle: visible ? qsTr ( "%n member(s)" , "" , d . communityDetails . nbMembers ) : ""
asset.name: visible ? d.communityDetails.image : ""
asset.isImage: ( asset . name !== "" )
asset.color: visible ? d.communityDetails.color : ""
}
Item { Layout.fillWidth: true }
StatusBaseText {
id: detectionLabel
Layout.alignment: Qt . AlignRight
2023-10-12 14:35:59 +00:00
font.pixelSize: Style . current . additionalTextSize
visible: ! ! d . inputKey
2023-07-31 15:56:00 +00:00
text: {
if ( d . errorMessage !== "" ) {
return d . errorMessage
}
if ( d . isPrivateKey ) {
return qsTr ( "Private key detected" )
}
if ( d . isPublicKey ) {
return qsTr ( "Public key detected" )
}
2023-07-21 17:21:11 +00:00
}
2023-07-31 15:56:00 +00:00
color: d . errorMessage === "" ? Theme.palette.successColor1 : Theme . palette . dangerColor1
}
}
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
visible: ( d . communityFound && d . isPrivateKey )
Layout.topMargin: 12
spacing: Style . current . padding
StatusWarningBox {
Layout.fillWidth: true
icon: "caution"
text: qsTr ( "Another device might currently have the control node for this Community. Running multiple control nodes will cause unforeseen issues. Make sure you delete the private key in that other device in the community management tab." )
bgColor: borderColor
}
StatusDialogDivider { Layout.fillWidth: true ; Layout.topMargin: Style . current . padding }
StatusBaseText {
Layout.topMargin: Style . current . halfPadding
visible: ( d . communityFound && d . isPrivateKey )
text: qsTr ( "I acknowledge that..." )
}
StatusCheckBox {
id: agreeToKeepOnline
Layout.fillWidth: true
text: qsTr ( "I must keep this device online and running Status for the Community to function" )
2022-11-28 14:57:56 +00:00
}
2022-02-02 17:54:28 +00:00
}
2021-02-12 18:19:31 +00:00
}
2020-12-18 20:55:33 +00:00
}
}