Akonadi Contacts

messagingwidget.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, [email protected]
5  Copyright (C) 2016-2020 Laurent Montel <montel.org>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "messagingwidget.h"
24 #include "../../widgets/contacteditorcombobox.h"
25 #include <editor/widgets/preferredlineeditwidget.h>
26 
27 #include <KContacts/Impp>
28 
29 #include <KLocalizedString>
30 #include <QHBoxLayout>
31 #include <QToolButton>
32 #include <QUrl>
33 
34 using namespace ContactEditor;
35 MessagingWidget::MessagingWidget(QWidget *parent)
36  : QWidget(parent)
37 {
38  QHBoxLayout *layout = new QHBoxLayout(this);
39  layout->setSpacing(0);
40  layout->setContentsMargins(0, 0, 0, 0);
41 
42  mMessagingEdit = new PreferredLineEditWidget(this);
43  mMessagingEdit->setTrapReturnKey(true);
44  mMessagingEdit->setPlaceholderText(i18n("Add an identifier"));
45  mMessagingEdit->setObjectName(QStringLiteral("messaginglineedit"));
46  connect(mMessagingEdit, &PreferredLineEditWidget::preferredChanged, this, &MessagingWidget::slotPreferredChanged);
47  layout->addWidget(mMessagingEdit);
48 
49  mProtocolCombo = new ContactEditor::ContactEditorComboBox(this);
50  mProtocolCombo->setObjectName(QStringLiteral("protocol"));
51  mProtocolCombo->addItem(i18nc("@item:inlistbox select from a list of IM protocols",
52  "Select..."));
53  layout->addWidget(mProtocolCombo);
54 
55  const auto protocols = KContacts::Impp::serviceTypes();
56  for (const QString &protocol : protocols) {
57  mProtocolCombo->addItem(QIcon::fromTheme(KContacts::Impp::serviceIcon(protocol)),
59  protocol);
60  }
61 
62  mAddButton = new QToolButton(this);
63  mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
64  mAddButton->setObjectName(QStringLiteral("addbutton"));
65  mAddButton->setToolTip(i18n("Add an IM"));
66  connect(mAddButton, &QToolButton::clicked, this, &MessagingWidget::slotAddMessaging);
67  layout->addWidget(mAddButton);
68 
69  mRemoveButton = new QToolButton(this);
70  mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
71  mRemoveButton->setObjectName(QStringLiteral("removebutton"));
72  mRemoveButton->setToolTip(i18n("Remove IM"));
73  connect(mRemoveButton, &QToolButton::clicked, this, &MessagingWidget::slotRemoveMessaging);
74  layout->addWidget(mRemoveButton);
75 }
76 
77 MessagingWidget::~MessagingWidget()
78 {
79 }
80 
81 void MessagingWidget::slotPreferredChanged()
82 {
83  Q_EMIT preferredChanged(this);
84 }
85 
86 void MessagingWidget::setIMAddress(const KContacts::Impp &address)
87 {
88  const int idx = mProtocolCombo->findData(address.serviceType());
89  if (idx >= 0) {
90  mProtocolCombo->setCurrentIndex(idx);
91  }
92  mMessagingEdit->setText(address.address().path());
93  mMessagingEdit->setPreferred(address.isPreferred());
94 }
95 
96 void MessagingWidget::setPreferred(bool b)
97 {
98  mMessagingEdit->setPreferred(b);
99 }
100 
101 KContacts::Impp MessagingWidget::imAddress() const
102 {
103  QUrl uri;
104  uri.setScheme(mProtocolCombo->currentData().toString());
105  uri.setPath(mMessagingEdit->text().trimmed());
106  KContacts::Impp addr;
107  addr.setAddress(uri);
108  addr.setPreferred(mMessagingEdit->preferred());
109  return addr;
110 }
111 
112 void MessagingWidget::updateAddRemoveButton(bool addButtonEnabled)
113 {
114  mAddButton->setEnabled(addButtonEnabled);
115 }
116 
117 void MessagingWidget::slotAddMessaging()
118 {
119  Q_EMIT addWidget(this);
120 }
121 
122 void MessagingWidget::slotRemoveMessaging()
123 {
124  Q_EMIT removeWidget(this);
125 }
126 
127 void MessagingWidget::clearWidget()
128 {
129  mProtocolCombo->setCurrentIndex(0);
130  mMessagingEdit->clear();
131 }
132 
133 void MessagingWidget::setReadOnly(bool readOnly)
134 {
135  mMessagingEdit->setReadOnly(readOnly);
136  mAddButton->setEnabled(!readOnly);
137  mRemoveButton->setEnabled(!readOnly);
138  mProtocolCombo->setEnabled(!readOnly);
139 }
QLayout * layout() const const
void setContentsMargins(int left, int top, int right, int bottom)
bool isPreferred() const
QString serviceLabel() const
void setPath(const QString &path, QUrl::ParsingMode mode)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)
Q_EMITQ_EMIT
QString serviceIcon() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 4 2020 23:08:30 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.