TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicdownloaddatarequest.cpp
Go to the documentation of this file.
2 #include "musicdownloadmanager.h"
3 
4 MusicDownloadDataRequest::MusicDownloadDataRequest(const QString &url, const QString &path, TTK::Download type, QObject *parent)
5  : MusicDownloadDataRequest(url, path, type, TTK::Record::Null, parent)
6 {
7 
8 }
9 
10 MusicDownloadDataRequest::MusicDownloadDataRequest(const QString &url, const QString &path, TTK::Download type, TTK::Record record, QObject *parent)
11  : MusicAbstractDownLoadRequest(url, path, type, parent),
12  m_createTime(-1),
13  m_redirection(false),
14  m_needUpdate(true),
15  m_recordType(record)
16 {
17 
18 }
19 
21 {
22  if(!m_file || (m_file->exists() && m_file->size() >= 4) || !m_file->open(QIODevice::WriteOnly) || m_url.isEmpty())
23  {
24  TTK_ERROR_STREAM("The data file create failed");
25  Q_EMIT downLoadDataChanged("The data file create failed");
26  deleteAll();
27  return;
28  }
29 
31 }
32 
34 {
35  m_speedTimer.start();
36 
37  QNetworkRequest request;
38  request.setUrl(url);
39  TTK::setSslConfiguration(&request);
41 
42  m_reply = m_manager.get(request);
43  connect(m_reply, SIGNAL(finished()), this, SLOT(downLoadFinished()));
44  connect(m_reply, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
45  connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64)));
47 
50  {
54  }
55 }
56 
58 {
59  if(!m_file || !m_reply)
60  {
61  deleteAll();
62  return;
63  }
64 
66  m_redirection = false;
67  m_file->flush();
68  m_file->close();
69 
70  const QVariant &redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
71  if(m_reply->error() != QNetworkReply::NoError)
72  {
73  m_file->remove();
74  }
75  else if(redirection.isValid())
76  {
78  m_redirection = true;
79  m_file->open(QIODevice::WriteOnly);
80  m_file->resize(0);
81  startToRequest(redirection.toString());
82  return;
83  }
84  else
85  {
86  if(m_needUpdate)
87  {
89  TTK_INFO_STREAM("Data download has finished");
90  }
91  }
92  deleteAll();
93 }
94 
96 {
97  if(m_file)
98  {
99  m_file->write(m_reply->readAll());
100  }
101 }
102 
103 void MusicDownloadDataRequest::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
104 {
105  MusicAbstractDownLoadRequest::downloadProgress(bytesReceived, bytesTotal);
108  {
109  const QString &total = TTK::Number::sizeByteToLabel(bytesTotal);
110  Q_EMIT downloadProgressChanged(bytesTotal != 0 ? bytesReceived * 100.0 / bytesTotal : 0, total, m_createTime);
111  }
112 }
113 
115 {
116  const qint64 speed = m_currentReceived - m_hasReceived;
117  const QString &label = TTK::Number::speedByteToLabel(speed);
118  const qint64 time = (speed != 0) ? (m_totalSize - m_currentReceived) / speed : 0;
119 
120  Q_EMIT downloadSpeedLabelChanged(label, time);
122 }
void downloadSpeedLabelChanged(const QString &speed, qint64 timeLeft)
void downloadProgressChanged(float percent, const QString &total, qint64 time)
MusicDownloadDataRequest(const QString &url, const QString &path, TTK::Download type, QObject *parent=nullptr)
virtual void deleteAll()
void downLoadDataChanged(const QString &bytes)
#define G_DOWNLOAD_MANAGER_PTR
The class of the abstract download data.
QNetworkReply * m_reply
void createDownloadItem(const QString &name, qint64 time)
The class of the download the type of data.
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone)
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
virtual void updateDownloadSpeed() overridefinal
TTK_MODULE_EXPORT QString speedByteToLabel(qint64 size)
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
The namespace of the process utils.
Definition: ttkcompat.h:24
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) overridefinal
QNetworkAccessManager m_manager
virtual void replyError(QNetworkReply::NetworkError error) override
The class of the download manager pair.
#define TTK_SLOT
Definition: ttkqtglobal.h:165
static qint64 currentTimestamp()
Definition: ttktime.cpp:249
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
virtual void deleteAll() overridefinal
virtual void downLoadFinished() override
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:69
virtual void startToRequest() override
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59