zathura/utils.c

229 lines
5.3 KiB
C
Raw Normal View History

2010-11-18 02:35:33 +01:00
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
2011-03-05 21:00:41 +01:00
#include <stdarg.h>
2010-11-18 03:15:32 +01:00
#include <string.h>
#include <stdio.h>
2010-11-18 02:35:33 +01:00
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
2010-11-18 02:35:33 +01:00
#include "utils.h"
2011-02-10 04:33:28 +01:00
#include "zathura.h"
#include "document.h"
2010-11-18 02:35:33 +01:00
#include <girara/datastructures.h>
#define BLOCK_SIZE 64
2010-11-18 02:35:33 +01:00
bool
file_exists(const char* path)
{
2011-02-09 12:29:09 +01:00
if (!access(path, F_OK)) {
2010-11-18 02:35:33 +01:00
return true;
} else {
return false;
}
}
const char*
file_get_extension(const char* path)
{
2011-02-09 12:29:09 +01:00
if (!path) {
2010-11-18 03:15:32 +01:00
return NULL;
}
unsigned int i = strlen(path);
2011-02-09 12:29:09 +01:00
for (; i > 0; i--)
2010-11-18 03:15:32 +01:00
{
2011-02-09 12:29:09 +01:00
if (*(path + i) != '.') {
2010-11-18 03:15:32 +01:00
continue;
2010-12-26 01:12:20 +01:00
} else {
2010-11-18 03:15:32 +01:00
break;
2010-12-26 01:12:20 +01:00
}
2010-11-18 03:15:32 +01:00
}
2011-02-09 12:29:09 +01:00
if (!i) {
2010-11-18 03:15:32 +01:00
return NULL;
}
return path + i + 1;
2010-11-18 02:35:33 +01:00
}
bool
file_valid_extension(zathura_t* zathura, const char* path)
{
if (path == NULL) {
return false;
}
2011-09-29 15:23:13 +02:00
const gchar* content_type = g_content_type_guess(path, NULL, 0, NULL);
if (content_type == NULL) {
return false;
}
2011-09-29 15:23:13 +02:00
bool result = false;
GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
if (g_content_type_equals(content_type, mapping->type)) {
result = true;
break;
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
2011-09-29 18:08:37 +02:00
g_free((void*)content_type);
2011-09-29 15:23:13 +02:00
return result;
}
bool
execute_command(char* const argv[], char** output)
{
2011-02-09 12:29:09 +01:00
if (!output) {
return false;
}
int p[2];
2011-02-09 12:29:09 +01:00
if (pipe(p)) {
return -1;
2010-12-26 01:12:20 +01:00
}
pid_t pid = fork();
2011-02-09 12:29:09 +01:00
if (pid == -1) { // failure
return false;
2011-02-09 12:29:09 +01:00
} else if (pid == 0) { // child
dup2(p[1], 1);
close(p[0]);
2011-02-09 12:29:09 +01:00
if (execvp(argv[0], argv) == -1) {
return false;
}
2010-12-26 01:12:20 +01:00
} else { // parent
dup2(p[0], 0);
close(p[1]);
/* read output */
unsigned int bc = BLOCK_SIZE;
unsigned int i = 0;
char* buffer = malloc(sizeof(char) * bc);
*output = NULL;
2011-02-09 12:29:09 +01:00
if (!buffer) {
close(p[0]);
return false;
}
char c;
2011-02-09 12:29:09 +01:00
while (1 == read(p[0], &c, 1)) {
buffer[i++] = c;
2011-02-09 12:29:09 +01:00
if (i == bc) {
bc += BLOCK_SIZE;
char* tmp = realloc(buffer, sizeof(char) * bc);
2011-02-09 12:29:09 +01:00
if (!tmp) {
free(buffer);
close(p[0]);
return false;
}
buffer = tmp;
}
}
char* tmp = realloc(buffer, sizeof(char) * (bc + 1));
2011-02-09 12:29:09 +01:00
if (!tmp) {
free(buffer);
close(p[0]);
return false;
}
buffer = tmp;
buffer[i] = '\0';
*output = buffer;
/* wait for child to terminate */
waitpid(pid, NULL, 0);
close(p[0]);
}
return true;
}
2010-12-28 09:47:09 +01:00
2011-02-10 04:33:28 +01:00
void
2011-09-29 16:50:52 +02:00
document_index_build(GtkTreeModel* model, GtkTreeIter* parent,
girara_tree_node_t* tree)
2011-02-10 04:33:28 +01:00
{
2011-09-29 16:50:52 +02:00
girara_list_t* list = girara_node_get_children(tree);
GIRARA_LIST_FOREACH(list, girara_tree_node_t*, iter, node)
zathura_index_element_t* index_element = (zathura_index_element_t*)girara_node_get_data(node);
2011-02-10 04:33:28 +01:00
2011-09-29 17:28:09 +02:00
gchar* description = NULL;
if (index_element->type == ZATHURA_LINK_TO_PAGE) {
description = g_strdup_printf("Page %d", index_element->target.page_number);
} else {
description = g_strdup(index_element->target.uri);
}
2011-09-29 16:50:52 +02:00
GtkTreeIter tree_iter;
gtk_tree_store_append(GTK_TREE_STORE(model), &tree_iter, parent);
2011-09-29 17:28:09 +02:00
gtk_tree_store_set(GTK_TREE_STORE(model), &tree_iter, 0, index_element->title, 1, description, 2, index_element, -1);
2011-09-29 16:50:52 +02:00
g_object_weak_ref(G_OBJECT(model), (GWeakNotify) zathura_index_element_free, index_element);
2011-09-29 17:28:09 +02:00
g_free(description);
2011-09-29 16:50:52 +02:00
if (girara_node_get_num_children(node) > 0) {
document_index_build(model, &tree_iter, node);
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(list, gchar*, iter, name);
2011-02-10 04:33:28 +01:00
}
2011-03-05 21:00:41 +01:00
void
page_calculate_offset(zathura_page_t* page, page_offset_t* offset)
2011-04-19 19:24:03 +02:00
{
g_return_if_fail(page != NULL && page->document != NULL && page->document->zathura != NULL && offset != NULL);
2011-04-19 19:24:03 +02:00
zathura_document_t* document = page->document;
zathura_t* zathura = document->zathura;
2012-02-07 18:00:47 +01:00
g_return_if_fail(gtk_widget_translate_coordinates(page->drawing_area,
zathura->ui.page_view, 0, 0, &(offset->x), &(offset->y)) == true);
2011-04-19 19:24:03 +02:00
}
zathura_rectangle_t
recalc_rectangle(zathura_page_t* page, zathura_rectangle_t rectangle)
{
if (page == NULL || page->document == NULL) {
return rectangle;
}
zathura_rectangle_t tmp;
switch (page->document->rotate) {
case 90:
tmp.x1 = rectangle.y2 * page->document->scale;
tmp.x2 = rectangle.y1 * page->document->scale;
tmp.y1 = rectangle.x1 * page->document->scale;
tmp.y2 = rectangle.x2 * page->document->scale;
break;
case 180:
tmp.x1 = (page->width - rectangle.x2) * page->document->scale;
tmp.x2 = (page->width - rectangle.x1) * page->document->scale;
tmp.y1 = rectangle.y2 * page->document->scale;
tmp.y2 = rectangle.y1 * page->document->scale;
break;
case 270:
tmp.x1 = (page->height - rectangle.y1) * page->document->scale;
tmp.x2 = (page->height - rectangle.y2) * page->document->scale;
tmp.y1 = (page->width - rectangle.x2) * page->document->scale;
tmp.y2 = (page->width - rectangle.x1) * page->document->scale;
break;
default:
tmp.x1 = rectangle.x1 * page->document->scale;
tmp.x2 = rectangle.x2 * page->document->scale;
tmp.y1 = (page->height - rectangle.y1) * page->document->scale;
tmp.y2 = (page->height - rectangle.y2) * page->document->scale;
}
return tmp;
}