2008-01-22 Christian Dywan <christian@imendio.com>
[WebKit-https.git] / WebKit / gtk / WebView / webkitwebsettings.cpp
1 /*
2  * Copyright (C) 2008 Christian Dywan <christian@imendio.com>
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
22 #include "webkitwebsettings.h"
23 #include "webkitprivate.h"
24
25 extern "C" {
26
27 G_DEFINE_TYPE(WebKitWebSettings, webkit_web_settings, G_TYPE_OBJECT)
28
29 struct _WebKitWebSettingsPrivate {
30     gchar* default_encoding;
31     gchar* cursive_font_family;
32     gchar* default_font_family;
33     gchar* fantasy_font_family;
34     gchar* monospace_font_family;
35     gchar* sans_serif_font_family;
36     gchar* serif_font_family;
37     guint default_font_size;
38     guint default_monospace_font_size;
39     guint minimum_font_size;
40     guint minimum_logical_font_size;
41     gboolean auto_load_images;
42     gboolean auto_shrink_images;
43     gboolean print_backgrounds;
44     gboolean enable_scripts;
45     gboolean enable_plugins;
46     gboolean resizable_text_areas;
47     gchar* user_stylesheet_uri;
48 };
49
50 #define WEBKIT_WEB_SETTINGS_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_SETTINGS, WebKitWebSettingsPrivate))
51
52 enum {
53     PROP_0,
54
55     PROP_DEFAULT_ENCODING,
56     PROP_CURSIVE_FONT_FAMILY,
57     PROP_DEFAULT_FONT_FAMILY,
58     PROP_FANTASY_FONT_FAMILY,
59     PROP_MONOSPACE_FONT_FAMILY,
60     PROP_SANS_SERIF_FONT_FAMILY,
61     PROP_SERIF_FONT_FAMILY,
62     PROP_DEFAULT_FONT_SIZE,
63     PROP_DEFAULT_MONOSPACE_FONT_SIZE,
64     PROP_MINIMUM_FONT_SIZE,
65     PROP_MINIMUM_LOGICAL_FONT_SIZE,
66     PROP_AUTO_LOAD_IMAGES,
67     PROP_AUTO_SHRINK_IMAGES,
68     PROP_PRINT_BACKGROUNDS,
69     PROP_ENABLE_SCRIPTS,
70     PROP_ENABLE_PLUGINS,
71     PROP_RESIZABLE_TEXT_AREAS,
72     PROP_USER_STYLESHEET_URI
73 };
74
75 static void webkit_web_settings_finalize(GObject* object);
76
77 static void webkit_web_settings_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
78
79 static void webkit_web_settings_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
80
81 static void webkit_web_settings_class_init(WebKitWebSettingsClass* klass)
82 {
83     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
84     gobject_class->finalize = webkit_web_settings_finalize;
85     gobject_class->set_property = webkit_web_settings_set_property;
86     gobject_class->get_property = webkit_web_settings_get_property;
87
88     GParamFlags flags = (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT);
89
90     g_object_class_install_property(gobject_class,
91                                     PROP_DEFAULT_ENCODING,
92                                     g_param_spec_string(
93                                     "default-encoding",
94                                     "Default Encoding",
95                                     "The default encoding used to display text.",
96                                     "iso-8859-1",
97                                     flags));
98
99     g_object_class_install_property(gobject_class,
100                                     PROP_CURSIVE_FONT_FAMILY,
101                                     g_param_spec_string(
102                                     "cursive-font-family",
103                                     "Cursive Font Family",
104                                     "The default Cursive font family used to display text.",
105                                     "serif",
106                                     flags));
107
108     g_object_class_install_property(gobject_class,
109                                     PROP_DEFAULT_FONT_FAMILY,
110                                     g_param_spec_string(
111                                     "default-font-family",
112                                     "Default Font Family",
113                                     "The default font family used to display text.",
114                                     "sans-serif",
115                                     flags));
116
117     g_object_class_install_property(gobject_class,
118                                     PROP_FANTASY_FONT_FAMILY,
119                                     g_param_spec_string(
120                                     "fantasy-font-family",
121                                     "Fantasy Font Family",
122                                     "The default Fantasy font family used to display text.",
123                                     "serif",
124                                     flags));
125
126     g_object_class_install_property(gobject_class,
127                                     PROP_MONOSPACE_FONT_FAMILY,
128                                     g_param_spec_string(
129                                     "monospace-font-family",
130                                     "Monospace Font Family",
131                                     "The default font family used to display monospace text.",
132                                     "monospace",
133                                     flags));
134
135     g_object_class_install_property(gobject_class,
136                                     PROP_SANS_SERIF_FONT_FAMILY,
137                                     g_param_spec_string(
138                                     "sans-serif-font-family",
139                                     "Sans Serif Font Family",
140                                     "The default Sans Serif font family used to display text.",
141                                     "sans-serif",
142                                     flags));
143
144     g_object_class_install_property(gobject_class,
145                                     PROP_SERIF_FONT_FAMILY,
146                                     g_param_spec_string(
147                                     "serif-font-family",
148                                     "Serif Font Family",
149                                     "The default Serif font family used to display text.",
150                                     "serif",
151                                     flags));
152
153     g_object_class_install_property(gobject_class,
154                                     PROP_DEFAULT_FONT_SIZE,
155                                     g_param_spec_int(
156                                     "default-font-size",
157                                     "Default Font Size",
158                                     "The default font size used to display text.",
159                                     5, G_MAXINT, 10,
160                                     flags));
161
162     g_object_class_install_property(gobject_class,
163                                     PROP_DEFAULT_MONOSPACE_FONT_SIZE,
164                                     g_param_spec_int(
165                                     "default-monospace-font-size",
166                                     "Default Monospace Font Size",
167                                     "The default font size used to display monospace text.",
168                                     5, G_MAXINT, 10,
169                                     flags));
170
171     g_object_class_install_property(gobject_class,
172                                     PROP_MINIMUM_FONT_SIZE,
173                                     g_param_spec_int(
174                                     "minimum-font-size",
175                                     "Minimum Font Size",
176                                     "The minimum font size used to display text.",
177                                     1, G_MAXINT, 5,
178                                     flags));
179
180     g_object_class_install_property(gobject_class,
181                                     PROP_MINIMUM_LOGICAL_FONT_SIZE,
182                                     g_param_spec_int(
183                                     "minimum-logical-font-size",
184                                     "Minimum Logical Font Size",
185                                     "The minimum logical font size used to display text.",
186                                     1, G_MAXINT, 5,
187                                     flags));
188
189     g_object_class_install_property(gobject_class,
190                                     PROP_AUTO_LOAD_IMAGES,
191                                     g_param_spec_boolean(
192                                     "auto-load-images",
193                                     "Auto Load Images",
194                                     "Load images automatically.",
195                                     TRUE,
196                                     flags));
197
198     g_object_class_install_property(gobject_class,
199                                     PROP_AUTO_SHRINK_IMAGES,
200                                     g_param_spec_boolean(
201                                     "auto-shrink-images",
202                                     "Auto Shrink Images",
203                                     "Automatically shrink standalone images to fit.",
204                                     TRUE,
205                                     flags));
206
207     g_object_class_install_property(gobject_class,
208                                     PROP_PRINT_BACKGROUNDS,
209                                     g_param_spec_boolean(
210                                     "print-backgrounds",
211                                     "Print Backgrounds",
212                                     "Whether background images should be printed.",
213                                     TRUE,
214                                     flags));
215
216     g_object_class_install_property(gobject_class,
217                                     PROP_ENABLE_SCRIPTS,
218                                     g_param_spec_boolean(
219                                     "enable-scripts",
220                                     "Enable Scripts",
221                                     "Enable embedded scripting languages.",
222                                     TRUE,
223                                     flags));
224
225     g_object_class_install_property(gobject_class,
226                                     PROP_ENABLE_PLUGINS,
227                                     g_param_spec_boolean(
228                                     "enable-plugins",
229                                     "Enable Plugins",
230                                     "Enable embedded plugin objects.",
231                                     TRUE,
232                                     flags));
233
234     g_object_class_install_property(gobject_class,
235                                     PROP_RESIZABLE_TEXT_AREAS,
236                                     g_param_spec_boolean(
237                                     "resizable-text-areas",
238                                     "Resizable Text Areas",
239                                     "Whether text areas are resizable.",
240                                     TRUE,
241                                     flags));
242
243     g_object_class_install_property(gobject_class,
244                                     PROP_USER_STYLESHEET_URI,
245                                     g_param_spec_string("user-stylesheet-uri",
246                                     "User Stylesheet URI",
247                                     "The URI of a stylesheet that is applied to every page.",
248                                     0,
249                                     flags));
250
251     g_type_class_add_private(klass, sizeof(WebKitWebSettingsPrivate));
252 }
253
254 static void webkit_web_settings_init(WebKitWebSettings* web_settings)
255 {
256     web_settings->priv = WEBKIT_WEB_SETTINGS_GET_PRIVATE(web_settings);
257 }
258
259 static void webkit_web_settings_finalize(GObject* object)
260 {
261     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
262     WebKitWebSettingsPrivate* priv = web_settings->priv;
263
264     g_free(priv->default_encoding);
265     g_free(priv->cursive_font_family);
266     g_free(priv->default_font_family);
267     g_free(priv->fantasy_font_family);
268     g_free(priv->monospace_font_family);
269     g_free(priv->sans_serif_font_family);
270     g_free(priv->serif_font_family);
271     g_free(priv->user_stylesheet_uri);
272
273     G_OBJECT_CLASS(webkit_web_settings_parent_class)->finalize(object);
274 }
275
276 static void webkit_web_settings_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
277 {
278     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
279     WebKitWebSettingsPrivate* priv = web_settings->priv;
280
281     switch(prop_id) {
282     case PROP_DEFAULT_ENCODING:
283         g_free(priv->default_encoding);
284         priv->default_encoding = g_strdup(g_value_get_string(value));
285         break;
286     case PROP_CURSIVE_FONT_FAMILY:
287         g_free(priv->cursive_font_family);
288         priv->cursive_font_family = g_strdup(g_value_get_string(value));
289         break;
290     case PROP_DEFAULT_FONT_FAMILY:
291         g_free(priv->default_font_family);
292         priv->default_font_family = g_strdup(g_value_get_string(value));
293         break;
294     case PROP_FANTASY_FONT_FAMILY:
295         g_free(priv->fantasy_font_family);
296         priv->fantasy_font_family = g_strdup(g_value_get_string(value));
297         break;
298     case PROP_MONOSPACE_FONT_FAMILY:
299         g_free(priv->monospace_font_family);
300         priv->monospace_font_family = g_strdup(g_value_get_string(value));
301         break;
302     case PROP_SANS_SERIF_FONT_FAMILY:
303         g_free(priv->sans_serif_font_family);
304         priv->sans_serif_font_family = g_strdup(g_value_get_string(value));
305         break;
306     case PROP_SERIF_FONT_FAMILY:
307         g_free(priv->serif_font_family);
308         priv->serif_font_family = g_strdup(g_value_get_string(value));
309         break;
310     case PROP_DEFAULT_FONT_SIZE:
311         priv->default_font_size = g_value_get_int(value);
312         break;
313     case PROP_DEFAULT_MONOSPACE_FONT_SIZE:
314         priv->default_monospace_font_size = g_value_get_int(value);
315         break;
316     case PROP_MINIMUM_FONT_SIZE:
317         priv->minimum_font_size = g_value_get_int(value);
318         break;
319     case PROP_MINIMUM_LOGICAL_FONT_SIZE:
320         priv->minimum_logical_font_size = g_value_get_int(value);
321         break;
322     case PROP_AUTO_LOAD_IMAGES:
323         priv->auto_load_images = g_value_get_boolean(value);
324         break;
325     case PROP_AUTO_SHRINK_IMAGES:
326         priv->auto_shrink_images = g_value_get_boolean(value);
327         break;
328     case PROP_PRINT_BACKGROUNDS:
329         priv->print_backgrounds = g_value_get_boolean(value);
330         break;
331     case PROP_ENABLE_SCRIPTS:
332         priv->enable_scripts = g_value_get_boolean(value);
333         break;
334     case PROP_ENABLE_PLUGINS:
335         priv->enable_plugins = g_value_get_boolean(value);
336         break;
337     case PROP_RESIZABLE_TEXT_AREAS:
338         priv->resizable_text_areas = g_value_get_boolean(value);
339         break;
340     case PROP_USER_STYLESHEET_URI:
341         g_free(priv->user_stylesheet_uri);
342         priv->user_stylesheet_uri = g_strdup(g_value_get_string(value));
343         break;
344     default:
345         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
346         break;
347     }
348 }
349
350 static void webkit_web_settings_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
351 {
352     WebKitWebSettings* web_settings = WEBKIT_WEB_SETTINGS(object);
353     WebKitWebSettingsPrivate* priv = web_settings->priv;
354
355     switch (prop_id) {
356     case PROP_DEFAULT_ENCODING:
357         g_value_set_string(value, priv->default_encoding);
358         break;
359     case PROP_CURSIVE_FONT_FAMILY:
360         g_value_set_string(value, priv->cursive_font_family);
361         break;
362     case PROP_DEFAULT_FONT_FAMILY:
363         g_value_set_string(value, priv->default_font_family);
364         break;
365     case PROP_FANTASY_FONT_FAMILY:
366         g_value_set_string(value, priv->fantasy_font_family);
367         break;
368     case PROP_MONOSPACE_FONT_FAMILY:
369         g_value_set_string(value, priv->monospace_font_family);
370         break;
371     case PROP_SANS_SERIF_FONT_FAMILY:
372         g_value_set_string(value, priv->sans_serif_font_family);
373         break;
374     case PROP_SERIF_FONT_FAMILY:
375         g_value_set_string(value, priv->serif_font_family);
376         break;
377     case PROP_DEFAULT_FONT_SIZE:
378         g_value_set_int(value, priv->default_font_size);
379         break;
380     case PROP_DEFAULT_MONOSPACE_FONT_SIZE:
381         g_value_set_int(value, priv->default_monospace_font_size);
382         break;
383     case PROP_MINIMUM_FONT_SIZE:
384         g_value_set_int(value, priv->minimum_font_size);
385         break;
386     case PROP_MINIMUM_LOGICAL_FONT_SIZE:
387         g_value_set_int(value, priv->minimum_logical_font_size);
388         break;
389     case PROP_AUTO_LOAD_IMAGES:
390         g_value_set_boolean(value, priv->auto_load_images);
391         break;
392     case PROP_AUTO_SHRINK_IMAGES:
393         g_value_set_boolean(value, priv->auto_shrink_images);
394         break;
395     case PROP_PRINT_BACKGROUNDS:
396         g_value_set_boolean(value, priv->print_backgrounds);
397         break;
398     case PROP_ENABLE_SCRIPTS:
399         g_value_set_boolean(value, priv->enable_scripts);
400         break;
401     case PROP_ENABLE_PLUGINS:
402         g_value_set_boolean(value, priv->enable_plugins);
403         break;
404     case PROP_RESIZABLE_TEXT_AREAS:
405         g_value_set_boolean(value, priv->resizable_text_areas);
406         break;
407     case PROP_USER_STYLESHEET_URI:
408         g_value_set_string(value, priv->user_stylesheet_uri);
409         break;
410     default:
411         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
412         break;
413     }
414 }
415
416 /**
417  * webkit_web_settings_new:
418  *
419  * Creates a new #WebKitWebSettings instance with default values. It must
420  * be manually attached to a WebView.
421  *
422  * Returns: a new #WebKitWebSettings instance
423  **/
424 WebKitWebSettings* webkit_web_settings_new()
425 {
426     return WEBKIT_WEB_SETTINGS(g_object_new(WEBKIT_TYPE_WEB_SETTINGS, NULL));
427 }
428
429 /**
430  * webkit_web_settings_copy:
431  *
432  * Copies an existing #WebKitWebSettings instance.
433  *
434  * Returns: a new #WebKitWebSettings instance
435  **/
436 WebKitWebSettings* webkit_web_settings_copy(WebKitWebSettings* web_settings)
437 {
438     WebKitWebSettingsPrivate* priv = web_settings->priv;
439
440     WebKitWebSettings* copy = WEBKIT_WEB_SETTINGS(g_object_new(WEBKIT_TYPE_WEB_SETTINGS,
441                  "default-encoding", priv->default_encoding,
442                  "cursive-font-family", priv->cursive_font_family,
443                  "default-font-family", priv->default_font_family,
444                  "fantasy-font-family", priv->fantasy_font_family,
445                  "monospace-font-family", priv->monospace_font_family,
446                  "sans-serif-font-family", priv->sans_serif_font_family,
447                  "serif-font-family", priv->serif_font_family,
448                  "default-font-size", priv->default_font_size,
449                  "default-monospace-font-size", priv->default_monospace_font_size,
450                  "minimum-font-size", priv->minimum_font_size,
451                  "minimum-logical-font-size", priv->minimum_logical_font_size,
452                  "auto-load-images", priv->auto_load_images,
453                  "auto-shrink-images", priv->auto_shrink_images,
454                  "print-backgrounds", priv->print_backgrounds,
455                  "enable-scripts", priv->enable_scripts,
456                  "enable-plugins", priv->enable_plugins,
457                  "resizable-text-areas", priv->resizable_text_areas,
458                  "user-stylesheet-uri", priv->user_stylesheet_uri,
459                  NULL));
460
461     return copy;
462 }
463
464 }