File indexing completed on 2024-11-24 04:39:30

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
0005     SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "mailwidget.h"
0011 #include "editor/generalinfoeditor/mail/mailtypecombobox.h"
0012 #include "editor/widgets/preferredlineeditwidget.h"
0013 #include <KLocalizedString>
0014 #include <QHBoxLayout>
0015 #include <QToolButton>
0016 
0017 using namespace Akonadi;
0018 MailWidget::MailWidget(QWidget *parent)
0019     : QWidget(parent)
0020     , mMailEdit(new PreferredLineEditWidget(this))
0021     , mMailType(new Akonadi::MailTypeCombobox(this))
0022     , mAddButton(new QToolButton(this))
0023     , mRemoveButton(new QToolButton(this))
0024 {
0025     auto layout = new QHBoxLayout(this);
0026     layout->setSpacing(0);
0027     layout->setContentsMargins({});
0028 
0029     mMailEdit->setTrapReturnKey(true);
0030     mMailEdit->setPlaceholderText(i18n("Add an Email Account"));
0031     mMailEdit->setObjectName(QLatin1StringView("mailedit"));
0032     layout->addWidget(mMailEdit);
0033     connect(mMailEdit, &PreferredLineEditWidget::preferredChanged, this, &MailWidget::slotPreferredChanged);
0034 
0035     mMailType->setObjectName(QLatin1StringView("mailtype"));
0036     layout->addWidget(mMailType);
0037 
0038     mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0039     mAddButton->setObjectName(QLatin1StringView("addbutton"));
0040     mAddButton->setToolTip(i18n("Add an Email"));
0041     connect(mAddButton, &QToolButton::clicked, this, &MailWidget::slotAddMail);
0042     layout->addWidget(mAddButton);
0043 
0044     mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0045     mRemoveButton->setObjectName(QLatin1StringView("removebutton"));
0046     mRemoveButton->setToolTip(i18n("Remove Email"));
0047     connect(mRemoveButton, &QToolButton::clicked, this, &MailWidget::slotRemoveMail);
0048     layout->addWidget(mRemoveButton);
0049 }
0050 
0051 MailWidget::~MailWidget() = default;
0052 
0053 void MailWidget::slotPreferredChanged()
0054 {
0055     Q_EMIT preferredChanged(this);
0056 }
0057 
0058 void MailWidget::clearWidget()
0059 {
0060     mMailEdit->clear();
0061     mEmail = KContacts::Email();
0062     mMailType->setCurrentIndex(0);
0063 }
0064 
0065 void MailWidget::updateAddRemoveButton(bool addButtonEnabled)
0066 {
0067     mAddButton->setEnabled(addButtonEnabled);
0068 }
0069 
0070 void MailWidget::setMail(const KContacts::Email &email)
0071 {
0072     mEmail = email;
0073     mMailEdit->setText(email.mail());
0074     mMailType->setCurrentIndex(mMailType->findData((int)email.type()));
0075     setPreferred(email.isPreferred());
0076 }
0077 
0078 KContacts::Email MailWidget::email()
0079 {
0080     mEmail.setEmail(mMailEdit->text());
0081     mEmail.setType(KContacts::Email::Type(mMailType->currentData().toInt()));
0082     mEmail.setPreferred(mMailEdit->preferred());
0083     return mEmail;
0084 }
0085 
0086 void MailWidget::setPreferred(bool b)
0087 {
0088     mMailEdit->setPreferred(b);
0089 }
0090 
0091 void MailWidget::setReadOnly(bool readOnly)
0092 {
0093     mMailEdit->setReadOnly(readOnly);
0094     mMailType->setEnabled(!readOnly);
0095     mAddButton->setEnabled(!readOnly);
0096     mRemoveButton->setEnabled(!readOnly);
0097 }
0098 
0099 void MailWidget::slotAddMail()
0100 {
0101     Q_EMIT addWidget(this);
0102 }
0103 
0104 void MailWidget::slotRemoveMail()
0105 {
0106     Q_EMIT removeWidget(this);
0107 }
0108 
0109 #include "moc_mailwidget.cpp"