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

OnlineTracker::OnlineTracker(const OnlineCredentials& credentials, OnlineCredentials::TimeLock &lock, QObject *parent) :
    QObject(parent),
    credentials(credentials),
    lock(lock),
    cachedEntries(NULL)
{
}

OnlineTracker::~OnlineTracker() {
    if (this->cachedEntries) {
        delete this->cachedEntries;
        this->cachedEntries = NULL;
    }
}

bool OnlineTracker::updateRemote(TvShow* show) {
    OnlineTracker::EntryList* entries = satisfyingEntries();
    if (!entries) {
        return false;
    }

    entries->makeSureLocalIsUpdated(this->identifierKey(), show);

    OnlineTracker::UpdateResult result = this->updateRemoteImpl(show, *entries);
    switch (result) {
    case OnlineTracker::success:
        show->setLastOnlineTrackerUpdate(this->identifierKey(), QDateTime::currentDateTimeUtc());
        log() << "update success" << show->name() << identifierKey();
        return true;
    case OnlineTracker::alreadySameAsLocal: {
        const OnlineTracker::Entry* entry = entries->get(this->identifierKey(), show);
        if (entry) {
            show->setLastOnlineTrackerUpdate(this->identifierKey(), entry->lastUpdate());
        }
        log() << "skip alreadySameAsLocal" << show->name() << identifierKey();
        return true;
    }
    case OnlineTracker::skipDueToNoChanges:
        log() << "skip up2date" << show->name() << identifierKey();
        return true;
    case OnlineTracker::invalid:
    case OnlineTracker::failedDueToMissingData:
    case OnlineTracker::failedDueToNetwork:
    default:
        log() << "update failure" << show->name() << result << identifierKey();
        return false;
    }
}


OnlineTracker::UpdateResult OnlineTracker::updateRemoteImpl(const TvShow* show, const OnlineTracker::EntryList& e) const {
    int id = show->getRemoteId(identifierKey());
    if (this->requiresRemoteId() && id <= 0) return failedDueToMissingData;

//    if (this->e == NULL) {
//        return failedDueToMissingData;
//    }

    const Entry* item = e.get(this->identifierKey(), show);
    if (item) {
        if (item->localIsUpToDate(this->identifierKey(), show) && !item->remoteIsUpToDate(show)) {
            if (item->remoteIsEq(show)) {
                return alreadySameAsLocal;
            }
            return this->updateinOnlineTrackerOrAdd(show, "update");
        }
        return skipDueToNoChanges;
    } else {
        return this->updateinOnlineTrackerOrAdd(show, "add");
    }
}


OnlineTracker::EntryList*OnlineTracker::satisfyingEntries() {
    if (!cachedEntries || cachedEntries->tooOld()) {
        this->cachedEntries = fetchRemote();
    }
    return this->cachedEntries;
}

OnlineTracker::EntryList::EntryList() :
    fetchTime(QDateTime::currentDateTimeUtc())
{
}

OnlineTracker::EntryList::~EntryList() {}

void OnlineTracker::EntryList::makeSureLocalIsUpdated(const QString trackerIdentifierKey, TvShow *show) const {
    const Entry* item = this->get(trackerIdentifierKey, show);
    if (item) {
        item->updateShow(trackerIdentifierKey, show);
    }
}

void OnlineTracker::Entry::updateShow(const QString trackerIdentifierKey, TvShow* show) const {
    if (!localIsUpToDate(trackerIdentifierKey, show)) {
        int marker = this->rewatchMarker() == 0 ? -1 : this->rewatchMarker();
        int count = this->rewatchCount();
        if (syncConflict(trackerIdentifierKey, show)) {
            show->episodeList().setMinimalWatched(this->watchedEpisodes());
            marker = std::max(marker, show->getRewatchMarker());
            count = std::max(count, show->getRewatchCount());
        } else {
            show->episodeList().setMaximalWatched(this->watchedEpisodes());
        }
        show->setRewatchCount(count, false);
        if (this->supportsRewatchMarker()) {
            show->setRewatchMarker(marker, false);
        }
        show->setLastOnlineTrackerUpdate(trackerIdentifierKey, this->lastUpdate());
    }
    if (show->getLastOnlineTrackerUpdate(trackerIdentifierKey).isNull()) {
        show->setLastOnlineTrackerUpdate(trackerIdentifierKey, this->lastUpdate());
    }
}


bool OnlineTracker::Entry::remoteIsEq(const TvShow *show) const {
    const TvShow::WatchStatus status = show->getStatus();
    const TvShow::WatchStatus statusWouldSendIfSynced = this->getStatusWouldSendIfSynced(status);
    const TvShow::WatchStatus entryStatus = this->watchStatus();
    // allow remote to claim completion, when unseparated OVAs are not watched, yet. Take it as up2date.
    const bool statusUpToDate =
            statusWouldSendIfSynced == entryStatus ||
            (entryStatus == TvShow::completed && statusWouldSendIfSynced == TvShow::watching);

    const bool episodesUpToDate =
            this->watchedEpisodes() >=
            std::min(this->totalEpisodes(), (int)show->episodeList().highestWatchedEpisodeNumber(0));

    const bool rewatchUpToDate =
            rewatchCount() >= show->getRewatchCount() &&
            (this->supportsRewatchMarker() ? rewatchMarker() >= show->getRewatchMarker() : true);

    return  statusUpToDate && episodesUpToDate && rewatchUpToDate;
}

bool OnlineTracker::EntryList::tooOld() const {
    return this->fetchTime.addSecs(60 * 5) < QDateTime::currentDateTimeUtc();
}


bool OnlineTracker::Entry::syncConflict(const QString trackerIdentifier, const TvShow* show) const {
    return show->getLastLocalUpdate() > lastUpdate() &&
            lastUpdate() > show->getLastOnlineTrackerUpdate(trackerIdentifier);
}

OnlineTracker::Entry::~Entry() {}

bool OnlineTracker::Entry::localIsUpToDate(const QString trackerIdentifier, const TvShow* show) const {
    const QDateTime lastLocalUpdate = show->getLastOnlineTrackerUpdate(trackerIdentifier);
    if (lastLocalUpdate.isNull()) {
        return show->episodeList().highestWatchedEpisodeNumber(0) >= this->watchedEpisodes();
    }
    bool localIsNew = lastLocalUpdate >= this->lastUpdate();
    return localIsNew;
}

bool OnlineTracker::Entry::remoteIsUpToDate(const TvShow* show) const {
    const QDateTime lastLocalUpdate = show->getLastLocalUpdate();
    return (!lastLocalUpdate.isNull() &&
            (!this->lastUpdate().isNull() && this->lastUpdate() >= lastLocalUpdate));
}

QDebug OnlineTracker::log() {
    return qDebug() << "TRACKER";
}
QDebug OnlineTracker::err() {
    return qDebug() << "TRACKER";
}