1 /*
2 Copyright (C) 2009 Jakub Wieczorek <faw217@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "qwebplugindatabase.h"
22 #include "qwebplugindatabase_p.h"
23
24 #include "PluginDatabase.h"
25 #include "PluginPackage.h"
26
27 using namespace WebCore;
28
29 /*!
30 \typedef QWebPluginInfo::MimeType
31 \since 4.6
32 \brief Represents a single MIME type supported by a plugin.
33 */
34
QWebPluginInfoPrivate(RefPtr<PluginPackage> pluginPackage)35 QWebPluginInfoPrivate::QWebPluginInfoPrivate(RefPtr<PluginPackage> pluginPackage)
36 : plugin(pluginPackage)
37 {
38 }
39
QWebPluginDatabasePrivate(PluginDatabase * pluginDatabase)40 QWebPluginDatabasePrivate::QWebPluginDatabasePrivate(PluginDatabase* pluginDatabase)
41 : database(pluginDatabase)
42 {
43 }
44
45 /*!
46 \class QWebPluginInfo
47 \since 4.6
48 \brief The QWebPluginInfo class represents a single Netscape plugin.
49
50 A QWebPluginInfo object represents a Netscape plugin picked up by WebKit
51 and included in the plugin database. This class contains information about
52 the plugin, such as its name(), description(), a list of MIME types that it
53 supports (can be accessed with mimeTypes()) and the path of the plugin
54 file.
55
56 Plugins can be enabled and disabled with setEnabled(). If a plugin is
57 disabled, it will not be used by WebKit to handle supported MIME types. To
58 check if a plugin is enabled or not, use enabled().
59
60 \sa QWebPluginDatabase
61 */
62
63 /*!
64 Constructs a null QWebPluginInfo.
65 */
QWebPluginInfo()66 QWebPluginInfo::QWebPluginInfo()
67 : d(new QWebPluginInfoPrivate(0))
68 {
69 }
70
QWebPluginInfo(PluginPackage * plugin)71 QWebPluginInfo::QWebPluginInfo(PluginPackage* plugin)
72 : d(new QWebPluginInfoPrivate(plugin))
73 {
74 }
75
76 /*!
77 Contructs a copy of \a other.
78 */
QWebPluginInfo(const QWebPluginInfo & other)79 QWebPluginInfo::QWebPluginInfo(const QWebPluginInfo& other)
80 : d(new QWebPluginInfoPrivate(other.d->plugin))
81 {
82 }
83
84 /*!
85 Destroys the plugin info.
86 */
~QWebPluginInfo()87 QWebPluginInfo::~QWebPluginInfo()
88 {
89 delete d;
90 }
91
92 /*!
93 Returns the name of the plugin.
94
95 \sa description()
96 */
name() const97 QString QWebPluginInfo::name() const
98 {
99 if (!d->plugin)
100 return QString();
101 return d->plugin->name();
102 }
103
104 /*!
105 Returns the description of the plugin.
106
107 \sa name()
108 */
description() const109 QString QWebPluginInfo::description() const
110 {
111 if (!d->plugin)
112 return QString();
113 return d->plugin->description();
114 }
115
116 /*!
117 Returns a list of MIME types supported by the plugin.
118
119 \sa supportsMimeType()
120 */
mimeTypes() const121 QList<QWebPluginInfo::MimeType> QWebPluginInfo::mimeTypes() const
122 {
123 if (!d->plugin)
124 return QList<MimeType>();
125
126 QList<MimeType> mimeTypes;
127 const MIMEToDescriptionsMap& mimeToDescriptions = d->plugin->mimeToDescriptions();
128 MIMEToDescriptionsMap::const_iterator end = mimeToDescriptions.end();
129 for (MIMEToDescriptionsMap::const_iterator it = mimeToDescriptions.begin(); it != end; ++it) {
130 MimeType mimeType;
131 mimeType.name = it->first;
132 mimeType.description = it->second;
133
134 QStringList fileExtensions;
135 Vector<String> extensions = d->plugin->mimeToExtensions().get(mimeType.name);
136
137 for (unsigned i = 0; i < extensions.size(); ++i)
138 fileExtensions.append(extensions[i]);
139
140 mimeType.fileExtensions = fileExtensions;
141 mimeTypes.append(mimeType);
142 }
143
144 return mimeTypes;
145 }
146
147 /*!
148 Returns true if the plugin supports a specific \a mimeType; otherwise
149 returns false.
150
151 \sa mimeTypes()
152 */
supportsMimeType(const QString & mimeType) const153 bool QWebPluginInfo::supportsMimeType(const QString& mimeType) const
154 {
155 QList<MimeType> types = mimeTypes();
156 foreach (const MimeType& type, types) {
157 if (type.name == mimeType)
158 return true;
159 }
160
161 return false;
162 }
163
164 /*!
165 Returns an absolute path to the plugin file.
166 */
path() const167 QString QWebPluginInfo::path() const
168 {
169 if (!d->plugin)
170 return QString();
171 return d->plugin->path();
172 }
173
174 /*!
175 Returns true if the plugin is a null plugin; otherwise returns false.
176 */
isNull() const177 bool QWebPluginInfo::isNull() const
178 {
179 return !d->plugin;
180 }
181
182 /*!
183 Enables or disables the plugin, depending on the \a enabled parameter.
184
185 Disabled plugins will not be picked up by WebKit when looking for a plugin
186 supporting a particular MIME type.
187
188 \sa isEnabled()
189 */
setEnabled(bool enabled)190 void QWebPluginInfo::setEnabled(bool enabled)
191 {
192 if (!d->plugin)
193 return;
194 d->plugin->setEnabled(enabled);
195 }
196
197 /*!
198 Returns true if the plugin is enabled; otherwise returns false.
199
200 \sa setEnabled()
201 */
isEnabled() const202 bool QWebPluginInfo::isEnabled() const
203 {
204 if (!d->plugin)
205 return false;
206 return d->plugin->isEnabled();
207 }
208
operator ==(const QWebPluginInfo & other) const209 bool QWebPluginInfo::operator==(const QWebPluginInfo& other) const
210 {
211 return d->plugin == other.d->plugin;
212 }
213
operator !=(const QWebPluginInfo & other) const214 bool QWebPluginInfo::operator!=(const QWebPluginInfo& other) const
215 {
216 return d->plugin != other.d->plugin;
217 }
218
operator =(const QWebPluginInfo & other)219 QWebPluginInfo &QWebPluginInfo::operator=(const QWebPluginInfo& other)
220 {
221 if (this == &other)
222 return *this;
223
224 d->plugin = other.d->plugin;
225 return *this;
226 }
227
228 /*!
229 \class QWebPluginDatabase
230 \since 4.6
231 \brief The QWebPluginDatabase class provides an interface for managing
232 Netscape plugins used by WebKit in QWebPages.
233
234 The QWebPluginDatabase class is a database of Netscape plugins that are used
235 by WebKit. The plugins are picked up by WebKit by looking up a set of search paths.
236 The default set can be accessed using defaultSearchPaths(). The search paths
237 can be changed, see searchPaths() and setSearchPaths(). Additional search paths
238 can also be added using addSearchPath().
239
240 The plugins that have been detected are exposed by the plugins() method.
241 The list contains QWebPlugin objects that hold both the metadata and the MIME
242 types that are supported by particular plugins.
243
244 WebKit specifies a plugin for a MIME type by looking for the first plugin that
245 supports the specific MIME type. To get a plugin, that is used by WebKit to
246 handle a specific MIME type, you can use the pluginForMimeType() function.
247
248 To change the way of resolving MIME types ambiguity, you can explicitly set
249 a preferred plugin for a specific MIME type, using setPreferredPluginForMimeType().
250
251 \sa QWebPluginInfo, QWebSettings::pluginDatabase()
252 */
253
QWebPluginDatabase(QObject * parent)254 QWebPluginDatabase::QWebPluginDatabase(QObject* parent)
255 : QObject(parent)
256 , d(new QWebPluginDatabasePrivate(PluginDatabase::installedPlugins()))
257 {
258 }
259
~QWebPluginDatabase()260 QWebPluginDatabase::~QWebPluginDatabase()
261 {
262 delete d;
263 }
264
265 /*!
266 Returns a list of plugins installed in the search paths.
267
268 This list will contain disabled plugins, although they will not be used by
269 WebKit.
270
271 \sa pluginForMimeType()
272 */
plugins() const273 QList<QWebPluginInfo> QWebPluginDatabase::plugins() const
274 {
275 QList<QWebPluginInfo> qwebplugins;
276 const Vector<PluginPackage*>& plugins = d->database->plugins();
277
278 for (unsigned int i = 0; i < plugins.size(); ++i) {
279 PluginPackage* plugin = plugins[i];
280 qwebplugins.append(QWebPluginInfo(plugin));
281 }
282
283 return qwebplugins;
284 }
285
286 /*!
287 Returns a default set of search paths.
288
289 \sa searchPaths(), setSearchPaths()
290 */
defaultSearchPaths()291 QStringList QWebPluginDatabase::defaultSearchPaths()
292 {
293 QStringList paths;
294
295 const Vector<String>& directories = PluginDatabase::defaultPluginDirectories();
296 for (unsigned int i = 0; i < directories.size(); ++i)
297 paths.append(directories[i]);
298
299 return paths;
300 }
301
302 /*!
303 Returns a list of search paths that are used by WebKit to look for plugins.
304
305 \sa defaultSearchPaths(), setSearchPaths()
306 */
searchPaths() const307 QStringList QWebPluginDatabase::searchPaths() const
308 {
309 QStringList paths;
310
311 const Vector<String>& directories = d->database->pluginDirectories();
312 for (unsigned int i = 0; i < directories.size(); ++i)
313 paths.append(directories[i]);
314
315 return paths;
316 }
317
318 /*!
319 Changes the search paths to \a paths.
320 The database is automatically refreshed.
321
322 \sa searchPaths(), defaultSearchPaths()
323 */
setSearchPaths(const QStringList & paths)324 void QWebPluginDatabase::setSearchPaths(const QStringList& paths)
325 {
326 Vector<String> directories;
327
328 for (unsigned int i = 0; i < paths.count(); ++i)
329 directories.append(paths.at(i));
330
331 d->database->setPluginDirectories(directories);
332 // PluginDatabase::setPluginDirectories() does not refresh the database.
333 d->database->refresh();
334 }
335
336 /*!
337 Adds an additional \a path to the current set.
338 The database is automatically refreshed.
339
340 \sa searchPaths(), setSearchPaths()
341 */
addSearchPath(const QString & path)342 void QWebPluginDatabase::addSearchPath(const QString& path)
343 {
344 d->database->addExtraPluginDirectory(path);
345 // PluginDatabase::addExtraPluginDirectory() does refresh the database.
346 }
347
348 /*!
349 Refreshes the plugin database, adds new plugins that have been found and removes
350 the ones that are no longer available in the search paths.
351
352 You can call this function when the set of plugins installed in the search paths
353 changes. You do not need to call this function when changing search paths,
354 in that case WebKit automatically refreshes the database.
355 */
refresh()356 void QWebPluginDatabase::refresh()
357 {
358 d->database->refresh();
359 }
360
361 /*!
362 Returns the plugin that is currently used by WebKit for a given \a mimeType.
363
364 \sa setPreferredPluginForMimeType()
365 */
pluginForMimeType(const QString & mimeType)366 QWebPluginInfo QWebPluginDatabase::pluginForMimeType(const QString& mimeType)
367 {
368 return QWebPluginInfo(d->database->pluginForMIMEType(mimeType));
369 }
370
371 /*!
372 Changes the preferred plugin for a given \a mimeType to \a plugin. The \a plugin
373 has to support the given \a mimeType, otherwise the setting will have no effect.
374
375 Calling the function with a null \a plugin resets the setting.
376
377 \sa pluginForMimeType()
378 */
setPreferredPluginForMimeType(const QString & mimeType,const QWebPluginInfo & plugin)379 void QWebPluginDatabase::setPreferredPluginForMimeType(const QString& mimeType, const QWebPluginInfo& plugin)
380 {
381 d->database->setPreferredPluginForMIMEType(mimeType, plugin.d->plugin.get());
382 }
383