-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdatasynchronizer.h
116 lines (100 loc) · 3.06 KB
/
webdatasynchronizer.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
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
#ifndef WEBDATASYNCHRONIZER_H
#define WEBDATASYNCHRONIZER_H
#include <QByteArray>
#include <QCryptographicHash>
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMap>
#include <QMetaObject>
#include <QMutex>
#include <QMutexLocker>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QObject>
#include <QRunnable>
#include <QScopedPointerDeleteLater>
#include <QSharedPointer>
#include <QSettings>
#include <QStandardPaths>
#include <QString>
#include <QThread>
#include <QUrl>
#include <QUrlQuery>
#include <QXmlStreamReader>
#include <algorithm>
#include <qgumbodocument.h>
#include <qgumbonode.h>
#include "models/invertebrate.h"
#include "models/stream.h"
#include "parsers/invertebratehandler.h"
#include "parsers/streamhandler.h"
enum class WebDataSynchonizerExitStatus {
SUCCEEDED,
FAILED_RUNTIME,
FAILED_NETWORK_ACCESS
};
enum class SyncOptions {
MANUAL_ONLY = 0,
ON_STARTUP,
WIFI
};
enum class SyncStatus {
SYNC_IN_PROGRESS = 0,
SYNC_HALTING,
READY_TO_SYNC
};
class WebDataSynchronizer : public QObject, public QRunnable
{
Q_OBJECT
int batchSize = 50;
// Owned by the application instance
QMutex* mutex = nullptr;
QMap<QString, Invertebrate> *invertebratesFromLocal = nullptr;
QMap<QString, Stream> *streamsFromLocal = nullptr;
// Owned properties
QEventLoop loop;
QString imagePath;
QDir directoryHelper;
QSharedPointer<QNetworkAccessManager> network; // Must be constructed on the threadpool thread, so this starts off null
QDateTime lastUpdate;
QNetworkReply *synchronouslyGetUrl(const QUrl &url, bool *ok);
void handleNetworkReplyForStreamList(QNetworkReply* reply);
void handleNetworkReplyForStreamData(QNetworkReply* reply);
void handleNetworkReplyForInvertebrateList(QNetworkReply* reply);
void handleNetworkReplyForInvertebrateData(QNetworkReply* reply);
void handleNetworkReplyForImageList(QNetworkReply* reply, QMap<QString, QList<Invertebrate *>> *invertebrateImages);
bool handleNetworkReplyForImageData(QNetworkReply* reply, QString localFileName);
void handleNetworkReplyForAbout(QNetworkReply* reply);
void saveData();
public:
explicit WebDataSynchronizer(QObject *parent = 0);
~WebDataSynchronizer();
void setData(QMutex *mutex, QMap<QString, Invertebrate> *invertebrates, QMap<QString, Stream> *streams);
void run();
bool syncingShouldContinue = true;
public slots:
void stop();
signals:
void started();
void statusUpdateMessage(const QString &status);
void finished(WebDataSynchonizerExitStatus status);
// void streamSyncComplete();
// void invertebrateSyncComplete();
void imageSyncComplete();
void aboutStringParsed(const QString& about);
void shouldStop();
private slots:
void syncInvertebrates();
void syncStreams();
void syncImages();
void syncAbout();
void finalize();
};
#endif // WEBDATASYNCHRONIZER_H