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) 2025 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 "appupdatemodel.h"
24 :
25 : #include "translation.h"
26 : #include "log.h"
27 :
28 : using namespace muse::update;
29 :
30 0 : AppUpdateModel::AppUpdateModel(QObject* parent)
31 0 : : QObject(parent), Contextable(muse::iocCtxForQmlObject(this))
32 : {
33 0 : setProgressTitle(muse::qtrc("update", "Updating %1").arg(application()->title().toQString()));
34 0 : }
35 :
36 0 : AppUpdateModel::~AppUpdateModel()
37 : {
38 0 : if (m_progress.isStarted()) {
39 0 : m_progress.cancel();
40 : }
41 0 : }
42 :
43 0 : void AppUpdateModel::load(const QString& mode)
44 : {
45 0 : if (mode != "download") {
46 0 : return;
47 : }
48 :
49 0 : RetVal<Progress> progress = service()->downloadRelease();
50 0 : if (!progress.ret) {
51 0 : LOGE() << progress.ret.toString();
52 0 : emit finished(progress.ret.code(), QString::fromStdString(progress.ret.text()), QString());
53 0 : return;
54 : }
55 :
56 0 : m_progress = progress.val;
57 0 : emit started();
58 :
59 0 : const RetVal<ReleaseInfo>& info = service()->lastCheckResult();
60 :
61 : //: Means that the download is currently in progress.
62 : //: %1 will be replaced by the app name, %2 by the version number of the version that is being downloaded.
63 0 : setProgressTitle(muse::qtrc("update", "Downloading %1 %2")
64 0 : .arg(application()->title().toQString(), QString::fromStdString(info.val.version)));
65 :
66 0 : m_progress.progressChanged().onReceive(this, [this](int64_t current, int64_t total, const std::string& msg) {
67 0 : if (!msg.empty()) {
68 0 : setProgressTitle(QString::fromStdString(msg));
69 : }
70 :
71 0 : setCurrentProgress(current);
72 0 : setTotalProgress(total);
73 0 : });
74 :
75 0 : m_progress.finished().onReceive(this, [this](const ProgressResult& res) {
76 0 : if (!res.ret) {
77 0 : LOGE() << res.ret.toString();
78 : }
79 :
80 0 : emit finished(res.ret.code(), QString::fromStdString(res.ret.text()), res.val.toQString());
81 0 : });
82 0 : }
83 :
84 0 : int AppUpdateModel::currentProgress() const
85 : {
86 0 : return m_currentProgress;
87 : }
88 :
89 0 : int AppUpdateModel::totalProgress() const
90 : {
91 0 : return m_totalProgress;
92 : }
93 :
94 0 : QString AppUpdateModel::progressTitle() const
95 : {
96 0 : return m_progressTitle;
97 : }
98 :
99 0 : void AppUpdateModel::setCurrentProgress(int progress)
100 : {
101 0 : if (m_currentProgress == progress) {
102 0 : return;
103 : }
104 :
105 0 : m_currentProgress = progress;
106 0 : emit currentProgressChanged();
107 : }
108 :
109 0 : void AppUpdateModel::setTotalProgress(int progress)
110 : {
111 0 : if (m_totalProgress == progress) {
112 0 : return;
113 : }
114 :
115 0 : m_totalProgress = progress;
116 0 : emit totalProgressChanged();
117 : }
118 :
119 0 : void AppUpdateModel::setProgressTitle(const QString& title)
120 : {
121 0 : if (m_progressTitle == title) {
122 0 : return;
123 : }
124 :
125 0 : m_progressTitle = title;
126 0 : emit progressTitleChanged();
127 : }
|