/* install-extension-dialog.c generated by valac 0.56.19, the Vala compiler
 * generated from install-extension-dialog.vala, do not modify */

/*
 * Copyright (c) 2025-2026 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "ft_plugin_gnome.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <adwaita.h>
#include <gio/gio.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.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  {
	GNOME_INSTALL_EXTENSION_DIALOG_0_PROPERTY,
	GNOME_INSTALL_EXTENSION_DIALOG_NUM_PROPERTIES
};
static GParamSpec* gnome_install_extension_dialog_properties[GNOME_INSTALL_EXTENSION_DIALOG_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 _GnomeInstallExtensionDialogPrivate {
	guint CLOSE_TIMEOUT_SECONDS;
	GtkStack* stack;
	GtkLabel* error_message_label;
	GtkTextView* error_text_view;
	GnomeShellExtension* shell_extension;
	guint timeout_id;
};

static gint GnomeInstallExtensionDialog_private_offset;
static gpointer gnome_install_extension_dialog_parent_class = NULL;
static GType gnome_install_extension_dialog_type_id = 0;

static void gnome_install_extension_dialog_close_after_timeout (GnomeInstallExtensionDialog* self,
                                                         guint seconds);
static gboolean __lambda7_ (GnomeInstallExtensionDialog* self);
static gboolean ___lambda7__gsource_func (gpointer self);
static void gnome_install_extension_dialog_show_spinner (GnomeInstallExtensionDialog* self);
static void gnome_install_extension_dialog_on_install_clicked (GnomeInstallExtensionDialog* self);
static void __lambda8_ (GnomeInstallExtensionDialog* self,
                 GObject* obj,
                 GAsyncResult* res);
static void ___lambda8__gasync_ready_callback (GObject* source_object,
                                        GAsyncResult* res,
                                        gpointer self);
static void _gnome_install_extension_dialog_on_install_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                            gpointer self);
static void gnome_install_extension_dialog_on_cancel_clicked (GnomeInstallExtensionDialog* self);
static void _gnome_install_extension_dialog_on_cancel_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                           gpointer self);
static void gnome_install_extension_dialog_on_abort_clicked (GnomeInstallExtensionDialog* self);
static void _gnome_install_extension_dialog_on_abort_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                          gpointer self);
static void gnome_install_extension_dialog_on_copy_to_clipboard_clicked (GnomeInstallExtensionDialog* self,
                                                                  GtkButton* button);
static void _gnome_install_extension_dialog_on_copy_to_clipboard_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                                      gpointer self);
static void gnome_install_extension_dialog_real_dispose (GObject* base);
static GObject * gnome_install_extension_dialog_constructor (GType type,
                                                      guint n_construct_properties,
                                                      GObjectConstructParam * construct_properties);
static void gnome_install_extension_dialog_finalize (GObject * obj);

static inline gpointer
gnome_install_extension_dialog_get_instance_private (GnomeInstallExtensionDialog* self)
{
	return G_STRUCT_MEMBER_P (self, GnomeInstallExtensionDialog_private_offset);
}

static gboolean
__lambda7_ (GnomeInstallExtensionDialog* self)
{
	gboolean result;
	self->priv->timeout_id = 0U;
	adw_dialog_close ((AdwDialog*) self);
	result = G_SOURCE_REMOVE;
	return result;
}

static gboolean
___lambda7__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda7_ ((GnomeInstallExtensionDialog*) self);
	return result;
}

static void
gnome_install_extension_dialog_close_after_timeout (GnomeInstallExtensionDialog* self,
                                                    guint seconds)
{
	g_return_if_fail (self != NULL);
	if (self->priv->timeout_id != 0U) {
		g_source_remove (self->priv->timeout_id);
	}
	self->priv->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, seconds, ___lambda7__gsource_func, g_object_ref (self), g_object_unref);
	g_source_set_name_by_id (self->priv->timeout_id, "Gnome.InstallExtensionDialog.close_after_timeout");
}

static void
gnome_install_extension_dialog_show_spinner (GnomeInstallExtensionDialog* self)
{
	GtkStack* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->stack;
	gtk_stack_set_visible_child_name (_tmp0_, "spinner");
	adw_dialog_set_can_close ((AdwDialog*) self, FALSE);
}

static void
__lambda8_ (GnomeInstallExtensionDialog* self,
            GObject* obj,
            GAsyncResult* res)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (res != NULL);
	{
		gboolean success = FALSE;
		GnomeShellExtension* _tmp0_;
		_tmp0_ = self->priv->shell_extension;
		success = gnome_shell_extension_install_extension_finish (_tmp0_, res, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == GNOME_SHELL_EXTENSION_ERROR) {
				goto __catch0_gnome_shell_extension_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;
		}
		adw_dialog_set_can_close ((AdwDialog*) self, TRUE);
		if (success) {
			GtkStack* _tmp1_;
			_tmp1_ = self->priv->stack;
			gtk_stack_set_visible_child_name (_tmp1_, "success");
			gnome_install_extension_dialog_close_after_timeout (self, self->priv->CLOSE_TIMEOUT_SECONDS);
		} else {
			adw_dialog_close ((AdwDialog*) self);
		}
	}
	goto __finally0;
	__catch0_gnome_shell_extension_error:
	{
		GError* _error_ = NULL;
		GError* _tmp2_;
		GtkStack* _tmp15_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = _error_;
		switch (_tmp2_->code) {
			case GNOME_SHELL_EXTENSION_ERROR_TIMED_OUT:
			{
				GtkLabel* _tmp3_;
				GtkLabel* _tmp4_;
				_tmp3_ = self->priv->error_message_label;
				gtk_label_set_label (_tmp3_, _ ("Time-out reached"));
				_tmp4_ = self->priv->error_message_label;
				gtk_widget_set_visible ((GtkWidget*) _tmp4_, TRUE);
				break;
			}
			case GNOME_SHELL_EXTENSION_ERROR_NOT_ALLOWED:
			{
				GtkLabel* _tmp5_;
				GtkLabel* _tmp6_;
				_tmp5_ = self->priv->error_message_label;
				gtk_label_set_label (_tmp5_, _ ("Installing extensions is not allowed"));
				_tmp6_ = self->priv->error_message_label;
				gtk_widget_set_visible ((GtkWidget*) _tmp6_, TRUE);
				break;
			}
			case GNOME_SHELL_EXTENSION_ERROR_DOWNLOAD_FAILED:
			{
				GtkLabel* _tmp7_;
				GtkLabel* _tmp8_;
				_tmp7_ = self->priv->error_message_label;
				gtk_label_set_label (_tmp7_, _ ("Failed to download the extension"));
				_tmp8_ = self->priv->error_message_label;
				gtk_widget_set_visible ((GtkWidget*) _tmp8_, TRUE);
				break;
			}
			default:
			{
				GtkTextView* _tmp9_;
				GtkTextBuffer* _tmp10_;
				GtkTextBuffer* _tmp11_;
				GError* _tmp12_;
				const gchar* _tmp13_;
				GtkLabel* _tmp14_;
				_tmp9_ = self->priv->error_text_view;
				_tmp10_ = gtk_text_view_get_buffer (_tmp9_);
				_tmp11_ = _tmp10_;
				_tmp12_ = _error_;
				_tmp13_ = _tmp12_->message;
				g_object_set (_tmp11_, "text", _tmp13_, NULL);
				_tmp14_ = self->priv->error_message_label;
				gtk_widget_set_visible ((GtkWidget*) _tmp14_, FALSE);
				break;
			}
		}
		adw_dialog_set_can_close ((AdwDialog*) self, TRUE);
		_tmp15_ = self->priv->stack;
		gtk_stack_set_visible_child_name (_tmp15_, "failure");
		_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;
	}
}

static void
___lambda8__gasync_ready_callback (GObject* source_object,
                                   GAsyncResult* res,
                                   gpointer self)
{
	__lambda8_ ((GnomeInstallExtensionDialog*) self, source_object, res);
	g_object_unref (self);
}

static void
gnome_install_extension_dialog_on_install_clicked (GnomeInstallExtensionDialog* self)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	GnomeShellExtension* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = adw_dialog_get_can_close ((AdwDialog*) self);
	_tmp1_ = _tmp0_;
	if (!_tmp1_) {
		return;
	}
	gnome_install_extension_dialog_show_spinner (self);
	_tmp2_ = self->priv->shell_extension;
	gnome_shell_extension_install_extension (_tmp2_, ___lambda8__gasync_ready_callback, g_object_ref (self));
}

static void
_gnome_install_extension_dialog_on_install_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                       gpointer self)
{
	gnome_install_extension_dialog_on_install_clicked ((GnomeInstallExtensionDialog*) self);
}

static void
gnome_install_extension_dialog_on_cancel_clicked (GnomeInstallExtensionDialog* self)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = adw_dialog_get_can_close ((AdwDialog*) self);
	_tmp1_ = _tmp0_;
	if (!_tmp1_) {
		return;
	}
	adw_dialog_close ((AdwDialog*) self);
}

static void
_gnome_install_extension_dialog_on_cancel_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                      gpointer self)
{
	gnome_install_extension_dialog_on_cancel_clicked ((GnomeInstallExtensionDialog*) self);
}

static void
gnome_install_extension_dialog_on_abort_clicked (GnomeInstallExtensionDialog* self)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = adw_dialog_get_can_close ((AdwDialog*) self);
	_tmp1_ = _tmp0_;
	if (!_tmp1_) {
		return;
	}
	adw_dialog_close ((AdwDialog*) self);
}

static void
_gnome_install_extension_dialog_on_abort_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                     gpointer self)
{
	gnome_install_extension_dialog_on_abort_clicked ((GnomeInstallExtensionDialog*) self);
}

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

static void
gnome_install_extension_dialog_on_copy_to_clipboard_clicked (GnomeInstallExtensionDialog* self,
                                                             GtkButton* button)
{
	GdkDisplay* display = NULL;
	GdkDisplay* _tmp0_;
	GdkDisplay* _tmp1_;
	gchar* error_message = NULL;
	GtkTextView* _tmp2_;
	GtkTextBuffer* _tmp3_;
	GtkTextBuffer* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	GdkDisplay* _tmp7_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (button != NULL);
	_tmp0_ = gdk_display_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	display = _tmp1_;
	_tmp2_ = self->priv->error_text_view;
	_tmp3_ = gtk_text_view_get_buffer (_tmp2_);
	_tmp4_ = _tmp3_;
	g_object_get (_tmp4_, "text", &_tmp5_, NULL);
	_tmp6_ = _tmp5_;
	error_message = _tmp6_;
	_tmp7_ = display;
	if (_tmp7_ != NULL) {
		GdkClipboard* clipboard = NULL;
		GdkDisplay* _tmp8_;
		GdkClipboard* _tmp9_;
		GdkClipboard* _tmp10_;
		GdkClipboard* _tmp11_;
		const gchar* _tmp12_;
		_tmp8_ = display;
		_tmp9_ = gdk_display_get_clipboard (_tmp8_);
		_tmp10_ = _g_object_ref0 (_tmp9_);
		clipboard = _tmp10_;
		_tmp11_ = clipboard;
		_tmp12_ = error_message;
		gdk_clipboard_set_text (_tmp11_, _tmp12_);
		_g_object_unref0 (clipboard);
	}
	_g_free0 (error_message);
	_g_object_unref0 (display);
}

static void
_gnome_install_extension_dialog_on_copy_to_clipboard_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                                 gpointer self)
{
	gnome_install_extension_dialog_on_copy_to_clipboard_clicked ((GnomeInstallExtensionDialog*) self, _sender);
}

static void
gnome_install_extension_dialog_real_dispose (GObject* base)
{
	GnomeInstallExtensionDialog * self;
	self = (GnomeInstallExtensionDialog*) base;
	if (self->priv->timeout_id != 0U) {
		g_source_remove (self->priv->timeout_id);
		self->priv->timeout_id = 0U;
	}
	_g_object_unref0 (self->priv->shell_extension);
	self->priv->shell_extension = NULL;
	G_OBJECT_CLASS (gnome_install_extension_dialog_parent_class)->dispose ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, adw_dialog_get_type (), AdwDialog));
}

GnomeInstallExtensionDialog*
gnome_install_extension_dialog_construct (GType object_type)
{
	GnomeInstallExtensionDialog * self = NULL;
	self = (GnomeInstallExtensionDialog*) g_object_new (object_type, NULL);
	return self;
}

GnomeInstallExtensionDialog*
gnome_install_extension_dialog_new (void)
{
	return gnome_install_extension_dialog_construct (GNOME_TYPE_INSTALL_EXTENSION_DIALOG);
}

static GObject *
gnome_install_extension_dialog_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GnomeInstallExtensionDialog * self;
	GnomeShellExtension* _tmp0_;
	parent_class = G_OBJECT_CLASS (gnome_install_extension_dialog_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_TYPE_INSTALL_EXTENSION_DIALOG, GnomeInstallExtensionDialog);
	_tmp0_ = gnome_shell_extension_new ();
	_g_object_unref0 (self->priv->shell_extension);
	self->priv->shell_extension = _tmp0_;
	return obj;
}

static void
gnome_install_extension_dialog_class_init (GnomeInstallExtensionDialogClass * klass,
                                           gpointer klass_data)
{
	gnome_install_extension_dialog_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GnomeInstallExtensionDialog_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) gnome_install_extension_dialog_real_dispose;
	G_OBJECT_CLASS (klass)->constructor = gnome_install_extension_dialog_constructor;
	G_OBJECT_CLASS (klass)->finalize = gnome_install_extension_dialog_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/plugins/gnome/install-extension-dialog.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "stack", FALSE, GnomeInstallExtensionDialog_private_offset + G_STRUCT_OFFSET (GnomeInstallExtensionDialogPrivate, stack));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "error_message_label", FALSE, GnomeInstallExtensionDialog_private_offset + G_STRUCT_OFFSET (GnomeInstallExtensionDialogPrivate, error_message_label));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "error_text_view", FALSE, GnomeInstallExtensionDialog_private_offset + G_STRUCT_OFFSET (GnomeInstallExtensionDialogPrivate, error_text_view));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_install_clicked", G_CALLBACK(_gnome_install_extension_dialog_on_install_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_cancel_clicked", G_CALLBACK(_gnome_install_extension_dialog_on_cancel_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_abort_clicked", G_CALLBACK(_gnome_install_extension_dialog_on_abort_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_copy_to_clipboard_clicked", G_CALLBACK(_gnome_install_extension_dialog_on_copy_to_clipboard_clicked_gtk_button_clicked));
}

static void
gnome_install_extension_dialog_instance_init (GnomeInstallExtensionDialog * self,
                                              gpointer klass)
{
	self->priv = gnome_install_extension_dialog_get_instance_private (self);
	self->priv->CLOSE_TIMEOUT_SECONDS = (guint) 3;
	self->priv->shell_extension = NULL;
	self->priv->timeout_id = 0U;
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
gnome_install_extension_dialog_finalize (GObject * obj)
{
	GnomeInstallExtensionDialog * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_TYPE_INSTALL_EXTENSION_DIALOG, GnomeInstallExtensionDialog);
	_g_object_unref0 (self->priv->shell_extension);
	G_OBJECT_CLASS (gnome_install_extension_dialog_parent_class)->finalize (obj);
}

GType
gnome_install_extension_dialog_get_type (void)
{
	return gnome_install_extension_dialog_type_id;
}

GType
gnome_install_extension_dialog_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (GnomeInstallExtensionDialogClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gnome_install_extension_dialog_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GnomeInstallExtensionDialog), 0, (GInstanceInitFunc) gnome_install_extension_dialog_instance_init, NULL };
	gnome_install_extension_dialog_type_id = g_type_module_register_type (module, adw_dialog_get_type (), "GnomeInstallExtensionDialog", &g_define_type_info, 0);
	GnomeInstallExtensionDialog_private_offset = sizeof (GnomeInstallExtensionDialogPrivate);
	return gnome_install_extension_dialog_type_id;
}

