File indexing completed on 2024-10-06 03:31:32

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "kwqdocumentmodel.h"
0005 #include "stateprefs.h"
0006 #include <keduvocdocument.h>
0007 
0008 KWQDocumentModel::KWQDocumentModel(QObject *parent)
0009     : QAbstractListModel(parent)
0010 {
0011     load();
0012 }
0013 
0014 KWQDocumentModel::~KWQDocumentModel()
0015 {
0016     save();
0017 }
0018 
0019 void KWQDocumentModel::load()
0020 {
0021     const auto urls = StatePrefs::documents();
0022     if (urls.empty()) {
0023         add(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwordquiz/examples/example.kvtml"))));
0024         add(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwordquiz/examples/french_verbs.kvtml"))));
0025         add(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwordquiz/examples/fill_in_the_blank.kvtml"))));
0026         add(QUrl::fromLocalFile(
0027             QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwordquiz/examples/us_states_and_capitals.kvtml"))));
0028 
0029         return;
0030     }
0031 
0032     for (const auto &url : urls) {
0033         auto doc = std::make_unique<KEduVocDocument>(nullptr);
0034         doc->open(QUrl(url), KEduVocDocument::FileIgnoreLock);
0035         m_documents.push_back(std::move(doc));
0036     }
0037 }
0038 
0039 void KWQDocumentModel::save() const
0040 {
0041     QStringList urls;
0042     for (const auto &document : std::as_const(m_documents)) {
0043         urls << document->url().url();
0044     }
0045     StatePrefs::setDocuments(urls);
0046     StatePrefs::self()->save();
0047 }
0048 
0049 int KWQDocumentModel::rowCount(const QModelIndex &parent) const
0050 {
0051     return parent.isValid() ? 0 : m_documents.size();
0052 }
0053 
0054 QVariant KWQDocumentModel::data(const QModelIndex &index, int role) const
0055 {
0056     Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
0057 
0058     const auto &document = m_documents[index.row()];
0059 
0060     switch (role) {
0061     case Qt::DisplayRole:
0062     case TitleRole:
0063         return document->title();
0064 
0065     case UrlRole:
0066         return document->url().url();
0067 
0068     case DocumentRole:
0069         return QVariant::fromValue(document.get());
0070 
0071     default:
0072         return {};
0073     }
0074 }
0075 
0076 QHash<int, QByteArray> KWQDocumentModel::roleNames() const
0077 {
0078     return {
0079         { TitleRole, "title" },
0080         { UrlRole, "url" },
0081         { DocumentRole, "document" },
0082     };
0083 }
0084 
0085 
0086 void KWQDocumentModel::add(KEduVocDocument *document)
0087 {
0088     document->setParent(nullptr);
0089     const int row = rowCount();
0090     beginInsertRows({}, row, row);
0091     m_documents.push_back(std::unique_ptr<KEduVocDocument>(document));
0092     endInsertRows();
0093 
0094     save();
0095 }
0096 
0097 int KWQDocumentModel::add(const QUrl &url)
0098 {
0099     int row = 0;
0100     for (const auto &doc : std::as_const(m_documents)) {
0101         if (doc->url() == url) {
0102             qDebug() << row;
0103             return row;
0104         }
0105         row++;
0106     }
0107 
0108     auto doc = std::make_unique<KEduVocDocument>(nullptr);
0109     doc->open(url, KEduVocDocument::FileIgnoreLock);
0110 
0111     row = rowCount();
0112     beginInsertRows({}, row, row);
0113     m_documents.push_back(std::move(doc));
0114     endInsertRows();
0115 
0116     save();
0117 
0118     return row;
0119 }
0120 
0121 void KWQDocumentModel::remove(const int row)
0122 {
0123     beginRemoveRows({}, row, row);
0124     m_documents.erase(std::begin(m_documents) + row);
0125     endRemoveRows();
0126 }
0127 
0128 void KWQDocumentModel::entryChanged(const KNSCore::Entry &entry)
0129 {
0130     if (entry.status() == KNSCore::Entry::Deleted) {
0131         const auto uninstalledFiles = entry.uninstalledFiles();
0132         for (const auto &url : uninstalledFiles) {
0133             auto it = std::find_if(cbegin(m_documents), cend(m_documents), [url](const auto &document) {
0134                 return document->url() == QUrl(url);
0135             });
0136 
0137             if (it == cend(m_documents)) {
0138                 continue;
0139             }
0140 
0141             beginRemoveRows({}, cend(m_documents) - it, cend(m_documents) - it);
0142             m_documents.erase(it);
0143             endInsertRows();
0144         }
0145     } else if (entry.status() == KNSCore::Entry::Installed) {
0146         qDebug() << entry.installedFiles();
0147         const auto installedFiles = entry.installedFiles();
0148         beginInsertRows({}, rowCount(), rowCount() + entry.installedFiles().count() - 1);
0149         for (const auto &url : installedFiles) {
0150             auto doc = std::make_unique<KEduVocDocument>(nullptr);
0151             doc->open(QUrl(QStringLiteral("file:") + url), KEduVocDocument::FileIgnoreLock);
0152             m_documents.push_back(std::move(doc));
0153         }
0154         endInsertRows();
0155     }
0156 }
0157 
0158 #include "moc_kwqdocumentmodel.cpp"