• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 
00026 #include <sys/stat.h>
00027 #ifdef __FreeBSD__
00028     #include <machine/param.h>
00029 #endif
00030 #include <sys/types.h>
00031 
00032 #ifdef Q_OS_UNIX
00033 #include <sys/ipc.h>
00034 #include <sys/shm.h>
00035 #endif
00036 
00037 #include <QtCore/QDir>
00038 #include <QtCore/QFile>
00039 #include <QtGui/QImage>
00040 #include <QtCore/QTimer>
00041 #include <QtCore/QRegExp>
00042 
00043 #include <kfileitem.h>
00044 #include <kapplication.h>
00045 #include <kde_file.h>
00046 #include <ktemporaryfile.h>
00047 #include <kservicetypetrader.h>
00048 #include <kcodecs.h>
00049 #include <kglobal.h>
00050 #include <kstandarddirs.h>
00051 #include <kservice.h>
00052 #include <QtCore/QLinkedList>
00053 #include <kconfiggroup.h>
00054 
00055 #include "jobuidelegate.h"
00056 #include "job_p.h"
00057 
00058 namespace KIO { struct PreviewItem; }
00059 using namespace KIO;
00060 
00061 struct KIO::PreviewItem
00062 {
00063     KFileItem item;
00064     KService::Ptr plugin;
00065 };
00066 
00067 class KIO::PreviewJobPrivate: public KIO::JobPrivate
00068 {
00069 public:
00070     enum { STATE_STATORIG, // if the thumbnail exists
00071            STATE_GETORIG, // if we create it
00072            STATE_CREATETHUMB // thumbnail:/ slave
00073     } state;
00074     PreviewJob *q;
00075 
00076     KFileItemList initialItems;
00077     QStringList enabledPlugins;
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     bool succeeded;
00104     // If the file to create a thumb for was a temp file, this is its name
00105     QString tempName;
00106     // Over that, it's too much
00107     KIO::filesize_t maximumSize;
00108     // the size for the icon overlay
00109     int iconSize;
00110     // the transparency of the blended mimetype icon
00111     int iconAlpha;
00112     // Shared memory segment Id. The segment is allocated to a size
00113     // of extent x extent x 4 (32 bit image) on first need.
00114     int shmid;
00115     // And the data area
00116     uchar *shmaddr;
00117     // Root of thumbnail cache
00118     QString thumbRoot;
00119 
00120     void getOrCreateThumbnail();
00121     bool statResultThumbnail();
00122     void createThumbnail( const QString& );
00123     void determineNextFile();
00124     void emitPreview(const QImage &thumb);
00125 
00126     void startPreview();
00127     void slotThumbData(KIO::Job *, const QByteArray &);
00128 
00129     Q_DECLARE_PUBLIC(PreviewJob)
00130 };
00131 
00132 PreviewJob::PreviewJob( const KFileItemList &items, int width, int height,
00133     int iconSize, int iconAlpha, bool scale, bool save,
00134     const QStringList *enabledPlugins )
00135     : KIO::Job(*new PreviewJobPrivate)
00136 {
00137     Q_D(PreviewJob);
00138     d->tOrig = 0;
00139     d->shmid = -1;
00140     d->shmaddr = 0;
00141     d->initialItems = items;
00142     d->enabledPlugins = enabledPlugins ? *enabledPlugins : QStringList();
00143     d->width = width;
00144     d->height = height ? height : width;
00145     d->cacheWidth = d->width;
00146     d->cacheHeight = d->height;
00147     d->iconSize = iconSize;
00148     d->iconAlpha = iconAlpha;
00149     d->bScale = scale;
00150     d->bSave = save && scale;
00151     d->succeeded = false;
00152     d->thumbRoot = QDir::homePath() + "/.thumbnails/";
00153     d->ignoreMaximumSize = false;
00154 
00155     // Return to event loop first, determineNextFile() might delete this;
00156     QTimer::singleShot(0, this, SLOT(startPreview()));
00157 }
00158 
00159 PreviewJob::~PreviewJob()
00160 {
00161 #ifdef Q_OS_UNIX
00162     Q_D(PreviewJob);
00163     if (d->shmaddr) {
00164         shmdt((char*)d->shmaddr);
00165         shmctl(d->shmid, IPC_RMID, 0);
00166     }
00167 #endif
00168 }
00169 
00170 void PreviewJobPrivate::startPreview()
00171 {
00172     Q_Q(PreviewJob);
00173     // Load the list of plugins to determine which mimetypes are supported
00174     const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator");
00175     QMap<QString, KService::Ptr> mimeMap;
00176 
00177     for (KService::List::ConstIterator it = plugins.constBegin(); it != plugins.constEnd(); ++it) {
00178         if (enabledPlugins.isEmpty() || enabledPlugins.contains((*it)->desktopEntryName()))
00179     {
00180         const QStringList mimeTypes = (*it)->serviceTypes();
00181         for (QStringList::ConstIterator mt = mimeTypes.constBegin(); mt != mimeTypes.constEnd(); ++mt)
00182             mimeMap.insert(*mt, *it);
00183     }
00184     }
00185 
00186     // Look for images and store the items in our todo list :)
00187     bool bNeedCache = false;
00188     KFileItemList::const_iterator kit = initialItems.constBegin();
00189     const KFileItemList::const_iterator kend = initialItems.constEnd();
00190     for ( ; kit != kend; ++kit )
00191     {
00192         PreviewItem item;
00193         item.item = *kit;
00194         const QString mimeType = item.item.mimetype();
00195         QMap<QString, KService::Ptr>::ConstIterator plugin = mimeMap.constFind(mimeType);
00196         if (plugin == mimeMap.constEnd())
00197 
00198         {
00199             QString groupMimeType = mimeType;
00200             groupMimeType.replace(QRegExp("/.*"), "/*");
00201             plugin = mimeMap.constFind(groupMimeType);
00202 
00203             if (plugin == mimeMap.constEnd())
00204             {
00205                 // check mime type inheritance, resolve aliases
00206                 const KMimeType::Ptr mimeInfo = KMimeType::mimeType(mimeType, KMimeType::ResolveAliases);
00207                 if (mimeInfo) {
00208                     const QStringList parentMimeTypes = mimeInfo->allParentMimeTypes();
00209                     Q_FOREACH(const QString& parentMimeType, parentMimeTypes) {
00210                         plugin = mimeMap.constFind(parentMimeType);
00211                         if (plugin != mimeMap.constEnd()) break;
00212                     }
00213                 }
00214             }
00215 #if 0 // KDE4: should be covered by inheritance above, all text mimetypes inherit from text/plain
00216             // if that's not enough, we need to invent something else
00217             if (plugin == mimeMap.end())
00218             {
00219                 // check X-KDE-Text property
00220                 KMimeType::Ptr mimeInfo = KMimeType::mimeType(mimeType);
00221                 QVariant textProperty = mimeInfo->property("X-KDE-text");
00222                 if (textProperty.isValid() && textProperty.type() == QVariant::Bool)
00223                 {
00224                     if (textProperty.toBool())
00225                     {
00226                         plugin = mimeMap.find("text/plain");
00227                         if (plugin == mimeMap.end())
00228                         {
00229                             plugin = mimeMap.find( "text/*" );
00230                         }
00231                     }
00232                 }
00233             }
00234 #endif
00235         }
00236 
00237         if (plugin != mimeMap.constEnd())
00238         {
00239             item.plugin = *plugin;
00240             items.append(item);
00241             if (!bNeedCache && bSave &&
00242                 ((*kit).url().protocol() != "file" ||
00243                  !(*kit).url().directory( KUrl::AppendTrailingSlash ).startsWith(thumbRoot)) &&
00244                 (*plugin)->property("CacheThumbnail").toBool())
00245                 bNeedCache = true;
00246         }
00247         else
00248         {
00249             emit q->failed( *kit );
00250         }
00251     }
00252 
00253   // Read configuration value for the maximum allowed size
00254     maximumSize = PreviewJob::maximumFileSize();
00255 
00256     if (bNeedCache)
00257     {
00258         if (width <= 128 && height <= 128) cacheWidth = cacheHeight = 128;
00259         else cacheWidth = cacheHeight = 256;
00260         thumbPath = thumbRoot + (cacheWidth == 128 ? "normal/" : "large/");
00261         KStandardDirs::makeDir(thumbPath, 0700);
00262     }
00263     else
00264         bSave = false;
00265 
00266     initialItems.clear();
00267     determineNextFile();
00268 }
00269 
00270 void PreviewJob::removeItem( const KUrl& url )
00271 {
00272     Q_D(PreviewJob);
00273     for (QLinkedList<PreviewItem>::Iterator it = d->items.begin(); it != d->items.end(); ++it)
00274         if ((*it).item.url() == url)
00275         {
00276             d->items.erase(it);
00277             break;
00278         }
00279 
00280     if (d->currentItem.item.url() == url)
00281     {
00282         KJob* job = subjobs().first();
00283         job->kill();
00284         removeSubjob( job );
00285         d->determineNextFile();
00286     }
00287 }
00288 
00289 void PreviewJob::setIgnoreMaximumSize(bool ignoreSize)
00290 {
00291     d_func()->ignoreMaximumSize = ignoreSize;
00292 }
00293 
00294 void PreviewJobPrivate::determineNextFile()
00295 {
00296     Q_Q(PreviewJob);
00297     if (!currentItem.item.isNull())
00298     {
00299         if (!succeeded)
00300             emit q->failed( currentItem.item );
00301     }
00302     // No more items ?
00303     if ( items.isEmpty() )
00304     {
00305         q->emitResult();
00306         return;
00307     }
00308     else
00309     {
00310         // First, stat the orig file
00311         state = PreviewJobPrivate::STATE_STATORIG;
00312         currentItem = items.first();
00313         succeeded = false;
00314         items.removeFirst();
00315         KIO::Job *job = KIO::stat( currentItem.item.url(), KIO::HideProgressInfo );
00316         job->addMetaData( "no-auth-prompt", "true" );
00317         q->addSubjob(job);
00318     }
00319 }
00320 
00321 void PreviewJob::slotResult( KJob *job )
00322 {
00323     Q_D(PreviewJob);
00324 
00325     removeSubjob(job);
00326     Q_ASSERT ( !hasSubjobs() ); // We should have only one job at a time ...
00327     switch ( d->state )
00328     {
00329         case PreviewJobPrivate::STATE_STATORIG:
00330         {
00331             if (job->error()) // that's no good news...
00332             {
00333                 // Drop this one and move on to the next one
00334                 d->determineNextFile();
00335                 return;
00336             }
00337             const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
00338             d->tOrig = entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, 0 );
00339             if ( !d->ignoreMaximumSize &&
00340                  (KIO::filesize_t)entry.numberValue( KIO::UDSEntry::UDS_SIZE, 0 ) > d->maximumSize &&
00341                  !d->currentItem.plugin->property("IgnoreMaximumSize").toBool()
00342                 ) {
00343                 d->determineNextFile();
00344                 return;
00345             }
00346 
00347             if ( !d->currentItem.plugin->property( "CacheThumbnail" ).toBool() )
00348             {
00349                 // This preview will not be cached, no need to look for a saved thumbnail
00350                 // Just create it, and be done
00351                 d->getOrCreateThumbnail();
00352                 return;
00353             }
00354 
00355             if ( d->statResultThumbnail() )
00356                 return;
00357 
00358             d->getOrCreateThumbnail();
00359             return;
00360         }
00361         case PreviewJobPrivate::STATE_GETORIG:
00362         {
00363             if (job->error())
00364             {
00365                 d->determineNextFile();
00366                 return;
00367             }
00368 
00369             d->createThumbnail( static_cast<KIO::FileCopyJob*>(job)->destUrl().path() );
00370             return;
00371         }
00372         case PreviewJobPrivate::STATE_CREATETHUMB:
00373         {
00374             if (!d->tempName.isEmpty())
00375             {
00376                 QFile::remove(d->tempName);
00377                 d->tempName.clear();
00378             }
00379             d->determineNextFile();
00380             return;
00381         }
00382     }
00383 }
00384 
00385 bool PreviewJobPrivate::statResultThumbnail()
00386 {
00387     if ( thumbPath.isEmpty() )
00388         return false;
00389 
00390     KUrl url = currentItem.item.url();
00391     // Don't include the password if any
00392     url.setPass(QString());
00393     origName = url.url();
00394 
00395     KMD5 md5( QFile::encodeName( origName ) );
00396     thumbName = QFile::encodeName( md5.hexDigest() ) + ".png";
00397 
00398     QImage thumb;
00399     if ( !thumb.load( thumbPath + thumbName ) ) return false;
00400 
00401     if ( thumb.text( "Thumb::URI", 0 ) != origName ||
00402          thumb.text( "Thumb::MTime", 0 ).toInt() != tOrig ) return false;
00403 
00404     // Found it, use it
00405     emitPreview( thumb );
00406     succeeded = true;
00407     determineNextFile();
00408     return true;
00409 }
00410 
00411 
00412 void PreviewJobPrivate::getOrCreateThumbnail()
00413 {
00414     Q_Q(PreviewJob);
00415     // We still need to load the orig file ! (This is getting tedious) :)
00416     const KFileItem& item = currentItem.item;
00417     const QString localPath = item.localPath();
00418     if ( !localPath.isEmpty() )
00419         createThumbnail( localPath );
00420     else
00421     {
00422         state = PreviewJobPrivate::STATE_GETORIG;
00423         KTemporaryFile localFile;
00424         localFile.setAutoRemove(false);
00425         localFile.open();
00426         KUrl localURL;
00427         localURL.setPath( tempName = localFile.fileName() );
00428         const KUrl currentURL = item.url();
00429         KIO::Job * job = KIO::file_copy( currentURL, localURL, -1, KIO::Overwrite | KIO::HideProgressInfo /* No GUI */ );
00430         job->addMetaData("thumbnail","1");
00431         q->addSubjob(job);
00432     }
00433 }
00434 
00435 void PreviewJobPrivate::createThumbnail( const QString &pixPath )
00436 {
00437     Q_Q(PreviewJob);
00438     state = PreviewJobPrivate::STATE_CREATETHUMB;
00439     KUrl thumbURL;
00440     thumbURL.setProtocol("thumbnail");
00441     thumbURL.setPath(pixPath);
00442     KIO::TransferJob *job = KIO::get(thumbURL, NoReload, HideProgressInfo);
00443     q->addSubjob(job);
00444     q->connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), SLOT(slotThumbData(KIO::Job *, const QByteArray &)));
00445     bool save = bSave && currentItem.plugin->property("CacheThumbnail").toBool();
00446     job->addMetaData("mimeType", currentItem.item.mimetype());
00447     job->addMetaData("width", QString().setNum(save ? cacheWidth : width));
00448     job->addMetaData("height", QString().setNum(save ? cacheHeight : height));
00449     job->addMetaData("iconSize", QString().setNum(save ? 64 : iconSize));
00450     job->addMetaData("iconAlpha", QString().setNum(iconAlpha));
00451     job->addMetaData("plugin", currentItem.plugin->library());
00452 #ifdef Q_OS_UNIX
00453     if (shmid == -1)
00454     {
00455         if (shmaddr) {
00456             shmdt((char*)shmaddr);
00457             shmctl(shmid, IPC_RMID, 0);
00458         }
00459         shmid = shmget(IPC_PRIVATE, cacheWidth * cacheHeight * 4, IPC_CREAT|0600);
00460         if (shmid != -1)
00461         {
00462             shmaddr = (uchar *)(shmat(shmid, 0, SHM_RDONLY));
00463             if (shmaddr == (uchar *)-1)
00464             {
00465                 shmctl(shmid, IPC_RMID, 0);
00466                 shmaddr = 0;
00467                 shmid = -1;
00468             }
00469         }
00470         else
00471             shmaddr = 0;
00472     }
00473     if (shmid != -1)
00474         job->addMetaData("shmid", QString().setNum(shmid));
00475 #endif
00476 }
00477 
00478 void PreviewJobPrivate::slotThumbData(KIO::Job *, const QByteArray &data)
00479 {
00480     bool save = bSave &&
00481                 currentItem.plugin->property("CacheThumbnail").toBool() &&
00482                 (currentItem.item.url().protocol() != "file" ||
00483                  !currentItem.item.url().directory( KUrl::AppendTrailingSlash ).startsWith(thumbRoot));
00484     QImage thumb;
00485 #ifdef Q_OS_UNIX
00486     if (shmaddr)
00487     {
00488         // Keep this in sync with kdebase/kioslave/thumbnail.cpp
00489         QDataStream str(data);
00490         int width, height;
00491         quint8 iFormat;
00492         str >> width >> height >> iFormat;
00493         QImage::Format format = static_cast<QImage::Format>( iFormat );
00494         thumb = QImage(shmaddr, width, height, format );
00495     }
00496     else
00497 #endif
00498         thumb.loadFromData(data);
00499 
00500     if (thumb.isNull()) {
00501         QDataStream s(data);
00502         s >> thumb;
00503     }
00504 
00505     if (save)
00506     {
00507         thumb.setText("Thumb::URI", origName);
00508         thumb.setText("Thumb::MTime", QString::number(tOrig));
00509         thumb.setText("Thumb::Size", number(currentItem.item.size()));
00510         thumb.setText("Thumb::Mimetype", currentItem.item.mimetype());
00511         thumb.setText("Software", "KDE Thumbnail Generator");
00512         KTemporaryFile temp;
00513         temp.setPrefix(thumbPath + "kde-tmp-");
00514         temp.setSuffix(".png");
00515         temp.setAutoRemove(false);
00516         if (temp.open()) //Only try to write out the thumbnail if we
00517         {                //actually created the temp file.
00518             thumb.save(temp.fileName(), "PNG");
00519             KDE_rename(QFile::encodeName(temp.fileName()), QFile::encodeName(thumbPath + thumbName));
00520         }
00521     }
00522     emitPreview( thumb );
00523     succeeded = true;
00524 }
00525 
00526 void PreviewJobPrivate::emitPreview(const QImage &thumb)
00527 {
00528     Q_Q(PreviewJob);
00529     QPixmap pix;
00530     if (thumb.width() > width || thumb.height() > height)
00531         pix = QPixmap::fromImage( thumb.scaled(QSize(width, height), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
00532     else
00533         pix = QPixmap::fromImage( thumb );
00534     emit q->gotPreview(currentItem.item, pix);
00535 }
00536 
00537 QStringList PreviewJob::availablePlugins()
00538 {
00539     QStringList result;
00540     const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator");
00541     for (KService::List::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
00542         if (!result.contains((*it)->desktopEntryName()))
00543             result.append((*it)->desktopEntryName());
00544     return result;
00545 }
00546 
00547 QStringList PreviewJob::supportedMimeTypes()
00548 {
00549     QStringList result;
00550     const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator");
00551     for (KService::List::ConstIterator it = plugins.begin(); it != plugins.end(); ++it)
00552         result += (*it)->serviceTypes();
00553     return result;
00554 }
00555 
00556 PreviewJob *KIO::filePreview( const KFileItemList &items, int width, int height,
00557     int iconSize, int iconAlpha, bool scale, bool save,
00558     const QStringList *enabledPlugins )
00559 {
00560     return new PreviewJob(items, width, height, iconSize, iconAlpha,
00561                           scale, save, enabledPlugins);
00562 }
00563 
00564 PreviewJob *KIO::filePreview( const KUrl::List &items, int width, int height,
00565     int iconSize, int iconAlpha, bool scale, bool save,
00566     const QStringList *enabledPlugins )
00567 {
00568     KFileItemList fileItems;
00569     for (KUrl::List::ConstIterator it = items.begin(); it != items.end(); ++it) {
00570         Q_ASSERT( (*it).isValid() ); // please call us with valid urls only
00571         fileItems.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, *it, true));
00572     }
00573     return new PreviewJob(fileItems, width, height, iconSize, iconAlpha,
00574                           scale, save, enabledPlugins);
00575 }
00576 
00577 KIO::filesize_t PreviewJob::maximumFileSize()
00578 {
00579     KConfigGroup cg( KGlobal::config(), "PreviewSettings" );
00580     return cg.readEntry( "MaximumSize", 5*1024*1024LL /* 5MB */ );
00581 }
00582 
00583 #include "previewjob.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal