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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "episodelist.h"
#include <QDebug>
#include "nwutils.h"
EpisodeList::EpisodeList(QObject* parent) : QObject(parent) {
}
EpisodeList::~EpisodeList() {
}
void EpisodeList::exportXbmcLinks(QDir) {
/*
if (!dir.exists()) {
dir.mkpath(".");
}
for (int i=0; i < episodes.length(); ++i) {
const Episode* f = episodes.at(i);
QString linkName = QString("%1. %2.%3").arg(f->xbmcEpisodeNumber(), f->xbmcEpisodeName(), f->fileExtension());
QFile::link(f->path(), dir.absoluteFilePath(linkName));
}
*/
}
void EpisodeList::readAsElement(nw::JsonReader &jr) {
jr.describeArray("episodes", "episode", episodes.length());
for (int i=0; jr.enterNextElement(i); ++i) {
Episode* episode = new Episode(&jr, this);
addEpisode(episode);
}
}
void EpisodeList::writeAsElement(nw::JsonWriter &jw) const {
jw.describeArray("episodes", "episode", episodes.length());
for (int i=0; jw.enterNextElement(i); ++i) {
Episode* episode = episodes[i];
episode->describe(&jw);
}
}
void EpisodeList::writeDetailed(nw::JsonWriter &jw, const QStringList& releaseGroupPreference) const {
jw.describeArray("episodes", "episode", episodes.length());
for (int i=0; jw.enterNextElement(i); ++i) {
Episode* episode = episodes[i];
episode->writeDetailed(jw, releaseGroupPreference);
}
}
void EpisodeList::addMovieFile(const VideoFile* movieFile) {
if (movieFile->path.isEmpty() || NULL != getEpisodeForPath(movieFile->path)) {
delete movieFile;
return;
}
Episode* ep = getEpisodeForNumber(movieFile->numericEpisodeNumber());
if (NULL != ep) {
ep->addPath(movieFile);
return;
}
addEpisode(new Episode(movieFile));
}
void EpisodeList::addEpisode(Episode *episode) {
if (this->getEpisodeForNumber(episode->getEpisodeNumber())) {
delete episode;
return; // TODO merge paths
}
this->episodes.push_back(episode);
connect(episodes.back(), SIGNAL(watchedChanged(bool,bool)), this, SLOT(watchedChanged(bool,bool)));
}
Episode *EpisodeList::getEpisodeForNumber(float number) {
if (number < 0) {
return NULL; // specials don't have a number
}
foreach (Episode* ep, episodes) {
if (ep->getEpisodeNumber() == number) {
return ep;
}
}
return NULL;
}
int EpisodeList::numberOfEpisodes() const
{
int count = 0;
for (int i=0; i < episodes.length(); ++i) {
if (!episodes.at(i)->isSpecial()) {
++count;
}
}
return count;
}
int EpisodeList::numberOfWatchedEpisodes() const
{
int count = 0;
for (int i=0; i < episodes.length(); ++i) {
if (!episodes.at(i)->isSpecial() && episodes.at(i)->getWatched()) {
++count;
}
}
return count;
}
float EpisodeList::highestDownloadedEpisodeNumber() const
{
float highest = -1;
for (int i=0; i < episodes.length(); ++i) {
float num = episodes.at(i)->getEpisodeNumber();
highest = num > highest ? num : highest;
}
return highest;
}
QStringList EpisodeList::releaseGroups() const {
QStringList groups;
foreach (const Episode* ep, episodes) {
QStringList epGroups = ep->releaseGroups();
foreach (QString group, epGroups) {
if (!groups.contains(group)) {
groups.push_back(group);
}
}
}
return groups;
}
float EpisodeList::highestWatchedEpisodeNumber(int min) const
{
float highest = min;
for (int i=0; i < episodes.length(); ++i) {
if (episodes.at(i)->getWatched()) {
float num = episodes.at(i)->getEpisodeNumber();
highest = num > highest ? num : highest;
}
}
return highest;
}
QString EpisodeList::mostDownloadedReleaseGroup() const {
QMap<QString, int> scores;
for (int i=0; i < episodes.length(); ++i) {
QStringList groups = episodes.at(i)->releaseGroups();
foreach (QString group, groups) {
scores[group]++;
}
}
std::pair<QString, int> highest;
for (QMap<QString, int>::iterator it=scores.begin(); it != scores.end(); ++it) {
int num = it.value();
if (num > highest.second) {
highest.second = num;
highest.first = it.key();
}
}
return highest.first;
}
bool epNumLess(const Episode* a, const Episode* b) {
return a->getEpisodeNumber() < b->getEpisodeNumber();
}
Episode* EpisodeList::getEpisodeForPath(const QString& path) {
for (int i=0; i < episodes.length(); ++i) {
Episode* f = episodes[i];
const VideoFile* mf = f->getMovieFileForPath(path);
if (mf) {
return f;
}
}
return NULL;
}
void EpisodeList::setMinimalWatched(int number) {
foreach (Episode* ep, episodes) {
float epNum = ep->getEpisodeNumber();
// ignore 11.5 style special episodes
if (epNum <= 0 || 0 != epNum - (int)epNum) {
continue;
}
if (epNum <= number) {
ep->setWatched(true);
}
}
}
void EpisodeList::setMaximalWatched(int number) {
foreach (Episode* ep, episodes) {
float epNum = ep->getEpisodeNumber();
// ignore 11.5 style special episodes
if (epNum <= 0 || 0 != epNum - (int)epNum) {
continue;
}
if (epNum <= number) {
ep->setWatched(true);
} else {
ep->setWatched(false);
}
}
}
void EpisodeList::setWatched(int number) {
foreach (Episode* ep, episodes) {
if (ep->isSpecial()) {
continue;
}
int epNum = ep->getEpisodeNumber();
if (epNum <= number) {
ep->setWatched(true);
} else {
ep->setWatched(false);
}
}
}
void EpisodeList::watchedChanged(bool oldValue, bool newValue) {
int count = numberOfWatchedEpisodes();
if (!oldValue && newValue) {
emit watchCountChanged(count-1, count);
} else if (oldValue && !newValue) {
emit watchCountChanged(count+1, count);
} else {
emit watchCountChanged(count, count);
}
}
void EpisodeList::beforeWatchedChanged(bool newValue, bool oldValue) {
Episode* ep = dynamic_cast<Episode*>(sender());
if (!ep) throw ("fuck up ep casting" + std::string(__func__));
if (oldValue != newValue && !ep->isSpecial()) {
int count = numberOfWatchedEpisodes();
int diff = oldValue ? -1 : +1;
emit beforeWatchCountChanged(count+diff, count);
}
}
QList<const VideoFile*> EpisodeList::missingFiles() const {
QList<const VideoFile*> missing;
foreach (Episode* ep, episodes) {
missing << ep->missingFiles();
}
return missing;
}
bool EpisodeList::removeFileFromList(QString filepath) {
foreach (Episode* const& e, episodes) {
bool success = e->removeFileFromList(filepath);
if (success) {
return success;
}
}
return false;
}
bool EpisodeList::hasNoFiles() {
foreach (Episode* const& e, episodes) {
if (!e->files.isEmpty()) {
return false;
}
}
return true;
}