File indexing completed on 2024-04-21 15:22:26

0001 /*
0002     SPDX-FileCopyrightText: 2009-2015 Gilles Caulier <caulier dot gilles at gmail dot com>
0003     SPDX-FileCopyrightText: 2009-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 "kexiv2previews.h"
0011 #include "kexiv2_p.h"
0012 #include "kexiv2.h"
0013 #include "libkexiv2_debug.h"
0014 
0015 namespace KExiv2Iface
0016 {
0017 
0018 class KExiv2PreviewsPrivate
0019 {
0020 public:
0021 
0022     KExiv2PreviewsPrivate()
0023     {
0024         manager = nullptr;
0025     }
0026 
0027     ~KExiv2PreviewsPrivate()
0028     {
0029         delete manager;
0030     }
0031 
0032 #if EXIV2_TEST_VERSION(0,28,0)
0033     void load(Exiv2::Image::UniquePtr image_)
0034 #else
0035     void load(Exiv2::Image::AutoPtr image_)
0036 #endif
0037     {
0038 #if EXIV2_TEST_VERSION(0,28,0)
0039         image                              = std::move(image_);
0040 #else
0041         image                              = image_;
0042 #endif
0043 
0044         image->readMetadata();
0045 
0046         manager                            = new Exiv2::PreviewManager(*image);
0047         Exiv2::PreviewPropertiesList props = manager->getPreviewProperties();
0048 
0049         // reverse order of list, which is smallest-first
0050         Exiv2::PreviewPropertiesList::reverse_iterator it;
0051 
0052         for (it = props.rbegin() ; it != props.rend() ; ++it)
0053         {
0054             properties << *it;
0055         }
0056     }
0057 
0058 public:
0059 
0060 #if EXIV2_TEST_VERSION(0,28,0)
0061     Exiv2::Image::UniquePtr         image;
0062 #else
0063     Exiv2::Image::AutoPtr           image;
0064 #endif
0065     Exiv2::PreviewManager*          manager;
0066     QList<Exiv2::PreviewProperties> properties;
0067 };
0068 
0069 KExiv2Previews::KExiv2Previews(const QString& filePath)
0070     : d(new KExiv2PreviewsPrivate)
0071 {
0072     try
0073     {
0074 #if EXIV2_TEST_VERSION(0,28,0)
0075         Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
0076         d->load(std::move(image));
0077 #else
0078         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
0079         d->load(image);
0080 #endif
0081     }
0082     catch( Exiv2::Error& e )
0083     {
0084         KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
0085     }
0086     catch(...)
0087     {
0088         qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
0089     }
0090 }
0091 
0092 KExiv2Previews::KExiv2Previews(const QByteArray& imgData)
0093     : d(new KExiv2PreviewsPrivate)
0094 {
0095     try
0096     {
0097 #if EXIV2_TEST_VERSION(0,28,0)
0098         Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
0099         d->load(std::move(image));
0100 #else
0101         Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
0102         d->load(image);
0103 #endif
0104     }
0105     catch( Exiv2::Error& e )
0106     {
0107         KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
0108     }
0109     catch(...)
0110     {
0111         qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
0112     }
0113 }
0114 
0115 KExiv2Previews::~KExiv2Previews() = default;
0116 
0117 bool KExiv2Previews::isEmpty()
0118 {
0119     return d->properties.isEmpty();
0120 }
0121 
0122 QSize KExiv2Previews::originalSize() const
0123 {
0124     if (d->image.get())
0125         return QSize(d->image->pixelWidth(), d->image->pixelHeight());
0126 
0127     return QSize();
0128 }
0129 
0130 QString KExiv2Previews::originalMimeType() const
0131 {
0132     if (d->image.get())
0133         return QString::fromLatin1(d->image->mimeType().c_str());
0134 
0135     return QString();
0136 }
0137 
0138 int KExiv2Previews::count()
0139 {
0140     return d->properties.size();
0141 }
0142 
0143 int KExiv2Previews::dataSize(int index)
0144 {
0145     if (index < 0 || index >= size()) return 0;
0146 
0147     return d->properties[index].size_;
0148 }
0149 
0150 int KExiv2Previews::width(int index)
0151 {
0152     if (index < 0 || index >= size()) return 0;
0153 
0154     return d->properties[index].width_;
0155 }
0156 
0157 int KExiv2Previews::height(int index)
0158 {
0159     if (index < 0 || index >= size()) return 0;
0160 
0161     return d->properties[index].height_;
0162 }
0163 
0164 QString KExiv2Previews::mimeType(int index)
0165 {
0166     if (index < 0 || index >= size()) return QString();
0167 
0168     return QString::fromLatin1(d->properties[index].mimeType_.c_str());
0169 }
0170 
0171 QString KExiv2Previews::fileExtension(int index)
0172 {
0173     if (index < 0 || index >= size()) return QString();
0174 
0175     return QString::fromLatin1(d->properties[index].extension_.c_str());
0176 }
0177 
0178 QByteArray KExiv2Previews::data(int index)
0179 {
0180     if (index < 0 || index >= size()) return QByteArray();
0181 
0182     qCDebug(LIBKEXIV2_LOG) << "index: "         << index;
0183     qCDebug(LIBKEXIV2_LOG) << "d->properties: " << count();
0184 
0185     try
0186     {
0187         Exiv2::PreviewImage image = d->manager->getPreviewImage(d->properties[index]);
0188         return QByteArray((const char*)image.pData(), image.size());
0189     }
0190     catch( Exiv2::Error& e )
0191     {
0192         KExiv2Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
0193         return QByteArray();
0194     }
0195     catch(...)
0196     {
0197         qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
0198         return QByteArray();
0199     }
0200 }
0201 
0202 QImage KExiv2Previews::image(int index)
0203 {
0204     QByteArray previewData = data(index);
0205     QImage     image;
0206 
0207     if (!image.loadFromData(previewData))
0208         return QImage();
0209 
0210     return image;
0211 }
0212 
0213 } // namespace KExiv2Iface