Commit abd52e45 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Use new foreach macros

Also fixes some memory leaks.
parent 93336db1
......@@ -108,9 +108,9 @@ cmd_bookmark_open(girara_session_t* session, girara_list_t* argument_list)
if (argc != 1) {
GString* string = g_string_new(NULL);
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
GIRARA_LIST_FOREACH_BODY(zathura->bookmarks.bookmarks, zathura_bookmark_t*, bookmark,
g_string_append_printf(string, "<b>%s</b>: %u\n", bookmark->id, bookmark->page);
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark);
);
if (strlen(string->str) > 0) {
g_string_erase(string, strlen(string->str) - 1, 1);
......@@ -190,15 +190,15 @@ cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list))
GString* string = g_string_new(NULL);
GIRARA_LIST_FOREACH(information, zathura_document_information_entry_t*, iter, entry)
if (entry != NULL) {
for (unsigned int i = 0; i < LENGTH(meta_fields); i++) {
if (meta_fields[i].field == entry->type) {
g_string_append_printf(string, "<b>%s:</b> %s\n", meta_fields[i].name, entry->value);
GIRARA_LIST_FOREACH_BODY(information, zathura_document_information_entry_t*, entry,
if (entry != NULL) {
for (unsigned int i = 0; i < LENGTH(meta_fields); i++) {
if (meta_fields[i].field == entry->type) {
g_string_append_printf(string, "<b>%s:</b> %s\n", meta_fields[i].name, entry->value);
}
}
}
}
GIRARA_LIST_FOREACH_END(information, zathura_document_information_entry_t*, iter, entry);
);
if (strlen(string->str) > 0) {
g_string_erase(string, strlen(string->str) - 1, 1);
......@@ -535,7 +535,7 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list)
if (zathura->document != NULL) {
const char* path = zathura_document_get_path(zathura->document);
GIRARA_LIST_FOREACH(argument_list, char*, iter, value)
GIRARA_LIST_FOREACH_BODY_WITH_ITER(argument_list, char*, iter, value,
char* r = girara_replace_substring(value, "$FILE", path);
if (r != NULL) {
......@@ -546,7 +546,7 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list)
girara_list_iterator_set(iter, s);
}
}
GIRARA_LIST_FOREACH_END(argument_list, char*, iter, value);
);
}
return girara_exec_with_argument_list(session, argument_list);
......
......@@ -184,9 +184,9 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, in
goto error_free;
}
GIRARA_LIST_FOREACH(names, const char*, iter, file)
girara_completion_group_add_element(group, file, NULL);
GIRARA_LIST_FOREACH_END(names, const char*, iter, file);
GIRARA_LIST_FOREACH_BODY(names, const char*, file,
girara_completion_group_add_element(group, file, NULL);
);
girara_list_free(names);
}
......@@ -197,10 +197,10 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, in
}
if (girara_list_size(recent_files) != 0) {
GIRARA_LIST_FOREACH(recent_files, const char*, iter, file)
GIRARA_LIST_FOREACH_BODY(recent_files, const char*, file,
girara_debug("adding %s (recent file)", file);
girara_completion_group_add_element(history_group, file, NULL);
GIRARA_LIST_FOREACH_END(recent_files, const char*, iter, file);
);
girara_list_free(recent_files);
} else {
girara_completion_group_free(history_group);
......@@ -278,13 +278,13 @@ cc_bookmarks(girara_session_t* session, const char* input)
}
const size_t input_length = strlen(input);
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
if (input_length <= strlen(bookmark->id) && !strncmp(input, bookmark->id, input_length)) {
gchar* paged = g_strdup_printf(_("Page %d"), bookmark->page);
girara_completion_group_add_element(group, bookmark->id, paged);
g_free(paged);
}
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark);
GIRARA_LIST_FOREACH_BODY(zathura->bookmarks.bookmarks, zathura_bookmark_t*, bookmark,
if (input_length <= strlen(bookmark->id) && !strncmp(input, bookmark->id, input_length)) {
gchar* paged = g_strdup_printf(_("Page %d"), bookmark->page);
girara_completion_group_add_element(group, bookmark->id, paged);
g_free(paged);
}
);
girara_completion_add_group(completion, group);
......@@ -334,14 +334,14 @@ cc_export(girara_session_t* session, const char* input)
if (attachments != NULL) {
bool added = false;
GIRARA_LIST_FOREACH(attachments, const char*, iter, attachment)
if (input_length <= strlen(attachment) && !strncmp(input, attachment, input_length)) {
char* attachment_string = g_strdup_printf("attachment-%s", attachment);
girara_completion_group_add_element(attachment_group, attachment_string, NULL);
g_free(attachment_string);
added = true;
}
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark);
GIRARA_LIST_FOREACH_BODY(attachments, const char*, attachment,
if (input_length <= strlen(attachment) && !strncmp(input, attachment, input_length)) {
char* attachment_string = g_strdup_printf("attachment-%s", attachment);
girara_completion_group_add_element(attachment_group, attachment_string, NULL);
g_free(attachment_string);
added = true;
}
);
if (added == true) {
girara_completion_add_group(completion, attachment_group);
......@@ -371,14 +371,14 @@ cc_export(girara_session_t* session, const char* input)
girara_list_t* images = zathura_page_images_get(page, NULL);
if (images != NULL) {
unsigned int image_number = 1;
GIRARA_LIST_FOREACH(images, zathura_image_t*, iter, UNUSED(image))
char* image_string = g_strdup_printf("image-p%d-%d", page_id + 1, image_number);
girara_completion_group_add_element(image_group, image_string, NULL);
g_free(image_string);
added = true;
image_number++;
GIRARA_LIST_FOREACH_END(images, zathura_image_t*, iter, image);
GIRARA_LIST_FOREACH_BODY(images, zathura_image_t*, UNUSED(image),
char* image_string = g_strdup_printf("image-p%d-%d", page_id + 1, image_number);
girara_completion_group_add_element(image_group, image_string, NULL);
g_free(image_string);
added = true;
image_number++;
);
girara_list_free(images);
}
}
......
......@@ -518,7 +518,7 @@ plain_save_jumplist(zathura_database_t* db, const char* file, girara_list_t* jum
GString* str_val = g_string_new(NULL);
GIRARA_LIST_FOREACH(jumplist, zathura_jump_t*, iter, jump)
GIRARA_LIST_FOREACH_BODY(jumplist, zathura_jump_t*, jump,
char buffer[G_ASCII_DTOSTR_BUF_SIZE] = { '\0' };
g_string_append_printf(str_val, "%d ", jump->page);
......@@ -526,7 +526,7 @@ plain_save_jumplist(zathura_database_t* db, const char* file, girara_list_t* jum
g_string_append_c(str_val, ' ');
g_string_append(str_val, g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, jump->y));
g_string_append_c(str_val, ' ');
GIRARA_LIST_FOREACH_END(jumplist, zathura_jump_t*, iter, jump);
);
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);
......
......@@ -158,35 +158,35 @@ cmd_marks_delete(girara_session_t* session, girara_list_t* argument_list)
return false;
}
GIRARA_LIST_FOREACH(argument_list, char*, iter, key_string)
if (key_string == NULL) {
girara_list_iterator_next(iter);
continue;
}
for (unsigned int i = 0; i < strlen(key_string); i++) {
char key = key_string[i];
if (((key >= 0x41 && key <= 0x5A) || (key >=
0x61 && key <= 0x7A)) == false) {
GIRARA_LIST_FOREACH_BODY_WITH_ITER(argument_list, char*, iter, key_string,
if (key_string == NULL) {
girara_list_iterator_next(iter);
continue;
}
/* search for existing mark */
girara_list_iterator_t* mark_iter = girara_list_iterator(zathura->global.marks);
do {
zathura_mark_t* mark = (zathura_mark_t*) girara_list_iterator_data(mark_iter);
if (mark == NULL) {
for (unsigned int i = 0; i < strlen(key_string); i++) {
char key = key_string[i];
if (((key >= 0x41 && key <= 0x5A) || (key >=
0x61 && key <= 0x7A)) == false) {
continue;
}
if (mark->key == key) {
girara_list_remove(zathura->global.marks, mark);
continue;
}
} while (girara_list_iterator_next(mark_iter) != NULL);
girara_list_iterator_free(mark_iter);
}
GIRARA_LIST_FOREACH_END(argument_list, char*, iter, key_string);
/* search for existing mark */
girara_list_iterator_t* mark_iter = girara_list_iterator(zathura->global.marks);
do {
zathura_mark_t* mark = (zathura_mark_t*) girara_list_iterator_data(mark_iter);
if (mark == NULL) {
continue;
}
if (mark->key == key) {
girara_list_remove(zathura->global.marks, mark);
continue;
}
} while (girara_list_iterator_next(mark_iter) != NULL);
girara_list_iterator_free(mark_iter);
}
);
return true;
}
......@@ -205,15 +205,16 @@ mark_add(zathura_t* zathura, int key)
double scale = zathura_document_get_scale(zathura->document);
/* search for existing mark */
GIRARA_LIST_FOREACH(zathura->global.marks, zathura_mark_t*, iter, mark)
if (mark->key == key) {
mark->page = page_id;
mark->position_x = position_x;
mark->position_y = position_y;
mark->scale = scale;
return;
}
GIRARA_LIST_FOREACH_END(zathura->global.marks, zathura_mark_t*, iter, mark);
GIRARA_LIST_FOREACH_BODY_WITH_ITER(zathura->global.marks, zathura_mark_t*, iter, mark,
if (mark->key == key) {
mark->page = page_id;
mark->position_x = position_x;
mark->position_y = position_y;
mark->scale = scale;
girara_list_iterator_free(iter);
return;
}
);
/* add new mark */
zathura_mark_t* mark = g_try_malloc0(sizeof(zathura_mark_t));
......@@ -238,20 +239,20 @@ mark_evaluate(zathura_t* zathura, int key)
}
/* search for existing mark */
GIRARA_LIST_FOREACH(zathura->global.marks, zathura_mark_t*, iter, mark)
if (mark != NULL && mark->key == key) {
zathura_document_set_scale(zathura->document,
zathura_correct_scale_value(zathura->ui.session, mark->scale));
render_all(zathura);
zathura_jumplist_add(zathura);
page_set(zathura, mark->page);
position_set(zathura, mark->position_x, mark->position_y);
zathura_jumplist_add(zathura);
return;
}
GIRARA_LIST_FOREACH_END(zathura->global.marks, zathura_mark_t*, iter, mark);
GIRARA_LIST_FOREACH_BODY(zathura->global.marks, zathura_mark_t*, mark,
if (mark != NULL && mark->key == key) {
zathura_document_set_scale(zathura->document,
zathura_correct_scale_value(zathura->ui.session, mark->scale));
render_all(zathura);
zathura_jumplist_add(zathura);
page_set(zathura, mark->page);
position_set(zathura, mark->position_x, mark->position_y);
zathura_jumplist_add(zathura);
break;
}
);
}
void
......
......@@ -401,18 +401,18 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v
/* get size of text that should be large enough for every link hint */
text = get_text_extents("888", priv->zathura, CAIRO_FONT_WEIGHT_BOLD);
GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link)
if (link != NULL) {
/* redraw link area */
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link));
redraw_rect(pageview, &rectangle);
/* also redraw area for link hint */
rectangle.x2 = rectangle.x1 + text.width;
rectangle.y1 = rectangle.y2 - text.height;
redraw_rect(pageview, &rectangle);
}
GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link);
GIRARA_LIST_FOREACH_BODY(priv->links.list, zathura_link_t*, link,
if (link != NULL) {
/* redraw link area */
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link));
redraw_rect(pageview, &rectangle);
/* also redraw area for link hint */
rectangle.x2 = rectangle.x1 + text.width;
rectangle.y1 = rectangle.y2 - text.height;
redraw_rect(pageview, &rectangle);
}
);
}
break;
case PROP_LINKS_OFFSET:
......@@ -589,46 +589,46 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo)
if (priv->links.draw == true && priv->links.n != 0) {
unsigned int link_counter = 0;
GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link)
if (link != NULL) {
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link));
/* draw position */
const GdkRGBA color = priv->zathura->ui.colors.highlight_color;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
cairo_rectangle(cairo, rectangle.x1, rectangle.y1,
(rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1));
cairo_fill(cairo);
GIRARA_LIST_FOREACH_BODY(priv->links.list, zathura_link_t*, link,
if (link != NULL) {
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, zathura_link_get_position(link));
/* draw text */
cairo_set_source_rgba(cairo, 0, 0, 0, 1);
cairo_move_to(cairo, rectangle.x1 + 1, rectangle.y2 - 1);
char* link_number = g_strdup_printf("%i", priv->links.offset + ++link_counter);
cairo_show_text(cairo, link_number);
g_free(link_number);
}
GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link);
/* draw position */
const GdkRGBA color = priv->zathura->ui.colors.highlight_color;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
cairo_rectangle(cairo, rectangle.x1, rectangle.y1,
(rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1));
cairo_fill(cairo);
/* draw text */
cairo_set_source_rgba(cairo, 0, 0, 0, 1);
cairo_move_to(cairo, rectangle.x1 + 1, rectangle.y2 - 1);
char* link_number = g_strdup_printf("%i", priv->links.offset + ++link_counter);
cairo_show_text(cairo, link_number);
g_free(link_number);
}
);
}
/* draw search results */
if (priv->search.list != NULL && priv->search.draw == true) {
int idx = 0;
GIRARA_LIST_FOREACH(priv->search.list, zathura_rectangle_t*, iter, rect)
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect);
GIRARA_LIST_FOREACH_BODY(priv->search.list, zathura_rectangle_t*, rect,
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect);
/* draw position */
if (idx == priv->search.current) {
const GdkRGBA color = priv->zathura->ui.colors.highlight_color_active;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
} else {
const GdkRGBA color = priv->zathura->ui.colors.highlight_color;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
}
cairo_rectangle(cairo, rectangle.x1, rectangle.y1,
(rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1));
cairo_fill(cairo);
++idx;
GIRARA_LIST_FOREACH_END(priv->search.list, zathura_rectangle_t*, iter, rect);
/* draw position */
if (idx == priv->search.current) {
const GdkRGBA color = priv->zathura->ui.colors.highlight_color_active;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
} else {
const GdkRGBA color = priv->zathura->ui.colors.highlight_color;
cairo_set_source_rgba(cairo, color.red, color.green, color.blue, transparency);
}
cairo_rectangle(cairo, rectangle.x1, rectangle.y1,
(rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1));
cairo_fill(cairo);
++idx;
);
}
/* draw selection */
if (priv->mouse.selection.y2 != -1 && priv->mouse.selection.x2 != -1) {
......@@ -860,10 +860,10 @@ redraw_all_rects(ZathuraPage* widget, girara_list_t* rectangles)
{
zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget);
GIRARA_LIST_FOREACH(rectangles, zathura_rectangle_t*, iter, rect)
GIRARA_LIST_FOREACH_BODY(rectangles, zathura_rectangle_t*, rect,
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect);
redraw_rect(widget, &rectangle);
GIRARA_LIST_FOREACH_END(rectangles, zathura_recantgle_t*, iter, rect);
);
}
zathura_link_t*
......@@ -963,13 +963,13 @@ cb_zathura_page_widget_button_release_event(GtkWidget* widget, GdkEventButton* b
}
if (priv->links.list != NULL && priv->links.n > 0) {
GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link)
zathura_rectangle_t rect = recalc_rectangle(priv->page, zathura_link_get_position(link));
if (rect.x1 <= button->x && rect.x2 >= button->x
&& rect.y1 <= button->y && rect.y2 >= button->y) {
zathura_link_evaluate(priv->zathura, link);
}
GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link);
GIRARA_LIST_FOREACH_BODY(priv->links.list, zathura_link_t*, link,
const zathura_rectangle_t rect = recalc_rectangle(priv->page, zathura_link_get_position(link));
if (rect.x1 <= button->x && rect.x2 >= button->x
&& rect.y1 <= button->y && rect.y2 >= button->y) {
zathura_link_evaluate(priv->zathura, link);
}
);
}
} else {
redraw_rect(ZATHURA_PAGE(widget), &priv->mouse.selection);
......@@ -1017,12 +1017,13 @@ cb_zathura_page_widget_motion_notify(GtkWidget* widget, GdkEventMotion* event)
if (priv->links.list != NULL && priv->links.n > 0) {
bool over_link = false;
GIRARA_LIST_FOREACH(priv->links.list, zathura_link_t*, iter, link)
GIRARA_LIST_FOREACH_BODY(priv->links.list, zathura_link_t*, link,
zathura_rectangle_t rect = recalc_rectangle(priv->page, zathura_link_get_position(link));
if (rect.x1 <= event->x && rect.x2 >= event->x && rect.y1 <= event->y && rect.y2 >= event->y) {
over_link = true;
break;
}
GIRARA_LIST_FOREACH_END(priv->links.list, zathura_link_t*, iter, link);
);
if (priv->mouse.over_link != over_link) {
if (over_link == true) {
......@@ -1096,12 +1097,12 @@ zathura_page_widget_popup_menu(GtkWidget* widget, GdkEventButton* event)
/* search for underlaying image */
zathura_image_t* image = NULL;
GIRARA_LIST_FOREACH(priv->images.list, zathura_image_t*, iter, image_it)
GIRARA_LIST_FOREACH_BODY(priv->images.list, zathura_image_t*, image_it,
zathura_rectangle_t rect = recalc_rectangle(priv->page, image_it->position);
if (rect.x1 <= event->x && rect.x2 >= event->x && rect.y1 <= event->y && rect.y2 >= event->y) {
image = image_it;
}
GIRARA_LIST_FOREACH_END(priv->images.list, zathura_image_t*, iter, image_it);
);
if (image == NULL) {
return;
......@@ -1184,13 +1185,13 @@ cb_menu_image_save(GtkMenuItem* item, ZathuraPage* page)
unsigned int page_id = zathura_page_get_index(priv->page) + 1;
unsigned int image_id = 1;
GIRARA_LIST_FOREACH(priv->images.list, zathura_image_t*, iter, image_it)
GIRARA_LIST_FOREACH_BODY(priv->images.list, zathura_image_t*, image_it,
if (image_it == priv->images.current) {
break;
}
image_id++;
GIRARA_LIST_FOREACH_END(priv->images.list, zathura_image_t*, iter, image_it);
);
/* set command */
char* export_command = g_strdup_printf(":export image-p%d-%d ", page_id, image_id);
......
......@@ -100,104 +100,104 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
return;
}
GIRARA_LIST_FOREACH(plugin_manager->path, char*, iter, plugindir)
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
char* path = g_build_filename(plugindir, name, NULL);
if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
girara_debug("%s is not a regular file. Skipping.", path);
g_free(path);
continue;
}
if (check_suffix(path) == false) {
girara_debug("%s is not a plugin file. Skipping.", path);
g_free(path);
continue;
}
zathura_plugin_t* plugin = NULL;
/* load plugin */
GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL);
if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, g_module_error());
g_free(path);
continue;
}
/* resolve symbols and check API and ABI version*/
const zathura_plugin_definition_t* plugin_definition = NULL;
if (g_module_symbol(handle, G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), (void**) &plugin_definition) == FALSE ||
plugin_definition == NULL) {
girara_error("could not find '%s' in plugin %s - is not a plugin or needs to be rebuilt", G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), path);
g_free(path);
g_module_close(handle);
continue;
}
/* check name */
if (plugin_definition->name == NULL) {
girara_error("plugin has no name");
g_free(path);
g_free(plugin);
g_module_close(handle);
continue;
}
/* check mime type */
if (plugin_definition->mime_types == NULL || plugin_definition->mime_types_size == 0) {
girara_error("plugin has no mime_types");
g_free(path);
g_free(plugin);
g_module_close(handle);
GIRARA_LIST_FOREACH_BODY_WITH_ITER(plugin_manager->path, char*, iter, plugindir,
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
plugin = g_try_malloc0(sizeof(zathura_plugin_t));
if (plugin == NULL) {
girara_error("Failed to allocate memory for plugin.");
g_free(path);
g_module_close(handle);
continue;
}
plugin->definition = plugin_definition;
plugin->functions = plugin_definition->functions;
plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle;
plugin->path = path;
// register mime types
for (size_t s = 0; s != plugin_definition->mime_types_size; ++s) {
zathura_plugin_add_mimetype(plugin, plugin_definition->mime_types[s]);
}
// register functions
if (plugin->definition->register_function != NULL) {
plugin->definition->register_function(&(plugin->functions));
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
char* path = g_build_filename(plugindir, name, NULL);
if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
girara_debug("%s is not a regular file. Skipping.", path);
g_free(path);
continue;
}
if (check_suffix(path) == false) {
girara_debug("%s is not a plugin file. Skipping.", path);
g_free(path);
continue;
}
zathura_plugin_t* plugin = NULL;
/* load plugin */
GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL);
if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, g_module_error());
g_free(path);
continue;
}
/* resolve symbols and check API and ABI version*/
const zathura_plugin_definition_t* plugin_definition = NULL;
if (g_module_symbol(handle, G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), (void**) &plugin_definition) == FALSE ||
plugin_definition == NULL) {
girara_error("could not find '%s' in plugin %s - is not a plugin or needs to be rebuilt", G_STRINGIFY(ZATHURA_PLUGIN_DEFINITION_SYMBOL), path);
g_free(path);
g_module_close(handle);
continue;
}
/* check name */
if (plugin_definition->name == NULL) {
girara_error("plugin has no name");
g_free(path);
g_free(plugin);
g_module_close(handle);
continue;
}
/* check mime type */
if (plugin_definition->mime_types == NULL || plugin_definition->mime_types_size == 0) {
girara_error("plugin has no mime_types");
g_free(path);
g_free(plugin);
g_module_close(handle);
continue;
}
plugin = g_try_malloc0(sizeof(zathura_plugin_t));
if (plugin == NULL) {
girara_error("Failed to allocate memory for plugin.");
g_free(path);
g_module_close(handle);
continue;
}
plugin->definition = plugin_definition;
plugin->functions = plugin_definition->functions;
plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle;
plugin->path = path;
// register mime types
for (size_t s = 0; s != plugin_definition->mime_types_size; ++s) {
zathura_plugin_add_mimetype(plugin, plugin_definition->mime_types[s]);
}
// register functions
if (plugin->definition->register_function != NULL) {
plugin->definition->register_function(&(plugin->functions));
}
bool ret = register_plugin(plugin_manager, plugin);
if (ret == false) {
girara_error("could not register plugin %s", path);
zathura_plugin_free(plugin);
} else {
girara_debug("successfully loaded plugin from %s", path);
girara_debug("plugin %s: version %u.%u.%u", plugin_definition->name,
plugin_definition->version.major, plugin_definition->version.minor,
plugin_definition->version.rev);
}
}
bool ret = register_plugin(plugin_manager, plugin);
if (ret == false) {
girara_error("could not register plugin %s", path);
zathura_plugin_free(plugin);
} else {
girara_debug("successfully loaded plugin from %s", path);
girara_debug("plugin %s: version %u.%u.%u", plugin_definition->name,
plugin_definition->version.major, plugin_definition->version.minor,
plugin_definition->version.rev);
}
}
g_dir_close(dir);
GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
g_dir_close(dir);
);
}
zathura_plugin_t*
......@@ -208,12 +208,12 @@ zathura_plugin_manager_get_plugin(zathura_plugin_manager_t* plugin_manager, cons
}
zathura_plugin_t* plugin = NULL;
GIRARA_LIST_FOREACH(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
if (g_content_type_equals(type, mapping->type)) {
plugin = mapping->plugin;
break;
}
GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
GIRARA_LIST_FOREACH_BODY(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, mapping,
if (g_content_type_equals(type, mapping->type)) {
plugin = mapping->plugin;
break;
}
);
return plugin;
}
......@@ -262,13 +262,13 @@ register_plugin(zathura_plugin_manager_t* plugin_manager, zathura_plugin_t* plug
}
bool at_least_one = false;
GIRARA_LIST_FOREACH(plugin->content_types, gchar*, iter, type)
if (plugin_mapping_new(plugin_manager, type, plugin) == false) {
girara_error("plugin: already registered for filetype %s\n", type);
} else {
at_least_one = true;
}