openvpn-endpoint-server/client/ui/login_dialog.py

96 lines
2.9 KiB
Python

"""Login dialog for server authentication."""
from PyQt6.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
QLineEdit, QPushButton, QLabel, QMessageBox, QComboBox
)
from PyQt6.QtCore import Qt
from config import DEFAULT_SERVER_URL, APP_NAME
class LoginDialog(QDialog):
"""Login dialog for user authentication."""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle(f"{APP_NAME} - Login")
self.setMinimumWidth(400)
self.setModal(True)
self._setup_ui()
def _setup_ui(self):
"""Setup UI components."""
layout = QVBoxLayout(self)
# Title
title = QLabel(APP_NAME)
title.setStyleSheet("font-size: 18px; font-weight: bold; margin-bottom: 10px;")
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(title)
# Form
form = QFormLayout()
# Server URL
self.server_input = QComboBox()
self.server_input.setEditable(True)
self.server_input.addItem(DEFAULT_SERVER_URL)
self.server_input.setCurrentText(DEFAULT_SERVER_URL)
form.addRow("Server:", self.server_input)
# Username
self.username_input = QLineEdit()
self.username_input.setPlaceholderText("Enter username")
form.addRow("Username:", self.username_input)
# Password
self.password_input = QLineEdit()
self.password_input.setPlaceholderText("Enter password")
self.password_input.setEchoMode(QLineEdit.EchoMode.Password)
form.addRow("Password:", self.password_input)
layout.addLayout(form)
# Error label
self.error_label = QLabel()
self.error_label.setStyleSheet("color: red;")
self.error_label.setVisible(False)
layout.addWidget(self.error_label)
# Buttons
button_layout = QHBoxLayout()
button_layout.addStretch()
self.login_button = QPushButton("Login")
self.login_button.setDefault(True)
self.login_button.clicked.connect(self.accept)
button_layout.addWidget(self.login_button)
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.reject)
button_layout.addWidget(cancel_button)
layout.addLayout(button_layout)
# Enter key handling
self.password_input.returnPressed.connect(self.login_button.click)
def get_credentials(self) -> tuple[str, str, str]:
"""Get entered credentials."""
return (
self.server_input.currentText().strip(),
self.username_input.text().strip(),
self.password_input.text()
)
def show_error(self, message: str):
"""Show error message."""
self.error_label.setText(message)
self.error_label.setVisible(True)
def clear_error(self):
"""Clear error message."""
self.error_label.setVisible(False)