Akonadi Contacts

mailwidgetlister.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
5 SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "mailwidgetlister.h"
11#include "mailwidget.h"
12
13#include <KContacts/Addressee>
14using namespace Akonadi;
15
16MailWidgetLister::MailWidgetLister(QWidget *parent)
17 : KWidgetLister(1, 8, parent)
18{
19 setNumberOfShownWidgetsTo(widgetsMinimum());
20 updateAddRemoveButton();
21}
22
23MailWidgetLister::~MailWidgetLister() = default;
24
25void MailWidgetLister::loadContact(const KContacts::Addressee &contact)
26{
27 KContacts::Email::List mailList = contact.emailList();
28 if (mailList.isEmpty()) {
29 setNumberOfShownWidgetsTo(1);
30 } else {
31 setNumberOfShownWidgetsTo(mailList.count());
32 const QList<QWidget *> widgetList = widgets();
33 auto wIt = widgetList.constBegin();
34 auto wEnd = widgetList.constEnd();
35 for (int i = 0; wIt != wEnd; ++wIt, ++i) {
36 auto w = qobject_cast<MailWidget *>(*wIt);
37 w->setMail(mailList.at(i));
38 }
39 }
40}
41
42void MailWidgetLister::storeContact(KContacts::Addressee &contact) const
43{
44 const QList<QWidget *> widgetList = widgets();
45 KContacts::Email::List emailList;
46 emailList.reserve(widgetList.count());
47 for (QWidget *widget : widgetList) {
48 auto w = qobject_cast<MailWidget *>(widget);
49 KContacts::Email newEmail = w->email();
50 if (newEmail.isValid()) {
51 emailList << newEmail;
52 }
53 }
54 contact.setEmailList(emailList);
55}
56
57void MailWidgetLister::setReadOnly(bool readOnly)
58{
59 const QList<QWidget *> widgetList = widgets();
60 for (QWidget *widget : widgetList) {
61 auto w = qobject_cast<MailWidget *>(widget);
62 w->setReadOnly(readOnly);
63 }
64}
65
66QWidget *MailWidgetLister::createWidget(QWidget *parent)
67{
68 auto w = new MailWidget(parent);
69 reconnectWidget(w);
70 return w;
71}
72
73void MailWidgetLister::reconnectWidget(MailWidget *w)
74{
75 connect(w, &MailWidget::addWidget, this, &MailWidgetLister::slotAddWidget, Qt::UniqueConnection);
76 connect(w, &MailWidget::removeWidget, this, &MailWidgetLister::slotRemoveWidget, Qt::UniqueConnection);
77 connect(w, &MailWidget::preferredChanged, this, &MailWidgetLister::slotPreferredChanged, Qt::UniqueConnection);
78}
79
80void MailWidgetLister::slotAddWidget(MailWidget *w)
81{
82 addWidgetAfterThisWidget(w);
83 updateAddRemoveButton();
84}
85
86void MailWidgetLister::slotRemoveWidget(MailWidget *w)
87{
88 if (widgets().count() == 1) {
89 w->clearWidget();
90 } else {
91 removeWidget(w);
92 updateAddRemoveButton();
93 }
94}
95
96void MailWidgetLister::slotPreferredChanged(MailWidget *w)
97{
98 const QList<QWidget *> widgetList = widgets();
99 for (QWidget *widget : widgetList) {
100 if (widget != w) {
101 (static_cast<MailWidget *>(widget))->setPreferred(false);
102 }
103 }
104}
105
106void MailWidgetLister::updateAddRemoveButton()
107{
108 const QList<QWidget *> widgetList = widgets();
109 const int numberOfWidget(widgetList.count());
110 bool addButtonEnabled = false;
111 if (numberOfWidget <= widgetsMinimum()) {
112 addButtonEnabled = true;
113 } else if (numberOfWidget >= widgetsMaximum()) {
114 addButtonEnabled = false;
115 } else {
116 addButtonEnabled = true;
117 }
118
119 for (QWidget *widget : widgetList) {
120 auto w = qobject_cast<MailWidget *>(widget);
121 w->updateAddRemoveButton(addButtonEnabled);
122 }
123}
124
125#include "moc_mailwidgetlister.cpp"
A widget for editing the display name of a contact.
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
UniqueConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.