Skip to content
Snippets Groups Projects
usermodel.cpp 3.47 KiB
Newer Older
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine 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 3 of the License, or                        *
 * (at your option) any later version.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine 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.                                             *
 *                                                                                          *
 * You should have received a copy of the GNU General Public License                        *
 * along with RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/



Armin Felder's avatar
Armin Felder committed
#include "usermodel.h"

UserModel::UserModel( QObject *parent ): QAbstractListModel( parent )
{

}

int UserModel::rowCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent );
Dennis Beier's avatar
Dennis Beier committed
    int count = userMap.values( current ).toSet().count();
Armin Felder's avatar
Armin Felder committed
    qDebug() << "count  of current channel users" << count;
    return count;
}

QVariant UserModel::data( const QModelIndex &index, int role ) const
{
    auto userList = userMap.values( current );

    if ( userList.count() < index.row() ) {
        return QVariant();
    }

    auto user = userList.at( index.row() );

    if ( role == UserRoles::UserName ) {
        return user->getUserName();
    };

    return QVariant();
}

bool UserModel::insertUser( const QString &channelId, const QSharedPointer<RocketChatUser> &user )
Armin Felder's avatar
Armin Felder committed
{

    if ( !user.isNull() && !duplicateCheck.contains( user->getUserId() ) ) {
Armin Felder's avatar
Armin Felder committed


Armin Felder's avatar
Armin Felder committed
        auto values =  userMap.values( channelId );
        int index = values.count();
Armin Felder's avatar
Armin Felder committed

Armin Felder's avatar
Armin Felder committed
        if ( channelId == current ) {
            beginInsertRows( QModelIndex(), index, index );
        }
Armin Felder's avatar
Armin Felder committed

Armin Felder's avatar
Armin Felder committed
        userMap.insert( channelId, user );
Armin Felder's avatar
Armin Felder committed

Armin Felder's avatar
Armin Felder committed
        if ( channelId == current ) {
            endInsertRows();
            emit countChanged();
        }
Armin Felder's avatar
Armin Felder committed
        return true;
    }
Armin Felder's avatar
Armin Felder committed
    //TODO: give some meainingfull return
    return true;
Armin Felder's avatar
Armin Felder committed
}

QString UserModel::getCurrent() const
{
    return current;
}

void UserModel::setCurrent( const QString &value )
{
    current = value;
}

void UserModel::onCurrentChannelChanged( const QString &newText )
{
    current = newText;
}

QHash<int, QByteArray> UserModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[UserName] = QByteArrayLiteral( "username" );
    roles[UserId] =  QByteArrayLiteral( "userId" );
Armin Felder's avatar
Armin Felder committed
    return roles;
}