Title: [89249] trunk/Tools
Revision
89249
Author
[email protected]
Date
2011-06-20 09:25:10 -0700 (Mon, 20 Jun 2011)

Log Message

2011-06-20  Lukasz Slachciak  <[email protected]>

        Reviewed by Martin Robinson.

        [GTK] General mechanism for adjusting WebKitWebSettings in GtkLauncher.
        https://bugs.webkit.org/show_bug.cgi?id=55308

        * GtkLauncher/main.c: Added general mechanism for adjusting WebKitWebSettings.
        (parseOptionEntryCallback): Callback for parsing option entry.
        (getOptionEntriesFromWebKitWebSettings): Basing on the WebKitWebSettings properties prepare
        list of option entries.
        (transformStringToBoolean): Transform function for boolean convertion used by parseOptionEntryCallback.
        (transformStringToInt): Transform function for int convertion used by parseOptionEntryCallback.
        (transformStringToFloat): Transform function for float convertion used by parseOptionEntryCallback.
        (parseAdditionalOptions): Help function parsing additional commandline options.
        (main): Added call to parseAdditionalOptions.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (89248 => 89249)


--- trunk/Tools/ChangeLog	2011-06-20 15:50:29 UTC (rev 89248)
+++ trunk/Tools/ChangeLog	2011-06-20 16:25:10 UTC (rev 89249)
@@ -1,3 +1,20 @@
+2011-06-20  Lukasz Slachciak  <[email protected]>
+
+        Reviewed by Martin Robinson.
+
+        [GTK] General mechanism for adjusting WebKitWebSettings in GtkLauncher.
+        https://bugs.webkit.org/show_bug.cgi?id=55308
+
+        * GtkLauncher/main.c: Added general mechanism for adjusting WebKitWebSettings.
+        (parseOptionEntryCallback): Callback for parsing option entry.
+        (getOptionEntriesFromWebKitWebSettings): Basing on the WebKitWebSettings properties prepare
+        list of option entries.
+        (transformStringToBoolean): Transform function for boolean convertion used by parseOptionEntryCallback.
+        (transformStringToInt): Transform function for int convertion used by parseOptionEntryCallback.
+        (transformStringToFloat): Transform function for float convertion used by parseOptionEntryCallback.
+        (parseAdditionalOptions): Help function parsing additional commandline options.
+        (main): Added call to parseAdditionalOptions.
+
 2011-06-20  Viatcheslav Ostapenko  <[email protected]>
 
         Reviewed by Andreas Kling.

Modified: trunk/Tools/GtkLauncher/main.c (89248 => 89249)


--- trunk/Tools/GtkLauncher/main.c	2011-06-20 15:50:29 UTC (rev 89248)
+++ trunk/Tools/GtkLauncher/main.c	2011-06-20 16:25:10 UTC (rev 89249)
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2006, 2007 Apple Inc.
  * Copyright (C) 2007 Alp Toker <[email protected]>
+ * Copyright (C) 2011 Lukasz Slachciak
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -25,6 +26,8 @@
  */
 
 #include <gtk/gtk.h>
+#include <stdlib.h>
+#include <string.h>
 #include <webkit/webkit.h>
 
 static gint windowCount = 0;
@@ -236,6 +239,135 @@
     return fileURL;
 }
 
+gboolean parseOptionEntryCallback(const gchar *optionNameFull, const gchar *value, gpointer data, GError **error)
+{
+    WebKitWebSettings *webkitSettings = (WebKitWebSettings *)data;
+
+    g_assert(webkitSettings);
+
+    if (strlen(optionNameFull) <= 2)
+        return FALSE;
+
+    /* We have two -- in option name so remove them. */
+    const gchar *optionName = optionNameFull + 2;
+    GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(webkitSettings), optionName);
+    if (!spec)
+        return FALSE;
+
+    /* Convert string to proper type. */
+    GValue valueString = {0, {{0}}};
+    GValue valueProperty = {0, {{0}}};
+    g_value_init(&valueString, G_TYPE_STRING);
+    g_value_init(&valueProperty, G_PARAM_SPEC_VALUE_TYPE(spec));
+    g_value_set_static_string(&valueString, value);
+    if (!g_value_transform(&valueString, &valueProperty))
+        return FALSE;
+
+    /* Set WebKitWebSettings properties. */
+    g_object_set_property(G_OBJECT(webkitSettings), optionName, &valueProperty);
+
+    return TRUE;
+}
+
+static GArray* getOptionEntriesFromWebKitWebSettings(WebKitWebSettings *webkitSettings)
+{
+    GParamSpec **propertySpecs;
+    GArray *optionEntriesArray;
+    guint numProperties, i;
+
+    propertySpecs = g_object_class_list_properties(G_OBJECT_GET_CLASS(webkitSettings), &numProperties);
+    if (!propertySpecs)
+        return 0;
+
+    optionEntriesArray = g_array_new(TRUE, TRUE, sizeof(GOptionEntry));
+    if (!optionEntriesArray) {
+        g_free(propertySpecs);
+        return 0;
+    }
+
+    for (i = 0; i < numProperties; i++) {
+        GParamSpec *param = propertySpecs[i];
+
+        /* Fill in structures only for writable properties. */
+        if (!param || !(param->flags & G_PARAM_WRITABLE))
+            continue;
+
+        GType gParamType = G_PARAM_SPEC_VALUE_TYPE(param);
+        if (gParamType == G_TYPE_BOOLEAN || gParamType == G_TYPE_STRING || gParamType == G_TYPE_INT
+            || gParamType == G_TYPE_FLOAT) {
+            GOptionEntry optionEntry;
+            optionEntry.long_name = g_param_spec_get_name(param);
+            /* There is no easy way to figure our short name for generated option entries.
+               optionEntry.short_name=*/
+            /* For bool arguments "enable" type make option argument not required. */
+            if (gParamType == G_TYPE_BOOLEAN && (strstr(optionEntry.long_name, "enable")))
+                optionEntry.flags = G_OPTION_FLAG_OPTIONAL_ARG;
+            optionEntry.arg = G_OPTION_ARG_CALLBACK;
+            optionEntry.arg_data = parseOptionEntryCallback;
+            optionEntry.description = g_param_spec_get_blurb(param);
+            optionEntry.arg_description = g_type_name(gParamType);
+            g_array_append_val(optionEntriesArray, optionEntry);
+        }
+    }
+    g_free(propertySpecs);
+
+    return optionEntriesArray;
+}
+
+static void transformStringToBoolean(const GValue *srcValue, GValue *destValue)
+{
+    const char* strValue = g_value_get_string(srcValue);
+    if (strValue) {
+        if (!g_ascii_strcasecmp(strValue, "true") || !strcmp(strValue, "1"))
+            g_value_set_boolean(destValue, TRUE);
+        else
+            g_value_set_boolean(destValue, FALSE);
+    } else /* When no option value provided, set "TRUE" by default. */
+        g_value_set_boolean(destValue, TRUE);
+}
+
+static void transformStringToInt(const GValue *srcValue, GValue *destValue)
+{
+    g_value_set_int(destValue, atoi(g_value_get_string(srcValue)));
+}
+
+static void transformStringToFloat(const GValue *srcValue, GValue *destValue)
+{
+    g_value_set_float(destValue, atof(g_value_get_string(srcValue)));
+}
+
+static gboolean parseAdditionalOptions(WebKitWebView *webView, int argc, char* argv[])
+{
+    g_value_register_transform_func(G_TYPE_STRING, G_TYPE_BOOLEAN, transformStringToBoolean);
+    g_value_register_transform_func(G_TYPE_STRING, G_TYPE_INT, transformStringToInt);
+    g_value_register_transform_func(G_TYPE_STRING, G_TYPE_FLOAT, transformStringToFloat);
+
+
+    WebKitWebSettings *webkitSettings = webkit_web_view_get_settings(webView);
+    GArray *optionEntriesArray = getOptionEntriesFromWebKitWebSettings(webkitSettings);
+
+    GOptionGroup *webSettingsGroup = g_option_group_new("websettings",
+                                                        "WebKitWebSettings writable properties for default WebKitWebView",
+                                                        "WebKitWebSettings properties",
+                                                        webkitSettings,
+                                                        NULL);
+    g_option_group_add_entries(webSettingsGroup, (GOptionEntry*) optionEntriesArray->data);
+
+    GOptionContext *context = g_option_context_new("[URL]");
+    g_option_context_add_group(context, webSettingsGroup);
+
+    GError *error = 0;
+    if (!g_option_context_parse(context, &argc, &argv, &error)) {
+        g_print("Failed to parse arguments: %s\n", error->message);
+        g_error_free(error);
+        g_option_context_free(context);
+        g_array_free(optionEntriesArray, TRUE);
+        return FALSE;
+    }
+    g_option_context_free(context);
+    g_array_free(optionEntriesArray, TRUE);
+    return TRUE;
+}
 int main(int argc, char* argv[])
 {
     WebKitWebView *webView;
@@ -260,6 +392,9 @@
 
     main_window = createWindow(&webView);
 
+    if (!parseAdditionalOptions(webView, argc, argv))
+        return 1;
+
     gchar *uri =(gchar*)(argc > 1 ? argv[1] : "http://www.google.com/");
     gchar *fileURL = filenameToURL(uri);
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to