Skip to content

Commit

Permalink
UI|Client: Added a data item model for represented saved sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 4, 2016
1 parent 37a2c3d commit ba70593
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
54 changes: 54 additions & 0 deletions doomsday/apps/client/include/ui/savedsessionlistdata.h
@@ -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
3 changes: 3 additions & 0 deletions doomsday/apps/client/src/ui/home/homewidget.cpp
Expand Up @@ -20,6 +20,7 @@
#include "ui/home/columnwidget.h"
#include "ui/home/nogamescolumnwidget.h"
#include "ui/home/gamecolumnwidget.h"
#include "ui/savedsessionlistdata.h"

#include <doomsday/doomsdayapp.h>
#include <doomsday/games.h>
Expand All @@ -35,6 +36,8 @@ static TimeDelta const SCROLL_SPAN = .5;
DENG_GUI_PIMPL(HomeWidget)
, DENG2_OBSERVES(Games, Readiness)
{
SavedSessionListData saveItems; ///< All the available save games as items.

dsize visibleColumnCount = 3; ///< Target.
QList<ColumnWidget *> allColumns; // not owned
QList<ColumnWidget *> columns; // Only the visible ones (not owned).
Expand Down
128 changes: 128 additions & 0 deletions doomsday/apps/client/src/ui/savedsessionlistdata.cpp
@@ -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;
}

0 comments on commit ba70593

Please sign in to comment.