Skip to content

Commit

Permalink
Save/load relative board path (needed e.g. for AppImage)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElTh0r0 committed Oct 12, 2019
1 parent 7ba0ad2 commit d7298ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
7 changes: 7 additions & 0 deletions board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QMessageBox>

Expand Down Expand Up @@ -497,8 +498,14 @@ void Board::saveGame(const QString &sSaveFile, const QString &sTime,
QByteArray ba;
QString sDebug;

// Save relative board folder, needed e.g. for AppImage
QDir dir(qApp->applicationDirPath());
QString sRelativeDir;
sRelativeDir = dir.relativeFilePath(m_sBoardFile);

saveConf.clear();
saveConf.setValue(QStringLiteral("BoardFile"), m_sBoardFile);
saveConf.setValue(QStringLiteral("BoardFileRelative"), sRelativeDir);
ba.append(sTime);
saveConf.setValue(QStringLiteral("ElapsedTime"), ba.toBase64());
ba.clear();
Expand Down
33 changes: 22 additions & 11 deletions iqpuzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,29 @@ void IQPuzzle::loadGame(QString sSaveFile) {
if (!sSaveFile.isEmpty()) {
QSettings tmpSet(sSaveFile, QSettings::IniFormat);
QString sBoard(tmpSet.value(QStringLiteral("BoardFile"), "").toString());
if (!sBoard.isEmpty()) {
QByteArray ba(tmpSet.value(
QStringLiteral("NumOfMoves"), "").toByteArray());
m_sSavedMoves = QByteArray::fromBase64(ba);
ba.clear();
ba = tmpSet.value(QStringLiteral("ElapsedTime"), "").toByteArray();
m_sSavedTime = QByteArray::fromBase64(ba);
this->startNewGame(sBoard, sSaveFile, m_sSavedTime, m_sSavedMoves);
} else {
QMessageBox::warning(this, qApp->applicationName(),
tr("Invalid saved puzzle."));
QString sBoardRel(tmpSet.value(
QStringLiteral("BoardFileRelative"), "").toString());
if (sBoard.isEmpty() || !QFile::exists(sBoard)) {
if (!sBoardRel.isEmpty() &&
QFile::exists(qApp->applicationDirPath() + "/" + sBoardRel)) {
sBoard = qApp->applicationDirPath() + "/" + sBoardRel;
} else {
QMessageBox::warning(this, qApp->applicationName(),
tr("Invalid saved puzzle."));
return;
}
}

QByteArray ba(tmpSet.value(
QStringLiteral("NumOfMoves"), "").toByteArray());
m_sSavedMoves = QByteArray::fromBase64(ba);
ba.clear();
ba = tmpSet.value(QStringLiteral("ElapsedTime"), "").toByteArray();
m_sSavedTime = QByteArray::fromBase64(ba);
this->startNewGame(sBoard, sSaveFile, m_sSavedTime, m_sSavedMoves);
} else {
QMessageBox::warning(this, qApp->applicationName(),
tr("Invalid saved puzzle."));
}
}

Expand Down

0 comments on commit d7298ce

Please sign in to comment.