File indexing completed on 2024-05-12 16:23:46

0001 // SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "webappmanagermodel.h"
0006 
0007 #include "webappmanager.h"
0008 
0009 WebAppManagerModel::WebAppManagerModel(QObject *parent)
0010     : QAbstractListModel(parent)
0011     , m_webAppMngr(WebAppManager::instance())
0012 {
0013 }
0014 
0015 WebAppManagerModel::~WebAppManagerModel() = default;
0016 
0017 int WebAppManagerModel::rowCount(const QModelIndex &index) const
0018 {
0019     return index.isValid() ? 0 : int(m_webAppMngr.applications().size());
0020 }
0021 
0022 QVariant WebAppManagerModel::data(const QModelIndex &index, int role) const
0023 {
0024     switch (role) {
0025     case Role::NameRole:
0026         return m_webAppMngr.applications()[index.row()].name;
0027     case Role::IconRole:
0028         return QString(WebAppManager::iconDirectory() + QString(QDir::separator()) + m_webAppMngr.applications()[index.row()].icon);
0029     case Role::UrlRole:
0030         return m_webAppMngr.applications()[index.row()].url;
0031     }
0032 
0033     Q_UNREACHABLE();
0034 
0035     return {};
0036 }
0037 
0038 QHash<int, QByteArray> WebAppManagerModel::roleNames() const
0039 {
0040     return {
0041         {Role::NameRole, QByteArrayLiteral("name")},
0042         {Role::IconRole, QByteArrayLiteral("desktopIcon")},
0043         {Role::UrlRole, QByteArrayLiteral("url")},
0044     };
0045 }
0046 
0047 void WebAppManagerModel::removeApp(int index)
0048 {
0049     beginRemoveRows({}, index, index);
0050     m_webAppMngr.removeApp(m_webAppMngr.applications()[index].name);
0051     endRemoveRows();
0052 }
0053 
0054 #include "moc_webappmanagermodel.cpp"