Commit f6754d7a authored by Sebastian Ramacher's avatar Sebastian Ramacher
Browse files

Split handle_method_call


Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent eda1dcec
......@@ -193,175 +193,215 @@ zathura_dbus_edit(ZathuraDbus* edit, unsigned int page, unsigned int x, unsigned
/* D-Bus handler */
static void
handle_method_call(GDBusConnection* UNUSED(connection),
const gchar* UNUSED(sender), const gchar* object_path,
const gchar* interface_name, const gchar* method_name,
GVariant* parameters, GDBusMethodInvocation* invocation,
void* data)
handle_open_document(zathura_t* zathura, GVariant* parameters,
GDBusMethodInvocation* invocation)
{
ZathuraDbus* dbus = data;
private_t* priv = GET_PRIVATE(dbus);
gchar* filename = NULL;
gchar* password = NULL;
gint page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
g_variant_get(parameters, "(ssi)", &filename, &password, &page);
document_close(zathura, false);
document_open_idle(zathura, filename,
strlen(password) > 0 ? password : NULL,
page,
NULL, NULL);
g_free(filename);
g_free(password);
GVariant* result = g_variant_new("(b)", true);
g_dbus_method_invocation_return_value(invocation, result);
}
girara_debug("Handling call '%s.%s' on '%s'.", interface_name, method_name,
object_path);
static void
handle_close_document(zathura_t* zathura, GVariant* UNUSED(parameters),
GDBusMethodInvocation* invocation)
{
const bool ret = document_close(zathura, false);
/* methods that work without open document */
if (g_strcmp0(method_name, "OpenDocument") == 0) {
gchar* filename = NULL;
gchar* password = NULL;
gint page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
g_variant_get(parameters, "(ssi)", &filename, &password, &page);
document_close(priv->zathura, false);
document_open_idle(priv->zathura, filename,
strlen(password) > 0 ? password : NULL,
page,
NULL, NULL);
g_free(filename);
g_free(password);
GVariant* result = g_variant_new("(b)", true);
g_dbus_method_invocation_return_value(invocation, result);
return;
} else if (g_strcmp0(method_name, "CloseDocument") == 0) {
const bool ret = document_close(priv->zathura, false);
GVariant* result = g_variant_new("(b)", ret);
g_dbus_method_invocation_return_value(invocation, result);
}
GVariant* result = g_variant_new("(b)", ret);
g_dbus_method_invocation_return_value(invocation, result);
return;
}
static void
handle_goto_page(zathura_t* zathura, GVariant* parameters,
GDBusMethodInvocation* invocation)
{
const unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
if (priv->zathura->document == NULL) {
g_dbus_method_invocation_return_dbus_error(invocation,
"org.pwmt.zathura.NoOpenDocumen",
"No document has been opened.");
return;
guint page = 0;
g_variant_get(parameters, "(u)", &page);
bool ret = true;
if (page >= number_of_pages) {
ret = false;
} else {
page_set(zathura, page);
}
const unsigned int number_of_pages = zathura_document_get_number_of_pages(priv->zathura->document);
GVariant* result = g_variant_new("(b)", ret);
g_dbus_method_invocation_return_value(invocation, result);
}
/* methods that require an open document */
if (g_strcmp0(method_name, "GotoPage") == 0) {
guint page = 0;
g_variant_get(parameters, "(u)", &page);
static void
handle_highlight_rects(zathura_t* zathura, GVariant* parameters,
GDBusMethodInvocation* invocation)
{
const unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
bool ret = true;
if (page >= number_of_pages) {
ret = false;
} else {
page_set(priv->zathura, page);
}
guint page = 0;
GVariantIter* iter = NULL;
GVariantIter* secondary_iter = NULL;
g_variant_get(parameters, "(ua(dddd)a(udddd))", &page, &iter,
&secondary_iter);
GVariant* result = g_variant_new("(b)", ret);
if (page >= number_of_pages) {
girara_debug("Got invalid page number.");
GVariant* result = g_variant_new("(b)", false);
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
g_dbus_method_invocation_return_value(invocation, result);
} else if (g_strcmp0(method_name, "HighlightRects") == 0) {
guint page = 0;
GVariantIter* iter = NULL;
GVariantIter* secondary_iter = NULL;
g_variant_get(parameters, "(ua(dddd)a(udddd))", &page, &iter,
&secondary_iter);
if (page >= number_of_pages) {
girara_debug("Got invalid page number.");
GVariant* result = g_variant_new("(b)", false);
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
g_dbus_method_invocation_return_value(invocation, result);
return;
}
return;
}
/* get rectangles */
girara_list_t** rectangles = g_try_malloc0(number_of_pages * sizeof(girara_list_t*));
if (rectangles == NULL) {
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
rectangles[page] = girara_list_new2(g_free);
if (rectangles[page] == NULL) {
g_free(rectangles);
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
/* get rectangles */
girara_list_t** rectangles = g_try_malloc0(number_of_pages * sizeof(girara_list_t*));
if (rectangles == NULL) {
zathura_rectangle_t temp_rect = { 0, 0, 0, 0 };
while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2,
&temp_rect.y1, &temp_rect.y2)) {
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
girara_list_free(rectangles[page]);
g_free(rectangles);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
rectangles[page] = girara_list_new2(g_free);
if (rectangles[page] == NULL) {
g_free(rectangles);
g_variant_iter_free(iter);
*rect = temp_rect;
girara_list_append(rectangles[page], rect);
}
g_variant_iter_free(iter);
/* get secondary rectangles */
guint temp_page = 0;
while (g_variant_iter_loop(secondary_iter, "(udddd)", &temp_page,
&temp_rect.x1, &temp_rect.x2, &temp_rect.y1,
&temp_rect.y2)) {
if (temp_page >= number_of_pages) {
/* error out here? */
girara_debug("Got invalid page number.");
continue;
}
if (rectangles[temp_page] == NULL) {
rectangles[temp_page] = girara_list_new2(g_free);
}
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL || rectangles[temp_page] == NULL) {
g_variant_iter_free(secondary_iter);
for (unsigned int p = 0; p != number_of_pages; ++p) {
girara_list_free(rectangles[p]);
}
g_free(rectangles);
g_free(rect);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
zathura_rectangle_t temp_rect = { 0, 0, 0, 0 };
while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2,
&temp_rect.y1, &temp_rect.y2)) {
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL) {
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
girara_list_free(rectangles[page]);
g_free(rectangles);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
*rect = temp_rect;
girara_list_append(rectangles[temp_page], rect);
}
g_variant_iter_free(secondary_iter);
*rect = temp_rect;
girara_list_append(rectangles[page], rect);
}
g_variant_iter_free(iter);
synctex_highlight_rects(zathura, page, rectangles);
g_free(rectangles);
/* get secondary rectangles */
guint temp_page = 0;
while (g_variant_iter_loop(secondary_iter, "(udddd)", &temp_page,
&temp_rect.x1, &temp_rect.x2, &temp_rect.y1,
&temp_rect.y2)) {
if (temp_page >= number_of_pages) {
/* error out here? */
girara_debug("Got invalid page number.");
continue;
}
GVariant* result = g_variant_new("(b)", true);
g_dbus_method_invocation_return_value(invocation, result);
}
if (rectangles[temp_page] == NULL) {
rectangles[temp_page] = girara_list_new2(g_free);
}
static void
handle_synctex_view(zathura_t* zathura, GVariant* parameters,
GDBusMethodInvocation* invocation)
{
gchar* input_file = NULL;
guint line = 0;
guint column = 0;
g_variant_get(parameters, "(suu)", &input_file, &line, &column);
zathura_rectangle_t* rect = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rect == NULL || rectangles[temp_page] == NULL) {
g_variant_iter_free(secondary_iter);
for (unsigned int p = 0; p != number_of_pages; ++p) {
girara_list_free(rectangles[p]);
}
g_free(rectangles);
g_free(rect);
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_NO_MEMORY,
"Failed to allocate memory.");
return;
}
const bool ret = synctex_view(zathura, input_file, line, column);
g_free(input_file);
*rect = temp_rect;
girara_list_append(rectangles[temp_page], rect);
}
g_variant_iter_free(secondary_iter);
GVariant* result = g_variant_new("(b)", ret);
g_dbus_method_invocation_return_value(invocation, result);
}
synctex_highlight_rects(priv->zathura, page, rectangles);
g_free(rectangles);
static void
handle_method_call(GDBusConnection* UNUSED(connection),
const gchar* UNUSED(sender), const gchar* object_path,
const gchar* interface_name, const gchar* method_name,
GVariant* parameters, GDBusMethodInvocation* invocation,
void* data)
{
ZathuraDbus* dbus = data;
private_t* priv = GET_PRIVATE(dbus);
GVariant* result = g_variant_new("(b)", true);
g_dbus_method_invocation_return_value(invocation, result);
} else if (g_strcmp0(method_name, "SynctexView") == 0) {
gchar* input_file = NULL;
guint line = 0;
guint column = 0;
g_variant_get(parameters, "(suu)", &input_file, &line, &column);
girara_debug("Handling call '%s.%s' on '%s'.", interface_name, method_name,
object_path);
const bool ret = synctex_view(priv->zathura, input_file, line, column);
g_free(input_file);
static const struct {
const char* method;
void (*handler)(zathura_t*, GVariant*, GDBusMethodInvocation*);
bool needs_document;
} handlers[] = {
{ "OpenDocument", handle_open_document, false },
{ "CloseDocument", handle_close_document, false },
{ "GotoPage", handle_goto_page, true },
{ "HighlightRects", handle_highlight_rects, true },
{ "SynctexView", handle_synctex_view, true }
};
for (size_t idx = 0; idx != sizeof(handlers) / sizeof(handlers[0]); ++idx) {
if (g_strcmp0(method_name, handlers[idx].method) != 0) {
continue;
}
GVariant* result = g_variant_new("(b)", ret);
g_dbus_method_invocation_return_value(invocation, result);
if (handlers[idx].needs_document == true && priv->zathura->document == NULL) {
g_dbus_method_invocation_return_dbus_error(
invocation, "org.pwmt.zathura.NoOpenDocumen",
"No document has been opened.");
return;
}
(*handlers[idx].handler)(priv->zathura, parameters, invocation);
return;
}
}
......
Supports Markdown
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