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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#include <QtCore/QDebug>
#include <QtCore/QDataStream>
#include <QtCore/QDirIterator>
#include "smartpen.h"
#include "paperreplay.h"
namespace
{
bool readUtfString(QDataStream &stream, QString &s)
{
quint16 len;
stream >> len;
QByteArray buffer(len, Qt::Uninitialized);
if (stream.readRawData(buffer.data(), len) != len) {
return false;
}
s = QString::fromUtf8(buffer);
return true;
}
}
PaperReplay::PaperReplay(QObject *parent) :
QObject(parent)
{
}
PaperReplay::Session::Session() : d()
{
}
PaperReplay::Session::Session(quint64 id) : d(new SessionData)
{
d->id = id;
}
PaperReplay::Session::~Session()
{
}
bool PaperReplay::Session::isValid() const
{
return d;
}
quint64 PaperReplay::Session::id() const
{
return d ? d->id : 0;
}
QString PaperReplay::Session::fileName() const
{
return d->file;
}
qint64 PaperReplay::Session::startTime() const
{
return d->start;
}
qint64 PaperReplay::Session::endTime() const
{
return d->end;
}
PaperReplay::SessionList::SessionList()
{
}
PaperReplay::SessionList::SessionList(const QMap<qint64, Session> &byTime)
: _m(byTime)
{
}
QList<PaperReplay::Session> PaperReplay::SessionList::sessionsDuringTime(qint64 time) const
{
QList<Session> sessions;
if (_m.isEmpty()) return sessions;
QMap<qint64, Session>::const_iterator it = _m.lowerBound(time);
if (it == _m.end()) --it;
while (it->d->start <= time && time <= it->d->end) {
sessions.append(*it);
if (it == _m.begin()) {
break;
} else {
--it;
}
}
return sessions;
}
bool PaperReplay::open(const QString &path, const QString &penSerial, quint64 notebookGuid)
{
_dir.setPath(path + QString("/userdata/%1/Paper Replay/99/%2/sessions")
.arg(penSerial)
.arg(qulonglong(notebookGuid), notebookGuid == 0 ? 1 : 16, 16, QLatin1Char('0')));
if (!_dir.exists()) {
qDebug() << "Cannot open paper replay:" << _dir.absolutePath() << "does not exist";
return false;
}
QDirIterator iter(_dir.path(), QStringList("PRS-*"), QDir::Dirs | QDir::NoDotAndDotDot);
while (iter.hasNext()) {
bool ok;
QDir sessionDir(iter.next());
quint64 sessionId = sessionDir.dirName().mid(4).toULongLong(&ok, 16);
if (!ok) {
qWarning() << "Invalid session identifier:" << sessionDir.dirName();
continue;
}
Session session(sessionId);
if (!parseSessionInfo(session.d, sessionDir.filePath("session.info"))) {
qWarning() << "Could not parse:" << sessionDir.absoluteFilePath("session.info");
}
if (!parseSessionPages(session.d, sessionDir.filePath("session.pages"))) {
qWarning() << "Could not parse:" << sessionDir.absoluteFilePath("session.pages");
}
if (!session.d->file.isEmpty()) {
session.d->file = sessionDir.filePath(session.d->file);
}
foreach (quint64 page, session.d->pages) {
_byPageTime[page].insert(session.d->start, session);
}
}
return true;
}
void PaperReplay::close()
{
_byPageTime.clear();
_dir.setPath(QString());
}
PaperReplay::SessionList PaperReplay::sessions(quint64 pageAddress) const
{
return SessionList(_byPageTime[pageAddress]);
}
bool PaperReplay::parseSessionInfo(SessionData *session, const QString &path)
{
QFile f(path);
if (f.open(QIODevice::ReadOnly)) {
return parseSessionInfo(session, &f);
} else {
return false;
}
}
bool PaperReplay::parseSessionInfo(SessionData *session, QIODevice *dev)
{
unsigned char magic[2];
if (dev->read(reinterpret_cast<char*>(magic), 2) != 2 ||
magic[0] != 0xFA || magic[1] != 0xCE) {
qWarning() << "Invalid magic";
return false;
}
char version = 0;
if (!dev->getChar(&version)) {
qWarning() << "Short read while getting version number";
return false;
}
switch (version) {
case 3:
return parseSessionInfoV3(session, dev);
default:
qWarning() << "Unknown version:" << version;
return false;
}
}
bool PaperReplay::parseSessionInfoV3(SessionData *session, QIODevice *dev)
{
QDataStream s(dev);
if (s.skipRawData(5) != 5) return false;
qint64 startTime, endTime, creationTime;
QString name;
s >> startTime >> endTime >> creationTime;
if (!readUtfString(s, name)) {
return false;
}
session->name = name;
session->start = startTime;
session->end = endTime;
qDebug() << "Session:" << name << Smartpen::fromPenTime(session->start) << Smartpen::fromPenTime(session->end);
quint16 num_clips;
s >> num_clips;
Q_ASSERT(num_clips == 1); // TODO: We do not yet know how to handle this scenario
for (uint i = 0; i < num_clips; ++i) {
QString file;
if (!readUtfString(s, file)) {
return false;
}
s >> startTime >> endTime;
qDebug() << " Clip:" << file << Smartpen::fromPenTime(startTime) << Smartpen::fromPenTime(endTime);
session->file = file;
}
quint16 num_strokes;
s >> num_strokes;
// TODO:
for (uint i = 0; i < num_strokes; ++i) {
quint64 a, b, c;
quint32 d;
QString nbGuid;
quint8 f, g;
s >> a >> b >> c >> d;
if (!readUtfString(s, nbGuid)) {
return false;
}
s >> f >> g;
qDebug() << " Stroke:" << a << b << c << d << nbGuid << f << g;
}
return true;
}
bool PaperReplay::parseSessionPages(SessionData *session, const QString &path)
{
QFile f(path);
if (f.open(QIODevice::ReadOnly)) {
return parseSessionPages(session, &f);
} else {
return false;
}
}
bool PaperReplay::parseSessionPages(SessionData *session, QIODevice *dev)
{
unsigned char magic[2];
if (dev->read(reinterpret_cast<char*>(magic), 2) != 2 ||
magic[0] != 0xCA || magic[1] != 0xBE) {
qWarning() << "Invalid magic";
return false;
}
char version = 0;
if (!dev->getChar(&version)) {
qWarning() << "Short read while getting version number";
return false;
}
switch (version) {
case 1:
return parseSessionPagesV1(session, dev);
default:
qWarning() << "Unknown version:" << version;
return false;
}
}
bool PaperReplay::parseSessionPagesV1(SessionData *session, QIODevice *dev)
{
QDataStream s(dev);
if (s.skipRawData(1) != 1) return false;
quint16 num_pages = 0;
s >> num_pages;
session->pages.reserve(session->pages.size() + num_pages);
for (uint i = 0; i < num_pages; ++i) {
quint64 address;
qint64 time;
s >> address >> time;
session->pages.append(address);
qDebug() << " Page:" << address << time << Smartpen::fromPenTime(time);
}
return true;
}
|