KDEWebKit
kwebpluginfactory.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE project. 00003 * 00004 * Copyright (C) 2008 Michael Howell <mhowell123@gmail.com> 00005 * Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org> 00006 * Copyright (C) 2009 Dawit Alemayehu <adawit @ kde.org> 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 00025 #include "kwebpluginfactory.h" 00026 #include "kwebpage.h" 00027 #include "kwebview.h" 00028 00029 #include <kmimetypetrader.h> 00030 #include <kservicetypetrader.h> 00031 #include <kmimetype.h> 00032 #include <kdebug.h> 00033 00034 #include <kio/job.h> 00035 #include <kparts/part.h> 00036 00037 #include <QtCore/QListIterator> 00038 #include <QtCore/QStringList> 00039 #include <QtCore/QList> 00040 00041 #include <QtWebKit/QWebPluginFactory> 00042 #include <QtWebKit/QWebFrame> 00043 #include <QtWebKit/QWebView> 00044 00045 #define QL1S(x) QLatin1String(x) 00046 #define QL1C(x) QLatin1Char(x) 00047 00048 00049 KWebPluginFactory::KWebPluginFactory(QObject *parent) 00050 :QWebPluginFactory(parent),d(0) 00051 { 00052 } 00053 00054 KWebPluginFactory::~KWebPluginFactory() 00055 { 00056 } 00057 00058 QObject* KWebPluginFactory::create(const QString& _mimeType, const QUrl& url, const QStringList& argumentNames, const QStringList& argumentValues) const 00059 { 00060 QString mimeType (_mimeType.trimmed()); 00061 // If no mimetype is provided, we do our best to correctly determine it here... 00062 if (mimeType.isEmpty()) { 00063 kDebug(800) << "Looking up missing mimetype for plugin resource:" << url; 00064 extractGuessedMimeType(url, &mimeType); 00065 kDebug(800) << "Updated mimetype to" << mimeType; 00066 } 00067 00068 // Defer handling of flash content to QtWebKit's builtin viewer. 00069 // If you want to use/test KDE's nspluginviewer, comment out the 00070 // if statement below. 00071 KParts::ReadOnlyPart* part = (excludedMimeType(mimeType) ? 0 : createPartInstanceFrom(mimeType, argumentNames, argumentValues, 0, parent())); 00072 00073 kDebug(800) << "Asked for" << mimeType << "plugin, got" << part; 00074 00075 if (part) { 00076 QMap<QString, QString> metaData = part->arguments().metaData(); 00077 QString urlStr = url.toString(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment); 00078 metaData.insert("PropagateHttpHeader", "true"); 00079 metaData.insert("referrer", urlStr); 00080 metaData.insert("cross-domain", urlStr); 00081 metaData.insert("main_frame_request", "TRUE"); 00082 metaData.insert("ssl_activate_warnings", "TRUE"); 00083 00084 KWebPage *page = qobject_cast<KWebPage *>(parent()); 00085 00086 if (page) { 00087 const QString scheme = page->currentFrame()->url().scheme(); 00088 if (page && (QString::compare(scheme, QL1S("https"), Qt::CaseInsensitive) == 0 || 00089 QString::compare(scheme, QL1S("webdavs"), Qt::CaseInsensitive) == 0)) 00090 metaData.insert("ssl_was_in_use", "TRUE"); 00091 else 00092 metaData.insert("ssl_was_in_use", "FALSE"); 00093 } 00094 00095 KParts::OpenUrlArguments openUrlArgs = part->arguments(); 00096 openUrlArgs.metaData() = metaData; 00097 openUrlArgs.setMimeType(mimeType); 00098 part->setArguments(openUrlArgs); 00099 part->openUrl(url); 00100 return part->widget(); 00101 } 00102 00103 return 0; 00104 } 00105 00106 QList<KWebPluginFactory::Plugin> KWebPluginFactory::plugins() const 00107 { 00108 QList<Plugin> plugins; 00109 return plugins; 00110 } 00111 00112 static bool isHttpProtocol(const QUrl& url) 00113 { 00114 const QString scheme (url.scheme()); 00115 return (scheme.startsWith(QL1S("http"), Qt::CaseInsensitive) 00116 || scheme.startsWith(QL1S("webdav"), Qt::CaseInsensitive)); 00117 } 00118 00119 void KWebPluginFactory::extractGuessedMimeType (const QUrl& url, QString* mimeType) const 00120 { 00121 if (mimeType) { 00122 const KUrl reqUrl ((isHttpProtocol(url) ? url.path() : url)); 00123 KMimeType::Ptr ptr = KMimeType::findByUrl(reqUrl, 0, reqUrl.isLocalFile(), true); 00124 if (!ptr->isDefault() && !ptr->name().startsWith(QL1S("inode/"), Qt::CaseInsensitive)) { 00125 *mimeType = ptr->name(); 00126 } 00127 } 00128 } 00129 00130 KParts::ReadOnlyPart* KWebPluginFactory::createPartInstanceFrom(const QString& mimeType, 00131 const QStringList& argumentNames, 00132 const QStringList& argumentValues, 00133 QWidget* parentWidget, 00134 QObject* parentObj) const 00135 { 00136 KParts::ReadOnlyPart* part = 0; 00137 00138 if (!mimeType.isEmpty()) { 00139 // Only attempt to find a KPart for the supported mime types... 00140 QVariantList arguments; 00141 const int count = argumentNames.count(); 00142 00143 for (int i = 0; i < count; ++i) { 00144 arguments << QString(argumentNames.at(i) + QL1S("=\"") + argumentValues.at(i) + QL1C('\"')); 00145 } 00146 part = KMimeTypeTrader::createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType, parentWidget, parentObj, QString(), arguments); 00147 } 00148 00149 return part; 00150 } 00151 00152 bool KWebPluginFactory::excludedMimeType (const QString& mimeType) const 00153 { 00154 if (mimeType.startsWith(QL1S("inode/"), Qt::CaseInsensitive)) 00155 return true; 00156 00157 if (mimeType.startsWith(QL1S("application/x-java"), Qt::CaseInsensitive)) 00158 return true; 00159 00160 if (mimeType == QL1S("application/x-shockwave-flash") || 00161 mimeType == QL1S("application/futuresplash")) 00162 return true; 00163 00164 return false; 00165 } 00166 00167 #include "kwebpluginfactory.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:47:57 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:47:57 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.