File indexing completed on 2024-05-12 16:25:05

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #include "mediamessagefiltermodel.h"
0005 #include "models/messageeventmodel.h"
0006 #include <Quotient/room.h>
0007 
0008 MediaMessageFilterModel::MediaMessageFilterModel(QObject *parent)
0009     : QSortFilterProxyModel(parent)
0010 {
0011 }
0012 
0013 bool MediaMessageFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0014 {
0015     const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
0016 
0017     if (index.data(MessageEventModel::DelegateTypeRole).toInt() == MessageEventModel::DelegateType::Image
0018         || index.data(MessageEventModel::DelegateTypeRole).toInt() == MessageEventModel::DelegateType::Video) {
0019         return true;
0020     }
0021     return false;
0022 }
0023 
0024 QVariant MediaMessageFilterModel::data(const QModelIndex &index, int role) const
0025 {
0026     if (role == SourceRole) {
0027         if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == MessageEventModel::DelegateType::Image) {
0028             return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()["source"].toUrl();
0029         } else if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == MessageEventModel::DelegateType::Video) {
0030             auto progressInfo = mapToSource(index).data(MessageEventModel::ProgressInfoRole).value<Quotient::FileTransferInfo>();
0031 
0032             if (progressInfo.completed()) {
0033                 return mapToSource(index).data(MessageEventModel::ProgressInfoRole).value<Quotient::FileTransferInfo>().localPath;
0034             } else {
0035                 return QUrl();
0036             }
0037         } else {
0038             return QUrl();
0039         }
0040     }
0041     if (role == TempSourceRole) {
0042         return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()["tempInfo"].toMap()["source"].toUrl();
0043     }
0044     if (role == TypeRole) {
0045         if (mapToSource(index).data(MessageEventModel::DelegateTypeRole).toInt() == MessageEventModel::DelegateType::Image) {
0046             return 0;
0047         } else {
0048             return 1;
0049         }
0050     }
0051     if (role == CaptionRole) {
0052         return mapToSource(index).data(Qt::DisplayRole);
0053     }
0054     if (role == SourceWidthRole) {
0055         return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()["width"].toFloat();
0056     }
0057     if (role == SourceHeightRole) {
0058         return mapToSource(index).data(MessageEventModel::MediaInfoRole).toMap()["height"].toFloat();
0059     }
0060 
0061     return sourceModel()->data(mapToSource(index), role);
0062 }
0063 
0064 QHash<int, QByteArray> MediaMessageFilterModel::roleNames() const
0065 {
0066     auto roles = sourceModel()->roleNames();
0067     roles[SourceRole] = "source";
0068     roles[TempSourceRole] = "tempSource";
0069     roles[TypeRole] = "type";
0070     roles[CaptionRole] = "caption";
0071     roles[SourceWidthRole] = "sourceWidth";
0072     roles[SourceHeightRole] = "sourceHeight";
0073     return roles;
0074 }
0075 
0076 int MediaMessageFilterModel::getRowForSourceItem(int sourceRow) const
0077 {
0078     return mapFromSource(sourceModel()->index(sourceRow, 0)).row();
0079 }
0080 
0081 #include "moc_mediamessagefiltermodel.cpp"