2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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 "qwebsettings.h"
24 #include "qwebpage_p.h"
28 #include "PageCache.h"
31 #include "PlatformString.h"
32 #include "IconDatabase.h"
35 #include "ApplicationCacheStorage.h"
36 #include "DatabaseTracker.h"
39 #include <QSharedData>
43 class QWebSettingsPrivate
46 QWebSettingsPrivate(WebCore::Settings *wcSettings = 0)
47 : settings(wcSettings)
51 QHash<int, QString> fontFamilies;
52 QHash<int, int> fontSizes;
53 QHash<int, bool> attributes;
54 QUrl userStyleSheetLocation;
55 QString localStorageDatabasePath;
56 QString offlineWebApplicationCachePath;
57 qint64 offlineStorageDefaultQuota;
60 WebCore::Settings *settings;
63 typedef QHash<int, QPixmap> WebGraphicHash;
64 Q_GLOBAL_STATIC(WebGraphicHash, _graphics)
66 static WebGraphicHash* graphics()
68 WebGraphicHash* hash = _graphics();
70 if (hash->isEmpty()) {
71 hash->insert(QWebSettings::MissingImageGraphic, QPixmap(QLatin1String(":webkit/resources/missingImage.png")));
72 hash->insert(QWebSettings::MissingPluginGraphic, QPixmap(QLatin1String(":webkit/resources/nullPlugin.png")));
73 hash->insert(QWebSettings::DefaultFrameIconGraphic, QPixmap(QLatin1String(":webkit/resources/urlIcon.png")));
74 hash->insert(QWebSettings::TextAreaSizeGripCornerGraphic, QPixmap(QLatin1String(":webkit/resources/textAreaResizeCorner.png")));
80 Q_GLOBAL_STATIC(QList<QWebSettingsPrivate *>, allSettings);
82 void QWebSettingsPrivate::apply()
85 settings->setTextAreasAreResizable(true);
87 QWebSettingsPrivate *global = QWebSettings::globalSettings()->d;
89 QString family = fontFamilies.value(QWebSettings::StandardFont,
90 global->fontFamilies.value(QWebSettings::StandardFont));
91 settings->setStandardFontFamily(family);
93 family = fontFamilies.value(QWebSettings::FixedFont,
94 global->fontFamilies.value(QWebSettings::FixedFont));
95 settings->setFixedFontFamily(family);
97 family = fontFamilies.value(QWebSettings::SerifFont,
98 global->fontFamilies.value(QWebSettings::SerifFont));
99 settings->setSerifFontFamily(family);
101 family = fontFamilies.value(QWebSettings::SansSerifFont,
102 global->fontFamilies.value(QWebSettings::SansSerifFont));
103 settings->setSansSerifFontFamily(family);
105 family = fontFamilies.value(QWebSettings::CursiveFont,
106 global->fontFamilies.value(QWebSettings::CursiveFont));
107 settings->setCursiveFontFamily(family);
109 family = fontFamilies.value(QWebSettings::FantasyFont,
110 global->fontFamilies.value(QWebSettings::FantasyFont));
111 settings->setFantasyFontFamily(family);
113 int size = fontSizes.value(QWebSettings::MinimumFontSize,
114 global->fontSizes.value(QWebSettings::MinimumFontSize));
115 settings->setMinimumFontSize(size);
117 size = fontSizes.value(QWebSettings::MinimumLogicalFontSize,
118 global->fontSizes.value(QWebSettings::MinimumLogicalFontSize));
119 settings->setMinimumLogicalFontSize(size);
121 size = fontSizes.value(QWebSettings::DefaultFontSize,
122 global->fontSizes.value(QWebSettings::DefaultFontSize));
123 settings->setDefaultFontSize(size);
125 size = fontSizes.value(QWebSettings::DefaultFixedFontSize,
126 global->fontSizes.value(QWebSettings::DefaultFixedFontSize));
127 settings->setDefaultFixedFontSize(size);
129 bool value = attributes.value(QWebSettings::AutoLoadImages,
130 global->attributes.value(QWebSettings::AutoLoadImages));
131 settings->setLoadsImagesAutomatically(value);
133 value = attributes.value(QWebSettings::JavascriptEnabled,
134 global->attributes.value(QWebSettings::JavascriptEnabled));
135 settings->setJavaScriptEnabled(value);
137 value = attributes.value(QWebSettings::JavascriptCanOpenWindows,
138 global->attributes.value(QWebSettings::JavascriptCanOpenWindows));
139 settings->setJavaScriptCanOpenWindowsAutomatically(value);
141 value = attributes.value(QWebSettings::JavaEnabled,
142 global->attributes.value(QWebSettings::JavaEnabled));
143 settings->setJavaEnabled(value);
145 value = attributes.value(QWebSettings::PluginsEnabled,
146 global->attributes.value(QWebSettings::PluginsEnabled));
147 settings->setPluginsEnabled(value);
149 value = attributes.value(QWebSettings::PrivateBrowsingEnabled,
150 global->attributes.value(QWebSettings::PrivateBrowsingEnabled));
151 settings->setPrivateBrowsingEnabled(value);
153 value = attributes.value(QWebSettings::JavascriptCanAccessClipboard,
154 global->attributes.value(QWebSettings::JavascriptCanAccessClipboard));
155 settings->setDOMPasteAllowed(value);
157 value = attributes.value(QWebSettings::DeveloperExtrasEnabled,
158 global->attributes.value(QWebSettings::DeveloperExtrasEnabled));
159 settings->setDeveloperExtrasEnabled(value);
161 QUrl location = !userStyleSheetLocation.isEmpty() ? userStyleSheetLocation : global->userStyleSheetLocation;
162 settings->setUserStyleSheetLocation(WebCore::KURL(location));
164 QString localStoragePath = !localStorageDatabasePath.isEmpty() ? localStorageDatabasePath : global->localStorageDatabasePath;
165 settings->setLocalStorageDatabasePath(localStoragePath);
167 value = attributes.value(QWebSettings::ZoomTextOnly,
168 global->attributes.value(QWebSettings::ZoomTextOnly));
169 settings->setZoomsTextOnly(value);
171 value = attributes.value(QWebSettings::PrintElementBackgrounds,
172 global->attributes.value(QWebSettings::PrintElementBackgrounds));
173 settings->setShouldPrintBackgrounds(value);
175 value = attributes.value(QWebSettings::OfflineStorageDatabaseEnabled,
176 global->attributes.value(QWebSettings::OfflineStorageDatabaseEnabled));
177 settings->setDatabasesEnabled(value);
179 value = attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled,
180 global->attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled));
181 settings->setOfflineWebApplicationCacheEnabled(value);
183 value = attributes.value(QWebSettings::LocalStorageDatabaseEnabled,
184 global->attributes.value(QWebSettings::LocalStorageDatabaseEnabled));
185 settings->setLocalStorageEnabled(value);
187 QList<QWebSettingsPrivate *> settings = *::allSettings();
188 for (int i = 0; i < settings.count(); ++i)
189 settings[i]->apply();
194 Returns the global settings object.
196 Any setting changed on the default object is automatically applied to all
197 QWebPage instances where the particular setting is not overriden already.
199 QWebSettings *QWebSettings::globalSettings()
201 static QWebSettings *global = 0;
203 global = new QWebSettings;
210 \brief The QWebSettings class provides an object to store the settings used
211 by QWebPage and QWebFrame.
213 Each QWebPage object has its own QWebSettings object, which configures the
214 settings for that page. If a setting is not configured, then it is looked
215 up in the global settings object, which can be accessed using
216 QWebSettings::globalSettings().
218 QWebSettings allows configuring font properties such as font size and font
219 family, the location of a custom stylesheet, and generic attributes like java
220 script, plugins, etc. The \l{QWebSettings::WebAttribute}{WebAttribute}
221 enum further describes this.
223 QWebSettings also configures global properties such as the web page memory
224 cache and the web page icon database, local database storage and offline
225 applications storage.
227 \section1 Web Application Support
229 WebKit provides support for features specified in \l{HTML 5} that improve the
230 performance and capabilities of Web applications. These include client-side
231 (offline) storage and the use of a Web application cache.
233 Client-side (offline) storage is an improvement over the use of cookies to
234 store persistent data in Web applications. Applications can configure and
235 enable the use of an offline storage database by calling the
236 setOfflineStoragePath() with an appropriate file path, and can limit the quota
237 for each application by calling setOfflineStorageDefaultQuota().
239 \sa QWebPage::settings(), QWebView::settings(), {Browser}
243 \enum QWebSettings::FontFamily
245 This enum describes the generic font families defined by CSS 2.
246 For more information see the
247 \l{http://www.w3.org/TR/REC-CSS2/fonts.html#generic-font-families}{CSS standard}.
258 \enum QWebSettings::FontSize
260 This enum describes the font sizes configurable through QWebSettings.
262 \value MinimumFontSize The hard minimum font size.
263 \value MinimumLogicalFontSize The minimum logical font size that is applied
264 after zooming with QWebFrame's textSizeMultiplier().
265 \value DefaultFontSize The default font size for regular text.
266 \value DefaultFixedFontSize The default font size for fixed-pitch text.
270 \enum QWebSettings::WebGraphic
272 This enums describes the standard graphical elements used in webpages.
274 \value MissingImageGraphic The replacement graphic shown when an image could not be loaded.
275 \value MissingPluginGraphic The replacement graphic shown when a plugin could not be loaded.
276 \value DefaultFrameIconGraphic The default icon for QWebFrame::icon().
277 \value TextAreaSizeGripCornerGraphic The graphic shown for the size grip of text areas.
281 \enum QWebSettings::WebAttribute
283 This enum describes various attributes that are configurable through QWebSettings.
285 \value AutoLoadImages Specifies whether images are automatically loaded in
287 \value JavascriptEnabled Enables or disables the running of JavaScript
289 \value JavaEnabled Enables or disables Java applets.
290 Currently Java applets are not supported.
291 \value PluginsEnabled Enables or disables plugins in web pages.
292 Currently Flash and other plugins are not supported.
293 \value PrivateBrowsingEnabled Private browsing prevents WebKit from
294 recording visited pages in the history and storing web page icons.
295 \value JavascriptCanOpenWindows Specifies whether JavaScript programs
296 can open new windows.
297 \value JavascriptCanAccessClipboard Specifies whether JavaScript programs
298 can read or write to the clipboard.
299 \value DeveloperExtrasEnabled Enables extra tools for Web developers.
300 Currently this enables the "Inspect" element in the context menu,
301 which shows the WebKit WebInspector for web site debugging.
302 \value LinksIncludedInFocusChain Specifies whether hyperlinks should be
303 included in the keyboard focus chain.
304 \value ZoomTextOnly Specifies whether the zoom factor on a frame applies to
305 only the text or all content.
306 \value PrintElementBackgrounds Specifies whether the background color and images
307 are also drawn when the page is printed.
308 \value OfflineStorageDatabaseEnabled Specifies whether support for the HTML 5
309 offline storage feature is enabled or not.
310 \value OfflineWebApplicationCacheEnabled Specifies whether support for the HTML 5
311 web application cache feature is enabled or not.
312 \value LocalStorageDatabaseEnabled Specifies whether support for the HTML 5
313 local storage feature is enabled or not.
319 QWebSettings::QWebSettings()
320 : d(new QWebSettingsPrivate)
322 // Initialize our global defaults
323 d->fontSizes.insert(QWebSettings::MinimumFontSize, 0);
324 d->fontSizes.insert(QWebSettings::MinimumLogicalFontSize, 0);
325 d->fontSizes.insert(QWebSettings::DefaultFontSize, 14);
326 d->fontSizes.insert(QWebSettings::DefaultFixedFontSize, 14);
327 d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
328 d->fontFamilies.insert(QWebSettings::FixedFont, QLatin1String("Courier New"));
329 d->fontFamilies.insert(QWebSettings::SerifFont, QLatin1String("Times New Roman"));
330 d->fontFamilies.insert(QWebSettings::SansSerifFont, QLatin1String("Arial"));
331 d->fontFamilies.insert(QWebSettings::CursiveFont, QLatin1String("Arial"));
332 d->fontFamilies.insert(QWebSettings::FantasyFont, QLatin1String("Arial"));
334 d->attributes.insert(QWebSettings::AutoLoadImages, true);
335 d->attributes.insert(QWebSettings::JavascriptEnabled, true);
336 d->attributes.insert(QWebSettings::LinksIncludedInFocusChain, true);
337 d->attributes.insert(QWebSettings::ZoomTextOnly, false);
338 d->attributes.insert(QWebSettings::PrintElementBackgrounds, true);
339 d->attributes.insert(QWebSettings::OfflineStorageDatabaseEnabled, true);
340 d->attributes.insert(QWebSettings::OfflineWebApplicationCacheEnabled, true);
341 d->attributes.insert(QWebSettings::LocalStorageDatabaseEnabled, true);
342 d->offlineStorageDefaultQuota = 5 * 1024 * 1024;
349 QWebSettings::QWebSettings(WebCore::Settings *settings)
350 : d(new QWebSettingsPrivate(settings))
352 d->settings = settings;
354 allSettings()->append(d);
360 QWebSettings::~QWebSettings()
363 allSettings()->removeAll(d);
369 Sets the font size for \a type to \a size.
371 void QWebSettings::setFontSize(FontSize type, int size)
373 d->fontSizes.insert(type, size);
378 Returns the default font size for \a type.
380 int QWebSettings::fontSize(FontSize type) const
382 int defaultValue = 0;
384 QWebSettingsPrivate *global = QWebSettings::globalSettings()->d;
385 defaultValue = global->fontSizes.value(type);
387 return d->fontSizes.value(type, defaultValue);
391 Resets the font size for \a type to the size specified in the global
394 This function has no effect on the global QWebSettings instance.
396 void QWebSettings::resetFontSize(FontSize type)
399 d->fontSizes.remove(type);
405 Specifies the location of a user stylesheet to load with every web page.
407 The \a location can be a URL or a path on the local filesystem.
409 \sa userStyleSheetUrl()
411 void QWebSettings::setUserStyleSheetUrl(const QUrl &location)
413 d->userStyleSheetLocation = location;
418 Returns the location of the user stylesheet.
420 \sa setUserStyleSheetUrl()
422 QUrl QWebSettings::userStyleSheetUrl() const
424 return d->userStyleSheetLocation;
428 Sets the path of the icon database to \a path. The icon database is used
429 to store "favicons" associated with web sites.
431 \a path must point to an existing directory where the icons are stored.
433 Setting an empty path disables the icon database.
435 void QWebSettings::setIconDatabasePath(const QString &path)
437 WebCore::iconDatabase()->delayDatabaseCleanup();
439 if (!path.isEmpty()) {
440 WebCore::iconDatabase()->setEnabled(true);
441 QFileInfo info(path);
442 if (info.isDir() && info.isWritable())
443 WebCore::iconDatabase()->open(path);
445 WebCore::iconDatabase()->setEnabled(false);
446 WebCore::iconDatabase()->close();
451 Returns the path of the icon database or an empty string if the icon
452 database is disabled.
454 \sa setIconDatabasePath(), clearIconDatabase()
456 QString QWebSettings::iconDatabasePath()
458 if (WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen()) {
459 return WebCore::iconDatabase()->databasePath();
466 Clears the icon database.
468 void QWebSettings::clearIconDatabase()
470 if (WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen())
471 WebCore::iconDatabase()->removeAllIcons();
475 Returns the web site's icon for \a url.
477 If the web site does not specify an icon, or the icon is not in the
478 database, a null QIcon is returned.
480 \note The returned icon's size is arbitrary.
482 \sa setIconDatabasePath()
484 QIcon QWebSettings::iconForUrl(const QUrl &url)
486 WebCore::Image* image = WebCore::iconDatabase()->iconForPageURL(WebCore::KURL(url).string(),
487 WebCore::IntSize(16, 16));
491 QPixmap *icon = image->nativeImageForCurrentFrame();
499 Sets \a graphic to be drawn when QtWebKit needs to draw an image of the
502 For example, when an image cannot be loaded the pixmap specified by
503 \l{QWebSettings::WebGraphic}{MissingImageGraphic} is drawn instead.
507 void QWebSettings::setWebGraphic(WebGraphic type, const QPixmap &graphic)
509 WebGraphicHash *h = graphics();
510 if (graphic.isNull())
513 h->insert(type, graphic);
517 Returns a previously set pixmap used to draw replacement graphics of the
520 For example, when an image cannot be loaded the pixmap specified by
521 \l{QWebSettings::WebGraphic}{MissingImageGraphic} is drawn instead.
525 QPixmap QWebSettings::webGraphic(WebGraphic type)
527 return graphics()->value(type);
531 Sets the maximum number of pages to hold in the memory cache to \a pages.
533 void QWebSettings::setMaximumPagesInCache(int pages)
535 WebCore::pageCache()->setCapacity(qMax(0, pages));
539 Returns the maximum number of web pages that are kept in the memory cache.
541 int QWebSettings::maximumPagesInCache()
543 return WebCore::pageCache()->capacity();
547 Specifies the capacities for the memory cache for dead objects such as
548 stylesheets or scripts.
550 The \a cacheMinDeadCapacity specifies the \e minimum number of bytes that
551 dead objects should consume when the cache is under pressure.
553 \a cacheMaxDead is the \e maximum number of bytes that dead objects should
554 consume when the cache is \bold not under pressure.
556 \a totalCapacity specifies the \e maximum number of bytes that the cache
557 should consume \bold overall.
559 The cache is enabled by default. Calling setObjectCacheCapacities(0, 0, 0)
560 will disable the cache. Calling it with one non-zero enables it again.
562 void QWebSettings::setObjectCacheCapacities(int cacheMinDeadCapacity, int cacheMaxDead, int totalCapacity)
564 bool disableCache = cacheMinDeadCapacity == 0 && cacheMaxDead == 0 && totalCapacity == 0;
565 WebCore::cache()->setDisabled(disableCache);
567 WebCore::cache()->setCapacities(qMax(0, cacheMinDeadCapacity),
568 qMax(0, cacheMaxDead),
569 qMax(0, totalCapacity));
573 Sets the actual font family to \a family for the specified generic family,
576 void QWebSettings::setFontFamily(FontFamily which, const QString &family)
578 d->fontFamilies.insert(which, family);
583 Returns the actual font family for the specified generic font family,
586 QString QWebSettings::fontFamily(FontFamily which) const
588 QString defaultValue;
590 QWebSettingsPrivate *global = QWebSettings::globalSettings()->d;
591 defaultValue = global->fontFamilies.value(which);
593 return d->fontFamilies.value(which, defaultValue);
597 Resets the actual font family to the default font family, specified by
600 This function has no effect on the global QWebSettings instance.
602 void QWebSettings::resetFontFamily(FontFamily which)
605 d->fontFamilies.remove(which);
611 \fn void QWebSettings::setAttribute(WebAttribute attribute, bool on)
613 Enables or disables the specified \a attribute feature depending on the
616 void QWebSettings::setAttribute(WebAttribute attr, bool on)
618 d->attributes.insert(attr, on);
623 \fn bool QWebSettings::testAttribute(WebAttribute attribute) const
625 Returns true if \a attribute is enabled; otherwise returns false.
627 bool QWebSettings::testAttribute(WebAttribute attr) const
629 bool defaultValue = false;
631 QWebSettingsPrivate *global = QWebSettings::globalSettings()->d;
632 defaultValue = global->attributes.value(attr);
634 return d->attributes.value(attr, defaultValue);
638 \fn void QWebSettings::resetAttribute(WebAttribute attribute)
640 Resets the setting of \a attribute.
641 This function has no effect on the global QWebSettings instance.
645 void QWebSettings::resetAttribute(WebAttribute attr)
648 d->attributes.remove(attr);
656 Sets the path for HTML5 offline storage to \a path.
658 \a path must point to an existing directory where the databases are stored.
660 Setting an empty path disables the feature.
662 \sa offlineStoragePath()
664 void QWebSettings::setOfflineStoragePath(const QString& path)
667 WebCore::DatabaseTracker::tracker().setDatabaseDirectoryPath(path);
674 Returns the path of the HTML5 offline storage or an empty string if the
677 \sa setOfflineStoragePath()
679 QString QWebSettings::offlineStoragePath()
682 return WebCore::DatabaseTracker::tracker().databaseDirectoryPath();
691 Sets the value of the default quota for new offline storage databases
694 void QWebSettings::setOfflineStorageDefaultQuota(qint64 maximumSize)
696 QWebSettings::globalSettings()->d->offlineStorageDefaultQuota = maximumSize;
702 Returns the value of the default quota for new offline storage databases.
704 qint64 QWebSettings::offlineStorageDefaultQuota()
706 return QWebSettings::globalSettings()->d->offlineStorageDefaultQuota;
711 \relates QWebSettings
713 Sets the path for HTML5 offline web application cache storage to \a path.
715 \a path must point to an existing directory where the cache is stored.
717 Setting an empty path disables the feature.
719 \sa offlineWebApplicationCachePath()
721 void QWEBKIT_EXPORT qt_websettings_setOfflineWebApplicationCachePath(const QString& path)
723 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
724 WebCore::cacheStorage().setCacheDirectory(path);
730 \relates QWebSettings
732 Returns the path of the HTML5 offline web application cache storage
733 or an empty string if the feature is disabled.
735 \sa setOfflineWebApplicationCachePath()
737 QString QWEBKIT_EXPORT qt_websettings_offlineWebApplicationCachePath()
739 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
740 return WebCore::cacheStorage().cacheDirectory();
748 \relates QWebSettings
750 Sets the path for HTML5 local storage databases to \a path.
752 \a path must point to an existing directory where the cache is stored.
754 Setting an empty path disables the feature.
756 \sa localStorageDatabasePath()
758 void QWEBKIT_EXPORT qt_websettings_setLocalStorageDatabasePath(QWebSettings* settings, const QString& path)
760 QWebSettingsPrivate *d = settings->handle();
761 d->localStorageDatabasePath = path;
767 \relates QWebSettings
769 Returns the path for HTML5 local storage databases
770 or an empty string if the feature is disabled.
772 \sa setLocalStorageDatabasePath()
774 QString QWEBKIT_EXPORT qt_websettings_localStorageDatabasePath(QWebSettings* settings)
776 return settings->handle()->localStorageDatabasePath;
780 \fn QWebSettingsPrivate* QWebSettings::handle() const