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 "messagingwidget.h"
0011 #include "../../widgets/contacteditorcombobox.h"
0012 #include "editor/widgets/preferredlineeditwidget.h"
0013 
0014 #include <KContacts/Impp>
0015 
0016 #include <KLocalizedString>
0017 #include <QHBoxLayout>
0018 #include <QToolButton>
0019 #include <QUrl>
0020 
0021 using namespace Akonadi;
0022 MessagingWidget::MessagingWidget(QWidget *parent)
0023     : QWidget(parent)
0024     , mMessagingEdit(new PreferredLineEditWidget(this))
0025     , mAddButton(new QToolButton(this))
0026     , mRemoveButton(new QToolButton(this))
0027     , mProtocolCombo(new Akonadi::ContactEditorComboBox(this))
0028 {
0029     auto layout = new QHBoxLayout(this);
0030     layout->setSpacing(0);
0031     layout->setContentsMargins({});
0032 
0033     mMessagingEdit->setTrapReturnKey(true);
0034     mMessagingEdit->setPlaceholderText(i18n("Add an identifier"));
0035     mMessagingEdit->setObjectName(QLatin1StringView("messaginglineedit"));
0036     connect(mMessagingEdit, &PreferredLineEditWidget::preferredChanged, this, &MessagingWidget::slotPreferredChanged);
0037     layout->addWidget(mMessagingEdit);
0038 
0039     mProtocolCombo->setObjectName(QLatin1StringView("protocol"));
0040     mProtocolCombo->addItem(i18nc("@item:inlistbox select from a list of IM protocols", "Select..."));
0041     layout->addWidget(mProtocolCombo);
0042 
0043     const auto protocols = KContacts::Impp::serviceTypes();
0044     for (const QString &protocol : protocols) {
0045         mProtocolCombo->addItem(QIcon::fromTheme(KContacts::Impp::serviceIcon(protocol)), KContacts::Impp::serviceLabel(protocol), protocol);
0046     }
0047 
0048     mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0049     mAddButton->setObjectName(QLatin1StringView("addbutton"));
0050     mAddButton->setToolTip(i18n("Add an IM"));
0051     connect(mAddButton, &QToolButton::clicked, this, &MessagingWidget::slotAddMessaging);
0052     layout->addWidget(mAddButton);
0053 
0054     mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0055     mRemoveButton->setObjectName(QLatin1StringView("removebutton"));
0056     mRemoveButton->setToolTip(i18n("Remove IM"));
0057     connect(mRemoveButton, &QToolButton::clicked, this, &MessagingWidget::slotRemoveMessaging);
0058     layout->addWidget(mRemoveButton);
0059 }
0060 
0061 MessagingWidget::~MessagingWidget() = default;
0062 
0063 void MessagingWidget::slotPreferredChanged()
0064 {
0065     Q_EMIT preferredChanged(this);
0066 }
0067 
0068 void MessagingWidget::setIMAddress(const KContacts::Impp &address)
0069 {
0070     const int idx = mProtocolCombo->findData(address.serviceType());
0071     if (idx >= 0) {
0072         mProtocolCombo->setCurrentIndex(idx);
0073     }
0074     mMessagingEdit->setText(address.address().path());
0075     mMessagingEdit->setPreferred(address.isPreferred());
0076 }
0077 
0078 void MessagingWidget::setPreferred(bool b)
0079 {
0080     mMessagingEdit->setPreferred(b);
0081 }
0082 
0083 KContacts::Impp MessagingWidget::imAddress() const
0084 {
0085     QUrl uri;
0086     uri.setScheme(mProtocolCombo->currentData().toString());
0087     uri.setPath(mMessagingEdit->text().trimmed());
0088     KContacts::Impp addr;
0089     addr.setAddress(uri);
0090     addr.setPreferred(mMessagingEdit->preferred());
0091     return addr;
0092 }
0093 
0094 void MessagingWidget::updateAddRemoveButton(bool addButtonEnabled)
0095 {
0096     mAddButton->setEnabled(addButtonEnabled);
0097 }
0098 
0099 void MessagingWidget::slotAddMessaging()
0100 {
0101     Q_EMIT addWidget(this);
0102 }
0103 
0104 void MessagingWidget::slotRemoveMessaging()
0105 {
0106     Q_EMIT removeWidget(this);
0107 }
0108 
0109 void MessagingWidget::clearWidget()
0110 {
0111     mProtocolCombo->setCurrentIndex(0);
0112     mMessagingEdit->clear();
0113 }
0114 
0115 void MessagingWidget::setReadOnly(bool readOnly)
0116 {
0117     mMessagingEdit->setReadOnly(readOnly);
0118     mAddButton->setEnabled(!readOnly);
0119     mRemoveButton->setEnabled(!readOnly);
0120     mProtocolCombo->setEnabled(!readOnly);
0121 }
0122 
0123 #include "moc_messagingwidget.cpp"