File indexing completed on 2025-03-09 03:57:06

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-08-08
0007  * Description : an option to provide camera information to the parser
0008  *
0009  * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "cameranameoption.h"
0016 
0017 // Qt includes
0018 
0019 #include <QScopedPointer>
0020 #include <QRegularExpression>
0021 
0022 // KDE includes
0023 
0024 #include <klocalizedstring.h>
0025 
0026 // Local includes
0027 
0028 #include "parser.h"
0029 #include "dmetadata.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 CameraNameOption::CameraNameOption()
0035     : Option(i18n("Camera"),
0036              i18n("Add the camera name"),
0037              QLatin1String("camera-photo"))
0038 {
0039     QString token(QLatin1String("[cam]"));
0040     addToken(token, i18n("Camera name"));
0041 
0042     QRegularExpression reg(escapeToken(token));
0043     reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
0044     setRegExp(reg);
0045 }
0046 
0047 QString CameraNameOption::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& /*match*/)
0048 {
0049     QString result;
0050 
0051     ItemInfo info = ItemInfo::fromUrl(settings.fileUrl);
0052 
0053     if (!info.isNull())
0054     {
0055         result = info.photoInfoContainer().make + QLatin1Char(' ') + info.photoInfoContainer().model;
0056     }
0057     else
0058     {
0059         // If ItemInfo is not available, read the information from the EXIF data
0060 
0061         QString make;
0062         QString model;
0063 
0064         QScopedPointer<DMetadata> meta(new DMetadata(settings.fileUrl.toLocalFile()));
0065 
0066         if (!meta->isEmpty())
0067         {
0068             MetaEngine::MetaDataMap dataMap;
0069             dataMap = meta->getExifTagsDataList(QStringList(), true);
0070 
0071             Q_FOREACH (const QString& key, dataMap.keys())
0072             {
0073                 if      (key.toLower().contains(QLatin1String("exif.image.model")))
0074                 {
0075                     model = dataMap[key];
0076                 }
0077                 else if (key.toLower().contains(QLatin1String("exif.image.make")))
0078                 {
0079                     make = dataMap[key];
0080                 }
0081             }
0082         }
0083 
0084         result = make + QLatin1Char(' ') + model;
0085     }
0086 
0087     result.replace(QLatin1Char('/'), QLatin1Char('-'));
0088 
0089     return result.simplified();
0090 }
0091 
0092 } // namespace Digikam
0093 
0094 #include "moc_cameranameoption.cpp"