<commit_before>/*
* Copyright (C) 2008-2013 Communi authors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/

#include "usermodel.h"
#include "session.h"
#include <ircmessage.h>
#include <ircsender.h>
#include <irc.h>

UserModel::UserModel(QObject* parent) : QAbstractListModel(parent)
{
    d.session = qobject_cast<Session*>(parent);
}

UserModel::~UserModel()
{
}

Session* UserModel::session() const
{
    return d.session;
}

void UserModel::setSession(Session* session)
{
    d.session = session;
    clearUsers();
}

QString UserModel::channel() const
{
    return d.channel;
}

void UserModel::setChannel(const QString& channel)
{
    d.channel = channel.toLower();
}

QStringList UserModel::users() const
{
    return d.names;
}

bool UserModel::hasUser(const QString& user) const
{
    return d.names.contains(user, Qt::CaseInsensitive);
}

void UserModel::addUser(const QString& user)
{
    addUsers(QStringList() << user);
}

void UserModel::addUsers(const QStringList& users)
{
    QStringList unique;
    QSet<QString> nameSet = d.names.toSet();
    foreach (const QString& user, users) {
        QString name = d.session->unprefixedUser(user);
        if (!nameSet.contains(name))
            unique += user;
    }

    if (!unique.isEmpty()) {
        beginInsertRows(QModelIndex(), 0, unique.count() - 1);
        foreach (const QString& user, unique) {
            QString name = d.session->unprefixedUser(user);
            d.names.prepend(name);
            d.prefixes.insert(name, d.session->userPrefix(user));
        }
        endInsertRows();
    }
}

void UserModel::removeUser(const QString& user)
{
    QString name = d.session->unprefixedUser(user);
    int idx = d.names.indexOf(name);
    if (idx != -1) {
        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        d.prefixes.remove(name);
        endRemoveRows();
    }
}

void UserModel::clearUsers()
{
    if (!d.names.isEmpty()) {
        beginResetModel();
        d.names.clear();
        endResetModel();
    }
}

void UserModel::renameUser(const QString& from, const QString& to)
{
    int idx = d.names.indexOf(from);
    if (idx != -1) {
        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        QString prefix = d.prefixes.take(from);
        endRemoveRows();

        beginInsertRows(QModelIndex(), 0, 0);
        d.names.prepend(to);
        d.prefixes[to] = prefix;
        endInsertRows();
    }
}

void UserModel::promoteUser(const QString& user)
{
    QString name = d.session->unprefixedUser(user);
    int idx = d.names.indexOf(name);
    if (idx > 0) {
        // TODO: fix QCompleter to listen QAIM::rowsMoved()
        //beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), 0);
        //d.names.move(idx, 0);
        //endMoveRows();

        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        endRemoveRows();

        beginInsertRows(QModelIndex(), 0, 0);
        d.names.prepend(name);
        endInsertRows();
    }
}

void UserModel::setUserMode(const QString& user, const QString& mode)
{
    int idx = d.names.indexOf(user);
    if (idx != -1) {
        bool add = true;
        IrcSessionInfo info(d.session);
        QString updated = d.prefixes.value(user);<commit_msg>UserModel: remove unnecessary calls to Session::unprefixedUser()<commit_after>/*
* Copyright (C) 2008-2013 Communi authors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/

#include "usermodel.h"
#include "session.h"
#include <ircmessage.h>
#include <ircsender.h>
#include <irc.h>

UserModel::UserModel(QObject* parent) : QAbstractListModel(parent)
{
    d.session = qobject_cast<Session*>(parent);
}

UserModel::~UserModel()
{
}

Session* UserModel::session() const
{
    return d.session;
}

void UserModel::setSession(Session* session)
{
    d.session = session;
    clearUsers();
}

QString UserModel::channel() const
{
    return d.channel;
}

void UserModel::setChannel(const QString& channel)
{
    d.channel = channel.toLower();
}

QStringList UserModel::users() const
{
    return d.names;
}

bool UserModel::hasUser(const QString& user) const
{
    return d.names.contains(user, Qt::CaseInsensitive);
}

void UserModel::addUser(const QString& user)
{
    addUsers(QStringList() << user);
}

void UserModel::addUsers(const QStringList& users)
{
    QStringList unique;
    QSet<QString> nameSet = d.names.toSet();
    foreach (const QString& user, users) {
        QString name = d.session->unprefixedUser(user);
        if (!nameSet.contains(name))
            unique += user;
    }

    if (!unique.isEmpty()) {
        beginInsertRows(QModelIndex(), 0, unique.count() - 1);
        foreach (const QString& user, unique) {
            QString name = d.session->unprefixedUser(user);
            d.names.prepend(name);
            d.prefixes.insert(name, d.session->userPrefix(user));
        }
        endInsertRows();
    }
}

void UserModel::removeUser(const QString& user)
{
    int idx = d.names.indexOf(user);
    if (idx != -1) {
        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        d.prefixes.remove(user);
        endRemoveRows();
    }
}

void UserModel::clearUsers()
{
    if (!d.names.isEmpty()) {
        beginResetModel();
        d.names.clear();
        endResetModel();
    }
}

void UserModel::renameUser(const QString& from, const QString& to)
{
    int idx = d.names.indexOf(from);
    if (idx != -1) {
        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        QString prefix = d.prefixes.take(from);
        endRemoveRows();

        beginInsertRows(QModelIndex(), 0, 0);
        d.names.prepend(to);
        d.prefixes[to] = prefix;
        endInsertRows();
    }
}

void UserModel::promoteUser(const QString& user)
{
    int idx = d.names.indexOf(user);
    if (idx > 0) {
        // TODO: fix QCompleter to listen QAIM::rowsMoved()
        //beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), 0);
        //d.names.move(idx, 0);
        //endMoveRows();

        beginRemoveRows(QModelIndex(), idx, idx);
        d.names.removeAt(idx);
        endRemoveRows();

        beginInsertRows(QModelIndex(), 0, 0);
        d.names.prepend(user);
        endInsertRows();
    }
}

void UserModel::setUserMode(const QString& user, const QString& mode)
{
    int idx = d.names.indexOf(user);
    if (idx != -1) {
        bool add = true;
        IrcSessionInfo info(d.session);
        QString updated = d.prefixes.value(user);
{'fraction_non_alphanumeric': 0.13462319036666215, 'fraction_numerical': 0.005682586930050061, 'mean_word_length': 3.057080131723381, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7427600', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2313, 'n_words': 584}