mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-12 15:49:28 -08:00
[DeckLoader] Refactor last load info into struct (#6366)
* [DeckLoader] Refactor last load info into struct * Use constant * [[nodiscard]] * do discard, I guess. --------- Co-authored-by: Brübach, Lukas <lukas.bruebach@student.fhws.de>
This commit is contained in:
@@ -25,13 +25,11 @@ const QStringList DeckLoader::ACCEPTED_FILE_EXTENSIONS = {"*.cod", "*.dec", "*.d
|
|||||||
const QStringList DeckLoader::FILE_NAME_FILTERS = {
|
const QStringList DeckLoader::FILE_NAME_FILTERS = {
|
||||||
tr("Common deck formats (%1)").arg(ACCEPTED_FILE_EXTENSIONS.join(" ")), tr("All files (*.*)")};
|
tr("Common deck formats (%1)").arg(ACCEPTED_FILE_EXTENSIONS.join(" ")), tr("All files (*.*)")};
|
||||||
|
|
||||||
DeckLoader::DeckLoader(QObject *parent)
|
DeckLoader::DeckLoader(QObject *parent) : QObject(parent), deckList(new DeckList())
|
||||||
: QObject(parent), deckList(new DeckList()), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList)
|
DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList) : QObject(parent), deckList(_deckList)
|
||||||
: QObject(parent), deckList(_deckList), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,8 +62,10 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool user
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
lastFileName = fileName;
|
lastLoadInfo = {
|
||||||
lastFileFormat = fmt;
|
.fileName = fileName,
|
||||||
|
.fileFormat = fmt,
|
||||||
|
};
|
||||||
if (userRequest) {
|
if (userRequest) {
|
||||||
updateLastLoadedTimestamp(fileName, fmt);
|
updateLastLoadedTimestamp(fileName, fmt);
|
||||||
}
|
}
|
||||||
@@ -86,8 +86,10 @@ bool DeckLoader::loadFromFileAsync(const QString &fileName, FileFormat fmt, bool
|
|||||||
watcher->deleteLater();
|
watcher->deleteLater();
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
lastFileName = fileName;
|
lastLoadInfo = {
|
||||||
lastFileFormat = fmt;
|
.fileName = fileName,
|
||||||
|
.fileFormat = fmt,
|
||||||
|
};
|
||||||
if (userRequest) {
|
if (userRequest) {
|
||||||
updateLastLoadedTimestamp(fileName, fmt);
|
updateLastLoadedTimestamp(fileName, fmt);
|
||||||
}
|
}
|
||||||
@@ -129,9 +131,9 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
|
|||||||
{
|
{
|
||||||
bool result = deckList->loadFromString_Native(nativeString);
|
bool result = deckList->loadFromString_Native(nativeString);
|
||||||
if (result) {
|
if (result) {
|
||||||
lastFileName = QString();
|
lastLoadInfo = {
|
||||||
lastFileFormat = CockatriceFormat;
|
.remoteDeckId = remoteDeckId,
|
||||||
lastRemoteDeckId = remoteDeckId;
|
};
|
||||||
|
|
||||||
emit deckLoaded();
|
emit deckLoaded();
|
||||||
}
|
}
|
||||||
@@ -157,8 +159,10 @@ bool DeckLoader::saveToFile(const QString &fileName, FileFormat fmt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
lastFileName = fileName;
|
lastLoadInfo = {
|
||||||
lastFileFormat = fmt;
|
.fileName = fileName,
|
||||||
|
.fileFormat = fmt,
|
||||||
|
};
|
||||||
qCInfo(DeckLoaderLog) << "Deck was saved -" << result;
|
qCInfo(DeckLoaderLog) << "Deck was saved -" << result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +205,10 @@ bool DeckLoader::updateLastLoadedTimestamp(const QString &fileName, FileFormat f
|
|||||||
file.close(); // Close the file to ensure changes are flushed
|
file.close(); // Close the file to ensure changes are flushed
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
lastFileName = fileName;
|
lastLoadInfo = {
|
||||||
lastFileFormat = fmt;
|
.fileName = fileName,
|
||||||
|
.fileFormat = fmt,
|
||||||
|
};
|
||||||
|
|
||||||
// Re-open the file and set the original timestamp
|
// Re-open the file and set the original timestamp
|
||||||
if (!file.open(QIODevice::ReadWrite)) {
|
if (!file.open(QIODevice::ReadWrite)) {
|
||||||
@@ -582,8 +588,10 @@ bool DeckLoader::convertToCockatriceFormat(QString fileName)
|
|||||||
} else {
|
} else {
|
||||||
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
|
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
|
||||||
}
|
}
|
||||||
lastFileName = newFileName;
|
lastLoadInfo = {
|
||||||
lastFileFormat = CockatriceFormat;
|
.fileName = newFileName,
|
||||||
|
.fileFormat = CockatriceFormat,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -28,6 +28,21 @@ public:
|
|||||||
CockatriceFormat
|
CockatriceFormat
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Information about where the deck was loaded from.
|
||||||
|
*
|
||||||
|
* For local decks, the remoteDeckId field will always be -1.
|
||||||
|
* For remote decks, fileName will be empty and fileFormat will always be CockatriceFormat
|
||||||
|
*/
|
||||||
|
struct LoadInfo
|
||||||
|
{
|
||||||
|
static constexpr int NON_REMOTE_ID = -1;
|
||||||
|
|
||||||
|
QString fileName = "";
|
||||||
|
FileFormat fileFormat = CockatriceFormat;
|
||||||
|
int remoteDeckId = NON_REMOTE_ID;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supported file extensions for decklist files
|
* Supported file extensions for decklist files
|
||||||
*/
|
*/
|
||||||
@@ -46,9 +61,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
DeckList *deckList;
|
DeckList *deckList;
|
||||||
QString lastFileName;
|
LoadInfo lastLoadInfo;
|
||||||
FileFormat lastFileFormat;
|
|
||||||
int lastRemoteDeckId;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DeckLoader(QObject *parent);
|
DeckLoader(QObject *parent);
|
||||||
@@ -56,26 +69,19 @@ public:
|
|||||||
DeckLoader(const DeckLoader &) = delete;
|
DeckLoader(const DeckLoader &) = delete;
|
||||||
DeckLoader &operator=(const DeckLoader &) = delete;
|
DeckLoader &operator=(const DeckLoader &) = delete;
|
||||||
|
|
||||||
[[nodiscard]] const QString &getLastFileName() const
|
const LoadInfo &getLastLoadInfo() const
|
||||||
{
|
{
|
||||||
return lastFileName;
|
return lastLoadInfo;
|
||||||
}
|
}
|
||||||
void setLastFileName(const QString &_lastFileName)
|
|
||||||
|
void setLastLoadInfo(const LoadInfo &info)
|
||||||
{
|
{
|
||||||
lastFileName = _lastFileName;
|
lastLoadInfo = info;
|
||||||
}
|
|
||||||
[[nodiscard]] FileFormat getLastFileFormat() const
|
|
||||||
{
|
|
||||||
return lastFileFormat;
|
|
||||||
}
|
|
||||||
[[nodiscard]] int getLastRemoteDeckId() const
|
|
||||||
{
|
|
||||||
return lastRemoteDeckId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool hasNotBeenLoaded() const
|
[[nodiscard]] bool hasNotBeenLoaded() const
|
||||||
{
|
{
|
||||||
return getLastFileName().isEmpty() && getLastRemoteDeckId() == -1;
|
return lastLoadInfo.fileName.isEmpty() && lastLoadInfo.remoteDeckId == LoadInfo::NON_REMOTE_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clearSetNamesAndNumbers(const DeckList *deckList);
|
static void clearSetNamesAndNumbers(const DeckList *deckList);
|
||||||
|
|||||||
@@ -224,8 +224,8 @@ void AbstractTabDeckEditor::openDeck(DeckLoader *deck)
|
|||||||
{
|
{
|
||||||
setDeck(deck);
|
setDeck(deck);
|
||||||
|
|
||||||
if (!deck->getLastFileName().isEmpty()) {
|
if (!deck->getLastLoadInfo().fileName.isEmpty()) {
|
||||||
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(deck->getLastFileName());
|
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(deck->getLastLoadInfo().fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -411,7 +411,7 @@ void AbstractTabDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLo
|
|||||||
bool AbstractTabDeckEditor::actSaveDeck()
|
bool AbstractTabDeckEditor::actSaveDeck()
|
||||||
{
|
{
|
||||||
DeckLoader *const deck = getDeckLoader();
|
DeckLoader *const deck = getDeckLoader();
|
||||||
if (deck->getLastRemoteDeckId() != -1) {
|
if (deck->getLastLoadInfo().remoteDeckId != DeckLoader::LoadInfo::NON_REMOTE_ID) {
|
||||||
QString deckString = deck->getDeckList()->writeToString_Native();
|
QString deckString = deck->getDeckList()->writeToString_Native();
|
||||||
if (deckString.length() > MAX_FILE_LENGTH) {
|
if (deckString.length() > MAX_FILE_LENGTH) {
|
||||||
QMessageBox::critical(this, tr("Error"), tr("Could not save remote deck"));
|
QMessageBox::critical(this, tr("Error"), tr("Could not save remote deck"));
|
||||||
@@ -419,7 +419,7 @@ bool AbstractTabDeckEditor::actSaveDeck()
|
|||||||
}
|
}
|
||||||
|
|
||||||
Command_DeckUpload cmd;
|
Command_DeckUpload cmd;
|
||||||
cmd.set_deck_id(static_cast<google::protobuf::uint32>(deck->getLastRemoteDeckId()));
|
cmd.set_deck_id(static_cast<google::protobuf::uint32>(deck->getLastLoadInfo().remoteDeckId));
|
||||||
cmd.set_deck_list(deckString.toStdString());
|
cmd.set_deck_list(deckString.toStdString());
|
||||||
|
|
||||||
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
|
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
|
||||||
@@ -427,9 +427,9 @@ bool AbstractTabDeckEditor::actSaveDeck()
|
|||||||
tabSupervisor->getClient()->sendCommand(pend);
|
tabSupervisor->getClient()->sendCommand(pend);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else if (deck->getLastFileName().isEmpty())
|
} else if (deck->getLastLoadInfo().fileName.isEmpty())
|
||||||
return actSaveDeckAs();
|
return actSaveDeckAs();
|
||||||
else if (deck->saveToFile(deck->getLastFileName(), deck->getLastFileFormat())) {
|
else if (deck->saveToFile(deck->getLastLoadInfo().fileName, deck->getLastLoadInfo().fileFormat)) {
|
||||||
setModified(false);
|
setModified(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ void DeckPreviewDeckTagsDisplayWidget::openTagEditDlg()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath);
|
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath);
|
||||||
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getLastFileName();
|
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getLastLoadInfo().fileName;
|
||||||
deckPreviewWidget->refreshBannerCardText();
|
deckPreviewWidget->refreshBannerCardText();
|
||||||
canAddTags = true;
|
canAddTags = true;
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ void DeckPreviewDeckTagsDisplayWidget::openTagEditDlg()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath);
|
deckPreviewWidget->deckLoader->convertToCockatriceFormat(deckPreviewWidget->filePath);
|
||||||
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getLastFileName();
|
deckPreviewWidget->filePath = deckPreviewWidget->deckLoader->getLastLoadInfo().fileName;
|
||||||
deckPreviewWidget->refreshBannerCardText();
|
deckPreviewWidget->refreshBannerCardText();
|
||||||
canAddTags = true;
|
canAddTags = true;
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ void DeckPreviewWidget::initializeUi(const bool deckLoadSuccess)
|
|||||||
|
|
||||||
bannerCardDisplayWidget->setCard(bannerCard);
|
bannerCardDisplayWidget->setCard(bannerCard);
|
||||||
bannerCardDisplayWidget->setFontSize(24);
|
bannerCardDisplayWidget->setFontSize(24);
|
||||||
setFilePath(deckLoader->getLastFileName());
|
setFilePath(deckLoader->getLastLoadInfo().fileName);
|
||||||
|
|
||||||
colorIdentityWidget = new ColorIdentityWidget(this, getColorIdentity());
|
colorIdentityWidget = new ColorIdentityWidget(this, getColorIdentity());
|
||||||
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader->getDeckList());
|
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, deckLoader->getDeckList());
|
||||||
@@ -185,7 +185,7 @@ QString DeckPreviewWidget::getColorIdentity()
|
|||||||
*/
|
*/
|
||||||
QString DeckPreviewWidget::getDisplayName() const
|
QString DeckPreviewWidget::getDisplayName() const
|
||||||
{
|
{
|
||||||
return deckLoader->getDeckList()->getName().isEmpty() ? QFileInfo(deckLoader->getLastFileName()).fileName()
|
return deckLoader->getDeckList()->getName().isEmpty() ? QFileInfo(deckLoader->getLastLoadInfo().fileName).fileName()
|
||||||
: deckLoader->getDeckList()->getName();
|
: deckLoader->getDeckList()->getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,7 +408,9 @@ void DeckPreviewWidget::actRenameFile()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
deckLoader->setLastFileName(newFilePath);
|
DeckLoader::LoadInfo lastLoadInfo = deckLoader->getLastLoadInfo();
|
||||||
|
lastLoadInfo.fileName = newFilePath;
|
||||||
|
deckLoader->setLastLoadInfo(lastLoadInfo);
|
||||||
|
|
||||||
// update VDS
|
// update VDS
|
||||||
setFilePath(newFilePath);
|
setFilePath(newFilePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user