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 "sessionsmanager.h"
24 :
25 : #include "project/inotationproject.h"
26 : #include "project/projectcommands.h"
27 :
28 : using namespace mu::appshell;
29 : using namespace muse;
30 :
31 0 : void SessionsManager::init()
32 : {
33 0 : update();
34 :
35 0 : projectSession()->projectChanged().onNotify(this, [this]() {
36 0 : update();
37 :
38 0 : if (project::INotationProjectPtr project = projectSession()->project()) {
39 0 : project->pathChanged().onNotify(this, [this]() {
40 0 : update();
41 0 : });
42 0 : }
43 0 : });
44 0 : }
45 :
46 0 : void SessionsManager::deinit()
47 : {
48 0 : bool isLastContext = multiwindowsProvider()->windowCount() == 1;
49 0 : if (!isLastContext) {
50 0 : return;
51 : }
52 :
53 0 : if (configuration()->startupModeType() != StartupModeType::ContinueLastSession) {
54 0 : reset();
55 : }
56 : }
57 :
58 0 : bool SessionsManager::hasProjectsForRestore()
59 : {
60 0 : return !configuration()->sessionProjectsPaths().empty();
61 : }
62 :
63 0 : void SessionsManager::restore()
64 : {
65 0 : io::paths_t projects = configuration()->sessionProjectsPaths();
66 0 : if (projects.empty()) {
67 0 : return;
68 : }
69 :
70 0 : for (const muse::io::path_t& path : projects) {
71 0 : commandDispatcher()->dispatch(project::PROJECT_OPEN_COMMAND, { { "url", muse::Val(path.toQUrl().toString()) } });
72 : }
73 0 : }
74 :
75 0 : void SessionsManager::reset()
76 : {
77 0 : configuration()->setSessionProjectsPaths({});
78 0 : }
79 :
80 0 : void SessionsManager::update()
81 : {
82 0 : muse::io::path_t newProjectPath;
83 :
84 0 : if (auto project = projectSession()->project()) {
85 0 : newProjectPath = project->isNewlyCreated() ? projectConfiguration()->newProjectTemporaryPath() : project->path();
86 0 : }
87 :
88 0 : if (newProjectPath == m_lastOpenedProjectPath) {
89 0 : return;
90 : }
91 :
92 0 : if (!m_lastOpenedProjectPath.empty()) {
93 0 : removeProjectFromSession(m_lastOpenedProjectPath);
94 : }
95 :
96 0 : if (!newProjectPath.empty()) {
97 0 : addProjectToSession(newProjectPath);
98 : }
99 :
100 0 : m_lastOpenedProjectPath = newProjectPath;
101 0 : }
102 :
103 0 : void SessionsManager::removeProjectFromSession(const muse::io::path_t& projectPath)
104 : {
105 0 : io::paths_t projects = configuration()->sessionProjectsPaths();
106 0 : if (projects.empty()) {
107 0 : return;
108 : }
109 :
110 0 : projects.erase(std::remove(projects.begin(), projects.end(), projectPath), projects.end());
111 0 : configuration()->setSessionProjectsPaths(projects);
112 0 : }
113 :
114 0 : void SessionsManager::addProjectToSession(const muse::io::path_t& projectPath)
115 : {
116 0 : io::paths_t projects = configuration()->sessionProjectsPaths();
117 :
118 0 : if (std::find(projects.begin(), projects.end(), projectPath) == projects.end()) {
119 0 : projects.push_back(projectPath);
120 : }
121 :
122 0 : configuration()->setSessionProjectsPaths(projects);
123 0 : }
|