diff --git a/kioslave/thumbnail/CMakeLists.txt b/kioslave/thumbnail/CMakeLists.txt index b20c41b..8b079d3 100644 --- a/kioslave/thumbnail/CMakeLists.txt +++ b/kioslave/thumbnail/CMakeLists.txt @@ -154,6 +154,20 @@ if (UNIX) endif (UNIX) install(TARGETS comicbookthumbnail DESTINATION ${PLUGIN_INSTALL_DIR}) +########### next target ############### + +set(webthumbnail_SRCS webcreator.cpp) +kde4_add_plugin(webthumbnail ${webthumbnail_SRCS}) +target_link_libraries(webthumbnail + ${KDE4_KIO_LIBS} + ${QT_QTWEBKIT_LIBRARY} + ${KDE4_KDEWEBKIT_LIBRARY} +) +#if (UNIX) +# target_link_libraries(webthumbnail ${KDE4_KPTY_LIBRARY}) +#endif (UNIX) +install(TARGETS webthumbnail DESTINATION ${PLUGIN_INSTALL_DIR}) + ########### install files ############### install(FILES thumbcreator.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR}) @@ -167,4 +181,5 @@ install(FILES djvuthumbnail.desktop desktopthumbnail.desktop comicbookthumbnail.desktop + webthumbnail.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/kioslave/thumbnail/thumbcreator.desktop b/kioslave/thumbnail/thumbcreator.desktop index 5fc7672..a8d7622 100644 --- a/kioslave/thumbnail/thumbcreator.desktop +++ b/kioslave/thumbnail/thumbcreator.desktop @@ -95,3 +95,6 @@ Type=bool [PropertyDef::IgnoreMaximumSize] Type=bool + +[PropertyDef::X-KDE-Protocol] +Type=QStringList \ No newline at end of file diff --git a/kioslave/thumbnail/webcreator.cpp b/kioslave/thumbnail/webcreator.cpp new file mode 100644 index 0000000..cc76817 --- /dev/null +++ b/kioslave/thumbnail/webcreator.cpp @@ -0,0 +1,202 @@ +/* + Copyright 2011 Sebastian Kügler + Copyright 2010 Richard Moore + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "webcreator.h" + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +/* +class WebCreatorPrivate +{ +public: + WebCreatorPrivate() : + page(0) + { + kDebug() << " === PRIVATE"; + } + + QWebPage *page; + QImage thumbnail; + QSize size; + QUrl url; + QEventLoop eventLoop; + +}; +*/ +extern "C" +{ + KDE_EXPORT ThumbCreator *new_creator() + { + return new WebCreator; + } +} + +WebCreator::WebCreator() + : m_page(0) +{ +} + +WebCreator::~WebCreator() +{ + delete m_page; +} + +bool WebCreator::create(const QString &path, int width, int height, QImage &img) +{ + kDebug() << "WEBCREATOR URL: " << path << width << "x" << height; + if (!m_page) { + //m_url = QUrl("http://lwn.net"); + m_url = QUrl(path); + kDebug() << " === NEW PAGE"; + m_page = new KWebPage(this); + kDebug() << " === SIZE"; + m_page->setViewportSize(QSize(width, height)); + m_page->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff ); + m_page->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff ); + qreal zoomFactor = (qreal)width / 800.0; // assume pages render well enough at 600px width + kDebug() << "ZoOOOMFACTOR: " << zoomFactor; + m_page->mainFrame()->setZoomFactor(zoomFactor); + kDebug() << " === LOAD: " << m_url; + m_page->mainFrame()->load( m_url ); + connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(completed(bool))); + + /* + m_html = new KHTMLPart; + connect(m_html, SIGNAL(completed()), SLOT(slotCompleted())); + m_html->setJScriptEnabled(false); + m_html->setJavaEnabled(false); + m_html->setPluginsEnabled(false); + m_html->setMetaRefreshEnabled(false); + m_html->setOnlyLocalReferences(true); + */ + } + + // 30 sec timeout + int t = startTimer(30000); + m_failed = false; + + // The event loop will either be terminated by the page loadFinished(), + // or by the timeout + m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents); + + killTimer(t); + + if (m_failed) { + return false; + } + + // Render QImage from WebPage + QPixmap pix(width, height); + pix.fill( Qt::transparent ); + + kDebug() << " ++++++++++++++ NOW PAINTING ++++++++++++" << m_page->mainFrame()->contentsSize() << pix.size(); + // render and rescale + QPainter p; + p.begin(&pix); + + m_page->setViewportSize(m_page->mainFrame()->contentsSize()); + m_page->mainFrame()->render(&p); + + //p.setPen(Qt::blue); + //p.setFont(QFont("Arial", 30)); + //p.drawText(QRect(0, 0, width, height), Qt::AlignCenter, "Poep."); + p.end(); + + kDebug() << " ++++++++++++++ DONE PAINTING ++++++++++++"; + + delete m_page; + m_page = 0; + //delete d; + //d = 0; + + //img = img.scaled(d->size, Qt::KeepAspectRatioByExpanding, + // Qt::SmoothTransformation); + kDebug() << " ++++++++++++++ toImage ++++++++++++"; + + pix.save("/tmp/webcreatorpreviewpix.png"); + img = pix.toImage(); + kDebug() << "SAVING ........"; + img.save("/tmp/webcreatorpreview.png"); + + /* + // render the HTML page on a bigger pixmap and use smoothScale, + // looks better than directly scaling with the QPainter (malte) + QPixmap pix; + if (width > 400 || height > 600) + { + if (height * 3 > width * 4) + pix = QPixmap(width, width * 4 / 3); + else + pix = QPixmap(height * 3 / 4, height); + } + else + pix = QPixmap(400, 600); + + // light-grey background, in case loadind the page failed + pix.fill( QColor( 245, 245, 245 ) ); + + int borderX = pix.width() / width, + borderY = pix.height() / height; + QRect rc(borderX, borderY, pix.width() - borderX * 2, pix.height() - borderY * 2); + + QPainter p; + p.begin(&pix); + m_html->paint(&p, rc); + p.end(); + + img = pix.toImage(); + m_html->closeUrl(); + */ + + return true; +} + +void WebCreator::timerEvent(QTimerEvent *) +{ + kDebug() << " WEBCREATOR:timerEvent"; + m_failed = true; + m_eventLoop.quit(); +} + +void WebCreator::completed(bool sucess) +{ + kDebug() << " WEBCREATOR:completed" << sucess; + m_failed = !sucess; + m_eventLoop.quit(); +} + +ThumbCreator::Flags WebCreator::flags() const +{ + return DrawFrame; +} + +#include "webcreator.moc" + diff --git a/kioslave/thumbnail/webcreator.h b/kioslave/thumbnail/webcreator.h new file mode 100644 index 0000000..b4c71ef --- /dev/null +++ b/kioslave/thumbnail/webcreator.h @@ -0,0 +1,56 @@ +/* + Copyright 2011 Sebastian Kügler + Copyright 2010 Richard Moore + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef WEBCREATOR_H +#define WEBCREATOR_H + +#include +#include +#include +#include +#include + +class KWebPage; +class WebCreatorPrivate; + +class WebCreator : public QObject, public ThumbCreator +{ + Q_OBJECT +public: + WebCreator(); + virtual ~WebCreator(); + virtual bool create(const QString &path, int width, int height, QImage &img); + virtual Flags flags() const; + +protected: + virtual void timerEvent(QTimerEvent *); + +private Q_SLOTS: + void completed(bool); + +private: + KWebPage *m_page; + QUrl m_url; + QEventLoop m_eventLoop; + bool m_failed; + +}; + +#endif diff --git a/kioslave/thumbnail/webthumbnail.desktop b/kioslave/thumbnail/webthumbnail.desktop new file mode 100644 index 0000000..c187fa4 --- /dev/null +++ b/kioslave/thumbnail/webthumbnail.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Type=Service +Name=Web pages +X-KDE-ServiceTypes=ThumbCreator +MimeType=text/html; +X-KDE-Protocol=http,https +X-KDE-Library=webthumbnail +CacheThumbnail=true