[Client] Confirm before joining a game opened from a game link

Joining a game from a cockatrice://joingame link is a navigation
decision, so restate what will be joined and ask before acting:
the confirm names the game description when the link carries one
(falling back to the room name and numeric id for older links), and
reports the host:port so links that point at a different server are
obvious. The intent chain is only started after confirmation.


Took 3 minutes
This commit is contained in:
Lukas Brübach 2026-08-16 21:33:51 +02:00
parent 3de7882f0c
commit 8197d37fcb

View file

@ -1,5 +1,7 @@
#include "url_parser.h"
#include "../widgets/tabs/tab_room.h"
#include "../widgets/tabs/tab_supervisor.h"
#include "../window_main.h"
#include "contexts/context_join_game.h"
#include "intent_join_server_game.h"
@ -9,6 +11,7 @@
#include <QMessageBox>
#include <QUrl>
#include <QUrlQuery>
#include <libcockatrice/network/client/abstract/abstract_client.h>
#include <memory>
IntentUrlParser::IntentUrlParser(QObject *parent, MainWindow *_mainWindow) : QObject(parent), mainWindow(_mainWindow)
@ -71,6 +74,44 @@ void IntentUrlParser::handleJoinGame(const QUrlQuery &query)
return;
}
const QString hostname = ctx->roomContext.serverContext.hostname;
const QString port = ctx->roomContext.serverContext.port;
const int roomId = ctx->roomContext.roomId;
const int gameId = ctx->gameId;
const QString server = QStringLiteral("%1:%2").arg(hostname, port);
// Prefer the room name over the raw numeric id: it means something to the
// user. The name is only known when we are already connected to the same
// server and sitting in that room — otherwise fall back to a plain prompt.
AbstractClient *client = mainWindow->getTabSupervisor()->getClient();
const bool sameServer = client != nullptr && client->getStatus() == StatusLoggedIn &&
hostname.compare(client->serverName(), Qt::CaseInsensitive) == 0 &&
QString::number(client->serverPort()) == port;
TabRoom *roomTab = sameServer ? mainWindow->getTabSupervisor()->getRoomTabs().value(roomId) : nullptr;
// Links built by newer clients embed the game description ("game" item);
// restate it in the confirm so it matches what the chat anchor showed.
// Unknown query items are ignored, so old links without it keep working.
// The multi-arg .arg() overloads replace in a single pass, so a description
// containing "%…" cannot corrupt later placeholders.
// FullyDecoded undoes every %XX escape and must match the chat anchor's
// decode mode, so a description containing "%" reads identically in both.
const QString gameDescription = query.queryItemValue("game", QUrl::FullyDecoded);
const QString gameIdStr = QString::number(gameId);
const QString message =
gameDescription.isEmpty()
? (roomTab ? tr("Join game #%1 in \"%2\" on %3?").arg(gameIdStr, roomTab->getRoomName(), server)
: tr("Join game #%1 on %2?").arg(gameIdStr, server))
: (roomTab ? tr("Join game \"%1\" (#%2) in \"%3\" on %4?")
.arg(gameDescription, gameIdStr, roomTab->getRoomName(), server)
: tr("Join game \"%1\" (#%2) on %3?").arg(gameDescription, gameIdStr, server));
const QMessageBox::StandardButton answer = QMessageBox::question(
mainWindow, tr("Join game"), message, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (answer != QMessageBox::Yes) {
return;
}
// The join game intent owns the context and the credential lookup; once the
// chain finishes (or fails) it deletes the whole tree.
ContextConnectToServer *serverContext = &ctx->roomContext.serverContext;