File indexing completed on 2025-01-19 03:53:47

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-03-21
0007  * Description : a class to hold GPS information about an item.
0008  *
0009  * SPDX-FileCopyrightText: 2010-2014 by Michael G. Hansen <mike at mghansen dot de>
0010  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "itemgps.h"
0017 
0018 // Local includes
0019 
0020 #include "coredb.h"
0021 #include "tagscache.h"
0022 #include "metadatahub.h"
0023 #include "itemposition.h"
0024 #include "scancontroller.h"
0025 #include "metaenginesettings.h"
0026 #include "itemextendedproperties.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 ItemGPS::ItemGPS(const ItemInfo& info)
0032     : GPSItemContainer(info.fileUrl()),
0033       m_info          (info)
0034 {
0035 }
0036 
0037 ItemGPS::~ItemGPS()
0038 {
0039 }
0040 
0041 bool ItemGPS::loadImageData()
0042 {
0043     // In first, we try to get GPS info from database.
0044 
0045     ItemPosition pos = m_info.imagePosition();
0046     m_dateTime       = m_info.dateTime();
0047 
0048     if (!pos.isEmpty() && pos.hasCoordinates())
0049     {
0050         m_gpsData.setLatLon(pos.latitudeNumber(), pos.longitudeNumber());
0051 
0052         if (pos.hasAltitude())
0053         {
0054             m_gpsData.setAltitude(pos.altitude());
0055         }
0056     }
0057 
0058     // mark us as not-dirty, because the data was just loaded:
0059 
0060     m_dirty      = false;
0061     m_savedState = m_gpsData;
0062 
0063     emitDataChanged();
0064 
0065     return true;
0066 }
0067 
0068 QString ItemGPS::saveChanges()
0069 {
0070     SaveProperties p = saveProperties();
0071 
0072     // Save info to database.
0073 
0074     ItemPosition pos = m_info.imagePosition();
0075 
0076     if (p.shouldWriteCoordinates)
0077     {
0078         pos.setLatitude(p.latitude);
0079         pos.setLongitude(p.longitude);
0080 
0081         if (p.shouldWriteAltitude)
0082         {
0083             pos.setAltitude(p.altitude);
0084         }
0085     }
0086 
0087     if (p.shouldRemoveCoordinates)
0088     {
0089         pos.remove();
0090     }
0091     else if (p.shouldRemoveAltitude)
0092     {
0093         pos.removeAltitude();
0094     }
0095 
0096     pos.apply();
0097 
0098     if (!m_tagList.isEmpty() && (m_writeXmpTags || m_writeMetaLoc))
0099     {
0100         QMap<QString, QVariant> attributes;
0101         IptcCoreLocationInfo locationInfo;
0102         QStringList tagsPath;
0103 
0104         for (int i = 0 ; i < m_tagList.count() ; ++i)
0105         {
0106             QString singleTagPath;
0107             QList<TagData> currentTagPath = m_tagList[i];
0108 
0109             for (int j = 0 ; j < currentTagPath.count() ; ++j)
0110             {
0111                 if (currentTagPath[j].tipName != QLatin1String("{Country code}"))
0112                 {
0113                     singleTagPath.append(QLatin1Char('/') + currentTagPath[j].tagName);
0114 
0115                     if (j == 0)
0116                     {
0117                         singleTagPath.remove(0, 1);
0118                     }
0119                 }
0120 
0121                 setLocationInfo(currentTagPath[j], locationInfo);
0122             }
0123 
0124             if (!singleTagPath.isEmpty())
0125             {
0126                 tagsPath << singleTagPath;
0127             }
0128         }
0129 
0130         if (m_writeXmpTags)
0131         {
0132             QList<int> tagIds = TagsCache::instance()->getOrCreateTags(tagsPath);
0133             CoreDbAccess().db()->addTagsToItems(QList<qlonglong>() << m_info.id(), tagIds);
0134         }
0135 
0136         if (m_writeMetaLoc)
0137         {
0138             ItemExtendedProperties ep(m_info.id());
0139             ep.setLocation(locationInfo);
0140         }
0141     }
0142 
0143     MetadataHub hub;
0144     hub.load(m_info);
0145     QString filePath = m_info.filePath();
0146 
0147     if (MetaEngineSettings::instance()->settings().useLazySync)
0148     {
0149         hub.write(filePath, MetadataHub::WRITE_TAGS     |
0150                             MetadataHub::WRITE_TEMPLATE |
0151                             MetadataHub::WRITE_POSITION);
0152     }
0153     else
0154     {
0155         ScanController::FileMetadataWrite writeScope(m_info);
0156         writeScope.changed(hub.write(filePath, MetadataHub::WRITE_TAGS     |
0157                                                MetadataHub::WRITE_TEMPLATE |
0158                                                MetadataHub::WRITE_POSITION));
0159     }
0160 
0161     m_dirty        = false;
0162     m_savedState   = m_gpsData;
0163     m_tagListDirty = false;
0164     m_savedTagList = m_tagList;
0165 
0166     emitDataChanged();
0167 
0168     return QString();
0169 }
0170 
0171 } // namespace Digikam