File indexing completed on 2024-05-05 05:40:27

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "model/chatroomsplittermodel.h"
0021 #include "model/filterinstantmessagingmodel.h"
0022 #include "model/instantmessagingmodel.h"
0023 #include <set>
0024 
0025 namespace InstantMessaging
0026 {
0027 ChatroomSplitterModel::ChatroomSplitterModel(QObject* parent) : QAbstractListModel(parent) {}
0028 
0029 int ChatroomSplitterModel::rowCount(const QModelIndex& parent) const
0030 {
0031     if(parent.isValid())
0032         return 0;
0033 
0034     return static_cast<int>(m_filterModels.size());
0035 }
0036 
0037 QVariant ChatroomSplitterModel::data(const QModelIndex& index, int role) const
0038 {
0039     if(!index.isValid())
0040         return QVariant();
0041 
0042     std::set<int> acceptedRole({FilterModelRole, UuidRole});
0043     auto it= acceptedRole.find(role);
0044     if(it == acceptedRole.end())
0045         return QVariant();
0046 
0047     QVariant var;
0048     auto model= m_filterModels.at(static_cast<std::size_t>(index.row())).get();
0049     switch(role)
0050     {
0051     case FilterModelRole:
0052         var= QVariant::fromValue(model);
0053         break;
0054     case UuidRole:
0055         var= model->uuid();
0056         break;
0057     }
0058 
0059     return var;
0060 }
0061 
0062 QHash<int, QByteArray> ChatroomSplitterModel::roleNames() const
0063 {
0064     static QHash<int, QByteArray> roles({{FilterModelRole, "filterModel"}, {UuidRole, "uuid"}});
0065     return roles;
0066 }
0067 
0068 void ChatroomSplitterModel::addFilterModel(InstantMessaging::InstantMessagingModel* sourceModel, QStringList list,
0069                                            bool all)
0070 {
0071     std::for_each(m_filterModels.begin(), m_filterModels.end(),
0072                   [list](const std::unique_ptr<FilterInstantMessagingModel>& model)
0073                   {
0074                       if(model->all())
0075                           model->setFilterParameter(true, list);
0076                   });
0077 
0078     std::unique_ptr<InstantMessaging::FilterInstantMessagingModel> model(new FilterInstantMessagingModel());
0079     model->setFilterParameter(all, list);
0080 
0081     model->setSourceModel(sourceModel);
0082 
0083     auto size= static_cast<int>(m_filterModels.size());
0084 
0085     beginInsertRows(QModelIndex(), size, size);
0086     m_filterModels.push_back(std::move(model));
0087     endInsertRows();
0088 }
0089 
0090 } // namespace InstantMessaging