diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-06-21 07:43:35 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-21 12:49:05 -0700 |
commit | 31fbc167850572f034c7fbb8551e10e0cfd7ca29 (patch) | |
tree | a1f2389eb89263a3d731abd3c4ea5cd5d1d8c7da /qthelper.cpp | |
parent | 785f9ba8356203b69672cc76b779b9458ccb9709 (diff) | |
download | subsurface-31fbc167850572f034c7fbb8551e10e0cfd7ca29.tar.gz |
Git storage: implement picture loading from git
The interesting challenge here is what to do with the picture data stored
in the git repository. If the pictures are already in the file system (for
example because Subsurface is runnin on the same machine that this data
file was saved on) it would be silly to extract them again every time the
dive log is opened.
So instead we try to figure out if the pictures can be located and only
create local copies of them if that isn't the case.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qthelper.cpp')
-rw-r--r-- | qthelper.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/qthelper.cpp b/qthelper.cpp index 3f904d6f1..abf8641ae 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -377,7 +377,8 @@ extern "C" void copy_image_and_overwrite(const char *cfileName, const char *cnew QFile file(newName); if (file.exists()) file.remove(); - QFile::copy(fileName, newName); + if (!QFile::copy(fileName, newName)) + qDebug() << "copy of" << fileName << "to" << newName << "failed"; } extern "C" bool string_sequence_contains(const char *string_sequence, const char *text) @@ -887,6 +888,33 @@ extern "C" const char *local_file_path(struct picture *picture) return strdup(qPrintable(localFileName)); } +extern "C" bool picture_exists(struct picture *picture) +{ + QString localFilename = fileFromHash(picture->hash); + QByteArray hash = hashFile(localFilename); + return same_string(hash.toHex().data(), picture->hash); +} + +/* when we get a picture from git storage (local or remote) and can't find the picture + * based on its hash, we create a local copy with the hash as filename and the appropriate + * suffix */ +extern "C" void savePictureLocal(struct picture *picture, const char *data, int len) +{ + QString dirname(system_default_directory()); + dirname += "/picturedata/"; + QDir localPictureDir(dirname); + localPictureDir.mkpath(dirname); + QString suffix(picture->filename); + suffix.replace(QRegularExpression(".*\\."), ""); + QString filename(dirname + picture->hash + "." + suffix); + QSaveFile out(filename); + if (out.open(QIODevice::WriteOnly)) { + out.write(data, len); + out.commit(); + add_hash(filename, QByteArray::fromHex(picture->hash)); + } +} + extern "C" void picture_load_exif_data(struct picture *p) { EXIFInfo exif; |