Warning, /network/neochat/src/qml/ServerComboBox.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 import QtQuick
0005 import QtQuick.Controls as QQC2
0006 import QtQuick.Layouts
0007 
0008 import org.kde.kirigami as Kirigami
0009 import org.kde.kirigamiaddons.delegates as Delegates
0010 
0011 import org.kde.neochat
0012 
0013 QQC2.ComboBox {
0014     id: root
0015 
0016     /**
0017      * @brief The connection for the current local user.
0018      */
0019     required property NeoChatConnection connection
0020 
0021     /**
0022      * @brief The server to get the search results from.
0023      */
0024     property string server
0025 
0026     Layout.preferredWidth: Kirigami.Units.gridUnit * 10
0027     Component.onCompleted: currentIndex = 0
0028 
0029     textRole: "url"
0030     valueRole: "url"
0031     model: ServerListModel {
0032         id: serverListModel
0033         connection: root.connection
0034     }
0035 
0036     delegate: Delegates.RoundedItemDelegate {
0037         id: serverItem
0038 
0039         required property int index
0040         required property string url
0041         required property bool isAddServerDelegate
0042         required property bool isHomeServer
0043         required property bool isDeletable
0044 
0045         text: isAddServerDelegate ? i18n("Add New Server") : url
0046         highlighted: false
0047 
0048         topInset: index === 0 ? Kirigami.Units.smallSpacing : Math.round(Kirigami.Units.smallSpacing / 2)
0049         bottomInset: index === ListView.view.count - 1 ? Kirigami.Units.smallSpacing : Math.round(Kirigami.Units.smallSpacing / 2)
0050 
0051         onClicked: if (isAddServerDelegate) {
0052             addServerSheet.open();
0053         }
0054 
0055         contentItem: RowLayout {
0056             spacing: Kirigami.Units.smallSpacing
0057 
0058             Delegates.SubtitleContentItem {
0059                 itemDelegate: serverItem
0060                 subtitle: serverItem.isHomeServer ? i18n("Home Server") : ""
0061                 Layout.fillWidth: true
0062             }
0063 
0064             QQC2.ToolButton {
0065                 visible: serverItem.isAddServerDelegate || serverItem.isDeletable
0066                 icon.name: serverItem.isAddServerDelegate ? "list-add" : "dialog-close"
0067                 text: i18nc("@action:button", "Add new server")
0068                 Accessible.name: text
0069                 display: QQC2.AbstractButton.IconOnly
0070 
0071                 onClicked: {
0072                     if (root.currentIndex === serverItem.index && serverItem.isDeletable) {
0073                         root.currentIndex = 0;
0074                         root.server = root.currentValue;
0075                         root.popup.close();
0076                     }
0077                     if (serverItem.isAddServerDelegate) {
0078                         addServerSheet.open();
0079                         serverItem.clicked();
0080                     } else {
0081                         serverListModel.removeServerAtIndex(serverItem.index);
0082                     }
0083                 }
0084             }
0085         }
0086     }
0087 
0088     onActivated: {
0089         if (currentIndex !== count - 1) {
0090             root.server = root.currentValue;
0091         }
0092     }
0093 
0094     Kirigami.OverlaySheet {
0095         id: addServerSheet
0096 
0097         parent: applicationWindow().overlay
0098 
0099         title: i18nc("@title:window", "Add server")
0100 
0101         onOpened: if (!serverUrlField.isValidServer && !addServerSheet.opened) {
0102             root.currentIndex = 0;
0103             root.server = root.currentValue;
0104         } else if (addServerSheet.opened) {
0105             serverUrlField.forceActiveFocus();
0106         }
0107 
0108         onClosed: if (serverUrlField.length <= 0) {
0109             root.currentIndex = root.indexOfValue(root.server);
0110         }
0111 
0112         contentItem: Kirigami.FormLayout {
0113             QQC2.Label {
0114                 Layout.minimumWidth: Kirigami.Units.gridUnit * 20
0115 
0116                 text: serverUrlField.length > 0 ? (serverUrlField.acceptableInput ? (serverUrlField.isValidServer ? i18n("Valid server entered") : i18n("This server cannot be resolved or has already been added")) : i18n("The entered text is not a valid url")) : i18n("Enter server url e.g. kde.org")
0117                 color: serverUrlField.length > 0 ? (serverUrlField.acceptableInput ? (serverUrlField.isValidServer ? Kirigami.Theme.positiveTextColor : Kirigami.Theme.negativeTextColor) : Kirigami.Theme.negativeTextColor) : Kirigami.Theme.textColor
0118             }
0119             QQC2.TextField {
0120                 id: serverUrlField
0121 
0122                 property bool isValidServer: false
0123 
0124                 Kirigami.FormData.label: i18n("Server URL")
0125                 onTextChanged: {
0126                     if (acceptableInput) {
0127                         serverListModel.checkServer(text);
0128                     }
0129                 }
0130 
0131                 validator: RegularExpressionValidator {
0132                     regularExpression: /^[a-zA-Z0-9-]{1,61}\.([a-zA-Z]{2,}|[a-zA-Z0-9-]{2,}\.[a-zA-Z]{2,3})$/
0133                 }
0134 
0135                 Connections {
0136                     target: serverListModel
0137                     function onServerCheckComplete(url, valid) {
0138                         if (url == serverUrlField.text && valid) {
0139                             serverUrlField.isValidServer = true;
0140                         }
0141                     }
0142                 }
0143             }
0144 
0145             QQC2.Button {
0146                 id: okButton
0147 
0148                 text: i18nc("@action:button", "Ok")
0149                 enabled: serverUrlField.acceptableInput && serverUrlField.isValidServer
0150                 onClicked: {
0151                     serverListModel.addServer(serverUrlField.text);
0152                     root.currentIndex = root.indexOfValue(serverUrlField.text);
0153                     root.server = root.currentValue;
0154                     serverUrlField.text = "";
0155                     addServerSheet.close();
0156                 }
0157             }
0158         }
0159     }
0160 }