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, [email protected]
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 
21 using namespace ContactEditor;
22 MessagingWidget::MessagingWidget(QWidget *parent)
23  : QWidget(parent)
24 {
25  auto layout = new QHBoxLayout(this);
26  layout->setSpacing(0);
27  layout->setContentsMargins(0, 0, 0, 0);
28 
29  mMessagingEdit = new PreferredLineEditWidget(this);
30  mMessagingEdit->setTrapReturnKey(true);
31  mMessagingEdit->setPlaceholderText(i18n("Add an identifier"));
32  mMessagingEdit->setObjectName(QStringLiteral("messaginglineedit"));
33  connect(mMessagingEdit, &PreferredLineEditWidget::preferredChanged, this, &MessagingWidget::slotPreferredChanged);
34  layout->addWidget(mMessagingEdit);
35 
36  mProtocolCombo = new ContactEditor::ContactEditorComboBox(this);
37  mProtocolCombo->setObjectName(QStringLiteral("protocol"));
38  mProtocolCombo->addItem(i18nc("@item:inlistbox select from a list of IM protocols", "Select..."));
39  layout->addWidget(mProtocolCombo);
40 
41  const auto protocols = KContacts::Impp::serviceTypes();
42  for (const QString &protocol : protocols) {
43  mProtocolCombo->addItem(QIcon::fromTheme(KContacts::Impp::serviceIcon(protocol)), KContacts::Impp::serviceLabel(protocol), protocol);
44  }
45 
46  mAddButton = new QToolButton(this);
47  mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
48  mAddButton->setObjectName(QStringLiteral("addbutton"));
49  mAddButton->setToolTip(i18n("Add an IM"));
50  connect(mAddButton, &QToolButton::clicked, this, &MessagingWidget::slotAddMessaging);
51  layout->addWidget(mAddButton);
52 
53  mRemoveButton = new QToolButton(this);
54  mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
55  mRemoveButton->setObjectName(QStringLiteral("removebutton"));
56  mRemoveButton->setToolTip(i18n("Remove IM"));
57  connect(mRemoveButton, &QToolButton::clicked, this, &MessagingWidget::slotRemoveMessaging);
58  layout->addWidget(mRemoveButton);
59 }
60 
61 MessagingWidget::~MessagingWidget()
62 {
63 }
64 
65 void MessagingWidget::slotPreferredChanged()
66 {
67  Q_EMIT preferredChanged(this);
68 }
69 
70 void MessagingWidget::setIMAddress(const KContacts::Impp &address)
71 {
72  const int idx = mProtocolCombo->findData(address.serviceType());
73  if (idx >= 0) {
74  mProtocolCombo->setCurrentIndex(idx);
75  }
76  mMessagingEdit->setText(address.address().path());
77  mMessagingEdit->setPreferred(address.isPreferred());
78 }
79 
80 void MessagingWidget::setPreferred(bool b)
81 {
82  mMessagingEdit->setPreferred(b);
83 }
84 
85 KContacts::Impp MessagingWidget::imAddress() const
86 {
87  QUrl uri;
88  uri.setScheme(mProtocolCombo->currentData().toString());
89  uri.setPath(mMessagingEdit->text().trimmed());
90  KContacts::Impp addr;
91  addr.setAddress(uri);
92  addr.setPreferred(mMessagingEdit->preferred());
93  return addr;
94 }
95 
96 void MessagingWidget::updateAddRemoveButton(bool addButtonEnabled)
97 {
98  mAddButton->setEnabled(addButtonEnabled);
99 }
100 
101 void MessagingWidget::slotAddMessaging()
102 {
103  Q_EMIT addWidget(this);
104 }
105 
106 void MessagingWidget::slotRemoveMessaging()
107 {
108  Q_EMIT removeWidget(this);
109 }
110 
111 void MessagingWidget::clearWidget()
112 {
113  mProtocolCombo->setCurrentIndex(0);
114  mMessagingEdit->clear();
115 }
116 
117 void MessagingWidget::setReadOnly(bool readOnly)
118 {
119  mMessagingEdit->setReadOnly(readOnly);
120  mAddButton->setEnabled(!readOnly);
121  mRemoveButton->setEnabled(!readOnly);
122  mProtocolCombo->setEnabled(!readOnly);
123 }
bool isPreferred() const
QString serviceLabel() const
void setPath(const QString &path, QUrl::ParsingMode mode)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
static QVector< QString > serviceTypes()
void clicked(bool checked)
void setScheme(const QString &scheme)
void setPreferred(bool preferred)
QString serviceType() const
QString i18n(const char *text, const TYPE &arg...)
QIcon fromTheme(const QString &name)
QString serviceIcon() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.