Cockatrice/libcockatrice_settings/libcockatrice/settings/servers_settings.h
seavor 371b74732e feat: register cockatrice:// and cockatrice-oracle:// protocol handlers
Adds OS-level URL-scheme handlers so users can click a link in a browser,
chat client, or third-party tool to launch Cockatrice straight into a
server / game / Oracle update.

Supported URL forms:
  cockatrice://joingame?hostname=H&port=P&roomid=R&gameid=G[&spectate=1]
  cockatrice-oracle://update[?spoilers=1]

Credentials passed via URL (username/password query params) are deliberately
ignored — URLs leak through shell history, browser history, EDR capture, etc.
If the target server requires auth and no saved credentials match, the Connect
dialog opens pre-filled with the URL's host/port so the user types their
password locally.

OS integration
- Linux: MimeType=x-scheme-handler/cockatrice (and -oracle) added to the
  .desktop files; Exec=cockatrice %u passes the URL through.
- Windows: NSIS installer writes HKCR\cockatrice and HKCR\cockatrice-oracle
  registry entries; uninstaller removes them.
- macOS: per-app Info.cockatrice.plist / Info.oracle.plist declare
  CFBundleURLTypes; a QFileOpenEvent filter is installed on QApplication
  before any nested event loop so cold-start URLs aren't lost.

New abstractions
- Intent (libcockatrice_utility/libcockatrice/utility/intent.h): abstract base
  for chained async actions.  Guarantees finished() fires at most once,
  execute() is idempotent, self-deletes via deleteLater, and
  startTimeoutSafetyNet() arms a configurable per-stage deadline.  Concrete
  intents (IntentConnectToServer, IntentLogin, IntentJoinServerRoom,
  IntentJoinServerGame) compose the joingame flow via UrlParser.
- SingleInstanceManager: async per-user local-socket primary/secondary
  handshake; URL forwarded from secondary to primary with QDataStream framing
  both ways.  shared_ptr-backed resolved flag survives every lambda capture.
- UrlSchemeEventFilter (new libcockatrice_utility_gui sibling library): QObject
  event filter that translates macOS QFileOpenEvent into a urlReceived(QString)
  signal.  Lives in its own Gui-bearing lib so libcockatrice_utility stays
  Core+Network only and doesn't drag Qt::Gui into servatrice.
- UrlUtils (header-only): pure URL parsing, fully unit-tested.

Wiring
- MainWindow::handleUrl(QString) — single entry point for any URL source.
- DlgConnect::prefillNewHost(host, port) — pre-fills new-host inputs.
- ServersSettings::findSavedCredsByHostPort — case-insensitive saved-creds
  lookup.
- TabSupervisor::requestJoinRoom + roomJoinedById / roomJoinFailedById signals,
  TabServer::roomAlreadyJoined for the short-circuit "already in this room"
  path — single source of truth for duplicate-join handling.

Tests
- 36 new unit tests across four single-purpose targets in tests/:
  - url_utils_test (22 tests) — scheme matching, port/room/game validation,
    spectator flag, credentials ignored, case-insensitivity.
  - url_scheme_event_filter_test (3 tests) — QFileOpenEvent capture.
  - intent_test (7 tests) — self-delete, abort propagation, parent-destruction-
    mid-flight, finish-once gate, execute() idempotence.
  - single_instance_manager_test (4 tests) — per-user socket naming, becoming-
    primary alone, forwarding to an existing primary, single-emission of
    roleResolved.

Build tooling (incidental)
- Dockerfile.format, docker-compose.format.yml, Makefile — a docker-based
  runner for format.sh that mirrors CI's desktop-lint step.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 19:40:05 -05:00

104 lines
3.7 KiB
C++

/**
* @file servers_settings.h
* @ingroup NetworkSettings
* @brief TODO: Document this.
*/
#ifndef SERVERSSETTINGS_H
#define SERVERSSETTINGS_H
#include "settings_manager.h"
#include <QLoggingCategory>
#include <QObject>
#include <optional>
#define SERVERSETTINGS_DEFAULT_HOST "server.cockatrice.us"
#define SERVERSETTINGS_DEFAULT_PORT "4748"
inline Q_LOGGING_CATEGORY(ServersSettingsLog, "servers_settings");
/**
* @brief Saved credentials for a server identified by hostname+port.
*
* @c password is empty when the entry's @c savePassword flag is false, in
* which case the caller should prompt the user for the password.
*/
struct SavedServerCreds
{
QString playerName;
QString password;
};
class ServersSettings : public SettingsManager
{
Q_OBJECT
friend class SettingsCache;
public:
int getPreviousHostLogin() const;
int getPrevioushostindex(const QString &) const;
QStringList getPreviousHostList() const;
QString getPrevioushostName() const;
QString getHostname(QString defaultHost = SERVERSETTINGS_DEFAULT_HOST) const;
QString getPort(QString defaultPort = SERVERSETTINGS_DEFAULT_PORT) const;
QString getPlayerName(QString defaultName = "") const;
QString getFPHostname(QString defaultHost = SERVERSETTINGS_DEFAULT_HOST) const;
QString getFPPort(QString defaultPort = SERVERSETTINGS_DEFAULT_PORT) const;
QString getFPPlayerName(QString defaultName = "") const;
QString getPassword();
/**
* @brief Look up saved credentials by hostname+port.
*
* Used by the URL-driven join flow to authenticate against a server
* without requiring credentials in the URL itself. Returns @c nullopt
* when no saved server matches. When the matched entry has
* @c savePassword == false, the returned creds have an empty @c password
* — the caller is expected to prompt the user.
*
* Hostname matching is case-insensitive.
*/
[[nodiscard]] std::optional<SavedServerCreds> findSavedCredsByHostPort(const QString &host, quint16 port) const;
QString getSaveName(QString defaultname = "");
QString getSite(QString defaultName = "");
bool getSavePassword() const;
int getAutoConnect() const;
void setPreviousHostLogin(int previous);
void setPrevioushostName(const QString &);
void setPreviousHostList(QStringList list);
void setAutoConnect(int autoconnect);
void setSite(QString site);
void setFPHostName(QString hostname);
void setFPPort(QString port);
void setFPPlayerName(QString playerName);
void addNewServer(const QString &saveName,
const QString &serv,
const QString &port,
const QString &username,
const QString &password,
bool savePassword,
const QString &site = QString());
void removeServer(QString servAddr);
bool updateExistingServer(QString saveName,
QString serv,
QString port,
QString username,
QString password,
bool savePassword,
QString site = QString());
bool updateExistingServerWithoutLoss(QString saveName,
QString serv = QString(),
QString port = QString(),
QString site = QString());
void setClearDebugLogStatus(bool abIsChecked);
bool getClearDebugLogStatus(bool abDefaultValue) const;
private:
explicit ServersSettings(const QString &settingPath, QObject *parent = nullptr);
ServersSettings(const ServersSettings & /*other*/);
};
#endif // SERVERSSETTINGS_H