root/src/fstracker.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
#include "fstracker.h"
#include "library.h"
#include "nwutils.h"

FsTracker::FsTracker(const FsTracker::Config config, const OnlineCredentials& credentials, OnlineCredentials::TimeLock &lock, QObject *parent)
    : OnlineTracker(credentials, lock, parent),
      config(config)
{
    this->remoteLib = new Library(config.path, this);
}

FsTracker::EntryList *FsTracker::fetchRemote() {
    bool noError = !config.path.isEmpty();
    if (noError) {
        noError *= config.createDirectory
                ? this->remoteLib->initDirectory()
                : this->remoteLib->getDirectory().exists();
        this->remoteLib->readAll(false);
    }
    return noError
            ? new EntryList(this->remoteLib)
            : NULL;
}

void insertRemoteEpisodes(const TvShow *show, TvShow& remoteShowRef) {
    for (const Episode* ep : show->episodeList().episodes) {

        Episode* remoteEp = NULL;
        QList<const VideoFile*> files = ep->getFiles();
        for (const VideoFile* vf : files) {
            if (remoteEp) {
                remoteEp->addPath(vf->path);
            } else {
                remoteEp = new Episode(vf->path, NULL);
            }
        }

        if (remoteEp) {
            remoteShowRef.episodeList().addEpisode(remoteEp);
        }
    }
}

OnlineTracker::UpdateResult FsTracker::updateinOnlineTrackerOrAdd(const TvShow *show, const QString &actionType) const {
    bool noError = true;

    if (actionType == "add") {
        /* oh gawd kill me all this copying is dangerous and I did not check everything, yet */
        TvShow& remoteShowRef = remoteLib->tvShow(show->name(), false);
        insertRemoteEpisodes(show, remoteShowRef);

        noError *= this->remoteLib->writeIndex();
    }
    TvShow* remoteShow = this->remoteLib->existingTvShow(show->name());
    if (!remoteShow) {
        return failedDueToNetwork;
    }

    if (remoteShow->episodeList().numberOfEpisodes() < show->episodeList().numberOfWatchedEpisodes()) {
        insertRemoteEpisodes(show, *remoteShow);
    }

    remoteShow->episodeList().setWatched(show->episodeList().numberOfWatchedEpisodes());

    remoteShow->setTotalEpisodes(show->getTotalEpisodes());
    remoteShow->setRewatchMarker(show->getRewatchMarker(), false);
    remoteShow->setRewatchCount(show->getRewatchCount(), false);
    remoteShow->setLastOnlineTrackerUpdate(identifierKey(), QDateTime::currentDateTimeUtc());

    /*
    remoteShow->setAiringStatus(show->getAiringStatus());
    remoteShow->setShowType(show->getShowType());
    remoteShow->setStartDate(show->getStartDate());
    remoteShow->setEndDate(show->getEndDate());
    remoteShow->setSynopsis(show->getSynopsis());
    remoteShow->setSynonyms(show->getSynonyms());
    TODO more and figure out when to sync them
    */

    noError *= show ? this->remoteLib->writeShow(remoteShow) : false;
    return noError
            ? success
            : failedDueToNetwork;
}

const FsTracker::Entry *FsTracker::EntryList::get(const QString, const TvShow *show) const {
//    int remoteId = show->getRemoteId(trackerIdentifierKey);
//    TvShow* remoteShow = remoteLib->filter().getShowForRemoteId(trackerIdentifierKey,remoteId);
    TvShow* remoteShow = remoteLib->existingTvShow(show->name());
    return remoteShow
            ? new Entry(remoteShow)
            : NULL;
}


FsTracker::Config::Config(QString configFilePath) {
    nw::JsonReader jr(configFilePath.toStdString());
    NwUtils::describe(jr, "path", path);
    NwUtils::describe(jr, "createDirectory", createDirectory);
    jr.close();
}