diff options
Diffstat (limited to 'smartpensyncer.cc')
-rw-r--r-- | smartpensyncer.cc | 88 |
1 files changed, 62 insertions, 26 deletions
diff --git a/smartpensyncer.cc b/smartpensyncer.cc index f076ff9..3556f77 100644 --- a/smartpensyncer.cc +++ b/smartpensyncer.cc @@ -20,13 +20,13 @@ #include <QtCore/QScopedArrayPointer> #include <QtCore/QThread> #include <QtCore/QDebug> -#include <quazip/quazipfile.h> +#include <quazipfile.h> #include <cstdio> #include "paperreplay.h" #include "notebookmodel.h" #include "smartpensyncer.h" -#define BUFFER_SIZE 16 * 1024 +#define BUFFER_SIZE 128 * 1024 namespace { class LockFile @@ -79,7 +79,7 @@ bool LockFile::lock() QFileInfo info(_path); if (info.exists()) { - if (info.created().secsTo(QDateTime::currentDateTime()) > 10 * 60) { + if (info.lastModified().secsTo(QDateTime::currentDateTime()) > 10 * 60) { if (QFile::remove(info.filePath())) { qDebug() << "Removing stale lock file:" << info.absoluteFilePath(); } @@ -106,9 +106,9 @@ Smartpen::PenTime TimestampFile::get() { if (_f.exists()) { if (_f.open(QIODevice::ReadOnly | QIODevice::Text)) { - QString data = QString::fromAscii(_f.readLine(24)); + QString data = QString::fromUtf8(_f.readLine(32)); _f.close(); - return data.toLongLong(); + return data.toULongLong(); } else { qWarning() << "Could not read timestamp file:" << _f.fileName(); } @@ -120,18 +120,21 @@ Smartpen::PenTime TimestampFile::get() void TimestampFile::set(Smartpen::PenTime time) { if (_f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - _f.write(QString::number(time).toAscii()); + _f.write(QString::number(time).toUtf8()); _f.close(); } else { qWarning() << "Could not set timestamp file:" << _f.fileName(); return; } } -} +} /* anonymous namespace */ SmartpenSyncer::SmartpenSyncer(const Smartpen::Address &addr, QObject *parent) : - QThread(parent), _addr(addr), _pen(new Smartpen(this)), _errored(false), _aborted(false) + QThread(parent), + _addr(addr), _errored(false), _aborted(false), + _pen(new Smartpen(this)) { + connect(_pen, SIGNAL(linkError(QString)), SLOT(handleLinkError(QString))); } SmartpenSyncer::~SmartpenSyncer() @@ -147,11 +150,6 @@ Smartpen::Address SmartpenSyncer::penAddress() const return _addr; } -QString SmartpenSyncer::penName() const -{ - return _penName; -} - bool SmartpenSyncer::hasErrors() const { return _errored; @@ -162,6 +160,12 @@ void SmartpenSyncer::abort() _aborted = true; } +void SmartpenSyncer::reset() +{ + _aborted = false; + _errored = false; +} + void SmartpenSyncer::run() { if (!_pen->connectToPen(_addr)) { @@ -170,19 +174,29 @@ void SmartpenSyncer::run() return; } + if (_aborted) { + _pen->disconnectFromPen(); + return; + } + _penName = _pen->getPenName(); qDebug() << "got pen name:" << _penName; - emit penNameChanged(); + + if (_penName.isEmpty()) { + _penName = _pen->getPenSerial(); + qDebug() << "pen with no name, using pen serial instead:" << _penName; + } + + emit gotPenName(_penName); QVariantMap penInfo = _pen->getPenInfo(); if (penInfo.isEmpty()) { qWarning() << "Could not get pen info"; _errored = true; + _pen->disconnectFromPen(); return; } - _penSerial = penInfo["penserial"].toString(); - _penDataDir.setPath(NotebookModel::userDataDirectory() + "/" + _penName + ".pen"); if (!_penDataDir.exists()) { if (!_penDataDir.mkpath(".")) { @@ -194,6 +208,12 @@ void SmartpenSyncer::run() _errored = true; } + if (_errored) { + qDebug() << "Sync finished with errors"; + } else { + qDebug() << "Sync finished"; + } + _pen->disconnectFromPen(); } @@ -205,10 +225,22 @@ bool SmartpenSyncer::syncPen() return false; } - TimestampFile lastSyncFile(_penDataDir.filePath(".lastsync")); + // Get the current user time offset from the pen + // and store it so that we have it even when the pen is offline + TimestampFile userTimeFile(_penDataDir.filePath(PEN_USER_TIME_FILE)); + Smartpen::PenTime userTime = _pen->getPenTime(Smartpen::Parameter::UserTime); + userTimeFile.set(userTime); + qDebug() << "pen time base:" << userTime << Smartpen::fromPenTime(userTime, 0); + + Smartpen::PenTime penTime = _pen->getPenTime(Smartpen::Parameter::RtcTime); + qDebug() << "pen current time:" << penTime << Smartpen::fromPenTime(userTime, penTime); + + // Read when is the last time we synchronized with this pen (in PenTime, not user time) + TimestampFile lastSyncFile(_penDataDir.filePath(PEN_LAST_SYNC_FILE)); Smartpen::PenTime lastSync = lastSyncFile.get(); if (lastSync != 0) lastSync += 1; // We want the changes _from_ the last sync + // Ask the pen for all the changes which happened since the last sync time QList<Smartpen::ChangeReport> changes = _pen->getChangeList(lastSync); Smartpen::PenTime changesEndTime = 0; @@ -248,16 +280,16 @@ bool SmartpenSyncer::syncPen() bool SmartpenSyncer::syncNotebook(Smartpen::PenTime lastSync, const Smartpen::ChangeReport &change) { - QDir notebookDir(_penDataDir.filePath(change.title + ".afd")); + QDir notebookDir(_penDataDir.filePath(change.title + "." AFD_NOTEBOOK_EXTENSION)); if (!notebookDir.exists()) { if (!notebookDir.mkpath(".")) { qWarning() << "Cannot create notebook data directory:" << notebookDir.absolutePath(); } } - LockFile lock(notebookDir.filePath(".sync.lck")); + LockFile lock(notebookDir.filePath(PEN_SYNC_LOCK_FILE)); if (!lock.lock()) { - qWarning() << "Notebook is already being synchronized; delete this file if it is not:" << notebookDir.absoluteFilePath(".sync.lck"); + qWarning() << "Notebook is already being synchronized; delete this file if it is not:" << notebookDir.absoluteFilePath(PEN_SYNC_LOCK_FILE); return false; } @@ -279,9 +311,9 @@ bool SmartpenSyncer::syncPaperReplay(Smartpen::PenTime lastSync, const Smartpen: } } - LockFile lock(replayDir.filePath(".sync.lck")); + LockFile lock(replayDir.filePath(PEN_SYNC_LOCK_FILE)); if (!lock.lock()) { - qWarning() << "Paper replay is already being synchronized; delete this file if it is not:" << replayDir.absoluteFilePath(".sync.lck"); + qWarning() << "Paper replay is already being synchronized; delete this file if it is not:" << replayDir.absoluteFilePath(PEN_SYNC_LOCK_FILE); return false; } @@ -305,7 +337,7 @@ bool SmartpenSyncer::extractZip(QByteArray &zipData, QDir &dir) return false; } - QScopedArrayPointer<char> buffer(new char[BUFFER_SIZE]); + QByteArray buffer(BUFFER_SIZE, Qt::Uninitialized); for (bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) { QString zipName = zip.getCurrentFileName(); @@ -336,7 +368,7 @@ bool SmartpenSyncer::extractZip(QByteArray &zipData, QDir &dir) } while (!zipFile.atEnd()) { - qint64 read = zipFile.read(buffer.data(), BUFFER_SIZE); + qint64 read = zipFile.read(buffer.data(), buffer.size()); if (read <= 0) { qWarning() << "short read on:" << zipName; zipFile.close(); @@ -354,8 +386,6 @@ bool SmartpenSyncer::extractZip(QByteArray &zipData, QDir &dir) zipFile.close(); } - buffer.reset(); - if (zip.getZipError() == UNZ_OK) { return true; } else { @@ -363,3 +393,9 @@ bool SmartpenSyncer::extractZip(QByteArray &zipData, QDir &dir) return false; } } + +void SmartpenSyncer::handleLinkError(const QString &msg) +{ + qWarning() << "link error:" << msg; + _errored = true; +} |