aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/globalfilechangeblocker.cpp
blob: 7514b5d3f9e7541dff71a0cc1788091060ab39e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (C) 2019 Orgad Shaneh <orgads@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "globalfilechangeblocker.h"

#include <QGuiApplication>

namespace Utils {

GlobalFileChangeBlocker::GlobalFileChangeBlocker()
{
    m_blockedState = QGuiApplication::applicationState() != Qt::ApplicationActive;
    connect(qApp, &QGuiApplication::applicationStateChanged,
            this, &GlobalFileChangeBlocker::applicationStateChanged);
}

GlobalFileChangeBlocker *GlobalFileChangeBlocker::instance()
{
    static GlobalFileChangeBlocker blocker;
    return &blocker;
}

void GlobalFileChangeBlocker::forceBlocked(bool blocked)
{
    blocked ? m_ignoreChanges.lock() : m_ignoreChanges.unlock();
    applicationStateChanged(QGuiApplication::applicationState());
}

void GlobalFileChangeBlocker::applicationStateChanged(Qt::ApplicationState state)
{
    const bool blocked = m_ignoreChanges.isLocked() || (state != Qt::ApplicationActive);
    if (blocked != m_blockedState) {
        emit stateChanged(blocked);
        m_blockedState = blocked;
    }
}

} // namespace Utils