File indexing completed on 2024-12-01 04:37:01

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     setPosition(KMessageWidget::Header);
0021     mDelayTimer->setSingleShot(true);
0022     mDelayTimer->setInterval(1s);
0023     connect(mDelayTimer, &QTimer::timeout, this, &ReconnectInfoWidget::slotUpdateTimer);
0024 
0025     connect(this, &KMessageWidget::linkActivated, this, &ReconnectInfoWidget::slotLinkActivated);
0026 }
0027 
0028 ReconnectInfoWidget::~ReconnectInfoWidget() = default;
0029 
0030 void ReconnectInfoWidget::slotUpdateTimer()
0031 {
0032     mCurrentDelay--;
0033     if (mCurrentDelay == 0) {
0034         updateText();
0035         Q_EMIT tryReconnect();
0036     } else {
0037         updateText();
0038         mDelayTimer->start();
0039     }
0040 }
0041 
0042 void ReconnectInfoWidget::slotLinkActivated(const QString &contents)
0043 {
0044     if (contents == QLatin1String("try_reconnect")) {
0045         if (mDelayTimer->isActive()) {
0046             mDelayTimer->stop();
0047         }
0048         Q_EMIT tryReconnect();
0049     }
0050 }
0051 
0052 int ReconnectInfoWidget::reconnectSecondDelay() const
0053 {
0054     return mReconnectSecondDelay;
0055 }
0056 
0057 void ReconnectInfoWidget::setReconnectSecondDelay(int newReconnectDelay)
0058 {
0059     mReconnectSecondDelay = newReconnectDelay;
0060     mCurrentDelay = mReconnectSecondDelay;
0061     updateText();
0062     animatedShow();
0063     if (mDelayTimer->isActive()) {
0064         mDelayTimer->stop();
0065     }
0066     mDelayTimer->start();
0067 }
0068 
0069 void ReconnectInfoWidget::updateText()
0070 {
0071     setText(i18n("%1 seconds before reconnecting. %2", mCurrentDelay, QStringLiteral("<a href=\"try_reconnect\">%1</a>").arg(i18n("(Try Reconnect)"))));
0072 }
0073 
0074 #include "moc_reconnectinfowidget.cpp"