[OpenDocString] kdeconnect-kde (cpp)
pluginmodel.cpp
PluginModel::PluginModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(this, &QAbstractItemModel::rowsInserted, this, &PluginModel::rowsChanged);
    connect(this, &QAbstractItemModel::rowsRemoved, this, &PluginModel::rowsChanged);

    beginResetModel();
    m_plugins = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"));
    endResetModel();
}
Constructs a plugin model object. The parent object is the parent object, and sets up signal/slot connections for row inserted and row removed events.
PluginModel::~PluginModel()
{
}
This constructor builds a plugin model object.
void PluginModel::setDeviceId(const QString &deviceId)
{
    if (deviceId == m_deviceId)
        return;

    m_deviceId = deviceId;
    DeviceDbusInterface *device = new DeviceDbusInterface(m_deviceId);
    m_config = KSharedConfig::openConfig(device->pluginsConfigFile());
}
Sets the device id by storing it in the m_deviceId, and opens the device - wide config file.
QVariant PluginModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    const KPluginMetaData &pluginEntry = m_plugins[index.row()];

    switch (role) {
    case Qt::CheckStateRole: {
        const QString def = pluginEntry.isEnabledByDefault() ? QStringLiteral("true") : QStringLiteral("false");
        return m_config->group("Plugins").readEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), def) == QStringLiteral("true");
    }
    case Qt::DisplayRole:
        return pluginEntry.name();
    case IconRole:
        return pluginEntry.iconName();
    case IdRole:
        return pluginEntry.pluginId();
    case ConfigSourceRole: {
        const QString configFile =
            QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kdeconnect/%1_config.qml").arg(pluginEntry.pluginId()));
        if (configFile.isEmpty())
            return QUrl();

        return QUrl::fromLocalFile(configFile);
    }
    case DescriptionRole:
        return pluginEntry.description();
    default:
        return QVariant();
    }
}
Returns the data for the given index and role.
QHash PluginModel::roleNames() const
{
    QHash roles = QAbstractItemModel::roleNames();
    roles[Qt::DisplayRole] = "name";
    roles[Qt::CheckStateRole] = "isChecked";
    roles[IconRole] = "iconName";
    roles[IdRole] = "pluginId";
    roles[ConfigSourceRole] = "configSource";
    roles[DescriptionRole] = "description";
    return roles;
}
Returns a hash of the roles.
bool PluginModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid()) {
        return false;
    }

    bool ret = false;

    if (role == Qt::CheckStateRole) {
        const KPluginMetaData &pluginEntry = m_plugins[index.row()];
        m_config->group("Plugins").writeEntry(QStringLiteral("%1Enabled").arg(pluginEntry.pluginId()), value);
        ret = true;
    }

    m_config->sync();

    if (ret) {
        Q_EMIT dataChanged(index, index);
    }

    return ret;
}
Sets the data for the given index to the given value and a role. It returns true if the index is invalid, and sets the value to the role in the plugin meta data.
int PluginModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }

    return m_plugins.size();
}
Returns the row count for the given parent index. If the parent index is invalid, this function returns 0.
QString PluginModel::deviceId()
{
    return m_deviceId;
}
Returns the device id stored in the internal list.