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() ...@@ -512,10 +512,12 @@ void Palapeli::GamePlay::restartPuzzle()
return; // If no puzzle was successfully loaded and started. return; // If no puzzle was successfully loaded and started.
} }
// Discard the *.save file. // Discard the *.save file.
static const QString pathTemplate = const QString puzzleLoc(
QString::fromLatin1("collection/%1.save"); QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
QFile(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, saveGamePath() + saveGameFileName(m_puzzle->identifier())));
pathTemplate.arg(m_puzzle->identifier()))).remove(); if (!puzzleLoc.isEmpty())
QFile(puzzleLoc).remove();
// Load the puzzle and re-shuffle the pieces. // Load the puzzle and re-shuffle the pieces.
loadPuzzle(); loadPuzzle();
} }
...@@ -730,13 +732,17 @@ void Palapeli::GamePlay::loadPuzzle() ...@@ -730,13 +732,17 @@ void Palapeli::GamePlay::loadPuzzle()
m_savegameTimer->stop(); // Just in case it is running ... m_savegameTimer->stop(); // Just in case it is running ...
emit reportProgress(0, 0); emit reportProgress(0, 0);
// Is there a saved game? // Is there a saved game?
static const QString pathTemplate = const QString puzzleLoc(
QString::fromLatin1("collection/%1.save"); QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, saveGamePath() + saveGameFileName(m_puzzle->identifier())));
pathTemplate.arg(m_puzzle->identifier())));
if (savedConfig.hasGroup(AppearanceSaveGroup)) { if (!puzzleLoc.isEmpty())
// Get settings for background, shadows, etc. in this puzzle. {
restorePuzzleSettings(&savedConfig); 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. // Return to the event queue to start the loading-widget graphics ASAP.
QTimer::singleShot(0, this, SLOT(loadPuzzleFile())); QTimer::singleShot(0, this, SLOT(loadPuzzleFile()));
...@@ -831,21 +837,24 @@ void Palapeli::GamePlay::loadPiecePositions() ...@@ -831,21 +837,24 @@ void Palapeli::GamePlay::loadPiecePositions()
m_puzzleTableScene->setPieceAreaSize(m_pieceAreaSize); m_puzzleTableScene->setPieceAreaSize(m_pieceAreaSize);
// Is there a saved game? // Is there a saved game?
static const QString pathTemplate = const QString puzzleLoc(
QString::fromLatin1("collection/%1.save"); QStandardPaths::locate(QStandardPaths::AppLocalDataLocation,
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, saveGamePath() + saveGameFileName(m_puzzle->identifier())));
pathTemplate.arg(m_puzzle->identifier()))); // empty -> file not found -> no saved game
bool oldFormat = false; bool oldFormat = false;
m_restoredGame = false; m_restoredGame = false;
int nHolders = 0; int nHolders = 0;
if (savedConfig.hasGroup(HeaderSaveGroup)) { KConfig savedConfig(puzzleLoc, KConfig::SimpleConfig); // here because needed inside 'if (m_restoredGame)'
KConfigGroup headerGroup(&savedConfig, HeaderSaveGroup); if (!puzzleLoc.isEmpty()) {
nHolders = headerGroup.readEntry("N_Holders", 0); if (savedConfig.hasGroup(HeaderSaveGroup)) {
m_restoredGame = true; KConfigGroup headerGroup(&savedConfig, HeaderSaveGroup);
} nHolders = headerGroup.readEntry("N_Holders", 0);
else if (savedConfig.hasGroup(FormerSaveGroup)) { m_restoredGame = true;
m_restoredGame = true; }
oldFormat = true; else if (savedConfig.hasGroup(FormerSaveGroup)) {
m_restoredGame = true;
oldFormat = true;
}
} }
if (m_restoredGame) if (m_restoredGame)
{ {
...@@ -1196,10 +1205,13 @@ void Palapeli::GamePlay::positionChanged(int reduction) ...@@ -1196,10 +1205,13 @@ void Palapeli::GamePlay::positionChanged(int reduction)
void Palapeli::GamePlay::updateSavedGame() void Palapeli::GamePlay::updateSavedGame()
{ {
static const QString pathTemplate = QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
QString::fromLatin1("collection/%1.save"); QLatin1Char('/') + saveGamePath();
KConfig savedConfig(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QDir d(path);
pathTemplate.arg(m_puzzle->identifier()))); if (!d.exists())
d.mkpath(path);
KConfig savedConfig(path + saveGameFileName(m_puzzle->identifier()), KConfig::SimpleConfig);
savePuzzleSettings(&savedConfig); savePuzzleSettings(&savedConfig);
......
...@@ -101,6 +101,8 @@ namespace Palapeli ...@@ -101,6 +101,8 @@ namespace Palapeli
void handleNewPieceSelection(View* view); void handleNewPieceSelection(View* view);
private: private:
static QString saveGamePath() { return QStringLiteral("collection/"); }
static QString saveGameFileName(const QString &name) { return QStringLiteral("%1.save").arg(name); }
void deletePuzzleViews(); void deletePuzzleViews();
void loadPuzzle(); void loadPuzzle();
void playVictoryAnimation(); 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