-
Notifications
You must be signed in to change notification settings - Fork 0
/
episode.cpp
77 lines (68 loc) · 1.52 KB
/
episode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "episode.h"
#include "downloader.h"
QUrl Episode::getUrl() const
{
return url;
}
QString Episode::getEpisodeTitle() const
{
return episodeTitle;
}
void Episode::setEpisodeTitle(const QString &value)
{
episodeTitle = value;
}
void Episode::abort()
{
downloader->abort(url);
}
Episode::Episode(const QString& podcastName, const QString &episodeTitle, const QUrl& url, Downloader *downloader, QObject *parent) : QObject(parent)
{
this->episodeTitle = episodeTitle;
this->downloader = downloader;
this->url = url;
this->filename = url.fileName();
this->podcastName = podcastName;
connect(downloader,&Downloader::downloadFailed,this,&Episode::downloadFailed);
connect(downloader,&Downloader::downloadSuccess,this,&Episode::downloadSuccess);
}
void Episode::load()
{
downloader->load(url,episodeTitle);
}
bool Episode::save(QDir directory)
{
if(!podcastName.isEmpty())
{
directory = QDir(QDir(directory).filePath(podcastName));
directory.mkpath(".");
}
QFile file(directory.filePath(filename));
if(file.open(QIODevice::WriteOnly))
{
if( (file.write(data) != data.size()) || !file.flush())
{
file.close();
return false;
}
else
{
file.close();
return true;
}
}
return false;
}
void Episode::downloadSuccess(const QUrl &url, const QByteArray &data)
{
if(url!=this->url)
return;
this->data = data;
emit complete();
}
void Episode::downloadFailed(const QUrl &url, QNetworkReply::NetworkError)
{
if(url!=this->url)
return;
emit failed();
}