File indexing completed on 2024-04-28 15:39:47

0001 // SPDX-FileCopyrightText: 2012 Jesper K. Pedersen <blackie@kde.org>
0002 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 #include "ExtractOneThumbnailJob.h"
0007 
0008 #include "HandleVideoThumbnailRequestJob.h"
0009 
0010 #include <DB/ImageDB.h>
0011 #include <ImageManager/ExtractOneVideoFrame.h>
0012 #include <Utilities/ImageUtil.h>
0013 
0014 #include <KLocalizedString>
0015 #include <QFile>
0016 #include <QImage>
0017 #include <QPainter>
0018 
0019 namespace
0020 {
0021 constexpr QFileDevice::Permissions FILE_PERMISSIONS { QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::WriteGroup | QFile::ReadOther };
0022 }
0023 
0024 namespace BackgroundJobs
0025 {
0026 
0027 ExtractOneThumbnailJob::ExtractOneThumbnailJob(const DB::FileName &fileName, int index, BackgroundTaskManager::Priority priority)
0028     : JobInterface(priority)
0029     , m_fileName(fileName)
0030     , m_index(index)
0031     , m_wasCanceled(false)
0032 {
0033     Q_ASSERT(index >= 0 && index <= 9);
0034 }
0035 
0036 void ExtractOneThumbnailJob::execute()
0037 {
0038     if (m_wasCanceled || frameName().exists())
0039         Q_EMIT completed();
0040     else {
0041         DB::ImageInfoPtr info = DB::ImageDB::instance()->info(m_fileName);
0042         const int length = info->videoLength();
0043         ImageManager::ExtractOneVideoFrame::extract(m_fileName, length * m_index / 10.0, this, SLOT(frameLoaded(QImage)));
0044     }
0045 }
0046 
0047 QString ExtractOneThumbnailJob::title() const
0048 {
0049     return i18n("Extracting Thumbnail");
0050 }
0051 
0052 QString ExtractOneThumbnailJob::details() const
0053 {
0054     return QString::fromLatin1("%1 #%2").arg(m_fileName.relative()).arg(m_index);
0055 }
0056 
0057 int ExtractOneThumbnailJob::index() const
0058 {
0059     return m_index;
0060 }
0061 
0062 void ExtractOneThumbnailJob::cancel()
0063 {
0064     m_wasCanceled = true;
0065 }
0066 
0067 void ExtractOneThumbnailJob::frameLoaded(const QImage &image)
0068 {
0069     if (!image.isNull()) {
0070 #if 0
0071         QImage img = image;
0072         {
0073             QPainter painter(&img);
0074             QFont fnt;
0075             fnt.setPointSize(24);
0076             painter.setFont(fnt);
0077             painter.drawText(QPoint(100,100),QString::number(m_index));
0078         }
0079 #endif
0080         Utilities::saveImage(frameName(), image, "JPEG");
0081     } else {
0082         // Create empty file to avoid that we recheck at next start up.
0083         QFile file(frameName().absolute());
0084         if (file.open(QFile::WriteOnly)) {
0085             file.setPermissions(FILE_PERMISSIONS);
0086             file.close();
0087         }
0088     }
0089     Q_EMIT completed();
0090 }
0091 
0092 DB::FileName ExtractOneThumbnailJob::frameName() const
0093 {
0094     return BackgroundJobs::HandleVideoThumbnailRequestJob::frameName(m_fileName, m_index);
0095 }
0096 
0097 } // namespace BackgroundJobs
0098 // vi:expandtab:tabstop=4 shiftwidth=4:
0099 
0100 #include "moc_ExtractOneThumbnailJob.cpp"