From 3a3e03999ad313cf86a6d4c653eafdc1e5ae358b Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Mon, 11 Sep 2023 17:28:20 +0200 Subject: [PATCH] Expose document info via dbus (fixes #355) The data is encoded as JSON and also includes the document index. --- data/org.pwmt.zathura.xml | 2 + meson.build | 3 +- zathura/dbus-interface.c | 82 ++++++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/data/org.pwmt.zathura.xml b/data/org.pwmt.zathura.xml index c6ed965..42d54b4 100644 --- a/data/org.pwmt.zathura.xml +++ b/data/org.pwmt.zathura.xml @@ -27,7 +27,9 @@ + + diff --git a/meson.build b/meson.build index 5825881..76e7d92 100644 --- a/meson.build +++ b/meson.build @@ -43,10 +43,11 @@ gio = dependency('gio-unix-2.0', required: host_machine.system() != 'windows') gthread = dependency('gthread-2.0', version: '>=2.50') gmodule = dependency('gmodule-no-export-2.0', version: '>=2.50') gtk3 = dependency('gtk+-3.0', version: '>=3.22') +json_glib = dependency('json-glib-1.0') cairo = dependency('cairo') magic = cc.find_library('magic', required: true) -build_dependencies = [libm, girara, glib, gio, gthread, gmodule, gtk3, cairo, magic] +build_dependencies = [libm, girara, glib, gio, gthread, gmodule, gtk3, cairo, magic, json_glib] if host_machine.system() == 'darwin' gtk_mac_integration = dependency('gtk-mac-integration-gtk3') diff --git a/zathura/dbus-interface.c b/zathura/dbus-interface.c index fac9ec9..aac0db5 100644 --- a/zathura/dbus-interface.c +++ b/zathura/dbus-interface.c @@ -4,6 +4,7 @@ #include "adjustment.h" #include "config.h" #include "document.h" +#include "links.h" #include "macros.h" #include "resources.h" #include "synctex.h" @@ -16,6 +17,7 @@ #include #include #include +#include #include #include @@ -533,14 +535,73 @@ handle_method_call(GDBusConnection* UNUSED(connection), const gchar* UNUSED(send } } -static GVariant* -handle_get_property(GDBusConnection* UNUSED(connection), - const gchar* UNUSED(sender), - const gchar* UNUSED(object_path), - const gchar* UNUSED(interface_name), - const gchar* property_name, GError** error, void* data) +static void +json_document_info_add_node(JsonBuilder* builder, girara_tree_node_t* index) { - ZathuraDbus* dbus = data; + girara_list_t* list = girara_node_get_children(index); + + GIRARA_LIST_FOREACH_BODY( + list, girara_tree_node_t*, node, do { + zathura_index_element_t* index_element = girara_node_get_data(node); + + json_builder_begin_object(builder); + json_builder_set_member_name(builder, "title"); + json_builder_add_string_value(builder, index_element->title); + + zathura_link_type_t type = zathura_link_get_type(index_element->link); + zathura_link_target_t target = zathura_link_get_target(index_element->link); + if (type == ZATHURA_LINK_GOTO_DEST) { + json_builder_set_member_name(builder, "page"); + json_builder_add_int_value(builder, target.page_number + 1); + } else { + json_builder_set_member_name(builder, "target"); + json_builder_add_string_value(builder, target.value); + } + + if (girara_node_get_num_children(node) > 0) { + json_builder_set_member_name(builder, "sub-index"); + json_builder_begin_array(builder); + json_document_info_add_node(builder, node); + json_builder_end_array(builder); + } + json_builder_end_object(builder); + } while (0);); +} + +static GVariant* +json_document_info(zathura_t* zathura) +{ + JsonBuilder* builder = json_builder_new(); + json_builder_begin_object(builder); + json_builder_set_member_name(builder, "filename"); + json_builder_add_string_value(builder, zathura_document_get_path(zathura->document)); + json_builder_set_member_name(builder, "number-of-pages"); + json_builder_add_int_value(builder, zathura_document_get_current_page_number(zathura->document)); + + json_builder_set_member_name(builder, "index"); + json_builder_begin_array(builder); + girara_tree_node_t* index = zathura_document_index_generate(zathura->document, NULL); + if (index != NULL) { + json_document_info_add_node(builder, index); + girara_node_free(index); + } + json_builder_end_array(builder); + + json_builder_end_object(builder); + + JsonNode* root = json_builder_get_root(builder); + char* serialized_root = json_to_string(root, true); + json_node_free(root); + g_object_unref(builder); + + return g_variant_new_take_string(serialized_root); +} + +static GVariant* +handle_get_property(GDBusConnection* UNUSED(connection), const gchar* UNUSED(sender), const gchar* UNUSED(object_path), + const gchar* UNUSED(interface_name), const gchar* property_name, GError** error, void* data) +{ + ZathuraDbus* dbus = data; ZathuraDbusPrivate* priv = zathura_dbus_get_instance_private(dbus); if (priv->zathura->document == NULL) { @@ -554,16 +615,17 @@ handle_get_property(GDBusConnection* UNUSED(connection), return g_variant_new_uint32(zathura_document_get_current_page_number(priv->zathura->document)); } else if (g_strcmp0(property_name, "numberofpages") == 0) { return g_variant_new_uint32(zathura_document_get_number_of_pages(priv->zathura->document)); + } else if (g_strcmp0(property_name, "documentinfo") == 0) { + return json_document_info(priv->zathura); } return NULL; } -static const GDBusInterfaceVTable interface_vtable = -{ +static const GDBusInterfaceVTable interface_vtable = { .method_call = handle_method_call, .get_property = handle_get_property, - .set_property = NULL + .set_property = NULL, }; static const unsigned int TIMEOUT = 3000;