Files
2026-12-01 19:59:12 +01:00

191 lines
6.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QCursor
from qgis.PyQt.QtWidgets import (
QApplication,
QCheckBox,
QDialog,
QDialogButtonBox,
QFormLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QMessageBox,
QPushButton,
QSpinBox,
QVBoxLayout,
)
from .. import auth, config, settings
from ..client import ApiClient
from ..compat import icon
from ..errors import GeoDonkeyError
from ..i18n import tr
from ..net import QgisTransport
from . import theme
class SettingsDialog(QDialog):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.setWindowTitle(tr("GeoDonkey settings"))
self.setWindowIcon(icon("geodonkey.svg"))
self.setMinimumWidth(460)
self._build()
self._load()
def _build(self) -> None:
box = QVBoxLayout(self)
box.setSpacing(8)
intro = QLabel(
tr("An API key unlocks DonkeyOSM queries. The map styles work without one.")
+ ' <a href="%s">%s</a>' % (config.TOKEN_URL, tr("Get a key")))
intro.setOpenExternalLinks(True)
intro.setWordWrap(True)
box.addWidget(intro)
form = QFormLayout()
form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
self.token = QLineEdit()
self.token.setEchoMode(QLineEdit.EchoMode.Password)
self.token.setPlaceholderText("gd_live_…")
self.token.textChanged.connect(self._token_changed)
show = QCheckBox(tr("Show"))
show.toggled.connect(
lambda on: self.token.setEchoMode(
QLineEdit.EchoMode.Normal if on else QLineEdit.EchoMode.Password))
key_row = QHBoxLayout()
key_row.addWidget(self.token, 1)
key_row.addWidget(show)
form.addRow(tr("API key"), key_row)
self.api_url = QLineEdit()
self.api_url.setPlaceholderText(config.DEFAULT_API_URL)
form.addRow(tr("API address"), self.api_url)
self.limit = QSpinBox()
self.limit.setRange(1, config.OSM_LIMIT_PRO)
self.limit.setSingleStep(10)
form.addRow(tr("Default maximum objects"), self.limit)
box.addLayout(form)
buttons = QHBoxLayout()
self.test_button = QPushButton(tr("Test key"))
self.test_button.setStyleSheet(theme.primary_button_style())
self.test_button.clicked.connect(self.test_token)
self.forget_button = QPushButton(tr("Remove key"))
self.forget_button.clicked.connect(self.forget_token)
buttons.addWidget(self.test_button)
buttons.addWidget(self.forget_button)
buttons.addStretch(1)
box.addLayout(buttons)
self.note = QLabel("")
self.note.setWordWrap(True)
self.note.setStyleSheet(theme.note_style())
box.addWidget(self.note)
self.storage_note = QLabel("")
self.storage_note.setWordWrap(True)
self.storage_note.setStyleSheet(theme.note_style())
theme.small(self.storage_note)
box.addWidget(self.storage_note)
standard = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
standard.accepted.connect(self.accept)
standard.rejected.connect(self.reject)
box.addWidget(standard)
def _load(self) -> None:
self.token.setText(auth.load_token())
self.api_url.setText(settings.api_url())
self.limit.setValue(int(settings.get("limit")))
if auth.available():
self.storage_note.setText(
tr("The key is stored encrypted in the QGIS authentication database."))
else:
self.storage_note.setText(
tr("The QGIS authentication database is locked or unavailable. "
"The key will only be kept until QGIS is closed – it is never "
"written in plain text."))
self.storage_note.setStyleSheet(theme.warn_style())
def _token_changed(self, text: str) -> None:
text = (text or "").strip()
if not text:
self.note.setText("")
return
if not auth.looks_like_token(text):
self.note.setText(
tr("That does not look like a GeoDonkey key. Keys start with {prefixes}.")
.format(prefixes=" or ".join(config.TOKEN_PREFIXES)))
self.note.setStyleSheet(theme.warn_style())
return
self.note.setText("")
self.note.setStyleSheet(theme.note_style())
def test_token(self) -> None:
token = self.token.text().strip()
if not token:
self.note.setText(tr("Enter a key first."))
self.note.setStyleSheet(theme.warn_style())
return
client = ApiClient(transport=QgisTransport(),
base_url=str(self.api_url.text() or config.DEFAULT_API_URL),
token=token)
QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor))
self.test_button.setEnabled(False)
try:
data = client.account()
except GeoDonkeyError as exc:
self.note.setText(exc.message)
self.note.setStyleSheet(theme.error_style())
return
finally:
self.test_button.setEnabled(True)
QApplication.restoreOverrideCursor()
self.note.setText(tr("Key works · {email} · {tier} · {coins} DC").format(
email=data.get("email") or "–",
tier=data.get("tier") or data.get("plan") or "–",
coins=data.get("coins")))
self.note.setStyleSheet(theme.good_style())
if data.get("pro"):
self.limit.setMaximum(config.OSM_LIMIT_PRO)
def forget_token(self) -> None:
auth.forget()
self.token.clear()
self.note.setText(tr("Key removed."))
self.note.setStyleSheet(theme.note_style())
def accept(self) -> None:
settings.set("api_url", str(self.api_url.text() or config.DEFAULT_API_URL).rstrip("/"))
settings.set("limit", int(self.limit.value()))
token = self.token.text().strip()
if not token:
auth.forget()
super().accept()
return
try:
auth.save_token(token)
except auth.AuthUnavailable:
auth.remember_for_session(token)
QMessageBox.warning(
self, tr("GeoDonkey"),
tr("The key could not be stored in the QGIS authentication database.\n\n"
"It will be used for this session and is forgotten when QGIS closes. "
"GeoDonkey never writes the key to disk in plain text.\n\n"
"To keep it permanently, set a master password under "
"Settings ▸ Options ▸ Authentication."))
super().accept()