From 1fa54c12337433455d7e9a65779b28d927050896 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 28 Jun 2010 13:44:43 -0500 Subject: Adding a new testing lib. --- tests/json-loader.h | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/json-loader.h (limited to 'tests/json-loader.h') diff --git a/tests/json-loader.h b/tests/json-loader.h new file mode 100644 index 0000000..b776c15 --- /dev/null +++ b/tests/json-loader.h @@ -0,0 +1,3 @@ + +#include + -- cgit v1.2.3 From 71e4cc90f10559cc675d696c773ed2825a2cbb8d Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 28 Jun 2010 15:55:53 -0500 Subject: Moving the JSON parsing code into the library. --- tests/Makefile.am | 2 + tests/json-loader.c | 87 ++++++++++++++++++++++++++++++++++++++++++- tests/json-loader.h | 8 ++++ tests/test-gtk-label-server.c | 71 +---------------------------------- 4 files changed, 98 insertions(+), 70 deletions(-) (limited to 'tests/json-loader.h') diff --git a/tests/Makefile.am b/tests/Makefile.am index 18ca628..d9468bb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -57,6 +57,7 @@ libdbusmenu_jsonloader_la_LDFLAGS = \ libdbusmenu_jsonloader_la_CFLAGS = \ $(DBUSMENUGLIB_CFLAGS) \ + $(DBUSMENUTESTS_CFLAGS) \ -I $(srcdir)/.. \ -Wall \ -Werror \ @@ -320,6 +321,7 @@ test_gtk_label_server_CFLAGS = \ test_gtk_label_server_LDADD = \ ../libdbusmenu-glib/libdbusmenu-glib.la \ ../libdbusmenu-gtk/libdbusmenu-gtk.la \ + libdbusmenu-jsonloader.la \ $(DBUSMENUGTK_LIBS) \ $(DBUSMENUTESTS_LIBS) diff --git a/tests/json-loader.c b/tests/json-loader.c index c2483b6..7cfc7d9 100644 --- a/tests/json-loader.c +++ b/tests/json-loader.c @@ -1,4 +1,89 @@ #include "json-loader.h" -const gchar * myval = "FIVE"; +static void +set_props (DbusmenuMenuitem * mi, JsonObject * node) +{ + if (node == NULL) return; + + GList * members = NULL; + for (members = json_object_get_members(node); members != NULL; members = g_list_next(members)) { + const gchar * member = members->data; + + if (!g_strcmp0(member, "id")) { continue; } + if (!g_strcmp0(member, "submenu")) { continue; } + + JsonNode * lnode = json_object_get_member(node, member); + if (JSON_NODE_TYPE(lnode) != JSON_NODE_VALUE) { continue; } + + GValue value = {0}; + json_node_get_value(lnode, &value); + dbusmenu_menuitem_property_set_value(mi, member, &value); + g_value_unset(&value); + } + + return; +} + +DbusmenuMenuitem * +dbusmenu_json_build_from_node (const JsonNode * cnode) +{ + JsonNode * node = (JsonNode *)cnode; /* To match the jsonglib API :( */ + + if (node == NULL) return NULL; + if (JSON_NODE_TYPE(node) != JSON_NODE_OBJECT) return NULL; + + JsonObject * layout = json_node_get_object(node); + + DbusmenuMenuitem * local = NULL; + if (json_object_has_member(layout, "id")) { + JsonNode * node = json_object_get_member(layout, "id"); + g_return_val_if_fail(JSON_NODE_TYPE(node) == JSON_NODE_VALUE, NULL); + local = dbusmenu_menuitem_new_with_id(json_node_get_int(node)); + } else { + local = dbusmenu_menuitem_new(); + } + + set_props(local, layout); + + if (json_object_has_member(layout, "submenu")) { + JsonNode * node = json_object_get_member(layout, "submenu"); + g_return_val_if_fail(JSON_NODE_TYPE(node) == JSON_NODE_ARRAY, local); + JsonArray * array = json_node_get_array(node); + guint count; + for (count = 0; count < json_array_get_length(array); count++) { + DbusmenuMenuitem * child = dbusmenu_json_build_from_node(json_array_get_element(array, count)); + if (child != NULL) { + dbusmenu_menuitem_child_append(local, child); + } + } + } + + /* g_debug("Layout to menu return: 0x%X", (unsigned int)local); */ + return local; +} + +DbusmenuMenuitem * +dbusmenu_json_build_from_file (const gchar * filename) +{ + JsonParser * parser = json_parser_new(); + + GError * error = NULL; + if (!json_parser_load_from_file(parser, filename, &error)) { + g_warning("Failed parsing file %s because: %s", filename, error->message); + g_error_free(error); + return NULL; + } + + JsonNode * root_node = json_parser_get_root(parser); + if (JSON_NODE_TYPE(root_node) != JSON_NODE_OBJECT) { + g_warning("Root node is not an object, fail. It's an: %s", json_node_type_name(root_node)); + return NULL; + } + + DbusmenuMenuitem * mi = dbusmenu_json_build_from_node(root_node); + + g_object_unref(parser); + + return mi; +} diff --git a/tests/json-loader.h b/tests/json-loader.h index b776c15..67e1c8b 100644 --- a/tests/json-loader.h +++ b/tests/json-loader.h @@ -1,3 +1,11 @@ +#ifndef __DBUSMENU_JSON_LOADER_H__ +#define __DBUSMENU_JSON_LOADER_H__ + #include +#include + +DbusmenuMenuitem * dbusmenu_json_build_from_node (const JsonNode * node); +DbusmenuMenuitem * dbusmenu_json_build_from_file (const gchar * filename); +#endif /* __DBUSMENU_JSON_LOADER_H__ */ diff --git a/tests/test-gtk-label-server.c b/tests/test-gtk-label-server.c index 32d7a43..32572fc 100644 --- a/tests/test-gtk-label-server.c +++ b/tests/test-gtk-label-server.c @@ -30,74 +30,7 @@ with this program. If not, see . #include #include - -static void -menuitem_click(DbusmenuMenuitem * mi, guint32 time, gpointer user_data) -{ - g_debug("Clicked on: %d @ %d", dbusmenu_menuitem_get_id(mi), time); - return; -} - -static void -set_props (DbusmenuMenuitem * mi, JsonObject * node) -{ - if (node == NULL) return; - - GList * members = NULL; - for (members = json_object_get_members(node); members != NULL; members = g_list_next(members)) { - const gchar * member = members->data; - - if (!g_strcmp0(member, "id")) { continue; } - if (!g_strcmp0(member, "submenu")) { continue; } - - JsonNode * lnode = json_object_get_member(node, member); - if (JSON_NODE_TYPE(lnode) != JSON_NODE_VALUE) { continue; } - - GValue value = {0}; - json_node_get_value(lnode, &value); - dbusmenu_menuitem_property_set_value(mi, member, &value); - g_value_unset(&value); - } - - return; -} - -static DbusmenuMenuitem * -layout2menuitem (JsonNode * inlayout) -{ - if (inlayout == NULL) return NULL; - if (JSON_NODE_TYPE(inlayout) != JSON_NODE_OBJECT) return NULL; - - JsonObject * layout = json_node_get_object(inlayout); - - DbusmenuMenuitem * local = NULL; - if (json_object_has_member(layout, "id")) { - JsonNode * node = json_object_get_member(layout, "id"); - g_return_val_if_fail(JSON_NODE_TYPE(node) == JSON_NODE_VALUE, NULL); - local = dbusmenu_menuitem_new_with_id(json_node_get_int(node)); - } else { - local = dbusmenu_menuitem_new(); - } - g_signal_connect(G_OBJECT(local), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(menuitem_click), NULL); - - set_props(local, layout); - - if (json_object_has_member(layout, "submenu")) { - JsonNode * node = json_object_get_member(layout, "submenu"); - g_return_val_if_fail(JSON_NODE_TYPE(node) == JSON_NODE_ARRAY, local); - JsonArray * array = json_node_get_array(node); - guint count; - for (count = 0; count < json_array_get_length(array); count++) { - DbusmenuMenuitem * child = layout2menuitem(json_array_get_element(array, count)); - if (child != NULL) { - dbusmenu_menuitem_child_append(local, child); - } - } - } - - /* g_debug("Layout to menu return: 0x%X", (unsigned int)local); */ - return local; -} +#include "json-loader.h" static JsonArray * root_array = NULL; static guint layouton = 0; @@ -114,7 +47,7 @@ timer_func (gpointer data) } g_debug("Updating to Layout %d", layouton); - dbusmenu_server_set_root(server, layout2menuitem(json_array_get_element(root_array, layouton))); + dbusmenu_server_set_root(server, dbusmenu_json_build_from_node(json_array_get_element(root_array, layouton))); layouton++; return TRUE; -- cgit v1.2.3 From 511e68461114325f3685bc547517668b179b4914 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 28 Jun 2010 22:39:59 -0500 Subject: Forgot copyright headers --- tests/json-loader.c | 20 ++++++++++++++++++++ tests/json-loader.h | 20 ++++++++++++++++++++ tests/test-json-client.c | 21 +++++++++++++++++++++ tests/test-json-server.c | 21 +++++++++++++++++++++ 4 files changed, 82 insertions(+) (limited to 'tests/json-loader.h') diff --git a/tests/json-loader.c b/tests/json-loader.c index 97f1c13..aad4295 100644 --- a/tests/json-loader.c +++ b/tests/json-loader.c @@ -1,3 +1,23 @@ +/* +A loader to turn JSON into dbusmenu menuitems + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, 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 . +*/ #include "json-loader.h" #include diff --git a/tests/json-loader.h b/tests/json-loader.h index 67e1c8b..666bb6e 100644 --- a/tests/json-loader.h +++ b/tests/json-loader.h @@ -1,3 +1,23 @@ +/* +A loader to turn JSON into dbusmenu menuitems + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, 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 . +*/ #ifndef __DBUSMENU_JSON_LOADER_H__ #define __DBUSMENU_JSON_LOADER_H__ diff --git a/tests/test-json-client.c b/tests/test-json-client.c index 62eb87c..73d64b0 100644 --- a/tests/test-json-client.c +++ b/tests/test-json-client.c @@ -1,3 +1,24 @@ +/* +Test to check the json-loader and dbusmenu-dumper + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, 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 . +*/ + #include #include #include diff --git a/tests/test-json-server.c b/tests/test-json-server.c index cf6b605..fe9507a 100644 --- a/tests/test-json-server.c +++ b/tests/test-json-server.c @@ -1,3 +1,24 @@ +/* +Test to check the json-loader and dbusmenu-dumper + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, 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 . +*/ + #include #include -- cgit v1.2.3