MuseScore  3.4
Music composition and notation
loginmanager_p.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2019 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __LOGINMANAGER_P_H__
21 #define __LOGINMANAGER_P_H__
22 
23 #include "config.h"
24 
25 namespace Ms {
26 
27 //---------------------------------------------------------
28 // ApiInfo
29 //---------------------------------------------------------
30 
31 class ApiInfo
32  {
33  static ApiInfo* _instance;
34 
35  ApiInfo(QByteArray clientId, QByteArray apiKey);
36  ApiInfo(const ApiInfo&) = delete;
37  ApiInfo& operator=(const ApiInfo&) = delete;
38 
39  static QByteArray genClientId();
40  static void createInstance();
41 
42  static QString apiInfoLocation();
43  static QString getOsInfo();
44 
45  static constexpr const char* mscoreHost = "https://musescore.com";
46  static constexpr const char* defaultUpdateScoreInfoPath = "/score/manage/upload/update";
47 
48  public:
49  static const ApiInfo& instance()
50  {
51  if (!_instance)
53  return *_instance;
54  }
55 
56  static constexpr const char* apiHost = "https://api.musescore.com/";
57  static constexpr const char* apiRoot = "/v2";
58  static constexpr const char* clientIdHeader = "X-MS-CLIENT-ID";
59  static constexpr const char* apiKeyHeader = "X-MS-API-KEY";
60  static constexpr const char* userAgentTemplate = "MS_EDITOR/%1.%2 (%3)";
61 
62  static constexpr const char* loginPage = "https://musescore.com/user/auth/webview";
63  static constexpr const char* loginSuccessPage = "https://musescore.com/user/auth/webview/success";
64 
65  static QUrl getUpdateScoreInfoUrl(const QString& scoreId, const QString& accessToken, bool newScore, const QString& customPath);
66 
67  static const QUrl loginUrl;
68  static const QUrl loginSuccessUrl;
69 
70  const QByteArray clientId;
71  const QByteArray apiKey;
72  const QByteArray userAgent;
73  };
74 
75 //---------------------------------------------------------
76 // ApiRequest
77 //---------------------------------------------------------
78 
79 class ApiRequest : public QObject
80  {
81  Q_OBJECT
82  public:
83  enum Method {
87  HTTP_DELETE
88  };
89 
90  private:
91  QUrl _url;
92  QUrlQuery _urlQuery;
93  QUrlQuery _bodyQuery;
94  QHttpMultiPart* _multipart = nullptr;
95 
96  QNetworkReply* _reply = nullptr;
97 
98  Method _method = HTTP_GET;
99 
100  int _retryCount = 0;
101 
102  QNetworkRequest buildRequest() const;
103 
104  signals:
105  void replyFinished(ApiRequest*);
106 
107  public:
108  ApiRequest(QObject* parent = nullptr)
109  : QObject(parent), _url(ApiInfo::apiHost) {}
110  ApiRequest& setMethod(Method m) { _method = m; return *this; }
111  ApiRequest& setPath(const QString& path) { _url.setPath(ApiInfo::apiRoot + path); return *this; }
112  ApiRequest& addGetParameter(const QString& key, const QString& val) { _urlQuery.addQueryItem(key, val); return *this; }
113  ApiRequest& addPostParameter(const QString& key, const QString& val) { _bodyQuery.addQueryItem(key, val); return *this; }
114  ApiRequest& setMultiPartData(QHttpMultiPart* m) { _multipart = m; m->setParent(this); return *this; }
115  ApiRequest& setToken(const QString& token);
116 
117  void executeRequest(QNetworkAccessManager* mgr);
118  QNetworkReply* reply() { return _reply; }
119  const QNetworkReply* reply() const { return _reply; }
120 
121  int retryCount() const { return _retryCount; }
122  };
123 
124 //---------------------------------------------------------
125 // ApiWebEngineRequestInterceptor
126 //---------------------------------------------------------
127 
128 #ifdef USE_WEBENGINE
129 class ApiWebEngineRequestInterceptor : public QWebEngineUrlRequestInterceptor
130  {
131  Q_OBJECT
132  public:
133  ApiWebEngineRequestInterceptor(QObject* parent) : QWebEngineUrlRequestInterceptor(parent) {}
134  void interceptRequest(QWebEngineUrlRequestInfo& info) override;
135  };
136 #endif
137 
138 //---------------------------------------------------------
139 // HttpStatus
140 //---------------------------------------------------------
141 
143  {
144  HTTP_OK = 200,
147  };
148 }
149 #endif
ApiRequest & setPath(const QString &path)
Definition: loginmanager_p.h:111
const QNetworkReply * reply() const
Definition: loginmanager_p.h:119
static const ApiInfo & instance()
Definition: loginmanager_p.h:49
static const QUrl loginUrl
Definition: loginmanager_p.h:67
static ApiInfo * _instance
Definition: loginmanager_p.h:33
Definition: loginmanager_p.h:31
const QByteArray apiKey
Definition: loginmanager_p.h:71
ApiRequest & addPostParameter(const QString &key, const QString &val)
Definition: loginmanager_p.h:113
int retryCount() const
Definition: loginmanager_p.h:121
Definition: loginmanager_p.h:84
static QString getOsInfo()
Definition: loginmanager.cpp:101
static constexpr const char * userAgentTemplate
Definition: loginmanager_p.h:60
ApiInfo & operator=(const ApiInfo &)=delete
Definition: loginmanager_p.h:86
ApiRequest(QObject *parent=nullptr)
Definition: loginmanager_p.h:108
static void createInstance()
Definition: loginmanager.cpp:67
static QString apiInfoLocation()
Definition: loginmanager.cpp:35
static constexpr const char * apiKeyHeader
Definition: loginmanager_p.h:59
QUrlQuery _bodyQuery
Definition: loginmanager_p.h:93
Definition: loginmanager_p.h:79
Definition: loginmanager_p.h:85
ApiInfo(QByteArray clientId, QByteArray apiKey)
Definition: loginmanager.cpp:114
static const QUrl loginSuccessUrl
Definition: loginmanager_p.h:68
static QByteArray genClientId()
Definition: loginmanager.cpp:44
ApiRequest & setMethod(Method m)
Definition: loginmanager_p.h:110
Definition: loginmanager_p.h:145
Definition: loginmanager_p.h:146
QUrlQuery _urlQuery
Definition: loginmanager_p.h:92
static constexpr const char * loginSuccessPage
Definition: loginmanager_p.h:63
static constexpr const char * defaultUpdateScoreInfoPath
Definition: loginmanager_p.h:46
QUrl _url
Definition: loginmanager_p.h:91
HttpStatus
Definition: loginmanager_p.h:142
Definition: aeolus.cpp:26
static constexpr const char * loginPage
Definition: loginmanager_p.h:62
Definition: loginmanager_p.h:144
ApiRequest & addGetParameter(const QString &key, const QString &val)
Definition: loginmanager_p.h:112
static constexpr const char * apiRoot
Definition: loginmanager_p.h:57
static QUrl getUpdateScoreInfoUrl(const QString &scoreId, const QString &accessToken, bool newScore, const QString &customPath)
Definition: loginmanager.cpp:130
static constexpr const char * apiHost
Definition: loginmanager_p.h:56
ApiRequest & setMultiPartData(QHttpMultiPart *m)
Definition: loginmanager_p.h:114
static constexpr const char * clientIdHeader
Definition: loginmanager_p.h:58
const QByteArray clientId
Definition: loginmanager_p.h:70
static constexpr const char * mscoreHost
Definition: loginmanager_p.h:45
QNetworkReply * reply()
Definition: loginmanager_p.h:118
const QByteArray userAgent
Definition: loginmanager_p.h:72
Method
Definition: loginmanager_p.h:83