8c249ce7c1b4b7e19aafa2babd6b1ce8fd06bccc
[WebKit-https.git] / WebKit / qt / Api / qwebsettings.cpp
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
23 #include "qwebsettings.h"
24
25 #include "qwebpage.h"
26 #include "qwebpage_p.h"
27
28 #include "Page.h"
29 #include "Settings.h"
30 #include "KURL.h"
31 #include "PlatformString.h"
32 #include "IconDatabase.h"
33
34 #include <QHash>
35 #include <QSharedData>
36
37 class QWebSettingsPrivate
38 {
39 public:
40     QWebSettingsPrivate(WebCore::Settings *wcSettings = 0)
41         : settings(wcSettings)
42     {
43     }
44
45     QHash<int, QString> fontFamilies;
46     QHash<int, int> fontSizes;
47     QHash<int, bool> attributes;
48     QString userStyleSheetLocation;
49
50     void apply();
51     WebCore::Settings *settings;
52 };
53
54 typedef QHash<int, QPixmap> WebGraphicHash;
55 Q_GLOBAL_STATIC(WebGraphicHash, graphics)
56
57 Q_GLOBAL_STATIC(QList<QWebSettingsPrivate *>, allSettings);
58
59 void QWebSettingsPrivate::apply()
60 {
61     if (settings) {
62         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
63
64         QString family = fontFamilies.value(QWebSettings::StandardFont,
65                                             global->fontFamilies.value(QWebSettings::StandardFont));
66         settings->setStandardFontFamily(family);
67
68         family = fontFamilies.value(QWebSettings::FixedFont,
69                                     global->fontFamilies.value(QWebSettings::FixedFont));
70         settings->setFixedFontFamily(family);
71
72         family = fontFamilies.value(QWebSettings::SerifFont,
73                                     global->fontFamilies.value(QWebSettings::SerifFont));
74         settings->setSerifFontFamily(family);
75
76         family = fontFamilies.value(QWebSettings::SansSerifFont,
77                                     global->fontFamilies.value(QWebSettings::SansSerifFont));
78         settings->setSansSerifFontFamily(family);
79
80         family = fontFamilies.value(QWebSettings::CursiveFont,
81                                     global->fontFamilies.value(QWebSettings::CursiveFont));
82         settings->setCursiveFontFamily(family);
83
84         family = fontFamilies.value(QWebSettings::FantasyFont,
85                                     global->fontFamilies.value(QWebSettings::FantasyFont));
86         settings->setFantasyFontFamily(family);
87
88         int size = fontSizes.value(QWebSettings::MinimumFontSize,
89                                    global->fontSizes.value(QWebSettings::MinimumFontSize));
90         settings->setMinimumFontSize(size);
91
92         size = fontSizes.value(QWebSettings::MinimumLogicalFontSize,
93                                    global->fontSizes.value(QWebSettings::MinimumLogicalFontSize));
94         settings->setMinimumLogicalFontSize(size);
95
96         size = fontSizes.value(QWebSettings::DefaultFontSize,
97                                    global->fontSizes.value(QWebSettings::DefaultFontSize));
98         settings->setDefaultFontSize(size);
99
100         size = fontSizes.value(QWebSettings::DefaultFixedFontSize,
101                                    global->fontSizes.value(QWebSettings::DefaultFixedFontSize));
102         settings->setDefaultFixedFontSize(size);
103
104         bool value = attributes.value(QWebSettings::AutoLoadImages,
105                                       global->attributes.value(QWebSettings::AutoLoadImages));
106         settings->setLoadsImagesAutomatically(value);
107
108         value = attributes.value(QWebSettings::JavascriptEnabled,
109                                       global->attributes.value(QWebSettings::JavascriptEnabled));
110         settings->setJavaScriptEnabled(value);
111
112         value = attributes.value(QWebSettings::JavascriptCanOpenWindows,
113                                       global->attributes.value(QWebSettings::JavascriptCanOpenWindows));
114         settings->setJavaScriptCanOpenWindowsAutomatically(value);
115
116         value = attributes.value(QWebSettings::JavaEnabled,
117                                       global->attributes.value(QWebSettings::JavaEnabled));
118         settings->setJavaEnabled(value);
119
120         value = attributes.value(QWebSettings::PluginsEnabled,
121                                       global->attributes.value(QWebSettings::PluginsEnabled));
122         settings->setPluginsEnabled(value);
123
124         value = attributes.value(QWebSettings::PrivateBrowsingEnabled,
125                                       global->attributes.value(QWebSettings::PrivateBrowsingEnabled));
126         settings->setPrivateBrowsingEnabled(value);
127
128         value = attributes.value(QWebSettings::JavascriptCanAccessClipboard,
129                                       global->attributes.value(QWebSettings::JavascriptCanAccessClipboard));
130         settings->setDOMPasteAllowed(value);
131
132         QString location = (!userStyleSheetLocation.isEmpty()) ? userStyleSheetLocation : global->userStyleSheetLocation;
133         settings->setUserStyleSheetLocation(WebCore::KURL(location));
134     } else {
135         QList<QWebSettingsPrivate *> settings = *::allSettings();
136         for (int i = 0; i < settings.count(); ++i)
137             settings[i]->apply();
138     }
139 }
140
141 QWebSettings *QWebSettings::defaultSettings()
142 {
143     static QWebSettings *global = 0;
144     if (!global)
145         global = new QWebSettings;
146     return global;
147 }
148
149 QWebSettings::QWebSettings()
150     : d(new QWebSettingsPrivate)
151 {
152     // Initialize our global defaults
153     // changing any of those will likely break the LayoutTests
154     d->fontSizes.insert(QWebSettings::MinimumFontSize, 5);
155     d->fontSizes.insert(QWebSettings::MinimumLogicalFontSize, 5);
156     d->fontSizes.insert(QWebSettings::DefaultFontSize, 14);
157     d->fontSizes.insert(QWebSettings::DefaultFixedFontSize, 14);
158     d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
159     d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
160     d->fontFamilies.insert(QWebSettings::FixedFont, QLatin1String("Courier"));
161     d->fontFamilies.insert(QWebSettings::SerifFont, QLatin1String("Times New Roman"));
162     d->fontFamilies.insert(QWebSettings::SansSerifFont, QLatin1String("Arial"));
163
164     d->attributes.insert(QWebSettings::AutoLoadImages, true);
165     d->attributes.insert(QWebSettings::JavascriptEnabled, true);
166 }
167
168 QWebSettings::QWebSettings(WebCore::Settings *settings)
169     : d(new QWebSettingsPrivate(settings))
170 {
171     d->settings = settings;
172     d->apply();
173     allSettings()->append(d);
174 }
175
176 QWebSettings::~QWebSettings()
177 {
178     if (d->settings)
179         allSettings()->removeOne(d);
180 }
181
182 void QWebSettings::setFontSize(FontSize type, int size)
183 {
184     d->fontSizes.insert(type, size);
185     d->apply();
186 }
187
188 int QWebSettings::fontSize(FontSize type) const
189 {
190     int defaultValue = 0;
191     if (d->settings) {
192         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
193         defaultValue = global->fontSizes.value(type);
194     }
195     return d->fontSizes.value(type, defaultValue);
196 }
197
198 void QWebSettings::resetFontSize(FontSize type)
199 {
200     d->fontSizes.remove(type);
201     d->apply();
202 }
203
204 void QWebSettings::setUserStyleSheetLocation(const QString &location)
205 {
206     d->userStyleSheetLocation = location;
207     d->apply();
208 }
209
210 QString QWebSettings::userStyleSheetLocation() const
211 {
212     return d->userStyleSheetLocation;
213 }
214
215 void QWebSettings::setIconDatabaseEnabled(bool enabled, const QString &location)
216 {
217     WebCore::iconDatabase()->setEnabled(enabled);
218     if (enabled) {
219       if (!location.isEmpty()) {
220           WebCore::iconDatabase()->open(location);
221       } else {
222           WebCore::iconDatabase()->open(WebCore::iconDatabase()->defaultDatabaseFilename());
223       }
224     } else {
225       WebCore::iconDatabase()->close();
226     }
227 }
228
229 bool QWebSettings::iconDatabaseEnabled()
230 {
231     return WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen();
232 }
233
234 void QWebSettings::setWebGraphic(WebGraphic type, const QPixmap &graphic)
235 {
236     WebGraphicHash *h = graphics();
237     if (graphic.isNull())
238         h->remove(type);
239     else
240         h->insert(type, graphic);
241 }
242
243 QPixmap QWebSettings::webGraphic(WebGraphic type)
244 {
245     return graphics()->value(type);
246 }
247
248 void QWebSettings::setFontFamily(FontType type, const QString &family)
249 {
250     d->fontFamilies.insert(type, family);
251     d->apply();
252 }
253
254 QString QWebSettings::fontFamily(FontType type) const
255 {
256     QString defaultValue;
257     if (d->settings) {
258         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
259         defaultValue = global->fontFamilies.value(type);
260     }
261     return d->fontFamilies.value(type, defaultValue);
262 }
263
264 void QWebSettings::resetFontFamily(FontType type)
265 {
266     d->fontFamilies.remove(type);
267     d->apply();
268 }
269
270 void QWebSettings::setAttribute(WebAttribute attr, bool on)
271 {
272     d->attributes.insert(attr, on);
273     d->apply();
274 }
275
276 bool QWebSettings::testAttribute(WebAttribute attr) const
277 {
278     bool defaultValue = false;
279     if (d->settings) {
280         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
281         defaultValue = global->attributes.value(attr);
282     }
283     return d->attributes.value(attr, defaultValue);
284 }
285
286 void QWebSettings::clearAttribute(WebAttribute attr)
287 {
288     d->attributes.remove(attr);
289     d->apply();
290 }
291