Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kioslave / thumbnail / thumbnail.cpp
blobc704f55d682be9996ff959e1b2ae56bfac3410a1
1 /* This file is part of the KDE libraries
2 Copyright (C) 2000 Malte Starostik <malte@kde.org>
3 2000 Carsten Pfeiffer <pfeiffer@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include <stdlib.h>
22 #include <unistd.h>
23 #ifdef __FreeBSD__
24 #include <machine/param.h>
25 #endif
26 #include <sys/types.h>
27 #include <sys/ipc.h>
28 #include <sys/shm.h>
30 #include <QtCore/QBuffer>
31 #include <QtCore/QFile>
32 #include <QtGui/QBitmap>
33 #include <QtGui/QImage>
34 #include <QtGui/QPainter>
35 #include <QtGui/QPixmap>
37 #include <kurl.h>
38 #include <kapplication.h>
39 #include <kcmdlineargs.h>
40 #include <kaboutdata.h>
41 #include <kglobal.h>
42 #include <kiconloader.h>
43 #include <kmimetype.h>
44 #include <klibloader.h>
45 #include <kdebug.h>
46 #include <kservice.h>
47 #include <kservicetype.h>
48 #include <kservicetypetrader.h>
49 #include <kmimetypetrader.h>
50 #include <kfilemetainfo.h>
51 #include <klocale.h>
53 #include <config-runtime.h> // For HAVE_NICE
54 #include "thumbnail.h"
55 #include <kio/thumbcreator.h>
56 #include <kconfiggroup.h>
58 // Use correctly KComponentData instead of KApplication (but then no QPixmap)
59 #undef USE_KINSTANCE
60 // Fix thumbnail: protocol
61 #define THUMBNAIL_HACK (1)
63 #ifdef THUMBNAIL_HACK
64 # include <QFileInfo>
65 #endif
67 // Recognized metadata entries:
68 // mimeType - the mime type of the file, used for the overlay icon if any
69 // width - maximum width for the thumbnail
70 // height - maximum height for the thumbnail
71 // iconSize - the size of the overlay icon to use if any
72 // iconAlpha - the transparency value used for icon overlays
73 // plugin - the name of the plugin library to be used for thumbnail creation.
74 // Provided by the application to save an addition KTrader
75 // query here.
76 // shmid - the shared memory segment id to write the image's data to.
77 // The segment is assumed to provide enough space for a 32-bit
78 // image sized width x height pixels.
79 // If this is given, the data returned by the slave will be:
80 // int width
81 // int height
82 // int depth
83 // Otherwise, the data returned is the image in PNG format.
85 using namespace KIO;
87 extern "C"
89 KDE_EXPORT int kdemain(int argc, char **argv);
93 int kdemain(int argc, char **argv)
95 #ifdef HAVE_NICE
96 nice( 5 );
97 #endif
99 #ifdef USE_KINSTANCE
100 KComponentData componentData("kio_thumbnail");
101 #else
102 // creating KApplication in a slave in not a very good idea,
103 // as dispatchLoop() doesn't allow it to process its messages,
104 // so it for example wouldn't reply to ksmserver - on the other
105 // hand, this slave uses QPixmaps for some reason, and they
106 // need QApplication
107 // and HTML previews need even KApplication :(
108 putenv(strdup("SESSION_MANAGER="));
109 //KApplication::disableAutoDcopRegistration();
110 KAboutData about("kio_thumbnail", 0, ki18n("kio_thumbmail"), "KDE 4.x.x");
111 KCmdLineArgs::init(&about);
113 KApplication app( true);
114 #endif
116 if (argc != 4)
118 kError(7115) << "Usage: kio_thumbnail protocol domain-socket1 domain-socket2" << endl;
119 exit(-1);
122 ThumbnailProtocol slave(argv[2], argv[3]);
123 slave.dispatchLoop();
125 return 0;
128 ThumbnailProtocol::ThumbnailProtocol(const QByteArray &pool, const QByteArray &app)
129 : SlaveBase("thumbnail", pool, app)
131 m_iconSize = 0;
134 ThumbnailProtocol::~ThumbnailProtocol()
136 qDeleteAll( m_creators );
137 m_creators.clear();
140 void ThumbnailProtocol::get(const KUrl &url)
142 m_mimeType = metaData("mimeType");
143 kDebug(7115) << "Wanting MIME Type:" << m_mimeType;
144 #ifdef THUMBNAIL_HACK
145 // ### HACK
146 bool direct=false;
147 if (m_mimeType.isEmpty())
149 kDebug(7115) << "PATH: " << url.path();
150 QFileInfo info(url.path());
151 if (info.isDir())
153 // We cannot process a directory
154 error(KIO::ERR_IS_DIRECTORY,url.path());
155 return;
157 else if (!info.exists())
159 // The file does not exist
160 error(KIO::ERR_DOES_NOT_EXIST,url.path());
161 return;
163 else if (!info.isReadable())
165 // The file is not readable!
166 error(KIO::ERR_COULD_NOT_READ,url.path());
167 return;
169 m_mimeType = KMimeType::findByUrl(url)->name();
170 kDebug(7115) << "Guessing MIME Type:" << m_mimeType;
171 direct=true; // thumbnail: was probably called from Konqueror
173 #endif
175 if (m_mimeType.isEmpty())
177 error(KIO::ERR_INTERNAL, i18n("No MIME Type specified."));
178 return;
181 m_width = metaData("width").toInt();
182 m_height = metaData("height").toInt();
183 int iconSize = metaData("iconSize").toInt();
185 if (m_width < 0 || m_height < 0)
187 error(KIO::ERR_INTERNAL, i18n("No or invalid size specified."));
188 return;
190 #ifdef THUMBNAIL_HACK
191 else if (!m_width || !m_height)
193 kDebug(7115) << "Guessing height, width, icon size!";
194 m_width=128;
195 m_height=128;
196 iconSize=128;
198 #endif
200 if (!iconSize)
201 iconSize = KIconLoader::global()->currentSize(KIconLoader::Desktop);
202 if (iconSize != m_iconSize) {
203 m_iconDict.clear();
205 m_iconSize = iconSize;
207 m_iconAlpha = metaData("iconAlpha").toInt();
209 QImage img;
211 KConfigGroup group( KGlobal::config(), "PreviewSettings" );
214 // ### KFMI
215 bool kfmiThumb = false;
216 if (group.readEntry( "UseFileThumbnails", true)) {
217 KService::Ptr service =
218 KMimeTypeTrader::self()->preferredService( m_mimeType, "KFilePlugin");
220 if ( service && service->isValid() && /*url.isLocalFile() && */
221 service->property("SupportsThumbnail").toBool())
223 KFileMetaInfo info(url.path(), m_mimeType, KFileMetaInfo::Thumbnail);
224 if (info.isValid())
226 KFileMetaInfoItem item = info.item("thumbnail");
227 if (item.isValid() && item.value().type() == QVariant::Image)
229 img = item.value().value<QImage>();
230 kDebug(7115) << "using KFMI for the thumbnail\n";
231 kfmiThumb = true;
236 ThumbCreator::Flags flags = ThumbCreator::None;
238 if (!kfmiThumb)
240 kDebug(7115) << "using thumb creator for the thumbnail\n";
241 QString plugin = metaData("plugin");
242 #ifdef THUMBNAIL_HACK
243 if (plugin.isEmpty())
245 KService::List offers = KMimeTypeTrader::self()->query( m_mimeType, QLatin1String( "ThumbCreator" ) );
247 if(!offers.isEmpty())
249 KService::Ptr serv;
250 serv = offers.first();
251 plugin = serv->library();
254 kDebug(7115) << "Guess plugin: " << plugin;
255 #endif
256 if (plugin.isEmpty())
258 error(KIO::ERR_INTERNAL, i18n("No plugin specified."));
259 return;
262 ThumbCreator *creator = m_creators[plugin];
263 if (!creator)
265 // Don't use KLibFactory here, this is not a QObject and
266 // neither is ThumbCreator
267 KLibrary *library = KLibLoader::self()->library(plugin);
268 if (library)
270 newCreator create = (newCreator)library->resolveFunction("new_creator");
271 if (create)
272 creator = create();
274 if (!creator)
276 error(KIO::ERR_INTERNAL, i18n("Cannot load ThumbCreator %1", plugin));
277 return;
279 m_creators.insert(plugin, creator);
282 if (!creator->create(url.path(), m_width, m_height, img))
284 error(KIO::ERR_INTERNAL, i18n("Cannot create thumbnail for %1", url.path()));
285 return;
287 flags = creator->flags();
290 if (img.width() > m_width || img.height() > m_height)
292 double imgRatio = (double)img.height() / (double)img.width();
293 if (imgRatio > (double)m_height / (double)m_width)
294 img = img.scaled( int(qMax((double)m_height / imgRatio, 1.0)), m_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
295 else
296 img = img.scaled(m_width, int(qMax((double)m_width * imgRatio, 1.0)), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
299 // ### FIXME
300 #ifndef USE_KINSTANCE
301 if (flags & ThumbCreator::DrawFrame)
303 QPixmap pix = QPixmap::fromImage( img );
304 int x2 = pix.width() - 1;
305 int y2 = pix.height() - 1;
306 // paint a black rectangle around the "page"
307 QPainter p;
308 p.begin( &pix );
309 p.setPen( QColor( 48, 48, 48 ));
310 p.drawLine( x2, 0, x2, y2 );
311 p.drawLine( 0, y2, x2, y2 );
312 p.setPen( QColor( 215, 215, 215 ));
313 p.drawLine( 0, 0, x2, 0 );
314 p.drawLine( 0, 0, 0, y2 );
315 p.end();
317 const QBitmap& mask = pix.mask();
318 if ( !mask.isNull() ) // need to update it so we can see the frame
320 QBitmap bitmap( mask );
321 QPainter painter;
322 painter.begin( &bitmap );
323 painter.drawLine( x2, 0, x2, y2 );
324 painter.drawLine( 0, y2, x2, y2 );
325 painter.drawLine( 0, 0, x2, 0 );
326 painter.drawLine( 0, 0, 0, y2 );
327 painter.end();
329 pix.setMask( bitmap );
332 img = pix.toImage();
334 #endif
336 if ((flags & ThumbCreator::BlendIcon) && KIconLoader::global()->alphaBlending(KIconLoader::Desktop))
338 // blending the mimetype icon in
339 QImage icon = getIcon();
341 int x = img.width() - icon.width() - 4;
342 x = qMax( x, 0 );
343 int y = img.height() - icon.height() - 6;
344 y = qMax( y, 0 );
345 QPainter p(&img);
346 p.setOpacity(m_iconAlpha/255.0);
347 p.drawImage(x, y, icon);
350 if (img.isNull())
352 error(KIO::ERR_INTERNAL, i18n("Failed to create a thumbnail."));
353 return;
356 const QString shmid = metaData("shmid");
357 if (shmid.isEmpty())
359 #ifdef THUMBNAIL_HACK
360 if (direct)
362 // If thumbnail was called directly from Konqueror, then the image needs to be raw
363 //kDebug(7115) << "RAW IMAGE TO STREAM";
364 QBuffer buf;
365 if (!buf.open(QIODevice::WriteOnly))
367 error(KIO::ERR_INTERNAL, i18n("Could not write image."));
368 return;
370 img.save(&buf,"PNG");
371 buf.close();
372 data(buf.buffer());
374 else
375 #endif
377 QByteArray imgData;
378 QDataStream stream( &imgData, QIODevice::WriteOnly );
379 //kDebug(7115) << "IMAGE TO STREAM";
380 stream << img;
381 data(imgData);
384 else
386 QByteArray imgData;
387 QDataStream stream( &imgData, QIODevice::WriteOnly );
388 //kDebug(7115) << "IMAGE TO SHMID";
389 void *shmaddr = shmat(shmid.toInt(), 0, 0);
390 if (shmaddr == (void *)-1)
392 error(KIO::ERR_INTERNAL, i18n("Failed to attach to shared memory segment %1", shmid));
393 return;
395 if (img.width() * img.height() > m_width * m_height)
397 error(KIO::ERR_INTERNAL, i18n("Image is too big for the shared memory segment"));
398 shmdt((char*)shmaddr);
399 return;
401 if( img.depth() != 32 ) // KIO::PreviewJob and this code below completely ignores colortable :-/,
402 img = img.convertToFormat(QImage::Format_ARGB32); // so make sure there is none
403 // Keep in sync with kdelibs/kio/kio/previewjob.cpp
404 stream << img.width() << img.height() << quint8(img.format());
405 memcpy(shmaddr, img.bits(), img.numBytes());
406 shmdt((char*)shmaddr);
407 data(imgData);
409 finished();
412 const QImage ThumbnailProtocol::getIcon()
414 if ( !m_iconDict.contains(m_mimeType) ) { // generate it
415 QImage icon( KIconLoader::global()->loadMimeTypeIcon( KMimeType::mimeType(m_mimeType)->iconName(), KIconLoader::Desktop, m_iconSize ).toImage() );
416 icon.setAlphaBuffer( true );
417 m_iconDict.insert( m_mimeType, icon );
419 return icon;
422 return m_iconDict.value( m_mimeType );