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