LCOV - code coverage report
Current view: top level - src/engraving/api/v1 - util.h Coverage Total Hit
Test: filtered_coverage.info Lines: 12.5 % 8 1
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-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              : #pragma once
      24              : 
      25              : #include <QDir>
      26              : 
      27              : #include "muse_framework_config.h"
      28              : 
      29              : #ifdef QT_QPROCESS_SUPPORTED
      30              : #include <QProcess>
      31              : #endif
      32              : 
      33              : #include "global/modularity/ioc.h"
      34              : #include "global/iglobalconfiguration.h"
      35              : #include "extensions/iextensionsconfiguration.h"
      36              : #include "project/iprojectconfiguration.h"
      37              : #include "notation/inotationconfiguration.h"
      38              : #include "audio/main/iaudioconfiguration.h"
      39              : #include "engraving/infrastructure/mscio.h"
      40              : #include "project/iprojectsession.h"
      41              : 
      42              : namespace mu::engraving::apiv1 {
      43              : //---------------------------------------------------------
      44              : ///   \class FileIO
      45              : ///   Provides a simple API to perform file reading and
      46              : ///   writing operations. To use this class in a plugin put
      47              : ///   \code import FileIO 3.0 \endcode
      48              : ///   to the top part of the .qml plugin file.
      49              : ///   After that FileIO object can be declared and used
      50              : ///   in QML code:
      51              : ///   \code
      52              : ///   import MuseScore 3.0
      53              : ///   import FileIO 3.0
      54              : ///   MuseScore {
      55              : ///       FileIO {
      56              : ///           id: exampleFile
      57              : ///           source: "/tmp/example.txt"
      58              : ///       }
      59              : ///       onRun: {
      60              : ///           var test = exampleFile.read();
      61              : ///           console.log(test); // will print the file content
      62              : ///       }
      63              : ///   }
      64              : ///   \endcode
      65              : //---------------------------------------------------------
      66              : 
      67              : class FileIO : public QObject, public muse::Contextable
      68              : {
      69              :     Q_OBJECT
      70              : 
      71              :     /// Path to the file which is operated on
      72              :     Q_PROPERTY(QString source READ source WRITE setSource)
      73              : 
      74              :     muse::GlobalInject<muse::IGlobalConfiguration> globalConfiguration;
      75              :     muse::GlobalInject<muse::extensions::IExtensionsConfiguration> extensionsConfiguration;
      76              :     muse::GlobalInject<mu::project::IProjectConfiguration> projectConfiguration;
      77              :     muse::GlobalInject<mu::notation::INotationConfiguration> notationConfiguration;
      78              :     muse::GlobalInject<muse::audio::IAudioConfiguration> audioConfiguration;
      79              :     muse::ContextInject<mu::project::IProjectSession> projectSession = { this };
      80              : 
      81              : public:
      82              : 
      83              :     /// \cond PLUGIN_API \private \endcond
      84              :     explicit FileIO(QObject* parent = 0);
      85              :     explicit FileIO(const muse::modularity::ContextPtr& ctx, QObject* parent = 0);
      86              : 
      87              :     /// Reads file contents and returns a string.
      88              :     /// In case error occurs, error() signal is emitted
      89              :     /// and an empty string is returned.
      90              :     Q_INVOKABLE QString read();
      91              :     /// muse::Returns true if the file exists
      92              :     Q_INVOKABLE bool exists();
      93              :     /// Writes a string to the file.
      94              :     /// \warning This function overwrites all the contents of
      95              :     /// the file pointed by FileIO::source so it becomes lost.
      96              :     /// \note For security reasons, files can only be written within
      97              :     /// the user's MuseScore data directories (userAppDataPath and userDataPath).
      98              :     /// Attempts to write outside these directories will be blocked.
      99              :     /// \returns `true` if an operation finished successfully.
     100              :     Q_INVOKABLE bool write(const QString& data);
     101              :     /**
     102              :      * Writes binary data to the file.
     103              :      * Each character code (0-255) in the string is written as a single byte.
     104              :      * Use this for binary files like images, soundfonts, etc.
     105              :      * \warning This function overwrites all the contents of
     106              :      * the file pointed by FileIO::source so it becomes lost.
     107              :      * \note For security reasons, files can only be written within
     108              :      * the user's MuseScore data directories (userAppDataPath and userDataPath).
     109              :      * Attempts to write outside these directories will be blocked.
     110              :      * \returns `true` if an operation finished successfully.
     111              :      */
     112              :     Q_INVOKABLE bool writeBinary(const QString& data);
     113              :     /// Removes the file
     114              :     Q_INVOKABLE bool remove();
     115              :     /// muse::Returns user's home directory
     116            0 :     Q_INVOKABLE QString homePath() { QDir dir; return dir.homePath(); }
     117              :     /// muse::Returns a path suitable for a temporary file
     118            0 :     Q_INVOKABLE QString tempPath() { QDir dir; return dir.tempPath(); }
     119              :     // Returns the user's MuseScore documents directory (default location for Scores, Plugins, SoundFonts, Styles, Templates)
     120              :     Q_INVOKABLE QString userDataPath();
     121              :     // Returns the user-configured Plugins directory (Preferences → Folders → Plugins)
     122              :     Q_INVOKABLE QString pluginsUserPath();
     123              :     // Returns the user-configured Scores directory (Preferences → Folders → Scores)
     124              :     Q_INVOKABLE QString userProjectsPath();
     125              :     // Returns the user-configured Templates directory (Preferences → Folders → Templates)
     126              :     Q_INVOKABLE QString userTemplatesPath();
     127              :     // Returns the user-configured Styles directory (Preferences → Folders → Styles)
     128              :     Q_INVOKABLE QString userStylesPath();
     129              :     // Returns the user-configured SoundFonts directories (Preferences → Folders → SoundFonts)
     130              :     Q_INVOKABLE QStringList userSoundFontDirectories();
     131              :     // Returns the plugin's folder's path
     132              :     Q_INVOKABLE QString pluginDirectoryPath();
     133              :     // Returns the project's path (ex: .../Desktop/project.mscz)
     134              :     Q_INVOKABLE QString projectPath();
     135              :     // Returns whether or not the project is stored as a folder (with a .mscx file)
     136              :     Q_INVOKABLE bool isProjectDirectory();
     137              :     // Returns the project's folder's path
     138              :     Q_INVOKABLE QString projectDirectoryPath();
     139              :     /// Returns true if the plugin is allowed to write to the path
     140              :     Q_INVOKABLE bool isPathWriteable(const QString& path);
     141              : 
     142              :     /// muse::Returns the file's last modification time
     143              :     Q_INVOKABLE int modifiedTime();
     144              : 
     145              :     /// \cond MS_INTERNAL
     146            0 :     QString source() { return m_source; }
     147              : 
     148              : public slots:
     149            8 :     void setSource(const QString& source) { m_source = source; }
     150              :     /// \endcond
     151              : 
     152              : signals:
     153              :     /// Emitted on file operations errors.
     154              :     /// Implement onError() in your FileIO object to handle this signal.
     155              :     /// \param msg A short textual description of the error occurred.
     156              :     void error(const QString& msg);
     157              : 
     158              : private:
     159              :     QString m_source;
     160              : };
     161              : 
     162              : #ifdef QT_QPROCESS_SUPPORTED
     163              : //---------------------------------------------------------
     164              : //   MsProcess
     165              : //   @@ QProcess
     166              : ///   \brief Start an external program.\ Available in QML
     167              : ///   as \p QProcess.
     168              : ///   \details Using this will most probably result in the
     169              : ///   plugin to be platform dependant. \since MuseScore 3.2
     170              : //---------------------------------------------------------
     171              : 
     172              : class MsProcess : public QProcess
     173              : {
     174              :     Q_OBJECT
     175              : 
     176              : public:
     177            0 :     MsProcess(QObject* parent = 0)
     178            0 :         : QProcess(parent) {}
     179              : 
     180              : public slots:
     181              :     /// Execute an external command.
     182              :     /// \param command A command line to execute.
     183              :     /// \warning This function is deprecated. Use \ref startWithArgs instead.
     184              :     Q_INVOKABLE void start(const QString& command);
     185              :     /// Execute an external command.
     186              :     /// \param program A program to execute.
     187              :     /// \param args An array of arguments passed to the program.
     188              :     /// \since MuseScore 4.3
     189              :     Q_INVOKABLE void startWithArgs(const QString& program, const QStringList& args);
     190              :     /// --
     191            0 :     Q_INVOKABLE bool waitForFinished(int msecs = 30000) { return QProcess::waitForFinished(msecs); }
     192              :     /// --
     193            0 :     Q_INVOKABLE QByteArray readAllStandardOutput() { return QProcess::readAllStandardOutput(); }
     194              : };
     195              : #endif // QT_QPROCESS_SUPPORTED
     196              : }
        

Generated by: LCOV version 2.0-1