249 lines
6.9 KiB
C++
249 lines
6.9 KiB
C++
#include "HistoryModel.h"
|
|
#include "AppState.h"
|
|
#include "FormatReader.h"
|
|
#include "formats/FormatRegistry.h"
|
|
|
|
#include <QDateTime>
|
|
#include <QFileInfo>
|
|
#include <QPointer>
|
|
#include <QtConcurrent>
|
|
|
|
namespace {
|
|
QString formatForPath(const QString &filePath)
|
|
{
|
|
if (QFileInfo(filePath).isDir())
|
|
return QStringLiteral("Folder");
|
|
return filePath.section('.', -1).toUpper();
|
|
}
|
|
}
|
|
|
|
HistoryModel::HistoryModel(FormatRegistry ®istry, QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
, m_registry(registry)
|
|
{
|
|
}
|
|
|
|
HistoryModel::~HistoryModel() = default;
|
|
|
|
int HistoryModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid())
|
|
return 0;
|
|
return m_entries.size();
|
|
}
|
|
|
|
QVariant HistoryModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
|
return {};
|
|
|
|
const auto &entry = m_entries.at(index.row());
|
|
switch (role) {
|
|
case FilePathRole:
|
|
return entry.filePath;
|
|
case TitleRole:
|
|
return entry.title;
|
|
case AuthorRole:
|
|
return entry.author;
|
|
case FormatRole:
|
|
return entry.format;
|
|
case ProgressRole:
|
|
return entry.progress;
|
|
case LastOpenedRole:
|
|
return entry.lastOpened;
|
|
case CoverImageUrlRole:
|
|
return entry.coverImageUrl;
|
|
default:
|
|
return {};
|
|
}
|
|
}
|
|
|
|
bool HistoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
|
|
return false;
|
|
|
|
auto &entry = m_entries[index.row()];
|
|
switch (role) {
|
|
case ProgressRole:
|
|
entry.progress = value.toDouble();
|
|
emit dataChanged(index, index, {ProgressRole});
|
|
return true;
|
|
case LastOpenedRole:
|
|
entry.lastOpened = value.toString();
|
|
emit dataChanged(index, index, {LastOpenedRole});
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
QHash<int, QByteArray> HistoryModel::roleNames() const
|
|
{
|
|
return {
|
|
{FilePathRole, "filePath"},
|
|
{TitleRole, "title"},
|
|
{AuthorRole, "author"},
|
|
{FormatRole, "format"},
|
|
{ProgressRole, "progress"},
|
|
{LastOpenedRole, "lastOpened"},
|
|
{CoverImageUrlRole, "coverImageUrl"}
|
|
};
|
|
}
|
|
|
|
void HistoryModel::load(AppState &state)
|
|
{
|
|
clear();
|
|
for (const QVariantMap &item : state.loadArray(QStringLiteral("history"))) {
|
|
const QString fp = item.value(QStringLiteral("filePath")).toString();
|
|
if (fp.isEmpty()) continue;
|
|
double progress = item.value(QStringLiteral("progress"), 0.0).toDouble();
|
|
progress = qBound(0.0, progress, 1.0);
|
|
const QString lo = item.value(QStringLiteral("lastOpened")).toString();
|
|
addOrUpdateBook(fp, progress, lo);
|
|
}
|
|
}
|
|
|
|
void HistoryModel::save(AppState &state) const
|
|
{
|
|
QList<QVariantMap> items;
|
|
items.reserve(m_entries.size());
|
|
for (const auto &entry : m_entries) {
|
|
items.append({
|
|
{QStringLiteral("filePath"), entry.filePath},
|
|
{QStringLiteral("progress"), entry.progress},
|
|
{QStringLiteral("lastOpened"), entry.lastOpened},
|
|
});
|
|
}
|
|
state.saveArray(QStringLiteral("history"), items);
|
|
}
|
|
|
|
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress, const QString &lastOpened)
|
|
{
|
|
for (int i = 0; i < m_entries.size(); ++i) {
|
|
if (m_entries[i].filePath == filePath) {
|
|
m_entries[i].progress = progress;
|
|
m_entries[i].lastOpened = lastOpened;
|
|
|
|
if (i != 0) {
|
|
beginMoveRows(QModelIndex(), i, i, QModelIndex(), 0);
|
|
m_entries.move(i, 0);
|
|
endMoveRows();
|
|
}
|
|
emit dataChanged(createIndex(0, 0), createIndex(0, 0), {ProgressRole, LastOpenedRole});
|
|
return;
|
|
}
|
|
}
|
|
|
|
HistoryEntry entry;
|
|
entry.filePath = filePath;
|
|
entry.progress = progress;
|
|
entry.lastOpened = lastOpened;
|
|
entry.format = formatForPath(filePath);
|
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
|
m_entries.prepend(entry);
|
|
endInsertRows();
|
|
emit countChanged();
|
|
|
|
requestMetadata(filePath);
|
|
}
|
|
|
|
void HistoryModel::addOrUpdateBook(const QString &filePath, double progress)
|
|
{
|
|
double resolvedProgress = progress;
|
|
if (progress < 0.0) {
|
|
for (const auto &entry : m_entries) {
|
|
if (entry.filePath == filePath) {
|
|
resolvedProgress = entry.progress;
|
|
break;
|
|
}
|
|
}
|
|
if (resolvedProgress < 0.0)
|
|
resolvedProgress = 0.0;
|
|
}
|
|
addOrUpdateBook(filePath, resolvedProgress,
|
|
QDateTime::currentDateTime().toString(Qt::ISODate));
|
|
}
|
|
|
|
void HistoryModel::removeBook(const QString &filePath)
|
|
{
|
|
for (int i = 0; i < m_entries.size(); ++i) {
|
|
if (m_entries[i].filePath == filePath) {
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
m_entries.removeAt(i);
|
|
endRemoveRows();
|
|
emit countChanged();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HistoryModel::clear()
|
|
{
|
|
if (m_entries.isEmpty())
|
|
return;
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_entries.size() - 1);
|
|
m_entries.clear();
|
|
endRemoveRows();
|
|
emit countChanged();
|
|
}
|
|
|
|
QString HistoryModel::filePath(int index) const
|
|
{
|
|
if (index < 0 || index >= m_entries.size()) return {};
|
|
return m_entries.at(index).filePath;
|
|
}
|
|
|
|
double HistoryModel::progress(int index) const
|
|
{
|
|
if (index < 0 || index >= m_entries.size()) return 0.0;
|
|
return m_entries.at(index).progress;
|
|
}
|
|
|
|
QString HistoryModel::lastOpened(int index) const
|
|
{
|
|
if (index < 0 || index >= m_entries.size()) return {};
|
|
return m_entries.at(index).lastOpened;
|
|
}
|
|
|
|
void HistoryModel::requestMetadata(const QString &filePath)
|
|
{
|
|
FormatRegistry *registry = &m_registry;
|
|
QPointer<HistoryModel> self(this);
|
|
|
|
(void)QtConcurrent::run([self, filePath, registry]() {
|
|
auto reader = registry->createReader(filePath);
|
|
QString title;
|
|
QString author;
|
|
QString coverUrl;
|
|
bool ok = false;
|
|
|
|
if (reader && reader->loadMetadata(filePath)) {
|
|
title = reader->title();
|
|
author = reader->author();
|
|
coverUrl = reader->coverImageUrl();
|
|
ok = true;
|
|
}
|
|
|
|
if (!self)
|
|
return;
|
|
|
|
QMetaObject::invokeMethod(self.data(), [self, filePath, title, author, coverUrl, ok]() {
|
|
if (!self || !ok) return;
|
|
for (int i = 0; i < self->m_entries.size(); ++i) {
|
|
if (self->m_entries[i].filePath == filePath) {
|
|
self->m_entries[i].title = title;
|
|
self->m_entries[i].author = author;
|
|
self->m_entries[i].coverImageUrl = coverUrl;
|
|
self->m_entries[i].metadataLoaded = true;
|
|
const QModelIndex idx = self->createIndex(i, 0);
|
|
emit self->dataChanged(idx, idx, {TitleRole, AuthorRole, CoverImageUrlRole});
|
|
break;
|
|
}
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
});
|
|
}
|