File indexing completed on 2024-06-02 05:43:56
0001 /* 0002 SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "FontThumbnail.h" 0007 #include "KfiConstants.h" 0008 #include "Misc.h" 0009 0010 #include <KPluginFactory> 0011 #include <KZip> 0012 0013 #include <QApplication> 0014 #include <QDebug> 0015 #include <QDir> 0016 #include <QFile> 0017 #include <QImage> 0018 #include <QMimeDatabase> 0019 #include <QPalette> 0020 #include <QTemporaryDir> 0021 0022 #include "debug.h" 0023 0024 CFontThumbnail::CFontThumbnail(QObject *parent, const QVariantList &args) 0025 : KIO::ThumbnailCreator(parent, args) 0026 { 0027 } 0028 0029 CFontThumbnail::~CFontThumbnail() = default; 0030 0031 KIO::ThumbnailResult CFontThumbnail::create(const KIO::ThumbnailRequest &request) 0032 { 0033 const QString &path = request.url().toLocalFile(); 0034 QString realPath(path); 0035 QTemporaryDir *tempDir = nullptr; 0036 0037 qCDebug(KCM_KFONTINST_THUMBNAIL) << "Create font thumbnail for:" << path << Qt::endl; 0038 0039 // Is this a appliaction/vnd.kde.fontspackage file? If so, extract 1 scalable font... 0040 QMimeDatabase db; 0041 if (KFI::Misc::isPackage(path) || "application/zip" == db.mimeTypeForFile(path, QMimeDatabase::MatchContent).name()) { 0042 KZip zip(path); 0043 0044 if (zip.open(QIODevice::ReadOnly)) { 0045 const KArchiveDirectory *zipDir = zip.directory(); 0046 0047 if (zipDir) { 0048 QStringList fonts(zipDir->entries()); 0049 0050 if (!fonts.isEmpty()) { 0051 QStringList::ConstIterator it(fonts.begin()), end(fonts.end()); 0052 0053 for (; it != end; ++it) { 0054 const KArchiveEntry *entry = zipDir->entry(*it); 0055 0056 if (entry && entry->isFile()) { 0057 delete tempDir; 0058 tempDir = new QTemporaryDir(QDir::tempPath() + "/" KFI_TMP_DIR_PREFIX); 0059 tempDir->setAutoRemove(true); 0060 0061 ((KArchiveFile *)entry)->copyTo(tempDir->path()); 0062 0063 QString mime(db.mimeTypeForFile(tempDir->filePath(entry->name())).name()); 0064 0065 if (mime == "font/ttf" || mime == "font/otf" || mime == "application/x-font-ttf" || mime == "application/x-font-otf" 0066 || mime == "application/x-font-type1") { 0067 realPath = tempDir->filePath(entry->name()); 0068 break; 0069 } else { 0070 ::unlink(QFile::encodeName(tempDir->filePath(entry->name())).data()); 0071 } 0072 } 0073 } 0074 } 0075 } 0076 } 0077 } 0078 0079 QColor bgnd(Qt::black); 0080 0081 bgnd.setAlpha(0); 0082 QImage img = m_engine.draw(realPath, 0083 KFI_NO_STYLE_INFO, 0084 0, 0085 QApplication::palette().text().color(), 0086 bgnd, 0087 request.targetSize().width(), 0088 request.targetSize().height(), 0089 true); 0090 0091 delete tempDir; 0092 return !img.isNull() ? KIO::ThumbnailResult::pass(img) : KIO::ThumbnailResult::fail(); 0093 } 0094 0095 K_PLUGIN_CLASS_WITH_JSON(CFontThumbnail, "fontthumbnail.json") 0096 0097 #include "FontThumbnail.moc"