Cleanup: Fold add_hash() call into learnHash() function

learnHash() was always called in conjunction with add_hash(). The
pattern was that a local filename and a hash were connected in
the hash-to-filename and the filename-to-hash maps. Then, the
original picture-filename or url were registered in the filename-to-hash
map.

This commit changes learnHash() to take three parameters (original-filename,
local-filename and hash) and do all of the above. The new code is
simpler because no dummy picture struct has to be generated in
DiveListView::loadImageFromURL().

The tests were extended to check for all hash<->filename associations.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-02-18 14:57:42 +01:00 committed by Lubomir I. Ivanov
parent b750a48f0f
commit f5eafe2a71
5 changed files with 21 additions and 14 deletions

View file

@ -71,8 +71,7 @@ void ImageDownloader::saveImage(QNetworkReply *reply, bool &success)
stream.writeRawData(imageData.data(), imageData.length());
imageFile.waitForBytesWritten(-1);
imageFile.close();
add_hash(imageFile.fileName(), hash.result());
learnHash(picture, hash.result());
learnHash(QString(picture->filename), imageFile.fileName(), hash.result());
}
// This should be called to make the picture actually show.
// Problem is DivePictureModel is not in core.

View file

@ -1149,12 +1149,13 @@ QByteArray hashFile(const QString filename)
}
}
void learnHash(const struct picture *picture, QByteArray hash)
void learnHash(const QString &originalName, const QString &localName, const QByteArray &hash)
{
if (hash.isNull())
return;
add_hash(localName, hash);
QMutexLocker locker(&hashOfMutex);
hashOf[QString(picture->filename)] = hash;
hashOf[originalName] = hash;
}
static bool haveHash(const QString &filename)

View file

@ -40,7 +40,7 @@ void hashPicture(struct picture *picture);
extern "C" char *hashstring(const char *filename);
QString localFilePath(const QString originalFilename);
QString fileFromHash(const char *hash);
void learnHash(const struct picture *picture, QByteArray hash);
void learnHash(const QString &originalName, const QString &localName, const QByteArray &hash);
weight_t string_to_weight(const char *str);
depth_t string_to_depth(const char *str);
pressure_t string_to_pressure(const char *str);

View file

@ -1000,11 +1000,7 @@ void DiveListView::loadImageFromURL(QUrl url)
stream.writeRawData(imageData.data(), imageData.length());
imageFile.waitForBytesWritten(-1);
imageFile.close();
add_hash(imageFile.fileName(), hash.result());
struct picture picture;
picture.hash = NULL;
picture.filename = strdup(url.toString().toUtf8().data());
learnHash(&picture, hash.result());
learnHash(url.toString(), imageFile.fileName(), hash.result());
matchImagesToDives(QStringList(url.toString()));
}
}

View file

@ -12,6 +12,11 @@ void TestPicture::initTestCase()
Q_INIT_RESOURCE(subsurface);
}
#define PIC1_NAME "/dives/images/wreck.jpg"
#define PIC2_NAME "/dives/images/data_after_EOI.jpg"
#define PIC1_HASH "929ad9499b7ae7a9e39ef63eb6c239604ac2adfa"
#define PIC2_HASH "fa8bd48f8f24017a81e1204f52300bd98b43d4a7"
void TestPicture::addPicture()
{
struct dive *dive;
@ -41,10 +46,16 @@ void TestPicture::addPicture()
QVERIFY(pic1->hash == NULL);
QVERIFY(pic2->hash == NULL);
learnHash(pic1, hashFile(localFilePath(pic1->filename)));
learnHash(pic2, hashFile(localFilePath(pic2->filename)));
QCOMPARE(hashstring(pic1->filename), "929ad9499b7ae7a9e39ef63eb6c239604ac2adfa");
QCOMPARE(hashstring(pic2->filename), "fa8bd48f8f24017a81e1204f52300bd98b43d4a7");
QByteArray hash1 = hashFile(localFilePath(pic1->filename));
QByteArray hash2 = hashFile(localFilePath(pic2->filename));
learnHash(pic1->filename, PIC1_NAME, hash1);
learnHash(pic2->filename, PIC2_NAME, hash2);
QCOMPARE(hashstring(pic1->filename), PIC1_HASH);
QCOMPARE(hashstring(pic2->filename), PIC2_HASH);
QCOMPARE(hashstring(PIC1_NAME), PIC1_HASH);
QCOMPARE(hashstring(PIC2_NAME), PIC2_HASH);
QCOMPARE(fileFromHash(PIC1_HASH), QString(PIC1_NAME));
QCOMPARE(fileFromHash(PIC2_HASH), QString(PIC2_NAME));
}