File indexing completed on 2024-11-24 04:50:44
0001 // SPDX-FileCopyrightText: 2023 Aakarsh MJ <mj.akarsh@gmail.com> 0002 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0003 0004 #include "mailheadermodel.h" 0005 #include <QList> 0006 0007 MailHeaderModel::MailHeaderModel(QObject *parent) 0008 : QAbstractListModel(parent) 0009 { 0010 HeaderItem newItem{Header::To, QString{}}; 0011 m_headers.append(newItem); 0012 } 0013 0014 QVariant MailHeaderModel::data(const QModelIndex &index, int role) const 0015 { 0016 Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)); 0017 0018 const auto &item = m_headers[index.row()]; 0019 switch (role) { 0020 case Qt::DisplayRole: 0021 case NameRole: 0022 return item.header; 0023 case ValueRole: 0024 return item.value; 0025 } 0026 return {}; 0027 } 0028 0029 int MailHeaderModel::rowCount(const QModelIndex &parent) const 0030 { 0031 if (parent.isValid()) 0032 return 0; 0033 return m_headers.size(); 0034 } 0035 0036 void MailHeaderModel::updateModel(const int row, const QString &value) 0037 { 0038 Q_ASSERT(row >= 0 && row < m_headers.count()); 0039 0040 const auto text = value.trimmed(); 0041 if (text.length() == 0 && row > 0 && row != rowCount() - 1) { 0042 // Delete row if it's empty and not the first nor the last one 0043 beginRemoveRows({}, row, row); 0044 m_headers.removeAt(row); 0045 endRemoveRows(); 0046 return; 0047 } 0048 0049 auto &header = m_headers[row]; 0050 header.value = text; 0051 Q_EMIT dataChanged(index(row, 0), index(row, 0), {ValueRole}); 0052 0053 if (row == rowCount() - 1) { 0054 beginInsertRows({}, row + 1, row + 1); 0055 m_headers.append(HeaderItem{Header::CC, QString{}}); 0056 endInsertRows(); 0057 } 0058 } 0059 0060 void MailHeaderModel::updateHeaderType(const int row, const Header headerName) 0061 { 0062 Q_ASSERT(row >= 0 && row < m_headers.count()); 0063 0064 auto &header = m_headers[row]; 0065 header.header = headerName; 0066 Q_EMIT dataChanged(index(row, 0), index(row, 0), {NameRole}); 0067 } 0068 0069 #include "moc_mailheadermodel.cpp"