Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "filesystemwatcher.h"
24 :
25 : #ifndef NO_QT_SUPPORT
26 : #include <QFileSystemWatcher>
27 : #endif
28 :
29 : namespace muse::io {
30 : #ifndef NO_QT_SUPPORT
31 :
32 0 : FileSystemWatcher::FileSystemWatcher()
33 0 : : m_watcher(std::make_unique<QFileSystemWatcher>())
34 : {
35 0 : QObject::connect(m_watcher.get(), &QFileSystemWatcher::fileChanged,
36 0 : [this](const QString& path) {
37 0 : m_fileChannel.send(path.toStdString());
38 0 : });
39 :
40 0 : QObject::connect(m_watcher.get(), &QFileSystemWatcher::directoryChanged,
41 0 : [this](const QString& path) {
42 0 : m_directoryChannel.send(path.toStdString());
43 0 : });
44 0 : }
45 :
46 0 : FileSystemWatcher::~FileSystemWatcher() = default;
47 :
48 0 : void FileSystemWatcher::startWatching(const std::string& path)
49 : {
50 0 : m_watcher->addPath(QString::fromStdString(path));
51 0 : }
52 :
53 0 : void FileSystemWatcher::stopWatching(const std::string& path)
54 : {
55 0 : if (path.empty()) {
56 0 : const auto watchedPaths = m_watcher->files();
57 0 : if (!watchedPaths.isEmpty()) {
58 0 : m_watcher->removePaths(watchedPaths);
59 : }
60 0 : const auto watchedDirs = m_watcher->directories();
61 0 : if (!watchedDirs.isEmpty()) {
62 0 : m_watcher->removePaths(watchedDirs);
63 : }
64 0 : return;
65 0 : }
66 0 : m_watcher->removePath(QString::fromStdString(path));
67 : }
68 :
69 : #else
70 :
71 : FileSystemWatcher::FileSystemWatcher() = default;
72 : FileSystemWatcher::~FileSystemWatcher() = default;
73 :
74 : void FileSystemWatcher::startWatching(const std::string&) {}
75 : void FileSystemWatcher::stopWatching(const std::string&) {}
76 :
77 : #endif
78 :
79 0 : muse::async::Channel<std::string> FileSystemWatcher::fileChanged() const
80 : {
81 0 : return m_fileChannel;
82 : }
83 :
84 0 : muse::async::Channel<std::string> FileSystemWatcher::directoryChanged() const
85 : {
86 0 : return m_directoryChannel;
87 : }
88 : }
|