[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:
RickyRister 2025-11-28 14:41:11 -08:00 committed by GitHub
parent 9ece4bfd9b
commit 858361e6d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 60 additions and 44 deletions

View file

@ -25,13 +25,11 @@ const QStringList DeckLoader::ACCEPTED_FILE_EXTENSIONS = {"*.cod", "*.dec", "*.d
const QStringList DeckLoader::FILE_NAME_FILTERS = {
tr("Common deck formats (%1)").arg(ACCEPTED_FILE_EXTENSIONS.join(" ")), tr("All files (*.*)")};
DeckLoader::DeckLoader(QObject *parent)
: QObject(parent), deckList(new DeckList()), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1)
DeckLoader::DeckLoader(QObject *parent) : QObject(parent), deckList(new DeckList())
{
}
DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList)
: QObject(parent), deckList(_deckList), lastFileFormat(CockatriceFormat), lastRemoteDeckId(-1)
DeckLoader::DeckLoader(QObject *parent, DeckList *_deckList) : QObject(parent), deckList(_deckList)
{
}
@ -64,8 +62,10 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool user
}
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
lastLoadInfo = {
.fileName = fileName,
.fileFormat = fmt,
};
if (userRequest) {
updateLastLoadedTimestamp(fileName, fmt);
}
@ -86,8 +86,10 @@ bool DeckLoader::loadFromFileAsync(const QString &fileName, FileFormat fmt, bool
watcher->deleteLater();
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
lastLoadInfo = {
.fileName = fileName,
.fileFormat = fmt,
};
if (userRequest) {
updateLastLoadedTimestamp(fileName, fmt);
}
@ -129,9 +131,9 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
{
bool result = deckList->loadFromString_Native(nativeString);
if (result) {
lastFileName = QString();
lastFileFormat = CockatriceFormat;
lastRemoteDeckId = remoteDeckId;
lastLoadInfo = {
.remoteDeckId = remoteDeckId,
};
emit deckLoaded();
}
@ -157,8 +159,10 @@ bool DeckLoader::saveToFile(const QString &fileName, FileFormat fmt)
}
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
lastLoadInfo = {
.fileName = fileName,
.fileFormat = fmt,
};
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
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
lastLoadInfo = {
.fileName = fileName,
.fileFormat = fmt,
};
// Re-open the file and set the original timestamp
if (!file.open(QIODevice::ReadWrite)) {
@ -582,8 +588,10 @@ bool DeckLoader::convertToCockatriceFormat(QString fileName)
} else {
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
}
lastFileName = newFileName;
lastFileFormat = CockatriceFormat;
lastLoadInfo = {
.fileName = newFileName,
.fileFormat = CockatriceFormat,
};
}
return result;

View file

@ -28,6 +28,21 @@ public:
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
*/
@ -46,9 +61,7 @@ public:
private:
DeckList *deckList;
QString lastFileName;
FileFormat lastFileFormat;
int lastRemoteDeckId;
LoadInfo lastLoadInfo;
public:
DeckLoader(QObject *parent);
@ -56,26 +69,19 @@ public:
DeckLoader(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;
}
[[nodiscard]] FileFormat getLastFileFormat() const
{
return lastFileFormat;
}
[[nodiscard]] int getLastRemoteDeckId() const
{
return lastRemoteDeckId;
lastLoadInfo = info;
}
[[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);