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 "startupscenario.h"
24 :
25 : #include <QCoreApplication>
26 :
27 : #include "appshelltypes.h"
28 : #include "translation.h"
29 : #include "types/version.h"
30 :
31 : #include "project/projectcommands.h"
32 :
33 : #include "muse_framework_config.h"
34 :
35 : #include "log.h"
36 :
37 : using namespace mu::appshell;
38 : using namespace muse;
39 :
40 : static const muse::UriQuery FIRST_LAUNCH_SETUP_URI("musescore://firstLaunchSetup?floating=true");
41 : static const muse::UriQuery WELCOME_DIALOG_URI("musescore://welcomedialog");
42 :
43 0 : static StartupModeType modeTypeTromString(const std::string& str)
44 : {
45 0 : if ("start-empty" == str) {
46 0 : return StartupModeType::StartEmpty;
47 : }
48 :
49 0 : if ("continue-last" == str) {
50 0 : return StartupModeType::ContinueLastSession;
51 : }
52 :
53 0 : if ("start-with-new" == str) {
54 0 : return StartupModeType::StartWithNewScore;
55 : }
56 :
57 0 : if ("start-with-file" == str) {
58 0 : return StartupModeType::StartWithScore;
59 : }
60 :
61 0 : return StartupModeType::StartEmpty;
62 : }
63 :
64 0 : static const Uri& startupPageUri(StartupModeType modeType)
65 : {
66 0 : switch (modeType) {
67 0 : case StartupModeType::StartEmpty:
68 : case StartupModeType::StartWithNewScore:
69 : case StartupModeType::Recovery:
70 0 : return HOME_URI;
71 0 : case StartupModeType::StartWithScore:
72 : case StartupModeType::ContinueLastSession:
73 0 : return NOTATION_URI;
74 : }
75 :
76 0 : return HOME_URI;
77 : }
78 :
79 0 : void StartupScenario::setStartupType(const std::optional<std::string>& type)
80 : {
81 0 : m_startupTypeStr = type ? type.value() : "";
82 0 : }
83 :
84 0 : bool StartupScenario::isStartWithNewFileAsSecondaryInstance() const
85 : {
86 0 : if (m_startupScoreFile.isValid()) {
87 0 : return false;
88 : }
89 :
90 0 : if (!m_startupTypeStr.empty()) {
91 0 : return modeTypeTromString(m_startupTypeStr) == StartupModeType::StartWithNewScore;
92 : }
93 :
94 0 : return false;
95 : }
96 :
97 0 : const mu::project::ProjectFile& StartupScenario::startupScoreFile() const
98 : {
99 0 : return m_startupScoreFile;
100 : }
101 :
102 0 : void StartupScenario::setStartupScoreFile(const std::optional<project::ProjectFile>& file)
103 : {
104 0 : m_startupScoreFile = file ? file.value() : project::ProjectFile();
105 0 : }
106 :
107 0 : void StartupScenario::runOnSplashScreen()
108 : {
109 0 : TRACEFUNC;
110 :
111 0 : if (!multiwindowsProvider()->isFirstWindow()) {
112 0 : registerAudioPlugins();
113 0 : return;
114 : }
115 :
116 0 : if (appUpdateScenario() && appUpdateScenario()->needCheckForUpdate()) {
117 0 : appUpdateScenario()->checkForUpdate(/*manual*/ false);
118 : }
119 :
120 0 : if (museSoundsUpdateScenario() && museSoundsUpdateScenario()->needCheckForUpdate()) {
121 0 : museSoundsUpdateScenario()->checkForUpdate(/*manual*/ false);
122 : }
123 :
124 0 : registerAudioPlugins();
125 0 : }
126 :
127 0 : void StartupScenario::registerAudioPlugins()
128 : {
129 0 : if (!registerAudioPluginsScenario()) {
130 0 : return;
131 : }
132 :
133 : //! NOTE Registering plugins shows a window (dialog) before the main window is shown.
134 : //! After closing it, the application may in a state where there are no open windows,
135 : //! which leads to automatic exit from the application.
136 : //! (Thanks to the splashscreen, but this is not an obvious detail)
137 0 : qApp->setQuitLockEnabled(false);
138 0 : registerAudioPluginsScenario()->updatePluginsRegistry();
139 0 : qApp->setQuitLockEnabled(true);
140 : }
141 :
142 0 : muse::async::Promise<muse::Ret> StartupScenario::runAfterSplashScreen()
143 : {
144 0 : TRACEFUNC;
145 :
146 0 : if (m_startupCompleted) {
147 0 : return async::make_promise<Ret>([](auto resolve, auto) {
148 0 : return resolve(muse::make_ok());
149 0 : });
150 : }
151 :
152 0 : m_startupCompleted = true;
153 :
154 0 : StartupModeType modeType = StartupModeType::StartEmpty;
155 0 : if (multiwindowsProvider()->isFirstWindow() && sessionsManager()->hasProjectsForRestore()) {
156 0 : modeType = StartupModeType::Recovery;
157 : } else {
158 0 : modeType = resolveStartupModeType();
159 : }
160 :
161 0 : const Uri startupUri = startupPageUri(modeType);
162 0 : return async::make_promise<Ret>([this, modeType, startupUri](auto resolve, auto) {
163 0 : interactive()->open(startupUri).onResolve(this, [this, modeType, resolve](const Val&) mutable {
164 0 : onStartupPageOpened(modeType);
165 0 : (void)resolve(muse::make_ok());
166 : });
167 0 : return async::Promise<Ret>::dummy_result();
168 0 : }, async::PromiseType::AsyncByBody);
169 0 : }
170 :
171 0 : bool StartupScenario::startupCompleted() const
172 : {
173 0 : return m_startupCompleted;
174 : }
175 :
176 0 : StartupModeType StartupScenario::resolveStartupModeType() const
177 : {
178 0 : if (m_startupScoreFile.isValid()) {
179 0 : return StartupModeType::StartWithScore;
180 : }
181 :
182 0 : if (!m_startupTypeStr.empty()) {
183 0 : return modeTypeTromString(m_startupTypeStr);
184 : }
185 :
186 0 : return configuration()->startupModeType();
187 : }
188 :
189 0 : void StartupScenario::onStartupPageOpened(StartupModeType modeType)
190 : {
191 0 : TRACEFUNC;
192 :
193 0 : switch (modeType) {
194 0 : case StartupModeType::StartEmpty:
195 0 : break;
196 0 : case StartupModeType::StartWithNewScore:
197 0 : commandDispatcher()->dispatch(project::PROJECT_NEW_COMMAND);
198 0 : break;
199 0 : case StartupModeType::ContinueLastSession:
200 0 : commandDispatcher()->dispatch(project::PROJECT_CONTINUE_LAST_SESSION_COMMAND);
201 0 : break;
202 0 : case StartupModeType::Recovery:
203 0 : restoreLastSession();
204 0 : break;
205 0 : case StartupModeType::StartWithScore: {
206 0 : project::ProjectFile file = m_startupScoreFile.isValid()
207 0 : ? m_startupScoreFile
208 0 : : project::ProjectFile(configuration()->startupScorePath());
209 0 : openScore(file);
210 0 : } break;
211 : }
212 :
213 0 : m_activeUpdateCheckCount = 0;
214 :
215 0 : if (museSoundsUpdateScenario() && museSoundsUpdateScenario()->checkInProgress()) {
216 0 : m_activeUpdateCheckCount++;
217 0 : museSoundsUpdateScenario()->checkInProgressChanged().onNotify(this, [this, modeType]() {
218 0 : museSoundsUpdateScenario()->checkInProgressChanged().disconnect(this);
219 0 : m_activeUpdateCheckCount--;
220 0 : showStartupDialogsIfNeed(modeType);
221 0 : }, Asyncable::Mode::SetReplace);
222 : }
223 :
224 0 : showStartupDialogsIfNeed(modeType);
225 0 : }
226 :
227 0 : void StartupScenario::showStartupDialogsIfNeed(StartupModeType modeType)
228 : {
229 0 : TRACEFUNC;
230 :
231 0 : if (m_activeUpdateCheckCount != 0) {
232 0 : return;
233 : }
234 :
235 : //! NOTE: The welcome dialog should not show if the first launch setup has not been completed, or if we're going
236 : //! to show a MuseSounds update dialog (see ProjectActionsController::doFinishOpenProject). MuseSampler's update
237 : //! dialog should be shown after the welcome dialog.
238 0 : const auto showWelcomeDialogAndSamplerUpdateIfNeed = [this, modeType]() {
239 0 : if (!configuration()->hasCompletedFirstLaunchSetup()) {
240 0 : interactive()->open(FIRST_LAUNCH_SETUP_URI);
241 0 : return;
242 : }
243 :
244 0 : const Version welcomeDialogLastShownVersion(configuration()->welcomeDialogLastShownVersion());
245 0 : const Version currentMuseScoreVersion(configuration()->museScoreVersion());
246 0 : if (welcomeDialogLastShownVersion < currentMuseScoreVersion) {
247 0 : configuration()->setWelcomeDialogShowOnStartup(true); // override user preference
248 0 : configuration()->setWelcomeDialogLastShownIndex(-1); // reset
249 : }
250 :
251 0 : const bool shouldCheckForMuseSamplerUpdate = modeType == StartupModeType::StartEmpty
252 0 : || modeType == StartupModeType::StartWithNewScore;
253 :
254 0 : if (shouldShowWelcomeDialog(modeType)) {
255 0 : interactive()->open(WELCOME_DIALOG_URI).onResolve(this, [this, shouldCheckForMuseSamplerUpdate](const Val&) {
256 0 : configuration()->setWelcomeDialogLastShownVersion(configuration()->museScoreVersion());
257 :
258 0 : if (shouldCheckForMuseSamplerUpdate) {
259 0 : checkAndShowMuseSamplerStatusIfNeed();
260 : }
261 0 : });
262 0 : } else if (shouldCheckForMuseSamplerUpdate) {
263 0 : checkAndShowMuseSamplerStatusIfNeed();
264 : }
265 0 : };
266 :
267 0 : showWelcomeDialogAndSamplerUpdateIfNeed();
268 0 : }
269 :
270 0 : bool StartupScenario::shouldShowWelcomeDialog(StartupModeType modeType) const
271 : {
272 0 : if (!configuration()->welcomeDialogShowOnStartup()) {
273 0 : return false;
274 : }
275 :
276 0 : if (!multiwindowsProvider()->isFirstWindow()) {
277 0 : return false;
278 : }
279 :
280 0 : if (museSoundsUpdateScenario() && museSoundsUpdateScenario()->hasUpdate()) {
281 0 : return false;
282 : }
283 :
284 0 : const Uri& startupUri = startupPageUri(modeType);
285 0 : return interactive()->currentUri().val == startupUri;
286 : }
287 :
288 0 : void StartupScenario::checkAndShowMuseSamplerStatusIfNeed()
289 : {
290 0 : if (museSamplerCheckStatusScenario() && !museSamplerCheckStatusScenario()->alreadyChecked()) {
291 0 : museSamplerCheckStatusScenario()->checkAndShowStatusIfNeed();
292 : }
293 0 : }
294 :
295 0 : void StartupScenario::openScore(const project::ProjectFile& file)
296 : {
297 0 : commandDispatcher()->dispatch(project::PROJECT_OPEN_COMMAND,
298 0 : { { "url", muse::Val(file.url.toString()) }, { "display_name", muse::Val(file.displayNameOverride) } });
299 0 : }
300 :
301 0 : void StartupScenario::restoreLastSession()
302 : {
303 0 : auto promise = interactive()->question(muse::trc("appshell", "The previous session quit unexpectedly."),
304 0 : muse::trc("appshell", "Do you want to restore the session?"),
305 0 : { IInteractive::Button::No, IInteractive::Button::Yes });
306 :
307 0 : promise.onResolve(this, [this](const IInteractive::Result& res) {
308 0 : if (res.isButton(IInteractive::Button::Yes)) {
309 0 : sessionsManager()->restore();
310 : } else {
311 0 : removeProjectsUnsavedChanges(configuration()->sessionProjectsPaths());
312 0 : sessionsManager()->reset();
313 0 : checkAndShowMuseSamplerStatusIfNeed();
314 : }
315 0 : });
316 0 : }
317 :
318 0 : void StartupScenario::removeProjectsUnsavedChanges(const io::paths_t& projectsPaths)
319 : {
320 0 : for (const muse::io::path_t& path : projectsPaths) {
321 0 : projectAutoSaver()->removeProjectUnsavedChanges(path);
322 : }
323 0 : }
|