<commit_before>/*
 * This file is part of telepathy-accounts-kcm
 *
 * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 * Copyright (C) 2011 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "parameter-edit-model.h"

#include "parameter-item.h"

#include <KApplication>
#include <KDebug>
#include <KLocale>
#include <KTitleWidget>

#include <QtGui/QValidator>

#include <TelepathyQt4/Profile>
#include <TelepathyQt4/ConnectionManager>

ParameterEditModel::ParameterEditModel(QObject *parent)
 : QAbstractListModel(parent)
{
    kDebug();
}

ParameterEditModel::~ParameterEditModel()
{
    kDebug();
}

int ParameterEditModel::rowCount(const QModelIndex &index) const
{
    // If the index is the root item, then return the row count.
    if (index == QModelIndex()) {
       return m_items.size();
    }

    // Otherwise, return 0 (as this is a list model, so all items
    // are children of the root item).
    return 0;
}

QVariant ParameterEditModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) {
        return QVariant();
    }

    // FIXME: This is a basic implementation just so I can see what's going
    // on while developing this code further. Needs expanding.
    QVariant data;

    switch(role)
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
    case ParameterEditModel::ValueRole:
        data = QVariant(m_items.at(index.row())->value());
        if(!data.isValid())
        {
            data = this->data(index, ParameterEditModel::DefaultValueRole);
        }
        break;
    case ParameterEditModel::DefaultValueRole:
        data = QVariant(m_items.at(index.row())->parameter().defaultValue());
        break;
    case ParameterEditModel::NameRole:
        data = QVariant(m_items.at(index.row())->name());
        break;
    case ParameterEditModel::LocalizedNameRole:
        data = QVariant(m_items.at(index.row())->localizedName());
        break;
    case ParameterEditModel::TypeRole:
        data = QVariant(m_items.at(index.row())->type());
        break;
    case ParameterEditModel::SecretRole:
        data = QVariant(m_items.at(index.row())->isSecret());
        break;
    case ParameterEditModel::RequiredRole:
        data = QVariant(m_items.at(index.row())->isRequired());
        break;
    case ParameterEditModel::RequiredForRegistrationRole:
        data = QVariant(m_items.at(index.row())->isRequiredForRegistration());
        break;
    case ParameterEditModel::ValidityRole:
        data = QVariant(m_items.at(index.row())->validity());
        break;
    default:
        break;
    }

    return data;
}

Qt::ItemFlags ParameterEditModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = 0;
    if (!index.isValid()) {
        return flags;
    }

    flags |= Qt::ItemIsSelectable;

    if (! m_items.at(index.row())->isMandatory()) {
        flags |= Qt::ItemIsEnabled;
    }
    return flags;
}

bool ParameterEditModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid()) {
        kDebug() << "Invalid item row accessed.";
        return false;
    }

    if (role == ParameterEditModel::ValueRole || role == Qt::EditRole) {
        //never edit items fixed in the profile.
        if(m_items.at(index.row())->isMandatory()) {
            return false;
        }

        m_items.at(index.row())->setValue(value);
        Q_EMIT dataChanged(index, index);
        return true;

    } else if (ParameterEditModel::ValidityRole) {
        if (value.toInt() == QValidator::Acceptable) {
            m_items.at(index.row())->setValidity(QValidator::Acceptable);
        } else if (value.toInt() == QValidator::Intermediate) {
            m_items.at(index.row())->setValidity(QValidator::Intermediate);
        } else {
            m_items.at(index.row())->setValidity(QValidator::Invalid);
        }

        Q_EMIT dataChanged(index, index);
        return true;

    } else {
        return false;
    }
}


QModelIndex ParameterEditModel::indexForParameter(const Tp::ProtocolParameter &parameter) const
{
    for(int i=0; i<m_items.size(); ++i) {
        if(m_items.at(i)->parameter() == parameter) {
            return createIndex(i,0);
        }
    }
    return QModelIndex();
}

QModelIndex ParameterEditModel::indexForParameter(const Tp::Profile::Parameter &parameter) const
{
    for(int i=0; i<m_items.size(); ++i) {
        ParameterItem *item = m_items.at(i);
        if(item->parameter().name() == parameter.name() && item->parameter().type() == parameter.type()) {
            return createIndex(i,0);
        }
    }
    return QModelIndex();
}

Tp::ProtocolParameter ParameterEditModel::parameter(const QString &parameterName) const
{
    foreach(ParameterItem* item, m_items) {
        if(item->parameter().name() == parameterName) {
            return item->parameter();
        }
    }

    return Tp::ProtocolParameter();
}


void ParameterEditModel::addItem(const Tp::ProtocolParameter &parameter, const Tp::Profile::Parameter &profileParameter, const QVariant &originalValue)
{
    // Check we are not creating duplicate items.
    foreach (const ParameterItem *item, m_items) {
        if(item->parameter() == parameter) {
            return;
        }
    }

    // Create a new ParameterItem and add it to the list.
    beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
    m_items.append(new ParameterItem(parameter, profileParameter, originalValue, this));
    endInsertRows();
}

void ParameterEditModel::addItems(const Tp::ProtocolParameterList &parameters, const Tp::Profile::ParameterList &profileParameters, const QVariantMap &parameterValues)
{
    foreach (const Tp::ProtocolParameter &parameter, parameters) {

        Tp::Profile::Parameter relevantProfileParameter;

        //try and find the correct profile parameter, if it can't be found leave it as empty.
        foreach(Tp::Profile::Parameter profileParameter, profileParameters) {
            if (profileParameter.name() == parameter.name()) {
                relevantProfileParameter = profileParameter;
                break;
            }
        }

        addItem(parameter, relevantProfileParameter, parameterValues[parameter.name()]);
    }
}<commit_msg>Added review comment<commit_after>/*
 * This file is part of telepathy-accounts-kcm
 *
 * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 * Copyright (C) 2011 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "parameter-edit-model.h"

#include "parameter-item.h"

#include <KApplication>
#include <KDebug>
#include <KLocale>
#include <KTitleWidget>

#include <QtGui/QValidator>

#include <TelepathyQt4/Profile>
#include <TelepathyQt4/ConnectionManager>

ParameterEditModel::ParameterEditModel(QObject *parent)
 : QAbstractListModel(parent)
{
    kDebug();
}

ParameterEditModel::~ParameterEditModel()
{
    kDebug();
}

int ParameterEditModel::rowCount(const QModelIndex &index) const
{
    // If the index is the root item, then return the row count.
    if (index == QModelIndex()) {
       return m_items.size();
    }

    // Otherwise, return 0 (as this is a list model, so all items
    // are children of the root item).
    return 0;
}

QVariant ParameterEditModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) {
        return QVariant();
    }

    // FIXME: This is a basic implementation just so I can see what's going
    // on while developing this code further. Needs expanding.
    QVariant data;

    switch(role)
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
    case ParameterEditModel::ValueRole:
        data = QVariant(m_items.at(index.row())->value());
        if(!data.isValid())
        {
            data = this->data(index, ParameterEditModel::DefaultValueRole);
        }
        break;
    case ParameterEditModel::DefaultValueRole:
        data = QVariant(m_items.at(index.row())->parameter().defaultValue());
        break;
    case ParameterEditModel::NameRole:
        data = QVariant(m_items.at(index.row())->name());
        break;
    case ParameterEditModel::LocalizedNameRole:
        data = QVariant(m_items.at(index.row())->localizedName());
        break;
    case ParameterEditModel::TypeRole:
        data = QVariant(m_items.at(index.row())->type());
        break;
    case ParameterEditModel::SecretRole:
        data = QVariant(m_items.at(index.row())->isSecret());
        break;
    case ParameterEditModel::RequiredRole:
        data = QVariant(m_items.at(index.row())->isRequired());
        break;
    case ParameterEditModel::RequiredForRegistrationRole:
        data = QVariant(m_items.at(index.row())->isRequiredForRegistration());
        break;
    case ParameterEditModel::ValidityRole:
        data = QVariant(m_items.at(index.row())->validity());
        break;
    default:
        break;
    }

    return data;
}

Qt::ItemFlags ParameterEditModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = 0;
    if (!index.isValid()) {
        return flags;
    }

    flags |= Qt::ItemIsSelectable;

    if (! m_items.at(index.row())->isMandatory()) {
        flags |= Qt::ItemIsEnabled;
    }
    return flags;
}

bool ParameterEditModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid()) {
        kDebug() << "Invalid item row accessed.";
        return false;
    }

    if (role == ParameterEditModel::ValueRole || role == Qt::EditRole) {
        //never edit items fixed in the profile.
        if(m_items.at(index.row())->isMandatory()) {
            return false;
        }

        m_items.at(index.row())->setValue(value);
        Q_EMIT dataChanged(index, index);
        return true;

    } else if (ParameterEditModel::ValidityRole) {
        if (value.toInt() == QValidator::Acceptable) {
            m_items.at(index.row())->setValidity(QValidator::Acceptable);
        } else if (value.toInt() == QValidator::Intermediate) {
            m_items.at(index.row())->setValidity(QValidator::Intermediate);
        } else {
            m_items.at(index.row())->setValidity(QValidator::Invalid);
        }

        Q_EMIT dataChanged(index, index);
        return true;

    } else {
        return false;
    }
}


QModelIndex ParameterEditModel::indexForParameter(const Tp::ProtocolParameter &parameter) const
{
    for(int i=0; i<m_items.size(); ++i) {
        if(m_items.at(i)->parameter() == parameter) {
            return createIndex(i,0);
        }
    }
    return QModelIndex();
}

QModelIndex ParameterEditModel::indexForParameter(const Tp::Profile::Parameter &parameter) const
{
    for(int i=0; i<m_items.size(); ++i) {
        ParameterItem *item = m_items.at(i);
        if(item->parameter().name() == parameter.name() && item->parameter().type() == parameter.type()) {
            return createIndex(i,0);
        }
    }
    return QModelIndex();
}

Tp::ProtocolParameter ParameterEditModel::parameter(const QString &parameterName) const
{
    foreach(ParameterItem* item, m_items) {
        if(item->parameter().name() == parameterName) {
            return item->parameter();
        }
    }

    return Tp::ProtocolParameter();
}


void ParameterEditModel::addItem(const Tp::ProtocolParameter &parameter, const Tp::Profile::Parameter &profileParameter, const QVariant &originalValue)
{
    // Check we are not creating duplicate items.
    foreach (const ParameterItem *item, m_items) {
        if(item->parameter() == parameter) {
            return;
        }
    }

    // Create a new ParameterItem and add it to the list.
    beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
    m_items.append(new ParameterItem(parameter, profileParameter, originalValue, this));
    endInsertRows();
}

void ParameterEditModel::addItems(const Tp::ProtocolParameterList &parameters, const Tp::Profile::ParameterList &profileParameters, const QVariantMap &parameterValues)
{
    foreach (const Tp::ProtocolParameter &parameter, parameters) {

        Tp::Profile::Parameter relevantProfileParameter;

        //try and find the correct profile parameter, if it can't be found leave it as empty.
        foreach (const Tp::Profile::Parameter &profileParameter, profileParameters) {
            if (profileParameter.name() == parameter.name()) {
                relevantProfileParameter = profileParameter;
                break;
            }
        }

        addItem(parameter, relevantProfileParameter, parameterValues[parameter.name()]);
    }
}
{'fraction_non_alphanumeric': 0.12415124008291044, 'fraction_numerical': 0.004860267314702308, 'mean_word_length': 3.177963571215288, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '365268', 'n_tokens_mistral': 4047, 'n_tokens_neox': 3960, 'n_words': 1032}