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 <kio/scheduler.h> 00036 #include <kparts/part.h> 00037 00038 #include <QtCore/QListIterator> 00039 #include <QtCore/QStringList> 00040 #include <QtCore/QList> 00041 #include <QtCore/QTimer> 00042 #include <QtCore/QEventLoop> 00043 00044 #include <QtWebKit/QWebPluginFactory> 00045 #include <QtWebKit/QWebFrame> 00046 #include <QtWebKit/QWebView> 00047 00048 #define QL1S(x) QLatin1String(x) 00049 #define QL1C(x) QLatin1Char(x) 00050 00051 static bool excludedMimeType(const QString &type) 00052 { 00053 if (type.startsWith(QL1S("inode/"), Qt::CaseInsensitive)) 00054 return true; 00055 00056 if (type.startsWith(QL1S("application/x-java"), Qt::CaseInsensitive)) 00057 return true; 00058 00059 if (type == QL1S("application/x-shockwave-flash") || 00060 type == QL1S("application/futuresplash")) 00061 return true; 00062 00063 return false; 00064 } 00065 00066 class KWebPluginFactory::KWebPluginFactoryPrivate 00067 { 00068 public: 00069 KWebPluginFactoryPrivate() {} 00070 00071 void _k_slotMimeType(KIO::Job *, const QString&); 00072 QString mimeType; 00073 }; 00074 00075 void KWebPluginFactory::KWebPluginFactoryPrivate::_k_slotMimeType(KIO::Job *kioJob, const QString& mimeType) 00076 { 00077 kDebug(800) << "Got mimetype" << mimeType; 00078 this->mimeType = mimeType; 00079 KIO::TransferJob * job = qobject_cast<KIO::TransferJob*> (kioJob); 00080 if (job) { 00081 job->putOnHold(); 00082 KIO::Scheduler::publishSlaveOnHold(); 00083 } 00084 } 00085 00086 KWebPluginFactory::KWebPluginFactory(QObject *parent) 00087 :QWebPluginFactory(parent), 00088 d(new KWebPluginFactoryPrivate) 00089 { 00090 } 00091 00092 KWebPluginFactory::~KWebPluginFactory() 00093 { 00094 delete d; 00095 } 00096 00097 QObject* KWebPluginFactory::create(const QString& _mimeType, const QUrl& url, const QStringList& argumentNames, const QStringList& argumentValues) const 00098 { 00099 // Only attempt to find a KPart for the supported mime types... 00100 QVariantList arguments; 00101 const int count = argumentNames.count(); 00102 00103 for (int i = 0; i < count; ++i) { 00104 arguments << QString(argumentNames.at(i) + QL1S("=\"") + argumentValues.at(i) + QL1C('\"')); 00105 } 00106 00107 QString mimeType (_mimeType.trimmed()); 00108 // If no mimetype is provided, we do our best to correctly determine it here... 00109 if (mimeType.isEmpty()) { 00110 kDebug(800) << "Looking up missing mimetype for plugin resource:" << url; 00111 const KUrl reqUrl (url); 00112 KMimeType::Ptr ptr = KMimeType::findByUrl(reqUrl, 0, reqUrl.isLocalFile()); 00113 // Stat the resource if we mimetype cannot be determined thru 00114 // KMimeType::findByUrl... 00115 if (ptr->isDefault()) { 00116 d->mimeType.clear(); 00117 QEventLoop eventLoop; 00118 KIO::TransferJob *job = KIO::get(reqUrl, KIO::NoReload, KIO::HideProgressInfo); 00119 connect(job, SIGNAL(mimetype (KIO::Job *, const QString&)), 00120 this, SLOT( _k_slotMimeType(KIO::Job *, const QString&))); 00121 connect (job, SIGNAL(finished (KJob *)), &eventLoop, SLOT(quit())); 00122 eventLoop.exec(); 00123 mimeType = d->mimeType; 00124 } else { 00125 mimeType = ptr->name(); 00126 } 00127 00128 // Disregard inode/* mime-types... 00129 if (mimeType.startsWith(QLatin1String("inode/"), Qt::CaseInsensitive)) 00130 mimeType.clear(); 00131 kDebug(800) << "Updated mimetype to" << mimeType; 00132 } 00133 00134 KParts::ReadOnlyPart* part = 0; 00135 00136 // Defer handling of flash content to QtWebKit's builtin viewer. 00137 // If you want to use/test KDE's nspluginviewer, comment out the 00138 // if statement below. 00139 if (!mimeType.isEmpty() && !excludedMimeType(mimeType)) 00140 part = KMimeTypeTrader::createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType, 0, parent(), QString(), arguments); 00141 00142 kDebug(800) << "Asked for" << mimeType << "plugin, got" << part; 00143 00144 if (part) { 00145 QMap<QString, QString> metaData = part->arguments().metaData(); 00146 QString urlStr = url.toString(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment); 00147 metaData.insert("PropagateHttpHeader", "true"); 00148 metaData.insert("referrer", urlStr); 00149 metaData.insert("cross-domain", urlStr); 00150 metaData.insert("main_frame_request", "TRUE"); 00151 metaData.insert("ssl_activate_warnings", "TRUE"); 00152 00153 KWebPage *page = qobject_cast<KWebPage *>(parent()); 00154 00155 if (page) { 00156 const QString scheme = page->mainFrame()->url().scheme(); 00157 if (page && (QString::compare(scheme, QL1S("https"), Qt::CaseInsensitive) == 0 || 00158 QString::compare(scheme, QL1S("webdavs"), Qt::CaseInsensitive) == 0)) 00159 metaData.insert("ssl_was_in_use", "TRUE"); 00160 else 00161 metaData.insert("ssl_was_in_use", "FALSE"); 00162 } 00163 00164 KParts::OpenUrlArguments openUrlArgs = part->arguments(); 00165 openUrlArgs.metaData() = metaData; 00166 openUrlArgs.setMimeType(mimeType); 00167 part->setArguments(openUrlArgs); 00168 part->openUrl(url); 00169 return part->widget(); 00170 } 00171 00172 return 0; 00173 } 00174 00175 QList<KWebPluginFactory::Plugin> KWebPluginFactory::plugins() const 00176 { 00177 QList<Plugin> plugins; 00178 return plugins; 00179 } 00180 00181 #include "kwebpluginfactory.moc"
KDE 4.6 API Reference