-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastore.cpp
More file actions
executable file
·224 lines (167 loc) · 4.86 KB
/
Copy pathdatastore.cpp
File metadata and controls
executable file
·224 lines (167 loc) · 4.86 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "datastore.h"
#include <QFile>
#include <QDebug>
#include <QSettings>
#include <QDir>
#include <QStandardPaths>
#define MAGIC_NUMBER 0x6F6E7365
#define MAGIC_VERSION 10
DataStore::DataStore()
{
connect(&download_manager, SIGNAL(gotData(QByteArray)), SLOT(onGotData(QByteArray)));
connect(&download_manager, SIGNAL(gotProgress(qint64,qint64)), SLOT(onGotProgress(qint64,qint64)));
QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
shows_file.setFileName(dataDir + "/shows.dat");
user_file.setFileName(dataDir + "/user.dat");
}
int DataStore::init(bool force)
{
data_version = QDataStream::Qt_5_6;
loadUser();
emit statusChanged(tr("Getting from cache..."));
load();
emit showsChanged();
emit statusChanged(tr("Ready."));
if(force || shows.empty()) {
emit statusChanged(tr("Downloading from network..."));
emit downloadStarted();
download_manager.singleShot("http://www.onsen.ag/easy/easy.php");
}
return 1;
}
int DataStore::load()
{
shows.clear();
if(!shows_file.exists())
return 1;
if(shows_file.open(QIODevice::ReadOnly)) {
QDataStream in(&shows_file);
/*quint32 magic;
in >> magic;
if (magic != MAGIC_NUMBER)
return 0; // invalid format
qint32 version;
in >> version;
if (version != MAGIC_VERSION)
return 0; // invalid version*/
in.setVersion(data_version);
in >> last_sync;
in >> shows;
shows_file.close();
return 1;
}
return 0;
}
void DataStore::save()
{
// Save show information
if (shows_file.open(QIODevice::WriteOnly)) {
QDataStream out(&shows_file);
/*out << (quint32)MAGIC_NUMBER;
out << (qint32)MAGIC_VERSION;*/
out.setVersion(data_version);
out << last_sync;
out << shows;
shows_file.flush();
shows_file.close();
} else
qDebug() << "Warning: Couldn't write data store";
}
int DataStore::loadUser()
{
favorites.clear();
viewDates.clear();
if(!user_file.exists())
return 1;
if (user_file.open(QIODevice::ReadOnly)) {
QDataStream in(&user_file);
/*quint32 magic;
in >> magic;
if (magic != MAGIC_NUMBER)
return 0; // invalid format
qint32 version;
in >> version;
if (version != MAGIC_VERSION)
return 0; // invalid version*/
in.setVersion(data_version);
in >> favorites;
in >> viewDates;
user_file.flush();
user_file.close();
return 1;
}
return 0;
}
void DataStore::saveUser()
{
if (user_file.open(QIODevice::WriteOnly))
{
QDataStream out(&user_file);
/*out << (quint32)MAGIC_NUMBER;
out << (qint32)MAGIC_VERSION;*/
out.setVersion(data_version);
out << favorites;
out << viewDates;
user_file.flush();
user_file.close();
}
}
/*void DataStore::addToArchive(const Show & cur_show) {
//shows_archive.append(cur_show);
}*/
void DataStore::setFavorite(const Show & cur_show, bool fav)
{
if(fav)
favorites.insert(cur_show.id);
else
favorites.remove(cur_show.id);
saveUser();
}
bool DataStore::isFavorite(const Show & cur_show)
{
return favorites.contains(cur_show.id);
}
void DataStore::setWatched(const Show & cur_show, QDate date)
{
QString key = cur_show.id + QString::number(cur_show.ep);
if(!date.isNull())
viewDates.insert(key, date);
else
viewDates.remove(key);
saveUser();
}
bool DataStore::isWatched(const Show & cur_show)
{
QString key = cur_show.id + QString::number(cur_show.ep);
return cur_show.file.isEmpty() || (viewDates.contains(key) && viewDates[key] > cur_show.update);
}
void DataStore::onGotProgress(qint64 value, qint64 total)
{
emit downloadProgress(value, total);
}
void DataStore::onGotData(QByteArray data)
{
emit statusChanged(tr("Parsing..."));
QVector<Show> latest_shows = api_onsen.read(data);
/* Calculate new shows and report them */
QVector<Show> new_shows;
QVector<Show> new_fav_shows;
QMutableVectorIterator<Show> i(latest_shows);
while(i.hasNext()) {
Show & cur_show = i.next();
QString key = cur_show.id + QString::number(cur_show.ep);
if(!shows.contains(key)) {
shows.insert(key, cur_show);
new_shows.append(cur_show);
if(isFavorite(cur_show))
new_fav_shows.append(cur_show);
}
}
last_sync = api_onsen.latestDate();
emit statusChanged(tr("Saving to cache..."));
save();
emit showsChanged();
qDebug() << "New shows found:" << new_shows.size() << " Favorites:" << new_fav_shows.size();
emit downloadFinished(new_shows, new_fav_shows);
emit statusChanged(tr("Ready."));
}