Added set command to edit setting on runtime.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 29 Oct 2012 20:32:27 +0000 (21:32 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 10 Nov 2012 13:42:55 +0000 (14:42 +0100)
config.mk
src/command.c
src/config.h
src/main.c
src/main.h
src/setting.c [new file with mode: 0644]
src/setting.h [new file with mode: 0644]
src/util.c
src/util.h

index de83bbd..dd4f505 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -21,6 +21,9 @@ CFLAGS += -Wmissing-parameter-type
 
 LDFLAGS += $(shell pkg-config --libs $(LIBS)) -lX11 -lXext
 
+# features
+CPPFLAGS += -DFEATURE_COOKIE
+
 CPPFLAGS += -DPROJECT=\"$(PROJECT)\"
 CPPFLAGS += -DVERSION=\"${VERSION}\"
 
index 0d648f1..6c8bdba 100644 (file)
@@ -52,6 +52,7 @@ static CommandInfo cmd_list[] = {
     {"nunmap",           vp_unmap,         {VP_MODE_NORMAL}},
     {"iunmap",           vp_unmap,         {VP_MODE_INSERT}},
     {"cunmap",           vp_unmap,         {VP_MODE_COMMAND}},
+    {"set",              vp_set,           {0}},
 };
 
 void command_init()
index f669dce..2cf0e49 100644 (file)
@@ -22,9 +22,6 @@
 
 #include "stdlib.h"
 
-/* features */
-#define FEATURE_COOKIE
-
 #define START_PAGE      "http://sourceforge.net/apps/trac/vimprobable"
 
 #define STATUS_BG_COLOR "#000000"           /* background color for status bar */
@@ -44,7 +41,7 @@ static const char *inputbox_bg[]   = { "#ffffff",            "#ff0000" };
 #define SCROLLSTEP (40) /* cursor difference in pixel for scrolling */
 
 const struct {
-    char *command;
+    charcommand;
 } default_config[] = {
     {"nmap o inputopen"},
     {"nmap O inputopencurrent"},
index e976534..1725492 100644 (file)
  * along with this program. If not, see http://www.gnu.org/licenses/.
  */
 
-#include "config.h"
 #include "main.h"
+#include "config.h"
 #include "util.h"
 #include "command.h"
 #include "keybind.h"
+#include "setting.h"
 
 /* variables */
 VpCore vp;
@@ -47,7 +48,6 @@ static void vp_read_config(void);
 static void vp_init_gui(void);
 static void vp_init_files(void);
 static void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition, const gchar* bg_color, const gchar* fg_color);
-static void vp_setup_settings(void);
 static void vp_setup_signals(void);
 static gboolean vp_load_uri(const Arg* arg);
 #ifdef FEATURE_COOKIE
@@ -165,7 +165,7 @@ static void vp_gotheaders_cb(SoupMessage* message, gpointer data)
 static gboolean vp_process_input(const char* input)
 {
     gboolean success;
-    gchar* line = g_strdup(input);
+    gchar* line = NULL;
     gchar* command = NULL;
     gchar** token;
 
@@ -173,6 +173,7 @@ static gboolean vp_process_input(const char* input)
         return FALSE;
     }
 
+    line = g_strdup(input);
     g_strstrip(line);
 
     /* get a possible command count */
@@ -399,6 +400,34 @@ gboolean vp_open(const Arg* arg)
     return vp_load_uri(arg);
 }
 
+gboolean vp_set(const Arg* arg)
+{
+    gboolean success;
+    gchar* line = NULL;
+    gchar** token;
+
+    if (!arg->s || !strlen(arg->s)) {
+        return FALSE;
+    }
+
+    line = g_strdup(arg->s);
+    g_strstrip(line);
+
+    /* split the input string into paramete and value part */
+    token = g_strsplit(line, "=", 2);
+    g_free(line);
+
+    if (!token[0]) {
+        /* TODO display current value */
+        g_strfreev(token);
+        return FALSE;
+    }
+    success = setting_run(token[0], token[1] ? token[1] : NULL);
+    g_strfreev(token);
+
+    return success;
+}
+
 void vp_update_urlbar(const gchar* uri)
 {
     gchar* markup;
@@ -470,6 +499,9 @@ static void vp_init(void)
     /* initialize the keybindings */
     keybind_init();
 
+    /* initialize settings */
+    setting_init();
+
     vp_read_config();
 
     vp.config.cookie_timeout = 4800;
@@ -534,8 +566,6 @@ static void vp_init_gui(void)
     soup_session_remove_feature_by_type(vp.net.soup_session, soup_cookie_jar_get_type());
 #endif
 
-    vp_setup_settings();
-
     /* Create a scrollable area */
     gui->viewport = gtk_scrolled_window_new(gui->adjust_h, gui->adjust_v);
     gtk_scrolled_window_set_policy(
@@ -625,14 +655,6 @@ static void vp_set_widget_font(GtkWidget* widget, const gchar* font_definition,
     gtk_widget_modify_base(widget, GTK_STATE_NORMAL, bg_color ? &bg : NULL);
 }
 
-static void vp_setup_settings(void)
-{
-    WebKitWebSettings *settings = webkit_web_view_get_settings(vp.gui.webview);
-
-    g_object_set(G_OBJECT(settings), "user-agent", SETTING_USER_AGENT, NULL);
-    webkit_web_view_set_settings(vp.gui.webview, settings);
-}
-
 static void vp_setup_signals(void)
 {
     Gui* gui              = &vp.gui;
index b9d7748..54e0d4e 100644 (file)
@@ -98,10 +98,18 @@ enum {
     FILES_LAST
 };
 
+typedef enum {
+    TYPE_CHAR,
+    TYPE_BOOLEAN,
+    TYPE_INTEGER,
+    TYPE_DOUBLE,
+    TYPE_COLOR,
+} Type;
+
 /* structs */
 typedef struct {
-    gint  i;
-    char* s;
+    gint     i;
+    gchar*   s;
 } Arg;
 
 /* statusbar */
@@ -181,5 +189,6 @@ gboolean vp_unmap(const Arg* arg);
 gboolean vp_set_mode(const Arg* arg);
 gboolean vp_input(const Arg* arg);
 gboolean vp_open(const Arg* arg);
+gboolean vp_set(const Arg* arg);
 
 #endif /* end of include guard: MAIN_H */
diff --git a/src/setting.c b/src/setting.c
new file mode 100644 (file)
index 0000000..c888b2d
--- /dev/null
@@ -0,0 +1,154 @@
+/**
+ * vimp - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#include "setting.h"
+#include "util.h"
+
+static gboolean setting_webkit(const Setting* s);
+
+static Setting default_settings[] = {
+    {"auto-load-images", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"auto-shrink-images", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"cursive-font-family", TYPE_CHAR, setting_webkit, {.s = "serif"}},
+    {"cursive-font-family", TYPE_CHAR, setting_webkit, {.s = "serif"}},
+    {"default-encoding", TYPE_CHAR, setting_webkit, {.s = "utf-8"}},
+    {"default-font-family", TYPE_CHAR, setting_webkit, {.s = "sans-serif"}},
+    {"default-font-size", TYPE_INTEGER, setting_webkit, {.i = 12}},
+    {"default-monospace-font-size", TYPE_INTEGER, setting_webkit, {.i = 10}},
+    {"enable-caret-browsing", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-default-context-menu", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-developer-extras", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-dns-prefetching", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-dom-paste", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-frame-flattening", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-file-access-from-file-uris", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-html5-database", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-html5-local-storage", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-java-applet", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-offline-web-application-cache", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-page-cache", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-plugins", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-private-browsing", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-scripts", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-site-specific-quirks", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-spatial-navigation", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-spell-checking", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enable-universal-access-from-file-uris", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"enable-xss-auditor", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"enforce-96-dpi", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"fantasy-font-family", TYPE_CHAR, setting_webkit, {.s = "serif"}},
+    {"javascript-can-access-clipboard", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"javascript-can-open-windows-automatically", TYPE_BOOLEAN, setting_webkit, {.i = 0}},
+    {"minimum-font-size", TYPE_INTEGER, setting_webkit, {.i = 5}},
+    {"minimum-logical-font-size", TYPE_INTEGER, setting_webkit, {.i = 5}},
+    {"monospace-font-family", TYPE_CHAR, setting_webkit, {.s = "monospace"}},
+    {"print-backgrounds", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"resizable-text-areas", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"sans-serif-font-family", TYPE_CHAR, setting_webkit, {.s = "sans-serif"}},
+    {"serif-font-family", TYPE_CHAR, setting_webkit, {.s = "serif"}},
+    {"spell-checking-languages", TYPE_CHAR, setting_webkit, {.s = NULL}},
+    {"tab-key-cycles-through-elements", TYPE_BOOLEAN, setting_webkit, {.i = 1}},
+    {"user-agent", TYPE_CHAR, setting_webkit, {.s = PROJECT "/" VERSION " (X11; Linux i686) AppleWebKit/535.22+ Compatible (Safari)"}},
+    {"user-stylesheet-uri", TYPE_CHAR, setting_webkit, {.s = NULL}},
+    {"zoom-step", TYPE_DOUBLE, setting_webkit, {.i = 100}},
+};
+
+static GHashTable* settings = NULL;
+
+
+void setting_init(void)
+{
+    Setting* s;
+    guint i;
+    settings = g_hash_table_new(g_str_hash, g_str_equal);
+
+    for (i = 0; i < LENGTH(default_settings); i++) {
+        s = &default_settings[i];
+        g_hash_table_insert(settings, (gpointer)s->name, s);
+
+        /* set the default settings */
+        s->func(s);
+    }
+}
+
+gboolean setting_run(const gchar* name, const gchar* param)
+{
+    gboolean result = FALSE;
+    Setting* s      = g_hash_table_lookup(settings, name);
+    if (!s) {
+        return FALSE;
+    }
+
+    if (!param) {
+        return s->func(s);
+    }
+
+    /* if string param is given convert it to the required data type and set
+     * it to the arg of the setting */
+    switch (s->type) {
+        case TYPE_BOOLEAN:
+            s->arg.i = util_atob(param) ? 1 : 0;
+            result = s->func(s);
+            break;
+
+        case TYPE_INTEGER:
+            s->arg.i = g_ascii_strtoull(param, (gchar**)NULL, 10);
+            result = s->func(s);
+            break;
+
+        case TYPE_DOUBLE:
+            s->arg.i = (1000 * g_ascii_strtod(param, (gchar**)NULL));
+            result = s->func(s);
+            break;
+
+        case TYPE_CHAR:
+        case TYPE_COLOR:
+            s->arg.s = g_strdup(param);
+            result = s->func(s);
+            g_free(s->arg.s);
+            break;
+    }
+
+    return result;
+}
+
+static gboolean setting_webkit(const Setting* s)
+{
+    WebKitWebSettings* web_setting = webkit_web_view_get_settings(vp.gui.webview);
+
+    switch (s->type) {
+        case TYPE_BOOLEAN:
+            g_object_set(G_OBJECT(web_setting), s->name, s->arg.i ? TRUE : FALSE, NULL);
+            break;
+
+        case TYPE_INTEGER:
+            g_object_set(G_OBJECT(web_setting), s->name, s->arg.i, NULL);
+            break;
+
+        case TYPE_DOUBLE:
+            g_object_set(G_OBJECT(web_setting), s->name, (s->arg.i / 1000.0), NULL);
+            break;
+
+        case TYPE_CHAR:
+        case TYPE_COLOR:
+            g_object_set(G_OBJECT(web_setting), s->name, s->arg.s, NULL);
+            break;
+    }
+    return TRUE;
+}
diff --git a/src/setting.h b/src/setting.h
new file mode 100644 (file)
index 0000000..d3150b0
--- /dev/null
@@ -0,0 +1,38 @@
+/**
+ * vimp - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#ifndef SETTING_H
+#define SETTING_H
+
+#include "main.h"
+
+typedef struct _Setting Setting;
+typedef gboolean (*SettingFunc)(const Setting*);
+
+struct _Setting {
+    gchar*      name;
+    Type        type;
+    SettingFunc func;
+    Arg         arg;
+};
+
+void setting_init(void);
+gboolean setting_run(const gchar* name, const gchar* param);
+
+#endif /* end of include guard: SETTING_H */
index b80ff4a..0401b2f 100644 (file)
 
 #include "util.h"
 #include <stdio.h>
-#include <glib.h>
 
-char* util_get_config_dir(void)
+gchar* util_get_config_dir(void)
 {
-    char *path = g_build_filename(g_get_user_config_dir(), PROJECT, NULL);
+    gchar *path = g_build_filename(g_get_user_config_dir(), PROJECT, NULL);
     util_create_dir_if_not_exists(path);
 
     return path;
 }
 
-char* util_get_cache_dir(void)
+gchar* util_get_cache_dir(void)
 {
-    char *path = g_build_filename(g_get_user_cache_dir(), PROJECT, NULL);
+    gchar *path = g_build_filename(g_get_user_cache_dir(), PROJECT, NULL);
     util_create_dir_if_not_exists(path);
 
     return path;
@@ -50,3 +49,13 @@ void util_create_file_if_not_exists(const char* filename) {
         fclose(f);
     }
 }
+
+gboolean util_atob(const gchar* str)
+{
+    if (str == NULL) {
+        return FALSE;
+    }
+
+    return g_ascii_strncasecmp(str, "true", 4) == 0
+        || g_ascii_strncasecmp(str, "on", 2) == 0;
+}
index b3cd41e..3d14843 100644 (file)
 #ifndef UTIL_H
 #define UTIL_H
 
-char* util_get_config_dir(void);
-char* util_get_cache_dir(void);
-void util_create_dir_if_not_exists(const char* dirpath);
-void util_create_file_if_not_exists(const char* filename);
+#include <glib.h>
+
+gchar* util_get_config_dir(void);
+gchar* util_get_cache_dir(void);
+void util_create_dir_if_not_exists(const gchar* dirpath);
+void util_create_file_if_not_exists(const gchar* filename);
+gboolean util_atob(const gchar* str);
 
 #endif /* end of include guard: UTIL_H */