forked from trav/lintunes
117 lines
4.7 KiB
Python
117 lines
4.7 KiB
Python
"""Share Library with a Friend… and Add Friend Library… — the one-paste
|
|
connection. Both talk to ``CassetteService``; every failure shows which step
|
|
failed and what Syncthing said."""
|
|
from __future__ import annotations
|
|
|
|
from PyQt6.QtCore import Qt
|
|
from PyQt6.QtGui import QGuiApplication
|
|
from PyQt6.QtWidgets import (
|
|
QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QPlainTextEdit,
|
|
QPushButton, QVBoxLayout,
|
|
)
|
|
|
|
from lintunes.cassette import invite as inv
|
|
from lintunes.cassette import syncthing_api as st
|
|
|
|
|
|
def failure_text(exc: Exception) -> str:
|
|
"""What to show for a failed action: the step and Syncthing's words, plus
|
|
the next step when the cause is Syncthing itself."""
|
|
if isinstance(exc, inv.InviteError):
|
|
return str(exc)
|
|
if isinstance(exc, st.SyncthingError):
|
|
if exc.kind == st.NOT_RUNNING:
|
|
return (f"{exc.step} failed: Syncthing isn't running. Start "
|
|
f"Syncthing and try again.")
|
|
if exc.kind in (st.BAD_KEY, st.NO_KEY):
|
|
return (f"{exc.step} failed: LinTunes can't reach Syncthing's API "
|
|
f"({exc.detail}). Check the API key in Sync Settings ▸ "
|
|
f"General.")
|
|
return f"{exc.step} failed. Syncthing said: {exc.detail}"
|
|
return f"Something went wrong: {exc}"
|
|
|
|
|
|
class ShareInviteDialog(QDialog):
|
|
"""Shows a freshly made invite code with a Copy button."""
|
|
|
|
def __init__(self, code: str, parent=None):
|
|
super().__init__(parent)
|
|
self.setWindowTitle("Share Library with a Friend")
|
|
layout = QVBoxLayout(self)
|
|
intro = QLabel(
|
|
"Send this code to one friend. They paste it into "
|
|
"Connections ▸ Add Friend Library… in their LinTunes.\n\n"
|
|
"It works once, and it doesn't expire. You don't both need to be "
|
|
"online now: you'll be connected the next time both computers are "
|
|
"on. Unused invites are listed in Sync Settings, where you can "
|
|
"cancel them.")
|
|
intro.setWordWrap(True)
|
|
layout.addWidget(intro)
|
|
self.code = QPlainTextEdit(code)
|
|
self.code.setReadOnly(True)
|
|
self.code.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth)
|
|
# Room for the whole code (four lines at this width) without scrolling.
|
|
lines = 5
|
|
self.code.setMinimumHeight(
|
|
self.code.fontMetrics().lineSpacing() * lines
|
|
+ 2 * (self.code.frameWidth() + int(self.code.document().documentMargin())))
|
|
layout.addWidget(self.code, stretch=1)
|
|
row = QHBoxLayout()
|
|
self.copy = QPushButton("Copy Code")
|
|
self.copy.setDefault(True)
|
|
self.copy.clicked.connect(self._copy)
|
|
self.copied = QLabel("")
|
|
row.addWidget(self.copy)
|
|
row.addWidget(self.copied, stretch=1)
|
|
done = QPushButton("Done")
|
|
done.clicked.connect(self.accept)
|
|
row.addWidget(done)
|
|
layout.addLayout(row)
|
|
# Open tall enough for the wrapped intro: a fixed height cut its last
|
|
# line off, since a word-wrapped label only knows its height for a
|
|
# given width.
|
|
width = 560
|
|
self.resize(width, max(layout.totalHeightForWidth(width),
|
|
layout.totalMinimumSize().height()))
|
|
|
|
def _copy(self):
|
|
QGuiApplication.clipboard().setText(self.code.toPlainText())
|
|
self.copied.setText("Copied.")
|
|
|
|
|
|
class AddFriendDialog(QDialog):
|
|
"""Paste a code, press Connect. ``service.accept_code`` does the work."""
|
|
|
|
def __init__(self, service, parent=None):
|
|
super().__init__(parent)
|
|
self._service = service
|
|
self.friend = None
|
|
self.setWindowTitle("Add Friend Library")
|
|
self.resize(520, 240)
|
|
layout = QVBoxLayout(self)
|
|
label = QLabel("Paste the invite code your friend sent you:")
|
|
layout.addWidget(label)
|
|
self.field = QPlainTextEdit()
|
|
self.field.setPlaceholderText("LTC1.…")
|
|
layout.addWidget(self.field, stretch=1)
|
|
self.message = QLabel("")
|
|
self.message.setWordWrap(True)
|
|
self.message.setTextInteractionFlags(
|
|
Qt.TextInteractionFlag.TextSelectableByMouse)
|
|
layout.addWidget(self.message)
|
|
buttons = QDialogButtonBox()
|
|
self.connect_button = buttons.addButton(
|
|
"Connect", QDialogButtonBox.ButtonRole.AcceptRole)
|
|
buttons.addButton(QDialogButtonBox.StandardButton.Cancel)
|
|
buttons.accepted.connect(self._connect)
|
|
buttons.rejected.connect(self.reject)
|
|
layout.addWidget(buttons)
|
|
|
|
def _connect(self):
|
|
try:
|
|
self.friend = self._service.accept_code(self.field.toPlainText())
|
|
except (inv.InviteError, st.SyncthingError) as exc:
|
|
self.message.setText(failure_text(exc))
|
|
return
|
|
self.accept()
|