How to implement a thumbnail creator plugin for KDE5?

128 Views Asked by At

I'm trying to make Dolphin to show thumbnails for STL files, so far unsuccessfully.

First I tried to use an existing plugin stl-thumb-kde. This didn't do anything: STL files still showed as grey boxes

STL thumbnail in Dolphin

Then I tried to implement my own plugin. Seeing that ThumbCreator is deprecated, I decided to extend a ThumbnailCreator instead. Here's my code:

StlThumbnailCreator.cpp

#include <fstream>
#include <iostream>

#include <QImage>
#include <QLoggingCategory>
#include <QSize>
#include <KIO/ThumbnailCreator>
#include <KPluginFactory>

#include <libstl_thumb.h>

Q_LOGGING_CATEGORY(LOG_STL, "com.stlthumbnail")

void clean(void* cleanupinfo)
{
    free(cleanupinfo);
}

class StlThumbnailCreator : public KIO::ThumbnailCreator
{
public:
    StlThumbnailCreator(QObject *parent, const QVariantList &args)
        : ThumbnailCreator(parent, args)
    {
    }

    KIO::ThumbnailResult create(const KIO::ThumbnailRequest &request) override
    {
        std::ofstream log("/home/snake/tmp/stlthumbnail.log");
        log << "Called create for " << request.url().fileName().toStdString() << "\n";

        const QSize ts = request.targetSize();
        const size_t bufferSize = ts.width() * ts.height() * 4;
        std::unique_ptr<uint8_t, void(*)(void*)> buffer((uint8_t*)malloc(bufferSize), clean);
        const std::string fileName = request.url().fileName().toStdString();

        if (!render_to_buffer(buffer.get(), ts.width(), ts.height(), fileName.c_str()))
        {
            log << "Failed to render\n";
            qCWarning(LOG_STL) << "STLTHUMBNAIL :: Failed to render " << request.url() << "\n";
            return KIO::ThumbnailResult::fail();
        }

        log << "Rendered " << ts.width() << "x" << ts.height() << "\n";
        uint8_t* pixels = buffer.release();
        QImageCleanupFunction f;
        auto result = QImage(pixels, ts.width(), ts.height(), ts.width() * 4, QImage::Format_RGBA8888, clean, pixels);
        return KIO::ThumbnailResult::pass(result);
    }
};

K_PLUGIN_CLASS_WITH_JSON(StlThumbnailCreator, "stlthumbnail.json");
#include "StlThumbnailCreator.moc"

stlthumbnail.json

{
  "CacheThumbnail": false,
  "IgnoreMaximumSize": true,
  "KPlugin": {
      "MimeTypes": [
          "model/stl",
          "model/x.stl-ascii",
          "model/x.stl-binary",
          "application/sla"
      ],
      "Name": "Stereo lithography models"
  }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(stlthumbnail)

set(CMAKE_CXX_STANDARD 17)

find_package(KF5KIO COMPONENTS KIOGui)

add_library(stlthumbnail MODULE StlThumbnailCreator.cpp)
target_link_libraries(stlthumbnail PRIVATE KF5::KIOGui stl_thumb)
set_target_properties(stlthumbnail PROPERTIES PREFIX "" AUTOMOC ON)

This builds without issues. I manually copy the resulting stlthumbnail.so into /usr/lib/qt/plugins/kf5/thumbcreator/. This should be enough, right? But there is no difference, even after logout. The ~/tmp/stlthumbnail.log file is never created which means that my plugin is never executed. I'm out of my depth now, any idea how I can make my plugin trigger?

Note that the underlying library stl-thumb works correctly: thumbnails are displayed in Nautilus, the command-line tool included in the library generates thumbnails, and I successfully wrote a test application which rendered an STL into a memory buffer exactly in the same way as the above source code does.

STL thumbnail in Nautilus

0

There are 0 best solutions below