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) 2026 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 : #pragma once
23 :
24 : #include <QNetworkReply>
25 : #include <QNetworkAccessManager>
26 : #include <QTimer>
27 : #include <QUrl>
28 :
29 : namespace muse::network {
30 : //! NOTE Local urls (qrc, file, data) are not a network access, they work in offline mode
31 0 : inline bool isNetworkUrl(const QUrl& url)
32 : {
33 0 : const QString scheme = url.scheme().toLower();
34 0 : return scheme == "http" || scheme == "https" || scheme == "ftp" || scheme == "ws" || scheme == "wss";
35 0 : }
36 :
37 : //! NOTE The reply to a request in offline mode: fails as a network error,
38 : //! asynchronously as a real request does
39 : class OfflineNetworkReply : public QNetworkReply
40 : {
41 : public:
42 0 : OfflineNetworkReply(QNetworkAccessManager::Operation operation, const QNetworkRequest& request, QObject* parent = nullptr)
43 0 : : QNetworkReply(parent)
44 : {
45 0 : setOperation(operation);
46 0 : setRequest(request);
47 0 : setUrl(request.url());
48 0 : setOpenMode(QIODevice::ReadOnly);
49 0 : setError(QNetworkReply::UnknownNetworkError, QStringLiteral("Network access is disabled (offline mode)"));
50 :
51 0 : QTimer::singleShot(0, this, [this]() {
52 0 : setFinished(true);
53 0 : emit errorOccurred(error());
54 0 : emit finished();
55 0 : });
56 0 : }
57 :
58 0 : void abort() override {}
59 :
60 : protected:
61 0 : qint64 readData(char*, qint64) override { return -1; }
62 : };
63 : }
|