File indexing completed on 2024-11-24 04:50:42

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "imppmodel.h"
0005 #include <KContacts/Impp>
0006 #include <KLocalizedString>
0007 
0008 ImppModel::ImppModel(QObject *parent)
0009     : QAbstractListModel(parent)
0010 {
0011 }
0012 
0013 void ImppModel::loadContact(const KContacts::Addressee &contact)
0014 {
0015     beginResetModel();
0016     m_impps = contact.imppList();
0017     endResetModel();
0018 }
0019 
0020 void ImppModel::storeContact(KContacts::Addressee &contact) const
0021 {
0022     contact.setImppList(m_impps);
0023 }
0024 
0025 int ImppModel::rowCount(const QModelIndex &parent) const
0026 {
0027     Q_UNUSED(parent);
0028     return m_impps.count();
0029 }
0030 
0031 QVariant ImppModel::data(const QModelIndex &idx, int role) const
0032 {
0033     const auto impp = m_impps[idx.row()];
0034     switch (role) {
0035     case Qt::DisplayRole:
0036     case UrlRole:
0037         return impp.address();
0038     case TypeRole:
0039         return impp.serviceType();
0040     case TypeLabelRole:
0041         return impp.serviceLabel();
0042     case TypeIconRole:
0043         return impp.serviceIcon();
0044     }
0045 
0046     return {};
0047 }
0048 
0049 bool ImppModel::setData(const QModelIndex &index, const QVariant &value, int role)
0050 {
0051     auto impp = m_impps[index.row()];
0052     switch (role) {
0053     case UrlRole:
0054         impp.setAddress(QUrl(value.toString()));
0055         m_impps.replace(index.row(), impp);
0056         Q_EMIT changed(m_impps);
0057         return true;
0058     }
0059     return false;
0060 }
0061 
0062 QHash<int, QByteArray> ImppModel::roleNames() const
0063 {
0064     return {
0065         {UrlRole, QByteArrayLiteral("url")},
0066         {TypeRole, QByteArrayLiteral("type")},
0067         {TypeLabelRole, QByteArrayLiteral("typeLabel")},
0068         {TypeIconRole, QByteArrayLiteral("typeIcon")},
0069     };
0070 }
0071 
0072 void ImppModel::addImpp(const QUrl &impp)
0073 {
0074     beginInsertRows({}, m_impps.count(), m_impps.count());
0075     m_impps.append(KContacts::Impp(QUrl(impp)));
0076     endInsertRows();
0077     Q_EMIT changed(m_impps);
0078 }
0079 
0080 void ImppModel::deleteImpp(const int row)
0081 {
0082     if (!hasIndex(row, 0)) {
0083         return;
0084     }
0085     beginRemoveRows({}, row, row);
0086     m_impps.removeAt(row);
0087     endRemoveRows();
0088     Q_EMIT changed(m_impps);
0089 }
0090 
0091 #include "moc_imppmodel.cpp"