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

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 "webwidget.h"
0011 #include "editor/widgets/preferredlineeditwidget.h"
0012 #include "webselecttypecombobox.h"
0013 #include <KLocalizedString>
0014 #include <QHBoxLayout>
0015 #include <QToolButton>
0016 #include <QUrl>
0017 using namespace Akonadi;
0018 
0019 WebWidget::WebWidget(QWidget *parent)
0020     : QWidget(parent)
0021     , mWebSiteEdit(new PreferredLineEditWidget(this))
0022     , mAddButton(new QToolButton(this))
0023     , mRemoveButton(new QToolButton(this))
0024     , mWebType(new Akonadi::WebSelectTypeCombobox(this))
0025 {
0026     auto layout = new QHBoxLayout(this);
0027     layout->setSpacing(0);
0028     layout->setContentsMargins({});
0029 
0030     mWebSiteEdit->setTrapReturnKey(true);
0031     mWebSiteEdit->setPlaceholderText(i18n("Add a Web Site"));
0032     mWebSiteEdit->setObjectName(QLatin1StringView("website"));
0033     layout->addWidget(mWebSiteEdit);
0034     connect(mWebSiteEdit, &PreferredLineEditWidget::preferredChanged, this, &WebWidget::slotPreferredChanged);
0035 
0036     mWebType->setObjectName(QLatin1StringView("webtype"));
0037     layout->addWidget(mWebType);
0038 
0039     mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0040     mAddButton->setObjectName(QLatin1StringView("addbutton"));
0041     mAddButton->setToolTip(i18n("Add a Web Site"));
0042     connect(mAddButton, &QToolButton::clicked, this, &WebWidget::slotAddWeb);
0043     layout->addWidget(mAddButton);
0044 
0045     mRemoveButton->setObjectName(QLatin1StringView("removebutton"));
0046     mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0047     mRemoveButton->setToolTip(i18n("Remove Web Site"));
0048     connect(mRemoveButton, &QToolButton::clicked, this, &WebWidget::slotRemoveWeb);
0049     layout->addWidget(mRemoveButton);
0050 }
0051 
0052 WebWidget::~WebWidget() = default;
0053 
0054 void WebWidget::clearWidget()
0055 {
0056     mWebSiteEdit->clear();
0057     mWebType->setCurrentIndex(0);
0058 }
0059 
0060 void WebWidget::updateAddRemoveButton(bool addButtonEnabled)
0061 {
0062     mAddButton->setEnabled(addButtonEnabled);
0063 }
0064 
0065 void WebWidget::slotAddWeb()
0066 {
0067     Q_EMIT addWidget(this);
0068 }
0069 
0070 void WebWidget::slotRemoveWeb()
0071 {
0072     Q_EMIT removeWidget(this);
0073 }
0074 
0075 void WebWidget::setPreferred(bool b)
0076 {
0077     mWebSiteEdit->setPreferred(b);
0078 }
0079 
0080 KContacts::ResourceLocatorUrl WebWidget::url()
0081 {
0082     mUrl.setUrl(QUrl(mWebSiteEdit->text().trimmed()));
0083     mUrl.setPreferred(mWebSiteEdit->preferred());
0084     mUrl.setType(KContacts::ResourceLocatorUrl::Type(mWebType->currentData().toInt()));
0085     return mUrl;
0086 }
0087 
0088 void WebWidget::loadWebSite(const KContacts::ResourceLocatorUrl &url)
0089 {
0090     mUrl = url;
0091     setPreferred(url.isPreferred());
0092     mWebType->setCurrentIndex(mWebType->findData((int)url.type()));
0093     mWebSiteEdit->setText(mUrl.url().toDisplayString());
0094 }
0095 
0096 void WebWidget::setReadOnly(bool readOnly)
0097 {
0098     mWebSiteEdit->setReadOnly(readOnly);
0099     mAddButton->setEnabled(!readOnly);
0100     mRemoveButton->setEnabled(!readOnly);
0101     mWebType->setEnabled(!readOnly);
0102 }
0103 
0104 void WebWidget::slotPreferredChanged()
0105 {
0106     Q_EMIT preferredChanged(this);
0107 }
0108 
0109 #include "moc_webwidget.cpp"