root/src/onlinetracker.h

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
#ifndef ONLINETRACKER_H
#define ONLINETRACKER_H

#include <QObject>
#include "tvshow.h"
#include "onlinecredentials.h"

class OnlineTracker : public QObject
{
    Q_OBJECT
public:
    enum UpdateResult {
        invalid,
        failedDueToMissingData,
        failedDueToNetwork,
        success,
        alreadySameAsLocal,
        skipDueToNoChanges
    };

    class Entry {
    public:
        virtual ~Entry();
        virtual QDateTime lastUpdate() const = 0;
        virtual int remoteId() const = 0;
        virtual int watchedEpisodes() const = 0;
        virtual int rewatchMarker() const = 0;
        virtual int rewatchCount() const = 0;
        virtual int totalEpisodes() const = 0;

        virtual TvShow::WatchStatus getStatusWouldSendIfSynced(TvShow::WatchStatus showStatus) const = 0;
        virtual TvShow::WatchStatus watchStatus() const = 0;

        bool localIsUpToDate(const QString trackerIdentifier, const TvShow* show) const;
        bool remoteIsUpToDate(const TvShow* show) const;
        bool syncConflict(const QString trackerIdentifier, const TvShow* show) const;
        void updateShow(const QString trackerIdentifierKey, TvShow *show) const;

        virtual bool supportsRewatchMarker() const = 0;
        bool remoteIsEq(const TvShow* show) const; ///< checks data not dates
    };

    class EntryList {
    public:
        EntryList();
        virtual ~EntryList();
        virtual const Entry* get(const QString trackerIdentifierKey, const TvShow* show) const = 0;
        void makeSureLocalIsUpdated(const QString trackerIdentifierKey, TvShow* show) const;
        // virtual void describe(nw::Describer& de) = 0;

        bool tooOld() const;
        const QDateTime fetchTime;
    };

    explicit OnlineTracker(const OnlineCredentials& credentials, OnlineCredentials::TimeLock& lock, QObject *parent = 0);
    virtual ~OnlineTracker();
    bool updateRemote(TvShow* show);
    virtual bool requiresRemoteId() const { return true; }
    virtual EntryList* fetchRemote() = 0; ///< Must create a new EntryList using online Data, or NULL on error
    virtual const QString identifierKey() const = 0;

    const OnlineCredentials& credentials;

protected:
    virtual UpdateResult updateinOnlineTrackerOrAdd(const TvShow* show, const QString& type) const = 0;

    OnlineCredentials::TimeLock& lock;

private:
    /// Either returns cached entries, or fetches new ones when cached are too old
    EntryList* satisfyingEntries();
    UpdateResult updateRemoteImpl(const TvShow* show, const EntryList& entries) const;

    EntryList* cachedEntries;

    static QDebug log();
    static QDebug err();
};

#endif // ONLINETRACKER_H