File indexing completed on 2024-05-12 15:54:50

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2015 Vishesh Handa <vhanda@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "imageprocessorrunnable.h"
0008 
0009 #include <QFileInfo>
0010 
0011 #include "exiv2extractor.h"
0012 #include "imagestorage.h"
0013 #include "reversegeocoder.h"
0014 
0015 using namespace Koko;
0016 
0017 ImageProcessorRunnable::ImageProcessorRunnable(const QString &filePath, ReverseGeoCoder *geoCoder)
0018     : QObject()
0019     , m_path(filePath)
0020     , m_geoCoder(geoCoder)
0021 {
0022 }
0023 
0024 void ImageProcessorRunnable::run()
0025 {
0026     ImageInfo ii;
0027     ii.path = m_path;
0028 
0029     Exiv2Extractor extractor;
0030     extractor.extract(m_path);
0031     if (extractor.error()) {
0032         emit finished();
0033         return;
0034     }
0035 
0036     double latitude = extractor.gpsLatitude();
0037     double longitude = extractor.gpsLongitude();
0038 
0039     if (latitude != 0.0 && longitude != 0.0) {
0040         QVariantMap map = m_geoCoder->lookup(latitude, longitude);
0041 
0042         QGeoAddress addr;
0043         addr.setCountry(map.value("country").toString());
0044         addr.setState(map.value("admin1").toString());
0045         addr.setCity(map.value("admin2").toString());
0046         ii.location.setAddress(addr);
0047     }
0048 
0049     if (extractor.favorite()) {
0050         ii.favorite = true;
0051     }
0052 
0053     ii.tags = extractor.tags();
0054 
0055     ii.dateTime = extractor.dateTime();
0056     if (ii.dateTime.isNull()) {
0057         ii.dateTime = QFileInfo(m_path).birthTime();
0058     }
0059 
0060     QMetaObject::invokeMethod(ImageStorage::instance(), "addImage", Qt::AutoConnection, Q_ARG(const ImageInfo &, ii));
0061 
0062     emit finished();
0063 }