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

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

#include "graphs.h"
#include <adwaita.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.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

enum  {
	GRAPHS_EXPORT_FIGURE_DIALOG_0_PROPERTY,
	GRAPHS_EXPORT_FIGURE_DIALOG_NUM_PROPERTIES
};
static GParamSpec* graphs_export_figure_dialog_properties[GRAPHS_EXPORT_FIGURE_DIALOG_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block21Data Block21Data;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _GraphsExportFigureDialogPrivate {
	AdwSwitchRow* _transparent;
	AdwComboRow* _file_format;
	AdwSpinRow* _width;
	AdwSpinRow* _height;
	GraphsWindow* window;
	GSettings* settings;
};

struct _Block21Data {
	int _ref_count_;
	GraphsExportFigureDialog* self;
	gchar* old_suffix;
	gchar* suffix;
	GtkFileDialog* dialog;
};

static gint GraphsExportFigureDialog_private_offset;
static gpointer graphs_export_figure_dialog_parent_class = NULL;

static AdwComboRow* graphs_export_figure_dialog_get_file_format (GraphsExportFigureDialog* self);
static AdwSwitchRow* graphs_export_figure_dialog_get_transparent (GraphsExportFigureDialog* self);
static AdwSpinRow* graphs_export_figure_dialog_get_width (GraphsExportFigureDialog* self);
static AdwSpinRow* graphs_export_figure_dialog_get_height (GraphsExportFigureDialog* self);
static void graphs_export_figure_dialog_on_use_window_size (GraphsExportFigureDialog* self);
static void _graphs_export_figure_dialog_on_use_window_size_adw_button_row_activated (AdwButtonRow* _sender,
                                                                               gpointer self);
static void graphs_export_figure_dialog_on_accept (GraphsExportFigureDialog* self);
static Block21Data* block21_data_ref (Block21Data* _data21_);
static void block21_data_unref (void * _userdata_);
static void __lambda69_ (Block21Data* _data21_,
                  GObject* d,
                  GAsyncResult* r);
static void ___lambda69__gasync_ready_callback (GObject* source_object,
                                         GAsyncResult* res,
                                         gpointer self);
static void _graphs_export_figure_dialog_on_accept_gtk_button_clicked (GtkButton* _sender,
                                                                gpointer self);
static void graphs_export_figure_dialog_finalize (GObject * obj);
static GType graphs_export_figure_dialog_get_type_once (void);
static void _vala_graphs_export_figure_dialog_get_property (GObject * object,
                                                     guint property_id,
                                                     GValue * value,
                                                     GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
graphs_export_figure_dialog_get_instance_private (GraphsExportFigureDialog* self)
{
	return G_STRUCT_MEMBER_P (self, GraphsExportFigureDialog_private_offset);
}

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

GraphsExportFigureDialog*
graphs_export_figure_dialog_construct (GType object_type,
                                       GraphsWindow* window)
{
	GraphsExportFigureDialog * self = NULL;
	GraphsWindow* _tmp0_;
	GSettings* _tmp1_;
	AdwComboRow* _tmp2_;
	GSettings* _tmp3_;
	AdwSwitchRow* _tmp4_;
	GSettings* _tmp5_;
	AdwSpinRow* _tmp6_;
	GSettings* _tmp7_;
	AdwSpinRow* _tmp8_;
	GSettings* _tmp9_;
	g_return_val_if_fail (window != NULL, NULL);
	self = (GraphsExportFigureDialog*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (self->priv->window);
	self->priv->window = _tmp0_;
	_tmp1_ = graphs_application_get_settings_child ("export-figure");
	_g_object_unref0 (self->priv->settings);
	self->priv->settings = _tmp1_;
	_tmp2_ = self->priv->_file_format;
	_tmp3_ = self->priv->settings;
	adw_combo_row_set_selected (_tmp2_, (guint) g_settings_get_enum (_tmp3_, "file-format"));
	_tmp4_ = self->priv->_transparent;
	_tmp5_ = self->priv->settings;
	adw_switch_row_set_active (_tmp4_, g_settings_get_boolean (_tmp5_, "transparent"));
	_tmp6_ = self->priv->_width;
	_tmp7_ = self->priv->settings;
	adw_spin_row_set_value (_tmp6_, (gdouble) g_settings_get_int (_tmp7_, "width"));
	_tmp8_ = self->priv->_height;
	_tmp9_ = self->priv->settings;
	adw_spin_row_set_value (_tmp8_, (gdouble) g_settings_get_int (_tmp9_, "height"));
	adw_dialog_present ((AdwDialog*) self, (GtkWidget*) window);
	return self;
}

GraphsExportFigureDialog*
graphs_export_figure_dialog_new (GraphsWindow* window)
{
	return graphs_export_figure_dialog_construct (GRAPHS_TYPE_EXPORT_FIGURE_DIALOG, window);
}

static void
graphs_export_figure_dialog_on_use_window_size (GraphsExportFigureDialog* self)
{
	AdwSpinRow* _tmp0_;
	GraphsWindow* _tmp1_;
	GraphsCanvas* _tmp2_;
	GraphsCanvas* _tmp3_;
	AdwSpinRow* _tmp4_;
	GraphsWindow* _tmp5_;
	GraphsCanvas* _tmp6_;
	GraphsCanvas* _tmp7_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_width;
	_tmp1_ = self->priv->window;
	_tmp2_ = graphs_window_get_canvas (_tmp1_);
	_tmp3_ = _tmp2_;
	adw_spin_row_set_value (_tmp0_, (gdouble) gtk_widget_get_width ((GtkWidget*) _tmp3_));
	_tmp4_ = self->priv->_height;
	_tmp5_ = self->priv->window;
	_tmp6_ = graphs_window_get_canvas (_tmp5_);
	_tmp7_ = _tmp6_;
	adw_spin_row_set_value (_tmp4_, (gdouble) gtk_widget_get_height ((GtkWidget*) _tmp7_));
}

static void
_graphs_export_figure_dialog_on_use_window_size_adw_button_row_activated (AdwButtonRow* _sender,
                                                                          gpointer self)
{
	graphs_export_figure_dialog_on_use_window_size ((GraphsExportFigureDialog*) self);
}

static Block21Data*
block21_data_ref (Block21Data* _data21_)
{
	g_atomic_int_inc (&_data21_->_ref_count_);
	return _data21_;
}

static void
block21_data_unref (void * _userdata_)
{
	Block21Data* _data21_;
	_data21_ = (Block21Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data21_->_ref_count_)) {
		GraphsExportFigureDialog* self;
		self = _data21_->self;
		_g_object_unref0 (_data21_->dialog);
		_g_free0 (_data21_->suffix);
		_g_free0 (_data21_->old_suffix);
		_g_object_unref0 (self);
		g_slice_free (Block21Data, _data21_);
	}
}

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

static void
__lambda69_ (Block21Data* _data21_,
             GObject* d,
             GAsyncResult* r)
{
	GraphsExportFigureDialog* self;
	GError* _inner_error0_ = NULL;
	self = _data21_->self;
	g_return_if_fail (r != NULL);
	{
		GFile* file = NULL;
		GFile* _tmp0_;
		GSettings* _tmp1_;
		GSettings* _tmp2_;
		AdwSwitchRow* _tmp3_;
		GSettings* _tmp4_;
		AdwSpinRow* _tmp5_;
		GSettings* _tmp6_;
		AdwSpinRow* _tmp7_;
		GSettings* _tmp8_;
		GraphsWindow* _tmp9_;
		GraphsData* _tmp10_;
		GraphsData* _tmp11_;
		GraphsWindow* _tmp12_;
		_tmp0_ = gtk_file_dialog_save_finish (_data21_->dialog, r, &_inner_error0_);
		file = _tmp0_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp1_ = self->priv->settings;
		g_settings_set_string (_tmp1_, "file-format", _data21_->suffix);
		_tmp2_ = self->priv->settings;
		_tmp3_ = self->priv->_transparent;
		g_settings_set_boolean (_tmp2_, "transparent", adw_switch_row_get_active (_tmp3_));
		_tmp4_ = self->priv->settings;
		_tmp5_ = self->priv->_width;
		g_settings_set_int (_tmp4_, "width", (gint) adw_spin_row_get_value (_tmp5_));
		_tmp6_ = self->priv->settings;
		_tmp7_ = self->priv->_height;
		g_settings_set_int (_tmp6_, "height", (gint) adw_spin_row_get_value (_tmp7_));
		_tmp8_ = self->priv->settings;
		_tmp9_ = self->priv->window;
		_tmp10_ = graphs_window_get_data (_tmp9_);
		_tmp11_ = _tmp10_;
		graphs_python_helper_export_figure (file, _tmp8_, _tmp11_);
		_tmp12_ = self->priv->window;
		graphs_window_add_toast_string_with_file (_tmp12_, _ ("Exported Figure"), file);
		adw_dialog_close ((AdwDialog*) self);
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GSettings* _tmp13_;
		g_clear_error (&_inner_error0_);
		_tmp13_ = self->priv->settings;
		g_settings_set_string (_tmp13_, "file-format", _data21_->old_suffix);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		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;
	}
}

static void
___lambda69__gasync_ready_callback (GObject* source_object,
                                    GAsyncResult* res,
                                    gpointer self)
{
	__lambda69_ (self, source_object, res);
	block21_data_unref (self);
}

static void
graphs_export_figure_dialog_on_accept (GraphsExportFigureDialog* self)
{
	Block21Data* _data21_;
	gchar* filename = NULL;
	gchar* _tmp0_;
	GSettings* _tmp1_;
	gchar* _tmp2_;
	GSettings* _tmp3_;
	AdwComboRow* _tmp4_;
	GSettings* _tmp5_;
	gchar* _tmp6_;
	GtkFileDialog* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	GListStore* filter_store = NULL;
	GListStore* _tmp12_;
	GtkFileFilter* filter = NULL;
	GtkFileFilter* _tmp13_;
	GtkStringObject* selected = NULL;
	AdwComboRow* _tmp14_;
	GObject* _tmp15_;
	GtkStringObject* _tmp16_;
	const gchar* _tmp17_;
	GraphsWindow* _tmp18_;
	g_return_if_fail (self != NULL);
	_data21_ = g_slice_new0 (Block21Data);
	_data21_->_ref_count_ = 1;
	_data21_->self = g_object_ref (self);
	_tmp0_ = g_strdup (C_ ("filename", "Exported Figure"));
	filename = _tmp0_;
	_tmp1_ = self->priv->settings;
	_tmp2_ = g_settings_get_string (_tmp1_, "file-format");
	_data21_->old_suffix = _tmp2_;
	_tmp3_ = self->priv->settings;
	_tmp4_ = self->priv->_file_format;
	g_settings_set_enum (_tmp3_, "file-format", (gint) adw_combo_row_get_selected (_tmp4_));
	_tmp5_ = self->priv->settings;
	_tmp6_ = g_settings_get_string (_tmp5_, "file-format");
	_data21_->suffix = _tmp6_;
	_tmp7_ = gtk_file_dialog_new ();
	_data21_->dialog = _tmp7_;
	_tmp8_ = string_to_string (filename);
	_tmp9_ = string_to_string (_data21_->suffix);
	_tmp10_ = g_strconcat (_tmp8_, ".", _tmp9_, NULL);
	_tmp11_ = _tmp10_;
	gtk_file_dialog_set_initial_name (_data21_->dialog, _tmp11_);
	_g_free0 (_tmp11_);
	gtk_file_dialog_set_accept_label (_data21_->dialog, _ ("Export"));
	_tmp12_ = g_list_store_new (gtk_file_filter_get_type ());
	filter_store = _tmp12_;
	_tmp13_ = gtk_file_filter_new ();
	filter = _tmp13_;
	_tmp14_ = self->priv->_file_format;
	_tmp15_ = adw_combo_row_get_selected_item (_tmp14_);
	_tmp16_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp15_, gtk_string_object_get_type (), GtkStringObject));
	selected = _tmp16_;
	_tmp17_ = gtk_string_object_get_string (selected);
	g_object_set (filter, "name", _tmp17_, NULL);
	gtk_file_filter_add_suffix (filter, _data21_->suffix);
	g_list_store_append (filter_store, (GObject*) filter);
	gtk_file_dialog_set_filters (_data21_->dialog, (GListModel*) filter_store);
	_tmp18_ = self->priv->window;
	gtk_file_dialog_save (_data21_->dialog, (GtkWindow*) _tmp18_, NULL, ___lambda69__gasync_ready_callback, block21_data_ref (_data21_));
	_g_object_unref0 (selected);
	_g_object_unref0 (filter);
	_g_object_unref0 (filter_store);
	_g_free0 (filename);
	block21_data_unref (_data21_);
	_data21_ = NULL;
}

static void
_graphs_export_figure_dialog_on_accept_gtk_button_clicked (GtkButton* _sender,
                                                           gpointer self)
{
	graphs_export_figure_dialog_on_accept ((GraphsExportFigureDialog*) self);
}

static AdwSwitchRow*
graphs_export_figure_dialog_get_transparent (GraphsExportFigureDialog* self)
{
	AdwSwitchRow* result;
	AdwSwitchRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_transparent;
	result = _tmp0_;
	return result;
}

static AdwComboRow*
graphs_export_figure_dialog_get_file_format (GraphsExportFigureDialog* self)
{
	AdwComboRow* result;
	AdwComboRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_file_format;
	result = _tmp0_;
	return result;
}

static AdwSpinRow*
graphs_export_figure_dialog_get_width (GraphsExportFigureDialog* self)
{
	AdwSpinRow* result;
	AdwSpinRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_width;
	result = _tmp0_;
	return result;
}

static AdwSpinRow*
graphs_export_figure_dialog_get_height (GraphsExportFigureDialog* self)
{
	AdwSpinRow* result;
	AdwSpinRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_height;
	result = _tmp0_;
	return result;
}

static void
graphs_export_figure_dialog_class_init (GraphsExportFigureDialogClass * klass,
                                        gpointer klass_data)
{
	graphs_export_figure_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraphsExportFigureDialog_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_graphs_export_figure_dialog_get_property;
	G_OBJECT_CLASS (klass)->finalize = graphs_export_figure_dialog_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/se/sjoerd/Graphs/ui/export-figure.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "transparent", FALSE, GraphsExportFigureDialog_private_offset + G_STRUCT_OFFSET (GraphsExportFigureDialogPrivate, _transparent));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "file_format", FALSE, GraphsExportFigureDialog_private_offset + G_STRUCT_OFFSET (GraphsExportFigureDialogPrivate, _file_format));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "width", FALSE, GraphsExportFigureDialog_private_offset + G_STRUCT_OFFSET (GraphsExportFigureDialogPrivate, _width));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "height", FALSE, GraphsExportFigureDialog_private_offset + G_STRUCT_OFFSET (GraphsExportFigureDialogPrivate, _height));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_use_window_size", G_CALLBACK(_graphs_export_figure_dialog_on_use_window_size_adw_button_row_activated));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_accept", G_CALLBACK(_graphs_export_figure_dialog_on_accept_gtk_button_clicked));
}

static void
graphs_export_figure_dialog_instance_init (GraphsExportFigureDialog * self,
                                           gpointer klass)
{
	self->priv = graphs_export_figure_dialog_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
graphs_export_figure_dialog_finalize (GObject * obj)
{
	GraphsExportFigureDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_EXPORT_FIGURE_DIALOG, GraphsExportFigureDialog);
	_g_object_unref0 (self->priv->window);
	_g_object_unref0 (self->priv->settings);
	G_OBJECT_CLASS (graphs_export_figure_dialog_parent_class)->finalize (obj);
}

/**
     * Export figure dialog
     */
static GType
graphs_export_figure_dialog_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraphsExportFigureDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) graphs_export_figure_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraphsExportFigureDialog), 0, (GInstanceInitFunc) graphs_export_figure_dialog_instance_init, NULL };
	GType graphs_export_figure_dialog_type_id;
	graphs_export_figure_dialog_type_id = g_type_register_static (adw_dialog_get_type (), "GraphsExportFigureDialog", &g_define_type_info, 0);
	GraphsExportFigureDialog_private_offset = g_type_add_instance_private (graphs_export_figure_dialog_type_id, sizeof (GraphsExportFigureDialogPrivate));
	return graphs_export_figure_dialog_type_id;
}

GType
graphs_export_figure_dialog_get_type (void)
{
	static volatile gsize graphs_export_figure_dialog_type_id__once = 0;
	if (g_once_init_enter (&graphs_export_figure_dialog_type_id__once)) {
		GType graphs_export_figure_dialog_type_id;
		graphs_export_figure_dialog_type_id = graphs_export_figure_dialog_get_type_once ();
		g_once_init_leave (&graphs_export_figure_dialog_type_id__once, graphs_export_figure_dialog_type_id);
	}
	return graphs_export_figure_dialog_type_id__once;
}

static void
_vala_graphs_export_figure_dialog_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec)
{
	GraphsExportFigureDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRAPHS_TYPE_EXPORT_FIGURE_DIALOG, GraphsExportFigureDialog);
	switch (property_id) {
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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;
}

