kexiv2previews.cpp 5.11 KB
Newer Older
Gilles Caulier's avatar
Gilles Caulier committed
1
/** ===========================================================
2
 * @file
3
 *
Gilles Caulier's avatar
cleanup    
Gilles Caulier committed
4
5
 * This file is a part of KDE project
 *
6
 *
Gilles Caulier's avatar
Gilles Caulier committed
7
8
 * @date   2009-11-14
 * @brief  Embedded preview loading
9
 *
Gilles Caulier's avatar
Gilles Caulier committed
10
 * @author Copyright (C) 2009-2015 by Gilles Caulier
Gilles Caulier's avatar
Gilles Caulier committed
11
 *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
12
 * @author Copyright (C) 2009-2012 by Marcel Wiesweg
Gilles Caulier's avatar
Gilles Caulier committed
13
 *         <a href="mailto:marcel dot wiesweg at gmx dot de">marcel dot wiesweg at gmx dot de</a>
14
15
16
17
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
Gilles Caulier's avatar
Gilles Caulier committed
18
19
 * either version 2, or (at your option)
 * any later version.
20
21
22
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Gilles Caulier's avatar
Gilles Caulier committed
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
25
26
27
 * GNU General Public License for more details.
 *
 * ============================================================ */

Andi Clemens's avatar
Andi Clemens committed
28
// Local includes
29

Gilles Caulier's avatar
Gilles Caulier committed
30
#include "kexiv2previews.h"
31
32
#include "kexiv2_p.h"
#include "kexiv2.h"
Gilles Caulier's avatar
Gilles Caulier committed
33
#include "libkexiv2_debug.h"
34
35
36
37

namespace KExiv2Iface
{

38
class Q_DECL_HIDDEN KExiv2Previews::Private
39
40
41
{
public:

Gilles Caulier's avatar
Gilles Caulier committed
42
    Private()
43
    {
Albert Astals Cid's avatar
Albert Astals Cid committed
44
        manager = nullptr;
45
46
    }

Gilles Caulier's avatar
Gilles Caulier committed
47
    ~Private()
48
49
50
51
52
53
    {
        delete manager;
    }

    void load(Exiv2::Image::AutoPtr image_)
    {
54
        image                              = image_;
55
56
57

        image->readMetadata();

58
        manager                            = new Exiv2::PreviewManager(*image);
59
60
61
62
        Exiv2::PreviewPropertiesList props = manager->getPreviewProperties();

        // reverse order of list, which is smallest-first
        Exiv2::PreviewPropertiesList::reverse_iterator it;
63
64

        for (it = props.rbegin() ; it != props.rend() ; ++it)
65
66
67
68
69
        {
            properties << *it;
        }
    }

70
71
public:

72
73
74
    Exiv2::Image::AutoPtr           image;
    Exiv2::PreviewManager*          manager;
    QList<Exiv2::PreviewProperties> properties;
75
76
77
};

KExiv2Previews::KExiv2Previews(const QString& filePath)
Gilles Caulier's avatar
Gilles Caulier committed
78
    : d(new Private)
79
80
81
{
    try
    {
82
        Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((const char*)(QFile::encodeName(filePath).constData()));
83
84
        d->load(image);
    }
85
    catch( Exiv2::Error& e )
86
    {
Gilles Caulier's avatar
port++    
Gilles Caulier committed
87
        KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
88
    }
89
90
    catch(...)
    {
Gilles Caulier's avatar
Gilles Caulier committed
91
        qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
92
    }
93
94
95
}

KExiv2Previews::KExiv2Previews(const QByteArray& imgData)
Gilles Caulier's avatar
Gilles Caulier committed
96
    : d(new Private)
97
98
99
100
101
102
{
    try
    {
        Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open((Exiv2::byte*)imgData.data(), imgData.size());
        d->load(image);
    }
103
    catch( Exiv2::Error& e )
104
    {
Gilles Caulier's avatar
port++    
Gilles Caulier committed
105
        KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
106
    }
107
108
    catch(...)
    {
Gilles Caulier's avatar
Gilles Caulier committed
109
        qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
110
    }
111
112
113
114
115
116
117
118
119
120
121
122
}

KExiv2Previews::~KExiv2Previews()
{
    delete d;
}

bool KExiv2Previews::isEmpty()
{
    return d->properties.isEmpty();
}

123
124
125
126
QSize KExiv2Previews::originalSize() const
{
    if (d->image.get())
        return QSize(d->image->pixelWidth(), d->image->pixelHeight());
127

128
129
130
131
132
133
    return QSize();
}

QString KExiv2Previews::originalMimeType() const
{
    if (d->image.get())
Gilles Caulier's avatar
port++    
Gilles Caulier committed
134
        return QString::fromLatin1(d->image->mimeType().c_str());
135

136
137
138
    return QString();
}

139
140
141
142
143
144
145
int KExiv2Previews::count()
{
    return d->properties.size();
}

int KExiv2Previews::dataSize(int index)
{
146
147
    if (index < 0 || index >= size()) return 0;

148
149
150
151
152
    return d->properties[index].size_;
}

int KExiv2Previews::width(int index)
{
153
154
    if (index < 0 || index >= size()) return 0;

155
156
157
158
159
    return d->properties[index].width_;
}

int KExiv2Previews::height(int index)
{
160
161
    if (index < 0 || index >= size()) return 0;

162
163
164
165
166
    return d->properties[index].height_;
}

QString KExiv2Previews::mimeType(int index)
{
Gilles Caulier's avatar
port++    
Gilles Caulier committed
167
    if (index < 0 || index >= size()) return QString();
168

169
170
171
172
173
    return QString::fromLatin1(d->properties[index].mimeType_.c_str());
}

QString KExiv2Previews::fileExtension(int index)
{
Gilles Caulier's avatar
port++    
Gilles Caulier committed
174
    if (index < 0 || index >= size()) return QString();
175

176
177
178
179
180
    return QString::fromLatin1(d->properties[index].extension_.c_str());
}

QByteArray KExiv2Previews::data(int index)
{
181
182
    if (index < 0 || index >= size()) return QByteArray();

Gilles Caulier's avatar
Gilles Caulier committed
183
184
    qCDebug(LIBKEXIV2_LOG) << "index: "         << index;
    qCDebug(LIBKEXIV2_LOG) << "d->properties: " << count();
185

186
187
188
189
190
    try
    {
        Exiv2::PreviewImage image = d->manager->getPreviewImage(d->properties[index]);
        return QByteArray((const char*)image.pData(), image.size());
    }
191
    catch( Exiv2::Error& e )
192
    {
Gilles Caulier's avatar
port++    
Gilles Caulier committed
193
        KExiv2::Private::printExiv2ExceptionError(QString::fromLatin1("Cannot load metadata using Exiv2 "), e);
194
195
        return QByteArray();
    }
196
197
    catch(...)
    {
Gilles Caulier's avatar
Gilles Caulier committed
198
        qCCritical(LIBKEXIV2_LOG) << "Default exception from Exiv2";
199
200
        return QByteArray();
    }
201
202
203
204
205
}

QImage KExiv2Previews::image(int index)
{
    QByteArray previewData = data(index);
206
207
    QImage     image;

208
209
210
    if (!image.loadFromData(previewData))
        return QImage();

211
    return image;
212
213
}

214
} // namespace KExiv2Iface