Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes: N#121 - Thumbnails should be cached on disk
This implementation has a number of issues,  but the base functionality now
works. New bugs will be raised.
  • Loading branch information
rburchell committed Feb 18, 2012
1 parent 29525ef commit c4523d7
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dirmodel.cpp
Expand Up @@ -159,7 +159,7 @@ QVariant DirModel::data(const QModelIndex &index, int role) const

if (fileName.endsWith(".jpg") ||
fileName.endsWith(".png")) {
return QUrl::fromLocalFile(fi.filePath());
return "image://nemoThumbnail/" + fi.filePath();
}

if (fi.isDir())
Expand Down
114 changes: 114 additions & 0 deletions filethumbnailprovider.cpp
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2012 Robin Burchell <robin+nemo@viroteck.net>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#include <QFile>
#include <QCryptographicHash>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QImageReader>

#include "filethumbnailprovider.h"

QImage FileThumbnailImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QSize actualSize;

if (requestedSize.isValid())
actualSize = requestedSize;
else
actualSize = QSize(64, 64);

if (size)
*size = actualSize;

qDebug() << Q_FUNC_INFO << "Requested image: " << id;
QByteArray baId = id.toLatin1(); // is there a more efficient way than a copy?

// check if we have it in cache
QCryptographicHash hash(QCryptographicHash::Sha1);

hash.addData(baId.constData(), baId.length());
QByteArray hashData = hash.result().toHex() + "nemo";

QString cachePath = QDesktopServices::storageLocation(QDesktopServices::CacheLocation) + ".nemothumbs";

// the syscalls make baby jesus cry; but this protects us against sins like users
QDir d(cachePath);
if (!d.exists())
d.mkpath(cachePath);
if (!d.exists("raw"))
d.mkdir("raw");

// in the future, we might store a nicer UI version which can blend in with our UI better, but
// we'll always want the raw version.

QFile fi(cachePath + QDir::separator() + "raw" + QDir::separator() + hashData);
if (fi.exists() && fi.open(QIODevice::ReadOnly)) {
// cached file exists! hooray.
qDebug() << Q_FUNC_INFO << "Read " << id << " from cache";
QImage img;
img.load(&fi, "JPG");
qDebug() << img.size();
return img;
} else {
// slow path: read image in, scale it, write to cache, return it
QImageReader ir(id);
QImage img = ir.read();
if (img.size() != actualSize) {
// TODO: we should probably handle cropping here too to get better results
qDebug() << Q_FUNC_INFO << "Wrote " << id << " to cache";
img = img.scaled(actualSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

if (fi.open(QIODevice::WriteOnly)) {
img.save(&fi, "JPG");
fi.flush();
fi.close();
} else {
qWarning() << "Couldn't cache " << id << " to " << fi.fileName();
}

return img;
} else {
return img;
}
}

// should be unreachable
Q_ASSERT(false);
return QImage();

// QImage pixmap(requestedSize.width() > 0 ? requestedSize.width() : actualSize.width(),
// requestedSize.height() > 0 ? requestedSize.height() : actualSize.height());
// pixmap.fill(QColor(id).rgba());

// return pixmap;
}
48 changes: 48 additions & 0 deletions filethumbnailprovider.h
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2012 Robin Burchell <robin+nemo@viroteck.net>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#ifndef FILETHUMBNAILPROVIDER_H
#define FILETHUMBNAILPROVIDER_H

#include <QDeclarativeImageProvider>

class FileThumbnailImageProvider : public QDeclarativeImageProvider
{
public:
FileThumbnailImageProvider()
: QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
{
}

QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
};

#endif // FILETHUMBNAILPROVIDER_H
4 changes: 4 additions & 0 deletions main.cpp
Expand Up @@ -42,6 +42,7 @@

#include "dirmodel.h"
#include "utils.h"
#include "filethumbnailprovider.h"

Q_DECLARE_METATYPE(QVector<QFileInfo>)

Expand All @@ -53,6 +54,9 @@ int main(int argc, char **argv)

QDeclarativeView v;

QDeclarativeEngine *e = v.engine();
e->addImageProvider(QLatin1String("nemoThumbnail"), new FileThumbnailImageProvider);

QDeclarativeContext *c = v.rootContext();
c->setContextProperty("fileBrowserUtils", new Utils);

Expand Down
6 changes: 4 additions & 2 deletions qmlfilemuncher.pro
Expand Up @@ -26,7 +26,8 @@ SOURCES += main.cpp \
iorequestworker.cpp \
iorequest.cpp \
dirmodel.cpp \
utils.cpp
utils.cpp \
filethumbnailprovider.cpp

RESOURCES += \
data.qrc
Expand All @@ -36,5 +37,6 @@ HEADERS += \
iorequestworker.h \
iorequest.h \
dirmodel.h \
utils.h
utils.h \
filethumbnailprovider.h

0 comments on commit c4523d7

Please sign in to comment.