aboutsummaryrefslogtreecommitdiff
path: root/paperreplay.cc
blob: 8bfb20e40b44d126729ac6894f9a0f4e7b72bb7c (plain)
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
#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)
{
}

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()) {
		QDir sessionDir(iter.next());
		bool ok;
		quint64 sessionId = sessionDir.dirName().mid(4).toULongLong(&ok, 16);
		if (!ok) {
			qWarning() << "Invalid session identifier:" << sessionDir.dirName();
		}
		Session &session = _sessions[sessionId];
		if (!parseSessionInfo(session, sessionDir.filePath("session.info"))) {
			qWarning() << "Could not parse:" << sessionDir.absoluteFilePath("session.info");
		}
		if (!parseSessionPages(session, sessionDir.filePath("session.pages"))) {
			qWarning() << "Could not parse:" << sessionDir.absoluteFilePath("session.pages");
		}
	}

	return true;
}

void PaperReplay::close()
{
	_sessions.clear();
	_byPage.clear();
	_dir.setPath(QString());
}

QList<PaperReplay::Session> PaperReplay::sessions()
{
	return _sessions.values();
}

QList<PaperReplay::Session> PaperReplay::sessions(quint64 pageAddress)
{
	QList<Session> sessions;
	QMultiMap<quint64, quint64>::iterator it = _byPage.find(pageAddress);
	while (it != _byPage.end() && it.key() == pageAddress) {
		sessions.append(_sessions[it.value()]);
	}
	return sessions;
}

bool PaperReplay::parseSessionInfo(Session &session, const QString &path)
{
	QFile f(path);
	if (f.open(QIODevice::ReadOnly)) {
		return parseSessionInfo(session, &f);
	} else {
		return false;
	}
}

bool PaperReplay::parseSessionInfo(Session &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(Session &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;
	}

	qDebug() << "Name:" << name << Smartpen::fromPenTime(startTime) << Smartpen::fromPenTime(endTime) << creationTime;

	quint16 num_clips;
	s >> num_clips;

	for (uint i = 0; i < num_clips; ++i) {
		QString file;
		if (!readUtfString(s, file)) {
			return false;
		}
		s >> startTime >> endTime;
		qDebug() << " Clip:" << file << startTime << endTime;
		qDebug() << "    " << QDateTime::fromTime_t(startTime) << QDateTime::fromTime_t(endTime);
	}

	quint16 num_strokes;
	s >> num_strokes;

	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(Session &session, const QString &path)
{
	QFile f(path);
	if (f.open(QIODevice::ReadOnly)) {
		return parseSessionPages(session, &f);
	} else {
		return false;
	}
}

bool PaperReplay::parseSessionPages(Session &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(Session &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, unk;
		s >> address >> unk;

		session.pages.append(address);
		qDebug() << " Page:" << address << unk;
	}

	return true;
}