From b02bf002a677e45dbaa4d28edadbaafc89a7badc Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Thu, 26 Feb 2015 14:39:42 +0100 Subject: Add hashes to images Upon successfull reading an image file, this computes a SHA1 hash of the image and saves it with the picture tag in the log file. When a file is not successfully loaded (for example because the log was created on a different computer) we look up the hash in a dictionary that maps hashes to local file names. That dictionary (actually two for both directions), is loaded on startup and saved upon destruction of the main window. Signed-off-by: Robert C. Helling Signed-off-by: Dirk Hohndel --- qthelper.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'qthelper.cpp') diff --git a/qthelper.cpp b/qthelper.cpp index 4cedc5597..74c67d769 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -27,6 +27,11 @@ #include #include #include +#include +#include +#include +#include +#include "divepicturewidget.h" #include @@ -780,3 +785,67 @@ extern "C" void reverseGeoLookup(degrees_t latitude, degrees_t longitude, uint32 ds->notes = add_to_string(ds->notes, "countrytag: %s", address.value("country").toString().toUtf8().data()); } } + +QHash hashOf; +QHash localFilenameOf; + +extern "C" char * hashstring(char * filename) +{ + return hashOf[QString(filename)].toHex().data(); +} + +void read_hashes() +{ + QFile hashfile(QString(system_default_directory()).append("/hashes")); + if (hashfile.open(QIODevice::ReadOnly)) { + QDataStream stream(&hashfile); + stream >> localFilenameOf; + hashfile.close(); + } +} + +void write_hashes() +{ + QSaveFile hashfile(QString(system_default_directory()).append("/hashes")); + if (hashfile.open(QIODevice::WriteOnly)) { + QDataStream stream(&hashfile); + stream << localFilenameOf; + hashfile.commit(); + } else { + qDebug() << "cannot open" << hashfile.fileName(); + } +} + +void add_hash(const QString filename, QByteArray hash) +{ + hashOf[filename] = hash; + localFilenameOf[hash] = filename; +} + +QByteArray hashFile(const QString filename) +{ + QCryptographicHash hash(QCryptographicHash::Sha1); + QFile imagefile(filename); + imagefile.open(QIODevice::ReadOnly); + hash.addData(&imagefile); + add_hash(filename, hash.result()); + return hash.result(); +} + +QString localFilePath(const QString originalFilename) +{ + return localFilenameOf[hashOf[originalFilename]]; +} + +QString fileFromHash(char *hash) +{ + return localFilenameOf[QByteArray::fromHex(hash)]; +} + +void updateHash(struct picture *picture) { + QByteArray hash = hashFile(fileFromHash(picture->hash)); + hashOf[QString(picture->filename)] = hash; + char *old = picture->hash; + picture->hash = strdup(hash.toHex()); + free(old); +} -- cgit v1.2.3-70-g09d2