1 /*
2 Copyright (C) 2008 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 #include "config.h"
21 #include "qwebsettings.h"
22
23 #include "qwebpage.h"
24 #include "qwebpage_p.h"
25 #include "qwebplugindatabase_p.h"
26
27 #include "Cache.h"
28 #include "CrossOriginPreflightResultCache.h"
29 #include "FontCache.h"
30 #include "Page.h"
31 #include "PageCache.h"
32 #include "Settings.h"
33 #include "KURL.h"
34 #include "PlatformString.h"
35 #include "IconDatabase.h"
36 #include "Image.h"
37 #include "IntSize.h"
38 #include "ApplicationCacheStorage.h"
39 #include "DatabaseTracker.h"
40 #include "FileSystem.h"
41
42 #include <QApplication>
43 #include <QDesktopServices>
44 #include <QDir>
45 #include <QHash>
46 #include <QSharedData>
47 #include <QUrl>
48 #include <QFileInfo>
49
50 #if ENABLE(QT_BEARER)
51 #include "NetworkStateNotifier.h"
52 #endif
53
qt_networkAccessAllowed(bool isAllowed)54 void QWEBKIT_EXPORT qt_networkAccessAllowed(bool isAllowed)
55 {
56 #if ENABLE(QT_BEARER)
57 WebCore::networkStateNotifier().setNetworkAccessAllowed(isAllowed);
58 #endif
59 }
60
61 class QWebSettingsPrivate {
62 public:
QWebSettingsPrivate(WebCore::Settings * wcSettings=0)63 QWebSettingsPrivate(WebCore::Settings* wcSettings = 0)
64 : settings(wcSettings)
65 {
66 }
67
68 QHash<int, QString> fontFamilies;
69 QHash<int, int> fontSizes;
70 QHash<int, bool> attributes;
71 QUrl userStyleSheetLocation;
72 QString defaultTextEncoding;
73 QString localStoragePath;
74 QString offlineWebApplicationCachePath;
75 qint64 offlineStorageDefaultQuota;
76
77 void apply();
78 WebCore::Settings* settings;
79 };
80
81 typedef QHash<int, QPixmap> WebGraphicHash;
Q_GLOBAL_STATIC(WebGraphicHash,_graphics)82 Q_GLOBAL_STATIC(WebGraphicHash, _graphics)
83
84 static WebGraphicHash* graphics()
85 {
86 WebGraphicHash* hash = _graphics();
87
88 if (hash->isEmpty()) {
89 hash->insert(QWebSettings::MissingImageGraphic, QPixmap(QLatin1String(":webkit/resources/missingImage.png")));
90 hash->insert(QWebSettings::MissingPluginGraphic, QPixmap(QLatin1String(":webkit/resources/nullPlugin.png")));
91 hash->insert(QWebSettings::DefaultFrameIconGraphic, QPixmap(QLatin1String(":webkit/resources/urlIcon.png")));
92 hash->insert(QWebSettings::TextAreaSizeGripCornerGraphic, QPixmap(QLatin1String(":webkit/resources/textAreaResizeCorner.png")));
93 hash->insert(QWebSettings::DeleteButtonGraphic, QPixmap(QLatin1String(":webkit/resources/deleteButton.png")));
94 }
95
96 return hash;
97 }
98
99 Q_GLOBAL_STATIC(QList<QWebSettingsPrivate*>, allSettings);
100
apply()101 void QWebSettingsPrivate::apply()
102 {
103 if (settings) {
104 settings->setTextAreasAreResizable(true);
105
106 QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
107
108 QString family = fontFamilies.value(QWebSettings::StandardFont,
109 global->fontFamilies.value(QWebSettings::StandardFont));
110 settings->setStandardFontFamily(family);
111
112 family = fontFamilies.value(QWebSettings::FixedFont,
113 global->fontFamilies.value(QWebSettings::FixedFont));
114 settings->setFixedFontFamily(family);
115
116 family = fontFamilies.value(QWebSettings::SerifFont,
117 global->fontFamilies.value(QWebSettings::SerifFont));
118 settings->setSerifFontFamily(family);
119
120 family = fontFamilies.value(QWebSettings::SansSerifFont,
121 global->fontFamilies.value(QWebSettings::SansSerifFont));
122 settings->setSansSerifFontFamily(family);
123
124 family = fontFamilies.value(QWebSettings::CursiveFont,
125 global->fontFamilies.value(QWebSettings::CursiveFont));
126 settings->setCursiveFontFamily(family);
127
128 family = fontFamilies.value(QWebSettings::FantasyFont,
129 global->fontFamilies.value(QWebSettings::FantasyFont));
130 settings->setFantasyFontFamily(family);
131
132 int size = fontSizes.value(QWebSettings::MinimumFontSize,
133 global->fontSizes.value(QWebSettings::MinimumFontSize));
134 settings->setMinimumFontSize(size);
135
136 size = fontSizes.value(QWebSettings::MinimumLogicalFontSize,
137 global->fontSizes.value(QWebSettings::MinimumLogicalFontSize));
138 settings->setMinimumLogicalFontSize(size);
139
140 size = fontSizes.value(QWebSettings::DefaultFontSize,
141 global->fontSizes.value(QWebSettings::DefaultFontSize));
142 settings->setDefaultFontSize(size);
143
144 size = fontSizes.value(QWebSettings::DefaultFixedFontSize,
145 global->fontSizes.value(QWebSettings::DefaultFixedFontSize));
146 settings->setDefaultFixedFontSize(size);
147
148 bool value = attributes.value(QWebSettings::AutoLoadImages,
149 global->attributes.value(QWebSettings::AutoLoadImages));
150 settings->setLoadsImagesAutomatically(value);
151
152 value = attributes.value(QWebSettings::JavascriptEnabled,
153 global->attributes.value(QWebSettings::JavascriptEnabled));
154 settings->setJavaScriptEnabled(value);
155 #if USE(ACCELERATED_COMPOSITING)
156 value = attributes.value(QWebSettings::AcceleratedCompositingEnabled,
157 global->attributes.value(QWebSettings::AcceleratedCompositingEnabled));
158
159 settings->setAcceleratedCompositingEnabled(value);
160 #endif
161 value = attributes.value(QWebSettings::JavascriptCanOpenWindows,
162 global->attributes.value(QWebSettings::JavascriptCanOpenWindows));
163 settings->setJavaScriptCanOpenWindowsAutomatically(value);
164
165 value = attributes.value(QWebSettings::JavaEnabled,
166 global->attributes.value(QWebSettings::JavaEnabled));
167 settings->setJavaEnabled(value);
168
169 value = attributes.value(QWebSettings::PluginsEnabled,
170 global->attributes.value(QWebSettings::PluginsEnabled));
171 settings->setPluginsEnabled(value);
172
173 value = attributes.value(QWebSettings::PrivateBrowsingEnabled,
174 global->attributes.value(QWebSettings::PrivateBrowsingEnabled));
175 settings->setPrivateBrowsingEnabled(value);
176
177 value = attributes.value(QWebSettings::JavascriptCanAccessClipboard,
178 global->attributes.value(QWebSettings::JavascriptCanAccessClipboard));
179 settings->setDOMPasteAllowed(value);
180
181 value = attributes.value(QWebSettings::DeveloperExtrasEnabled,
182 global->attributes.value(QWebSettings::DeveloperExtrasEnabled));
183 settings->setDeveloperExtrasEnabled(value);
184
185 QUrl location = !userStyleSheetLocation.isEmpty() ? userStyleSheetLocation : global->userStyleSheetLocation;
186 settings->setUserStyleSheetLocation(WebCore::KURL(location));
187
188 QString encoding = !defaultTextEncoding.isEmpty() ? defaultTextEncoding: global->defaultTextEncoding;
189 settings->setDefaultTextEncodingName(encoding);
190
191 QString storagePath = !localStoragePath.isEmpty() ? localStoragePath : global->localStoragePath;
192 settings->setLocalStorageDatabasePath(storagePath);
193
194 value = attributes.value(QWebSettings::ZoomTextOnly,
195 global->attributes.value(QWebSettings::ZoomTextOnly));
196 settings->setZoomsTextOnly(value);
197
198 value = attributes.value(QWebSettings::PrintElementBackgrounds,
199 global->attributes.value(QWebSettings::PrintElementBackgrounds));
200 settings->setShouldPrintBackgrounds(value);
201
202 value = attributes.value(QWebSettings::OfflineStorageDatabaseEnabled,
203 global->attributes.value(QWebSettings::OfflineStorageDatabaseEnabled));
204 settings->setDatabasesEnabled(value);
205
206 value = attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled,
207 global->attributes.value(QWebSettings::OfflineWebApplicationCacheEnabled));
208 settings->setOfflineWebApplicationCacheEnabled(value);
209
210 value = attributes.value(QWebSettings::LocalStorageEnabled,
211 global->attributes.value(QWebSettings::LocalStorageEnabled));
212 settings->setLocalStorageEnabled(value);
213
214 value = attributes.value(QWebSettings::LocalContentCanAccessRemoteUrls,
215 global->attributes.value(QWebSettings::LocalContentCanAccessRemoteUrls));
216 settings->setAllowUniversalAccessFromFileURLs(value);
217
218 value = attributes.value(QWebSettings::XSSAuditorEnabled,
219 global->attributes.value(QWebSettings::XSSAuditorEnabled));
220 settings->setXSSAuditorEnabled(value);
221
222 settings->setUsesPageCache(WebCore::pageCache()->capacity());
223 } else {
224 QList<QWebSettingsPrivate*> settings = *::allSettings();
225 for (int i = 0; i < settings.count(); ++i)
226 settings[i]->apply();
227 }
228 }
229
230 /*!
231 Returns the global settings object.
232
233 Any setting changed on the default object is automatically applied to all
234 QWebPage instances where the particular setting is not overriden already.
235 */
globalSettings()236 QWebSettings* QWebSettings::globalSettings()
237 {
238 static QWebSettings* global = 0;
239 if (!global)
240 global = new QWebSettings;
241 return global;
242 }
243
244 /*!
245 \class QWebSettings
246 \since 4.4
247 \brief The QWebSettings class provides an object to store the settings used
248 by QWebPage and QWebFrame.
249
250 \inmodule QtWebKit
251
252 Each QWebPage object has its own QWebSettings object, which configures the
253 settings for that page. If a setting is not configured, then it is looked
254 up in the global settings object, which can be accessed using
255 globalSettings().
256
257 QWebSettings allows configuration of browser properties, such as font sizes and
258 families, the location of a custom style sheet, and generic attributes like
259 JavaScript and plugins. Individual attributes are set using the setAttribute()
260 function. The \l{QWebSettings::WebAttribute}{WebAttribute} enum further describes
261 each attribute.
262
263 QWebSettings also configures global properties such as the Web page memory
264 cache and the Web page icon database, local database storage and offline
265 applications storage.
266
267 \section1 Enabling Plugins
268
269 Support for browser plugins can enabled by setting the
270 \l{QWebSettings::PluginsEnabled}{PluginsEnabled} attribute. For many applications,
271 this attribute is enabled for all pages by setting it on the
272 \l{globalSettings()}{global settings object}.
273
274 \section1 Web Application Support
275
276 WebKit provides support for features specified in \l{HTML 5} that improve the
277 performance and capabilities of Web applications. These include client-side
278 (offline) storage and the use of a Web application cache.
279
280 Client-side (offline) storage is an improvement over the use of cookies to
281 store persistent data in Web applications. Applications can configure and
282 enable the use of an offline storage database by calling the
283 setOfflineStoragePath() with an appropriate file path, and can limit the quota
284 for each application by calling setOfflineStorageDefaultQuota().
285
286 \sa QWebPage::settings(), QWebView::settings(), {Web Browser}
287 */
288
289 /*!
290 \enum QWebSettings::FontFamily
291
292 This enum describes the generic font families defined by CSS 2.
293 For more information see the
294 \l{http://www.w3.org/TR/REC-CSS2/fonts.html#generic-font-families}{CSS standard}.
295
296 \value StandardFont
297 \value FixedFont
298 \value SerifFont
299 \value SansSerifFont
300 \value CursiveFont
301 \value FantasyFont
302 */
303
304 /*!
305 \enum QWebSettings::FontSize
306
307 This enum describes the font sizes configurable through QWebSettings.
308
309 \value MinimumFontSize The hard minimum font size.
310 \value MinimumLogicalFontSize The minimum logical font size that is applied
311 after zooming with QWebFrame's textSizeMultiplier().
312 \value DefaultFontSize The default font size for regular text.
313 \value DefaultFixedFontSize The default font size for fixed-pitch text.
314 */
315
316 /*!
317 \enum QWebSettings::WebGraphic
318
319 This enums describes the standard graphical elements used in webpages.
320
321 \value MissingImageGraphic The replacement graphic shown when an image could not be loaded.
322 \value MissingPluginGraphic The replacement graphic shown when a plugin could not be loaded.
323 \value DefaultFrameIconGraphic The default icon for QWebFrame::icon().
324 \value TextAreaSizeGripCornerGraphic The graphic shown for the size grip of text areas.
325 */
326
327 /*!
328 \enum QWebSettings::WebAttribute
329
330 This enum describes various attributes that are configurable through QWebSettings.
331
332 \value AutoLoadImages Specifies whether images are automatically loaded in
333 web pages.
334 \value DnsPrefetchEnabled Specifies whether QtWebkit will try to pre-fetch DNS entries to
335 speed up browsing. This only works as a global attribute. Only for Qt 4.6 and later.
336 \value JavascriptEnabled Enables or disables the running of JavaScript
337 programs.
338 \value JavaEnabled Enables or disables Java applets.
339 Currently Java applets are not supported.
340 \value PluginsEnabled Enables or disables plugins in Web pages.
341 \value PrivateBrowsingEnabled Private browsing prevents WebKit from
342 recording visited pages in the history and storing web page icons.
343 \value JavascriptCanOpenWindows Specifies whether JavaScript programs
344 can open new windows.
345 \value JavascriptCanAccessClipboard Specifies whether JavaScript programs
346 can read or write to the clipboard.
347 \value DeveloperExtrasEnabled Enables extra tools for Web developers.
348 Currently this enables the "Inspect" element in the context menu as
349 well as the use of QWebInspector which controls the WebKit WebInspector
350 for web site debugging.
351 \value LinksIncludedInFocusChain Specifies whether hyperlinks should be
352 included in the keyboard focus chain.
353 \value ZoomTextOnly Specifies whether the zoom factor on a frame applies to
354 only the text or all content.
355 \value PrintElementBackgrounds Specifies whether the background color and images
356 are also drawn when the page is printed.
357 \value OfflineStorageDatabaseEnabled Specifies whether support for the HTML 5
358 offline storage feature is enabled or not. Disabled by default.
359 \value OfflineWebApplicationCacheEnabled Specifies whether support for the HTML 5
360 web application cache feature is enabled or not. Disabled by default.
361 \value LocalStorageEnabled Specifies whether support for the HTML 5
362 local storage feature is enabled or not. Disabled by default.
363 \value LocalStorageDatabaseEnabled \e{This enum value is deprecated.} Use
364 QWebSettings::LocalStorageEnabled instead.
365 \value LocalContentCanAccessRemoteUrls Specifies whether locally loaded documents are allowed to access remote urls.
366 \value XSSAuditorEnabled Specifies whether load requests should be monitored for cross-site scripting attempts.
367 */
368
369 /*!
370 \internal
371 */
QWebSettings()372 QWebSettings::QWebSettings()
373 : d(new QWebSettingsPrivate)
374 {
375 // Initialize our global defaults
376 d->fontSizes.insert(QWebSettings::MinimumFontSize, 0);
377 d->fontSizes.insert(QWebSettings::MinimumLogicalFontSize, 0);
378 d->fontSizes.insert(QWebSettings::DefaultFontSize, 16);
379 d->fontSizes.insert(QWebSettings::DefaultFixedFontSize, 13);
380 d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
381 d->fontFamilies.insert(QWebSettings::FixedFont, QLatin1String("Courier New"));
382 d->fontFamilies.insert(QWebSettings::SerifFont, QLatin1String("Times New Roman"));
383 d->fontFamilies.insert(QWebSettings::SansSerifFont, QLatin1String("Arial"));
384 d->fontFamilies.insert(QWebSettings::CursiveFont, QLatin1String("Arial"));
385 d->fontFamilies.insert(QWebSettings::FantasyFont, QLatin1String("Arial"));
386
387 d->attributes.insert(QWebSettings::AutoLoadImages, true);
388 d->attributes.insert(QWebSettings::DnsPrefetchEnabled, false);
389 d->attributes.insert(QWebSettings::JavascriptEnabled, true);
390 d->attributes.insert(QWebSettings::LinksIncludedInFocusChain, true);
391 d->attributes.insert(QWebSettings::ZoomTextOnly, false);
392 d->attributes.insert(QWebSettings::PrintElementBackgrounds, true);
393 d->attributes.insert(QWebSettings::OfflineStorageDatabaseEnabled, false);
394 d->attributes.insert(QWebSettings::OfflineWebApplicationCacheEnabled, false);
395 d->attributes.insert(QWebSettings::LocalStorageEnabled, false);
396 d->attributes.insert(QWebSettings::LocalContentCanAccessRemoteUrls, false);
397 d->attributes.insert(QWebSettings::AcceleratedCompositingEnabled, false);
398 d->offlineStorageDefaultQuota = 5 * 1024 * 1024;
399 d->defaultTextEncoding = QLatin1String("iso-8859-1");
400 }
401
402 /*!
403 \internal
404 */
QWebSettings(WebCore::Settings * settings)405 QWebSettings::QWebSettings(WebCore::Settings* settings)
406 : d(new QWebSettingsPrivate(settings))
407 {
408 d->settings = settings;
409 d->apply();
410 allSettings()->append(d);
411 }
412
413 /*!
414 \internal
415 */
~QWebSettings()416 QWebSettings::~QWebSettings()
417 {
418 if (d->settings)
419 allSettings()->removeAll(d);
420
421 delete d;
422 }
423
424 /*!
425 Sets the font size for \a type to \a size.
426 */
setFontSize(FontSize type,int size)427 void QWebSettings::setFontSize(FontSize type, int size)
428 {
429 d->fontSizes.insert(type, size);
430 d->apply();
431 }
432
433 /*!
434 Returns the default font size for \a type.
435 */
fontSize(FontSize type) const436 int QWebSettings::fontSize(FontSize type) const
437 {
438 int defaultValue = 0;
439 if (d->settings) {
440 QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
441 defaultValue = global->fontSizes.value(type);
442 }
443 return d->fontSizes.value(type, defaultValue);
444 }
445
446 /*!
447 Resets the font size for \a type to the size specified in the global
448 settings object.
449
450 This function has no effect on the global QWebSettings instance.
451 */
resetFontSize(FontSize type)452 void QWebSettings::resetFontSize(FontSize type)
453 {
454 if (d->settings) {
455 d->fontSizes.remove(type);
456 d->apply();
457 }
458 }
459
460 /*!
461 Specifies the location of a user stylesheet to load with every web page.
462
463 The \a location must be either a path on the local filesystem, or a data URL
464 with UTF-8 and Base64 encoded data, such as:
465
466 "data:text/css;charset=utf-8;base64,cCB7IGJhY2tncm91bmQtY29sb3I6IHJlZCB9Ow==;"
467
468 \sa userStyleSheetUrl()
469 */
setUserStyleSheetUrl(const QUrl & location)470 void QWebSettings::setUserStyleSheetUrl(const QUrl& location)
471 {
472 d->userStyleSheetLocation = location;
473 d->apply();
474 }
475
476 /*!
477 Returns the location of the user stylesheet.
478
479 \sa setUserStyleSheetUrl()
480 */
userStyleSheetUrl() const481 QUrl QWebSettings::userStyleSheetUrl() const
482 {
483 return d->userStyleSheetLocation;
484 }
485
486 /*!
487 \since 4.6
488 Specifies the default text encoding system.
489
490 The \a encoding, must be a string describing an encoding such as "utf-8",
491 "iso-8859-1", etc. If left empty a default value will be used. For a more
492 extensive list of encoding names see \l{QTextCodec}
493
494 \sa defaultTextEncoding()
495 */
setDefaultTextEncoding(const QString & encoding)496 void QWebSettings::setDefaultTextEncoding(const QString& encoding)
497 {
498 d->defaultTextEncoding = encoding;
499 d->apply();
500 }
501
502 /*!
503 \since 4.6
504 Returns the default text encoding.
505
506 \sa setDefaultTextEncoding()
507 */
defaultTextEncoding() const508 QString QWebSettings::defaultTextEncoding() const
509 {
510 return d->defaultTextEncoding;
511 }
512
513 /*!
514 Sets the path of the icon database to \a path. The icon database is used
515 to store "favicons" associated with web sites.
516
517 \a path must point to an existing directory where the icons are stored.
518
519 Setting an empty path disables the icon database.
520 */
setIconDatabasePath(const QString & path)521 void QWebSettings::setIconDatabasePath(const QString& path)
522 {
523 WebCore::iconDatabase()->delayDatabaseCleanup();
524
525 if (!path.isEmpty()) {
526 WebCore::iconDatabase()->setEnabled(true);
527 QFileInfo info(path);
528 if (info.isDir() && info.isWritable())
529 WebCore::iconDatabase()->open(path);
530 } else {
531 WebCore::iconDatabase()->setEnabled(false);
532 WebCore::iconDatabase()->close();
533 }
534 }
535
536 /*!
537 Returns the path of the icon database or an empty string if the icon
538 database is disabled.
539
540 \sa setIconDatabasePath(), clearIconDatabase()
541 */
iconDatabasePath()542 QString QWebSettings::iconDatabasePath()
543 {
544 if (WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen())
545 return WebCore::iconDatabase()->databasePath();
546 else
547 return QString();
548 }
549
550 /*!
551 Clears the icon database.
552 */
clearIconDatabase()553 void QWebSettings::clearIconDatabase()
554 {
555 if (WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen())
556 WebCore::iconDatabase()->removeAllIcons();
557 }
558
559 /*!
560 Returns the web site's icon for \a url.
561
562 If the web site does not specify an icon, or the icon is not in the
563 database, a null QIcon is returned.
564
565 \note The returned icon's size is arbitrary.
566
567 \sa setIconDatabasePath()
568 */
iconForUrl(const QUrl & url)569 QIcon QWebSettings::iconForUrl(const QUrl& url)
570 {
571 WebCore::Image* image = WebCore::iconDatabase()->iconForPageURL(WebCore::KURL(url).string(),
572 WebCore::IntSize(16, 16));
573 if (!image)
574 return QPixmap();
575
576 QPixmap* icon = image->nativeImageForCurrentFrame();
577 if (!icon)
578 return QPixmap();
579
580 return* icon;
581 }
582
583 /*
584 Returns the plugin database object.
585
586 QWebPluginDatabase *QWebSettings::pluginDatabase()
587 {
588 static QWebPluginDatabase* database = 0;
589 if (!database)
590 database = new QWebPluginDatabase();
591 return database;
592 }
593 */
594
595 /*!
596 Sets \a graphic to be drawn when QtWebKit needs to draw an image of the
597 given \a type.
598
599 For example, when an image cannot be loaded the pixmap specified by
600 \l{QWebSettings::WebGraphic}{MissingImageGraphic} is drawn instead.
601
602 \sa webGraphic()
603 */
setWebGraphic(WebGraphic type,const QPixmap & graphic)604 void QWebSettings::setWebGraphic(WebGraphic type, const QPixmap& graphic)
605 {
606 WebGraphicHash* h = graphics();
607 if (graphic.isNull())
608 h->remove(type);
609 else
610 h->insert(type, graphic);
611 }
612
613 /*!
614 Returns a previously set pixmap used to draw replacement graphics of the
615 specified \a type.
616
617 For example, when an image cannot be loaded the pixmap specified by
618 \l{QWebSettings::WebGraphic}{MissingImageGraphic} is drawn instead.
619
620 \sa setWebGraphic()
621 */
webGraphic(WebGraphic type)622 QPixmap QWebSettings::webGraphic(WebGraphic type)
623 {
624 return graphics()->value(type);
625 }
626
627 /*!
628 Frees up as much memory as possible by cleaning all memory caches such
629 as page, object and font cache.
630
631 \since 4.6
632 */
clearMemoryCaches()633 void QWebSettings::clearMemoryCaches()
634 {
635 // Turn the cache on and off. Disabling the object cache will remove all
636 // resources from the cache. They may still live on if they are referenced
637 // by some Web page though.
638 if (!WebCore::cache()->disabled()) {
639 WebCore::cache()->setDisabled(true);
640 WebCore::cache()->setDisabled(false);
641 }
642
643 int pageCapacity = WebCore::pageCache()->capacity();
644 // Setting size to 0, makes all pages be released.
645 WebCore::pageCache()->setCapacity(0);
646 WebCore::pageCache()->releaseAutoreleasedPagesNow();
647 WebCore::pageCache()->setCapacity(pageCapacity);
648
649 // Invalidating the font cache and freeing all inactive font data.
650 WebCore::fontCache()->invalidate();
651
652 // Empty the Cross-Origin Preflight cache
653 WebCore::CrossOriginPreflightResultCache::shared().empty();
654 }
655
656 /*!
657 Sets the maximum number of pages to hold in the memory page cache to \a pages.
658
659 The Page Cache allows for a nicer user experience when navigating forth or back
660 to pages in the forward/back history, by pausing and resuming up to \a pages
661 per page group.
662
663 For more information about the feature, please refer to:
664
665 http://webkit.org/blog/427/webkit-page-cache-i-the-basics/
666 */
setMaximumPagesInCache(int pages)667 void QWebSettings::setMaximumPagesInCache(int pages)
668 {
669 QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
670 WebCore::pageCache()->setCapacity(qMax(0, pages));
671 global->apply();
672 }
673
674 /*!
675 Returns the maximum number of web pages that are kept in the memory cache.
676 */
maximumPagesInCache()677 int QWebSettings::maximumPagesInCache()
678 {
679 return WebCore::pageCache()->capacity();
680 }
681
682 /*!
683 Specifies the capacities for the memory cache for dead objects such as
684 stylesheets or scripts.
685
686 The \a cacheMinDeadCapacity specifies the \e minimum number of bytes that
687 dead objects should consume when the cache is under pressure.
688
689 \a cacheMaxDead is the \e maximum number of bytes that dead objects should
690 consume when the cache is \bold not under pressure.
691
692 \a totalCapacity specifies the \e maximum number of bytes that the cache
693 should consume \bold overall.
694
695 The cache is enabled by default. Calling setObjectCacheCapacities(0, 0, 0)
696 will disable the cache. Calling it with one non-zero enables it again.
697 */
setObjectCacheCapacities(int cacheMinDeadCapacity,int cacheMaxDead,int totalCapacity)698 void QWebSettings::setObjectCacheCapacities(int cacheMinDeadCapacity, int cacheMaxDead, int totalCapacity)
699 {
700 bool disableCache = !cacheMinDeadCapacity && !cacheMaxDead && !totalCapacity;
701 WebCore::cache()->setDisabled(disableCache);
702
703 WebCore::cache()->setCapacities(qMax(0, cacheMinDeadCapacity),
704 qMax(0, cacheMaxDead),
705 qMax(0, totalCapacity));
706 }
707
708 /*!
709 Sets the actual font family to \a family for the specified generic family,
710 \a which.
711 */
setFontFamily(FontFamily which,const QString & family)712 void QWebSettings::setFontFamily(FontFamily which, const QString& family)
713 {
714 d->fontFamilies.insert(which, family);
715 d->apply();
716 }
717
718 /*!
719 Returns the actual font family for the specified generic font family,
720 \a which.
721 */
fontFamily(FontFamily which) const722 QString QWebSettings::fontFamily(FontFamily which) const
723 {
724 QString defaultValue;
725 if (d->settings) {
726 QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
727 defaultValue = global->fontFamilies.value(which);
728 }
729 return d->fontFamilies.value(which, defaultValue);
730 }
731
732 /*!
733 Resets the actual font family to the default font family, specified by
734 \a which.
735
736 This function has no effect on the global QWebSettings instance.
737 */
resetFontFamily(FontFamily which)738 void QWebSettings::resetFontFamily(FontFamily which)
739 {
740 if (d->settings) {
741 d->fontFamilies.remove(which);
742 d->apply();
743 }
744 }
745
746 /*!
747 \fn void QWebSettings::setAttribute(WebAttribute attribute, bool on)
748
749 Enables or disables the specified \a attribute feature depending on the
750 value of \a on.
751 */
setAttribute(WebAttribute attr,bool on)752 void QWebSettings::setAttribute(WebAttribute attr, bool on)
753 {
754 d->attributes.insert(attr, on);
755 d->apply();
756 }
757
758 /*!
759 \fn bool QWebSettings::testAttribute(WebAttribute attribute) const
760
761 Returns true if \a attribute is enabled; otherwise returns false.
762 */
testAttribute(WebAttribute attr) const763 bool QWebSettings::testAttribute(WebAttribute attr) const
764 {
765 bool defaultValue = false;
766 if (d->settings) {
767 QWebSettingsPrivate* global = QWebSettings::globalSettings()->d;
768 defaultValue = global->attributes.value(attr);
769 }
770 return d->attributes.value(attr, defaultValue);
771 }
772
773 /*!
774 \fn void QWebSettings::resetAttribute(WebAttribute attribute)
775
776 Resets the setting of \a attribute.
777 This function has no effect on the global QWebSettings instance.
778
779 \sa globalSettings()
780 */
resetAttribute(WebAttribute attr)781 void QWebSettings::resetAttribute(WebAttribute attr)
782 {
783 if (d->settings) {
784 d->attributes.remove(attr);
785 d->apply();
786 }
787 }
788
789 /*!
790 \since 4.5
791
792 Sets the path for HTML5 offline storage to \a path.
793
794 \a path must point to an existing directory where the databases are stored.
795
796 Setting an empty path disables the feature.
797
798 \sa offlineStoragePath()
799 */
setOfflineStoragePath(const QString & path)800 void QWebSettings::setOfflineStoragePath(const QString& path)
801 {
802 #if ENABLE(DATABASE)
803 WebCore::DatabaseTracker::tracker().setDatabaseDirectoryPath(path);
804 #endif
805 }
806
807 /*!
808 \since 4.5
809
810 Returns the path of the HTML5 offline storage or an empty string if the
811 feature is disabled.
812
813 \sa setOfflineStoragePath()
814 */
offlineStoragePath()815 QString QWebSettings::offlineStoragePath()
816 {
817 #if ENABLE(DATABASE)
818 return WebCore::DatabaseTracker::tracker().databaseDirectoryPath();
819 #else
820 return QString();
821 #endif
822 }
823
824 /*!
825 \since 4.5
826
827 Sets the value of the default quota for new offline storage databases
828 to \a maximumSize.
829 */
setOfflineStorageDefaultQuota(qint64 maximumSize)830 void QWebSettings::setOfflineStorageDefaultQuota(qint64 maximumSize)
831 {
832 QWebSettings::globalSettings()->d->offlineStorageDefaultQuota = maximumSize;
833 }
834
835 /*!
836 \since 4.5
837
838 Returns the value of the default quota for new offline storage databases.
839 */
offlineStorageDefaultQuota()840 qint64 QWebSettings::offlineStorageDefaultQuota()
841 {
842 return QWebSettings::globalSettings()->d->offlineStorageDefaultQuota;
843 }
844
845 /*!
846 \since 4.6
847 \relates QWebSettings
848
849 Sets the path for HTML5 offline web application cache storage to \a path.
850
851 An application cache acts like an HTTP cache in some sense. For documents
852 that use the application cache via JavaScript, the loader mechinery will
853 first ask the application cache for the contents, before hitting the
854 network.
855
856 The feature is described in details at:
857 http://dev.w3.org/html5/spec/Overview.html#appcache
858
859 \a path must point to an existing directory where the cache is stored.
860
861 Setting an empty path disables the feature.
862
863 \sa offlineWebApplicationCachePath()
864 */
setOfflineWebApplicationCachePath(const QString & path)865 void QWebSettings::setOfflineWebApplicationCachePath(const QString& path)
866 {
867 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
868 WebCore::cacheStorage().setCacheDirectory(path);
869 #endif
870 }
871
872 /*!
873 \since 4.6
874 \relates QWebSettings
875
876 Returns the path of the HTML5 offline web application cache storage
877 or an empty string if the feature is disabled.
878
879 \sa setOfflineWebApplicationCachePath()
880 */
offlineWebApplicationCachePath()881 QString QWebSettings::offlineWebApplicationCachePath()
882 {
883 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
884 return WebCore::cacheStorage().cacheDirectory();
885 #else
886 return QString();
887 #endif
888 }
889
890 /*!
891 \since 4.6
892
893 Sets the value of the quota for the offline web application cache
894 to \a maximumSize.
895 */
setOfflineWebApplicationCacheQuota(qint64 maximumSize)896 void QWebSettings::setOfflineWebApplicationCacheQuota(qint64 maximumSize)
897 {
898 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
899 WebCore::cacheStorage().setMaximumSize(maximumSize);
900 #endif
901 }
902
903 /*!
904 \since 4.6
905
906 Returns the value of the quota for the offline web application cache.
907 */
offlineWebApplicationCacheQuota()908 qint64 QWebSettings::offlineWebApplicationCacheQuota()
909 {
910 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
911 return WebCore::cacheStorage().maximumSize();
912 #else
913 return 0;
914 #endif
915 }
916
917 /*!
918 \since 4.6
919 \relates QWebSettings
920
921 Sets the path for HTML5 local storage to \a path.
922
923 For more information on HTML5 local storage see the
924 \l{http://www.w3.org/TR/webstorage/#the-localstorage-attribute}{Web Storage standard}.
925
926 Support for local storage can enabled by setting the
927 \l{QWebSettings::LocalStorageEnabled}{LocalStorageEnabled} attribute.
928
929 \sa localStoragePath()
930 */
setLocalStoragePath(const QString & path)931 void QWebSettings::setLocalStoragePath(const QString& path)
932 {
933 d->localStoragePath = path;
934 d->apply();
935 }
936
937 /*!
938 \since 4.6
939 \relates QWebSettings
940
941 Returns the path for HTML5 local storage.
942
943 \sa setLocalStoragePath()
944 */
localStoragePath() const945 QString QWebSettings::localStoragePath() const
946 {
947 return d->localStoragePath;
948 }
949
950 /*!
951 \since 4.6
952 \relates QWebSettings
953
954 Enables WebKit persistent data and sets the path to \a path.
955 If the \a path is empty the path for persistent data is set to the
956 user-specific data location specified by
957 \l{QDesktopServices::DataLocation}{DataLocation}.
958
959 \sa localStoragePath()
960 */
enablePersistentStorage(const QString & path)961 void QWebSettings::enablePersistentStorage(const QString& path)
962 {
963 QString storagePath;
964
965 if (path.isEmpty()) {
966 storagePath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
967
968 if (storagePath.isEmpty())
969 storagePath = WebCore::pathByAppendingComponent(QDir::homePath(), QCoreApplication::applicationName());
970 } else
971 storagePath = path;
972
973 WebCore::makeAllDirectories(storagePath);
974
975 QWebSettings::setIconDatabasePath(storagePath);
976 QWebSettings::setOfflineWebApplicationCachePath(storagePath);
977 QWebSettings::setOfflineStoragePath(WebCore::pathByAppendingComponent(storagePath, "Databases"));
978 QWebSettings::globalSettings()->setLocalStoragePath(WebCore::pathByAppendingComponent(storagePath, "LocalStorage"));
979 QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
980 QWebSettings::globalSettings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
981 QWebSettings::globalSettings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
982 }
983
984 /*!
985 \fn QWebSettingsPrivate* QWebSettings::handle() const
986 \internal
987 */
988