Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/PreviewPane.qml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ Rectangle {
font.pixelSize: 12
elide: Text.ElideRight
}
// A type nothing installed can draw: which package would.
Text {
readonly property string missing: big.status === Image.Error ? thumbnails.missingPackage(pane.item.mime) : ""
visible: missing.length > 0
Layout.fillWidth: true
text: qsTr("Thumbnails of this type need the %1 package.").arg(missing)
color: theme.secondaryForeground
font.pixelSize: 12
wrapMode: Text.Wrap
}
}

GridLayout {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@
"This computer can't make %1 drives.": "This computer can't make %1 drives.",
"This folder is empty": "This folder is empty",
"This folder no longer exists.": "This folder no longer exists.",
"Thumbnails of this type need the %1 package.": "Thumbnails of this type need the %1 package.",
"Trash": "Trash",
"Try again once the work under way is done.": "Try again once the work under way is done.",
"Type a path, or a server's address like sftp://host/ (Tab completes)": "Type a path, or a server's address like sftp://host/ (Tab completes)",
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ int main(int argc, char *argv[]) {
engine.rootContext()->setContextProperty("theme", &theme);
engine.rootContext()->setContextProperty("places", &places);
engine.rootContext()->setContextProperty("plugins", &plugins);
engine.rootContext()->setContextProperty("thumbnails", &thumbnails);

Windows windows(&engine, &plugins);
if (!windows.open(start, select))
Expand Down
332 changes: 332 additions & 0 deletions src/rawembed.cpp

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/rawembed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <QImage>
#include <QString>

// Camera RAW thumbnails from the embedded JPEG, found in the TIFF directories of CR2 NEF NRW ARW SR2 DNG PEF.
namespace RawEmbed {

bool handles(const QString &mime);

// The largest valid embedded JPEG, upright and at most `pixels` on its longer side; null when the file holds none.
QImage thumbnail(const QString &path, int pixels);

} // namespace RawEmbed
123 changes: 111 additions & 12 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "thumbnails.h"
#include "rawembed.h"

#include <QBuffer>
#include <QCryptographicHash>
Expand Down Expand Up @@ -79,6 +80,71 @@ QStringList expandCommand(const QString &command, const QString &input, const QS
return args;
}

// Each optional package's thumbnailers and the MimeType= lines they ship, as "video/mp4;video/webm;".
struct PackageTypes {
const char *package;
const char *types;
};
const PackageTypes packageTypes[] = {
{"ffmpegthumbnailer",
"video/3gpp;video/3gpp2;video/annodex;video/dv;video/isivideo;video/matroska;video/matroska-3d;"
"video/mj2;video/mp2t;video/mp4;video/mpeg;video/ogg;video/quicktime;video/vnd.avi;"
"video/vnd.mpegurl;video/vnd.radgamettools.bink;video/vnd.radgamettools.smacker;"
"video/vnd.rn-realvideo;video/vnd.vivo;video/vnd.youtube.yt;video/wavelet;video/webm;"
"video/x-anim;video/x-flic;video/x-flv;video/x-javafx;video/x-matroska;video/x-matroska-3d;"
"video/x-mjpeg;video/x-mng;video/x-ms-wmv;video/x-nsv;video/x-ogm+ogg;video/x-sgi-movie;"
"video/x-theora+ogg;application/mxf;application/vnd.ms-asf;application/vnd.rn-realmedia;"
"application/matroska;application/x-matroska;application/ogg;"},
{"glycin",
"image/avif;image/heif;image/apng;image/bmp;image/gif;image/jp2;image/jpeg;image/png;image/qoi;"
"image/tiff;image/vnd.microsoft.icon;image/webp;image/x-dds;image/x-exr;image/x-jp2-codestream;"
"image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;"
"image/x-portable-pixmap;image/x-tga;image/x-win-bitmap;image/x-xbitmap;image/x-xpixmap;"
"image/jxl;image/svg+xml;image/svg+xml-compressed;"},
{"papers",
"application/vnd.comicbook-rar;application/vnd.comicbook+zip;application/x-cb7;application/x-cbr;"
"application/x-cbt;application/x-cbz;application/x-ext-cb7;application/x-ext-cbr;"
"application/x-ext-cbt;application/x-ext-cbz;application/x-ext-djv;application/x-ext-djvu;"
"image/vnd.djvu;image/vnd.djvu+multipage;application/pdf;application/x-bzpdf;"
"application/x-ext-pdf;application/x-gzpdf;application/x-xzpdf;application/illustrator;"
"image/tiff;"},
{"libgsf",
"application/msword;application/vnd.ms-excel;application/vnd.ms-powerpoint;"
"application/vnd.oasis.opendocument.chart;application/vnd.oasis.opendocument.formula;"
"application/vnd.oasis.opendocument.graphics;"
"application/vnd.oasis.opendocument.graphics-template;application/vnd.oasis.opendocument.image;"
"application/vnd.oasis.opendocument.presentation;"
"application/vnd.oasis.opendocument.presentation-template;"
"application/vnd.oasis.opendocument.spreadsheet;"
"application/vnd.oasis.opendocument.spreadsheet-template;application/vnd.oasis.opendocument.text;"
"application/vnd.oasis.opendocument.text-master;application/vnd.oasis.opendocument.text-template;"
"application/vnd.openxmlformats-officedocument.presentationml.presentation;"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document;"
"application/vnd.sun.xml.calc;application/vnd.sun.xml.calc.template;application/vnd.sun.xml.draw;"
"application/vnd.sun.xml.draw.template;application/vnd.sun.xml.impress;"
"application/vnd.sun.xml.impress.template;application/vnd.sun.xml.math;"
"application/vnd.sun.xml.writer;application/vnd.sun.xml.writer.global;"
"application/vnd.sun.xml.writer.template;"},
};

// The package for each type by its own name, read once; the first package listed wins, so glycin draws TIFF before papers.
const QHash<QString, QString> &packageFor() {
static const QHash<QString, QString> table = [] {
QMimeDatabase mimes;
QHash<QString, QString> byType;
for (const PackageTypes &entry : packageTypes) {
for (const QString &declared : QString::fromLatin1(entry.types).split(';', Qt::SkipEmptyParts)) {
const QString type = mimes.mimeTypeForName(declared).name();
if (const QString key = type.isEmpty() ? declared : type; !byType.contains(key))
byType.insert(key, QString::fromLatin1(entry.package));
}
}
return byType;
}();
return table;
}

} // namespace

Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbnailerDirectories)
Expand All @@ -94,7 +160,10 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna
for (const QString &base : data)
directories << base + "/thumbnailers";
}
QMimeDatabase mimes;
for (const QString &directory : directories) {
// A spec may name a type by an alias, as glycin names DDS pictures image/x-dds.
QHash<QString, QString> named, aliased;
QDirIterator it(directory, {"*.thumbnailer"}, QDir::Files);
while (it.hasNext()) {
// A tiny desktop-entry reader: QSettings would treat the
Expand All @@ -115,12 +184,28 @@ Thumbnails::Thumbnails(const QString &cacheDirectory, const QStringList &thumbna
const QString tryExec = fields.value("TryExec");
if (exec.isEmpty() || (!tryExec.isEmpty() && QStandardPaths::findExecutable(tryExec).isEmpty()))
continue;
for (const QString &mime : fields.value("MimeType").split(';', Qt::SkipEmptyParts))
m_thumbnailers.insert(mime.trimmed(), exec);
for (const QString &declared : fields.value("MimeType").split(';', Qt::SkipEmptyParts)) {
const QString mime = declared.trimmed();
named.insert(mime, exec);
if (const QString type = mimes.mimeTypeForName(mime).name(); !type.isEmpty())
aliased.insert(type, exec);
}
}
// Within one directory the spec that names a type itself wins over one that names an alias of it.
m_thumbnailers.insert(aliased);
m_thumbnailers.insert(named);
}
}

QString Thumbnails::missingPackage(const QString &mime) const {
// Qt draws its own pictures in-process, an installed thumbnailer needs nothing more, and pdftoppm draws a PDF without one.
if ((mime.startsWith("image/") && QImageReader::supportedMimeTypes().contains(mime.toLatin1()))
|| m_thumbnailers.contains(mime)
|| (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()))
return {};
return packageFor().value(mime);
}

QString Thumbnails::cacheKey(const QString &path) {
return QCryptographicHash::hash(fileUri(path).toUtf8(), QCryptographicHash::Md5).toHex() + ".png";
}
Expand All @@ -144,9 +229,12 @@ QString Thumbnails::cachePath(const QString &path, int size) const {
}

QString Thumbnails::failPath(const QString &path) const {
return m_cache + "/fail/folio/" + cacheKey(path);
return m_cache + "/" + failures + "/" + cacheKey(path);
}

// The marker's own key for the thumbnailer command the type had when it failed, empty for none.
static const QString kFailedWith = QStringLiteral("X-Folio::Thumbnailer");

QImage Thumbnails::load(const QString &path, const QString &mime, int size) const {
const QFileInfo info(path);
if (!info.isFile())
Expand All @@ -157,24 +245,25 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
return {};

const QDateTime modified = info.lastModified();
if (const QImage image(cachePath(path, size)); madeFor(image, modified))
if (const QImage image(cachePath(path, size)); madeFor(image, path, modified))
return image;
if (failedBefore(path, modified))
return {};

// A folder's peek asks for any picture ("image/*"); named by extension,
// as the grid names it, it goes to the same thumbnailer as its tile.
const QString type = mime.endsWith("/*")
? QMimeDatabase().mimeTypeForFile(path, QMimeDatabase::MatchExtension).name()
: mime;
const QString thumbnailer = m_thumbnailers.value(type);
if (failedBefore(path, modified, thumbnailer))
return {};
const int pixels = bucketPixels(bucketFor(size));
bool attempted = false;
QImage image = generate(path, type, pixels, attempted);
if (image.isNull()) {
// Only a reader or thumbnailer that ran and failed is remembered, so
// one installed later still gets its turn.
if (attempted)
rememberFailure(path, modified);
rememberFailure(path, modified, thumbnailer);
return {};
}
if (image.width() > pixels || image.height() > pixels)
Expand All @@ -184,6 +273,12 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
}

QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const {
// A camera RAW with no installed thumbnailer, the desktop's own answer, shows the JPEG the camera embedded.
if (RawEmbed::handles(mime) && !m_thumbnailers.contains(mime)) {
attempted = true;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if (const QImage image = RawEmbed::thumbnail(path, pixels); !image.isNull())
return image;
}
// Common photo formats decode fastest in-process; anything Qt can't read
// (HEIC, JPEG XL) falls through to the installed thumbnailers.
if (mime.startsWith("image/")) {
Expand Down Expand Up @@ -310,14 +405,16 @@ void Thumbnails::remember(QImage image, const QString &path, int size, const QDa
save(std::move(image), cachePath(path, size), path, modified);
}

void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) const {
void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const {
QImage marker(1, 1, QImage::Format_ARGB32);
marker.fill(Qt::transparent);
marker.setText(kFailedWith, thumbnailer);
save(std::move(marker), failPath(path), path, modified);
}

bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) const {
return madeFor(QImage(failPath(path)), modified);
bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified, const QString &thumbnailer) const {
const QImage marker(failPath(path));
return madeFor(marker, path, modified) && marker.text(kFailedWith) == thumbnailer;
}

// Whether a cached thumbnail (or failure marker) was made from the file as it
Expand All @@ -326,9 +423,11 @@ bool Thumbnails::failedBefore(const QString &path, const QDateTime &modified) co
// the milliseconds. One another app made goes by the seconds alone.
// QImageReader::text() splits keys at ':', so these are only readable from
// the decoded image.
bool Thumbnails::madeFor(const QImage &image, const QDateTime &modified) {
bool Thumbnails::madeFor(const QImage &image, const QString &path, const QDateTime &modified) {
const QString exact = image.text("X-Folio::MTime");
return !image.isNull() && image.text("Thumb::MTime") == QString::number(modified.toSecsSinceEpoch())
// The spec asks a reader to check that Thumb::URI names the file too.
return !image.isNull() && image.text("Thumb::URI") == fileUri(path)
&& image.text("Thumb::MTime") == QString::number(modified.toSecsSinceEpoch())
&& (exact.isEmpty() || exact == QString::number(modified.toMSecsSinceEpoch()));
}

Expand Down
16 changes: 12 additions & 4 deletions src/thumbnails.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QHash>
#include <QDateTime>
#include <QImage>
#include <QObject>
#include <QQuickAsyncImageProvider>
#include <QStringList>
#include <QThreadPool>
Expand All @@ -15,7 +16,9 @@
// /usr/share/thumbnailers (videos via ffmpegthumbnailer, and so on), with
// Poppler's pdftoppm drawing PDFs when no thumbnailer does. Nothing is generated until a
// tile asks for it: there is no background crawl.
class Thumbnails {
class Thumbnails : public QObject {
Q_OBJECT

public:
explicit Thumbnails(const QString &cacheDirectory = {}, const QStringList &thumbnailerDirectories = {});

Expand All @@ -24,11 +27,15 @@ class Thumbnails {
QImage load(const QString &path, const QString &mime, int size) const;

static QString cacheKey(const QString &path);
// The spec's fail/<program>-<version>: the number is raised when Folio learns to draw more, so old failures are tried again.
static inline const QString failures = QStringLiteral("fail/folio-2");
static QString bucketFor(int size);
// A text file's first lines set on a page in the given colors. Made
// fresh each time rather than cached, since the colors follow the theme.
static QImage textPage(const QString &path, int pixels, const QColor &paper, const QColor &ink);
QString thumbnailerFor(const QString &mime) const { return m_thumbnailers.value(mime); }
// The package that would draw `mime` when neither Qt nor an installed thumbnailer does, or nothing.
Q_INVOKABLE QString missingPackage(const QString &mime) const;

private:
QString cachePath(const QString &path, int size) const;
Expand All @@ -38,10 +45,11 @@ class Thumbnails {
QImage runThumbnailer(const QString &command, const QString &path, int pixels) const;
static QImage pdfPage(const QString &path, int pixels);
void remember(QImage image, const QString &path, int size, const QDateTime &modified) const;
void rememberFailure(const QString &path, const QDateTime &modified) const;
bool failedBefore(const QString &path, const QDateTime &modified) const;
// A failure goes with the type's thumbnailer as it was then, none included: one installed since gets its turn.
void rememberFailure(const QString &path, const QDateTime &modified, const QString &thumbnailer) const;
bool failedBefore(const QString &path, const QDateTime &modified, const QString &thumbnailer) const;
void save(QImage image, const QString &target, const QString &path, const QDateTime &modified) const;
static bool madeFor(const QImage &image, const QDateTime &modified);
static bool madeFor(const QImage &image, const QString &path, const QDateTime &modified);

QString m_cache;
QHash<QString, QString> m_thumbnailers;
Expand Down
Loading