TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicdownloadqueuerequest.cpp
Go to the documentation of this file.
2 
3 #include <QStringList>
4 
7 {
8 
9 }
10 
12  : MusicAbstractDownLoadRequest(data.m_url, data.m_path, type, parent),
13  m_isDownload(false),
14  m_isAbort(false)
15 {
16  m_request = new QNetworkRequest;
19 }
20 
21 MusicDownloadQueueRequest::MusicDownloadQueueRequest(const MusicDownloadQueueDataList &datas, TTK::Download type, QObject *parent)
23 {
24  addQueue(datas);
25 }
26 
28 {
29  delete m_request;
30  m_request = nullptr;
31  deleteAll();
32 }
33 
34 void MusicDownloadQueueRequest::addQueue(const MusicDownloadQueueDataList &datas) noexcept
35 {
36  m_queue = datas;
37 }
38 
40 {
41  if(!m_queue.isEmpty())
42  {
44  }
45 }
46 
48 {
49  clear();
50  if(m_isDownload && m_reply)
51  {
52  m_isAbort = true;
53  m_reply->abort();
54  m_file->close();
55  m_file->remove();
56  delete m_reply;
57  m_reply = nullptr;
58  m_isAbort = false;
59  }
60 }
61 
63 {
64  m_queue.clear();
65 }
66 
68 {
69  if(m_isAbort || !m_request || !m_reply || !m_file)
70  {
71  return;
72  }
73 
75  m_file->flush();
76  m_file->close();
77  m_isDownload = false;
78 
80  Q_EMIT downLoadDataChanged(m_queue.takeFirst().m_path);
81 
83 }
84 
86 {
87  if(!m_file || !m_reply)
88  {
89  return;
90  }
91 
92  m_file->write(m_reply->readAll());
93  m_file->flush();
94 }
95 
96 void MusicDownloadQueueRequest::handleError(QNetworkReply::NetworkError code)
97 {
98  if(!m_file || !m_reply)
99  {
100  return;
101  }
102 
103 #ifndef TTK_DEBUG
104  Q_UNUSED(code);
105 #endif
106  TTK_ERROR_STREAM("QNetworkReply::NetworkError:" << code << m_reply->errorString());
107  m_file->flush();
108 
109  if(!m_isAbort)
110  {
112  }
113 
114  startToRequest();
115 }
116 
118 {
119  m_isDownload = true;
120  delete m_file;
121  m_file = new QFile(m_queue.front().m_path, this);
122 
123  if(!m_file->open(QIODevice::WriteOnly))
124  {
125  m_file->close();
126  delete m_file;
127  m_file = nullptr;
128  return;
129  }
130 
131  if(!m_request || url.isEmpty())
132  {
133  return;
134  }
135 
136  m_speedTimer.start();
137  m_request->setUrl(url);
138  m_reply = m_manager.get(*m_request);
139  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
140  connect(m_reply, SIGNAL(readyRead()), SLOT(handleReadyRead()));
142 }
143 
145 {
146  if(m_queue.isEmpty())
147  {
148  return;
149  }
150 
151  if(QFile::exists(m_queue.front().m_path))
152  {
153  Q_EMIT downLoadDataChanged(m_queue.takeFirst().m_path);
154  startOrderQueue();
155  }
156  else if(G_NETWORK_PTR->isOnline())
157  {
158  startDownload(m_queue.front().m_url);
159  }
160 }
virtual void deleteAll()
void downLoadDataChanged(const QString &bytes)
The class of the abstract download data.
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone) noexcept
QNetworkReply * m_reply
void addQueue(const MusicDownloadQueueDataList &datas) noexcept
The class of the download data from queue request.
void startDownload(const QString &url)
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={}) noexcept
QNetworkAccessManager m_manager
void handleError(QNetworkReply::NetworkError code)
#define TTK_SLOT
Definition: ttkqtglobal.h:177
virtual void startToRequest() overridefinal
The class of the download queue data.
Definition: inftrees.h:24
virtual void deleteAll() overridefinal
#define G_NETWORK_PTR
MusicDownloadQueueDataList m_queue
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
MusicDownloadQueueRequest(TTK::Download type, QObject *parent=nullptr)
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59
virtual void downLoadFinished() overridefinal