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