1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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
21#include "config.h"
22#include "QtPlatformPlugin.h"
23
24#include "qwebkitplatformplugin.h"
25
26#include <QCoreApplication>
27#include <QDir>
28#include <QPluginLoader>
29
30namespace WebCore {
31
32bool QtPlatformPlugin::load(const QString& file)
33{
34    m_loader.setFileName(file);
35    if (!m_loader.load())
36        return false;
37
38    QObject* obj = m_loader.instance();
39    if (obj) {
40        m_plugin = qobject_cast<QWebKitPlatformPlugin*>(obj);
41        if (m_plugin)
42            return true;
43    }
44
45    m_loader.unload();
46    return false;
47}
48
49bool QtPlatformPlugin::load()
50{
51    const QLatin1String suffix("/webkit/");
52    const QStringList paths = QCoreApplication::libraryPaths();
53
54    for (int i = 0; i < paths.count(); ++i) {
55        const QDir dir(paths[i] + suffix);
56        if (!dir.exists())
57            continue;
58        const QStringList files = dir.entryList(QDir::Files);
59        for (int j = 0; j < files.count(); ++j) {
60            if (load(dir.absoluteFilePath(files.at(j))))
61                return true;
62        }
63    }
64    return false;
65}
66
67QtPlatformPlugin::~QtPlatformPlugin()
68{
69    m_loader.unload();
70}
71
72bool QtPlatformPlugin::loadStaticallyLinkedPlugin()
73{
74    QObjectList objs = QPluginLoader::staticInstances();
75    for (int i = 0; i < objs.size(); ++i) {
76        m_plugin = qobject_cast<QWebKitPlatformPlugin*>(objs[i]);
77        if (m_plugin)
78            return true;
79    }
80    return false;
81}
82
83QWebKitPlatformPlugin* QtPlatformPlugin::plugin()
84{
85    if (m_loaded)
86        return m_plugin;
87    m_loaded = true;
88
89    if (loadStaticallyLinkedPlugin())
90        return m_plugin;
91
92    // Plugin path is stored in a static variable to avoid searching for the plugin
93    // more then once.
94    static QString pluginPath;
95
96    if (pluginPath.isNull()) {
97        if (load())
98            pluginPath = m_loader.fileName();
99    } else
100        load(pluginPath);
101
102    return m_plugin;
103}
104
105QWebSelectMethod* QtPlatformPlugin::createSelectInputMethod()
106{
107    QWebKitPlatformPlugin* p = plugin();
108    return p ? static_cast<QWebSelectMethod*>(p->createExtension(QWebKitPlatformPlugin::MultipleSelections)) : 0;
109}
110
111
112QWebNotificationPresenter* QtPlatformPlugin::createNotificationPresenter()
113{
114    QWebKitPlatformPlugin* p = plugin();
115    return p ? static_cast<QWebNotificationPresenter*>(p->createExtension(QWebKitPlatformPlugin::Notifications)) : 0;
116}
117
118QWebHapticFeedbackPlayer* QtPlatformPlugin::createHapticFeedbackPlayer()
119{
120    QWebKitPlatformPlugin* p = plugin();
121    return p ? static_cast<QWebHapticFeedbackPlayer*>(p->createExtension(QWebKitPlatformPlugin::Haptics)) : 0;
122}
123
124QWebTouchModifier* QtPlatformPlugin::createTouchModifier()
125{
126    QWebKitPlatformPlugin* p = plugin();
127    return p ? static_cast<QWebTouchModifier*>(p->createExtension(QWebKitPlatformPlugin::TouchInteraction)) : 0;
128}
129
130#if ENABLE(VIDEO) && USE(QT_MULTIMEDIA)
131QWebFullScreenVideoHandler* QtPlatformPlugin::createFullScreenVideoHandler()
132{
133    QWebKitPlatformPlugin* p = plugin();
134    return p ? static_cast<QWebFullScreenVideoHandler*>(p->createExtension(QWebKitPlatformPlugin::FullScreenVideoPlayer)) : 0;
135}
136#endif
137
138}
139