Akonadi Contacts

messagingwidget.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 "messagingwidget.h"
11#include "../../widgets/contacteditorcombobox.h"
12#include "editor/widgets/preferredlineeditwidget.h"
13
14#include <KContacts/Impp>
15
16#include <KLocalizedString>
17#include <QHBoxLayout>
18#include <QToolButton>
19#include <QUrl>
20
21using namespace Akonadi;
22MessagingWidget::MessagingWidget(QWidget *parent)
23 : QWidget(parent)
24 , mMessagingEdit(new PreferredLineEditWidget(this))
25 , mAddButton(new QToolButton(this))
26 , mRemoveButton(new QToolButton(this))
27 , mProtocolCombo(new Akonadi::ContactEditorComboBox(this))
28{
29 auto layout = new QHBoxLayout(this);
30 layout->setSpacing(0);
31 layout->setContentsMargins({});
32
33 mMessagingEdit->setTrapReturnKey(true);
34 mMessagingEdit->setPlaceholderText(i18nc("@info:placeholder", "Add an identifier"));
35 mMessagingEdit->setObjectName(QLatin1StringView("messaginglineedit"));
36 connect(mMessagingEdit, &PreferredLineEditWidget::preferredChanged, this, &MessagingWidget::slotPreferredChanged);
37 layout->addWidget(mMessagingEdit);
38
39 mProtocolCombo->setObjectName(QLatin1StringView("protocol"));
40 mProtocolCombo->addItem(i18nc("@item:inlistbox select from a list of IM protocols", "Select..."));
41 layout->addWidget(mProtocolCombo);
42
43 const auto protocols = KContacts::Impp::serviceTypes();
44 for (const QString &protocol : protocols) {
45 mProtocolCombo->addItem(QIcon::fromTheme(KContacts::Impp::serviceIcon(protocol)), KContacts::Impp::serviceLabel(protocol), protocol);
46 }
47
48 mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
49 mAddButton->setObjectName(QLatin1StringView("addbutton"));
50 mAddButton->setToolTip(i18nc("@info:tooltip", "Add an IM"));
51 connect(mAddButton, &QToolButton::clicked, this, &MessagingWidget::slotAddMessaging);
52 layout->addWidget(mAddButton);
53
54 mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
55 mRemoveButton->setObjectName(QLatin1StringView("removebutton"));
56 mRemoveButton->setToolTip(i18nc("@info:tooltip", "Remove IM"));
57 connect(mRemoveButton, &QToolButton::clicked, this, &MessagingWidget::slotRemoveMessaging);
58 layout->addWidget(mRemoveButton);
59}
60
61MessagingWidget::~MessagingWidget() = default;
62
63void MessagingWidget::slotPreferredChanged()
64{
65 Q_EMIT preferredChanged(this);
66}
67
68void MessagingWidget::setIMAddress(const KContacts::Impp &address)
69{
70 const int idx = mProtocolCombo->findData(address.serviceType());
71 if (idx >= 0) {
72 mProtocolCombo->setCurrentIndex(idx);
73 }
74 mMessagingEdit->setText(address.address().path());
75 mMessagingEdit->setPreferred(address.isPreferred());
76}
77
78void MessagingWidget::setPreferred(bool b)
79{
80 mMessagingEdit->setPreferred(b);
81}
82
83KContacts::Impp MessagingWidget::imAddress() const
84{
85 QUrl uri;
86 uri.setScheme(mProtocolCombo->currentData().toString());
87 uri.setPath(mMessagingEdit->text().trimmed());
88 KContacts::Impp addr;
89 addr.setAddress(uri);
90 addr.setPreferred(mMessagingEdit->preferred());
91 return addr;
92}
93
94void MessagingWidget::updateAddRemoveButton(bool addButtonEnabled)
95{
96 mAddButton->setEnabled(addButtonEnabled);
97}
98
99void MessagingWidget::slotAddMessaging()
100{
101 Q_EMIT addWidget(this);
102}
103
104void MessagingWidget::slotRemoveMessaging()
105{
106 Q_EMIT removeWidget(this);
107}
108
109void MessagingWidget::clearWidget()
110{
111 mProtocolCombo->setCurrentIndex(0);
112 mMessagingEdit->clear();
113}
114
115void MessagingWidget::setReadOnly(bool readOnly)
116{
117 mMessagingEdit->setReadOnly(readOnly);
118 mAddButton->setEnabled(!readOnly);
119 mRemoveButton->setEnabled(!readOnly);
120 mProtocolCombo->setEnabled(!readOnly);
121}
122
123#include "moc_messagingwidget.cpp"
QString serviceLabel() const
QString serviceIcon() const
static QList< QString > serviceTypes()
void setPreferred(bool preferred)
virtual void setReadOnly(bool)
virtual void setText(const QString &)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
PostalAddress address(const QVariant &location)
void clicked(bool checked)
void setCurrentIndex(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QIcon fromTheme(const QString &name)
void clear()
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setPath(const QString &path, ParsingMode mode)
void setScheme(const QString &scheme)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:36:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.