File indexing completed on 2024-05-12 16:27:34

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "reconnectinfowidget.h"
0008 #include <KLocalizedString>
0009 #include <QTimer>
0010 #include <chrono>
0011 using namespace std::chrono_literals;
0012 
0013 ReconnectInfoWidget::ReconnectInfoWidget(QWidget *parent)
0014     : KMessageWidget(parent)
0015     , mDelayTimer(new QTimer(this))
0016 {
0017     setVisible(false);
0018     setCloseButtonVisible(false);
0019     setMessageType(Information);
0020 #if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
0021     setPosition(KMessageWidget::Header);
0022 #endif
0023     mDelayTimer->setSingleShot(true);
0024     mDelayTimer->setInterval(1s);
0025     connect(mDelayTimer, &QTimer::timeout, this, &ReconnectInfoWidget::slotUpdateTimer);
0026 
0027     connect(this, &KMessageWidget::linkActivated, this, &ReconnectInfoWidget::slotLinkActivated);
0028 }
0029 
0030 ReconnectInfoWidget::~ReconnectInfoWidget() = default;
0031 
0032 void ReconnectInfoWidget::slotUpdateTimer()
0033 {
0034     mCurrentDelay--;
0035     if (mCurrentDelay == 0) {
0036         updateText();
0037         Q_EMIT tryReconnect();
0038     } else {
0039         updateText();
0040         mDelayTimer->start();
0041     }
0042 }
0043 
0044 void ReconnectInfoWidget::slotLinkActivated(const QString &contents)
0045 {
0046     if (contents == QLatin1String("try_reconnect")) {
0047         if (mDelayTimer->isActive()) {
0048             mDelayTimer->stop();
0049         }
0050         Q_EMIT tryReconnect();
0051     }
0052 }
0053 
0054 int ReconnectInfoWidget::reconnectSecondDelay() const
0055 {
0056     return mReconnectSecondDelay;
0057 }
0058 
0059 void ReconnectInfoWidget::setReconnectSecondDelay(int newReconnectDelay)
0060 {
0061     mReconnectSecondDelay = newReconnectDelay;
0062     mCurrentDelay = mReconnectSecondDelay;
0063     updateText();
0064     animatedShow();
0065     if (mDelayTimer->isActive()) {
0066         mDelayTimer->stop();
0067     }
0068     mDelayTimer->start();
0069 }
0070 
0071 void ReconnectInfoWidget::updateText()
0072 {
0073     setText(i18n("%1 seconds before reconnecting. %2", mCurrentDelay, QStringLiteral("<a href=\"try_reconnect\">%1</a>").arg(i18n("(Try Reconnect)"))));
0074 }
0075 
0076 #include "moc_reconnectinfowidget.cpp"