| 1 | /* |
| 2 | * Copyright (C) 2013-2015 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-service |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "inactivity-timer.h" |
| 22 | |
| 23 | #include <online-accounts-common/debug.h> |
| 24 | |
| 25 | using namespace OnlineAccountsUi; |
| 26 | |
| 27 | InactivityTimer::InactivityTimer(int interval, QObject *parent): |
| 28 | QObject(parent), |
| 29 | m_interval(interval) |
| 30 | { |
| 31 | m_timer.setSingleShot(true); |
| 32 | QObject::connect(&m_timer, SIGNAL(timeout()), |
| 33 | this, SLOT(onTimeout())); |
| 34 | } |
| 35 | |
| 36 | void InactivityTimer::watchObject(QObject *object) |
| 37 | { |
| 38 | connect(object, SIGNAL(isIdleChanged()), SLOT(onIdleChanged())); |
| 39 | m_watchedObjects.append(object); |
| 40 | |
| 41 | /* Force an initial check */ |
| 42 | onIdleChanged(); |
| 43 | } |
| 44 | |
| 45 | void InactivityTimer::onIdleChanged() |
| 46 | { |
| 47 | if (allObjectsAreIdle()) { |
| 48 | m_timer.start(m_interval); |
| 49 | } else { |
| 50 | m_timer.stop(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void InactivityTimer::onTimeout() |
| 55 | { |
| 56 | DEBUG(); |
| 57 | if (allObjectsAreIdle()) { |
| 58 | Q_EMIT timeout(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool InactivityTimer::allObjectsAreIdle() const |
| 63 | { |
| 64 | Q_FOREACH(const QObject *object, m_watchedObjects) { |
| 65 | if (!object->property("isIdle").toBool()) { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |