LCOV - code coverage report
Current view: top level - muse/framework/workspace/internal - workspacemanager.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 43.9 % 244 107
Test Date: 2026-11-19 04:14:36

            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              : #include "workspacemanager.h"
      23              : 
      24              : #include "types/uri.h"
      25              : 
      26              : #include "log.h"
      27              : 
      28              : using namespace muse;
      29              : using namespace muse::workspace;
      30              : 
      31              : static const std::string WORKSPACE_EXT(".mws");
      32              : 
      33            0 : static bool containsWorkspace(const IWorkspacePtrList& list, const IWorkspacePtr& workspace)
      34              : {
      35            0 :     return std::find(list.cbegin(), list.cend(), workspace) != list.cend();
      36              : }
      37              : 
      38            3 : void WorkspaceManager::init()
      39              : {
      40            3 :     configuration()->currentWorkspaceNameChanged().onReceive(this, [this](const std::string&) {
      41            0 :         setupCurrentWorkspace();
      42            0 :     });
      43              : 
      44            3 :     load();
      45            3 : }
      46              : 
      47            3 : void WorkspaceManager::deinit()
      48              : {
      49            3 :     saveCurrentWorkspace();
      50              : 
      51            3 :     m_currentWorkspace = nullptr;
      52            3 :     m_defaultWorkspace = nullptr;
      53            3 :     m_workspaces.clear();
      54            3 : }
      55              : 
      56            0 : bool WorkspaceManager::isInited() const
      57              : {
      58            0 :     return m_defaultWorkspace != nullptr;
      59              : }
      60              : 
      61            4 : IWorkspacePtr WorkspaceManager::defaultWorkspace() const
      62              : {
      63            4 :     return m_defaultWorkspace;
      64              : }
      65              : 
      66            5 : IWorkspacePtr WorkspaceManager::currentWorkspace() const
      67              : {
      68            5 :     return m_currentWorkspace;
      69              : }
      70              : 
      71            0 : async::Notification WorkspaceManager::currentWorkspaceAboutToBeChanged() const
      72              : {
      73            0 :     return m_currentWorkspaceAboutToBeChanged;
      74              : }
      75              : 
      76            0 : async::Notification WorkspaceManager::currentWorkspaceChanged() const
      77              : {
      78            0 :     return m_currentWorkspaceChanged;
      79              : }
      80              : 
      81            2 : IWorkspacePtrList WorkspaceManager::workspaces() const
      82              : {
      83            2 :     IWorkspacePtrList iworkspaces;
      84            2 :     iworkspaces.reserve(m_workspaces.size());
      85            4 :     for (const WorkspacePtr& w : m_workspaces) {
      86            2 :         iworkspaces.push_back(w);
      87              :     }
      88            2 :     return iworkspaces;
      89            0 : }
      90              : 
      91            0 : Ret WorkspaceManager::setWorkspaces(const IWorkspacePtrList& workspaces)
      92              : {
      93            0 :     Ret ret = removeMissingWorkspaces(workspaces);
      94              : 
      95            0 :     if (ret) {
      96            0 :         ret = addNonExistentWorkspaces(workspaces);
      97              :     }
      98              : 
      99            0 :     m_workspacesListChanged.notify();
     100              : 
     101            0 :     return ret;
     102            0 : }
     103              : 
     104            0 : async::Notification WorkspaceManager::workspacesListChanged() const
     105              : {
     106            0 :     return m_workspacesListChanged;
     107              : }
     108              : 
     109            0 : IWorkspacePtr WorkspaceManager:: cloneWorkspace(const IWorkspacePtr& workspace, const std::string& newWorkspaceName) const
     110              : {
     111            0 :     return std::make_shared<Workspace>(makeNewWorkspacePath(newWorkspaceName), dynamic_cast<Workspace*>(workspace.get()), iocContext());
     112              : }
     113              : 
     114            0 : void WorkspaceManager::changeCurrentWorkspace(const std::string& newWorkspaceName)
     115              : {
     116            0 :     prepareCurrentWorkspaceForChange();
     117            0 :     doChangeCurrentWorkspace(newWorkspaceName);
     118            0 : }
     119              : 
     120            0 : void WorkspaceManager::doChangeCurrentWorkspace(const std::string& newWorkspaceName)
     121              : {
     122            0 :     if (configuration()->currentWorkspaceName() == newWorkspaceName || newWorkspaceName.empty()) {
     123            0 :         return;
     124              :     }
     125              : 
     126            0 :     configuration()->setCurrentWorkspaceName(newWorkspaceName);
     127              : }
     128              : 
     129            0 : void WorkspaceManager::createAndAppendNewWorkspace()
     130              : {
     131            0 :     prepareCurrentWorkspaceForChange();
     132              : 
     133            0 :     QStringList workspaceNames;
     134            0 :     for (const IWorkspacePtr& workspace: workspaces()) {
     135            0 :         workspaceNames << QString::fromStdString(workspace->name());
     136            0 :     }
     137              : 
     138            0 :     UriQuery uri("muse://workspace/create");
     139            0 :     uri.addParam("workspaceNames", Val(workspaceNames.join(',')));
     140              : 
     141            0 :     interactive()->open(uri).onResolve(this, [this](const Val& val) {
     142            0 :         QVariantMap meta = val.toQVariant().toMap();
     143            0 :         QString name = meta.value("name").toString();
     144            0 :         IF_ASSERT_FAILED(!name.isEmpty()) {
     145            0 :             return;
     146              :         }
     147              : 
     148            0 :         IWorkspacePtr newWorkspace = cloneWorkspace(currentWorkspace(), name.toStdString());
     149            0 :         if (!newWorkspace) {
     150            0 :             return;
     151              :         }
     152              : 
     153            0 :         IWorkspacePtrList workspaces = this->workspaces();
     154            0 :         workspaces.emplace_back(newWorkspace);
     155              : 
     156            0 :         setWorkspaces(workspaces);
     157              : 
     158            0 :         configuration()->setCurrentWorkspaceName(name.toStdString());
     159            0 :     });
     160            0 : }
     161              : 
     162            0 : void WorkspaceManager::openConfigureWorkspacesDialog()
     163              : {
     164            0 :     prepareCurrentWorkspaceForChange();
     165              : 
     166            0 :     interactive()->open("muse://workspace/select").onResolve(this, [this](const Val& val) {
     167            0 :         doChangeCurrentWorkspace(val.toString());
     168            0 :     });
     169            0 : }
     170              : 
     171            0 : WorkspacePtr WorkspaceManager::doNewWorkspace(const std::string& workspaceName) const
     172              : {
     173            0 :     return std::make_shared<Workspace>(makeNewWorkspacePath(workspaceName), iocContext());
     174              : }
     175              : 
     176            0 : io::path_t WorkspaceManager::makeNewWorkspacePath(const std::string& workspaceName) const
     177              : {
     178            0 :     return configuration()->userWorkspacesPath() + "/" + workspaceName + WORKSPACE_EXT;
     179              : }
     180              : 
     181            4 : void WorkspaceManager::appendNewWorkspace(WorkspacePtr workspace)
     182              : {
     183            4 :     setupConnectionsToNewWorkspace(workspace);
     184            4 :     m_workspaces.push_back(workspace);
     185            4 : }
     186              : 
     187            4 : void WorkspaceManager::setupConnectionsToNewWorkspace(const IWorkspacePtr workspace)
     188              : {
     189            4 :     std::string newWorkspaceName = workspace->name();
     190            4 :     workspace->reloadNotification().onNotify(this, [this, newWorkspaceName](){
     191            0 :         if (m_currentWorkspace->name() == newWorkspaceName) {
     192            0 :             m_currentWorkspaceChanged.notify();
     193              :         }
     194            0 :     });
     195            4 : }
     196              : 
     197            0 : Ret WorkspaceManager::removeMissingWorkspaces(const IWorkspacePtrList& newWorkspaceList)
     198              : {
     199            0 :     IWorkspacePtrList oldWorkspaceList = workspaces();
     200            0 :     for (const IWorkspacePtr& oldWorkspace : oldWorkspaceList) {
     201            0 :         if (containsWorkspace(newWorkspaceList, oldWorkspace)) {
     202            0 :             continue;
     203              :         }
     204              : 
     205            0 :         Ret ret = removeWorkspace(oldWorkspace);
     206            0 :         if (!ret) {
     207            0 :             return ret;
     208              :         }
     209            0 :     }
     210              : 
     211            0 :     if (!containsWorkspace(newWorkspaceList, m_currentWorkspace)) {
     212            0 :         m_currentWorkspace = nullptr;
     213              :     }
     214              : 
     215            0 :     return make_ret(Ret::Code::Ok);
     216            0 : }
     217              : 
     218            0 : Ret WorkspaceManager::removeWorkspace(const IWorkspacePtr& workspace)
     219              : {
     220            0 :     if (!canRemoveWorkspace(workspace)) {
     221            0 :         return make_ret(Ret::Code::Ok);
     222              :     }
     223              : 
     224            0 :     std::string workspaceName = workspace->name();
     225            0 :     for (auto it = m_workspaces.begin(); it != m_workspaces.end(); ++it) {
     226            0 :         if (it->get()->name() == workspaceName) {
     227            0 :             Ret ret = fileSystem()->remove(it->get()->filePath());
     228            0 :             if (ret) {
     229            0 :                 m_workspaces.erase(it);
     230              :             }
     231              : 
     232            0 :             return ret;
     233            0 :         }
     234              :     }
     235              : 
     236            0 :     return make_ret(Ret::Code::Ok);
     237            0 : }
     238              : 
     239            0 : bool WorkspaceManager::canRemoveWorkspace(const IWorkspacePtr& workspace) const
     240              : {
     241            0 :     return !workspace->isBuiltin();
     242              : }
     243              : 
     244            0 : Ret WorkspaceManager::addNonExistentWorkspaces(const IWorkspacePtrList& newWorkspaceList)
     245              : {
     246            0 :     IWorkspacePtrList existentWorkspaces = workspaces();
     247            0 :     for (const IWorkspacePtr& workspace : newWorkspaceList) {
     248            0 :         if (containsWorkspace(existentWorkspaces, workspace)) {
     249            0 :             continue;
     250              :         }
     251              : 
     252            0 :         Ret ret = addWorkspace(workspace);
     253            0 :         if (!ret) {
     254            0 :             return ret;
     255              :         }
     256            0 :     }
     257              : 
     258            0 :     return make_ret(Ret::Code::Ok);
     259            0 : }
     260              : 
     261            0 : Ret WorkspaceManager::addWorkspace(IWorkspacePtr workspace)
     262              : {
     263            0 :     auto writable = std::dynamic_pointer_cast<Workspace>(workspace);
     264            0 :     if (!writable) {
     265            0 :         return make_ret(Ret::Code::Ok);
     266              :     }
     267              : 
     268            0 :     Ret ret = writable->save();
     269              : 
     270            0 :     if (ret) {
     271            0 :         appendNewWorkspace(writable);
     272              :     }
     273              : 
     274            0 :     return ret;
     275            0 : }
     276              : 
     277            3 : void WorkspaceManager::load()
     278              : {
     279            3 :     reloadWorkspaceFiles();
     280              : 
     281            3 :     setupDefaultWorkspace();
     282            3 :     setupCurrentWorkspace();
     283            3 : }
     284              : 
     285            4 : void WorkspaceManager::reloadWorkspaceFiles()
     286              : {
     287            4 :     m_workspaces.clear();
     288              : 
     289            4 :     io::paths_t files = findWorkspaceFiles();
     290            8 :     for (const io::path_t& file : files) {
     291            4 :         auto workspace = std::make_shared<Workspace>(file, iocContext());
     292            4 :         appendNewWorkspace(workspace);
     293            4 :     }
     294              : 
     295            4 :     m_workspacesListChanged.notify();
     296            4 : }
     297              : 
     298            4 : io::paths_t WorkspaceManager::findWorkspaceFiles() const
     299              : {
     300            4 :     auto findFiles = [this](const io::path_t& dirPath) {
     301            4 :         std::string filter = "*" + WORKSPACE_EXT;
     302           16 :         RetVal<io::paths_t> files = fileSystem()->scanFiles(dirPath, { filter });
     303            4 :         if (!files.ret) {
     304            0 :             LOGE() << files.ret.toString();
     305              :         }
     306              : 
     307            8 :         return files.val;
     308            8 :     };
     309              : 
     310            4 :     io::paths_t builtinWorkspacesPaths = configuration()->builtinWorkspacesFilePaths();
     311              : 
     312            4 :     io::paths_t userWorkspacesPaths = findFiles(configuration()->userWorkspacesPath());
     313            4 :     std::set<io::path_t> userWorkspacesFileNames;
     314            4 :     for (const io::path_t& dirPath : userWorkspacesPaths) {
     315            0 :         userWorkspacesFileNames.insert(io::filename(dirPath));
     316              :     }
     317              : 
     318            4 :     muse::remove_if(builtinWorkspacesPaths, [=](const io::path_t& path) -> bool {
     319            4 :         io::path_t fileName = io::filename(path);
     320            8 :         return muse::contains(userWorkspacesFileNames, fileName);
     321            4 :     });
     322              : 
     323            4 :     io::paths_t result;
     324            4 :     muse::join(result, builtinWorkspacesPaths);
     325            4 :     muse::join(result, userWorkspacesPaths);
     326              : 
     327            4 :     return result;
     328            4 : }
     329              : 
     330            3 : void WorkspaceManager::setupDefaultWorkspace()
     331              : {
     332            3 :     std::string defaultWorkspaceName = configuration()->defaultWorkspaceName();
     333            3 :     WorkspacePtr workspace = findAndInit(defaultWorkspaceName);
     334            3 :     if (workspace) {
     335            3 :         m_defaultWorkspace = workspace;
     336            3 :         return;
     337              :     }
     338              : 
     339            0 :     LOGW() << "not found default workspace, will be created new";
     340              : 
     341            0 :     m_defaultWorkspace = doNewWorkspace(defaultWorkspaceName);
     342            0 :     m_workspaces.push_back(m_defaultWorkspace);
     343              : 
     344            0 :     Ret ret = fileSystem()->makePath(configuration()->userWorkspacesPath());
     345            0 :     if (!ret) {
     346            0 :         LOGE() << ret.toString();
     347            0 :         return;
     348              :     }
     349              : 
     350            0 :     ret = m_defaultWorkspace->load();
     351            0 :     if (!ret) {
     352            0 :         LOGE() << ret.toString();
     353            0 :         return;
     354              :     }
     355            6 : }
     356              : 
     357            3 : void WorkspaceManager::setupCurrentWorkspace()
     358              : {
     359            3 :     std::string workspaceName = configuration()->currentWorkspaceName();
     360            3 :     if (m_currentWorkspace && m_currentWorkspace->isLoaded()) {
     361            0 :         if (m_currentWorkspace->name() == workspaceName) {
     362            0 :             return;
     363              :         }
     364              : 
     365            0 :         saveCurrentWorkspace();
     366              : 
     367              :         //! NOTE Perhaps we need to unload the current workspace (clear memory)
     368              :     }
     369              : 
     370            3 :     WorkspacePtr workspace = findAndInit(workspaceName);
     371            3 :     if (!workspace) {
     372              :         //! NOTE The workspace may have been created by another context/process
     373              :         //! and saved to disk. Try reloading the file list before falling back.
     374            1 :         reloadWorkspaceFiles();
     375            1 :         workspace = findAndInit(workspaceName);
     376              :     }
     377              : 
     378            3 :     if (!workspace) {
     379            1 :         std::string defaultWorkspaceName = configuration()->defaultWorkspaceName();
     380            1 :         LOGW() << "failed get workspace: " << workspaceName << ", will use " << defaultWorkspaceName;
     381              : 
     382              :         //! NOTE Already should be inited
     383            1 :         IF_ASSERT_FAILED(m_defaultWorkspace) {
     384            0 :             setupDefaultWorkspace();
     385              :         }
     386              : 
     387            1 :         workspace = m_defaultWorkspace;
     388            1 :         configuration()->setCurrentWorkspaceName(defaultWorkspaceName);
     389            1 :     }
     390              : 
     391            3 :     m_currentWorkspace = workspace;
     392            3 :     m_currentWorkspaceChanged.notify();
     393            3 : }
     394              : 
     395            7 : WorkspacePtr WorkspaceManager::findByName(const std::string& name) const
     396              : {
     397            9 :     for (auto workspace : m_workspaces) {
     398            7 :         if (workspace->name() == name) {
     399            5 :             return workspace;
     400              :         }
     401            2 :     }
     402              : 
     403            2 :     return nullptr;
     404              : }
     405              : 
     406            7 : WorkspacePtr WorkspaceManager::findAndInit(const std::string& name) const
     407              : {
     408            7 :     WorkspacePtr workspace = findByName(name);
     409            7 :     if (!workspace) {
     410            2 :         return nullptr;
     411              :     }
     412              : 
     413            5 :     if (!workspace->isLoaded()) {
     414            3 :         Ret ret = workspace->load();
     415            3 :         if (!ret) {
     416            0 :             LOGE() << "failed load workspace: " << name;
     417            0 :             return nullptr;
     418              :         }
     419            3 :     }
     420              : 
     421            5 :     return workspace;
     422            7 : }
     423              : 
     424            3 : void WorkspaceManager::saveCurrentWorkspace()
     425              : {
     426            3 :     if (!m_currentWorkspace) {
     427            0 :         return;
     428              :     }
     429              : 
     430            3 :     Ret ret = m_currentWorkspace->save();
     431            3 :     if (!ret) {
     432            0 :         LOGE() << "failed save current workspace, err: " << ret.toString();
     433              :     }
     434            3 : }
     435              : 
     436            0 : void WorkspaceManager::prepareCurrentWorkspaceForChange()
     437              : {
     438            0 :     m_currentWorkspaceAboutToBeChanged.notify();
     439            0 : }
        

Generated by: LCOV version 2.0-1