2 Copyright (C) 2009 Jakub Wieczorek <faw217@gmail.com>
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.
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.
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.
21 #include "qwebplugindatabase_p.h"
23 #include "PluginDatabase.h"
24 #include "PluginPackage.h"
26 using namespace WebCore;
30 \typedef QWebPluginInfo::MimeType
32 \brief Represents a single MIME type supported by a plugin.
39 \brief The QWebPluginInfo class represents a single Netscape plugin.
41 A QWebPluginInfo object represents a Netscape plugin picked up by WebKit
42 and included in the plugin database. This class contains information about
43 the plugin, such as its name(), description(), a list of MIME types that it
44 supports (can be accessed with mimeTypes()) and the path of the plugin
47 Plugins can be enabled and disabled with setEnabled(). If a plugin is
48 disabled, it will not be used by WebKit to handle supported MIME types. To
49 check if a plugin is enabled or not, use enabled().
51 \sa QWebPluginDatabase
55 Constructs a null QWebPluginInfo.
57 QWebPluginInfo::QWebPluginInfo()
62 QWebPluginInfo::QWebPluginInfo(PluginPackage* package)
70 Contructs a copy of \a other.
72 QWebPluginInfo::QWebPluginInfo(const QWebPluginInfo& other)
73 : m_package(other.m_package)
80 Destroys the plugin info.
82 QWebPluginInfo::~QWebPluginInfo()
89 Returns the name of the plugin.
93 QString QWebPluginInfo::name() const
97 return m_package->name();
101 Returns the description of the plugin.
105 QString QWebPluginInfo::description() const
109 return m_package->description();
113 Returns a list of MIME types supported by the plugin.
115 \sa supportsMimeType()
117 QList<QWebPluginInfo::MimeType> QWebPluginInfo::mimeTypes() const
119 if (m_package && m_mimeTypes.isEmpty()) {
120 const MIMEToDescriptionsMap& mimeToDescriptions = m_package->mimeToDescriptions();
121 MIMEToDescriptionsMap::const_iterator end = mimeToDescriptions.end();
123 for (MIMEToDescriptionsMap::const_iterator it = mimeToDescriptions.begin(); it != end; ++it) {
125 mimeType.name = it->first;
126 mimeType.description = it->second;
128 QStringList fileExtensions;
129 Vector<String> extensions = m_package->mimeToExtensions().get(mimeType.name);
131 for (unsigned i = 0; i < extensions.size(); ++i)
132 fileExtensions.append(extensions[i]);
134 mimeType.fileExtensions = fileExtensions;
135 m_mimeTypes.append(mimeType);
143 Returns true if the plugin supports a specific \a mimeType; otherwise
148 bool QWebPluginInfo::supportsMimeType(const QString& mimeType) const
152 return m_package->mimeToDescriptions().contains(mimeType);
156 Returns an absolute path to the plugin file.
158 QString QWebPluginInfo::path() const
162 return m_package->path();
166 Returns true if the plugin is a null plugin; otherwise returns false.
168 bool QWebPluginInfo::isNull() const
174 Enables or disables the plugin, depending on the \a enabled parameter.
176 Disabled plugins will not be picked up by WebKit when looking for a plugin
177 supporting a particular MIME type.
181 void QWebPluginInfo::setEnabled(bool enabled)
185 m_package->setEnabled(enabled);
189 Returns true if the plugin is enabled; otherwise returns false.
193 bool QWebPluginInfo::isEnabled() const
197 return m_package->isEnabled();
201 Returns true if this plugin info is the same as the \a other plugin info.
203 bool QWebPluginInfo::operator==(const QWebPluginInfo& other) const
205 return m_package == other.m_package;
209 Returns true if this plugin info is different from the \a other plugin info.
211 bool QWebPluginInfo::operator!=(const QWebPluginInfo& other) const
213 return m_package != other.m_package;
217 Assigns the \a other plugin info to this plugin info, and returns a reference
220 QWebPluginInfo &QWebPluginInfo::operator=(const QWebPluginInfo& other)
227 m_package = other.m_package;
230 m_mimeTypes = other.m_mimeTypes;
236 \class QWebPluginDatabase
239 \brief The QWebPluginDatabase class provides an interface for managing
240 Netscape plugins used by WebKit in QWebPages.
242 The QWebPluginDatabase class is a database of Netscape plugins that are used
243 by WebKit. The plugins are picked up by WebKit by looking up a set of search paths.
244 The default set can be accessed using defaultSearchPaths(). The search paths
245 can be changed, see searchPaths() and setSearchPaths(). Additional search paths
246 can also be added using addSearchPath().
248 The plugins that have been detected are exposed by the plugins() method.
249 The list contains QWebPlugin objects that hold both the metadata and the MIME
250 types that are supported by particular plugins.
252 WebKit specifies a plugin for a MIME type by looking for the first plugin that
253 supports the specific MIME type. To get a plugin, that is used by WebKit to
254 handle a specific MIME type, you can use the pluginForMimeType() function.
256 To change the way of resolving MIME types ambiguity, you can explicitly set
257 a preferred plugin for a specific MIME type, using setPreferredPluginForMimeType().
259 \sa QWebPluginInfo, QWebSettings::pluginDatabase()
262 QWebPluginDatabase::QWebPluginDatabase(QObject* parent)
264 , m_database(PluginDatabase::installedPlugins())
268 QWebPluginDatabase::~QWebPluginDatabase()
273 Returns a list of plugins installed in the search paths.
275 This list will contain disabled plugins, although they will not be used by
278 \sa pluginForMimeType()
280 QList<QWebPluginInfo> QWebPluginDatabase::plugins() const
282 QList<QWebPluginInfo> qwebplugins;
283 const Vector<PluginPackage*>& plugins = m_database->plugins();
285 for (unsigned int i = 0; i < plugins.size(); ++i) {
286 PluginPackage* plugin = plugins[i];
287 #if ENABLE(NETSCAPE_PLUGIN_METADATA_CACHE)
288 if (!plugin->ensurePluginLoaded())
291 qwebplugins.append(QWebPluginInfo(plugin));
298 Returns a default set of search paths.
300 \sa searchPaths(), setSearchPaths()
302 QStringList QWebPluginDatabase::defaultSearchPaths()
306 const Vector<String>& directories = PluginDatabase::defaultPluginDirectories();
307 for (unsigned int i = 0; i < directories.size(); ++i)
308 paths.append(directories[i]);
314 Returns a list of search paths that are used by WebKit to look for plugins.
316 \sa defaultSearchPaths(), setSearchPaths()
318 QStringList QWebPluginDatabase::searchPaths() const
322 const Vector<String>& directories = m_database->pluginDirectories();
323 for (unsigned int i = 0; i < directories.size(); ++i)
324 paths.append(directories[i]);
330 Changes the search paths to \a paths.
331 The database is automatically refreshed.
333 \sa searchPaths(), defaultSearchPaths()
335 void QWebPluginDatabase::setSearchPaths(const QStringList& paths)
337 Vector<String> directories;
339 for (int i = 0; i < paths.count(); ++i)
340 directories.append(paths.at(i));
342 m_database->setPluginDirectories(directories);
343 // PluginDatabase::setPluginDirectories() does not refresh the database.
344 m_database->refresh();
348 Adds an additional \a path to the current set.
349 The database is automatically refreshed.
351 \sa searchPaths(), setSearchPaths()
353 void QWebPluginDatabase::addSearchPath(const QString& path)
355 m_database->addExtraPluginDirectory(path);
356 // PluginDatabase::addExtraPluginDirectory() does refresh the database.
360 Refreshes the plugin database, adds new plugins that have been found and removes
361 the ones that are no longer available in the search paths.
363 You can call this function when the set of plugins installed in the search paths
364 changes. You do not need to call this function when changing search paths,
365 in that case WebKit automatically refreshes the database.
367 void QWebPluginDatabase::refresh()
369 m_database->refresh();
373 Returns the plugin that is currently used by WebKit for a given \a mimeType.
375 \sa setPreferredPluginForMimeType()
377 QWebPluginInfo QWebPluginDatabase::pluginForMimeType(const QString& mimeType)
379 return QWebPluginInfo(m_database->pluginForMIMEType(mimeType));
383 Changes the preferred plugin for a given \a mimeType to \a plugin. The \a plugin
384 has to support the given \a mimeType, otherwise the setting will have no effect.
386 Calling the function with a null \a plugin resets the setting.
388 \sa pluginForMimeType()
390 void QWebPluginDatabase::setPreferredPluginForMimeType(const QString& mimeType, const QWebPluginInfo& plugin)
392 m_database->setPreferredPluginForMIMEType(mimeType, plugin.m_package);