File indexing completed on 2024-06-16 05:08:35

0001 #include "kcmslistmodel.h"
0002 #include <QFile>
0003 
0004 KcmsListModel::KcmsListModel(QObject *parent)
0005     : QAbstractListModel(parent)
0006 {
0007     connect(&m_configuration, &Configuration::mycroftEnabledChanged, this, &KcmsListModel::loadKcms);
0008 }
0009 
0010 KcmsListModel::~KcmsListModel() = default;
0011 
0012 QHash<int, QByteArray> KcmsListModel::roleNames() const
0013 {
0014     QHash<int, QByteArray> roles;
0015     roles[KcmIdRole] = "kcmId";
0016     roles[KcmIconNameRole] = "kcmIconName";
0017     roles[KcmDescriptionRole] = "kcmDescription";
0018     roles[KcmNameRole] = "kcmName";
0019     roles[KcmRole] = "kcm";
0020     return roles;
0021 }
0022 
0023 int KcmsListModel::count()
0024 {
0025     return m_kcms.count();
0026 }
0027 
0028 void KcmsListModel::loadKcms()
0029 {
0030     qDebug() << "Loading kcms";
0031     beginResetModel();
0032     m_kcms.clear();
0033 
0034     m_mycroftEnabled = m_configuration.mycroftEnabled();
0035 
0036     QMap<int, KcmData> orderedList;
0037     QList<KcmData> unorderedList;
0038 
0039     const auto kcmPlugins = KPluginMetaData::findPlugins("kcms");
0040     // only get the mediacenter kcms
0041     for (const auto &kcm : kcmPlugins) {
0042         if (kcm.pluginId().contains("mediacenter")) {
0043             KcmData kcmData;
0044             kcmData.name = kcm.name();
0045             kcmData.description = kcm.description();
0046             kcmData.iconName = kcm.iconName();
0047             kcmData.id = kcm.pluginId();
0048 
0049             auto it = m_appPositions.constFind(kcm.pluginId());
0050             if (it != m_appPositions.constEnd()) {
0051                 orderedList.insert(it.value(), kcmData);
0052             } else {
0053                 unorderedList.append(kcmData);
0054             }
0055         }
0056     }
0057 
0058     KcmData wallpaperData;
0059     wallpaperData.name = "Wallpaper";
0060     wallpaperData.iconName = "preferences-desktop-wallpaper";
0061     wallpaperData.description = "Change the desktop wallpaper";
0062     wallpaperData.id = "kcm_mediacenter_wallpaper";
0063     unorderedList.append(wallpaperData);
0064 
0065     // Mycroft-Skill-Installer is no longer maintained or functional
0066     // Disable forced entry
0067 
0068     // KcmData mycroftSkillInstallerData;
0069     // mycroftSkillInstallerData.name = "Mycroft Skill Installer";
0070     // mycroftSkillInstallerData.iconName = "download";
0071     // mycroftSkillInstallerData.description = "Install Mycroft skills";
0072     // mycroftSkillInstallerData.id = "kcm_mediacenter_mycroft_skill_installer";
0073 
0074     // if (m_mycroftEnabled) {
0075     //     unorderedList.append(mycroftSkillInstallerData);
0076     // }
0077 
0078     m_kcms << orderedList.values();
0079     m_kcms << unorderedList;
0080 
0081     endResetModel();
0082     Q_EMIT countChanged();
0083 
0084     qDebug() << "KCM's discovered: " << m_kcms.size();
0085 }
0086 
0087 QVariant KcmsListModel::data(const QModelIndex &index, int role) const
0088 {
0089     if (!index.isValid()) {
0090         return QVariant();
0091     }
0092 
0093     switch (role) {
0094     case Qt::DisplayRole:
0095     case KcmIdRole:
0096         return m_kcms.at(index.row()).id;
0097     case KcmIconNameRole:
0098         return m_kcms.at(index.row()).iconName;
0099     case KcmDescriptionRole:
0100         return m_kcms.at(index.row()).description;
0101     case KcmNameRole:
0102         return m_kcms.at(index.row()).name;
0103     case KcmRole:
0104         return m_kcms.at(index.row()).id;
0105     default:
0106         return QVariant();
0107     }
0108 }
0109 
0110 void KcmsListModel::moveRow(const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild)
0111 {
0112     Q_UNUSED(sourceParent);
0113     Q_UNUSED(destinationParent);
0114     moveItem(sourceRow, destinationChild);
0115 }
0116 
0117 void KcmsListModel::moveItem(int row, int destination)
0118 {
0119     if (row < 0 || destination < 0 || row >= m_kcms.length() || destination >= m_kcms.length() || row == destination) {
0120         return;
0121     }
0122     if (destination > row) {
0123         ++destination;
0124     }
0125 
0126     beginMoveRows(QModelIndex(), row, row, QModelIndex(), destination);
0127     if (destination > row) {
0128         KcmData data = m_kcms.at(row);
0129         m_kcms.insert(destination, data);
0130         m_kcms.takeAt(row);
0131     } else {
0132         KcmData data = m_kcms.takeAt(row);
0133         m_kcms.insert(destination, data);
0134     }
0135 
0136     m_appOrder.clear();
0137     m_appPositions.clear();
0138     int i = 0;
0139     for (const auto &app : std::as_const(m_kcms)) {
0140         m_appOrder << app.id;
0141         m_appPositions[app.id] = i;
0142         ++i;
0143     }
0144 
0145     Q_EMIT appOrderChanged();
0146     endMoveRows();
0147 }
0148 
0149 int KcmsListModel::rowCount(const QModelIndex &parent) const
0150 {
0151     if (parent.isValid()) {
0152         return 0;
0153     }
0154 
0155     return m_kcms.count();
0156 }
0157 
0158 Qt::ItemFlags KcmsListModel::flags(const QModelIndex &index) const
0159 {
0160     if (!index.isValid()) {
0161         return Qt::NoItemFlags;
0162     }
0163     return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0164 }
0165 
0166 QStringList KcmsListModel::appOrder() const
0167 {
0168     return m_appOrder;
0169 }
0170 
0171 void KcmsListModel::setAppOrder(const QStringList &order)
0172 {
0173     if (m_appOrder == order) {
0174         return;
0175     }
0176 
0177     m_appOrder = order;
0178     m_appPositions.clear();
0179     int i = 0;
0180     for (const auto &app : std::as_const(m_appOrder)) {
0181         m_appPositions[app] = i;
0182         ++i;
0183     }
0184     Q_EMIT appOrderChanged();
0185 }