summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/RecursiveFileSystemWatcher.cpp
blob: b67c4e7bff221ccbb25976511597a48bc186cd34 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "RecursiveFileSystemWatcher.h"

#include <QDebug>

RecursiveFileSystemWatcher::RecursiveFileSystemWatcher(QObject* parent)
	: QObject(parent),
	  m_watcher(new QFileSystemWatcher(this))
{
	connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &RecursiveFileSystemWatcher::fileChange);
	connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &RecursiveFileSystemWatcher::directoryChange);
}

void RecursiveFileSystemWatcher::setRootDir(const QDir& root)
{
	bool wasEnabled = m_isEnabled;
	disable();
	m_root = root;
	setFiles(scanRecursive(m_root));
	if (wasEnabled)
	{
		enable();
	}
}
void RecursiveFileSystemWatcher::setWatchFiles(const bool watchFiles)
{
	bool wasEnabled = m_isEnabled;
	disable();
	m_watchFiles = watchFiles;
	if (wasEnabled)
	{
		enable();
	}
}

void RecursiveFileSystemWatcher::enable()
{
	if (m_isEnabled)
	{
		return;
	}
	Q_ASSERT(m_root != QDir::root());
	addFilesToWatcherRecursive(m_root);
	m_isEnabled = true;
}
void RecursiveFileSystemWatcher::disable()
{
	if (!m_isEnabled)
	{
		return;
	}
	m_isEnabled = false;
	m_watcher->removePaths(m_watcher->files());
	m_watcher->removePaths(m_watcher->directories());
}

void RecursiveFileSystemWatcher::setFiles(const QStringList& files)
{
	if (files != m_files)
	{
		m_files = files;
		emit filesChanged();
	}
}

void RecursiveFileSystemWatcher::addFilesToWatcherRecursive(const QDir& dir)
{
	m_watcher->addPath(dir.absolutePath());
	for (const QString& directory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
	{
		addFilesToWatcherRecursive(dir.absoluteFilePath(directory));
	}
	if (m_watchFiles)
	{
		for (const QFileInfo& info : dir.entryInfoList(QDir::Files))
		{
			m_watcher->addPath(info.absoluteFilePath());
		}
	}
}
QStringList RecursiveFileSystemWatcher::scanRecursive(const QDir& directory)
{
	QStringList ret;
	if (!m_matcher)
	{
		return {};
	}
	for (const QString& dir : directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden))
	{
		ret.append(scanRecursive(directory.absoluteFilePath(dir)));
	}
	for (const QString& file : directory.entryList(QDir::Files | QDir::Hidden))
	{
		auto relPath = m_root.relativeFilePath(directory.absoluteFilePath(file));
		if (m_matcher(relPath))
		{
			ret.append(relPath);
		}
	}
	return ret;
}

void RecursiveFileSystemWatcher::fileChange(const QString& path)
{
	emit fileChanged(path);
}
void RecursiveFileSystemWatcher::directoryChange([[maybe_unused]] const QString& path)
{
	setFiles(scanRecursive(m_root));
}