TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicwydownloadtextrequest.cpp
Go to the documentation of this file.
2 
3 MusicWYDownLoadTextRequest::MusicWYDownLoadTextRequest(const QString &url, const QString &path, QObject *parent)
4  : MusicAbstractDownLoadRequest(url, path, TTK::Download::Lrc, parent)
5 {
6 
7 }
8 
10 {
11  if(!m_file || (m_file->exists() && m_file->size() >= 4) || !m_file->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) || m_url.isEmpty())
12  {
13  Q_EMIT downLoadDataChanged("The wangyi text file create failed");
14  TTK_ERROR_STREAM(className() << "file create failed");
15  deleteAll();
16  return;
17  }
18 
19  m_speedTimer.start();
20 
21  QNetworkRequest request;
22  request.setUrl(m_url);
23  TTK::setSslConfiguration(&request);
25 
26  m_reply = m_manager.get(request);
27  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
28  connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
30 }
31 
33 {
35  if(m_reply && m_file && m_reply->error() == QNetworkReply::NoError)
36  {
37  QJson::Parser json;
38  bool ok = false;
39  const QVariant &data = json.parse(m_reply->readAll(), &ok);
40  if(ok)
41  {
42  QVariantMap value = data.toMap();
43  if(value["code"].toInt() == 200)
44  {
45  value = value["lrc"].toMap();
46  if(!value.isEmpty())
47  {
48  const QString &data = value["lyric"].toString();
49  QTextStream outstream(m_file);
50  outstream.setCodec("UTF-8");
51  outstream << data.toUtf8();
52  QtStreamEndLine(outstream);
53  m_file->close();
54  TTK_INFO_STREAM(className() << "download has finished");
55  }
56  }
57  else
58  {
59  TTK_ERROR_STREAM(className() << "download file error");
60  m_file->remove();
61  m_file->close();
62  }
63  }
64  }
65 
67  deleteAll();
68 }
void downLoadDataChanged(const QString &bytes)
The class of the abstract download data.
QNetworkReply * m_reply
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone)
virtual void startToRequest() overridefinal
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
#define QtStreamEndLine(p)
Stream endl.
Definition: ttkqtcompat.h:115
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
The namespace of the process utils.
Definition: ttkcompat.h:24
QNetworkAccessManager m_manager
virtual void replyError(QNetworkReply::NetworkError error) override
QVariant parse(QIODevice *io, bool *ok=0)
Read JSON string from the I/O Device and converts it to a QVariant object.
Definition: parser.cpp:69
#define TTK_SLOT
Definition: ttkqtglobal.h:165
MusicWYDownLoadTextRequest(const QString &url, const QString &path, QObject *parent=nullptr)
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
virtual void downLoadFinished() overridefinal
virtual void deleteAll() overridefinal
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:69
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59