Commit 90051d7c authored by Christian Ehrlicher's avatar Christian Ehrlicher

Fix for QStandardPaths porting

Summary:
Since QStandardPaths::locate() only returns a valid filename when
the file exists, we have to make sure to not pass an empty
filename to KConfig when trying to restore / save a puzzle

Test Plan:
1. remove ~/.local/share/palapeli (very important!)
           2. Start a puzzle, quit palapeli
           3. Start palapeli and proceed with the last puzzle
              -> puzzle is loaded correctly
           4. Try to restart puuzle (after finished or within)
              -> all pieces are placed randomly

Reviewers: #kde_games, ltoscano

Reviewed By: ltoscano

Subscribers: ltoscano, #kde_games

Differential Revision: https://phabricator.kde.org/D7502
parent f9490f5b
......@@ -512,10 +512,12 @@ void Palapeli::GamePlay::restartPuzzle()
return; // If no puzzle was successfully loaded and started.
}
// Discard the *.save file.
static const QString pathTemplate =
QString::fromLatin1("collection/%1.save");
QFile(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
pathTemplate.arg(m_puzzle->identifier()))).remove();
const QString puzzleLoc(
QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
saveGamePath() + saveGameFileName(m_puzzle->identifier())));
if (!puzzleLoc.isEmpty())
QFile(puzzleLoc).remove();
// Load the puzzle and re-shuffle the pieces.
loadPuzzle();
}
......@@ -730,14 +732,18 @@ void Palapeli::GamePlay::loadPuzzle()
m_savegameTimer->stop(); // Just in case it is running ...
emit reportProgress(0, 0);
// Is there a saved game?
static const QString pathTemplate =
QString::fromLatin1("collection/%1.save");
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
pathTemplate.arg(m_puzzle->identifier())));
const QString puzzleLoc(
QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
saveGamePath() + saveGameFileName(m_puzzle->identifier())));
if (!puzzleLoc.isEmpty())
{
KConfig savedConfig(puzzleLoc, KConfig::SimpleConfig);
if (savedConfig.hasGroup(AppearanceSaveGroup)) {
// Get settings for background, shadows, etc. in this puzzle.
restorePuzzleSettings(&savedConfig);
}
}
// Return to the event queue to start the loading-widget graphics ASAP.
QTimer::singleShot(0, this, SLOT(loadPuzzleFile()));
qDebug() << "END loadPuzzle()";
......@@ -831,13 +837,15 @@ void Palapeli::GamePlay::loadPiecePositions()
m_puzzleTableScene->setPieceAreaSize(m_pieceAreaSize);
// Is there a saved game?
static const QString pathTemplate =
QString::fromLatin1("collection/%1.save");
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
pathTemplate.arg(m_puzzle->identifier())));
const QString puzzleLoc(
QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
saveGamePath() + saveGameFileName(m_puzzle->identifier())));
// empty -> file not found -> no saved game
bool oldFormat = false;
m_restoredGame = false;
int nHolders = 0;
KConfig savedConfig(puzzleLoc, KConfig::SimpleConfig); // here because needed inside 'if (m_restoredGame)'
if (!puzzleLoc.isEmpty()) {
if (savedConfig.hasGroup(HeaderSaveGroup)) {
KConfigGroup headerGroup(&savedConfig, HeaderSaveGroup);
nHolders = headerGroup.readEntry("N_Holders", 0);
......@@ -847,6 +855,7 @@ void Palapeli::GamePlay::loadPiecePositions()
m_restoredGame = true;
oldFormat = true;
}
}
if (m_restoredGame)
{
// IDW TODO - Enable piece-holder actions.
......@@ -1196,10 +1205,13 @@ void Palapeli::GamePlay::positionChanged(int reduction)
void Palapeli::GamePlay::updateSavedGame()
{
static const QString pathTemplate =
QString::fromLatin1("collection/%1.save");
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
pathTemplate.arg(m_puzzle->identifier())));
QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
QLatin1Char('/') + saveGamePath();
QDir d(path);
if (!d.exists())
d.mkpath(path);
KConfig savedConfig(path + saveGameFileName(m_puzzle->identifier()), KConfig::SimpleConfig);
savePuzzleSettings(&savedConfig);
......
......@@ -101,6 +101,8 @@ namespace Palapeli
void handleNewPieceSelection(View* view);
private:
static QString saveGamePath() { return QStringLiteral("collection/"); }
static QString saveGameFileName(const QString &name) { return QStringLiteral("%1.save").arg(name); }
void deletePuzzleViews();
void loadPuzzle();
void playVictoryAnimation();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment