File indexing completed on 2024-05-12 17:15:12

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "messagesortfilter.h"
0025 #include "eavesdroppermodel.h"
0026 
0027 #include "message.h"
0028 
0029 #include <QDebug>
0030 
0031 MessageSortFilter::MessageSortFilter(QObject *parent)
0032    : QSortFilterProxyModel(parent),
0033      m_onlyUnanswered(false)
0034 {
0035     setDynamicSortFilter(true);
0036 }
0037 
0038 bool MessageSortFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0039 {
0040     if (sourceParent.isValid() || (m_filterString.isEmpty() && !m_onlyUnanswered)) {
0041         return true;
0042     }
0043 
0044     EavesdropperModel *model = static_cast<EavesdropperModel *>(sourceModel());
0045     const std::vector<MessageRecord> &msgList = model->m_messages;
0046     const MessageRecord &msg = msgList[sourceRow];
0047 
0048     if (m_onlyUnanswered) {
0049         if (msg.message->type() == Message::MethodCallMessage) {
0050             if (!msg.isAwaitingReply()) {
0051                 return false;
0052             }
0053         } else if (msg.message->type() == Message::ErrorMessage) {
0054             if (!msg.isReplyToKnownCall()) {
0055                 return false;
0056             }
0057         } else {
0058             return false;
0059         }
0060     }
0061 
0062     if (m_filterString.isEmpty()) {
0063         return true;
0064     }
0065     return msg.conversationMethod(msgList).contains(m_filterString, Qt::CaseInsensitive) ||
0066            msg.niceSender(msgList).contains(m_filterString, Qt::CaseInsensitive) ||
0067            msg.niceDestination(msgList).contains(m_filterString, Qt::CaseInsensitive) ||
0068            QString::fromStdString(msg.message->interface()).contains(m_filterString, Qt::CaseInsensitive) ||
0069            QString::fromStdString(msg.message->path()).contains(m_filterString, Qt::CaseInsensitive);
0070 }
0071 
0072 bool MessageSortFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const
0073 {
0074     Q_ASSERT(!left.parent().isValid());
0075     Q_ASSERT(!right.parent().isValid());
0076     EavesdropperModel *model = static_cast<EavesdropperModel *>(sourceModel());
0077     const std::vector<MessageRecord> &msgList = model->m_messages;
0078     const MessageRecord &leftMsg = msgList[left.row()];
0079     const MessageRecord &rightMsg = msgList[right.row()];
0080 
0081     return leftMsg.conversationStartTime(msgList) < rightMsg.conversationStartTime(msgList);
0082 }
0083 
0084 void MessageSortFilter::setFilterString(const QString &s)
0085 {
0086     m_filterString = s;
0087     invalidateFilter();
0088 }
0089 
0090 void MessageSortFilter::setOnlyUnanswered(bool onlyUnanswered)
0091 {
0092     m_onlyUnanswered = onlyUnanswered;
0093     invalidateFilter();
0094 }