[DeckShare] Extract the share-creation response handling into DeckShareUtils

This commit is contained in:
Lukas Brübach 2026-09-18 23:56:40 +02:00
parent 389dc8ac17
commit 6bf64ea712
5 changed files with 45 additions and 35 deletions

View file

@ -4,6 +4,8 @@
#include <QGuiApplication>
#include <QTimeZone>
#include <libcockatrice/network/client/abstract/abstract_client.h>
#include <libcockatrice/protocol/pb/response.pb.h>
#include <libcockatrice/protocol/pb/response_deck_share_create.pb.h>
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

View file

@ -11,6 +11,7 @@
#include <QString>
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

View file

@ -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();
}

View file

@ -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);
}

View file

@ -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();
}