Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI|Client: Added a data item model for represented saved sessions
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** @file savedsessionlistdata.h UI data items representing available saved games. | ||
* | ||
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#ifndef DENG_CLIENT_UI_SAVEDSESSIONLISTDATA_H | ||
#define DENG_CLIENT_UI_SAVEDSESSIONLISTDATA_H | ||
|
||
#include <de/ui/ListData> | ||
#include <doomsday/SavedSession> | ||
|
||
/** | ||
* List data model for available saved sessions. | ||
*/ | ||
class SavedSessionListData : public de::ui::ListData | ||
{ | ||
public: | ||
struct SaveItem : public de::ui::Item, | ||
DENG2_OBSERVES(de::File, Deletion) | ||
{ | ||
SavedSession const *session; | ||
|
||
SaveItem(SavedSession const &session); | ||
~SaveItem(); | ||
|
||
bool isValid() const; | ||
de::String title() const; | ||
de::String gameId() const; | ||
de::String savePath() const; | ||
|
||
void fileBeingDeleted(de::File const &); | ||
}; | ||
|
||
public: | ||
SavedSessionListData(); | ||
|
||
private: | ||
DENG2_PRIVATE(d) | ||
}; | ||
|
||
#endif // DENG_CLIENT_UI_SAVEDSESSIONLISTDATA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** @file savedsessionlistdata.cpp | ||
* | ||
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi> | ||
* | ||
* @par License | ||
* GPL: http://www.gnu.org/licenses/gpl.html | ||
* | ||
* <small>This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by the | ||
* Free Software Foundation; either version 2 of the License, or (at your | ||
* option) any later version. This program is distributed in the hope that it | ||
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. You should have received a copy of the GNU | ||
* General Public License along with this program; if not, see: | ||
* http://www.gnu.org/licenses</small> | ||
*/ | ||
|
||
#include "ui/savedsessionlistdata.h" | ||
|
||
#include <doomsday/Session> | ||
#include <de/Loop> | ||
|
||
using namespace de; | ||
|
||
DENG2_PIMPL(SavedSessionListData) | ||
, DENG2_OBSERVES(Session::SavedIndex, AvailabilityUpdate) | ||
{ | ||
LoopCallback mainCall; | ||
|
||
Instance(Public *i) : Base(i) | ||
{ | ||
Session::savedIndex().audienceForAvailabilityUpdate() += this; | ||
} | ||
|
||
~Instance() | ||
{ | ||
Session::savedIndex().audienceForAvailabilityUpdate() -= this; | ||
} | ||
|
||
void updateFromSavedIndex() | ||
{ | ||
// Remove obsolete entries. | ||
for(ui::Data::Pos idx = self.size() - 1; idx < self.size(); --idx) | ||
{ | ||
if(!self.at(idx).as<SaveItem>().isValid()) | ||
{ | ||
self.remove(idx); | ||
} | ||
} | ||
|
||
// Add new entries. | ||
DENG2_FOR_EACH_CONST(Session::SavedIndex::All, i, Session::savedIndex().all()) | ||
{ | ||
ui::Data::Pos found = self.findData(i.key()); | ||
if(found == ui::Data::InvalidPos) | ||
{ | ||
SavedSession &session = *i.value(); | ||
if(session.path().beginsWith("/home/savegames")) // Ignore non-user savegames. | ||
{ | ||
// Needs to be added. | ||
self.append(new SaveItem(session)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void savedIndexAvailabilityUpdate(Session::SavedIndex const &) | ||
{ | ||
mainCall.enqueue([this] () { updateFromSavedIndex(); }); | ||
} | ||
}; | ||
|
||
SavedSessionListData::SavedSessionListData() | ||
: d(new Instance(this)) | ||
{ | ||
d->updateFromSavedIndex(); | ||
} | ||
|
||
// SaveItem ------------------------------------------------------------------- | ||
|
||
SavedSessionListData::SaveItem::SaveItem(SavedSession const &session) | ||
: session(&session) | ||
{ | ||
session.audienceForDeletion() += this; | ||
} | ||
|
||
SavedSessionListData::SaveItem::~SaveItem() | ||
{ | ||
if(session) session->audienceForDeletion() -= this; | ||
} | ||
|
||
bool SavedSessionListData::SaveItem::isValid() const | ||
{ | ||
return session != nullptr; | ||
} | ||
|
||
String SavedSessionListData::SaveItem::title() const | ||
{ | ||
if(session) | ||
{ | ||
return session->metadata().gets("userDescription"); | ||
} | ||
return ""; | ||
} | ||
|
||
String SavedSessionListData::SaveItem::gameId() const | ||
{ | ||
if(session) | ||
{ | ||
return session->metadata().gets("gameIdentityKey"); | ||
} | ||
return ""; | ||
} | ||
|
||
String SavedSessionListData::SaveItem::savePath() const | ||
{ | ||
if(session) | ||
{ | ||
return session->path().toLower(); | ||
} | ||
return ""; | ||
} | ||
|
||
void SavedSessionListData::SaveItem::fileBeingDeleted(File const &) | ||
{ | ||
session = nullptr; | ||
} |