Commit 0cd4a43c authored by Sebastian Ramacher's avatar Sebastian Ramacher

Add command to dump settings to JSON file

Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 39c8e7aa
......@@ -28,6 +28,10 @@ else
LIBNOTIFY_PC_NAME =
endif
ifneq (${WITH_JSON},0)
CPPFLAGS += -DWITH_JSON
endif
ifeq (,$(findstring -DGETTEXT_PACKAGE,${CPPFLAGS}))
CPPFLAGS += -DGETTEXT_PACKAGE=\"${GETTEXT_PACKAGE}\"
endif
......
......@@ -19,6 +19,9 @@ SOVERSION = ${SOMAJOR}.${SOMINOR}
# libnotify
WITH_LIBNOTIFY ?= $(shell (pkg-config libnotify --atleast-version=0.7.0 && echo 1) || echo 0)
# libjson-c
WITH_JSON ?= $(shell (pkg-config json-c --exists && echo 1) || echo 0)
# paths
PREFIX ?= /usr
LIBDIR ?= ${PREFIX}/lib
......@@ -56,8 +59,13 @@ LIBNOTIFY_INC ?= $(shell pkg-config --cflags libnotify)
LIBNOTIFY_LIB ?= $(shell pkg-config --libs libnotify)
endif
INCS = ${GTK_INC} ${LIBNOTIFY_INC}
LIBS = ${GTK_LIB} ${LIBNOTIFY_LIB} -lm
ifneq (${WITH_JSON},0)
JSON_INC ?= $(shell pkg-config --cflags json-c)
JSON_LIB ?= $(shell pkg-config --libs json-c)
endif
INCS = ${GTK_INC} ${LIBNOTIFY_INC} ${JSON_INC}
LIBS = ${GTK_LIB} ${LIBNOTIFY_LIB} ${JSON_LIB} -lm
# flags
CFLAGS += -std=c99 -pedantic -Wall -Wextra -fPIC $(INCS)
......
......@@ -280,11 +280,14 @@ girara_config_load_default(girara_session_t* session)
girara_inputbar_shortcut_add(session, GDK_CONTROL_MASK, GDK_KEY_n, girara_isc_command_history, GIRARA_NEXT, NULL);
/* commands */
girara_inputbar_command_add(session, "exec", NULL, girara_cmd_exec, NULL, _("Execute a command"));
girara_inputbar_command_add(session, "map", "m", girara_cmd_map, NULL, _("Map a key sequence"));
girara_inputbar_command_add(session, "quit", "q", girara_cmd_quit, NULL, _("Quit the program"));
girara_inputbar_command_add(session, "set", "s", girara_cmd_set, girara_cc_set, _("Set an option"));
girara_inputbar_command_add(session, "unmap", NULL, girara_cmd_unmap, NULL, _("Unmap a key sequence"));
girara_inputbar_command_add(session, "exec", NULL, girara_cmd_exec, NULL, _("Execute a command"));
girara_inputbar_command_add(session, "map", "m", girara_cmd_map, NULL, _("Map a key sequence"));
girara_inputbar_command_add(session, "quit", "q", girara_cmd_quit, NULL, _("Quit the program"));
girara_inputbar_command_add(session, "set", "s", girara_cmd_set, girara_cc_set, _("Set an option"));
girara_inputbar_command_add(session, "unmap", NULL, girara_cmd_unmap, NULL, _("Unmap a key sequence"));
#ifdef WITH_JSON
girara_inputbar_command_add(session, "dump", NULL, girara_cmd_dump_config, NULL, _("Dump settings to a file"));
#endif
/* config handle */
girara_config_handle_add(session, "map", girara_cmd_map);
......
......@@ -107,7 +107,8 @@ HIDDEN bool girara_cmd_set(girara_session_t* session,
/**
* Execute an external command
* * @param session The used girara session
*
* @param session The used girara session
* @param argument_list List of passed arguments
* @return TRUE No error occured
* @return FALSE An error occured
......@@ -115,6 +116,19 @@ HIDDEN bool girara_cmd_set(girara_session_t* session,
HIDDEN bool girara_cmd_exec(girara_session_t* session,
girara_list_t* argument_list);
#ifdef WITH_JSON
/**
* Dump current settings to a JSON file
*
* @param session The used girara session
* @param argument_list List of passed arguments
* @return TRUE No error occured
* @return FALSE An error occured
*/
HIDDEN bool girara_cmd_dump_config(girara_session_t* session,
girara_list_t* argument_list);
#endif
/**
* Process argument as a sequence of keys that were typed by the user
*
......
......@@ -3,12 +3,16 @@
#include <stdlib.h>
#include <glib.h>
#include <string.h>
#ifdef WITH_JSON
#include <json.h>
#endif
#include "settings.h"
#include "datastructures.h"
#include "completion.h"
#include "session.h"
#include "internal.h"
#include "utils.h"
/**
* Structure of a settings entry
......@@ -222,3 +226,73 @@ girara_cc_set(girara_session_t* session, const char* input)
return completion;
}
#ifdef WITH_JSON
bool
girara_cmd_dump_config(girara_session_t* session, girara_list_t* argument_list)
{
if (session == NULL || argument_list == NULL) {
return false;
}
const size_t number_of_arguments = girara_list_size(argument_list);
if (number_of_arguments != 1) {
girara_warning("Invalid number of arguments passed: %zu instead of 1",
number_of_arguments);
girara_notify(session, GIRARA_ERROR,
_("Invalid number of arguments passed: %zu instead of 1"),
number_of_arguments);
return false;
return false;
}
json_object* json_config = json_object_new_object();
GIRARA_LIST_FOREACH(session->private_data->settings, girara_setting_t*, iter, setting)
json_object* json_setting = json_object_new_object();
json_object* json_value = NULL;
json_object* json_type = NULL;
switch(setting->type) {
case BOOLEAN:
json_value = json_object_new_boolean(setting->value.b);
json_type = json_object_new_string("boolean");
break;
case FLOAT:
json_value = json_object_new_double(setting->value.f);
json_type = json_object_new_string("float");
break;
case INT:
json_value = json_object_new_int(setting->value.i);
json_type = json_object_new_string("int");
break;
case STRING:
json_value = json_object_new_string(setting->value.s ? setting->value.s : "");
json_type = json_object_new_string("string");
break;
default:
girara_debug("Invalid setting: %s", setting->name);
}
if (json_value != NULL) {
json_object_object_add(json_setting, "value", json_value);
json_object_object_add(json_setting, "type", json_type);
}
if (setting->description != NULL) {
json_object_object_add(json_setting, "description",
json_object_new_string(setting->description));
}
json_object_object_add(json_setting, "init-only",
json_object_new_boolean(setting->init_only));
json_object_object_add(json_config, setting->name, json_setting);
GIRARA_LIST_FOREACH_END(session->private_data->settings, girara_setting_t*, iter, setting);
json_object_to_file_ext(girara_list_nth(argument_list, 0), json_config,
JSON_C_TO_STRING_PRETTY);
json_object_put(json_config);
return true;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment