Commit 063819fc authored by Carl Schwan's avatar Carl Schwan 🚴
Browse files

Add email address model to contact detail

parent 0bc65846
Pipeline #174523 passed with stage
in 1 minute and 56 seconds
......@@ -52,6 +52,8 @@ set(kalendar_SRCS
contacts/addresseewrapper.h
contacts/addressmodel.cpp
contacts/addressmodel.h
contacts/emailmodel.h
contacts/emailmodel.cpp
contacts/globalcontactmodel.cpp
contacts/globalcontactmodel.h
contacts/contactmanager.h
......
......@@ -6,13 +6,14 @@
#include <KLocalizedString>
#include <QBitArray>
#include <QJSValue>
#include <akonadi/itemmonitor.h>
#include <kcontacts/addressee.h>
#include <Akonadi/ItemMonitor>
#include <KContacts/Addressee>
AddresseeWrapper::AddresseeWrapper(QObject *parent)
: QObject(parent)
, Akonadi::ItemMonitor()
, m_addressesModel(new AddressModel(this))
, m_emailModel(new EmailModel(this))
{
Akonadi::ItemFetchScope scope;
scope.fetchFullPayload();
......@@ -80,6 +81,7 @@ void AddresseeWrapper::setAddressee(const KContacts::Addressee &addressee)
{
m_addressee = addressee;
m_addressesModel->setAddresses(addressee.addresses());
m_emailModel->setEmails(addressee.emailList());
notifyDataChanged();
}
......@@ -141,3 +143,8 @@ QString AddresseeWrapper::preferredEmail() const
{
return m_addressee.preferredEmail();
}
EmailModel *AddresseeWrapper::emailModel() const
{
return m_emailModel;
}
......@@ -4,7 +4,6 @@
#pragma once
#include "addressmodel.h"
#include <Akonadi/CollectionIdentificationAttribute>
#include <Akonadi/Item>
#include <Akonadi/ItemFetchJob>
......@@ -12,7 +11,9 @@
#include <Akonadi/ItemMonitor>
#include <KContacts/Addressee>
#include <QObject>
#include <qdatetime.h>
#include "emailmodel.h"
#include "addressmodel.h"
/// This class is a QObject wrapper for a KContact::Adressee
class AddresseeWrapper : public QObject, public Akonadi::ItemMonitor
......@@ -27,6 +28,7 @@ class AddresseeWrapper : public QObject, public Akonadi::ItemMonitor
Q_PROPERTY(KContacts::PhoneNumber::List phoneNumbers READ phoneNumbers NOTIFY phoneNumbersChanged)
Q_PROPERTY(KContacts::Picture photo READ photo NOTIFY photoChanged)
Q_PROPERTY(AddressModel *addressesModel READ addressesModel CONSTANT)
Q_PROPERTY(EmailModel *emailModel READ emailModel CONSTANT)
public:
AddresseeWrapper(QObject *parent = nullptr);
......@@ -38,6 +40,7 @@ public:
KContacts::PhoneNumber::List phoneNumbers() const;
AddressModel *addressesModel() const;
EmailModel *emailModel() const;
qint64 collectionId() const;
void setCollectionId(qint64 collectionId);
......@@ -65,4 +68,5 @@ private:
KContacts::Addressee m_addressee;
qint64 m_collectionId = -1; // For when we want to edit, this is temporary
AddressModel *m_addressesModel;
EmailModel *m_emailModel;
};
// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "emailmodel.h"
#include <KLocalizedString>
EmailModel::EmailModel(QObject *parent)
: QAbstractListModel(parent)
{}
int EmailModel::rowCount(const QModelIndex &parent) const
{
return m_emails.count();
}
QVariant EmailModel::data(const QModelIndex &idx, int role) const
{
const auto email = m_emails[idx.row()];
switch (role) {
case Qt::DisplayRole:
return email.mail();
case TypeRole:
if (email.type() & KContacts::Email::Work) {
return i18n("Work:");
}
if (email.type() & KContacts::Email::Home) {
return i18n("Home:");
}
return i18n("Other:");
case DefaultRole:
return email.isPreferred();
}
return {};
}
QHash<int, QByteArray> EmailModel::roleNames() const
{
return {
{Qt::DisplayRole, QByteArrayLiteral("display")},
{TypeRole, QByteArrayLiteral("type")},
{DefaultRole, QByteArrayLiteral("default")},
};
}
void EmailModel::setEmails(const KContacts::Email::List &emails)
{
beginResetModel();
m_emails = emails;
endResetModel();
}
// SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.1-or-later
#pragma once
#include <QAbstractListModel>
#include <KContacts/Email>
class EmailModel : public QAbstractListModel
{
Q_OBJECT
public:
enum ExtraRole {
TypeRole = Qt::UserRole + 1,
DefaultRole,
};
EmailModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &idx, int role) const override;
QHash<int, QByteArray> roleNames() const override;
void setEmails(const KContacts::Email::List &emails);
private:
KContacts::Email::List m_emails;
};
......@@ -174,6 +174,16 @@ Kirigami.ScrollablePage {
Kirigami.FormData.labelAlignment: Qt.AlignTop
}
}
Repeater {
model: addressee.emailModel
Controls.Label {
visible: text !== ""
text: model.display
Kirigami.FormData.label: model.type
Kirigami.FormData.labelAlignment: Qt.AlignTop
}
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment