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

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

#include "graphs.h"
#include <adwaita.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.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_ADD_EQUATION_DIALOG_0_PROPERTY,
	GRAPHS_ADD_EQUATION_DIALOG_NUM_PROPERTIES
};
static GParamSpec* graphs_add_equation_dialog_properties[GRAPHS_ADD_EQUATION_DIALOG_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _GraphsAddEquationDialogPrivate {
	AdwEntryRow* _equation;
	GtkButton* _confirm_button;
	AdwEntryRow* _item_name;
	GraphsWindow* window;
	GSettings* settings;
};

static gint GraphsAddEquationDialog_private_offset;
static gpointer graphs_add_equation_dialog_parent_class = NULL;

static AdwEntryRow* graphs_add_equation_dialog_get_equation (GraphsAddEquationDialog* self);
static void graphs_add_equation_dialog_on_accept (GraphsAddEquationDialog* self);
static AdwEntryRow* graphs_add_equation_dialog_get_item_name (GraphsAddEquationDialog* self);
static void _graphs_add_equation_dialog_on_accept_gtk_button_clicked (GtkButton* _sender,
                                                               gpointer self);
static void graphs_add_equation_dialog_on_equation_change (GraphsAddEquationDialog* self);
static GtkButton* graphs_add_equation_dialog_get_confirm_button (GraphsAddEquationDialog* self);
static void _graphs_add_equation_dialog_on_equation_change_g_object_notify (GObject* _sender,
                                                                     GParamSpec* pspec,
                                                                     gpointer self);
static void graphs_add_equation_dialog_finalize (GObject * obj);
static GType graphs_add_equation_dialog_get_type_once (void);
static void _vala_graphs_add_equation_dialog_get_property (GObject * object,
                                                    guint property_id,
                                                    GValue * value,
                                                    GParamSpec * pspec);
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 inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
graphs_add_equation_dialog_get_instance_private (GraphsAddEquationDialog* self)
{
	return G_STRUCT_MEMBER_P (self, GraphsAddEquationDialog_private_offset);
}

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

GraphsAddEquationDialog*
graphs_add_equation_dialog_construct (GType object_type,
                                      GraphsWindow* window)
{
	GraphsAddEquationDialog * self = NULL;
	GraphsWindow* _tmp0_;
	GSettings* _tmp1_;
	AdwEntryRow* _tmp2_;
	GSettings* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	g_return_val_if_fail (window != NULL, NULL);
	self = (GraphsAddEquationDialog*) 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 ("add-equation");
	_g_object_unref0 (self->priv->settings);
	self->priv->settings = _tmp1_;
	_tmp2_ = self->priv->_equation;
	_tmp3_ = self->priv->settings;
	_tmp4_ = g_settings_get_string (_tmp3_, "equation");
	_tmp5_ = _tmp4_;
	gtk_editable_set_text ((GtkEditable*) _tmp2_, _tmp5_);
	_g_free0 (_tmp5_);
	adw_dialog_present ((AdwDialog*) self, (GtkWidget*) window);
	return self;
}

GraphsAddEquationDialog*
graphs_add_equation_dialog_new (GraphsWindow* window)
{
	return graphs_add_equation_dialog_construct (GRAPHS_TYPE_ADD_EQUATION_DIALOG, window);
}

static void
graphs_add_equation_dialog_on_accept (GraphsAddEquationDialog* self)
{
	gchar* equation_str = NULL;
	AdwEntryRow* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	GSettings* _tmp3_;
	const gchar* _tmp4_;
	gchar* name = NULL;
	AdwEntryRow* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	const gchar* _tmp8_;
	GraphsItem* item = NULL;
	GraphsWindow* _tmp11_;
	GraphsData* _tmp12_;
	GraphsData* _tmp13_;
	const gchar* _tmp14_;
	GraphsEquationItem* _tmp15_;
	const gchar* _tmp16_;
	GraphsItem** items = NULL;
	GraphsItem* _tmp17_;
	GraphsItem** _tmp18_;
	gint items_length1;
	gint _items_size_;
	GraphsWindow* _tmp19_;
	GraphsData* _tmp20_;
	GraphsData* _tmp21_;
	GraphsWindow* _tmp22_;
	GraphsData* _tmp23_;
	GraphsData* _tmp24_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_equation;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) _tmp0_);
	_tmp2_ = g_strdup (_tmp1_);
	equation_str = _tmp2_;
	_tmp3_ = self->priv->settings;
	_tmp4_ = equation_str;
	g_settings_set_string (_tmp3_, "equation", _tmp4_);
	_tmp5_ = self->priv->_item_name;
	_tmp6_ = gtk_editable_get_text ((GtkEditable*) _tmp5_);
	_tmp7_ = g_strdup (_tmp6_);
	name = _tmp7_;
	_tmp8_ = name;
	if (g_strcmp0 (_tmp8_, "") == 0) {
		const gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp9_ = equation_str;
		_tmp10_ = g_strconcat ("Y = ", _tmp9_, NULL);
		_g_free0 (name);
		name = _tmp10_;
	}
	_tmp11_ = self->priv->window;
	_tmp12_ = graphs_window_get_data (_tmp11_);
	_tmp13_ = _tmp12_;
	_tmp14_ = equation_str;
	_tmp15_ = graphs_item_factory_new_equation_item (_tmp13_, _tmp14_);
	item = (GraphsItem*) _tmp15_;
	_tmp16_ = name;
	graphs_item_set_name (item, _tmp16_);
	_tmp17_ = _g_object_ref0 (item);
	_tmp18_ = g_new0 (GraphsItem*, 1 + 1);
	_tmp18_[0] = _tmp17_;
	items = _tmp18_;
	items_length1 = 1;
	_items_size_ = items_length1;
	_tmp19_ = self->priv->window;
	_tmp20_ = graphs_window_get_data (_tmp19_);
	_tmp21_ = _tmp20_;
	graphs_data_add_items (_tmp21_, items, (gint) items_length1);
	_tmp22_ = self->priv->window;
	_tmp23_ = graphs_window_get_data (_tmp22_);
	_tmp24_ = _tmp23_;
	graphs_data_optimize_limits (_tmp24_);
	adw_dialog_close ((AdwDialog*) self);
	items = (_vala_array_free (items, items_length1, (GDestroyNotify) g_object_unref), NULL);
	_g_object_unref0 (item);
	_g_free0 (name);
	_g_free0 (equation_str);
}

static void
_graphs_add_equation_dialog_on_accept_gtk_button_clicked (GtkButton* _sender,
                                                          gpointer self)
{
	graphs_add_equation_dialog_on_accept ((GraphsAddEquationDialog*) self);
}

static void
graphs_add_equation_dialog_on_equation_change (GraphsAddEquationDialog* self)
{
	AdwEntryRow* _tmp0_;
	const gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_equation;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) _tmp0_);
	if (graphs_python_helper_validate_equation (_tmp1_)) {
		AdwEntryRow* _tmp2_;
		GtkButton* _tmp3_;
		_tmp2_ = self->priv->_equation;
		gtk_widget_remove_css_class ((GtkWidget*) _tmp2_, "error");
		_tmp3_ = self->priv->_confirm_button;
		gtk_widget_set_sensitive ((GtkWidget*) _tmp3_, TRUE);
	} else {
		AdwEntryRow* _tmp4_;
		GtkButton* _tmp5_;
		_tmp4_ = self->priv->_equation;
		gtk_widget_add_css_class ((GtkWidget*) _tmp4_, "error");
		_tmp5_ = self->priv->_confirm_button;
		gtk_widget_set_sensitive ((GtkWidget*) _tmp5_, FALSE);
	}
}

static void
_graphs_add_equation_dialog_on_equation_change_g_object_notify (GObject* _sender,
                                                                GParamSpec* pspec,
                                                                gpointer self)
{
	graphs_add_equation_dialog_on_equation_change ((GraphsAddEquationDialog*) self);
}

static AdwEntryRow*
graphs_add_equation_dialog_get_equation (GraphsAddEquationDialog* self)
{
	AdwEntryRow* result;
	AdwEntryRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_equation;
	result = _tmp0_;
	return result;
}

static GtkButton*
graphs_add_equation_dialog_get_confirm_button (GraphsAddEquationDialog* self)
{
	GtkButton* result;
	GtkButton* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_confirm_button;
	result = _tmp0_;
	return result;
}

static AdwEntryRow*
graphs_add_equation_dialog_get_item_name (GraphsAddEquationDialog* self)
{
	AdwEntryRow* result;
	AdwEntryRow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_item_name;
	result = _tmp0_;
	return result;
}

static void
graphs_add_equation_dialog_class_init (GraphsAddEquationDialogClass * klass,
                                       gpointer klass_data)
{
	graphs_add_equation_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraphsAddEquationDialog_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_graphs_add_equation_dialog_get_property;
	G_OBJECT_CLASS (klass)->finalize = graphs_add_equation_dialog_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/se/sjoerd/Graphs/ui/add-equation.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "equation", FALSE, GraphsAddEquationDialog_private_offset + G_STRUCT_OFFSET (GraphsAddEquationDialogPrivate, _equation));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "confirm_button", FALSE, GraphsAddEquationDialog_private_offset + G_STRUCT_OFFSET (GraphsAddEquationDialogPrivate, _confirm_button));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "item_name", FALSE, GraphsAddEquationDialog_private_offset + G_STRUCT_OFFSET (GraphsAddEquationDialogPrivate, _item_name));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_accept", G_CALLBACK(_graphs_add_equation_dialog_on_accept_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_equation_change", G_CALLBACK(_graphs_add_equation_dialog_on_equation_change_g_object_notify));
}

static void
graphs_add_equation_dialog_instance_init (GraphsAddEquationDialog * self,
                                          gpointer klass)
{
	self->priv = graphs_add_equation_dialog_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
graphs_add_equation_dialog_finalize (GObject * obj)
{
	GraphsAddEquationDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_ADD_EQUATION_DIALOG, GraphsAddEquationDialog);
	_g_object_unref0 (self->priv->window);
	_g_object_unref0 (self->priv->settings);
	G_OBJECT_CLASS (graphs_add_equation_dialog_parent_class)->finalize (obj);
}

/**
     * Add Equation dialog.
     */
static GType
graphs_add_equation_dialog_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraphsAddEquationDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) graphs_add_equation_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraphsAddEquationDialog), 0, (GInstanceInitFunc) graphs_add_equation_dialog_instance_init, NULL };
	GType graphs_add_equation_dialog_type_id;
	graphs_add_equation_dialog_type_id = g_type_register_static (adw_dialog_get_type (), "GraphsAddEquationDialog", &g_define_type_info, 0);
	GraphsAddEquationDialog_private_offset = g_type_add_instance_private (graphs_add_equation_dialog_type_id, sizeof (GraphsAddEquationDialogPrivate));
	return graphs_add_equation_dialog_type_id;
}

GType
graphs_add_equation_dialog_get_type (void)
{
	static volatile gsize graphs_add_equation_dialog_type_id__once = 0;
	if (g_once_init_enter (&graphs_add_equation_dialog_type_id__once)) {
		GType graphs_add_equation_dialog_type_id;
		graphs_add_equation_dialog_type_id = graphs_add_equation_dialog_get_type_once ();
		g_once_init_leave (&graphs_add_equation_dialog_type_id__once, graphs_add_equation_dialog_type_id);
	}
	return graphs_add_equation_dialog_type_id__once;
}

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

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

