LCOV - code coverage report
Current view: top level - src/project/internal - exportprojectscenario.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 83.7 % 300 251
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              : #include "exportprojectscenario.h"
      24              : 
      25              : #include <thread>
      26              : 
      27              : #include <QTimer>
      28              : 
      29              : #include "global/io/fileinfo.h"
      30              : #include "global/io/filestream.h"
      31              : 
      32              : #include "translation.h"
      33              : #include "log.h"
      34              : 
      35              : #include "engraving/dom/score.h"
      36              : 
      37              : #include "notation/iexcerptnotation.h" // IWYU pragma: keep
      38              : #include "notation/imasternotation.h"
      39              : #include "notation/inotation.h"
      40              : #include "notation/inotationelements.h" // IWYU pragma: keep
      41              : #include "notation/inotationpainting.h" // IWYU pragma: keep
      42              : 
      43              : #include "inotationproject.h"
      44              : 
      45              : using namespace muse;
      46              : using namespace muse::io;
      47              : using namespace mu::project;
      48              : using namespace mu::notation;
      49              : using muse::async::Promise;
      50              : 
      51              : struct ExportProjectScenario::ExportState {
      52              :     INotationPtrList notations;
      53              :     std::vector<ViewMode> viewModes;
      54              : 
      55              :     std::vector<ExportFile> files;
      56              :     size_t currentFileIndex = 0;
      57              :     bool isCurrentFileConfirmed = false; // the user agreed to replace or retry the current file
      58              :     FileConflictPolicy fileConflictPolicy = FileConflictPolicy::Undefined;
      59              : 
      60              :     muse::io::path_t destinationPath;
      61              :     bool openDestinationFolderOnExport = false;
      62              : 
      63              :     Progress* writerProgress = nullptr;
      64              :     Promise<Ret>::Resolve resolve;
      65              : 
      66           58 :     void moveToNextFile()
      67              :     {
      68           58 :         ++currentFileIndex;
      69           58 :         isCurrentFileConfirmed = false;
      70           58 :     }
      71              : };
      72              : 
      73            0 : std::vector<INotationWriter::UnitType> ExportProjectScenario::supportedUnitTypes(const ExportType& exportType) const
      74              : {
      75            0 :     IF_ASSERT_FAILED(!exportType.suffixes.isEmpty()) {
      76            0 :         return {};
      77              :     }
      78              : 
      79            0 :     auto writer = writers()->writer(exportType.suffixes.front().toStdString());
      80            0 :     if (!writer) {
      81            0 :         return {};
      82              :     }
      83              : 
      84            0 :     return writer->supportedUnitTypes();
      85            0 : }
      86              : 
      87            0 : RetVal<muse::io::path_t> ExportProjectScenario::askExportPath(const INotationPtrList& notations, const ExportType& exportType,
      88              :                                                               INotationWriter::UnitType unitType, muse::io::path_t defaultDirPath) const
      89              : {
      90            0 :     INotationProjectPtr project = context()->project();
      91              : 
      92            0 :     std::string filenameAddition;
      93              : 
      94              :     // If only one file will be created, the filename will be exactly what the user
      95              :     // types in the save dialog and therefore we can put the file dialog in charge of
      96              :     // asking the user whether an existing file should be overridden. Otherwise, we
      97              :     // will take care of that ourselves.
      98            0 :     bool isCreatingOnlyOneFile = guessIsCreatingOnlyOneFile(notations, unitType);
      99            0 :     bool isExportingOnlyOneScore = notations.size() == 1;
     100              : 
     101            0 :     if (unitType == INotationWriter::UnitType::MULTI_PART && !isExportingOnlyOneScore) {
     102            0 :         bool containsMaster = std::find_if(notations.cbegin(), notations.cend(), [this](INotationPtr notation) {
     103            0 :             return isMainNotation(notation);
     104            0 :         }) != notations.cend();
     105              : 
     106            0 :         if (containsMaster) {
     107              :             //: Used in export filename suggestion. Please use only characters that are valid for filenames.
     108            0 :             filenameAddition = "-" + muse::trc("project/export", "Score_and_Parts");
     109              :         } else {
     110              :             //: Used in export filename suggestion. Please use only characters that are valid for filenames.
     111            0 :             filenameAddition = "-" + muse::trc("project/export", "Parts");
     112              :         }
     113            0 :     } else if (isExportingOnlyOneScore) {
     114            0 :         if (!isMainNotation(notations.front())) {
     115            0 :             filenameAddition = "-" + io::escapeFileName(notations.front()->name()).toStdString();
     116              :         }
     117              : 
     118            0 :         if (unitType == INotationWriter::UnitType::PER_PAGE && isCreatingOnlyOneFile) {
     119              :             // So there is only one page
     120            0 :             filenameAddition += "-1";
     121              :         }
     122              :     }
     123              : 
     124            0 :     muse::io::path_t defaultPath;
     125            0 :     if (defaultDirPath == "") {
     126            0 :         defaultPath = configuration()->defaultSavingFilePath(project, filenameAddition, exportType.suffixes.front().toStdString());
     127              :     } else {
     128            0 :         defaultPath = defaultDirPath.appendingComponent(io::filename(project->path(), false) + filenameAddition)
     129            0 :                       .appendingSuffix(exportType.suffixes.front().toStdString());
     130              :     }
     131              : 
     132            0 :     RetVal<muse::io::path_t> exportPath;
     133            0 :     exportPath.val = interactive()->selectSavingFileSync(muse::trc("project/export", "Export"), defaultPath,
     134            0 :                                                          exportType.filter(), isCreatingOnlyOneFile);
     135            0 :     exportPath.ret = !exportPath.val.empty();
     136              : 
     137            0 :     return exportPath;
     138            0 : }
     139              : 
     140           48 : Promise<Ret> ExportProjectScenario::exportScores(notation::INotationPtrList notations, const muse::io::path_t destinationPath,
     141              :                                                  INotationWriter::UnitType unitType, bool openDestinationFolderOnExport) const
     142              : {
     143           96 :     return async::make_promise<Ret>([this, notations, destinationPath, unitType, openDestinationFolderOnExport](auto resolve) {
     144           48 :         m_pendingExports.push_back([this, notations, destinationPath, unitType, openDestinationFolderOnExport, resolve]() {
     145           48 :             startExport(notations, destinationPath, unitType, openDestinationFolderOnExport, resolve);
     146              :         });
     147              : 
     148              :         //! NOTE The exports go one after another, so that they do not change the view modes of each other.
     149              :         //! The resolve is lost if it is called before the caller subscribes to the promise,
     150              :         //! so an export always starts on the next event loop
     151           48 :         if (!m_isExporting) {
     152           46 :             m_isExporting = true;
     153           46 :             startNextExportLater();
     154              :         }
     155              : 
     156           48 :         return Promise<Ret>::dummy_result();
     157           96 :     }, async::PromiseType::AsyncByBody);
     158              : }
     159              : 
     160           93 : void ExportProjectScenario::startNextExportLater() const
     161              : {
     162           93 :     QTimer::singleShot(0, &m_timersContext, [this]() {
     163           93 :         if (m_pendingExports.empty()) {
     164           45 :             m_isExporting = false;
     165           45 :             return;
     166              :         }
     167              : 
     168           48 :         std::function<void()> start = std::move(m_pendingExports.front());
     169           48 :         m_pendingExports.pop_front();
     170           48 :         start();
     171           48 :     });
     172           93 : }
     173              : 
     174           48 : void ExportProjectScenario::startExport(const notation::INotationPtrList& notations, const muse::io::path_t& destinationPath,
     175              :                                         INotationWriter::UnitType unitType, bool openDestinationFolderOnExport,
     176              :                                         const Promise<Ret>::Resolve& resolve) const
     177              : {
     178           48 :     std::string suffix = io::suffix(destinationPath);
     179           48 :     INotationWriterPtr writer = writers()->writer(suffix);
     180              : 
     181           48 :     if (!writer) {
     182            2 :         (void)resolve(make_ret(Ret::Code::NotSupported));
     183            2 :         startNextExportLater();
     184            2 :         return;
     185              :     }
     186              : 
     187           46 :     IF_ASSERT_FAILED(writer->supportsUnitType(unitType)) {
     188            0 :         (void)resolve(make_ret(Ret::Code::NotSupported));
     189            0 :         startNextExportLater();
     190            0 :         return;
     191              :     }
     192              : 
     193              :     // Make sure we do this in the same as we did it in `askExportPath`, so before initializing notations
     194           46 :     bool isCreatingOnlyOneFile = guessIsCreatingOnlyOneFile(notations, unitType);
     195              : 
     196              :     // The user might have selected not-yet-inited excerpts
     197           46 :     ExcerptNotationList excerptsToInit;
     198           46 :     ExcerptNotationList potentialExcerpts = masterNotation()->potentialExcerpts();
     199              : 
     200          117 :     for (const INotationPtr& notation : notations) {
     201           71 :         auto it = std::find_if(potentialExcerpts.cbegin(), potentialExcerpts.cend(), [notation](const IExcerptNotationPtr& excerpt) {
     202            5 :             return excerpt->notation() == notation;
     203              :         });
     204              : 
     205           71 :         if (it != potentialExcerpts.cend()) {
     206            2 :             excerptsToInit.push_back(*it);
     207              :         }
     208              :     }
     209              : 
     210           46 :     masterNotation()->initExcerpts(excerptsToInit);
     211              : 
     212              :     // Scores that are closed may have never been laid out, so we lay them out now
     213          117 :     for (const INotationPtr& notation : notations) {
     214           71 :         mu::engraving::Score* score = notation->elements()->msScore();
     215           71 :         if (!score->autoLayoutEnabled()) {
     216            0 :             score->doLayout();
     217              :         }
     218              :     }
     219              : 
     220           46 :     auto state = std::make_shared<ExportState>();
     221           46 :     state->notations = notations;
     222           46 :     state->destinationPath = destinationPath;
     223           46 :     state->openDestinationFolderOnExport = openDestinationFolderOnExport;
     224              : 
     225              :     // Backup view modes
     226           46 :     state->viewModes = viewModes(notations);
     227           46 :     setViewModes(notations, ViewMode::PAGE);
     228              : 
     229           46 :     state->files = exportFiles(notations, destinationPath, writer, unitType, isCreatingOnlyOneFile);
     230              : 
     231              :     // If isCreatingOnlyOneFile, the save dialog has already asked whether to replace
     232              :     // any existing files. If the user cancels, the filepath will be empty, so we would
     233              :     // not reach this point. But if we do, existing files should be overridden.
     234           46 :     state->fileConflictPolicy = isCreatingOnlyOneFile ? FileConflictPolicy::ReplaceAll : FileConflictPolicy::Undefined;
     235              : 
     236           46 :     state->writerProgress = writer->progress();
     237           46 :     if (state->writerProgress) {
     238            3 :         showExportProgress(isAudioExport(suffix));
     239            3 :         m_exportProgress.start();
     240              : 
     241            3 :         state->writerProgress->progressChanged().onReceive(this, [this, state](int64_t current, int64_t total, const std::string& status) {
     242            2 :             m_exportProgress.progress(state->currentFileIndex * total + current, state->files.size() * total, status);
     243            2 :         }, async::Asyncable::Mode::SetReplace);
     244              : 
     245            3 :         m_exportProgress.canceled().onNotify(this, [writer]() {
     246            1 :             writer->abort();
     247            1 :         }, muse::async::Asyncable::Mode::SetReplace);
     248              :     }
     249              : 
     250           46 :     state->resolve = resolve;
     251           46 :     exportNextFiles(state);
     252           50 : }
     253              : 
     254            0 : const ExportInfo& ExportProjectScenario::exportInfo() const
     255              : {
     256            0 :     return m_exportInfo;
     257              : }
     258              : 
     259            0 : void ExportProjectScenario::setExportInfo(const ExportInfo& exportInfo)
     260              : {
     261            0 :     m_exportInfo = exportInfo;
     262            0 : }
     263              : 
     264           46 : bool ExportProjectScenario::guessIsCreatingOnlyOneFile(const notation::INotationPtrList& notations,
     265              :                                                        INotationWriter::UnitType unitType) const
     266              : {
     267           46 :     switch (unitType) {
     268            5 :     case INotationWriter::UnitType::PER_PAGE: {
     269            5 :         if (notations.size() == 1) {
     270            4 :             INotationPtr notation = notations.front();
     271              : 
     272              :             // Check if it is not a potential (not-yet-initialized) excerpt
     273            4 :             ExcerptNotationList potentialExcerpts = masterNotation()->potentialExcerpts();
     274              : 
     275            4 :             auto it = std::find_if(potentialExcerpts.cbegin(), potentialExcerpts.cend(), [notation](const IExcerptNotationPtr& excerpt) {
     276            1 :                     return excerpt->notation() == notation;
     277              :                 });
     278              : 
     279            4 :             if (it == potentialExcerpts.cend()) {
     280            3 :                 ViewMode viewMode = notation->painting()->viewMode();
     281            3 :                 notation->painting()->setViewMode(ViewMode::PAGE);
     282              : 
     283            3 :                 bool onePage = notations.front()->elements()->pages().size() == 1;
     284              : 
     285            3 :                 notation->painting()->setViewMode(viewMode);
     286              : 
     287            3 :                 return onePage;
     288              :             }
     289            7 :         }
     290              : 
     291            2 :         return false;
     292              :     };
     293           40 :     case INotationWriter::UnitType::PER_PART:
     294           40 :         return notations.size() == 1;
     295            1 :     case INotationWriter::UnitType::MULTI_PART:
     296            1 :         return true;
     297              :     }
     298              : 
     299            0 :     return false;
     300              : }
     301              : 
     302           46 : std::vector<ExportProjectScenario::ExportFile> ExportProjectScenario::exportFiles(const INotationPtrList& notations,
     303              :                                                                                   const muse::io::path_t& destinationPath,
     304              :                                                                                   INotationWriterPtr writer,
     305              :                                                                                   INotationWriter::UnitType unitType,
     306              :                                                                                   bool isCreatingOnlyOneFile) const
     307              : {
     308           46 :     std::vector<ExportFile> files;
     309              : 
     310           46 :     bool isExportingOnlyOneScore = notations.size() == 1;
     311              : 
     312              :     INotationWriter::Options options {
     313           46 :         { INotationWriter::OptionKey::UNIT_TYPE, Val(unitType) },
     314          138 :     };
     315              : 
     316           46 :     switch (unitType) {
     317            5 :     case INotationWriter::UnitType::PER_PAGE: {
     318           11 :         for (const INotationPtr& notation : notations) {
     319            6 :             size_t pageCount = notation->elements()->msScore()->pages().size();
     320            6 :             bool isMain = isMainNotation(notation);
     321              : 
     322           15 :             for (size_t page = 0; page < pageCount; ++page) {
     323            9 :                 options[INotationWriter::OptionKey::PAGE_NUMBER] = Val(static_cast<int>(page));
     324              : 
     325              :                 muse::io::path_t definitivePath = isCreatingOnlyOneFile
     326            9 :                                                   ? destinationPath
     327            9 :                                                   : completeExportPath(destinationPath, notation, isMain, isExportingOnlyOneScore,
     328            9 :                                                                        static_cast<int>(page));
     329              : 
     330           18 :                 files.push_back({ definitivePath, [writer, notation, options](IODevice& destinationDevice) {
     331            9 :                             return writer->write(notation, destinationDevice, options);
     332              :                         } });
     333            9 :             }
     334              :         }
     335            5 :     } break;
     336           40 :     case INotationWriter::UnitType::PER_PART: {
     337          102 :         for (const INotationPtr& notation : notations) {
     338              :             muse::io::path_t definitivePath = isCreatingOnlyOneFile
     339           62 :                                               ? destinationPath
     340           62 :                                               : completeExportPath(destinationPath, notation, isMainNotation(
     341           62 :                                                                        notation), isExportingOnlyOneScore);
     342              : 
     343          124 :             files.push_back({ definitivePath, [writer, notation, options](IODevice& destinationDevice) {
     344           52 :                         return writer->write(notation, destinationDevice, options);
     345              :                     } });
     346           62 :         }
     347           40 :     } break;
     348            1 :     case INotationWriter::UnitType::MULTI_PART: {
     349            2 :         files.push_back({ destinationPath, [writer, notations, options](IODevice& destinationDevice) {
     350            1 :                     return writer->writeList(notations, destinationDevice, options);
     351              :                 } });
     352            1 :     } break;
     353              :     }
     354              : 
     355           46 :     return files;
     356          236 : }
     357              : 
     358           46 : bool ExportProjectScenario::isMainNotation(INotationPtr notation) const
     359              : {
     360           46 :     return masterNotation()->notation() == notation;
     361              : }
     362              : 
     363          142 : IMasterNotationPtr ExportProjectScenario::masterNotation() const
     364              : {
     365          142 :     return context()->masterNotation();
     366              : }
     367              : 
     368           47 : muse::io::path_t ExportProjectScenario::completeExportPath(const muse::io::path_t& basePath, INotationPtr notation, bool isMain,
     369              :                                                            bool isExportingOnlyOneScore, int pageIndex) const
     370              : {
     371           47 :     muse::io::path_t result = io::dirpath(basePath) + "/" + io::completeBasename(basePath);
     372              : 
     373           47 :     if (!isMain && !isExportingOnlyOneScore) {
     374           24 :         result += "-" + io::escapeFileName(notation->name()).toStdString();
     375              :     }
     376              : 
     377           47 :     if (pageIndex > -1) {
     378            7 :         result += "-" + std::to_string(pageIndex + 1);
     379              :     }
     380              : 
     381           47 :     result += "." + io::suffix(basePath);
     382              : 
     383           47 :     return result;
     384            0 : }
     385              : 
     386           60 : void ExportProjectScenario::exportNextFiles(const ExportStatePtr& state) const
     387              : {
     388          113 :     while (state->currentFileIndex < state->files.size()) {
     389           78 :         const ExportFile& file = state->files.at(state->currentFileIndex);
     390              : 
     391           78 :         if (!state->isCurrentFileConfirmed && fileSystem()->exists(file.path)) {
     392           15 :             if (state->fileConflictPolicy == FileConflictPolicy::Undefined) {
     393           11 :                 askForReplaceFile(state);
     394           25 :                 return;
     395              :             }
     396              : 
     397            4 :             if (state->fileConflictPolicy == FileConflictPolicy::SkipAll) {
     398            1 :                 state->moveToNextFile();
     399           53 :                 continue;
     400              :             }
     401              :         }
     402              : 
     403           66 :         Ret ret = writeFile(file);
     404           66 :         if (ret) {
     405           52 :             state->moveToNextFile();
     406           52 :             continue;
     407              :         }
     408              : 
     409           14 :         if (ret.code() == static_cast<int>(Ret::Code::Cancel)) {
     410            7 :             finishExport(state, ret);
     411            7 :             return;
     412              :         }
     413              : 
     414            7 :         askForRetry(state);
     415            7 :         return;
     416           66 :     }
     417              : 
     418           35 :     finishExport(state, muse::make_ok());
     419              : }
     420              : 
     421           45 : void ExportProjectScenario::finishExport(const ExportStatePtr& state, const Ret& ret) const
     422              : {
     423              :     // Restore view modes
     424           45 :     setViewModes(state->notations, state->viewModes);
     425              : 
     426           45 :     if (state->writerProgress) {
     427            3 :         m_exportProgress.finish(muse::make_ok());
     428            3 :         state->writerProgress->progressChanged().disconnect(this);
     429            3 :         m_exportProgress.canceled().disconnect(this);
     430              :     }
     431              : 
     432           45 :     if (ret && state->openDestinationFolderOnExport) {
     433            1 :         openFolder(state->destinationPath);
     434              :     }
     435              : 
     436           45 :     (void)state->resolve(ret);
     437              : 
     438           45 :     startNextExportLater();
     439           45 : }
     440              : 
     441           11 : void ExportProjectScenario::askForReplaceFile(const ExportStatePtr& state) const
     442              : {
     443           11 :     constexpr int Replace = static_cast<int>(IInteractive::Button::CustomButton) + 1;
     444           11 :     constexpr int ReplaceAll = static_cast<int>(IInteractive::Button::CustomButton) + 2;
     445           11 :     constexpr int Skip = static_cast<int>(IInteractive::Button::CustomButton) + 3;
     446           11 :     constexpr int SkipAll = static_cast<int>(IInteractive::Button::CustomButton) + 4;
     447              : 
     448           11 :     const String filename = FileInfo(state->files.at(state->currentFileIndex).path).fileName();
     449              : 
     450          110 :     interactive()->question(
     451           22 :         muse::trc("project/export", "File already exists"),
     452           11 :         muse::qtrc("project/export", "A file already exists with the filename %1. Do you want to replace it?")
     453           22 :         .arg(filename).toStdString(), {
     454           11 :         IInteractive::ButtonData(Replace, muse::trc("project/export", "Replace")),
     455           11 :         IInteractive::ButtonData(ReplaceAll, muse::trc("project/export", "Replace all")),
     456           11 :         IInteractive::ButtonData(Skip, muse::trc("project/export", "Skip")),
     457           11 :         IInteractive::ButtonData(SkipAll, muse::trc("project/export", "Skip all"))
     458              :     })
     459           11 :     .onResolve(this, [this, state](const IInteractive::Result& result) {
     460           10 :         switch (result.button()) {
     461            1 :             case ReplaceAll:
     462            1 :                 state->fileConflictPolicy = FileConflictPolicy::ReplaceAll; // fallthrough
     463            5 :             case Replace:
     464            5 :                 state->isCurrentFileConfirmed = true;
     465            5 :                 break;
     466            3 :             case SkipAll:
     467            3 :                 state->fileConflictPolicy = FileConflictPolicy::SkipAll; // fallthrough
     468            5 :             case Skip:
     469              :             default:
     470            5 :                 state->moveToNextFile();
     471            5 :                 break;
     472              :         }
     473              : 
     474           10 :         continueExportLater(state);
     475           10 :     });
     476           22 : }
     477              : 
     478            7 : void ExportProjectScenario::askForRetry(const ExportStatePtr& state) const
     479              : {
     480            7 :     const String filename = FileInfo(state->files.at(state->currentFileIndex).path).fileName();
     481              : 
     482           28 :     interactive()->question(
     483           14 :         muse::trc("project/export", "Error"),
     484            7 :         muse::qtrc("project/export", "An error occurred while writing the file %1. Do you want to retry?")
     485           14 :         .arg(filename).toStdString(), { IInteractive::Button::Retry, IInteractive::Button::Abort })
     486            7 :     .onResolve(this, [this, state](const IInteractive::Result& result) {
     487            7 :         if (result.standardButton() != IInteractive::Button::Retry) {
     488            3 :             finishExport(state, make_ret(Ret::Code::Cancel));
     489            3 :             return;
     490              :         }
     491              : 
     492            4 :         state->isCurrentFileConfirmed = true;
     493            4 :         continueExportLater(state);
     494              :     });
     495            7 : }
     496              : 
     497           14 : void ExportProjectScenario::continueExportLater(const ExportStatePtr& state) const
     498              : {
     499              :     //! NOTE The writers may process events deep inside (e.g. the audio export),
     500              :     //! which can't be done neither in Async's callback nor while the question dialog is being closed,
     501              :     //! so continue the export on the next event loop
     502           14 :     QTimer::singleShot(0, &m_timersContext, [this, state]() {
     503           14 :         exportNextFiles(state);
     504           14 :     });
     505           14 : }
     506              : 
     507           66 : Ret ExportProjectScenario::writeFile(const ExportFile& file) const
     508              : {
     509              :     //! NOTE Most writers stream to the destination device (file on disk).
     510              :     //! But there is one atypical case:
     511              :     //! Export score to unpacked directory - creates a directory and writes files to it
     512              : 
     513              :     // Remove previous file because we need to know whether
     514              :     // exportFunction writes at path
     515           66 :     Ret ret = fileSystem()->remove(file.path);
     516           66 :     if (!ret) {
     517            2 :         return ret;
     518              :     }
     519              : 
     520           64 :     FileStream outputFile(file.path);
     521          128 :     outputFile.setMeta("file_path", file.path.toStdString());
     522           64 :     if (!outputFile.open(IODevice::WriteOnly)) {
     523            2 :         return make_ret(Ret::Code::UnknownError);
     524              :     }
     525              : 
     526           62 :     ret = file.exportFunction(outputFile);
     527           62 :     outputFile.close();
     528              : 
     529           62 :     if (ret.code() == static_cast<int>(Ret::Code::Cancel)) {
     530            7 :         const bool isFileMode = fileSystem()->exists(file.path);
     531            7 :         if (!isFileMode) {
     532            5 :             return ret;
     533              :         }
     534              : 
     535              :         // On Windows, remove() may fail immediately after close()
     536              :         // because the file lock has not been released yet
     537            3 :         for (int i = 0; i < 10; ++i) {
     538            3 :             if (fileSystem()->remove(file.path)) {
     539            2 :                 return ret;
     540              :             }
     541              : 
     542            1 :             std::this_thread::sleep_for(std::chrono::milliseconds(100));
     543              :         }
     544              :     }
     545              : 
     546           55 :     return ret;
     547           66 : }
     548              : 
     549            3 : void ExportProjectScenario::showExportProgress(bool isAudioExport) const
     550              : {
     551            3 :     std::string title = isAudioExport ? muse::trc("project/export", "Exporting audio…") : muse::trc("project/export", "Exporting…");
     552              : 
     553            3 :     interactive()->showProgress(title, m_exportProgress);
     554            3 : }
     555              : 
     556            1 : void ExportProjectScenario::openFolder(const muse::io::path_t& path) const
     557              : {
     558            1 :     Ret ret = platformInteractive()->revealInFileBrowser(path.toQString());
     559              : 
     560            1 :     if (!ret) {
     561            1 :         LOGE() << "Could not open folder: " << path.toQString();
     562              :     }
     563            1 : }
     564              : 
     565           46 : std::vector<ViewMode> ExportProjectScenario::viewModes(const notation::INotationPtrList& notations) const
     566              : {
     567           46 :     std::vector<ViewMode> modes;
     568          117 :     for (const INotationPtr& notation : notations) {
     569           71 :         modes.push_back(notation->painting()->viewMode());
     570              :     }
     571              : 
     572           46 :     return modes;
     573            0 : }
     574              : 
     575           91 : void ExportProjectScenario::setViewModes(const notation::INotationPtrList& notations,
     576              :                                          const std::vector<notation::ViewMode>& viewModes) const
     577              : {
     578           91 :     IF_ASSERT_FAILED(notations.size() == viewModes.size()) {
     579            0 :         return;
     580              :     }
     581              : 
     582          231 :     for (size_t i = 0; i < notations.size(); ++i) {
     583          140 :         notations[i]->painting()->setViewMode(viewModes[i]);
     584              :     }
     585              : }
     586              : 
     587           46 : void ExportProjectScenario::setViewModes(const notation::INotationPtrList& notations, ViewMode viewMode) const
     588              : {
     589           46 :     std::vector<notation::ViewMode> modes(notations.size(), viewMode);
     590           46 :     setViewModes(notations, modes);
     591           46 : }
        

Generated by: LCOV version 2.0-1