File indexing completed on 2024-02-25 16:44:40

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "switchchannelhistorymodel.h"
0008 
0009 SwitchChannelHistoryModel::SwitchChannelHistoryModel(QObject *parent)
0010     : QAbstractListModel{parent}
0011 {
0012 }
0013 
0014 SwitchChannelHistoryModel::~SwitchChannelHistoryModel() = default;
0015 
0016 int SwitchChannelHistoryModel::rowCount(const QModelIndex &parent) const
0017 {
0018     if (parent.isValid()) {
0019         return 0; // flat model
0020     }
0021     return mSwichChannelsInfo.count();
0022 }
0023 
0024 QVariant SwitchChannelHistoryModel::data(const QModelIndex &index, int role) const
0025 {
0026     if (index.row() < 0 || index.row() >= mSwichChannelsInfo.count()) {
0027         return {};
0028     }
0029     const SwitchChannelInfo info = mSwichChannelsInfo.at(index.row());
0030     switch (role) {
0031     case SwitchChannelHistoryRoles::Name:
0032     case Qt::DisplayRole:
0033         return info.mName;
0034     case SwitchChannelHistoryRoles::Identifier:
0035         return info.mIdentifier;
0036     case SwitchChannelHistoryRoles::AvatarInfo:
0037         return QVariant::fromValue(info.mAvatarInfo);
0038     }
0039     return {};
0040 }
0041 
0042 void SwitchChannelHistoryModel::addHistory(const SwitchChannelInfo &info)
0043 {
0044     if (!mSwichChannelsInfo.isEmpty()) {
0045         if (mSwichChannelsInfo.at(mSwichChannelsInfo.count() - 1) == info) {
0046             return;
0047         }
0048     }
0049     if (mSwichChannelsInfo.count() > 10) {
0050         mSwichChannelsInfo.takeFirst();
0051     }
0052     mSwichChannelsInfo.removeAll(info);
0053     mSwichChannelsInfo.prepend(info);
0054     Q_EMIT dataChanged(createIndex(0, 0), createIndex(mSwichChannelsInfo.size() - 1, 1), {});
0055 }
0056 
0057 bool SwitchChannelHistoryModel::SwitchChannelInfo::operator==(const SwitchChannelInfo &other) const
0058 {
0059     return other.mIdentifier == mIdentifier && other.mName == mName && other.mAvatarInfo == mAvatarInfo;
0060 }
0061 
0062 #include "moc_switchchannelhistorymodel.cpp"