File indexing completed on 2024-03-24 16:02:07

0001 /*
0002     SPDX-FileCopyrightText: 2006-2015 Gilles Caulier <caulier dot gilles at gmail dot com>
0003     SPDX-FileCopyrightText: 2006-2012 Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // Local includes
0009 
0010 #include "kexiv2_p.h"
0011 #include "kexiv2.h"
0012 #include "libkexiv2_debug.h"
0013 
0014 namespace KExiv2Iface
0015 {
0016 
0017 bool KExiv2::canWriteComment(const QString& filePath)
0018 {
0019     try
0020     {
0021 #if EXIV2_TEST_VERSION(0,28,0)
0022         Exiv2::Image::UniquePtr image
0023 #else
0024         Exiv2::Image::AutoPtr image
0025 #endif
0026                                     = Exiv2::ImageFactory::open((const char*)
0027                                       (QFile::encodeName(filePath).constData()));
0028 
0029         Exiv2::AccessMode mode = image->checkMode(Exiv2::mdComment);
0030         return (mode == Exiv2::amWrite || mode == Exiv2::amReadWrite);
0031     }
0032     catch( Exiv2::Error& e )
0033     {
0034         std::string s(e.what());
0035         qCCritical(LIBKEXIV2_LOG) << "Cannot check Comment access mode using Exiv2 (Error #"
0036 #if EXIV2_TEST_VERSION(0,28,0)
0037                                   << Exiv2::Error(e.code()).what()
0038 #else
0039                                   << e.code() << ": " << s.c_str()
0040 #endif
0041                                   << ")";
0042     }
0043     catch(...)
0044     {
0045         qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
0046     }
0047 
0048     return false;
0049 }
0050 
0051 bool KExiv2::hasComments() const
0052 {
0053     return !d->imageComments().empty();
0054 }
0055 
0056 bool KExiv2::clearComments() const
0057 {
0058     return setComments(QByteArray());
0059 }
0060 
0061 QByteArray KExiv2::getComments() const
0062 {
0063     return QByteArray(d->imageComments().data(), d->imageComments().size());
0064 }
0065 
0066 QString KExiv2::getCommentsDecoded() const
0067 {
0068     return d->detectEncodingAndDecode(d->imageComments());
0069 }
0070 
0071 bool KExiv2::setComments(const QByteArray& data) const
0072 {
0073     d->imageComments() = std::string(data.data(), data.size());
0074     return true;
0075 }
0076 
0077 QString KExiv2::detectLanguageAlt(const QString& value, QString& lang)
0078 {
0079     // Ex. from an Xmp tag Xmp.tiff.copyright: "lang="x-default" (c) Gilles Caulier 2007"
0080 
0081     if (value.size() > 6 && value.startsWith(QString::fromLatin1("lang=\"")))
0082     {
0083         int pos = value.indexOf(QString::fromLatin1("\""), 6);
0084 
0085         if (pos != -1)
0086         {
0087             lang = value.mid(6, pos-6);
0088             return (value.mid(pos+2));
0089         }
0090     }
0091 
0092     lang.clear();
0093     return value;
0094 }
0095 
0096 }  // NameSpace KExiv2Iface