/* Copyright 2006-2026 The QElectroTech Team This file is part of QElectroTech. QElectroTech 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, either version 2 of the License, or (at your option) any later version. QElectroTech 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 QElectroTech. If not, see . */ #include "scriptlibrary.h" #include "../qetapp.h" #include #include #include #include #include #include #include ScriptLibrary &ScriptLibrary::instance() { static ScriptLibrary library; return library; } /** @brief ScriptLibrary::folder Where stored scripts live: "scripts" in the user's data folder, next to the user's own element and title block collections. */ QString ScriptLibrary::folder() { return QETApp::dataDir() + QStringLiteral("/scripts"); } /** @brief ScriptLibrary::actionId The ShortcutManager id of a script's action. The "diagrameditor." prefix is what puts it among the commands the shortcut bar can show. */ QString ScriptLibrary::actionId(const QString &script_id) { return QStringLiteral("diagrameditor.script.") + script_id; } ScriptLibrary::ScriptLibrary() { //An editor saving a file often writes it more than once, or //replaces it through a rename: wait for it to settle. m_rescan_timer.setSingleShot(true); m_rescan_timer.setInterval(250); connect(&m_rescan_timer, &QTimer::timeout, this, &ScriptLibrary::rescan); connect(&m_watcher, &QFileSystemWatcher::directoryChanged, &m_rescan_timer, qOverload<>(&QTimer::start)); connect(&m_watcher, &QFileSystemWatcher::fileChanged, &m_rescan_timer, qOverload<>(&QTimer::start)); rescan(); } /** @brief ScriptLibrary::watch Watch the folder and each script in it, or, while the folder does not exist yet, the data folder it will be created in. */ void ScriptLibrary::watch() { if (!m_watcher.files().isEmpty()) m_watcher.removePaths(m_watcher.files()); if (!m_watcher.directories().isEmpty()) m_watcher.removePaths(m_watcher.directories()); const QDir dir(folder()); if (!dir.exists()) { if (QFileInfo::exists(QETApp::dataDir())) { m_watcher.addPath(QETApp::dataDir()); } return; } m_watcher.addPath(dir.path()); QStringList files; for (const Script &s : std::as_const(m_scripts)) files << s.path; for (const QString &e : std::as_const(m_errors)) files << dir.filePath(e.section(QLatin1Char(':'), 0, 0)); if (!files.isEmpty()) m_watcher.addPaths(files); } void ScriptLibrary::rescan() { QList