void ThumbnailProtocol::get(const QUrl &url) { m_mimeType = metaData("mimeType"); //qDebug() << "Wanting MIME Type:" << m_mimeType; #ifdef THUMBNAIL_HACK // ### HACK bool direct=false; if (m_mimeType.isEmpty()) { QFileInfo info(url.path()); //qDebug() << "PATH: " << url.path() << "isDir:" << info.isDir(); if (!info.exists()) { // The file does not exist error(KIO::ERR_DOES_NOT_EXIST,url.path()); return; } else if (!info.isReadable()) { // The file is not readable! error(KIO::ERR_COULD_NOT_READ,url.path()); return; } if (info.isDir()) { m_mimeType = "inode/directory"; } else { const QMimeDatabase db; m_mimeType = db.mimeTypeForUrl(QUrl(info.filePath())).name(); } //qDebug() << "Guessing MIME Type:" << m_mimeType; direct=true; // thumbnail: URL was probably typed in Konqueror } #endif if (m_mimeType.isEmpty()) { error(KIO::ERR_INTERNAL, i18n("No MIME Type specified.")); return; } m_width = metaData("width").toInt(); m_height = metaData("height").toInt(); int iconSize = metaData("iconSize").toInt(); if (m_width < 0 || m_height < 0) { error(KIO::ERR_INTERNAL, i18n("No or invalid size specified.")); return; } #ifdef THUMBNAIL_HACK else if (!m_width || !m_height) { //qDebug() << "Guessing height, width, icon size!"; m_width = 128; m_height = 128; iconSize = 128; } #endif if (!iconSize) { iconSize = KIconLoader::global()->currentSize(KIconLoader::Desktop); } if (iconSize != m_iconSize) { m_iconDict.clear(); } m_iconSize = iconSize; m_iconAlpha = metaData("iconAlpha").toInt(); QImage img; KConfigGroup group( KSharedConfig::openConfig(), "PreviewSettings" ); bool kfmiThumb = false; // TODO Figure out if we can use KFileMetadata as a last resource ThumbCreator::Flags flags = ThumbCreator::None; if (!kfmiThumb) { QString plugin = metaData("plugin"); if ((plugin.isEmpty() || plugin == "directorythumbnail") && m_mimeType == "inode/directory") { img = thumbForDirectory(url); if(img.isNull()) { error(KIO::ERR_INTERNAL, i18n("Cannot create thumbnail for directory")); return; } } else { #ifdef THUMBNAIL_HACK if (plugin.isEmpty()) { plugin = pluginForMimeType(m_mimeType); } //qDebug() << "Guess plugin: " << plugin; #endif if (plugin.isEmpty()) { error(KIO::ERR_INTERNAL, i18n("No plugin specified.")); return; } ThumbCreator* creator = getThumbCreator(plugin); if(!creator) { error(KIO::ERR_INTERNAL, i18n("Cannot load ThumbCreator %1", plugin)); return; } ThumbSequenceCreator* sequenceCreator = dynamic_cast<ThumbSequenceCreator*>(creator); if(sequenceCreator) sequenceCreator->setSequenceIndex(sequenceIndex()); if (!creator->create(url.path(), m_width, m_height, img)) { error(KIO::ERR_INTERNAL, i18n("Cannot create thumbnail for %1", url.path())); return; } flags = creator->flags(); } } scaleDownImage(img, m_width, m_height); if (flags & ThumbCreator::DrawFrame) { int x2 = img.width() - 1; int y2 = img.height() - 1; // paint a black rectangle around the "page" QPainter p; p.begin( &img ); p.setPen( QColor( 48, 48, 48 )); p.drawLine( x2, 0, x2, y2 ); p.drawLine( 0, y2, x2, y2 ); p.setPen( QColor( 215, 215, 215 )); p.drawLine( 0, 0, x2, 0 ); p.drawLine( 0, 0, 0, y2 ); p.end(); } if ((flags & ThumbCreator::BlendIcon) && KIconLoader::global()->alphaBlending(KIconLoader::Desktop)) { // blending the mimetype icon in QImage icon = getIcon(); int x = img.width() - icon.width() - 4; x = qMax( x, 0 ); int y = img.height() - icon.height() - 6; y = qMax( y, 0 ); QPainter p(&img); p.setOpacity(m_iconAlpha/255.0); p.drawImage(x, y, icon); } if (img.isNull()) { error(KIO::ERR_INTERNAL, i18n("Failed to create a thumbnail.")); return; } const QString shmid = metaData("shmid"); if (shmid.isEmpty()) { #ifdef THUMBNAIL_HACK if (direct) { // If thumbnail was called directly from Konqueror, then the image needs to be raw //qDebug() << "RAW IMAGE TO STREAM"; QBuffer buf; if (!buf.open(QIODevice::WriteOnly)) { error(KIO::ERR_INTERNAL, i18n("Could not write image.")); return; } img.save(&buf,"PNG"); buf.close(); mimeType("image/png"); data(buf.buffer()); } else #endif { QByteArray imgData; QDataStream stream( &imgData, QIODevice::WriteOnly ); //qDebug() << "IMAGE TO STREAM"; stream << img; mimeType("application/octet-stream"); data(imgData); } } else { #ifndef Q_OS_WIN QByteArray imgData; QDataStream stream( &imgData, QIODevice::WriteOnly ); //qDebug() << "IMAGE TO SHMID"; void *shmaddr = shmat(shmid.toInt(), 0, 0); if (shmaddr == (void *)-1) { error(KIO::ERR_INTERNAL, i18n("Failed to attach to shared memory segment %1", shmid)); return; } if (img.width() * img.height() > m_width * m_height) { error(KIO::ERR_INTERNAL, i18n("Image is too big for the shared memory segment")); shmdt((char*)shmaddr); return; } if( img.format() != QImage::Format_ARGB32 ) { // KIO::PreviewJob and this code below completely ignores colortable :-/, img = img.convertToFormat(QImage::Format_ARGB32); // so make sure there is none } // Keep in sync with kdelibs/kio/kio/previewjob.cpp stream << img.width() << img.height() << quint8(img.format()); memcpy(shmaddr, img.bits(), img.byteCount()); shmdt((char*)shmaddr); mimeType("application/octet-stream"); data(imgData); #endif } finished(); }
bool ThumbnailProtocol::createSubThumbnail(QImage& thumbnail, const QString& filePath, int segmentWidth, int segmentHeight) { if (m_enabledPlugins.isEmpty()) { const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings"); m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList() << "imagethumbnail" << "jpegthumbnail" << "videopreview"); } const QMimeDatabase db; const QUrl fileUrl = QUrl::fromLocalFile(filePath); const QString subPlugin = pluginForMimeType(db.mimeTypeForUrl(fileUrl).name()); if (subPlugin.isEmpty() || !m_enabledPlugins.contains(subPlugin)) { return false; } ThumbCreator* subCreator = getThumbCreator(subPlugin); if (!subCreator) { // qDebug() << "found no creator for" << dir.filePath(); return false; } if ((segmentWidth <= 256) && (segmentHeight <= 256)) { // check whether a cached version of the file is available for // 128 x 128 or 256 x 256 pixels int cacheSize = 0; QCryptographicHash md5(QCryptographicHash::Md5); md5.addData(QFile::encodeName(fileUrl.toString())); const QString thumbName = QFile::encodeName(md5.result().toHex()).append(".png"); if (m_thumbBasePath.isEmpty()) { m_thumbBasePath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/thumbnails/"); QDir basePath(m_thumbBasePath); basePath.mkpath("normal/"); QFile::setPermissions(basePath.absoluteFilePath("normal"), QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); basePath.mkpath("large/"); QFile::setPermissions(basePath.absoluteFilePath("large"), QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); } QDir thumbPath(m_thumbBasePath); if ((segmentWidth <= 128) && (segmentHeight <= 128)) { cacheSize = 128; thumbPath.cd("normal"); } else { cacheSize = 256; thumbPath.cd("large"); } if (!thumbnail.load(thumbPath.absoluteFilePath(thumbName))) { // no cached version is available, a new thumbnail must be created QSaveFile thumbnailfile(thumbPath.absoluteFilePath(thumbName)); bool savedCorrectly = false; if (subCreator->create(filePath, cacheSize, cacheSize, thumbnail)) { scaleDownImage(thumbnail, cacheSize, cacheSize); // The thumbnail has been created successfully. Store the thumbnail // to the cache for future access. if (thumbnailfile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { savedCorrectly = thumbnail.save(&thumbnailfile, "PNG"); } } else { return false; } if(savedCorrectly) { thumbnailfile.commit(); } } } else if (!subCreator->create(filePath, segmentWidth, segmentHeight, thumbnail)) { return false; } return true; }
void ThumbnailProtocol::get(const KURL &url) { m_mimeType = metaData("mimeType"); kdDebug(7115) << "Wanting MIME Type:" << m_mimeType << endl; #ifdef THUMBNAIL_HACK // ### HACK bool direct=false; if (m_mimeType.isEmpty()) { kdDebug(7115) << "PATH: " << url.path() << endl; TQFileInfo info(url.path()); if (info.isDir()) { // We cannot process a directory error(TDEIO::ERR_IS_DIRECTORY,url.path()); return; } else if (!info.exists()) { // The file does not exist error(TDEIO::ERR_DOES_NOT_EXIST,url.path()); return; } else if (!info.isReadable()) { // The file is not readable! error(TDEIO::ERR_COULD_NOT_READ,url.path()); return; } m_mimeType = KMimeType::findByURL(url)->name(); kdDebug(7115) << "Guessing MIME Type:" << m_mimeType << endl; direct=true; // thumbnail: was probably called from Konqueror } #endif if (m_mimeType.isEmpty()) { error(TDEIO::ERR_INTERNAL, i18n("No MIME Type specified.")); return; } m_width = metaData("width").toInt(); m_height = metaData("height").toInt(); int iconSize = metaData("iconSize").toInt(); if (m_width < 0 || m_height < 0) { error(TDEIO::ERR_INTERNAL, i18n("No or invalid size specified.")); return; } #ifdef THUMBNAIL_HACK else if (!m_width || !m_height) { kdDebug(7115) << "Guessing height, width, icon size!" << endl; m_width=128; m_height=128; iconSize=128; } #endif if (!iconSize) iconSize = TDEGlobal::iconLoader()->currentSize(TDEIcon::Desktop); if (iconSize != m_iconSize) m_iconDict.clear(); m_iconSize = iconSize; m_iconAlpha = metaData("iconAlpha").toInt(); if (m_iconAlpha) m_iconAlpha = (m_iconAlpha << 24) | 0xffffff; TQImage img; TDEConfigGroup group( TDEGlobal::config(), "PreviewSettings" ); // ### KFMI bool kfmiThumb = false; if (group.readBoolEntry( "UseFileThumbnails", true )) { KService::Ptr service = KServiceTypeProfile::preferredService( m_mimeType, "KFilePlugin"); if ( service && service->isValid() && /*url.isLocalFile() && */ service->property("SupportsThumbnail").toBool()) { KFileMetaInfo info(url.path(), m_mimeType, KFileMetaInfo::Thumbnail); if (info.isValid()) { KFileMetaInfoItem item = info.item(KFileMimeTypeInfo::Thumbnail); if (item.isValid() && item.value().type() == TQVariant::Image) { img = item.value().toImage(); kdDebug(7115) << "using KFMI for the thumbnail\n"; kfmiThumb = true; } } } } ThumbCreator::Flags flags = ThumbCreator::None; if (!kfmiThumb) { kdDebug(7115) << "using thumb creator for the thumbnail\n"; TQString plugin = metaData("plugin"); #ifdef THUMBNAIL_HACK if (plugin.isEmpty()) { TDETrader::OfferList plugins = TDETrader::self()->query("ThumbCreator"); TQMap<TQString, KService::Ptr> mimeMap; for (TDETrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it) { TQStringList mimeTypes = (*it)->property("MimeTypes").toStringList(); for (TQStringList::ConstIterator mt = mimeTypes.begin(); mt != mimeTypes.end(); ++mt) { if ((*mt)==m_mimeType) { plugin=(*it)->library(); break; } } if (!plugin.isEmpty()) break; } } kdDebug(7115) << "Guess plugin: " << plugin << endl; #endif if (plugin.isEmpty()) { error(TDEIO::ERR_INTERNAL, i18n("No plugin specified.")); return; } ThumbCreator *creator = m_creators[plugin]; if (!creator) { // Don't use KLibFactory here, this is not a TQObject and // neither is ThumbCreator KLibrary *library = KLibLoader::self()->library(TQFile::encodeName(plugin)); if (library) { newCreator create = (newCreator)library->symbol("new_creator"); if (create) creator = create(); } if (!creator) { error(TDEIO::ERR_INTERNAL, i18n("Cannot load ThumbCreator %1").arg(plugin)); return; } m_creators.insert(plugin, creator); } if (!creator->create(url.path(), m_width, m_height, img)) { error(TDEIO::ERR_INTERNAL, i18n("Cannot create thumbnail for %1").arg(url.path())); return; } flags = creator->flags(); } if (img.width() > m_width || img.height() > m_height) { double imgRatio = (double)img.height() / (double)img.width(); if (imgRatio > (double)m_height / (double)m_width) img = img.smoothScale( int(TQMAX((double)m_height / imgRatio, 1)), m_height); else img = img.smoothScale(m_width, int(TQMAX((double)m_width * imgRatio, 1))); } // ### FIXME #ifndef USE_KINSTANCE if (flags & ThumbCreator::DrawFrame) { TQPixmap pix; pix.convertFromImage(img); int x2 = pix.width() - 1; int y2 = pix.height() - 1; // paint a black rectangle around the "page" TQPainter p; p.begin( &pix ); p.setPen( TQColor( 48, 48, 48 )); p.drawLine( x2, 0, x2, y2 ); p.drawLine( 0, y2, x2, y2 ); p.setPen( TQColor( 215, 215, 215 )); p.drawLine( 0, 0, x2, 0 ); p.drawLine( 0, 0, 0, y2 ); p.end(); const TQBitmap *mask = pix.mask(); if ( mask ) // need to update it so we can see the frame { TQBitmap bitmap( *mask ); TQPainter painter; painter.begin( &bitmap ); painter.drawLine( x2, 0, x2, y2 ); painter.drawLine( 0, y2, x2, y2 ); painter.drawLine( 0, 0, x2, 0 ); painter.drawLine( 0, 0, 0, y2 ); painter.end(); pix.setMask( bitmap ); } img = pix.convertToImage(); } #endif if ((flags & ThumbCreator::BlendIcon) && TDEGlobal::iconLoader()->alphaBlending(TDEIcon::Desktop)) { // blending the mimetype icon in TQImage icon = getIcon(); int x = img.width() - icon.width() - 4; x = TQMAX( x, 0 ); int y = img.height() - icon.height() - 6; y = TQMAX( y, 0 ); KImageEffect::blendOnLower( x, y, icon, img ); } if (img.isNull()) { error(TDEIO::ERR_INTERNAL, i18n("Failed to create a thumbnail.")); return; } const TQString shmid = metaData("shmid"); if (shmid.isEmpty()) { #ifdef THUMBNAIL_HACK if (direct) { // If thumbnail was called directly from Konqueror, then the image needs to be raw //kdDebug(7115) << "RAW IMAGE TO STREAM" << endl; TQBuffer buf; if (!buf.open(IO_WriteOnly)) { error(TDEIO::ERR_INTERNAL, i18n("Could not write image.")); return; } img.save(&buf,"PNG"); buf.close(); data(buf.buffer()); } else #endif { TQByteArray imgData; TQDataStream stream( imgData, IO_WriteOnly ); //kdDebug(7115) << "IMAGE TO STREAM" << endl; stream << img; data(imgData); } } else { TQByteArray imgData; TQDataStream stream( imgData, IO_WriteOnly ); //kdDebug(7115) << "IMAGE TO SHMID" << endl; void *shmaddr = shmat(shmid.toInt(), 0, 0); if (shmaddr == (void *)-1) { error(TDEIO::ERR_INTERNAL, i18n("Failed to attach to shared memory segment %1").arg(shmid)); return; } if (img.width() * img.height() > m_width * m_height) { error(TDEIO::ERR_INTERNAL, i18n("Image is too big for the shared memory segment")); shmdt((char*)shmaddr); return; } if( img.depth() != 32 ) // TDEIO::PreviewJob and this code below completely img = img.convertDepth( 32 ); // ignores colortable :-/, so make sure there is none stream << img.width() << img.height() << img.depth() << img.hasAlphaBuffer(); memcpy(shmaddr, img.bits(), img.numBytes()); shmdt((char*)shmaddr); data(imgData); } finished(); }