|
1 /* |
|
2 Copyright (C) 2007 Trolltech ASA |
|
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 This class provides all functionality needed for loading images, style sheets and html |
|
20 pages from the web. It has a memory cache for these objects. |
|
21 */ |
|
22 #include "qwebobjectplugin_p.h" |
|
23 #include <qwebobjectpluginconnector.h> |
|
24 #include <qcoreapplication.h> |
|
25 #include <qfileinfo.h> |
|
26 |
|
27 #ifndef QT_NO_LIBRARY |
|
28 Q_GLOBAL_STATIC_WITH_ARGS(QWebFactoryLoader, loader, |
|
29 (QWebObjectPluginFactoryInterface_iid, QCoreApplication::libraryPaths(), QLatin1String("/webplugins"))) |
|
30 #endif |
|
31 |
|
32 |
|
33 QWebFactoryLoader::QWebFactoryLoader(const char *iid, const QStringList &paths, const QString &suffix, Qt::CaseSensitivity) |
|
34 : QFactoryLoader(iid, paths, suffix) |
|
35 { |
|
36 QStringList plugins = keys(); |
|
37 foreach(QString k, plugins) { |
|
38 QWebObjectPlugin *plugin = qobject_cast<QWebObjectPlugin *>(instance(k)); |
|
39 if (!plugin) |
|
40 continue; |
|
41 Info info; |
|
42 info.name = k; |
|
43 info.description = plugin->descriptionForKey(k); |
|
44 QStringList mimetypes = plugin->mimetypesForKey(k); |
|
45 foreach(QString m, mimetypes) { |
|
46 MimeInfo mime; |
|
47 mime.type = m; |
|
48 mime.extensions = plugin->extensionsForMimetype(m); |
|
49 info.mimes << mime; |
|
50 } |
|
51 m_pluginInfo.append(info); |
|
52 } |
|
53 } |
|
54 |
|
55 QWebFactoryLoader *QWebFactoryLoader::self() |
|
56 { |
|
57 return loader(); |
|
58 } |
|
59 |
|
60 |
|
61 QString QWebFactoryLoader::descriptionForName(const QString &key) const |
|
62 { |
|
63 foreach(const Info &info, m_pluginInfo) { |
|
64 if (info.name == key) |
|
65 return info.description; |
|
66 } |
|
67 return QString(); |
|
68 } |
|
69 |
|
70 QStringList QWebFactoryLoader::mimetypesForName(const QString &key) const |
|
71 { |
|
72 foreach(const Info &info, m_pluginInfo) { |
|
73 if (info.name == key) { |
|
74 QStringList mimetypes; |
|
75 foreach (const MimeInfo &m, info.mimes) |
|
76 mimetypes.append(m.type); |
|
77 return mimetypes; |
|
78 } |
|
79 } |
|
80 return QStringList(); |
|
81 } |
|
82 |
|
83 QString QWebFactoryLoader::mimeTypeForExtension(const QString &extension) |
|
84 { |
|
85 foreach(const Info &info, m_pluginInfo) { |
|
86 foreach (const MimeInfo &m, info.mimes) { |
|
87 if (m.extensions.contains(extension)) |
|
88 return m.type; |
|
89 } |
|
90 } |
|
91 return QString(); |
|
92 } |
|
93 |
|
94 |
|
95 QStringList QWebFactoryLoader::extensions() const |
|
96 { |
|
97 QStringList extensions; |
|
98 foreach(const Info &info, m_pluginInfo) { |
|
99 foreach (const MimeInfo &m, info.mimes) |
|
100 extensions << m.extensions; |
|
101 } |
|
102 return QStringList(); |
|
103 |
|
104 } |
|
105 |
|
106 QString QWebFactoryLoader::nameForMimetype(const QString &mimeType) const |
|
107 { |
|
108 foreach(const Info &info, m_pluginInfo) { |
|
109 foreach (const MimeInfo &m, info.mimes) |
|
110 if (m.type == mimeType) |
|
111 return info.name; |
|
112 } |
|
113 return QString(); |
|
114 } |
|
115 |
|
116 QObject *QWebFactoryLoader::create(QWebFrame *frame, |
|
117 const QUrl &url, |
|
118 const QString &_mimeType, |
|
119 const QStringList &argumentNames, |
|
120 const QStringList &argumentValues) |
|
121 { |
|
122 QString mimeType = _mimeType; |
|
123 if (mimeType.isEmpty()) { |
|
124 QFileInfo fi(url.path()); |
|
125 mimeType = mimeTypeForExtension(fi.suffix()); |
|
126 } |
|
127 QString name = nameForMimetype(mimeType); |
|
128 QWebObjectPlugin *plugin = qobject_cast<QWebObjectPlugin *>(instance(name)); |
|
129 if (!plugin) |
|
130 return 0; |
|
131 QWebObjectPluginConnector *connector = new QWebObjectPluginConnector(frame); |
|
132 return plugin->create(connector, url, mimeType, argumentNames, argumentValues); |
|
133 } |
|
134 |
|
135 |
|
136 |
|
137 /*! \class QWebObjectPlugin |
|
138 |
|
139 This class is a plugin for the HTML object tag. It can be used to embed arbitrary content in a web page. |
|
140 */ |
|
141 |
|
142 |
|
143 QWebObjectPlugin::QWebObjectPlugin(QObject *parent) |
|
144 : QObject(parent) |
|
145 { |
|
146 } |
|
147 |
|
148 QWebObjectPlugin::~QWebObjectPlugin() |
|
149 { |
|
150 } |
|
151 |
|
152 /*! |
|
153 \fn QStringList QWebObjectPlugin::keys() const |
|
154 |
|
155 The keys should be unique names. |
|
156 */ |
|
157 |
|
158 /*! |
|
159 A description for \a key. |
|
160 */ |
|
161 QString QWebObjectPlugin::descriptionForKey(const QString &key) const |
|
162 { |
|
163 return QString(); |
|
164 } |
|
165 |
|
166 /*! |
|
167 returns the mimetypes that can be handled by \a key. |
|
168 */ |
|
169 QStringList QWebObjectPlugin::mimetypesForKey(const QString &key) const |
|
170 { |
|
171 return QStringList(); |
|
172 } |
|
173 |
|
174 |
|
175 /*! |
|
176 \fn QStringList QWebObjectPlugin::extensionsForMimetype() const |
|
177 |
|
178 Should return a list of extensions that are recognised to match the \a mimeType. |
|
179 */ |
|
180 QStringList QWebObjectPlugin::extensionsForMimetype(const QString &mimeType) const |
|
181 { |
|
182 return QStringList(); |
|
183 } |
|
184 |
|
185 /*! |
|
186 \fn QObject *QWebObjectPlugin::create(QWebObjectPluginConnector *connector, const QUrl &url, const QString &mimeType, const QStringList &argumentNames, const QStringList &argumentValues) const |
|
187 |
|
188 Creates a QObject with \a connector to handle \a mimeType. \a argumentNames and \a argumentValues are a set of key-value pairs passed directly |
|
189 from the <param> elements contained in the HTML object tag. |
|
190 */ |
|
191 |
|
192 |