Warning, file /network/ruqola/src/widgets/bannerinfodialog/bannermessagewidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "bannermessagewidget.h"
0007 #include "ruqolawidgets_debug.h"
0008 #include <KLocalizedString>
0009 #include <QAction>
0010 #include <QDesktopServices>
0011 #include <QUrl>
0012 
0013 BannerMessageWidget::BannerMessageWidget(QWidget *parent)
0014     : KMessageWidget(parent)
0015 {
0016     setVisible(false);
0017     setCloseButtonVisible(false);
0018     setMessageType(Information);
0019     setWordWrap(true);
0020 #if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
0021     setPosition(KMessageWidget::Header);
0022 #endif
0023 
0024     auto readAction = new QAction(i18n("Mark as Read"), this);
0025     readAction->setObjectName(QStringLiteral("read_action"));
0026     connect(readAction, &QAction::triggered, this, &BannerMessageWidget::slotReadInfo);
0027     addAction(readAction);
0028     connect(this, &BannerMessageWidget::linkActivated, this, &BannerMessageWidget::slotOpenUrl);
0029 }
0030 
0031 BannerMessageWidget::~BannerMessageWidget() = default;
0032 
0033 void BannerMessageWidget::slotReadInfo()
0034 {
0035     if (mBannerInfos.isEmpty()) {
0036         qCWarning(RUQOLAWIDGETS_LOG) << "banner info is empty! It's a bug";
0037     } else {
0038         const auto info = mBannerInfos.takeFirst();
0039 
0040         Q_EMIT infoWasRead(info.identifier);
0041         updateInfo();
0042     }
0043 }
0044 
0045 const QVector<BannerInfos::UnreadInformation> &BannerMessageWidget::bannerInfos() const
0046 {
0047     return mBannerInfos;
0048 }
0049 
0050 void BannerMessageWidget::setBannerInfos(const QVector<BannerInfos::UnreadInformation> &newBannerInfo)
0051 {
0052     if (mBannerInfos != newBannerInfo) {
0053         mBannerInfos = newBannerInfo;
0054         updateInfo();
0055     }
0056 }
0057 
0058 void BannerMessageWidget::updateInfo()
0059 {
0060     if (mBannerInfos.isEmpty()) {
0061         animatedHide();
0062     } else {
0063         const auto info = mBannerInfos.first();
0064         setText(info.i18nMessage);
0065         animatedShow();
0066     }
0067 }
0068 
0069 void BannerMessageWidget::slotOpenUrl(const QString &url)
0070 {
0071     QDesktopServices::openUrl(QUrl(url));
0072 }
0073 
0074 #include "moc_bannermessagewidget.cpp"