[OpenDocString] kdeconnect-kde (cpp)
notifyingapplicationmodel.cpp
NotifyingApplicationModel::NotifyingApplicationModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}
This constructor builds a notifying application model object. It takes the parent object of the application model and its duties.
NotifyingApplicationModel::~NotifyingApplicationModel()
{
}
This removes the notification method from the application model list.
QVector NotifyingApplicationModel::apps()
{
    return m_apps;
}
Returns a copy of the apps list, which uses implicit sharing for its internal state.
void NotifyingApplicationModel::appendApp(const NotifyingApplication &app)
{
    if (app.name.isEmpty() || apps().contains(app))
        return;
    beginInsertRows(QModelIndex(), m_apps.size(), m_apps.size());
    m_apps.append(app);
    endInsertRows();
}
This adds an NotifyingApplication object to the internal list of apps. It only adds the application if it doesn't exist. It inserts the number of rows to the internal list of apps, if the list is not empty, and ends the insert rows.
bool NotifyingApplicationModel::containsApp(const QString &name) const
{
    for (const auto &a : m_apps)
        if (a.name == name)
            return true;
    return false;
}
Returns true if the application with the given name exists.
Qt::ItemFlags NotifyingApplicationModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = Qt::ItemIsEnabled;
    if (index.isValid() && index.row() >= 0 && index.row() < m_apps.size() && index.column() < 3) {
        if (index.column() == 0)
            flags |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
        else if (index.column() == 2) {
            if (m_apps[index.row()].active)
                flags |= Qt::ItemIsEditable;
            else
                flags ^= Qt::ItemIsEnabled;
        } else if (index.column() == 1) {
            if (!m_apps[index.row()].active)
                flags ^= Qt::ItemIsEnabled;
        }
    }
    return flags;
}
Returns the item flags for the given index.
void NotifyingApplicationModel::clearApplications()
{
    if (!m_apps.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, m_apps.size() - 1);
        m_apps.clear();
        endRemoveRows();
    }
}
This removes all applications from the internal list.
QVariant NotifyingApplicationModel::data(const QModelIndex &index, int role) const
{
    Q_UNUSED(role);
    if (!index.isValid() || index.row() < 0 || index.row() >= m_apps.size() || index.column() > 3) {
        return QVariant();
    }

    switch (role) {
    case Qt::TextAlignmentRole: {
        if (index.column() == 0)
            return int(Qt::AlignCenter | Qt::AlignVCenter);
        else
            return int(Qt::AlignLeft | Qt::AlignVCenter);
        break;
    }
    case Qt::DisplayRole: {
        if (index.column() == 1)
            return m_apps[index.row()].name;
        else if (index.column() == 0)
            return QVariant(); // m_apps[index.row()].active;
        else if (index.column() == 2)
            return m_apps[index.row()].blacklistExpression.pattern();
        else
            return QVariant();
        break;
    }
    case Qt::DecorationRole: {
        if (index.column() == 1)
            return QIcon::fromTheme(m_apps[index.row()].icon, QIcon::fromTheme(QStringLiteral("application-x-executable")));
        else
            return QVariant();
        break;
    }
    case Qt::EditRole: {
        if (index.column() == 0)
            return m_apps[index.row()].active ? Qt::Checked : Qt::Unchecked;
        else if (index.column() == 2)
            return m_apps[index.row()].blacklistExpression.pattern();
        else
            return QVariant();
        break;
    }
    case Qt::CheckStateRole: {
        if (index.column() == 0)
            return m_apps[index.row()].active ? Qt::Checked : Qt::Unchecked;
        else
            return QVariant();
        break;
    }
    }
    return QVariant();
}
Returns a QVariant object that represents the data of the given index in the internal list of apps.
bool NotifyingApplicationModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || (index.column() != 0 && index.column() != 2) || index.row() < 0 || index.row() >= m_apps.size())
        return false;

    bool res = false;
    QModelIndex bottomRight = createIndex(index.row(), index.column());
    switch (role) {
    case Qt::CheckStateRole: {
        if (index.column() == 0) {
            m_apps[index.row()].active = ((Qt::CheckState)value.toInt() == Qt::Checked);
            bottomRight = createIndex(index.row(), index.column() + 1);
            res = true;
        }
        break;
    }
    case Qt::EditRole: {
        if (index.column() == 2) {
            m_apps[index.row()].blacklistExpression.setPattern(value.toString());
            res = true;
        }
    }
    }
    if (res) {
        Q_EMIT dataChanged(index, bottomRight);
        Q_EMIT applicationsChanged(); // -> notify config that we need to save
    }
    return res;
}
Sets the data value for the given index and role to the given value. It creates the bottom - right index, and sets the active value on the application model. It returns true on the first successful change, and false otherwise.
void NotifyingApplicationModel::sort(int column, Qt::SortOrder order)
{
    if (column != 1)
        return;

    if (order == Qt::AscendingOrder)
        std::sort(m_apps.begin(), m_apps.end(), [](const NotifyingApplication &a, const NotifyingApplication &b) {
            return (a.name.compare(b.name, Qt::CaseInsensitive) < 1);
        });
    else
        std::sort(m_apps.begin(), m_apps.end(), [](const NotifyingApplication &a, const NotifyingApplication &b) {
            return (b.name.compare(a.name, Qt::CaseInsensitive) < 1);
        });
    Q_EMIT dataChanged(createIndex(0, 0), createIndex(m_apps.size(), 2));
}
This sorts the NotifyingApplications in the given column by the given sort order. If the column is not 1, this function does nothing.
QVariant NotifyingApplicationModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
{
    switch (role) {
    case Qt::DisplayRole: {
        if (section == 1)
            return i18n("Name");
        else if (section == 0)
            return QVariant(); // i18n("Sync");
        else
            return i18n("Blacklisted");
    }
    case Qt::ToolTipRole: {
        if (section == 1)
            return i18n("Name of a notifying application.");
        else if (section == 0)
            return i18n("Synchronize notifications of an application?");
        else
            return i18n(
                "Regular expression defining which notifications should not be sent.\nThis pattern is applied to the summary and, if selected above, the body "
                "of notifications.");
    }
    }
    return QVariant();
}
Returns a QVariant object that represents the data for the given section and role.
int NotifyingApplicationModel::columnCount(const QModelIndex &) const
{
    return 3;
}
Returns the number of columns in the model index.
int NotifyingApplicationModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        // Return size 0 if we are a child because this is not a tree
        return 0;
    }
    return m_apps.size();
}
Returns the row count for the given parent index. If the parent is not a tree, it returns 0.