From d334906cdb5ad988c9e3ad1e9827eab774d1a283 Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 21 Jun 2015 20:49:42 +0200 Subject: use pen time, not host wall clock to store lastsync timestamp --- smartpen.cc | 33 ++++++++++++++++++------ smartpen.h | 17 +++++++------ smartpensyncer.cc | 75 ++++++++++++++++++++++++++++++++----------------------- smartpensyncer.h | 4 +-- 4 files changed, 82 insertions(+), 47 deletions(-) diff --git a/smartpen.cc b/smartpen.cc index c89cf2d..79e9a01 100644 --- a/smartpen.cc +++ b/smartpen.cc @@ -157,12 +157,14 @@ QVariantMap Smartpen::getPenInfo() return result; } -QList Smartpen::getChangeList(const QDateTime &from) +QList Smartpen::getChangeList(PenTime from) { QList result; - QByteArray data = getObject(QString("changelist?start_time=%1").arg(toPenTime(from))); + QByteArray data = getObject(QString("changelist?start_time=%1").arg(from)); QXmlStreamReader r(data); + qDebug() << QString::fromAscii(data); + advanceToFirstChildElement(r, "xml"); advanceToFirstChildElement(r, "changelist"); @@ -176,13 +178,30 @@ QList Smartpen::getChangeList(const QDateTime &from) if (attrs.hasAttribute("guid")) { report.guid = attrs.value("guid").toString(); report.title = attrs.value("title").toString(); + + // Figure out the change record endtime to enumerating the + // endtime of all the record's changed pages + report.endTime = 0; + while (r.readNextStartElement()) { + if (r.name() == "page") { + QXmlStreamAttributes attrs = r.attributes(); + PenTime endTime = attrs.value("end_time").toString().toLongLong(); + if (endTime > report.endTime) report.endTime = endTime; + } + r.skipCurrentElement(); + } + result.append(report); } else if (attrs.hasAttribute("classname")) { report.className = attrs.value("classname").toString(); report.title = attrs.value("title").toString(); + report.endTime = attrs.value("end_time").toString().toLongLong(); result.append(report); + r.skipCurrentElement(); + } else { + qWarning() << "Unknown change report format"; + r.skipCurrentElement(); } - r.skipCurrentElement(); } else { r.skipCurrentElement(); } @@ -195,14 +214,14 @@ QList Smartpen::getChangeList(const QDateTime &from) return result; } -QByteArray Smartpen::getLspData(const QString &name, const QDateTime &from) +QByteArray Smartpen::getLspData(const QString &name, PenTime from) { - return getObject(QString("lspdata?name=%1&start_time=%2").arg(name).arg(toPenTime(from))); + return getObject(QString("lspdata?name=%1&start_time=%2").arg(name).arg(from)); } -QByteArray Smartpen::getPaperReplay(const QDateTime &from) +QByteArray Smartpen::getPaperReplay(PenTime from) { - return getObject(QString("lspdata?name=com.livescribe.paperreplay.PaperReplay&start_time=%1&returnVersion=0.3&remoteCaller=WIN_LD_200").arg(toPenTime(from))); + return getObject(QString("lspdata?name=com.livescribe.paperreplay.PaperReplay&start_time=%1&returnVersion=0.3&remoteCaller=WIN_LD_200").arg(from)); } qint64 Smartpen::toPenTime(const QDateTime &dt) diff --git a/smartpen.h b/smartpen.h index 12ed898..18ac955 100644 --- a/smartpen.h +++ b/smartpen.h @@ -40,6 +40,8 @@ public: ~Smartpen(); typedef QPair Address; + typedef qint64 PenTime; + typedef quint64 PenId; bool isConnected() const; @@ -57,18 +59,19 @@ public: QString guid; QString className; QString title; + PenTime endTime; }; - QList getChangeList(const QDateTime &from = QDateTime()); + QList getChangeList(PenTime from = 0); - QByteArray getLspData(const QString &name, const QDateTime &from = QDateTime()); - QByteArray getPaperReplay(const QDateTime &from = QDateTime()); + QByteArray getLspData(const QString &name, PenTime from = 0); + QByteArray getPaperReplay(PenTime from = 0); - static qint64 toPenTime(const QDateTime &dt); - static QDateTime fromPenTime(qint64 t); + static PenTime toPenTime(const QDateTime &dt); + static QDateTime fromPenTime(PenTime t); - static QString toPenSerial(quint64 id); - static quint64 toPenId(const QString &serial); + static QString toPenSerial(PenId id); + static PenId toPenId(const QString &serial); public slots: bool connectToPen(const Address &addr); diff --git a/smartpensyncer.cc b/smartpensyncer.cc index cdf9bc6..f076ff9 100644 --- a/smartpensyncer.cc +++ b/smartpensyncer.cc @@ -48,11 +48,11 @@ class TimestampFile public: TimestampFile(const QString &path); - QDateTime get(); - void set(); + Smartpen::PenTime get(); + void set(Smartpen::PenTime time); private: - QFileInfo _fi; + QFile _f; }; LockFile::LockFile(const QString &path) @@ -98,29 +98,34 @@ bool LockFile::lock() } TimestampFile::TimestampFile(const QString &path) - : _fi(path) + : _f(path) { } -QDateTime TimestampFile::get() +Smartpen::PenTime TimestampFile::get() { - qDebug() << "Checking timestamp" << _fi.filePath(); - if (_fi.exists()) { - return _fi.lastModified(); - } else { - return QDateTime(); + if (_f.exists()) { + if (_f.open(QIODevice::ReadOnly | QIODevice::Text)) { + QString data = QString::fromAscii(_f.readLine(24)); + _f.close(); + return data.toLongLong(); + } else { + qWarning() << "Could not read timestamp file:" << _f.fileName(); + } } + + return 0; } -void TimestampFile::set() +void TimestampFile::set(Smartpen::PenTime time) { - QFile f(_fi.filePath()); - if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - qWarning() << "Could not set timestamp file:" << _fi.absoluteFilePath(); + if (_f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { + _f.write(QString::number(time).toAscii()); + _f.close(); + } else { + qWarning() << "Could not set timestamp file:" << _f.fileName(); return; } - f.close(); - _fi.refresh(); } } @@ -200,34 +205,48 @@ bool SmartpenSyncer::syncPen() return false; } - TimestampFile lastSync(_penDataDir.filePath(".lastsync")); - QList changes = _pen->getChangeList(lastSync.get()); + TimestampFile lastSyncFile(_penDataDir.filePath(".lastsync")); + Smartpen::PenTime lastSync = lastSyncFile.get(); + if (lastSync != 0) lastSync += 1; // We want the changes _from_ the last sync + + QList changes = _pen->getChangeList(lastSync); + + Smartpen::PenTime changesEndTime = 0; foreach(const Smartpen::ChangeReport &change, changes) { if (!change.guid.isEmpty()) { qDebug() << "Synchronizing guid: " << change.guid << change.title; - if (!syncNotebook(change)) { + if (!syncNotebook(lastSync, change)) { return false; } } else if (change.className == "com.livescribe.paperreplay.PaperReplay") { qDebug() << "Synchronizing paper replay"; - if (!syncPaperReplay()) { + if (!syncPaperReplay(lastSync, change)) { return false; } + } else { + qWarning() << "Unknown change report"; + return false; } if (_aborted) { qWarning() << "Aborting sync"; return false; } + + if (change.endTime > changesEndTime) { + changesEndTime = change.endTime; + } } - lastSync.set(); + if (changesEndTime > 0) { + lastSyncFile.set(changesEndTime); + } return true; } -bool SmartpenSyncer::syncNotebook(const Smartpen::ChangeReport &change) +bool SmartpenSyncer::syncNotebook(Smartpen::PenTime lastSync, const Smartpen::ChangeReport &change) { QDir notebookDir(_penDataDir.filePath(change.title + ".afd")); if (!notebookDir.exists()) { @@ -242,19 +261,16 @@ bool SmartpenSyncer::syncNotebook(const Smartpen::ChangeReport &change) return false; } - TimestampFile lastSync(notebookDir.filePath(".lastsync")); - QByteArray lspData = _pen->getLspData(change.guid, lastSync.get()); + QByteArray lspData = _pen->getLspData(change.guid, lastSync); if (!extractZip(lspData, notebookDir)) { return false; } - lastSync.set(); - return true; } -bool SmartpenSyncer::syncPaperReplay() +bool SmartpenSyncer::syncPaperReplay(Smartpen::PenTime lastSync, const Smartpen::ChangeReport &) { QDir replayDir(_penDataDir.filePath(PAPER_REPLAY)); if (!replayDir.exists()) { @@ -269,15 +285,12 @@ bool SmartpenSyncer::syncPaperReplay() return false; } - TimestampFile lastSync(replayDir.filePath(".lastsync")); - QByteArray replayData = _pen->getPaperReplay(lastSync.get()); + QByteArray replayData = _pen->getPaperReplay(lastSync); if (!extractZip(replayData, replayDir)) { return false; } - lastSync.set(); - return true; } diff --git a/smartpensyncer.h b/smartpensyncer.h index 57ac6d7..ea31917 100644 --- a/smartpensyncer.h +++ b/smartpensyncer.h @@ -44,8 +44,8 @@ public slots: private: void run(); bool syncPen(); - bool syncNotebook(const Smartpen::ChangeReport &change); - bool syncPaperReplay(); + bool syncNotebook(Smartpen::PenTime lastSync, const Smartpen::ChangeReport &change); + bool syncPaperReplay(Smartpen::PenTime lastSync, const Smartpen::ChangeReport &change); bool extractZip(QByteArray &zipData, QDir &dir); private: -- cgit v1.2.3