-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcastclient.cpp
180 lines (167 loc) · 3.94 KB
/
podcastclient.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "podcastclient.h"
QStringList PodcastClient::getFeedsFromSettings()
{
QStringList resultList = settings.value("feeds").toStringList();
if(resultList.isEmpty())
{
QString string = settings.value("feeds").toString();
if(!string.isEmpty())
resultList.push_back(string);
}
return resultList;
}
PodcastClient::PodcastClient(QObject *parent) : QObject(parent)
{
if(settings.value("NumDownloads").isNull())
settings.setValue("NumDownloads",10);
if(settings.value("Dest").isNull())
settings.setValue("Dest",".");
else
{
if(!QDir(settings.value("Dest").toString()).exists())
{
settings.setValue("Dest",",");
}
}
downloader.setMaxConnections(settings.value("NumDownloads").toInt());
foreach(QString url, getFeedsFromSettings())
{
Podcast* podcast = new Podcast(QUrl(url), &downloader, this);
podcast->setTargetFolder(QDir(settings.value("Dest").toString()));
podcasts.push_back(podcast);
connect(podcast,&Podcast::done,this,&PodcastClient::podcastDone);
connect(podcast,&Podcast::writingFailed,this,&PodcastClient::podcastWritingFailed);
}
}
bool PodcastClient::downloadAll()
{
if(podcasts.isEmpty())
{
out << "No podcasts in list. Done." << endl;
return true;
}
finishedCtr = 0;
foreach(Podcast* podcast, podcasts)
{
podcast->update();
}
return false;
}
bool PodcastClient::addPodcast(const QUrl &url, const QString &mode)
{
podcasts.clear();
finishedCtr = 0;
if(!url.isValid())
{
out << "Invalid URL." << endl;
return true;
}
if(mode=="last" || mode.isEmpty())
{
Podcast* podcast = new Podcast(url, &downloader, this);
podcasts.push_back(podcast);
connect(podcast,&Podcast::done,this,&PodcastClient::podcastDone);
podcast->init(true);
QStringList feeds;
foreach(QString url, getFeedsFromSettings())
{
feeds.push_back(url);
}
feeds.push_back(url.toString());
settings.setValue("feeds",feeds);
return false;
}
else if(mode=="all")
{
QStringList feeds;
foreach(QString url, getFeedsFromSettings())
{
feeds.push_back(url);
}
feeds.push_back(url.toString());
settings.setValue("feeds",feeds);
return true;
}
else if(mode=="none")
{
Podcast* podcast = new Podcast(url, &downloader, this);
podcasts.push_back(podcast);
connect(podcast,&Podcast::done,this,&PodcastClient::podcastDone);
podcast->init(false);
QStringList feeds;
foreach(QString url, getFeedsFromSettings())
{
feeds.push_back(url);
}
feeds.push_back(url.toString());
settings.setValue("feeds",feeds);
return false;
}
else
{
out << "Invalid adding mode: " << mode << endl;
out << "Modes are: last, all, none" << endl;
return true;
}
}
void PodcastClient::removePodcast(const QUrl &url)
{
QStringList feeds;
foreach(QString url, getFeedsFromSettings())
{
feeds.push_back(url);
}
feeds.removeAll(url.toString());
if(feeds.isEmpty())
settings.remove("feeds");
else
settings.setValue("feeds",feeds);
}
void PodcastClient::setDest(const QString &dest)
{
QDir dir(dest);
settings.setValue("Dest",dest);
settings.sync();
if(!dir.exists())
{
out << "Target directory does not exist." << endl;
}
}
QString PodcastClient::getDest()
{
return settings.value("Dest").toString();
}
void PodcastClient::list()
{
foreach(QString url, getFeedsFromSettings())
{
out << url << endl;
}
}
void PodcastClient::setMaxDownloads(int num)
{
downloader.setMaxConnections(num);
settings.setValue("NumDownloads",num);
settings.sync();
}
int PodcastClient::getMaxDownloads()
{
return downloader.getMaxConnections();
}
void PodcastClient::podcastDone()
{
finishedCtr++;
if(finishedCtr==podcasts.size())
{
settings.sync();
QCoreApplication::exit(0);
}
}
void PodcastClient::podcastWritingFailed()
{
out << "Aborting downloads..." << endl;
foreach(Podcast* podcast, podcasts)
{
podcast->abort();
}
}