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
|
#include <QtCore/QBuffer>
#include <QtCore/QScopedArrayPointer>
#include <QtCore/QThread>
#include <QtCore/QDebug>
#include <QtGui/QDesktopServices>
#include <quazip/quazipfile.h>
#include "paperreplay.h"
#include "smartpensyncer.h"
#define BUFFER_SIZE 16 * 1024
namespace {
static QDateTime getTimestampFileDate(const QString &path)
{
QFileInfo info(path);
qDebug() << "Checking timestamp" << info.filePath();
if (info.exists()) {
return info.lastModified();
} else {
return QDateTime();
}
}
static void setTimestampFileDate(const QString &path)
{
QFile f(path);
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning() << "Could not set timestamp file:" << path;
return;
}
f.close();
}
void removeTimestampFile(const QString &path)
{
QFile f(path);
if (!f.remove()) {
qWarning() << "Cannot remove timestamp file:" << path;
}
}
}
SmartpenSyncer::SmartpenSyncer(const Smartpen::Address &addr, QObject *parent) :
QThread(parent), _addr(addr), _pen(new Smartpen(this)), _errored(false), _aborted(false)
{
}
SmartpenSyncer::~SmartpenSyncer()
{
if (isRunning()) {
_aborted = true;
wait();
}
}
Smartpen::Address SmartpenSyncer::penAddress() const
{
return _addr;
}
QString SmartpenSyncer::penName() const
{
return _penName;
}
void SmartpenSyncer::abort()
{
_aborted = true;
}
void SmartpenSyncer::run()
{
if (!_pen->connectToPen(_addr)) {
qWarning() << "Could not connect to pen with USB address: " << _addr;
_errored = true;
return;
}
_penName = _pen->getPenName();
qDebug() << "got pen name:" << _penName;
emit penNameChanged();
QVariantMap penInfo = _pen->getPenInfo();
if (penInfo.isEmpty()) {
qWarning() << "Could not get pen info";
_errored = true;
return;
}
_penSerial = penInfo["penserial"].toString();
_penDataDir.setPath(QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/" + _penName + ".pen");
if (!_penDataDir.exists()) {
if (!_penDataDir.mkpath(".")) {
qWarning() << "Cannot create pen data directory:" << _penDataDir.absolutePath();
}
}
if (!syncPen()) {
_errored = true;
}
_pen->disconnectFromPen();
}
bool SmartpenSyncer::syncPen()
{
QDateTime lastSyncTime = getTimestampFileDate(_penDataDir.filePath(".lastsync"));
QList<Smartpen::ChangeReport> changes = _pen->getChangeList(lastSyncTime);
setTimestampFileDate(_penDataDir.filePath(".sync.lck"));
foreach(const Smartpen::ChangeReport &change, changes) {
if (!change.guid.isEmpty()) {
qDebug() << "Synchronizing guid: " << change.guid << change.title;
if (!syncNotebook(change)) {
return false;
}
} else if (change.className == "com.livescribe.paperreplay.PaperReplay") {
qDebug() << "Synchronizing paper replay";
if (!syncPaperReplay()) {
return false;
}
}
}
setTimestampFileDate(_penDataDir.filePath(".lastsync"));
removeTimestampFile(_penDataDir.filePath(".sync.lck"));
return true;
}
bool SmartpenSyncer::syncNotebook(const Smartpen::ChangeReport &change)
{
QDir notebookDir(_penDataDir.filePath(change.title + ".afd"));
if (!notebookDir.exists()) {
if (!notebookDir.mkpath(".")) {
qWarning() << "Cannot create notebook data directory:" << notebookDir.absolutePath();
}
}
setTimestampFileDate(notebookDir.filePath(".sync.lck"));
QDateTime lastSyncTime = getTimestampFileDate(notebookDir.filePath(".lastsync"));
QByteArray lspData = _pen->getLspData(change.guid, lastSyncTime);
if (!extractZip(lspData, notebookDir)) {
return false;
}
setTimestampFileDate(notebookDir.filePath(".lastsync"));
removeTimestampFile(notebookDir.filePath(".sync.lck"));
return true;
}
bool SmartpenSyncer::syncPaperReplay()
{
QDir replayDir(_penDataDir.filePath(PAPER_REPLAY));
if (!replayDir.exists()) {
if (!replayDir.mkpath(".")) {
qWarning() << "Cannot create PaperReplay data directory:" << replayDir.absolutePath();
}
}
setTimestampFileDate(replayDir.filePath(".sync.lck"));
QDateTime lastSyncTime = getTimestampFileDate(replayDir.filePath(".lastsync"));
QByteArray replayData = _pen->getPaperReplay(lastSyncTime);
if (!extractZip(replayData, replayDir)) {
return false;
}
setTimestampFileDate(replayDir.filePath(".lastsync"));
removeTimestampFile(replayDir.filePath(".sync.lck"));
return true;
}
bool SmartpenSyncer::extractZip(QByteArray &zipData, QDir &dir)
{
QBuffer zipBuffer(&zipData);
QuaZip zip(&zipBuffer);
QuaZipFile zipFile(&zip);
if (!zip.open(QuaZip::mdUnzip)) {
qWarning() << "Could not open zip file";
return false;
}
QScopedArrayPointer<char> buffer(new char[BUFFER_SIZE]);
for (bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
QString zipName = zip.getCurrentFileName();
if (!dir.absoluteFilePath(zipName).startsWith(dir.absolutePath())) {
qWarning() << "broken zip filename:" << zipName;
continue;
}
QFileInfo finfo(dir.filePath(zipName));
if (!dir.mkpath(finfo.path())) {
qWarning() << "cannot mkpath for:" << finfo.absoluteFilePath();
}
if (zipName.endsWith('/')) {
// Nothing to do
continue;
}
if (!zipFile.open(QIODevice::ReadOnly)) {
qWarning() << "cannot open zip file for reading:" << zipName;
continue;
}
QFile file(finfo.filePath());
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning() << "cannot open for writing:" << finfo.absoluteFilePath();
zipFile.close();
continue;
}
while (!zipFile.atEnd()) {
qint64 read = zipFile.read(buffer.data(), BUFFER_SIZE);
if (read <= 0) {
qWarning() << "short read on:" << zipName;
zipFile.close();
continue;
}
qint64 written = file.write(buffer.data(), read);
if (written != read) {
qWarning() << "short write on:" << file.fileName();
zipFile.close();
continue;
}
}
file.close();
zipFile.close();
}
buffer.reset();
if (zip.getZipError() == UNZ_OK) {
return true;
} else {
qWarning() << "Error while decompressing";
return false;
}
}
|