// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // The Telegram sign-in: the number, the code it sends, the password that account // may have, and the QR login that needs neither. // // The page asks the transport what state it is in rather than keeping one of its // own: TDLib decides this, and a second copy is a second thing to be wrong. import QtQuick import QtQuick.Layouts import QtQuick.Controls as QQC2 import org.kde.kirigami as Kirigami import org.kde.kirigamiaddons.formcard as FormCard import org.kde.prison import koutnet.app FormCard.FormCardPage { id: root title: i18nc("@title:window", "Telegram account") // The values of TelegramAuthState on the C++ side. QML has no other way to // read an enum, and a second numbering to keep in step is a second thing to // be wrong. readonly property int needApiCredentials: 1 readonly property int needPhoneNumber: 2 readonly property int needCode: 3 readonly property int needPassword: 4 readonly property int ready: 5 readonly property int state: telegramRooms.authState readonly property bool hasCredentials: appSettings.telegramApiId.length > 0 && appSettings.telegramApiHash.length > 0 Connections { target: telegramRooms function onAuthStateChanged() { // The code arrives in the same place every time, and a page sitting // on a stale state would send it as a phone number. if (root.state === root.needCode) codeField.text = "" } } ColumnLayout { Layout.fillWidth: true spacing: Kirigami.Units.largeSpacing ThemedFormCard { Layout.fillWidth: true // One line for the state rather than one per state: a form that // shows nothing and says nothing is the case worth fixing, and it // is covered by the field below rather than by a second card. FormCard.FormTextDelegate { icon.name: root.state === root.ready ? "dialog-information" : "send-to" text: { if (root.state !== root.ready) { return root.state === root.needApiCredentials ? i18nc("@info:status the application credentials are missing", "No application credentials") : (root.state === root.needCode ? i18nc("@info:status waiting for login code", "Enter login code") : (root.state === root.needPassword ? i18nc("@info:status waiting for two-step verification password", "Two-step verification password required") : i18nc("@info:status the account is not signed in yet", "Not signed in"))) } const uname = telegramRooms.ownUsername || "" const dname = telegramRooms.ownDisplayName || "" if (uname.length > 0 && dname.length > 0 && uname !== dname) return i18nc("@info:status Telegram account is signed in as user", "Signed in as %1 (%2)", dname, uname) if (uname.length > 0) return i18nc("@info:status Telegram account is signed in as user", "Signed in as %1", uname) if (dname.length > 0) return i18nc("@info:status Telegram account is signed in as user", "Signed in as %1", dname) return i18nc("@info:status the account is signed in", "Signed in") } description: root.state === root.needApiCredentials ? i18nc("@info:whatsthis", "Telegram issues an api id and an api hash at my.telegram.org. They go in the settings, under the Telegram tab, and nothing here works until they are there.") : (root.state === root.ready ? i18nc("@info:whatsthis", "Chats and messages are on their way.") : (root.state === root.needCode ? i18nc("@info:whatsthis", "Enter the login code sent by Telegram (SMS or in-app message).") : (root.state === root.needPassword ? i18nc("@info:whatsthis", "Enter your Telegram two-step verification (cloud) password. This is not the SMS/login code and not your phone password.") : i18nc("@info:whatsthis", "Enter the phone number of the account, or sign in with a QR code if available.")))) } // What Telegram said no to. Above the fields, because it belongs to // the whole form rather than to whichever one was last pressed. FormCard.FormTextDelegate { visible: telegramRooms.authError.length > 0 icon.name: "dialog-warning" text: i18nc("@info:status what Telegram refused", "Telegram refused that") description: telegramRooms.authError } FormCard.FormTextFieldDelegate { id: phoneField visible: root.state === root.needPhoneNumber && root.hasCredentials label: i18nc("@label:textbox", "Phone number") placeholderText: i18nc("@info:placeholder an international number", "+7 900 000 00 00") onAccepted: telegramRooms.sendPhoneNumber(phoneField.text) } FormCard.FormTextFieldDelegate { id: codeField visible: root.state === root.needCode label: i18nc("@label:textbox", "Login code") placeholderText: i18nc("@info:placeholder the code Telegram sent", "00000") inputMethodHints: Qt.ImhDigitsOnly onAccepted: telegramRooms.sendCode(codeField.text) } FormCard.FormTextFieldDelegate { id: passwordField visible: root.state === root.needPassword label: i18nc("@label:textbox Telegram two-step verification password", "Two-step verification password") placeholderText: i18nc("@info:placeholder Telegram 2FA cloud password", "Cloud password (2FA)") echoMode: TextInput.Password onAccepted: telegramRooms.sendPassword(passwordField.text) } FormCard.FormButtonDelegate { visible: root.state === root.needPhoneNumber && root.hasCredentials text: i18nc("@action:button send the phone number to Telegram", "Send") icon.name: "go-next" onClicked: telegramRooms.sendPhoneNumber(phoneField.text) } FormCard.FormButtonDelegate { visible: root.state === root.needPhoneNumber && root.hasCredentials text: i18nc("@action:button request Telegram QR code login", "Sign in with QR code") icon.name: "view-barcode-qr" onClicked: telegramRooms.requestQrCodeLogin() } FormCard.FormButtonDelegate { visible: root.state === root.needCode text: i18nc("@action:button confirm the login code", "Sign in") icon.name: "go-next" onClicked: telegramRooms.sendCode(codeField.text) } FormCard.FormButtonDelegate { visible: root.state === root.needPassword text: i18nc("@action:button send the Telegram two-step verification password", "Submit password") icon.name: "go-next" onClicked: telegramRooms.sendPassword(passwordField.text) } } // The QR login, when Telegram offers one: scan with Telegram on another // device to confirm sign-in. ThemedFormCard { Layout.fillWidth: true visible: telegramRooms.qrCodeUrl.length > 0 FormCard.AbstractFormDelegate { background: null contentItem: ColumnLayout { spacing: Kirigami.Units.largeSpacing Kirigami.Heading { Layout.alignment: Qt.AlignHCenter level: 4 text: i18nc("@info:status signing in by picture", "Sign in by QR code") } Rectangle { Layout.alignment: Qt.AlignHCenter Layout.preferredWidth: Math.min(root.width - Kirigami.Units.gridUnit * 6, Kirigami.Units.gridUnit * 18) Layout.preferredHeight: Layout.preferredWidth radius: Kirigami.Units.smallSpacing color: "white" Barcode { anchors.fill: parent anchors.margins: Kirigami.Units.smallSpacing barcodeType: Barcode.QRCode content: telegramRooms.qrCodeUrl foregroundColor: "black" backgroundColor: "white" } } QQC2.Label { Layout.fillWidth: true horizontalAlignment: Text.AlignHCenter wrapMode: Text.Wrap opacity: 0.7 text: i18nc("@info", "Scan this code with Telegram on your phone (Settings → Devices → Link Desktop Device).") } QQC2.Label { Layout.fillWidth: true horizontalAlignment: Text.AlignHCenter wrapMode: Text.WrapAnywhere opacity: 0.6 font.pointSize: Kirigami.Theme.smallFont.pointSize text: telegramRooms.qrCodeUrl } } } FormCard.FormButtonDelegate { text: i18nc("@action:button open the Telegram QR login in a browser", "Open link in browser") icon.name: "view-conjugate" onClicked: Qt.openUrlExternally(telegramRooms.qrCodeUrl) } } Item { Layout.fillHeight: true } } }