1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "cmakeitemkeeper.h"
6#include "common/common.h"
7
8namespace {
9static CmakeItemKeeper *ins {nullptr};
10};
11
12/*!
13 * \brief The CmakeItemKeeperPrivate class
14 */
15class CmakeItemKeeperPrivate
16{
17 friend class CmakeItemKeeper;
18 QHash<QStandardItem*, QPair<QString, QStringList>> itemCmakeFileNodes;
19};
20
21/*!
22 * \brief CmakeItemKeeper::CmakeItemKeeper
23 */
24CmakeItemKeeper::CmakeItemKeeper()
25 : d (new CmakeItemKeeperPrivate)
26{
27 // 全量变动监听,避免因为业务逻辑导致无法监听某个文件
28 QObject::connect(this, &Inotify::modified,
29 this, &CmakeItemKeeper::notifyFromWatcher);
30
31 QObject::connect(this, &Inotify::ignoreModified,
32 this, &CmakeItemKeeper::notifyFromWatcher);
33}
34
35CmakeItemKeeper *CmakeItemKeeper::instance() {
36 if (!::ins)
37 ::ins = new CmakeItemKeeper();
38 return ins;
39}
40
41CmakeItemKeeper::~CmakeItemKeeper()
42{
43 if (d)
44 delete d;
45}
46
47void CmakeItemKeeper::addCmakeRootFile(QStandardItem *root, const QString rootPath)
48{
49 d->itemCmakeFileNodes[root].first = rootPath;
50 Inotify::addPath(rootPath);
51 Inotify::removeIgnorePath(rootPath);
52}
53
54void CmakeItemKeeper::addCmakeSubFiles(QStandardItem *root, const QStringList subPaths)
55{
56 d->itemCmakeFileNodes[root].second += subPaths;
57 for (auto &val : subPaths) {
58 Inotify::addPath(val);
59 Inotify::removeIgnorePath(val);
60 }
61}
62
63void CmakeItemKeeper::delCmakeFileNode(QStandardItem *rootItem)
64{
65 Inotify::removePath(d->itemCmakeFileNodes.value(rootItem).first);
66 auto subFiles = d->itemCmakeFileNodes.value(rootItem).second;
67 for (auto &val : subFiles) {
68 Inotify::removePath(val);
69 }
70
71 d->itemCmakeFileNodes.remove(rootItem);
72}
73
74void CmakeItemKeeper::notifyFromWatcher(const QString &filePath)
75{
76 for (auto val: d->itemCmakeFileNodes) {
77 if (val.second.contains(filePath)
78 || val.first == filePath) {
79 emit cmakeFileNodeNotify(d->itemCmakeFileNodes.key(val), val);
80 break; //后续会出发 delete操作 再执行foreach将会崩溃
81 }
82 }
83}
84