2014-05-30 14:38:27 -03:00
|
|
|
#include "divepicturewidget.h"
|
2015-05-29 14:42:57 -03:00
|
|
|
#include "divepicturemodel.h"
|
2014-10-19 16:15:19 +02:00
|
|
|
#include "metrics.h"
|
2015-01-17 22:43:52 +13:00
|
|
|
#include "dive.h"
|
|
|
|
#include "divelist.h"
|
2015-03-02 16:18:16 +01:00
|
|
|
#include <unistd.h>
|
2014-06-26 14:01:31 -03:00
|
|
|
#include <QtConcurrentMap>
|
2015-02-26 14:39:42 +01:00
|
|
|
#include <QtConcurrentRun>
|
2015-03-10 10:22:34 -07:00
|
|
|
#include <QFuture>
|
2014-06-26 14:01:31 -03:00
|
|
|
#include <QDir>
|
2015-02-26 14:39:42 +01:00
|
|
|
#include <QCryptographicHash>
|
2015-03-02 16:18:16 +01:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
2015-02-26 14:39:42 +01:00
|
|
|
#include <mainwindow.h>
|
|
|
|
#include <qthelper.h>
|
2015-03-02 16:18:16 +01:00
|
|
|
#include <QStandardPaths>
|
|
|
|
|
2015-05-29 14:42:57 -03:00
|
|
|
void loadPicture(struct picture *picture)
|
2015-03-02 16:18:16 +01:00
|
|
|
{
|
|
|
|
ImageDownloader download(picture);
|
|
|
|
download.load();
|
|
|
|
}
|
2015-02-26 14:39:42 +01:00
|
|
|
|
2015-03-03 11:12:07 +01:00
|
|
|
SHashedImage::SHashedImage(struct picture *picture) : QImage()
|
2015-02-26 14:39:42 +01:00
|
|
|
{
|
2015-03-03 11:12:07 +01:00
|
|
|
QUrl url = QUrl::fromUserInput(QString(picture->filename));
|
|
|
|
if(url.isLocalFile())
|
|
|
|
load(url.toLocalFile());
|
2015-02-26 14:39:42 +01:00
|
|
|
if (isNull()) {
|
|
|
|
// Hash lookup.
|
|
|
|
load(fileFromHash(picture->hash));
|
2015-03-02 16:18:16 +01:00
|
|
|
if (!isNull()) {
|
2015-02-26 14:39:42 +01:00
|
|
|
QtConcurrent::run(updateHash, picture);
|
2015-03-02 16:18:16 +01:00
|
|
|
} else {
|
2015-05-29 14:42:57 -03:00
|
|
|
QtConcurrent::run(loadPicture, picture);
|
2015-03-02 16:18:16 +01:00
|
|
|
}
|
2015-02-26 14:39:42 +01:00
|
|
|
} else {
|
2015-03-03 11:12:07 +01:00
|
|
|
QByteArray hash = hashFile(url.toLocalFile());
|
2015-02-26 14:39:42 +01:00
|
|
|
free(picture->hash);
|
|
|
|
picture->hash = strdup(hash.toHex().data());
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 14:38:27 -03:00
|
|
|
|
2015-03-02 16:18:16 +01:00
|
|
|
ImageDownloader::ImageDownloader(struct picture *pic)
|
|
|
|
{
|
|
|
|
picture = pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageDownloader::load(){
|
2015-03-03 11:12:07 +01:00
|
|
|
QUrl url = QUrl::fromUserInput(QString(picture->filename));
|
2015-03-02 16:18:16 +01:00
|
|
|
if (url.isValid()) {
|
|
|
|
QEventLoop loop;
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
connect(&manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(saveImage(QNetworkReply *)));
|
|
|
|
QNetworkReply *reply = manager.get(request);
|
|
|
|
while (reply->isRunning()) {
|
|
|
|
loop.processEvents();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageDownloader::saveImage(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
QByteArray imageData = reply->readAll();
|
2015-04-24 17:10:55 +02:00
|
|
|
QImage image = QImage();
|
|
|
|
image.loadFromData(imageData);
|
|
|
|
if (image.isNull())
|
|
|
|
return;
|
2015-03-02 16:18:16 +01:00
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(imageData);
|
2015-03-17 12:09:03 +01:00
|
|
|
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(path);
|
|
|
|
QFile imageFile(path.append("/").append(hash.result().toHex()));
|
2015-03-02 16:18:16 +01:00
|
|
|
if (imageFile.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream stream(&imageFile);
|
|
|
|
stream.writeRawData(imageData.data(), imageData.length());
|
|
|
|
imageFile.waitForBytesWritten(-1);
|
|
|
|
imageFile.close();
|
|
|
|
add_hash(imageFile.fileName(), hash.result());
|
|
|
|
learnHash(picture, hash.result());
|
|
|
|
DivePictureModel::instance()->updateDivePictures();
|
|
|
|
}
|
|
|
|
reply->manager()->deleteLater();
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2014-06-03 16:48:59 -07:00
|
|
|
DivePictureWidget::DivePictureWidget(QWidget *parent) : QListView(parent)
|
2014-05-30 14:38:27 -03:00
|
|
|
{
|
2014-06-27 16:16:55 +04:00
|
|
|
connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(doubleClicked(const QModelIndex &)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivePictureWidget::doubleClicked(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
QString filePath = model()->data(index, Qt::DisplayPropertyRole).toString();
|
2015-02-26 14:39:42 +01:00
|
|
|
emit photoDoubleClicked(localFilePath(filePath));
|
2014-05-30 14:38:27 -03:00
|
|
|
}
|