keepassxc

Форк
0
/
InactivityTimer.cpp 
81 строка · 2.1 Кб
1
/*
2
 *  Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "InactivityTimer.h"
19

20
#include <QCoreApplication>
21
#include <QTimer>
22

23
InactivityTimer::InactivityTimer(QObject* parent)
24
    : QObject(parent)
25
    , m_timer(new QTimer(this))
26
    , m_active(false)
27
{
28
    m_timer->setSingleShot(true);
29
    connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
30
}
31

32
void InactivityTimer::setInactivityTimeout(int inactivityTimeout)
33
{
34
    Q_ASSERT(inactivityTimeout > 0);
35

36
    m_timer->setInterval(inactivityTimeout);
37
}
38

39
void InactivityTimer::activate()
40
{
41
    if (!m_active) {
42
        qApp->installEventFilter(this);
43
    }
44
    m_active = true;
45
    m_timer->start();
46
}
47

48
void InactivityTimer::deactivate()
49
{
50
    qApp->removeEventFilter(this);
51
    m_active = false;
52
    m_timer->stop();
53
}
54

55
bool InactivityTimer::eventFilter(QObject* watched, QEvent* event)
56
{
57
    const QEvent::Type type = event->type();
58
    // clang-format off
59
    if ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease)
60
        || (type >= QEvent::HoverEnter && type <= QEvent::HoverMove)
61
        || (type == QEvent::Wheel)) {
62
        m_timer->start();
63
    }
64
    // clang-format on
65

66
    return QObject::eventFilter(watched, event);
67
}
68

69
void InactivityTimer::timeout()
70
{
71
    // make sure we don't emit the signal a second time while it's still processed
72
    if (!m_emitMutx.tryLock()) {
73
        return;
74
    }
75

76
    if (m_active && !m_timer->isActive()) {
77
        emit inactivityDetected();
78
    }
79

80
    m_emitMutx.unlock();
81
}
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.