KIO
previewjob.cpp
Go to the documentation of this file.
00001 // -*- c++ -*- 00002 // vim: ts=4 sw=4 et 00003 /* This file is part of the KDE libraries 00004 Copyright (C) 2000 David Faure <faure@kde.org> 00005 2000 Carsten Pfeiffer <pfeiffer@kde.org> 00006 2001 Malte Starostik <malte.starostik@t-online.de> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include "previewjob.h" 00025 #include <kdebug.h> 00026 00027 #include <sys/stat.h> 00028 #include <sys/types.h> 00029 00030 #ifdef Q_OS_UNIX 00031 #include <sys/ipc.h> 00032 #include <sys/shm.h> 00033 #endif 00034 00035 #include <QtCore/QDir> 00036 #include <QtCore/QFile> 00037 #include <QtGui/QImage> 00038 #include <QtCore/QTimer> 00039 #include <QtCore/QRegExp> 00040 00041 #include <kfileitem.h> 00042 #include <kde_file.h> 00043 #include <ktemporaryfile.h> 00044 #include <kservicetypetrader.h> 00045 #include <kcodecs.h> 00046 #include <kglobal.h> 00047 #include <kstandarddirs.h> 00048 #include <kservice.h> 00049 #include <QtCore/QLinkedList> 00050 #include <kconfiggroup.h> 00051 #include <kprotocolinfo.h> 00052 00053 #include "jobuidelegate.h" 00054 #include "job_p.h" 00055 00056 namespace KIO { struct PreviewItem; } 00057 using namespace KIO; 00058 00059 struct KIO::PreviewItem 00060 { 00061 KFileItem item; 00062 KService::Ptr plugin; 00063 }; 00064 00065 class KIO::PreviewJobPrivate: public KIO::JobPrivate 00066 { 00067 public: 00068 enum { STATE_STATORIG, // if the thumbnail exists 00069 STATE_GETORIG, // if we create it 00070 STATE_CREATETHUMB // thumbnail:/ slave 00071 } state; 00072 PreviewJob *q; 00073 00074 KFileItemList initialItems; 00075 QStringList enabledPlugins; 00076 // Some plugins support remote URLs, <protocol, mimetypes> 00077 QHash<QString, QStringList> m_remoteProtocolPlugins; 00078 // Our todo list :) 00079 // We remove the first item at every step, so use QLinkedList 00080 QLinkedList<PreviewItem> items; 00081 // The current item 00082 PreviewItem currentItem; 00083 // The modification time of that URL 00084 time_t tOrig; 00085 // Path to thumbnail cache for the current size 00086 QString thumbPath; 00087 // Original URL of current item in TMS format 00088 // (file:///path/to/file instead of file:/path/to/file) 00089 QString origName; 00090 // Thumbnail file name for current item 00091 QString thumbName; 00092 // Size of thumbnail 00093 int width; 00094 int height; 00095 // Unscaled size of thumbnail (128 or 256 if cache is enabled) 00096 int cacheWidth; 00097 int cacheHeight; 00098 // Whether the thumbnail should be scaled 00099 bool bScale; 00100 // Whether we should save the thumbnail 00101 bool bSave; 00102 bool ignoreMaximumSize; 00103 int sequenceIndex; 00104 bool succeeded; 00105 // If the file to create a thumb for was a temp file, this is its name 00106 QString tempName; 00107 KIO::filesize_t maximumLocalSize; 00108 KIO::filesize_t maximumRemoteSize; 00109 // the size for the icon overlay 00110 int iconSize; 00111 // the transparency of the blended mimetype icon 00112 int iconAlpha; 00113 // Shared memory segment Id. The segment is allocated to a size 00114 // of extent x extent x 4 (32 bit image) on first need. 00115 int shmid; 00116 // And the data area 00117 uchar *shmaddr; 00118 // Root of thumbnail cache 00119 QString thumbRoot; 00120 00121 void getOrCreateThumbnail(); 00122 bool statResultThumbnail(); 00123 void createThumbnail( const QString& ); 00124 void determineNextFile(); 00125 void emitPreview(const QImage &thumb); 00126 00127 void startPreview(); 00128 void slotThumbData(KIO::Job *, const QByteArray &); 00129 00130 Q_DECLARE_PUBLIC(PreviewJob) 00131 }; 00132 00133 #ifndef KDE_NO_DEPRECATED 00134 PreviewJob::PreviewJob( const KFileItemList &items, int width, int height, 00135 int iconSize, int iconAlpha, bool scale, bool save, 00136 const QStringList *enabledPlugins ) 00137 : KIO::Job(*new PreviewJobPrivate) 00138 { 00139 Q_D(PreviewJob); 00140 d->tOrig = 0; 00141 d->shmid = -1; 00142 d->shmaddr = 0; 00143 d->initialItems = items; 00144 d->enabledPlugins = enabledPlugins ? *enabledPlugins : availablePlugins(); 00145 d->width = width; 00146 d->height = height ? height : width; 00147 d->cacheWidth = d->width; 00148 d->cacheHeight = d->height; 00149 d->iconSize = iconSize; 00150 d->iconAlpha = iconAlpha; 00151 d->bScale = scale; 00152 d->bSave = save && scale; 00153 d->succeeded = false; 00154 d->thumbRoot = QDir::homePath() + QLatin1String("/.thumbnails/"); 00155 d->ignoreMaximumSize = false; 00156 d->sequenceIndex = 0; 00157 d->maximumLocalSize = 0; 00158 d->maximumRemoteSize = 0; 00159 00160 // Return to event loop first, determineNextFile() might delete this; 00161 QTimer::singleShot(0, this, SLOT(startPreview())); 00162 } 00163 #endif 00164 00165 PreviewJob::PreviewJob(const KFileItemList &items, 00166 const QSize &size, 00167 const QStringList *enabledPlugins) : 00168 KIO::Job(*new PreviewJobPrivate) 00169 { 00170 Q_D(PreviewJob); 00171 d->tOrig = 0; 00172 d->shmid = -1; 00173 d->shmaddr = 0; 00174 d->initialItems = items; 00175 if (enabledPlugins) { 00176 d->enabledPlugins = *enabledPlugins; 00177 } else { 00178 const KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings"); 00179 d->enabledPlugins = globalConfig.readEntry("Plugins", QStringList() 00180 << "directorythumbnail" 00181 << "imagethumbnail" 00182 << "jpegthumbnail"); 00183 } 00184 d->width = size.width(); 00185 d->height = size.height(); 00186 d->cacheWidth = d->width; 00187 d->cacheHeight = d->height; 00188 d->iconSize = 0; 00189 d->iconAlpha = 70; 00190 d->bScale = true; 00191 d->bSave = true; 00192 d->succeeded = false; 00193 d->thumbRoot = QDir::homePath() + QLatin1String("/.thumbnails/"); 00194 d->ignoreMaximumSize = false; 00195 d->sequenceIndex = 0; 00196 d->maximumLocalSize = 0; 00197 d->maximumRemoteSize = 0; 00198 00199 // Return to event loop first, determineNextFile() might delete this; 00200 QTimer::singleShot(0, this, SLOT(startPreview())); 00201 } 00202 00203 PreviewJob::~PreviewJob() 00204 { 00205 #ifdef Q_OS_UNIX 00206 Q_D(PreviewJob); 00207 if (d->shmaddr) { 00208 shmdt((char*)d->shmaddr); 00209 shmctl(d->shmid, IPC_RMID, 0); 00210 } 00211 #endif 00212 } 00213 00214 void PreviewJob::setOverlayIconSize(int size) 00215 { 00216 Q_D(PreviewJob); 00217 d->iconSize = size; 00218 } 00219 00220 int PreviewJob::overlayIconSize() const 00221 { 00222 Q_D(const PreviewJob); 00223 return d->iconSize; 00224 } 00225 00226 void PreviewJob::setOverlayIconAlpha(int alpha) 00227 { 00228 Q_D(PreviewJob); 00229 d->iconAlpha = qBound(0, alpha, 255); 00230 } 00231 00232 int PreviewJob::overlayIconAlpha() const 00233 { 00234 Q_D(const PreviewJob); 00235 return d->iconAlpha; 00236 } 00237 00238 void PreviewJob::setScaleType(ScaleType type) 00239 { 00240 Q_D(PreviewJob); 00241 switch (type) { 00242 case Unscaled: 00243 d->bScale = false; 00244 d->bSave = false; 00245 break; 00246 case Scaled: 00247 d->bScale = true; 00248 d->bSave = false; 00249 break; 00250 case ScaledAndCached: 00251 d->bScale = true; 00252 d->bSave = true; 00253 break; 00254 default: 00255 break; 00256 } 00257 } 00258 00259 PreviewJob::ScaleType PreviewJob::scaleType() const 00260 { 00261 Q_D(const PreviewJob); 00262 if (d->bScale) { 00263 return d->bSave ? ScaledAndCached : Scaled; 00264 } 00265 return Unscaled; 00266 } 00267 00268 void PreviewJobPrivate::startPreview() 00269 { 00270 Q_Q(PreviewJob); 00271 // Load the list of plugins to determine which mimetypes are supported 00272 const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator"); 00273 QMap<QString, KService::Ptr> mimeMap; 00274 QHash<QString, QHash<QString, KService::Ptr> > protocolMap; 00275 for (KService::List::ConstIterator it = plugins.constBegin(); it != plugins.constEnd(); ++it) { 00276 const QStringList protocols = (*it)->property("X-KDE-Protocol").toStringList(); 00277 foreach (const QString &protocol, protocols) { 00278 QStringList mtypes = (*it)->serviceTypes(); 00279 // Filter out non-mimetype servicetypes 00280 // TODO KDE5: use KService::mimeTypes() 00281 foreach (const QString &_mtype, mtypes) { 00282 if (!((*it)->hasMimeType(_mtype))) { 00283 mtypes.removeAll(_mtype); 00284 } 00285 } 00286 // Add supported mimetype for this protocol 00287 QStringList &_ms = m_remoteProtocolPlugins[protocol]; 00288 foreach (const QString &_m, mtypes) { 00289 protocolMap[protocol].insert(_m, *it); 00290 if (!_ms.contains(_m)) { 00291 _ms.append(_m); 00292 } 00293 } 00294 } 00295 if (enabledPlugins.contains((*it)->desktopEntryName())) { 00296 const QStringList mimeTypes = (*it)->serviceTypes(); 00297 for (QStringList::ConstIterator mt = mimeTypes.constBegin(); mt != mimeTypes.constEnd(); ++mt) 00298 mimeMap.insert(*mt, *it); 00299 } 00300 } 00301 00302 // Look for images and store the items in our todo list :) 00303 bool bNeedCache = false; 00304 KFileItemList::const_iterator kit = initialItems.constBegin(); 00305 const KFileItemList::const_iterator kend = initialItems.constEnd(); 00306 for ( ; kit != kend; ++kit ) 00307 { 00308 PreviewItem item; 00309 item.item = *kit; 00310 const QString mimeType = item.item.mimetype(); 00311 KService::Ptr plugin(0); 00312 00313 // look for protocol-specific thumbnail plugins first 00314 QHash<QString, QHash<QString, KService::Ptr> >::const_iterator it = protocolMap.constFind(item.item.url().protocol()); 00315 if (it != protocolMap.constEnd()) { 00316 plugin = it.value().value(mimeType); 00317 } 00318 00319 if (!plugin) { 00320 QMap<QString, KService::Ptr>::ConstIterator pluginIt = mimeMap.constFind(mimeType); 00321 if (pluginIt == mimeMap.constEnd()) { 00322 QString groupMimeType = mimeType; 00323 groupMimeType.replace(QRegExp("/.*"), "/*"); 00324 pluginIt = mimeMap.constFind(groupMimeType); 00325 00326 if (pluginIt == mimeMap.constEnd()) { 00327 // check mime type inheritance, resolve aliases 00328 const KMimeType::Ptr mimeInfo = KMimeType::mimeType(mimeType); 00329 if (mimeInfo) { 00330 const QStringList parentMimeTypes = mimeInfo->allParentMimeTypes(); 00331 Q_FOREACH(const QString& parentMimeType, parentMimeTypes) { 00332 pluginIt = mimeMap.constFind(parentMimeType); 00333 if (pluginIt != mimeMap.constEnd()) 00334 break; 00335 } 00336 } 00337 } 00338 } 00339 00340 if (pluginIt != mimeMap.constEnd()) { 00341 plugin = *pluginIt; 00342 } 00343 } 00344 00345 if (plugin) { 00346 item.plugin = plugin; 00347 items.append(item); 00348 if (!bNeedCache && bSave && 00349 ((*kit).url().protocol() != "file" || 00350 !(*kit).url().directory( KUrl::AppendTrailingSlash ).startsWith(thumbRoot)) && 00351 plugin->property("CacheThumbnail").toBool()) { 00352 bNeedCache = true; 00353 } 00354 } else { 00355 emit q->failed( *kit ); 00356 } 00357 } 00358 00359 KConfigGroup cg( KGlobal::config(), "PreviewSettings" ); 00360 maximumLocalSize = cg.readEntry( "MaximumSize", 5*1024*1024LL /* 5MB */ ); 00361 maximumRemoteSize = cg.readEntry( "MaximumRemoteSize", 0 ); 00362 00363 if (bNeedCache) 00364 { 00365 if (width <= 128 && height <= 128) cacheWidth = cacheHeight = 128; 00366 else cacheWidth = cacheHeight = 256; 00367 thumbPath = thumbRoot + (cacheWidth == 128 ? "normal/" : "large/"); 00368 KStandardDirs::makeDir(thumbPath, 0700); 00369 } 00370 else 00371 bSave = false; 00372 00373 initialItems.clear(); 00374 determineNextFile(); 00375 } 00376 00377 void PreviewJob::removeItem( const KUrl& url ) 00378 { 00379 Q_D(PreviewJob); 00380 for (QLinkedList<PreviewItem>::Iterator it = d->items.begin(); it != d->items.end(); ++it) 00381 if ((*it).item.url() == url) 00382 { 00383 d->items.erase(it); 00384 break; 00385 } 00386 00387 if (d->currentItem.item.url() == url) 00388 { 00389 KJob* job = subjobs().first(); 00390 job->kill(); 00391 removeSubjob( job ); 00392 d->determineNextFile(); 00393 } 00394 } 00395 00396 void KIO::PreviewJob::setSequenceIndex(int index) { 00397 d_func()->sequenceIndex = index; 00398 } 00399 00400 int KIO::PreviewJob::sequenceIndex() const { 00401 return d_func()->sequenceIndex; 00402 } 00403 00404 void PreviewJob::setIgnoreMaximumSize(bool ignoreSize) 00405 { 00406 d_func()->ignoreMaximumSize = ignoreSize; 00407 } 00408 00409 void PreviewJobPrivate::determineNextFile() 00410 { 00411 Q_Q(PreviewJob); 00412 if (!currentItem.item.isNull()) 00413 { 00414 if (!succeeded) 00415 emit q->failed( currentItem.item ); 00416 } 00417 // No more items ? 00418 if ( items.isEmpty() ) 00419 { 00420 q->emitResult(); 00421 return; 00422 } 00423 else 00424 { 00425 // First, stat the orig file 00426 state = PreviewJobPrivate::STATE_STATORIG; 00427 currentItem = items.first(); 00428 succeeded = false; 00429 items.removeFirst(); 00430 KIO::Job *job = KIO::stat( currentItem.item.url(), KIO::HideProgressInfo ); 00431 job->addMetaData( "no-auth-prompt", "true" ); 00432 q->addSubjob(job); 00433 } 00434 } 00435 00436 void PreviewJob::slotResult( KJob *job ) 00437 { 00438 Q_D(PreviewJob); 00439 00440 removeSubjob(job); 00441 Q_ASSERT ( !hasSubjobs() ); // We should have only one job at a time ... 00442 switch ( d->state ) 00443 { 00444 case PreviewJobPrivate::STATE_STATORIG: 00445 { 00446 if (job->error()) // that's no good news... 00447 { 00448 // Drop this one and move on to the next one 00449 d->determineNextFile(); 00450 return; 00451 } 00452 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult(); 00453 d->tOrig = entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, 0 ); 00454 00455 bool skipCurrentItem = false; 00456 const KIO::filesize_t size = (KIO::filesize_t)entry.numberValue( KIO::UDSEntry::UDS_SIZE, 0 ); 00457 const KUrl itemUrl = d->currentItem.item.mostLocalUrl(); 00458 00459 if (itemUrl.isLocalFile() || KProtocolInfo::protocolClass(itemUrl.protocol()) == QLatin1String(":local")) 00460 { 00461 skipCurrentItem = !d->ignoreMaximumSize && size > d->maximumLocalSize 00462 && !d->currentItem.plugin->property("IgnoreMaximumSize").toBool(); 00463 } 00464 else 00465 { 00466 // For remote items the "IgnoreMaximumSize" plugin property is not respected 00467 skipCurrentItem = !d->ignoreMaximumSize && size > d->maximumRemoteSize; 00468 00469 // Remote directories are not supported, don't try to do a file_copy on them 00470 if (!skipCurrentItem) { 00471 // TODO update item.mimeType from the UDS entry, in case it wasn't set initially 00472 KMimeType::Ptr mime = d->currentItem.item.mimeTypePtr(); 00473 if (mime && mime->is("inode/directory")) { 00474 skipCurrentItem = true; 00475 } 00476 } 00477 } 00478 if (skipCurrentItem) 00479 { 00480 d->determineNextFile(); 00481 return; 00482 } 00483 00484 bool pluginHandlesSequences = d->currentItem.plugin->property("HandleSequences", QVariant::Bool).toBool(); 00485 if ( !d->currentItem.plugin->property( "CacheThumbnail" ).toBool() || (d->sequenceIndex && pluginHandlesSequences) ) 00486 { 00487 // This preview will not be cached, no need to look for a saved thumbnail 00488 // Just create it, and be done 00489 d->getOrCreateThumbnail(); 00490 return; 00491 } 00492 00493 if ( d->statResultThumbnail() ) 00494 return; 00495 00496 d->getOrCreateThumbnail(); 00497 return; 00498 } 00499 case PreviewJobPrivate::STATE_GETORIG: 00500 { 00501 if (job->error()) 00502 { 00503 d->determineNextFile(); 00504 return; 00505 } 00506 00507 d->createThumbnail( static_cast<KIO::FileCopyJob*>(job)->destUrl().toLocalFile() ); 00508 return; 00509 } 00510 case PreviewJobPrivate::STATE_CREATETHUMB: 00511 { 00512 if (!d->tempName.isEmpty()) 00513 { 00514 QFile::remove(d->tempName); 00515 d->tempName.clear(); 00516 } 00517 d->determineNextFile(); 00518 return; 00519 } 00520 } 00521 } 00522 00523 bool PreviewJobPrivate::statResultThumbnail() 00524 { 00525 if ( thumbPath.isEmpty() ) 00526 return false; 00527 00528 KUrl url = currentItem.item.mostLocalUrl(); 00529 // Don't include the password if any 00530 url.setPass(QString()); 00531 origName = url.url(); 00532 00533 KMD5 md5( QFile::encodeName( origName ) ); 00534 thumbName = QFile::encodeName( md5.hexDigest() ) + ".png"; 00535 00536 QImage thumb; 00537 if ( !thumb.load( thumbPath + thumbName ) ) return false; 00538 00539 if ( thumb.text( "Thumb::URI", 0 ) != origName || 00540 thumb.text( "Thumb::MTime", 0 ).toInt() != tOrig ) return false; 00541 00542 QString thumbnailerVersion = currentItem.plugin->property("ThumbnailerVersion", QVariant::String).toString(); 00543 00544 if (!thumbnailerVersion.isEmpty() && thumb.text("Software", 0).startsWith("KDE Thumbnail Generator")) { 00545 //Check if the version matches 00546 //The software string should read "KDE Thumbnail Generator pluginName (vX)" 00547 QString softwareString = thumb.text("Software", 0).remove("KDE Thumbnail Generator").trimmed(); 00548 if (softwareString.isEmpty()) { 00549 // The thumbnail has been created with an older version, recreating 00550 return false; 00551 } 00552 int versionIndex = softwareString.lastIndexOf("(v"); 00553 if (versionIndex < 0) { 00554 return false; 00555 } 00556 00557 QString cachedVersion = softwareString.remove(0, versionIndex+2); 00558 cachedVersion.chop(1); 00559 uint thumbnailerMajor = thumbnailerVersion.toInt(); 00560 uint cachedMajor = cachedVersion.toInt(); 00561 if (thumbnailerMajor > cachedMajor) { 00562 return false; 00563 } 00564 } 00565 00566 // Found it, use it 00567 emitPreview( thumb ); 00568 succeeded = true; 00569 determineNextFile(); 00570 return true; 00571 } 00572 00573 00574 void PreviewJobPrivate::getOrCreateThumbnail() 00575 { 00576 Q_Q(PreviewJob); 00577 // We still need to load the orig file ! (This is getting tedious) :) 00578 const KFileItem& item = currentItem.item; 00579 const QString localPath = item.localPath(); 00580 if (!localPath.isEmpty()) { 00581 createThumbnail( localPath ); 00582 } else { 00583 const KUrl fileUrl = item.url(); 00584 // heuristics for remote URL support 00585 bool supportsProtocol = false; 00586 if (m_remoteProtocolPlugins.value(fileUrl.scheme()).contains(item.mimetype())) { 00587 // There's a plugin supporting this protocol and mimetype 00588 supportsProtocol = true; 00589 } else if (m_remoteProtocolPlugins.value("KIO").contains(item.mimetype())) { 00590 // Assume KIO understands any URL, ThumbCreator slaves who have 00591 // X-KDE-Protocol=KIO, will get feed the remote URL directly. 00592 supportsProtocol = true; 00593 } 00594 00595 if (supportsProtocol) { 00596 createThumbnail(fileUrl.url()); 00597 return; 00598 } 00599 // No plugin support access to this remote content, copy the file 00600 // to the local machine, then create the thumbnail 00601 state = PreviewJobPrivate::STATE_GETORIG; 00602 KTemporaryFile localFile; 00603 localFile.setAutoRemove(false); 00604 localFile.open(); 00605 KUrl localURL; 00606 localURL.setPath( tempName = localFile.fileName() ); 00607 const KUrl currentURL = item.mostLocalUrl(); 00608 KIO::Job * job = KIO::file_copy( currentURL, localURL, -1, KIO::Overwrite | KIO::HideProgressInfo /* No GUI */ ); 00609 job->addMetaData("thumbnail","1"); 00610 q->addSubjob(job); 00611 } 00612 } 00613 00614 void PreviewJobPrivate::createThumbnail( const QString &pixPath ) 00615 { 00616 Q_Q(PreviewJob); 00617 state = PreviewJobPrivate::STATE_CREATETHUMB; 00618 KUrl thumbURL; 00619 thumbURL.setProtocol("thumbnail"); 00620 thumbURL.setPath(pixPath); 00621 KIO::TransferJob *job = KIO::get(thumbURL, NoReload, HideProgressInfo); 00622 q->addSubjob(job); 00623 q->connect(job, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(slotThumbData(KIO::Job*,QByteArray))); 00624 bool save = bSave && currentItem.plugin->property("CacheThumbnail").toBool() && !sequenceIndex; 00625 job->addMetaData("mimeType", currentItem.item.mimetype()); 00626 job->addMetaData("width", QString().setNum(save ? cacheWidth : width)); 00627 job->addMetaData("height", QString().setNum(save ? cacheHeight : height)); 00628 job->addMetaData("iconSize", QString().setNum(save ? 64 : iconSize)); 00629 job->addMetaData("iconAlpha", QString().setNum(iconAlpha)); 00630 job->addMetaData("plugin", currentItem.plugin->library()); 00631 if(sequenceIndex) 00632 job->addMetaData("sequence-index", QString().setNum(sequenceIndex)); 00633 00634 #ifdef Q_OS_UNIX 00635 if (shmid == -1) 00636 { 00637 if (shmaddr) { 00638 shmdt((char*)shmaddr); 00639 shmctl(shmid, IPC_RMID, 0); 00640 } 00641 shmid = shmget(IPC_PRIVATE, cacheWidth * cacheHeight * 4, IPC_CREAT|0600); 00642 if (shmid != -1) 00643 { 00644 shmaddr = (uchar *)(shmat(shmid, 0, SHM_RDONLY)); 00645 if (shmaddr == (uchar *)-1) 00646 { 00647 shmctl(shmid, IPC_RMID, 0); 00648 shmaddr = 0; 00649 shmid = -1; 00650 } 00651 } 00652 else 00653 shmaddr = 0; 00654 } 00655 if (shmid != -1) 00656 job->addMetaData("shmid", QString().setNum(shmid)); 00657 #endif 00658 } 00659 00660 void PreviewJobPrivate::slotThumbData(KIO::Job *, const QByteArray &data) 00661 { 00662 bool save = bSave && 00663 currentItem.plugin->property("CacheThumbnail").toBool() && 00664 (currentItem.item.url().protocol() != "file" || 00665 !currentItem.item.url().directory( KUrl::AppendTrailingSlash ).startsWith(thumbRoot)) && !sequenceIndex; 00666 QImage thumb; 00667 #ifdef Q_OS_UNIX 00668 if (shmaddr) 00669 { 00670 // Keep this in sync with kdebase/kioslave/thumbnail.cpp 00671 QDataStream str(data); 00672 int width, height; 00673 quint8 iFormat; 00674 str >> width >> height >> iFormat; 00675 QImage::Format format = static_cast<QImage::Format>( iFormat ); 00676 thumb = QImage(shmaddr, width, height, format ).copy(); 00677 } 00678 else 00679 #endif 00680 thumb.loadFromData(data); 00681 00682 if (thumb.isNull()) { 00683 QDataStream s(data); 00684 s >> thumb; 00685 } 00686 00687 QString tempFileName; 00688 bool savedCorrectly = false; 00689 if (save) 00690 { 00691 thumb.setText("Thumb::URI", origName); 00692 thumb.setText("Thumb::MTime", QString::number(tOrig)); 00693 thumb.setText("Thumb::Size", number(currentItem.item.size())); 00694 thumb.setText("Thumb::Mimetype", currentItem.item.mimetype()); 00695 QString thumbnailerVersion = currentItem.plugin->property("ThumbnailerVersion", QVariant::String).toString(); 00696 QString signature = QString("KDE Thumbnail Generator "+currentItem.plugin->name()); 00697 if (!thumbnailerVersion.isEmpty()) { 00698 signature.append(" (v"+thumbnailerVersion+')'); 00699 } 00700 thumb.setText("Software", signature); 00701 KTemporaryFile temp; 00702 temp.setPrefix(thumbPath + "kde-tmp-"); 00703 temp.setSuffix(".png"); 00704 temp.setAutoRemove(false); 00705 if (temp.open()) //Only try to write out the thumbnail if we 00706 { //actually created the temp file. 00707 tempFileName = temp.fileName(); 00708 savedCorrectly = thumb.save(tempFileName, "PNG"); 00709 } 00710 } 00711 if(savedCorrectly) 00712 { 00713 Q_ASSERT(!tempFileName.isEmpty()); 00714 KDE::rename(tempFileName, thumbPath + thumbName); 00715 } 00716 emitPreview( thumb ); 00717 succeeded = true; 00718 } 00719 00720 void PreviewJobPrivate::emitPreview(const QImage &thumb) 00721 { 00722 Q_Q(PreviewJob); 00723 QPixmap pix; 00724 if (thumb.width() > width || thumb.height() > height) 00725 pix = QPixmap::fromImage( thumb.scaled(QSize(width, height), Qt::KeepAspectRatio, Qt::SmoothTransformation) ); 00726 else 00727 pix = QPixmap::fromImage( thumb ); 00728 emit q->gotPreview(currentItem.item, pix); 00729 } 00730 00731 QStringList PreviewJob::availablePlugins() 00732 { 00733 QStringList result; 00734 const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator"); 00735 for (KService::List::ConstIterator it = plugins.begin(); it != plugins.end(); ++it) 00736 if (!result.contains((*it)->desktopEntryName())) 00737 result.append((*it)->desktopEntryName()); 00738 return result; 00739 } 00740 00741 QStringList PreviewJob::supportedMimeTypes() 00742 { 00743 QStringList result; 00744 const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator"); 00745 for (KService::List::ConstIterator it = plugins.begin(); it != plugins.end(); ++it) 00746 result += (*it)->serviceTypes(); 00747 return result; 00748 } 00749 00750 #ifndef KDE_NO_DEPRECATED 00751 PreviewJob *KIO::filePreview( const KFileItemList &items, int width, int height, 00752 int iconSize, int iconAlpha, bool scale, bool save, 00753 const QStringList *enabledPlugins ) 00754 { 00755 return new PreviewJob(items, width, height, iconSize, iconAlpha, 00756 scale, save, enabledPlugins); 00757 } 00758 00759 PreviewJob *KIO::filePreview( const KUrl::List &items, int width, int height, 00760 int iconSize, int iconAlpha, bool scale, bool save, 00761 const QStringList *enabledPlugins ) 00762 { 00763 KFileItemList fileItems; 00764 for (KUrl::List::ConstIterator it = items.begin(); it != items.end(); ++it) { 00765 Q_ASSERT( (*it).isValid() ); // please call us with valid urls only 00766 fileItems.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, *it, true)); 00767 } 00768 return new PreviewJob(fileItems, width, height, iconSize, iconAlpha, 00769 scale, save, enabledPlugins); 00770 } 00771 #endif 00772 00773 PreviewJob *KIO::filePreview(const KFileItemList &items, const QSize &size, const QStringList *enabledPlugins) 00774 { 00775 return new PreviewJob(items, size, enabledPlugins); 00776 } 00777 00778 #ifndef KDE_NO_DEPRECATED 00779 KIO::filesize_t PreviewJob::maximumFileSize() 00780 { 00781 KConfigGroup cg( KGlobal::config(), "PreviewSettings" ); 00782 return cg.readEntry( "MaximumSize", 5*1024*1024LL /* 5MB */ ); 00783 } 00784 #endif 00785 00786 #include "previewjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:55:23 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:55:23 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.