Update last loaded timestamp in decklist file and then restore original last modified timestamp if a user requests a deck load.

This commit is contained in:
Lukas Brübach 2025-01-06 11:08:15 +01:00
parent 132159c7f6
commit f5dcf522f1
8 changed files with 82 additions and 12 deletions

View file

@ -1045,7 +1045,7 @@ void TabDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLocation d
DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName); DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName);
auto *l = new DeckLoader; auto *l = new DeckLoader;
if (l->loadFromFile(fileName, fmt)) { if (l->loadFromFile(fileName, fmt, true)) {
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(fileName); SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(fileName);
updateBannerCardComboBox(); updateBannerCardComboBox();
if (!l->getBannerCard().isEmpty()) { if (!l->getBannerCard().isEmpty()) {

View file

@ -203,7 +203,7 @@ void TabDeckStorage::actOpenLocalDeck()
QString filePath = localDirModel->filePath(curLeft); QString filePath = localDirModel->filePath(curLeft);
DeckLoader deckLoader; DeckLoader deckLoader;
if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat)) if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat, true))
continue; continue;
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(filePath); SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(filePath);

View file

@ -307,7 +307,7 @@ void DeckViewContainer::replaceDeckStorageWithDeckView(QMouseEvent *event, DeckP
QString deckString; QString deckString;
DeckLoader deck; DeckLoader deck;
bool error = !deck.loadFromFile(fileName, fmt); bool error = !deck.loadFromFile(fileName, fmt, true);
if (!error) { if (!error) {
deckString = deck.writeToString_Native(); deckString = deck.writeToString_Native();
error = deckString.length() > MAX_FILE_LENGTH; error = deckString.length() > MAX_FILE_LENGTH;
@ -357,7 +357,7 @@ void DeckViewContainer::loadDeckFromFile(const QString &filePath)
QString deckString; QString deckString;
DeckLoader deck; DeckLoader deck;
bool error = !deck.loadFromFile(filePath, fmt); bool error = !deck.loadFromFile(filePath, fmt, true);
if (!error) { if (!error) {
deckString = deck.writeToString_Native(); deckString = deck.writeToString_Native();
error = deckString.length() > MAX_FILE_LENGTH; error = deckString.length() > MAX_FILE_LENGTH;

View file

@ -78,7 +78,7 @@ void TabDeckStorageVisual::actOpenLocalDeck(QMouseEvent *event, DeckPreviewCardP
{ {
(void)event; (void)event;
DeckLoader deckLoader; DeckLoader deckLoader;
if (!deckLoader.loadFromFile(instance->filePath, DeckLoader::CockatriceFormat)) if (!deckLoader.loadFromFile(instance->filePath, DeckLoader::CockatriceFormat, true))
return; return;
emit openDeckEditor(&deckLoader); emit openDeckEditor(&deckLoader);

View file

@ -7,6 +7,7 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringList> #include <QStringList>
@ -34,7 +35,7 @@ DeckLoader::DeckLoader(const DeckLoader &other)
{ {
} }
bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt) bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest)
{ {
QFile file(fileName); QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
@ -50,7 +51,7 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt)
result = loadFromFile_Native(&file); result = loadFromFile_Native(&file);
qDebug() << "Loaded from" << fileName << "-" << result; qDebug() << "Loaded from" << fileName << "-" << result;
if (!result) { if (!result) {
qDebug() << "Retying as plain format"; qDebug() << "Retrying as plain format";
file.seek(0); file.seek(0);
result = loadFromFile_Plain(&file); result = loadFromFile_Plain(&file);
fmt = PlainTextFormat; fmt = PlainTextFormat;
@ -65,6 +66,9 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt)
if (result) { if (result) {
lastFileName = fileName; lastFileName = fileName;
lastFileFormat = fmt; lastFileFormat = fmt;
if (userRequest) {
updateLastLoadedTimestamp(fileName, fmt);
}
emit deckLoaded(); emit deckLoaded();
} }
@ -110,6 +114,59 @@ bool DeckLoader::saveToFile(const QString &fileName, FileFormat fmt)
return result; return result;
} }
bool DeckLoader::updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt)
{
QFileInfo fileInfo(fileName);
if (!fileInfo.exists()) {
qWarning() << "File does not exist:" << fileName;
return false;
}
QDateTime originalTimestamp = fileInfo.lastModified();
// Open the file for writing
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Failed to open file for writing:" << fileName;
return false;
}
bool result = false;
// Perform file modifications
switch (fmt) {
case PlainTextFormat:
break;
case CockatriceFormat:
setLastLoadedTimestamp(QDateTime::currentDateTime().toString());
result = saveToFile_Native(&file);
break;
}
file.close(); // Close the file to ensure changes are flushed
if (result) {
lastFileName = fileName;
lastFileFormat = fmt;
// Re-open the file and set the original timestamp
if (!file.open(QIODevice::ReadWrite)) {
qWarning() << "Failed to re-open file to set timestamp:" << fileName;
return false;
}
if (!file.setFileTime(originalTimestamp, QFileDevice::FileModificationTime)) {
qWarning() << "Failed to set modification time for file:" << fileName;
file.close();
return false;
}
file.close();
}
return result;
}
// This struct is here to support the forEachCard function call, defined in decklist. It // This struct is here to support the forEachCard function call, defined in decklist. It
// requires a function to be called for each card, and passes an inner node and a card for // requires a function to be called for each card, and passes an inner node and a card for
// each card in the decklist. // each card in the decklist.
@ -120,7 +177,7 @@ struct FormatDeckListForExport
QString &sideBoardCards; QString &sideBoardCards;
// create main operator for struct, allowing the foreachcard to work. // create main operator for struct, allowing the foreachcard to work.
FormatDeckListForExport(QString &_mainBoardCards, QString &_sideBoardCards) FormatDeckListForExport(QString &_mainBoardCards, QString &_sideBoardCards)
: mainBoardCards(_mainBoardCards), sideBoardCards(_sideBoardCards){}; : mainBoardCards(_mainBoardCards), sideBoardCards(_sideBoardCards) {};
void operator()(const InnerDecklistNode *node, const DecklistCardNode *card) const void operator()(const InnerDecklistNode *node, const DecklistCardNode *card) const
{ {

View file

@ -42,9 +42,10 @@ public:
static FileFormat getFormatFromName(const QString &fileName); static FileFormat getFormatFromName(const QString &fileName);
bool loadFromFile(const QString &fileName, FileFormat fmt); bool loadFromFile(const QString &fileName, FileFormat fmt, bool userRequest = false);
bool loadFromRemote(const QString &nativeString, int remoteDeckId); bool loadFromRemote(const QString &nativeString, int remoteDeckId);
bool saveToFile(const QString &fileName, FileFormat fmt); bool saveToFile(const QString &fileName, FileFormat fmt);
bool updateLastLoadedTimestamp(const QString &fileName, FileFormat fmt);
QString exportDeckToDecklist(); QString exportDeckToDecklist();
void resolveSetNameAndNumberToProviderID(); void resolveSetNameAndNumberToProviderID();

View file

@ -368,7 +368,8 @@ DeckList::DeckList()
// TODO: https://qt-project.org/doc/qt-4.8/qobject.html#no-copy-constructor-or-assignment-operator // TODO: https://qt-project.org/doc/qt-4.8/qobject.html#no-copy-constructor-or-assignment-operator
DeckList::DeckList(const DeckList &other) DeckList::DeckList(const DeckList &other)
: QObject(), name(other.name), comments(other.comments), bannerCard(other.bannerCard), deckHash(other.deckHash) : QObject(), name(other.name), comments(other.comments), bannerCard(other.bannerCard), deckHash(other.deckHash),
lastLoadedTimestamp(other.lastLoadedTimestamp)
{ {
root = new InnerDecklistNode(other.getRoot()); root = new InnerDecklistNode(other.getRoot());
@ -419,13 +420,14 @@ bool DeckList::readElement(QXmlStreamReader *xml)
{ {
const QString childName = xml->name().toString(); const QString childName = xml->name().toString();
if (xml->isStartElement()) { if (xml->isStartElement()) {
if (childName == "deckname") if (childName == "lastLoadedTimestamp") {
lastLoadedTimestamp = xml->readElementText();
} else if (childName == "deckname")
name = xml->readElementText(); name = xml->readElementText();
else if (childName == "comments") else if (childName == "comments")
comments = xml->readElementText(); comments = xml->readElementText();
else if (childName == "bannerCard") { else if (childName == "bannerCard") {
bannerCard = xml->readElementText(); bannerCard = xml->readElementText();
qDebug() << "Deckloader found the banner card " << bannerCard;
} else if (childName == "zone") { } else if (childName == "zone") {
InnerDecklistNode *newZone = getZoneObjFromName(xml->attributes().value("name").toString()); InnerDecklistNode *newZone = getZoneObjFromName(xml->attributes().value("name").toString());
newZone->readElement(xml); newZone->readElement(xml);
@ -445,6 +447,7 @@ void DeckList::write(QXmlStreamWriter *xml)
{ {
xml->writeStartElement("cockatrice_deck"); xml->writeStartElement("cockatrice_deck");
xml->writeAttribute("version", "1"); xml->writeAttribute("version", "1");
xml->writeTextElement("lastLoadedTimestamp", lastLoadedTimestamp);
xml->writeTextElement("deckname", name); xml->writeTextElement("deckname", name);
xml->writeTextElement("comments", comments); xml->writeTextElement("comments", comments);
xml->writeTextElement("bannerCard", bannerCard); xml->writeTextElement("bannerCard", bannerCard);

View file

@ -252,6 +252,7 @@ class DeckList : public QObject
private: private:
QString name, comments, bannerCard; QString name, comments, bannerCard;
QString deckHash; QString deckHash;
QString lastLoadedTimestamp;
QMap<QString, SideboardPlan *> sideboardPlans; QMap<QString, SideboardPlan *> sideboardPlans;
InnerDecklistNode *root; InnerDecklistNode *root;
void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result) const; void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result) const;
@ -283,6 +284,10 @@ public slots:
{ {
bannerCard = _bannerCard; bannerCard = _bannerCard;
} }
void setLastLoadedTimestamp(const QString &_lastLoadedTimestamp = QString())
{
lastLoadedTimestamp = _lastLoadedTimestamp;
}
public: public:
explicit DeckList(); explicit DeckList();
@ -301,6 +306,10 @@ public:
{ {
return bannerCard; return bannerCard;
} }
QString getLastLoadedTimestamp() const
{
return lastLoadedTimestamp;
}
QList<MoveCard_ToZone> getCurrentSideboardPlan(); QList<MoveCard_ToZone> getCurrentSideboardPlan();
void setCurrentSideboardPlan(const QList<MoveCard_ToZone> &plan); void setCurrentSideboardPlan(const QList<MoveCard_ToZone> &plan);
const QMap<QString, SideboardPlan *> &getSideboardPlans() const const QMap<QString, SideboardPlan *> &getSideboardPlans() const