TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicdownloadtextrequest.cpp
Go to the documentation of this file.
2 
3 MusicDownloadTextRequest::MusicDownloadTextRequest(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 text file create failed");
14  TTK_ERROR_STREAM("The text 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);
24  TTK::setContentTypeHeader(&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  const QByteArray &bytes = m_reply->readAll();
38  if(!bytes.isEmpty())
39  {
40  QTextStream outstream(m_file);
41 #if TTK_QT_VERSION_CHECK(6,0,0)
42  outstream.setEncoding(QStringConverter::Utf8);
43 #else
44  outstream.setCodec("UTF-8");
45 #endif
46  outstream << QString(bytes).remove("\r").toUtf8();
47  outstream << QtNamespace(endl);
48  m_file->close();
49  TTK_INFO_STREAM("Text download has finished");
50  }
51  else
52  {
53  TTK_ERROR_STREAM("Text download file error");
54  m_file->remove();
55  m_file->close();
56  }
57  }
58 
60  deleteAll();
61 }
virtual void downloadFinished() overridefinal
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone) noexcept
QNetworkReply * m_reply
void downloadDataChanged(const QString &bytes)
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
The namespace of the application object.
Definition: ttkcompat.h:24
QNetworkAccessManager m_manager
virtual void startToRequest() overridefinal
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
TTK_MODULE_EXPORT void setContentTypeHeader(QNetworkRequest *request, const QByteArray &data={}) noexcept
virtual void replyError(QNetworkReply::NetworkError error) override
#define TTK_SLOT
Definition: ttkqtglobal.h:177
#define QtNamespace(p)
Qt use namespace.
Definition: ttkqtcompat.h:129
The class of the abstract download data.
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
virtual void deleteAll() overridefinal
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59
MusicDownloadTextRequest(const QString &url, const QString &path, QObject *parent=nullptr)