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.
24 #include <machine/param.h>
26 #include <sys/types.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>
38 #include <kapplication.h>
39 #include <kcmdlineargs.h>
40 #include <kaboutdata.h>
42 #include <kiconloader.h>
43 #include <kmimetype.h>
44 #include <klibloader.h>
47 #include <kservicetype.h>
48 #include <kservicetypetrader.h>
49 #include <kmimetypetrader.h>
50 #include <kfilemetainfo.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)
60 // Fix thumbnail: protocol
61 #define THUMBNAIL_HACK (1)
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
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:
83 // Otherwise, the data returned is the image in PNG format.
89 KDE_EXPORT
int kdemain(int argc
, char **argv
);
93 int kdemain(int argc
, char **argv
)
100 KComponentData
componentData("kio_thumbnail");
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
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);
118 kError(7115) << "Usage: kio_thumbnail protocol domain-socket1 domain-socket2" << endl
;
122 ThumbnailProtocol
slave(argv
[2], argv
[3]);
123 slave
.dispatchLoop();
128 ThumbnailProtocol::ThumbnailProtocol(const QByteArray
&pool
, const QByteArray
&app
)
129 : SlaveBase("thumbnail", pool
, app
)
134 ThumbnailProtocol::~ThumbnailProtocol()
136 qDeleteAll( m_creators
);
140 void ThumbnailProtocol::get(const KUrl
&url
)
142 m_mimeType
= metaData("mimeType");
143 kDebug(7115) << "Wanting MIME Type:" << m_mimeType
;
144 #ifdef THUMBNAIL_HACK
147 if (m_mimeType
.isEmpty())
149 kDebug(7115) << "PATH: " << url
.path();
150 QFileInfo
info(url
.path());
153 // We cannot process a directory
154 error(KIO::ERR_IS_DIRECTORY
,url
.path());
157 else if (!info
.exists())
159 // The file does not exist
160 error(KIO::ERR_DOES_NOT_EXIST
,url
.path());
163 else if (!info
.isReadable())
165 // The file is not readable!
166 error(KIO::ERR_COULD_NOT_READ
,url
.path());
169 m_mimeType
= KMimeType::findByUrl(url
)->name();
170 kDebug(7115) << "Guessing MIME Type:" << m_mimeType
;
171 direct
=true; // thumbnail: was probably called from Konqueror
175 if (m_mimeType
.isEmpty())
177 error(KIO::ERR_INTERNAL
, i18n("No MIME Type specified."));
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."));
190 #ifdef THUMBNAIL_HACK
191 else if (!m_width
|| !m_height
)
193 kDebug(7115) << "Guessing height, width, icon size!";
201 iconSize
= KIconLoader::global()->currentSize(KIconLoader::Desktop
);
202 if (iconSize
!= m_iconSize
) {
205 m_iconSize
= iconSize
;
207 m_iconAlpha
= metaData("iconAlpha").toInt();
211 KConfigGroup
group( KGlobal::config(), "PreviewSettings" );
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
);
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";
236 ThumbCreator::Flags flags
= ThumbCreator::None
;
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())
250 serv
= offers
.first();
251 plugin
= serv
->library();
254 kDebug(7115) << "Guess plugin: " << plugin
;
256 if (plugin
.isEmpty())
258 error(KIO::ERR_INTERNAL
, i18n("No plugin specified."));
262 ThumbCreator
*creator
= m_creators
[plugin
];
265 // Don't use KLibFactory here, this is not a QObject and
266 // neither is ThumbCreator
267 KLibrary
*library
= KLibLoader::self()->library(plugin
);
270 newCreator create
= (newCreator
)library
->resolveFunction("new_creator");
276 error(KIO::ERR_INTERNAL
, i18n("Cannot load ThumbCreator %1", plugin
));
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()));
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
);
296 img
= img
.scaled(m_width
, int(qMax((double)m_width
* imgRatio
, 1.0)), Qt::IgnoreAspectRatio
, Qt::SmoothTransformation
);
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"
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
);
317 const QBitmap
& mask
= pix
.mask();
318 if ( !mask
.isNull() ) // need to update it so we can see the frame
320 QBitmap
bitmap( mask
);
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
);
329 pix
.setMask( bitmap
);
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;
343 int y
= img
.height() - icon
.height() - 6;
346 p
.setOpacity(m_iconAlpha
/255.0);
347 p
.drawImage(x
, y
, icon
);
352 error(KIO::ERR_INTERNAL
, i18n("Failed to create a thumbnail."));
356 const QString shmid
= metaData("shmid");
359 #ifdef THUMBNAIL_HACK
362 // If thumbnail was called directly from Konqueror, then the image needs to be raw
363 //kDebug(7115) << "RAW IMAGE TO STREAM";
365 if (!buf
.open(QIODevice::WriteOnly
))
367 error(KIO::ERR_INTERNAL
, i18n("Could not write image."));
370 img
.save(&buf
,"PNG");
378 QDataStream
stream( &imgData
, QIODevice::WriteOnly
);
379 //kDebug(7115) << "IMAGE TO STREAM";
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
));
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
);
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
);
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
);
422 return m_iconDict
.value( m_mimeType
);