root/src/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
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
#include "episode.h"
#include <stdlib.h>
#include <QDebug>
#include "utils.h"

Episode::Episode(nw::Describer *jw, QObject* parent) :
    QObject(parent),
    episodeNumber(VideoFile::INVALID)
{
    this->describe(jw);
}

Episode::Episode(QString path, QObject *parent) :
    QObject(parent),
    episodeNumber(VideoFile::INVALID)
{
    addPath(path);
}

Episode::Episode(const VideoFile *path, QObject *parent) :
    QObject(parent),
    episodeNumber(VideoFile::INVALID)
{
    addPath(path);
}

Episode::~Episode() {
    foreach (const VideoFile* m, files) {
        delete m;
    }
}

void Episode::describe(nw::Describer *de) {
    NwUtils::describe(*de, "watchedDate", watchedDate);

    de->describeValueArray("files", files.length());
    for (int i=0; de->enterNextElement(i); ++i) {
        if (de->isInReadMode()) {
            QString path;
            NwUtils::describeValue(*de, path);
            addPath(new VideoFile(path, false));
        } else {
            QString path = files.at(i)->path;
            NwUtils::describeValue(*de, path);
        }
    }
}

void Episode::writeDetailed(nw::JsonWriter &jw, const QStringList& releaseGroupPreference) {
    NwUtils::describe(jw, "showName", showName);
    bool watched = getWatched();
    NwUtils::describe(jw, "watched", watched);
    NwUtils::describe(jw, "watchedDate", watchedDate);
    NwUtils::describe(jw, "numericEpisodeNumber", episodeNumber);

    // TODO don't rely on an abs path for TVSHOWPAGE
    const VideoFile* best = this->bestFile(releaseGroupPreference);
    if (best) {
        VideoFile copy = *best;
        bool exists = copy.exists();
        NwUtils::describe(jw, "path", copy.path);
        NwUtils::describe(jw, "episodeNumber", copy.episodeNumber);
        NwUtils::describe(jw, "releaseGroup", copy.releaseGroup);
        NwUtils::describe(jw, "episodeName", copy.episodeName);
        NwUtils::describeValueArray(jw, "tech", copy.techTags);
        NwUtils::describe(jw, "seasonName", copy.seasonName);
        NwUtils::describe(jw, "hashId", copy.hashId);
        NwUtils::describe(jw, "exists", exists);
    }
}

const VideoFile *Episode::getMovieFileForPath(QString path) {
    foreach (const VideoFile* mf, files) {
        if (mf->path == path) {
            return mf;
        }
    }
    return NULL;
}

const VideoFile *Episode::bestFile(const QStringList& releaseGroupPreference) const {
    std::pair<const VideoFile*, int> best = std::pair<const VideoFile*, int>(NULL,-1);
    const int worst = releaseGroupPreference.length();
    foreach (const VideoFile* file, files) {
        if (!file) {
            continue;
        }
        int score = releaseGroupPreference.indexOf(file->releaseGroup);
        score = -1 == score ? worst : score;
        score += worst * !file->exists();
        if (best.second == -1 || score < best.second) {
            best.first = file;
            best.second = score;
        }
    }
    return best.first;
}

QString Episode::getShowName() const {
    return showName;
}

bool Episode::getWatched() const {
    return !watchedDate.isNull();
}

void Episode::setWatched(bool value) {
    bool oldValue = this->getWatched();
    emit beforeWatchedChanged(value, oldValue);
    if (value) {
        if (!oldValue) {
            watchedDate = QDateTime::currentDateTime();
        }
    } else {
        watchedDate = QDateTime();
    }
    emit watchedChanged(oldValue, value);
}

float Episode::getEpisodeNumber() const {
    return this->episodeNumber;
}

QDateTime Episode::getWatchedDate() const
{
    return watchedDate;
}

QStringList Episode::releaseGroups() const {
    QStringList groups;
    foreach (const VideoFile* mf, files) {
        if (!groups.contains(mf->releaseGroup)) {
            groups.push_back(mf->releaseGroup);
        }
    }
    return groups;
}

bool Episode::isSpecial() const {
    return episodeNumber == VideoFile::SPECIAL;
}


void Episode::addPath(QString path) {
    foreach (const VideoFile* f, files) {
        if (f->path == path) {
            return;
        }
    }
    pushFile(new VideoFile(path));
}

void Episode::addPath(const VideoFile *movieFile) {
    QString path = movieFile->path;
    foreach (const VideoFile* f, files) {
        if (f->path == path) {
            delete movieFile;
            return;
        }
    }
    pushFile(movieFile);
}


void Episode::pushFile(const VideoFile* mf) {
    if (this->files.isEmpty()) {
        this->episodeNumber = mf->numericEpisodeNumber();
        this->showName = mf->showName;
    }
    this->files.push_back(mf);
}

QList<const VideoFile*> Episode::missingFiles() const {
    return Utils::filter(files, [](const VideoFile*const& f) -> bool {
        return !f->exists();
    });
}

bool Episode::removeFileFromList(QString filepath) {
    // holy macaroni! c++ syntax up my ass
    foreach (const VideoFile* const& f, files) {
        if(f->path == filepath) {
            return files.removeOne(f);
        }
    }
    return false;
}
QList<const VideoFile*> Episode::getFiles() const {
    return files;
}