/* utilities.c generated by valac 0.56.19, the Vala compiler
 * generated from utilities.vala, do not modify */

/* SPDX-License-Identifier: GPL-3.0-or-later*/

#include "graphs.h"
#include <glib.h>
#include <float.h>
#include <math.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <stdarg.h>
#include <glib/gi18n-lib.h>
#include <unistd.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_settings_schema_unref0(var) ((var == NULL) ? NULL : (var = (g_settings_schema_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_uri_unref0(var) ((var == NULL) ? NULL : (var = (g_uri_unref (var), NULL)))

VALA_EXTERN gdouble graphs_tools_get_srgb (gdouble color);
static gboolean _vala_string_array_contains (gchar* * stack,
                                      gssize stack_length,
                                      const gchar* needle);
VALA_EXTERN gchar* graphs_tools_get_friendly_path (GFile* file);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

/**
     * Get value at axis fraction.
     *
     * Obtain the selected value of an axis given at which percentage (in terms
     * of fraction) of the length this axis is selected given the start and end
     * range of this axis.
     */
gdouble
graphs_get_value_at_fraction (gdouble fraction,
                              gdouble start,
                              gdouble end,
                              GraphsScale scale)
{
	gdouble result;
	switch (scale) {
		case GRAPHS_SCALE_LINEAR:
		case GRAPHS_SCALE_RADIANS:
		{
			result = start + (fraction * (end - start));
			return result;
		}
		case GRAPHS_SCALE_LOG:
		{
			{
				gdouble log_start = 0.0;
				gdouble log_end = 0.0;
				gdouble log_range = 0.0;
				gdouble log_value = 0.0;
				log_start = log10 (start);
				log_end = log10 (end);
				log_range = log_end - log_start;
				log_value = log_start + (log_range * fraction);
				result = pow (10., log_value);
				return result;
			}
		}
		case GRAPHS_SCALE_LOG2:
		{
			{
				gdouble log_start = 0.0;
				gdouble log_end = 0.0;
				gdouble log_range = 0.0;
				gdouble log_value = 0.0;
				log_start = log2 (start);
				log_end = log2 (end);
				log_range = log_end - log_start;
				log_value = log_start + (log_range * fraction);
				result = pow (2., log_value);
				return result;
			}
		}
		case GRAPHS_SCALE_SQUAREROOT:
		{
			{
				gdouble sqrt_start = 0.0;
				gdouble sqrt_end = 0.0;
				gdouble sqrt_range = 0.0;
				gdouble sqrt_value = 0.0;
				sqrt_start = fmax (0., sqrt (start));
				sqrt_end = sqrt (end);
				sqrt_range = sqrt_end - sqrt_start;
				sqrt_value = sqrt_start + (sqrt_range * fmax (0., fraction));
				result = sqrt_value * sqrt_value;
				return result;
			}
		}
		case GRAPHS_SCALE_INVERSE:
		{
			{
				gdouble adjusted_start = 0.0;
				gboolean _tmp0_ = FALSE;
				gdouble scaled_range = 0.0;
				adjusted_start = start;
				if (start <= 0.) {
					_tmp0_ = end > 0.;
				} else {
					_tmp0_ = FALSE;
				}
				if (_tmp0_) {
					adjusted_start = end / 10.;
				}
				scaled_range = (1. / adjusted_start) - (1. / end);
				result = 1. / ((1. / end) + (fraction * scaled_range));
				return result;
			}
		}
		default:
		{
			g_assert_not_reached ();
		}
	}
}

/**
     * Get fraction of axis at absolute value.
     *
     * Obtain the fraction of the total length of the selected axis a specific
     * value corresponds to given the start and end range of the axis.
     */
gdouble
graphs_get_fraction_at_value (gdouble val,
                              gdouble start,
                              gdouble end,
                              GraphsScale scale)
{
	gdouble result;
	switch (scale) {
		case GRAPHS_SCALE_LINEAR:
		case GRAPHS_SCALE_RADIANS:
		{
			result = (val - start) / (end - start);
			return result;
		}
		case GRAPHS_SCALE_LOG:
		{
			{
				gdouble log_start = 0.0;
				gdouble log_end = 0.0;
				gdouble log_value = 0.0;
				gdouble log_range = 0.0;
				log_start = log10 (start);
				log_end = log10 (end);
				log_value = log10 (val);
				log_range = log_end - log_start;
				result = (log_value - log_start) / log_range;
				return result;
			}
		}
		case GRAPHS_SCALE_LOG2:
		{
			{
				gdouble log_start = 0.0;
				gdouble log_end = 0.0;
				gdouble log_value = 0.0;
				gdouble log_range = 0.0;
				log_start = log2 (start);
				log_end = log2 (end);
				log_value = log2 (val);
				log_range = log_end - log_start;
				result = (log_value - log_start) / log_range;
				return result;
			}
		}
		case GRAPHS_SCALE_SQUAREROOT:
		{
			{
				gdouble adjusted_start = 0.0;
				gdouble sqrt_start = 0.0;
				gdouble sqrt_end = 0.0;
				gdouble sqrt_value = 0.0;
				gdouble sqrt_range = 0.0;
				adjusted_start = fmax (0., start);
				sqrt_start = sqrt (adjusted_start);
				sqrt_end = sqrt (end);
				sqrt_value = sqrt (val);
				sqrt_range = sqrt_end - sqrt_start;
				result = (sqrt_value - sqrt_start) / sqrt_range;
				return result;
			}
		}
		case GRAPHS_SCALE_INVERSE:
		{
			{
				gdouble adjusted_start = 0.0;
				gboolean _tmp0_ = FALSE;
				gdouble scaled_range = 0.0;
				gdouble scaled_data_point = 0.0;
				adjusted_start = start;
				if (start <= 0.) {
					_tmp0_ = end > 0.;
				} else {
					_tmp0_ = FALSE;
				}
				if (_tmp0_) {
					adjusted_start = end / 10.;
				}
				scaled_range = (1. / adjusted_start) - (1. / end);
				scaled_data_point = 1. / val;
				result = (scaled_data_point - (1. / end)) / scaled_range;
				return result;
			}
		}
		default:
		{
			g_assert_not_reached ();
		}
	}
}

/**
         * Reset a settings instance to default values.
         */
void
graphs_tools_reset_settings (GSettings* settings)
{
	GSettingsSchema* _tmp0_;
	GSettingsSchema* _tmp1_;
	GSettingsSchema* _tmp2_;
	gchar** _tmp3_;
	gchar** _tmp4_;
	gchar** _tmp5_;
	gint _tmp5__length1;
	g_return_if_fail (settings != NULL);
	g_object_get (settings, "settings-schema", &_tmp0_, NULL);
	_tmp1_ = _tmp0_;
	_tmp2_ = _tmp1_;
	_tmp4_ = _tmp3_ = g_settings_schema_list_keys (_tmp2_);
	_tmp5_ = _tmp4_;
	_tmp5__length1 = _vala_array_length (_tmp3_);
	_g_settings_schema_unref0 (_tmp2_);
	{
		gchar** key_collection = NULL;
		gint key_collection_length1 = 0;
		gint _key_collection_size_ = 0;
		gint key_it = 0;
		key_collection = _tmp5_;
		key_collection_length1 = _tmp5__length1;
		for (key_it = 0; key_it < key_collection_length1; key_it = key_it + 1) {
			gchar* _tmp6_;
			gchar* key = NULL;
			_tmp6_ = g_strdup (key_collection[key_it]);
			key = _tmp6_;
			{
				const gchar* _tmp7_;
				_tmp7_ = key;
				g_settings_reset (settings, _tmp7_);
				_g_free0 (key);
			}
		}
		key_collection = (_vala_array_free (key_collection, key_collection_length1, (GDestroyNotify) g_free), NULL);
	}
}

/**
         * Convert a color in hex representation to Gdk.RGBA
         */
void
graphs_tools_hex_to_rgba (const gchar* hex,
                          GdkRGBA* result)
{
	GdkRGBA rgba = {0};
	g_return_if_fail (hex != NULL);
	memset (&rgba, 0, sizeof (GdkRGBA));
	gdk_rgba_parse (&rgba, hex);
	*result = rgba;
	return;
}

/**
         * Convert a Gdk.RGBA to a color in hex representation
         */
gchar*
graphs_tools_rgba_to_hex (GdkRGBA* rgba)
{
	GString* builder = NULL;
	GString* _tmp0_;
	GdkRGBA _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	GdkRGBA _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	GdkRGBA _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* result;
	g_return_val_if_fail (rgba != NULL, NULL);
	_tmp0_ = g_string_new ("#");
	builder = _tmp0_;
	_tmp1_ = *rgba;
	_tmp2_ = g_strdup_printf ("%02x", (guint) (_tmp1_.red * 255));
	_tmp3_ = _tmp2_;
	g_string_append (builder, _tmp3_);
	_g_free0 (_tmp3_);
	_tmp4_ = *rgba;
	_tmp5_ = g_strdup_printf ("%02x", (guint) (_tmp4_.green * 255));
	_tmp6_ = _tmp5_;
	g_string_append (builder, _tmp6_);
	_g_free0 (_tmp6_);
	_tmp7_ = *rgba;
	_tmp8_ = g_strdup_printf ("%02x", (guint) (_tmp7_.blue * 255));
	_tmp9_ = _tmp8_;
	g_string_append (builder, _tmp9_);
	_g_free0 (_tmp9_);
	_tmp10_ = builder->str;
	_tmp11_ = g_utf8_strup (_tmp10_, (gssize) -1);
	result = _tmp11_;
	_g_string_free0 (builder);
	return result;
}

/**
         * Calculate luminance of a color in hex representation.
         */
gdouble
graphs_tools_get_luminance_from_hex (const gchar* hex)
{
	GdkRGBA _tmp0_ = {0};
	gdouble result;
	g_return_val_if_fail (hex != NULL, -1.0);
	graphs_tools_hex_to_rgba (hex, &_tmp0_);
	result = graphs_tools_get_luminance_from_rgba (&_tmp0_);
	return result;
}

gdouble
graphs_tools_get_srgb (gdouble color)
{
	gdouble result;
	if (color <= 0.03928) {
		result = color / 12.92;
		return result;
	} else {
		result = pow ((color + 0.055) / 1.055, 2.4);
		return result;
	}
}

/**
         * Calculate luminance of a Gdk.RGBA
         */
gdouble
graphs_tools_get_luminance_from_rgba (GdkRGBA* rgba)
{
	gdouble r = 0.0;
	GdkRGBA _tmp0_;
	gdouble g = 0.0;
	GdkRGBA _tmp1_;
	gdouble b = 0.0;
	GdkRGBA _tmp2_;
	gdouble result;
	g_return_val_if_fail (rgba != NULL, -1.0);
	_tmp0_ = *rgba;
	r = graphs_tools_get_srgb ((gdouble) _tmp0_.red);
	_tmp1_ = *rgba;
	g = graphs_tools_get_srgb ((gdouble) _tmp1_.green);
	_tmp2_ = *rgba;
	b = graphs_tools_get_srgb ((gdouble) _tmp2_.blue);
	result = ((0.2126 * r) + (0.7152 * g)) + (0.0722 * b);
	return result;
}

/**
         * Get the contrast between two colors
         */
gdouble
graphs_tools_get_contrast (GdkRGBA* bg_color,
                           GdkRGBA* fg_color)
{
	gdouble bg_luminance = 0.0;
	GdkRGBA _tmp0_;
	gdouble fg_luminance = 0.0;
	GdkRGBA _tmp1_;
	gdouble min_value = 0.0;
	gdouble max_value = 0.0;
	gdouble result;
	g_return_val_if_fail (bg_color != NULL, -1.0);
	g_return_val_if_fail (fg_color != NULL, -1.0);
	_tmp0_ = *bg_color;
	bg_luminance = graphs_tools_get_luminance_from_rgba (&_tmp0_);
	_tmp1_ = *fg_color;
	fg_luminance = graphs_tools_get_luminance_from_rgba (&_tmp1_);
	min_value = MIN (bg_luminance, fg_luminance) + 0.05;
	max_value = MAX (bg_luminance, fg_luminance) + 0.05;
	result = max_value / min_value;
	return result;
}

static gboolean
_vala_string_array_contains (gchar* * stack,
                             gssize stack_length,
                             const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

gchar*
graphs_tools_get_duplicate_string (const gchar* original,
                                   gchar** used,
                                   gint used_length1)
{
	gchar* old_str = NULL;
	gchar* _tmp1_;
	guint i = 0U;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (original != NULL, NULL);
	if (!_vala_string_array_contains (used, used_length1, original)) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (original);
		result = _tmp0_;
		return result;
	}
	_tmp1_ = g_strdup (original);
	old_str = _tmp1_;
	{
		GRegex* regex = NULL;
		GRegex* _tmp2_;
		GMatchInfo* info = NULL;
		GRegex* _tmp3_;
		GMatchInfo* _tmp4_ = NULL;
		gboolean _tmp5_;
		_tmp2_ = g_regex_new ("(?P<string>.+) \\(\\d+\\)", 0, 0, &_inner_error0_);
		regex = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = regex;
		_tmp5_ = g_regex_match (_tmp3_, original, 0, &_tmp4_);
		_g_match_info_unref0 (info);
		info = _tmp4_;
		if (_tmp5_) {
			GMatchInfo* _tmp6_;
			gchar* _tmp7_;
			_tmp6_ = info;
			_tmp7_ = g_match_info_fetch_named (_tmp6_, "string");
			_g_free0 (old_str);
			old_str = _tmp7_;
		}
		_g_match_info_unref0 (info);
		_g_regex_unref0 (regex);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (old_str);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	i = (guint) 1;
	while (TRUE) {
		gchar* new_str = NULL;
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		gchar* _tmp12_;
		gchar* _tmp13_;
		const gchar* _tmp14_;
		guint _tmp15_;
		_tmp8_ = old_str;
		_tmp9_ = string_to_string (_tmp8_);
		_tmp10_ = g_strdup_printf ("%u", i);
		_tmp11_ = _tmp10_;
		_tmp12_ = g_strconcat (_tmp9_, " (", _tmp11_, ")", NULL);
		_tmp13_ = _tmp12_;
		_g_free0 (_tmp11_);
		new_str = _tmp13_;
		_tmp14_ = new_str;
		if (!_vala_string_array_contains (used, used_length1, _tmp14_)) {
			result = new_str;
			_g_free0 (old_str);
			return result;
		}
		_tmp15_ = i;
		i = _tmp15_ + 1;
		_g_free0 (new_str);
	}
}

/**
         * Get the platforms config directory
         */
GFile*
graphs_tools_get_config_directory (GError** error)
{
	GFile* main_directory = NULL;
	const gchar* _tmp0_;
	GFile* _tmp1_;
	GFile* _tmp2_ = NULL;
	GFile* _tmp3_;
	GFile* _tmp4_;
	GError* _inner_error0_ = NULL;
	GFile* result;
	_tmp0_ = g_get_user_config_dir ();
	_tmp1_ = g_file_new_for_path (_tmp0_);
	main_directory = _tmp1_;
	_tmp3_ = g_file_get_child_for_display_name (main_directory, "graphs", &_inner_error0_);
	_tmp2_ = _tmp3_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (main_directory);
		return NULL;
	}
	_tmp4_ = _tmp2_;
	_tmp2_ = NULL;
	result = _tmp4_;
	_g_object_unref0 (_tmp2_);
	_g_object_unref0 (main_directory);
	return result;
}

/**
         * Get the filename of a file
         */
gchar*
graphs_tools_get_filename (GFile* file)
{
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (file != NULL, NULL);
	{
		GFileInfo* info = NULL;
		GFileInfo* _tmp0_;
		const gchar* _tmp1_;
		gchar* _tmp2_;
		_tmp0_ = g_file_query_info (file, "standard::display-name", G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
		info = _tmp0_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp1_ = g_file_info_get_display_name (info);
		_tmp2_ = g_strdup (_tmp1_);
		result = _tmp2_;
		_g_object_unref0 (info);
		return result;
	}
	goto __finally0;
	__catch0_g_error:
	{
		gchar* _tmp3_;
		g_clear_error (&_inner_error0_);
		_tmp3_ = g_file_get_basename (file);
		result = _tmp3_;
		return result;
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

/**
         * Build a dialog from `dialogs.blp`
         */
static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

GObject*
graphs_tools_build_dialog (const gchar* name)
{
	gchar* path = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	GtkBuilder* builder = NULL;
	GtkBuilder* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	GObject* _tmp9_;
	GObject* _tmp10_;
	GObject* _tmp11_;
	GObject* result;
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = string_replace (name, "_", "-");
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat ("/se/sjoerd/Graphs/ui/dialogs/", _tmp1_, NULL);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_strconcat (_tmp3_, ".ui", NULL);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_g_free0 (_tmp1_);
	path = _tmp5_;
	_tmp6_ = gtk_builder_new_from_resource (path);
	builder = _tmp6_;
	_tmp7_ = g_strconcat (name, "_dialog", NULL);
	_tmp8_ = _tmp7_;
	_tmp9_ = gtk_builder_get_object (builder, _tmp8_);
	_tmp10_ = _g_object_ref0 (_tmp9_);
	_tmp11_ = _tmp10_;
	_g_free0 (_tmp8_);
	result = _tmp11_;
	_g_object_unref0 (builder);
	_g_free0 (path);
	return result;
}

/**
         * Open the containing folder of a file
         */
void
graphs_tools_open_file_location (GFile* file)
{
	GtkFileLauncher* file_launcher = NULL;
	GtkFileLauncher* _tmp0_;
	g_return_if_fail (file != NULL);
	_tmp0_ = gtk_file_launcher_new (file);
	file_launcher = _tmp0_;
	gtk_file_launcher_open_containing_folder (file_launcher, NULL, NULL, NULL, NULL);
	_g_object_unref0 (file_launcher);
}

/**
         * Create a file filter matching the suffixes.
         */
GtkFileFilter*
graphs_tools_create_file_filter (const gchar* name,
                                 ...)
{
	GtkFileFilter* file_filter = NULL;
	gchar* _tmp0_;
	GtkFileFilter* _tmp1_ = NULL;
	va_list l = {0};
	GtkFileFilter* result;
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = g_strdup (name);
	_tmp1_ = gtk_file_filter_new ();
	g_object_set (_tmp1_, "name", _tmp0_, NULL);
	_g_free0 (_tmp0_);
	file_filter = _tmp1_;
	va_start (l, name);
	while (TRUE) {
		gchar* suffix = NULL;
		const gchar* _tmp2_;
		gchar* _tmp3_;
		const gchar* _tmp4_;
		GtkFileFilter* _tmp5_;
		const gchar* _tmp6_;
		_tmp2_ = va_arg (l, gchar*);
		_tmp3_ = g_strdup (_tmp2_);
		suffix = _tmp3_;
		_tmp4_ = suffix;
		if (_tmp4_ == NULL) {
			_g_free0 (suffix);
			break;
		}
		_tmp5_ = file_filter;
		_tmp6_ = suffix;
		gtk_file_filter_add_suffix (_tmp5_, _tmp6_);
		_g_free0 (suffix);
	}
	result = file_filter;
	va_end (l);
	return result;
}

/**
         * Create a catchall filter
         */
GtkFileFilter*
graphs_tools_create_all_filter (void)
{
	GtkFileFilter* all_filter = NULL;
	gchar* _tmp0_;
	GtkFileFilter* _tmp1_ = NULL;
	GtkFileFilter* result;
	_tmp0_ = g_strdup (_ ("All Files"));
	_tmp1_ = gtk_file_filter_new ();
	g_object_set (_tmp1_, "name", _tmp0_, NULL);
	_g_free0 (_tmp0_);
	all_filter = _tmp1_;
	gtk_file_filter_add_pattern (all_filter, "*");
	result = all_filter;
	return result;
}

/**
         * Create a ListStore with given FileFilters.
         */
GListStore*
graphs_tools_create_file_filters (gboolean add_all,
                                  ...)
{
	GListStore* list_store = NULL;
	GListStore* _tmp0_;
	va_list l = {0};
	GListStore* result;
	_tmp0_ = g_list_store_new (gtk_file_filter_get_type ());
	list_store = _tmp0_;
	va_start (l, add_all);
	while (TRUE) {
		GtkFileFilter* filter = NULL;
		GtkFileFilter* _tmp1_;
		GtkFileFilter* _tmp2_;
		GtkFileFilter* _tmp3_;
		GListStore* _tmp4_;
		GtkFileFilter* _tmp5_;
		_tmp1_ = va_arg (l, GtkFileFilter*);
		_tmp2_ = _g_object_ref0 (_tmp1_);
		filter = _tmp2_;
		_tmp3_ = filter;
		if (_tmp3_ == NULL) {
			_g_object_unref0 (filter);
			break;
		}
		_tmp4_ = list_store;
		_tmp5_ = filter;
		g_list_store_append (_tmp4_, (GObject*) _tmp5_);
		_g_object_unref0 (filter);
	}
	if (add_all) {
		GListStore* _tmp6_;
		GtkFileFilter* _tmp7_;
		GtkFileFilter* _tmp8_;
		_tmp6_ = list_store;
		_tmp7_ = graphs_tools_create_all_filter ();
		_tmp8_ = _tmp7_;
		g_list_store_append (_tmp6_, (GObject*) _tmp8_);
		_g_object_unref0 (_tmp8_);
	}
	result = list_store;
	va_end (l);
	return result;
}

/**
         * Get the friendly path of a file.
         */
static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

gchar*
graphs_tools_get_friendly_path (GFile* file)
{
	gchar* uri = NULL;
	gchar* _tmp0_;
	GUri* parsed = NULL;
	gchar* path = NULL;
	GUri* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* host = NULL;
	GUri* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* full_path = NULL;
	const gchar* _tmp12_;
	const gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* filepath = NULL;
	const gchar* _tmp18_;
	gchar* _tmp19_;
	gint uid = 0;
	gchar* doc_portal = NULL;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	const gchar* _tmp24_;
	const gchar* _tmp25_;
	const gchar* _tmp43_;
	gchar* home = NULL;
	const gchar* _tmp46_;
	gchar* _tmp47_;
	const gchar* _tmp48_;
	const gchar* _tmp49_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (file != NULL, NULL);
	_tmp0_ = g_file_get_uri (file);
	uri = _tmp0_;
	{
		GUri* _tmp1_ = NULL;
		const gchar* _tmp2_;
		GUri* _tmp3_;
		GUri* _tmp4_;
		_tmp2_ = uri;
		_tmp3_ = g_uri_parse (_tmp2_, G_URI_FLAGS_NONE, &_inner_error0_);
		_tmp1_ = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_URI_ERROR) {
				goto __catch0_g_uri_error;
			}
			_g_uri_unref0 (parsed);
			_g_free0 (uri);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp4_ = _tmp1_;
		_tmp1_ = NULL;
		_g_uri_unref0 (parsed);
		parsed = _tmp4_;
		_g_uri_unref0 (_tmp1_);
	}
	goto __finally0;
	__catch0_g_uri_error:
	{
		gchar* _tmp5_;
		g_clear_error (&_inner_error0_);
		_tmp5_ = g_strdup ("");
		result = _tmp5_;
		_g_uri_unref0 (parsed);
		_g_free0 (uri);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_uri_unref0 (parsed);
		_g_free0 (uri);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp6_ = parsed;
	_tmp7_ = g_uri_get_path (_tmp6_);
	_tmp8_ = g_uri_unescape_string (_tmp7_, NULL);
	path = _tmp8_;
	_tmp9_ = parsed;
	_tmp10_ = g_uri_get_host (_tmp9_);
	_tmp11_ = g_strdup (_tmp10_);
	host = _tmp11_;
	_tmp12_ = host;
	_tmp13_ = path;
	_tmp14_ = g_strconcat (_tmp12_, _tmp13_, NULL);
	_tmp15_ = _tmp14_;
	_tmp16_ = g_build_filename (_tmp15_, NULL);
	_tmp17_ = _tmp16_;
	_g_free0 (_tmp15_);
	full_path = _tmp17_;
	_tmp18_ = full_path;
	_tmp19_ = g_path_get_dirname (_tmp18_);
	filepath = _tmp19_;
	uid = (gint) getuid ();
	_tmp20_ = g_strdup_printf ("%i", uid);
	_tmp21_ = _tmp20_;
	_tmp22_ = g_strconcat ("/run/user/", _tmp21_, "/doc/", NULL);
	_tmp23_ = _tmp22_;
	_g_free0 (_tmp21_);
	doc_portal = _tmp23_;
	_tmp24_ = filepath;
	_tmp25_ = doc_portal;
	if (g_str_has_prefix (_tmp24_, _tmp25_)) {
		gchar* fallback = NULL;
		gchar* _tmp26_;
		GFileInfo* info = NULL;
		gchar* host_path = NULL;
		GFileInfo* _tmp30_;
		const gchar* _tmp31_;
		gchar* _tmp32_;
		const gchar* _tmp33_;
		_tmp26_ = g_strdup (_ ("Document Portal"));
		fallback = _tmp26_;
		{
			GFileInfo* _tmp27_ = NULL;
			GFileInfo* _tmp28_;
			GFileInfo* _tmp29_;
			_tmp28_ = g_file_query_info (file, "xattr::document-portal.host-path", G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
			_tmp27_ = _tmp28_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			_tmp29_ = _tmp27_;
			_tmp27_ = NULL;
			_g_object_unref0 (info);
			info = _tmp29_;
			_g_object_unref0 (_tmp27_);
		}
		goto __finally1;
		__catch1_g_error:
		{
			g_clear_error (&_inner_error0_);
			result = fallback;
			_g_object_unref0 (info);
			_g_free0 (doc_portal);
			_g_free0 (filepath);
			_g_free0 (full_path);
			_g_free0 (host);
			_g_free0 (path);
			_g_uri_unref0 (parsed);
			_g_free0 (uri);
			return result;
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (info);
			_g_free0 (fallback);
			_g_free0 (doc_portal);
			_g_free0 (filepath);
			_g_free0 (full_path);
			_g_free0 (host);
			_g_free0 (path);
			_g_uri_unref0 (parsed);
			_g_free0 (uri);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp30_ = info;
		_tmp31_ = g_file_info_get_attribute_string (_tmp30_, "xattr::document-portal.host-path");
		_tmp32_ = g_strdup (_tmp31_);
		host_path = _tmp32_;
		_tmp33_ = host_path;
		if (_tmp33_ != NULL) {
			gint len = 0;
			const gchar* _tmp34_;
			gint _tmp35_;
			gint _tmp36_;
			gboolean _tmp37_ = FALSE;
			const gchar* _tmp41_;
			gchar* _tmp42_;
			_tmp34_ = host_path;
			_tmp35_ = strlen (_tmp34_);
			_tmp36_ = _tmp35_;
			len = _tmp36_;
			if (len > 4) {
				const gchar* _tmp38_;
				_tmp38_ = host_path;
				_tmp37_ = g_str_has_suffix (_tmp38_, "\\x00");
			} else {
				_tmp37_ = FALSE;
			}
			if (_tmp37_) {
				const gchar* _tmp39_;
				gchar* _tmp40_;
				_tmp39_ = host_path;
				_tmp40_ = string_substring (_tmp39_, (glong) 0, (glong) (len - 4));
				_g_free0 (host_path);
				host_path = _tmp40_;
			}
			_tmp41_ = host_path;
			_tmp42_ = g_path_get_dirname (_tmp41_);
			_g_free0 (filepath);
			filepath = _tmp42_;
		} else {
			result = fallback;
			_g_free0 (host_path);
			_g_object_unref0 (info);
			_g_free0 (doc_portal);
			_g_free0 (filepath);
			_g_free0 (full_path);
			_g_free0 (host);
			_g_free0 (path);
			_g_uri_unref0 (parsed);
			_g_free0 (uri);
			return result;
		}
		_g_free0 (host_path);
		_g_object_unref0 (info);
		_g_free0 (fallback);
	}
	_tmp43_ = filepath;
	if (g_str_has_prefix (_tmp43_, "/var/home")) {
		const gchar* _tmp44_;
		gchar* _tmp45_;
		_tmp44_ = filepath;
		_tmp45_ = string_substring (_tmp44_, (glong) 4, (glong) -1);
		_g_free0 (filepath);
		filepath = _tmp45_;
	}
	_tmp46_ = g_get_home_dir ();
	_tmp47_ = g_strdup (_tmp46_);
	home = _tmp47_;
	_tmp48_ = filepath;
	_tmp49_ = home;
	if (g_str_has_prefix (_tmp48_, _tmp49_)) {
		const gchar* _tmp50_;
		const gchar* _tmp51_;
		gint _tmp52_;
		gint _tmp53_;
		gchar* _tmp54_;
		gchar* _tmp55_;
		gchar* _tmp56_;
		const gchar* _tmp57_;
		_tmp50_ = filepath;
		_tmp51_ = home;
		_tmp52_ = strlen (_tmp51_);
		_tmp53_ = _tmp52_;
		_tmp54_ = string_substring (_tmp50_, (glong) _tmp53_, (glong) -1);
		_tmp55_ = _tmp54_;
		_tmp56_ = g_strconcat ("~", _tmp55_, NULL);
		_g_free0 (filepath);
		filepath = _tmp56_;
		_g_free0 (_tmp55_);
		_tmp57_ = filepath;
		if (g_strcmp0 (_tmp57_, "~") == 0) {
			gchar* _tmp58_;
			_tmp58_ = g_strdup ("~/");
			_g_free0 (filepath);
			filepath = _tmp58_;
		}
	}
	result = filepath;
	_g_free0 (home);
	_g_free0 (doc_portal);
	_g_free0 (full_path);
	_g_free0 (host);
	_g_free0 (path);
	_g_uri_unref0 (parsed);
	_g_free0 (uri);
	return result;
}

/**
         * Convert an integer to an alphabetic representation.
         * example: 27 -> AA
         *
         * often used in spreadsheets.
         */
gchar*
graphs_tools_int_to_alpha (gint i)
{
	GString* _result_ = NULL;
	GString* _tmp0_;
	gint num = 0;
	GString* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	_tmp0_ = g_string_new ("");
	_result_ = _tmp0_;
	num = i + 1;
	while (TRUE) {
		gint remainder = 0;
		GString* _tmp1_;
		if (!(num > 0)) {
			break;
		}
		remainder = (num - 1) % 26;
		_tmp1_ = _result_;
		g_string_prepend_c (_tmp1_, (gchar) ('A' + remainder));
		num = (num - 1) / 26;
	}
	_tmp2_ = _result_;
	_result_ = NULL;
	_tmp3_ = g_string_free_and_steal (_tmp2_);
	result = _tmp3_;
	_g_string_free0 (_result_);
	return result;
}

/**
         * Convert an alphabetic representation to an integer.
         * example: AA -> 27
         *
         * often used in spreadsheets.
         */
static gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

gint
graphs_tools_alpha_to_int (const gchar* label)
{
	gint index = 0;
	gint result;
	g_return_val_if_fail (label != NULL, -1);
	index = 0;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				gint _tmp2_;
				gint _tmp3_;
				gchar c = '\0';
				if (!_tmp0_) {
					gint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				_tmp2_ = strlen (label);
				_tmp3_ = _tmp2_;
				if (!(i < _tmp3_)) {
					break;
				}
				c = string_get (label, (glong) i);
				if (!g_ascii_isalpha (c)) {
					result = -1;
					return result;
				}
				index = (index * 26) + ((g_ascii_tolower (c) - 'a') + 1);
			}
		}
	}
	result = index - 1;
	return result;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

