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

/*
 * Copyright (c) 2024-2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Authors: Kamil Prusko <kamilprusko@gmail.com>
 */

#include "ft_ui.h"
#include <glib.h>
#include "ft_core.h"
#include <gtk/gtk.h>
#include <glib-object.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  {
	FT_COMMAND_ENTRY_ROW_0_PROPERTY,
	FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY,
	FT_COMMAND_ENTRY_ROW_NUM_PROPERTIES
};
static GParamSpec* ft_command_entry_row_properties[FT_COMMAND_ENTRY_ROW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _FtCommandEntryRowPrivate {
	gboolean _use_subshell;
	FtCommand* _command;
	GtkImage* edit_icon;
	GtkText* text_widget;
	gboolean text_changed;
};

static gint FtCommandEntryRow_private_offset;
static gpointer ft_command_entry_row_parent_class = NULL;

static void ft_command_entry_row_insert_at_cursor (FtCommandEntryRow* self,
                                            const gchar* text);
static void ft_command_entry_row_update (FtCommandEntryRow* self);
static void ft_command_entry_row_on_insert_variable_popover_selected (FtCommandEntryRow* self,
                                                               const gchar* variable_name,
                                                               const gchar* variable_format_name);
static void _ft_command_entry_row_on_insert_variable_popover_selected_ft_variable_popover_selected (FtVariablePopover* _sender,
                                                                                             const gchar* variable_name,
                                                                                             const gchar* variable_format_name,
                                                                                             gpointer self);
static void ft_command_entry_row_on_text_widget_state_flags_changed (FtCommandEntryRow* self,
                                                              GtkWidget* widget,
                                                              GtkStateFlags previous_state_flags);
static void ft_command_entry_row_on_text_widget_changed (FtCommandEntryRow* self,
                                                  GtkEditable* editable);
static void ft_command_entry_row_real_dispose (GObject* base);
static void _ft_command_entry_row_on_text_widget_state_flags_changed_gtk_widget_state_flags_changed (GtkWidget* _sender,
                                                                                              GtkStateFlags previous_state_flags,
                                                                                              gpointer self);
static void _ft_command_entry_row_on_text_widget_changed_gtk_editable_changed (GtkEditable* _sender,
                                                                        gpointer self);
static GObject * ft_command_entry_row_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties);
VALA_EXTERN GtkWidget* ft_get_child_by_buildable_id (GtkWidget* widget,
                                         const gchar* buildable_id);
static void ft_command_entry_row_finalize (GObject * obj);
static GType ft_command_entry_row_get_type_once (void);
static void _vala_ft_command_entry_row_get_property (GObject * object,
                                              guint property_id,
                                              GValue * value,
                                              GParamSpec * pspec);
static void _vala_ft_command_entry_row_set_property (GObject * object,
                                              guint property_id,
                                              const GValue * value,
                                              GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
ft_command_entry_row_get_instance_private (FtCommandEntryRow* self)
{
	return G_STRUCT_MEMBER_P (self, FtCommandEntryRow_private_offset);
}

static void
ft_command_entry_row_insert_at_cursor (FtCommandEntryRow* self,
                                       const gchar* text)
{
	gint initial_position = 0;
	gint _tmp0_;
	gint _tmp1_;
	gint position = 0;
	gint _tmp2_;
	gint _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (text != NULL);
	g_object_get ((GtkEditable*) self, "cursor-position", &_tmp0_, NULL);
	_tmp1_ = _tmp0_;
	initial_position = _tmp1_;
	position = initial_position;
	_tmp2_ = strlen (text);
	_tmp3_ = _tmp2_;
	gtk_editable_insert_text ((GtkEditable*) self, text, _tmp3_, &position);
	if (position != initial_position) {
		gtk_editable_set_position ((GtkEditable*) self, position);
	}
}

static void
ft_command_entry_row_update (FtCommandEntryRow* self)
{
	FtCommand* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_command;
	if (_tmp0_ == NULL) {
		const gchar* _tmp1_;
		const gchar* _tmp2_;
		FtCommand* _tmp3_;
		FtCommand* _tmp4_;
		_tmp1_ = gtk_editable_get_text ((GtkEditable*) self);
		_tmp2_ = _tmp1_;
		_tmp3_ = ft_command_new (_tmp2_);
		_g_object_unref0 (self->priv->_command);
		self->priv->_command = _tmp3_;
		_tmp4_ = self->priv->_command;
		ft_command_set_use_subshell (_tmp4_, self->priv->_use_subshell);
	} else {
		FtCommand* _tmp5_;
		const gchar* _tmp6_;
		const gchar* _tmp7_;
		FtCommand* _tmp8_;
		_tmp5_ = self->priv->_command;
		_tmp6_ = gtk_editable_get_text ((GtkEditable*) self);
		_tmp7_ = _tmp6_;
		ft_command_set_line (_tmp5_, _tmp7_);
		_tmp8_ = self->priv->_command;
		ft_command_set_use_subshell (_tmp8_, self->priv->_use_subshell);
	}
	{
		FtCommand* _tmp9_;
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		GtkImage* _tmp13_;
		_tmp9_ = self->priv->_command;
		_tmp10_ = ft_command_get_line (_tmp9_);
		_tmp11_ = _tmp10_;
		if (g_strcmp0 (_tmp11_, "") != 0) {
			FtCommand* _tmp12_;
			_tmp12_ = self->priv->_command;
			ft_command_validate (_tmp12_, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				if (_inner_error0_->domain == FT_COMMAND_ERROR) {
					goto __catch0_ft_command_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;
			}
		}
		_tmp13_ = self->priv->edit_icon;
		if (_tmp13_ != NULL) {
			GtkImage* _tmp14_;
			_tmp14_ = self->priv->edit_icon;
			g_object_set (_tmp14_, "icon-name", "document-edit-symbolic", NULL);
		}
		gtk_widget_set_tooltip_text ((GtkWidget*) self, "");
		gtk_widget_remove_css_class ((GtkWidget*) self, "error");
	}
	goto __finally0;
	__catch0_ft_command_error:
	{
		GError* _error_ = NULL;
		GtkImage* _tmp15_;
		GError* _tmp17_;
		const gchar* _tmp18_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp15_ = self->priv->edit_icon;
		if (_tmp15_ != NULL) {
			GtkImage* _tmp16_;
			_tmp16_ = self->priv->edit_icon;
			g_object_set (_tmp16_, "icon-name", "dialog-warning-symbolic", NULL);
		}
		_tmp17_ = _error_;
		_tmp18_ = _tmp17_->message;
		gtk_widget_set_tooltip_text ((GtkWidget*) self, _tmp18_);
		gtk_widget_add_css_class ((GtkWidget*) self, "error");
		_g_error_free0 (_error_);
	}
	__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;
	}
	self->priv->text_changed = FALSE;
}

static void
ft_command_entry_row_on_insert_variable_popover_selected (FtCommandEntryRow* self,
                                                          const gchar* variable_name,
                                                          const gchar* variable_format_name)
{
	gchar* variable_display_name = NULL;
	gchar* _tmp0_;
	gchar* variable_format_display_name = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_ = NULL;
	const gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (variable_name != NULL);
	g_return_if_fail (variable_format_name != NULL);
	_tmp0_ = ft_to_camel_case (variable_name);
	variable_display_name = _tmp0_;
	_tmp1_ = ft_to_camel_case (variable_format_name);
	variable_format_display_name = _tmp1_;
	_tmp3_ = variable_format_display_name;
	if (g_strcmp0 (_tmp3_, "") != 0) {
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		_tmp4_ = variable_display_name;
		_tmp5_ = variable_format_display_name;
		_tmp6_ = g_strdup_printf ("${%s:%s}", _tmp4_, _tmp5_);
		_g_free0 (_tmp2_);
		_tmp2_ = _tmp6_;
	} else {
		const gchar* _tmp7_;
		gchar* _tmp8_;
		_tmp7_ = variable_display_name;
		_tmp8_ = g_strdup_printf ("${%s}", _tmp7_);
		_g_free0 (_tmp2_);
		_tmp2_ = _tmp8_;
	}
	ft_command_entry_row_insert_at_cursor (self, _tmp2_);
	_g_free0 (_tmp2_);
	_g_free0 (variable_format_display_name);
	_g_free0 (variable_display_name);
}

static void
_ft_command_entry_row_on_insert_variable_popover_selected_ft_variable_popover_selected (FtVariablePopover* _sender,
                                                                                        const gchar* variable_name,
                                                                                        const gchar* variable_format_name,
                                                                                        gpointer self)
{
	ft_command_entry_row_on_insert_variable_popover_selected ((FtCommandEntryRow*) self, variable_name, variable_format_name);
}

static void
ft_command_entry_row_on_text_widget_state_flags_changed (FtCommandEntryRow* self,
                                                         GtkWidget* widget,
                                                         GtkStateFlags previous_state_flags)
{
	gboolean focused = FALSE;
	gboolean _tmp0_ = FALSE;
	g_return_if_fail (self != NULL);
	g_return_if_fail (widget != NULL);
	focused = (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_FOCUS_WITHIN) == GTK_STATE_FLAG_FOCUS_WITHIN;
	if (!focused) {
		_tmp0_ = self->priv->text_changed;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		ft_command_entry_row_update (self);
	} else {
		if (focused) {
			self->priv->text_changed = TRUE;
			gtk_widget_remove_css_class ((GtkWidget*) self, "error");
			gtk_widget_set_tooltip_text ((GtkWidget*) self, "");
		}
	}
}

static void
ft_command_entry_row_on_text_widget_changed (FtCommandEntryRow* self,
                                             GtkEditable* editable)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (editable != NULL);
	self->priv->text_changed = TRUE;
}

static void
_ft_command_entry_row_on_text_widget_state_flags_changed_gtk_widget_state_flags_changed (GtkWidget* _sender,
                                                                                         GtkStateFlags previous_state_flags,
                                                                                         gpointer self)
{
	ft_command_entry_row_on_text_widget_state_flags_changed ((FtCommandEntryRow*) self, _sender, previous_state_flags);
}

static void
_ft_command_entry_row_on_text_widget_changed_gtk_editable_changed (GtkEditable* _sender,
                                                                   gpointer self)
{
	ft_command_entry_row_on_text_widget_changed ((FtCommandEntryRow*) self, _sender);
}

static void
ft_command_entry_row_real_dispose (GObject* base)
{
	FtCommandEntryRow * self;
	GtkText* _tmp0_;
	self = (FtCommandEntryRow*) base;
	_tmp0_ = self->priv->text_widget;
	if (_tmp0_ != NULL) {
		GtkText* _tmp1_;
		guint _tmp2_;
		GtkText* _tmp3_;
		guint _tmp4_;
		_tmp1_ = self->priv->text_widget;
		g_signal_parse_name ("state-flags-changed", gtk_widget_get_type (), &_tmp2_, NULL, FALSE);
		g_signal_handlers_disconnect_matched ((GtkWidget*) _tmp1_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp2_, 0, NULL, (GCallback) _ft_command_entry_row_on_text_widget_state_flags_changed_gtk_widget_state_flags_changed, self);
		_tmp3_ = self->priv->text_widget;
		g_signal_parse_name ("changed", gtk_editable_get_type (), &_tmp4_, NULL, FALSE);
		g_signal_handlers_disconnect_matched ((GtkEditable*) _tmp3_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp4_, 0, NULL, (GCallback) _ft_command_entry_row_on_text_widget_changed_gtk_editable_changed, self);
		self->priv->text_widget = NULL;
	}
	_g_object_unref0 (self->priv->_command);
	self->priv->_command = NULL;
	self->priv->edit_icon = NULL;
	G_OBJECT_CLASS (ft_command_entry_row_parent_class)->dispose ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, adw_entry_row_get_type (), AdwEntryRow));
}

FtCommandEntryRow*
ft_command_entry_row_construct (GType object_type)
{
	FtCommandEntryRow * self = NULL;
	self = (FtCommandEntryRow*) g_object_new (object_type, NULL);
	return self;
}

FtCommandEntryRow*
ft_command_entry_row_new (void)
{
	return ft_command_entry_row_construct (FT_TYPE_COMMAND_ENTRY_ROW);
}

gboolean
ft_command_entry_row_get_use_subshell (FtCommandEntryRow* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_use_subshell;
	return result;
}

void
ft_command_entry_row_set_use_subshell (FtCommandEntryRow* self,
                                       gboolean value)
{
	g_return_if_fail (self != NULL);
	self->priv->_use_subshell = value;
	ft_command_entry_row_update (self);
	g_object_notify_by_pspec ((GObject *) self, ft_command_entry_row_properties[FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY]);
}

static GObject *
ft_command_entry_row_constructor (GType type,
                                  guint n_construct_properties,
                                  GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	FtCommandEntryRow * self;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	GtkWidget* _tmp2_;
	GtkWidget* _tmp3_;
	GtkWidget* _tmp4_;
	GtkWidget* _tmp5_;
	GtkText* _tmp6_;
	GtkImage* _tmp10_;
	parent_class = G_OBJECT_CLASS (ft_command_entry_row_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_COMMAND_ENTRY_ROW, FtCommandEntryRow);
	_tmp0_ = gtk_list_box_row_get_child ((GtkListBoxRow*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = ft_get_child_by_buildable_id (_tmp1_, "text");
	self->priv->text_widget = G_TYPE_CHECK_INSTANCE_TYPE (_tmp2_, gtk_text_get_type ()) ? ((GtkText*) _tmp2_) : NULL;
	_tmp3_ = gtk_list_box_row_get_child ((GtkListBoxRow*) self);
	_tmp4_ = _tmp3_;
	_tmp5_ = ft_get_child_by_buildable_id (_tmp4_, "edit_icon");
	self->priv->edit_icon = G_TYPE_CHECK_INSTANCE_TYPE (_tmp5_, gtk_image_get_type ()) ? ((GtkImage*) _tmp5_) : NULL;
	_tmp6_ = self->priv->text_widget;
	if (_tmp6_ != NULL) {
		GtkText* _tmp7_;
		GtkText* _tmp8_;
		GtkText* _tmp9_;
		_tmp7_ = self->priv->text_widget;
		gtk_text_set_max_length (_tmp7_, 500);
		_tmp8_ = self->priv->text_widget;
		g_signal_connect_object ((GtkWidget*) _tmp8_, "state-flags-changed", (GCallback) _ft_command_entry_row_on_text_widget_state_flags_changed_gtk_widget_state_flags_changed, self, 0);
		_tmp9_ = self->priv->text_widget;
		g_signal_connect_object ((GtkEditable*) _tmp9_, "changed", (GCallback) _ft_command_entry_row_on_text_widget_changed_gtk_editable_changed, self, 0);
	} else {
		g_warning ("command-entryrow.vala:44: Could not find text widget.");
	}
	_tmp10_ = self->priv->edit_icon;
	if (_tmp10_ == NULL) {
		g_warning ("command-entryrow.vala:48: Could not find edit icon.");
	}
	return obj;
}

static void
ft_command_entry_row_class_init (FtCommandEntryRowClass * klass,
                                 gpointer klass_data)
{
	ft_command_entry_row_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &FtCommandEntryRow_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) ft_command_entry_row_real_dispose;
	G_OBJECT_CLASS (klass)->get_property = _vala_ft_command_entry_row_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_ft_command_entry_row_set_property;
	G_OBJECT_CLASS (klass)->constructor = ft_command_entry_row_constructor;
	G_OBJECT_CLASS (klass)->finalize = ft_command_entry_row_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY, ft_command_entry_row_properties[FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY] = g_param_spec_boolean ("use-subshell", "use-subshell", "use-subshell", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/io/github/focustimerhq/FocusTimer/ui/preferences/automation/action/command-entryrow.ui");
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_insert_variable_popover_selected", G_CALLBACK(_ft_command_entry_row_on_insert_variable_popover_selected_ft_variable_popover_selected));
}

static void
ft_command_entry_row_instance_init (FtCommandEntryRow * self,
                                    gpointer klass)
{
	self->priv = ft_command_entry_row_get_instance_private (self);
	self->priv->_use_subshell = FALSE;
	self->priv->_command = NULL;
	self->priv->edit_icon = NULL;
	self->priv->text_widget = NULL;
	self->priv->text_changed = FALSE;
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
ft_command_entry_row_finalize (GObject * obj)
{
	FtCommandEntryRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_COMMAND_ENTRY_ROW, FtCommandEntryRow);
	_g_object_unref0 (self->priv->_command);
	G_OBJECT_CLASS (ft_command_entry_row_parent_class)->finalize (obj);
}

static GType
ft_command_entry_row_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtCommandEntryRowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_command_entry_row_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtCommandEntryRow), 0, (GInstanceInitFunc) ft_command_entry_row_instance_init, NULL };
	GType ft_command_entry_row_type_id;
	ft_command_entry_row_type_id = g_type_register_static (adw_entry_row_get_type (), "FtCommandEntryRow", &g_define_type_info, 0);
	FtCommandEntryRow_private_offset = g_type_add_instance_private (ft_command_entry_row_type_id, sizeof (FtCommandEntryRowPrivate));
	return ft_command_entry_row_type_id;
}

GType
ft_command_entry_row_get_type (void)
{
	static volatile gsize ft_command_entry_row_type_id__once = 0;
	if (g_once_init_enter (&ft_command_entry_row_type_id__once)) {
		GType ft_command_entry_row_type_id;
		ft_command_entry_row_type_id = ft_command_entry_row_get_type_once ();
		g_once_init_leave (&ft_command_entry_row_type_id__once, ft_command_entry_row_type_id);
	}
	return ft_command_entry_row_type_id__once;
}

static void
_vala_ft_command_entry_row_get_property (GObject * object,
                                         guint property_id,
                                         GValue * value,
                                         GParamSpec * pspec)
{
	FtCommandEntryRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_COMMAND_ENTRY_ROW, FtCommandEntryRow);
	switch (property_id) {
		case FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY:
		g_value_set_boolean (value, ft_command_entry_row_get_use_subshell (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_ft_command_entry_row_set_property (GObject * object,
                                         guint property_id,
                                         const GValue * value,
                                         GParamSpec * pspec)
{
	FtCommandEntryRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_COMMAND_ENTRY_ROW, FtCommandEntryRow);
	switch (property_id) {
		case FT_COMMAND_ENTRY_ROW_USE_SUBSHELL_PROPERTY:
		ft_command_entry_row_set_use_subshell (self, g_value_get_boolean (value));
		break;
		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;
}

