File indexing completed on 2024-05-12 16:25:45

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "admincustomsoundmodel.h"
0008 #include <KLocalizedString>
0009 
0010 AdminCustomSoundModel::AdminCustomSoundModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 AdminCustomSoundModel::~AdminCustomSoundModel() = default;
0016 
0017 int AdminCustomSoundModel::rowCount(const QModelIndex &parent) const
0018 {
0019     if (parent.isValid()) {
0020         return 0; // flat model
0021     }
0022     return mCustomSounds.count();
0023 }
0024 
0025 QVariant AdminCustomSoundModel::headerData(int section, Qt::Orientation orientation, int role) const
0026 {
0027     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0028         switch (static_cast<CustomSoundsRoles>(section)) {
0029         case CustomSoundsRoles::Name:
0030             return i18n("Name");
0031         case CustomSoundsRoles::Identifier:
0032             return i18n("Identifier");
0033         }
0034     }
0035     return {};
0036 }
0037 
0038 int AdminCustomSoundModel::columnCount(const QModelIndex &parent) const
0039 {
0040     Q_UNUSED(parent)
0041     constexpr int val = static_cast<int>(CustomSoundsRoles::LastColumn) + 1;
0042     return val;
0043 }
0044 
0045 QVariant AdminCustomSoundModel::data(const QModelIndex &index, int role) const
0046 {
0047     if (index.row() < 0 || index.row() >= mCustomSounds.count()) {
0048         return {};
0049     }
0050     if (role != Qt::DisplayRole) {
0051         return {};
0052     }
0053 
0054     const CustomSoundInfo &customSound = mCustomSounds.at(index.row());
0055     const int col = index.column();
0056     switch (static_cast<CustomSoundsRoles>(col)) {
0057     case CustomSoundsRoles::Name:
0058         return customSound.name();
0059     case CustomSoundsRoles::Identifier:
0060         return customSound.identifier();
0061     }
0062 
0063     return {};
0064 }
0065 
0066 int AdminCustomSoundModel::total() const
0067 {
0068     return mCustomSounds.count();
0069 }
0070 
0071 void AdminCustomSoundModel::clear()
0072 {
0073     if (!mCustomSounds.isEmpty()) {
0074         beginResetModel();
0075         mCustomSounds.clear();
0076         endResetModel();
0077     }
0078 }
0079 
0080 void AdminCustomSoundModel::parseElements(const QJsonObject &obj)
0081 {
0082     clear();
0083     mCustomSounds.parseCustomSounds(obj);
0084     if (!mCustomSounds.isEmpty()) {
0085         beginInsertRows(QModelIndex(), 0, mCustomSounds.count() - 1);
0086         endInsertRows();
0087     }
0088     checkFullList();
0089     Q_EMIT totalChanged();
0090 }
0091 
0092 void AdminCustomSoundModel::checkFullList()
0093 {
0094     setHasFullList(mCustomSounds.count() == mCustomSounds.total());
0095 }
0096 
0097 const CustomSoundsInfo &AdminCustomSoundModel::customSounds() const
0098 {
0099     return mCustomSounds;
0100 }
0101 
0102 void AdminCustomSoundModel::setCustomSounds(const CustomSoundsInfo &newCustomSounds)
0103 {
0104     clear();
0105     if (!mCustomSounds.isEmpty()) {
0106         beginInsertRows(QModelIndex(), 0, mCustomSounds.count() - 1);
0107         mCustomSounds = newCustomSounds;
0108         endInsertRows();
0109     }
0110 }
0111 
0112 void AdminCustomSoundModel::addMoreElements(const QJsonObject &obj)
0113 {
0114     const int numberOfElement = mCustomSounds.count();
0115     mCustomSounds.parseCustomSounds(obj);
0116     beginInsertRows(QModelIndex(), numberOfElement, mCustomSounds.count() - 1);
0117     endInsertRows();
0118     checkFullList();
0119 }
0120 
0121 QList<int> AdminCustomSoundModel::hideColumns() const
0122 {
0123     return {CustomSoundsRoles::Identifier};
0124 }
0125 
0126 void AdminCustomSoundModel::removeElement(const QString &identifier)
0127 {
0128     const int userCount = mCustomSounds.count();
0129     for (int i = 0; i < userCount; ++i) {
0130         if (mCustomSounds.at(i).identifier() == identifier) {
0131             beginRemoveRows(QModelIndex(), i, i);
0132             mCustomSounds.takeAt(i);
0133             mCustomSounds.setTotal(mCustomSounds.count()); // Update total
0134             endRemoveRows();
0135             Q_EMIT totalChanged();
0136             break;
0137         }
0138     }
0139 }
0140 
0141 #include "moc_admincustomsoundmodel.cpp"