File indexing completed on 2025-01-26 05:06:22

0001 /*
0002     SPDX-FileCopyrightText: 2008 Fredrik Höglund <fredrik@kde.org>
0003     SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "previewpluginsmodel.h"
0009 
0010 #include <KIO/PreviewJob>
0011 
0012 #include <algorithm>
0013 
0014 static bool lessThan(const KPluginMetaData &a, const KPluginMetaData &b)
0015 {
0016     return QString::localeAwareCompare(a.name(), b.name()) < 0;
0017 }
0018 
0019 PreviewPluginsModel::PreviewPluginsModel(QObject *parent)
0020     : QAbstractListModel(parent)
0021     , m_plugins(KIO::PreviewJob::availableThumbnailerPlugins())
0022 {
0023     std::stable_sort(m_plugins.begin(), m_plugins.end(), lessThan);
0024 
0025     m_checkedRows = QList<bool>(m_plugins.size(), false);
0026 }
0027 
0028 PreviewPluginsModel::~PreviewPluginsModel()
0029 {
0030 }
0031 
0032 QHash<int, QByteArray> PreviewPluginsModel::roleNames() const
0033 {
0034     return {
0035         {Qt::DisplayRole, "display"},
0036         {Qt::CheckStateRole, "checked"},
0037     };
0038 }
0039 
0040 QVariant PreviewPluginsModel::data(const QModelIndex &index, int role) const
0041 {
0042     if (index.row() < 0 || index.row() >= m_plugins.size()) {
0043         return QVariant();
0044     }
0045 
0046     switch (role) {
0047     case Qt::DisplayRole:
0048         return m_plugins.at(index.row()).name();
0049 
0050     case Qt::CheckStateRole:
0051         return m_checkedRows.at(index.row()) ? Qt::Checked : Qt::Unchecked;
0052     }
0053 
0054     return QVariant();
0055 }
0056 
0057 bool PreviewPluginsModel::setData(const QModelIndex &index, const QVariant &value, int role)
0058 {
0059     if (index.row() < 0 || index.row() >= m_plugins.size()) {
0060         return false;
0061     }
0062 
0063     if (role == Qt::CheckStateRole) {
0064         m_checkedRows[index.row()] = value.toBool();
0065         Q_EMIT dataChanged(index, index, {role});
0066         return true;
0067     }
0068 
0069     return false;
0070 }
0071 
0072 int PreviewPluginsModel::indexOfPlugin(const QString &pluginId) const
0073 {
0074     for (int i = 0; i < m_plugins.size(); i++) {
0075         if (m_plugins.at(i).pluginId() == pluginId) {
0076             return i;
0077         }
0078     }
0079     return -1;
0080 }
0081 
0082 void PreviewPluginsModel::setCheckedPlugins(const QStringList &list)
0083 {
0084     QStringList plugins = list;
0085     if (plugins.isEmpty()) {
0086         plugins = KIO::PreviewJob::defaultPlugins();
0087     }
0088 
0089     m_checkedRows = QList<bool>(m_plugins.size(), false);
0090 
0091     for (const QString &name : std::as_const(plugins)) {
0092         const int row = indexOfPlugin(name);
0093         if (row != -1) {
0094             m_checkedRows[row] = true;
0095         }
0096     }
0097 
0098     Q_EMIT dataChanged(index(0, 0), index(m_plugins.size() - 1, 0), {Qt::CheckStateRole});
0099 
0100     Q_EMIT checkedPluginsChanged();
0101 }
0102 
0103 QStringList PreviewPluginsModel::checkedPlugins() const
0104 {
0105     QStringList list;
0106     for (int i = 0; i < m_checkedRows.size(); ++i) {
0107         if (m_checkedRows.at(i)) {
0108             list.append(m_plugins.at(i).pluginId());
0109         }
0110     }
0111 
0112     const QStringList defaultPlugins = KIO::PreviewJob::defaultPlugins();
0113 
0114     // If the list of checked plugins is the default set, return an empty list
0115     // which denotes the default set.
0116     // This ensures newly installed thumbnails are always automatically enabled.
0117     if (std::is_permutation(list.constBegin(), list.constEnd(), defaultPlugins.begin())) {
0118         return QStringList();
0119     }
0120 
0121     return list;
0122 }