Fix divecomputer nickname handling

Don't overwrite existing data.

[Dirk Hohndel: rewrote this a litte, but the logic is the same]

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2015-09-17 14:38:20 -07:00 committed by Dirk Hohndel
parent ca6f2c238a
commit 09ded17e1c
2 changed files with 29 additions and 12 deletions

View file

@ -58,25 +58,41 @@ const DiveComputerNode *DiveComputerList::get(const QString &m)
return NULL; return NULL;
} }
void DiveComputerList::addDC(const QString &m, uint32_t d, const QString &n, const QString &s, const QString &f) void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const
{
if (nickName != n)
qDebug("new nickname %s for DC model %s deviceId 0x%x", n.toUtf8().data(), model.toUtf8().data(), deviceId);
if (serialNumber != s)
qDebug("new serial number %s for DC model %s deviceId 0x%x", s.toUtf8().data(), model.toUtf8().data(), deviceId);
if (firmware != f)
qDebug("new firmware version %s for DC model %s deviceId 0x%x", f.toUtf8().data(), model.toUtf8().data(), deviceId);
}
void DiveComputerList::addDC(QString m, uint32_t d, QString n, QString s, QString f)
{ {
if (m.isEmpty() || d == 0) if (m.isEmpty() || d == 0)
return; return;
const DiveComputerNode *existNode = this->getExact(m, d); const DiveComputerNode *existNode = this->getExact(m, d);
DiveComputerNode newNode(m, d, s, f, n);
if (existNode) { if (existNode) {
if (newNode.changesValues(*existNode)) { // Update any non-existent fields from the old entry
if (n.size() && existNode->nickName != n) if (n.isEmpty())
qDebug("new nickname %s for DC model %s deviceId 0x%x", n.toUtf8().data(), m.toUtf8().data(), d); n = existNode->nickName;
if (f.size() && existNode->firmware != f) if (s.isEmpty())
qDebug("new firmware version %s for DC model %s deviceId 0x%x", f.toUtf8().data(), m.toUtf8().data(), d); s = existNode->serialNumber;
if (s.size() && existNode->serialNumber != s) if (f.isEmpty())
qDebug("new serial number %s for DC model %s deviceId 0x%x", s.toUtf8().data(), m.toUtf8().data(), d); f = existNode->firmware;
} else {
// Do all the old values match?
if (n == existNode->nickName && s == existNode->serialNumber && f == existNode->firmware)
return; return;
}
// debugging: show changes
existNode->showchanges(n, s, f);
dcMap.remove(m, *existNode); dcMap.remove(m, *existNode);
} }
DiveComputerNode newNode(m, d, s, f, n);
dcMap.insert(m, newNode); dcMap.insert(m, newNode);
} }

View file

@ -12,6 +12,7 @@ public:
bool operator==(const DiveComputerNode &a) const; bool operator==(const DiveComputerNode &a) const;
bool operator!=(const DiveComputerNode &a) const; bool operator!=(const DiveComputerNode &a) const;
bool changesValues(const DiveComputerNode &b) const; bool changesValues(const DiveComputerNode &b) const;
void showchanges(const QString &n, const QString &s, const QString &f) const;
QString model; QString model;
uint32_t deviceId; uint32_t deviceId;
QString serialNumber; QString serialNumber;
@ -25,7 +26,7 @@ public:
~DiveComputerList(); ~DiveComputerList();
const DiveComputerNode *getExact(const QString &m, uint32_t d); const DiveComputerNode *getExact(const QString &m, uint32_t d);
const DiveComputerNode *get(const QString &m); const DiveComputerNode *get(const QString &m);
void addDC(const QString &m, uint32_t d, const QString &n = QString(), const QString &s = QString(), const QString &f = QString()); void addDC(QString m, uint32_t d, QString n = QString(), QString s = QString(), QString f = QString());
DiveComputerNode matchDC(const QString &m, uint32_t d); DiveComputerNode matchDC(const QString &m, uint32_t d);
DiveComputerNode matchModel(const QString &m); DiveComputerNode matchModel(const QString &m);
QMultiMap<QString, DiveComputerNode> dcMap; QMultiMap<QString, DiveComputerNode> dcMap;