File indexing completed on 2025-01-19 03:56:00

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-09-15
0007  * Description : Exiv2 library interface.
0008  *               Comments manipulation methods.
0009  *
0010  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 // Local includes
0018 
0019 #include "metaengine_p.h"
0020 #include "digikam_debug.h"
0021 #include "digikam_config.h"
0022 
0023 #if defined(Q_CC_CLANG)
0024 #   pragma clang diagnostic push
0025 #   pragma clang diagnostic ignored "-Wdeprecated-declarations"
0026 #endif
0027 
0028 namespace Digikam
0029 {
0030 
0031 bool MetaEngine::canWriteComment(const QString& filePath)
0032 {
0033     QMutexLocker lock(&s_metaEngineMutex);
0034 
0035     try
0036     {
0037 
0038 #if defined Q_OS_WIN && defined EXV_UNICODE_PATH
0039 
0040         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const wchar_t*)filePath.utf16());
0041 
0042 #elif defined Q_OS_WIN
0043 
0044         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(QFile::encodeName(filePath).constData());
0045 
0046 #else
0047 
0048         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filePath.toUtf8().constData());
0049 
0050 #endif
0051 
0052         Exiv2::AccessMode mode      = image->checkMode(Exiv2::mdComment);
0053 
0054         return ((mode == Exiv2::amWrite) || (mode == Exiv2::amReadWrite));
0055     }
0056     catch (Exiv2::AnyError& e)
0057     {
0058         qCCritical(DIGIKAM_METAENGINE_LOG) << "Cannot check Comment access mode with Exiv2 (Error #"
0059                                            << (int)e.code() << ": " << QString::fromStdString(e.what()) << ")";
0060     }
0061     catch (...)
0062     {
0063         qCCritical(DIGIKAM_METAENGINE_LOG) << "Default exception from Exiv2";
0064     }
0065 
0066     return false;
0067 }
0068 
0069 bool MetaEngine::hasComments() const
0070 {
0071     return !d->itemComments().empty();
0072 }
0073 
0074 bool MetaEngine::clearComments() const
0075 {
0076     return setComments(QByteArray());
0077 }
0078 
0079 QByteArray MetaEngine::getComments() const
0080 {
0081     return QByteArray(d->itemComments().data(), (int)d->itemComments().size());
0082 }
0083 
0084 QString MetaEngine::getCommentsDecoded() const
0085 {
0086     return d->detectEncodingAndDecode(d->itemComments());
0087 }
0088 
0089 bool MetaEngine::setComments(const QByteArray& data) const
0090 {
0091     d->itemComments() = std::string(data.data(), data.size());
0092 
0093     return true;
0094 }
0095 
0096 QString MetaEngine::detectLanguageAlt(const QString& value, QString& lang)
0097 {
0098     // Ex. from an Xmp tag Xmp.tiff.copyright: "lang="x-default"
0099 
0100     if (value.size() > 6 && value.startsWith(QLatin1String("lang=\"")))
0101     {
0102         int pos = value.indexOf(QLatin1String("\""), 6);
0103 
0104         if (pos != -1)
0105         {
0106             lang = value.mid(6, pos-6);
0107             return (value.mid(pos+2));
0108         }
0109     }
0110 
0111     lang.clear();
0112 
0113     return value;
0114 }
0115 
0116 } // namespace Digikam
0117 
0118 #if defined(Q_CC_CLANG)
0119 #   pragma clang diagnostic pop
0120 #endif