Changed the getter functions in QWebSettings to transparently resolve against the...
[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         QString location = (!userStyleSheetLocation.isEmpty()) ? userStyleSheetLocation : global->userStyleSheetLocation;
129         settings->setUserStyleSheetLocation(WebCore::KURL(location));
130     } else {
131         QList<QWebSettingsPrivate *> settings = *::allSettings();
132         for (int i = 0; i < settings.count(); ++i)
133             settings[i]->apply();
134     }
135 }
136
137 QWebSettings *QWebSettings::defaultSettings()
138 {
139     static QWebSettings *global = 0;
140     if (!global)
141         global = new QWebSettings;
142     return global;
143 }
144
145 QWebSettings::QWebSettings()
146     : d(new QWebSettingsPrivate)
147 {
148     // Initialize our global defaults
149     // changing any of those will likely break the LayoutTests
150     d->fontSizes.insert(QWebSettings::MinimumFontSize, 5);
151     d->fontSizes.insert(QWebSettings::MinimumLogicalFontSize, 5);
152     d->fontSizes.insert(QWebSettings::DefaultFontSize, 14);
153     d->fontSizes.insert(QWebSettings::DefaultFixedFontSize, 14);
154     d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
155     d->fontFamilies.insert(QWebSettings::StandardFont, QLatin1String("Arial"));
156     d->fontFamilies.insert(QWebSettings::FixedFont, QLatin1String("Courier"));
157     d->fontFamilies.insert(QWebSettings::SerifFont, QLatin1String("Times New Roman"));
158     d->fontFamilies.insert(QWebSettings::SansSerifFont, QLatin1String("Arial"));
159
160     d->attributes.insert(QWebSettings::AutoLoadImages, true);
161     d->attributes.insert(QWebSettings::JavascriptEnabled, true);
162 }
163
164 QWebSettings::QWebSettings(WebCore::Settings *settings)
165     : d(new QWebSettingsPrivate(settings))
166 {
167     d->settings = settings;
168     d->apply();
169     allSettings()->append(d);
170 }
171
172 QWebSettings::~QWebSettings()
173 {
174     if (d->settings)
175         allSettings()->removeOne(d);
176 }
177
178 void QWebSettings::setFontSize(FontSize type, int size)
179 {
180     d->fontSizes.insert(type, size);
181     d->apply();
182 }
183
184 int QWebSettings::fontSize(FontSize type) const
185 {
186     int defaultValue = 0;
187     if (d->settings) {
188         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
189         defaultValue = global->fontSizes.value(type);
190     }
191     return d->fontSizes.value(type, defaultValue);
192 }
193
194 void QWebSettings::resetFontSize(FontSize type)
195 {
196     d->fontSizes.remove(type);
197     d->apply();
198 }
199
200 void QWebSettings::setUserStyleSheetLocation(const QString &location)
201 {
202     d->userStyleSheetLocation = location;
203     d->apply();
204 }
205
206 QString QWebSettings::userStyleSheetLocation() const
207 {
208     return d->userStyleSheetLocation;
209 }
210
211 void QWebSettings::setIconDatabaseEnabled(bool enabled, const QString &location)
212 {
213     WebCore::iconDatabase()->setEnabled(enabled);
214     if (enabled) {
215       if (!location.isEmpty()) {
216           WebCore::iconDatabase()->open(location);
217       } else {
218           WebCore::iconDatabase()->open(WebCore::iconDatabase()->defaultDatabaseFilename());
219       }
220     } else {
221       WebCore::iconDatabase()->close();
222     }
223 }
224
225 bool QWebSettings::iconDatabaseEnabled()
226 {
227     return WebCore::iconDatabase()->isEnabled() && WebCore::iconDatabase()->isOpen();
228 }
229
230 void QWebSettings::setWebGraphic(WebGraphic type, const QPixmap &graphic)
231 {
232     WebGraphicHash *h = graphics();
233     if (graphic.isNull())
234         h->remove(type);
235     else
236         h->insert(type, graphic);
237 }
238
239 QPixmap QWebSettings::webGraphic(WebGraphic type)
240 {
241     return graphics()->value(type);
242 }
243
244 void QWebSettings::setFontFamily(FontType type, const QString &family)
245 {
246     d->fontFamilies.insert(type, family);
247     d->apply();
248 }
249
250 QString QWebSettings::fontFamily(FontType type) const
251 {
252     QString defaultValue;
253     if (d->settings) {
254         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
255         defaultValue = global->fontFamilies.value(type);
256     }
257     return d->fontFamilies.value(type, defaultValue);
258 }
259
260 void QWebSettings::resetFontFamily(FontType type)
261 {
262     d->fontFamilies.remove(type);
263     d->apply();
264 }
265
266 void QWebSettings::setAttribute(WebAttribute attr, bool on)
267 {
268     d->attributes.insert(attr, on);
269     d->apply();
270 }
271
272 bool QWebSettings::testAttribute(WebAttribute attr) const
273 {
274     bool defaultValue = false;
275     if (d->settings) {
276         QWebSettingsPrivate *global = QWebSettings::defaultSettings()->d;
277         defaultValue = global->attributes.value(attr);
278     }
279     return d->attributes.value(attr, defaultValue);
280 }
281
282 void QWebSettings::clearAttribute(WebAttribute attr)
283 {
284     d->attributes.remove(attr);
285     d->apply();
286 }
287