1/*
2 * This file is part of the KDE project.
3 *
4 * Copyright (C) 2008 Michael Howell <mhowell123@gmail.com>
5 * Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
6 * Copyright (C) 2009 Dawit Alemayehu <adawit @ kde.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#include "kwebpluginfactory.h"
26#include "kwebpage.h"
27#include "kwebview.h"
28
29#include <kmimetypetrader.h>
30#include <kservicetypetrader.h>
31#include <kmimetype.h>
32#include <kdebug.h>
33
34#include <kio/job.h>
35#include <kparts/part.h>
36
37#include <QtCore/QListIterator>
38#include <QtCore/QStringList>
39#include <QtCore/QList>
40
41#include <QtWebKit/QWebPluginFactory>
42#include <QtWebKit/QWebFrame>
43#include <QtWebKit/QWebView>
44
45#define QL1S(x) QLatin1String(x)
46#define QL1C(x) QLatin1Char(x)
47
48
49KWebPluginFactory::KWebPluginFactory(QObject *parent)
50 :QWebPluginFactory(parent),d(0)
51{
52}
53
54KWebPluginFactory::~KWebPluginFactory()
55{
56}
57
58QObject* KWebPluginFactory::create(const QString& _mimeType, const QUrl& url, const QStringList& argumentNames, const QStringList& argumentValues) const
59{
60 QString mimeType (_mimeType.trimmed());
61 // If no mimetype is provided, we do our best to correctly determine it here...
62 if (mimeType.isEmpty()) {
63 kDebug(800) << "Looking up missing mimetype for plugin resource:" << url;
64 extractGuessedMimeType(url, &mimeType);
65 kDebug(800) << "Updated mimetype to" << mimeType;
66 }
67
68 // Defer handling of flash content to QtWebKit's builtin viewer.
69 // If you want to use/test KDE's nspluginviewer, comment out the
70 // if statement below.
71 KParts::ReadOnlyPart* part = (excludedMimeType(mimeType) ? 0 : createPartInstanceFrom(mimeType, argumentNames, argumentValues, 0, parent()));
72
73 kDebug(800) << "Asked for" << mimeType << "plugin, got" << part;
74
75 if (part) {
76 QMap<QString, QString> metaData = part->arguments().metaData();
77 QString urlStr = url.toString(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment);
78 metaData.insert("PropagateHttpHeader", "true");
79 metaData.insert("referrer", urlStr);
80 metaData.insert("cross-domain", urlStr);
81 metaData.insert("main_frame_request", "TRUE");
82 metaData.insert("ssl_activate_warnings", "TRUE");
83
84 KWebPage *page = qobject_cast<KWebPage *>(parent());
85
86 if (page) {
87 const QString scheme = page->currentFrame()->url().scheme();
88 if (page && (QString::compare(scheme, QL1S("https"), Qt::CaseInsensitive) == 0 ||
89 QString::compare(scheme, QL1S("webdavs"), Qt::CaseInsensitive) == 0))
90 metaData.insert("ssl_was_in_use", "TRUE");
91 else
92 metaData.insert("ssl_was_in_use", "FALSE");
93 }
94
95 KParts::OpenUrlArguments openUrlArgs = part->arguments();
96 openUrlArgs.metaData() = metaData;
97 openUrlArgs.setMimeType(mimeType);
98 part->setArguments(openUrlArgs);
99 part->openUrl(url);
100 return part->widget();
101 }
102
103 return 0;
104}
105
106QList<KWebPluginFactory::Plugin> KWebPluginFactory::plugins() const
107{
108 QList<Plugin> plugins;
109 return plugins;
110}
111
112static bool isHttpProtocol(const QUrl& url)
113{
114 const QString scheme (url.scheme());
115 return (scheme.startsWith(QL1S("http"), Qt::CaseInsensitive)
116 || scheme.startsWith(QL1S("webdav"), Qt::CaseInsensitive));
117}
118
119void KWebPluginFactory::extractGuessedMimeType (const QUrl& url, QString* mimeType) const
120{
121 if (mimeType) {
122 const KUrl reqUrl ((isHttpProtocol(url) ? url.path() : url));
123 KMimeType::Ptr ptr = KMimeType::findByUrl(reqUrl, 0, reqUrl.isLocalFile(), true);
124 if (!ptr->isDefault() && !ptr->name().startsWith(QL1S("inode/"), Qt::CaseInsensitive)) {
125 *mimeType = ptr->name();
126 }
127 }
128}
129
130KParts::ReadOnlyPart* KWebPluginFactory::createPartInstanceFrom(const QString& mimeType,
131 const QStringList& argumentNames,
132 const QStringList& argumentValues,
133 QWidget* parentWidget,
134 QObject* parentObj) const
135{
136 KParts::ReadOnlyPart* part = 0;
137
138 if (!mimeType.isEmpty()) {
139 // Only attempt to find a KPart for the supported mime types...
140 QVariantList arguments;
141 const int count = argumentNames.count();
142
143 for (int i = 0; i < count; ++i) {
144 arguments << QString(argumentNames.at(i) + QL1S("=\"") + argumentValues.at(i) + QL1C('\"'));
145 }
146 part = KMimeTypeTrader::createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType, parentWidget, parentObj, QString(), arguments);
147 }
148
149 return part;
150}
151
152bool KWebPluginFactory::excludedMimeType (const QString& mimeType) const
153{
154 if (mimeType.startsWith(QL1S("inode/"), Qt::CaseInsensitive))
155 return true;
156
157 if (mimeType.startsWith(QL1S("application/x-java"), Qt::CaseInsensitive))
158 return true;
159
160 if (mimeType == QL1S("application/x-shockwave-flash") ||
161 mimeType == QL1S("application/futuresplash"))
162 return true;
163
164 return false;
165}
166
167#include "kwebpluginfactory.moc"
168