File indexing completed on 2024-07-14 15:12:21

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "typingnotification.h"
0008 #include <QTimer>
0009 
0010 extern LIBRUQOLACORE_TESTS_EXPORT int timerTimeOutValueMs;
0011 LIBRUQOLACORE_TESTS_EXPORT int timerTimeOutValueMs = 2000;
0012 
0013 TypingNotification::TypingNotification(QObject *parent)
0014     : QObject(parent)
0015     , mTimer(new QTimer(this))
0016 {
0017     mTimer->setObjectName(QStringLiteral("typingnotificationtimer"));
0018     mTimer->setInterval(timerTimeOutValueMs);
0019     mTimer->setSingleShot(true);
0020     connect(mTimer, &QTimer::timeout, this, &TypingNotification::slotTimeout);
0021 }
0022 
0023 TypingNotification::~TypingNotification()
0024 {
0025     if (mTimer->isActive()) {
0026         mTimer->stop();
0027     }
0028 }
0029 
0030 void TypingNotification::textNotificationChanged(const QString &roomId, bool emptyString)
0031 {
0032     if (mTimer->isActive()) {
0033         mTimer->stop();
0034     }
0035     if (mTypingInprogress) {
0036         if (emptyString) {
0037             mTypingInprogress = false;
0038             // 1) Send info about typing.
0039             Q_EMIT informTypingStatus(roomId, false);
0040         } else {
0041             if (mRoomId != roomId) {
0042                 // We changed room.
0043                 // 1) stop typing in old room
0044                 Q_EMIT informTypingStatus(roomId, false);
0045 
0046                 // 2) start info about typing in new room.
0047                 Q_EMIT informTypingStatus(mRoomId, true);
0048             }
0049 
0050             // 3) restart timer.
0051             mTimer->start();
0052         }
0053     } else {
0054         mTypingInprogress = true;
0055         Q_EMIT informTypingStatus(roomId, true);
0056         // Send info about typing.
0057         // Restart timer.
0058         mTimer->start();
0059     }
0060     mRoomId = roomId;
0061 }
0062 
0063 void TypingNotification::slotTimeout()
0064 {
0065     mTypingInprogress = false;
0066     // Send info about stopping typing.
0067     Q_EMIT informTypingStatus(mRoomId, false);
0068 }
0069 
0070 #include "moc_typingnotification.cpp"