From 6bf64ea7125de0e6add492f29a5994fac0ae6371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Fri, 18 Sep 2026 23:56:40 +0200 Subject: [PATCH] [DeckShare] Extract the share-creation response handling into DeckShareUtils --- .../widgets/deck_share/deck_share_utils.cpp | 19 +++++++++++++++++++ .../widgets/deck_share/deck_share_utils.h | 18 ++++++++++++++++++ .../widgets/dialogs/dlg_share_deck.cpp | 13 ++----------- .../widgets/tabs/tab_deck_storage.cpp | 15 +++------------ .../tab_deck_storage_visual.cpp | 15 +++------------ 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/cockatrice/src/interface/widgets/deck_share/deck_share_utils.cpp b/cockatrice/src/interface/widgets/deck_share/deck_share_utils.cpp index 14c568303..a07150c08 100644 --- a/cockatrice/src/interface/widgets/deck_share/deck_share_utils.cpp +++ b/cockatrice/src/interface/widgets/deck_share/deck_share_utils.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace DeckShareUtils { @@ -25,4 +27,21 @@ QString formatShareExpiry(const QDateTime &expiry) return expiry.toLocalTime().toString(); } +ShareResponse handleShareResponse(const AbstractClient *client, const Response &response) +{ + const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); + const QString token = QString::fromStdString(resp.token()); + + const QString link = buildShareLink(client, token); + copyShareLinkToClipboard(link); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) + const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC); +#else + const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC); +#endif + + return {link, expiry}; +} + } // namespace DeckShareUtils \ No newline at end of file diff --git a/cockatrice/src/interface/widgets/deck_share/deck_share_utils.h b/cockatrice/src/interface/widgets/deck_share/deck_share_utils.h index 0034233fe..a8950e21a 100644 --- a/cockatrice/src/interface/widgets/deck_share/deck_share_utils.h +++ b/cockatrice/src/interface/widgets/deck_share/deck_share_utils.h @@ -11,6 +11,7 @@ #include class AbstractClient; +class Response; /** * @brief Shared helpers for creating temporary deck shares. @@ -18,6 +19,15 @@ class AbstractClient; namespace DeckShareUtils { +/** + * @brief The outcome of a successful share-create response. + */ +struct ShareResponse +{ + QString link; ///< The share link that was copied to the clipboard. + QDateTime expiry; ///< When the share expires (UTC). +}; + /** * @brief Builds the cockatrice:// link for a freshly created deck share. * @param client Used to embed the target server's hostname and port. @@ -36,6 +46,14 @@ QString copyShareLinkToClipboard(const QString &link); */ QString formatShareExpiry(const QDateTime &expiry); +/** + * @brief Handles a successful Response_DeckShareCreate: builds the share link, + * copies it to the clipboard, and derives the share expiry. + * @param client Used to embed the target server's hostname and port. + * @param response The successful response carrying the share token and expiry. + */ +ShareResponse handleShareResponse(const AbstractClient *client, const Response &response); + } // namespace DeckShareUtils #endif // DECK_SHARE_UTILS_H \ No newline at end of file diff --git a/cockatrice/src/interface/widgets/dialogs/dlg_share_deck.cpp b/cockatrice/src/interface/widgets/dialogs/dlg_share_deck.cpp index 032407e38..f1ba52f82 100644 --- a/cockatrice/src/interface/widgets/dialogs/dlg_share_deck.cpp +++ b/cockatrice/src/interface/widgets/dialogs/dlg_share_deck.cpp @@ -70,20 +70,11 @@ void DlgShareDeck::shareFinished(const Response &response, const CommandContaine return; } - const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); - const QString token = QString::fromStdString(resp.token()); -#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC); -#else - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC); -#endif - - const QString link = DeckShareUtils::buildShareLink(client, token); - DeckShareUtils::copyShareLinkToClipboard(link); + const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response); QMessageBox::information(this, tr("Share deck"), tr("Share link created and copied to the clipboard:\n\n%1\n\n" "The share expires on %2.") - .arg(link, DeckShareUtils::formatShareExpiry(expiry))); + .arg(share.link, DeckShareUtils::formatShareExpiry(share.expiry))); accept(); } \ No newline at end of file diff --git a/cockatrice/src/interface/widgets/tabs/tab_deck_storage.cpp b/cockatrice/src/interface/widgets/tabs/tab_deck_storage.cpp index a22f8e7dc..d9de42031 100644 --- a/cockatrice/src/interface/widgets/tabs/tab_deck_storage.cpp +++ b/cockatrice/src/interface/widgets/tabs/tab_deck_storage.cpp @@ -778,19 +778,10 @@ void TabDeckStorage::shareFromTreeFinished(const Response &response, const Comma true); return; } - const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); - const QString token = QString::fromStdString(resp.token()); -#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC); -#else - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC); -#endif + const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response); - const QString link = DeckShareUtils::buildShareLink(client, token); - DeckShareUtils::copyShareLinkToClipboard(link); - - showShareNotice( - tr("Share link copied to the clipboard.\nExpires on %1.").arg(DeckShareUtils::formatShareExpiry(expiry))); + showShareNotice(tr("Share link copied to the clipboard.\nExpires on %1.") + .arg(DeckShareUtils::formatShareExpiry(share.expiry))); setShareModeEnabled(false); } diff --git a/cockatrice/src/interface/widgets/tabs/visual_deck_storage/tab_deck_storage_visual.cpp b/cockatrice/src/interface/widgets/tabs/visual_deck_storage/tab_deck_storage_visual.cpp index b652b3f24..fdf8fe465 100644 --- a/cockatrice/src/interface/widgets/tabs/visual_deck_storage/tab_deck_storage_visual.cpp +++ b/cockatrice/src/interface/widgets/tabs/visual_deck_storage/tab_deck_storage_visual.cpp @@ -166,19 +166,10 @@ void TabDeckStorageVisual::shareFinished(const Response &response, const Command return; } - const Response_DeckShareCreate &resp = response.GetExtension(Response_DeckShareCreate::ext); - const QString token = QString::fromStdString(resp.token()); -#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), QTimeZone::UTC); -#else - const QDateTime expiry = QDateTime::fromSecsSinceEpoch(resp.expires_at(), Qt::UTC); -#endif + const DeckShareUtils::ShareResponse share = DeckShareUtils::handleShareResponse(client, response); - const QString link = DeckShareUtils::buildShareLink(client, token); - DeckShareUtils::copyShareLinkToClipboard(link); - - showShareNotice( - tr("Share link copied to the clipboard.\nExpires on %1.").arg(DeckShareUtils::formatShareExpiry(expiry))); + showShareNotice(tr("Share link copied to the clipboard.\nExpires on %1.") + .arg(DeckShareUtils::formatShareExpiry(share.expiry))); exitShareMode(); }