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

/* Application.vala
 *
 * Copyright 2020-2021 Dylan McCall <dylan@dylanmccall.ca>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "settings.h"
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <adwaita.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  {
	BREAK_TIMER_SETTINGS_APPLICATION_0_PROPERTY,
	BREAK_TIMER_SETTINGS_APPLICATION_NUM_PROPERTIES
};
static GParamSpec* break_timer_settings_application_properties[BREAK_TIMER_SETTINGS_APPLICATION_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 _BreakTimerSettingsApplicationPrivate {
	BreakTimerSettingsBreakManager* break_manager;
	BreakTimerSettingsMainWindow* main_window;
	gboolean initial_focus;
};

static gint BreakTimerSettingsApplication_private_offset;
static gpointer break_timer_settings_application_parent_class = NULL;

static void break_timer_settings_application_real_activate (GApplication* base);
static void break_timer_settings_application_on_main_window_is_active_changed (BreakTimerSettingsApplication* self);
static void _break_timer_settings_application_on_main_window_is_active_changed_g_object_notify (GObject* _sender,
                                                                                         GParamSpec* pspec,
                                                                                         gpointer self);
static void break_timer_settings_application_delayed_start (BreakTimerSettingsApplication* self);
static void break_timer_settings_application_real_startup (GApplication* base);
static void break_timer_settings_application_on_about_activate_cb (BreakTimerSettingsApplication* self);
static void _break_timer_settings_application_on_about_activate_cb_g_simple_action_activate (GSimpleAction* _sender,
                                                                                      GVariant* parameter,
                                                                                      gpointer self);
static void _g_application_quit_g_simple_action_activate (GSimpleAction* _sender,
                                                   GVariant* parameter,
                                                   gpointer self);
static gboolean ___lambda8_ (BreakTimerSettingsApplication* self);
static gboolean ____lambda8__gsource_func (gpointer self);
static void break_timer_settings_application_delayed_start_cb (BreakTimerSettingsApplication* self);
static void _break_timer_settings_application_delayed_start_cb_break_timer_settings_break_manager_break_status_available (BreakTimerSettingsBreakManager* _sender,
                                                                                                                   gpointer self);
static gboolean __lambda9_ (BreakTimerSettingsApplication* self);
static gboolean ___lambda9__gsource_func (gpointer self);
static void break_timer_settings_application_show_about_dialog (BreakTimerSettingsApplication* self);
static void break_timer_settings_application_finalize (GObject * obj);
static GType break_timer_settings_application_get_type_once (void);
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
break_timer_settings_application_get_instance_private (BreakTimerSettingsApplication* self)
{
	return G_STRUCT_MEMBER_P (self, BreakTimerSettingsApplication_private_offset);
}

BreakTimerSettingsApplication*
break_timer_settings_application_construct (GType object_type)
{
	BreakTimerSettingsApplication * self = NULL;
	self = (BreakTimerSettingsApplication*) g_object_new (object_type, "application-id", SETTINGS_APPLICATION_ID, "flags", G_APPLICATION_FLAGS_NONE, NULL);
	return self;
}

BreakTimerSettingsApplication*
break_timer_settings_application_new (void)
{
	return break_timer_settings_application_construct (BREAK_TIMER_SETTINGS_TYPE_APPLICATION);
}

static void
_break_timer_settings_application_on_main_window_is_active_changed_g_object_notify (GObject* _sender,
                                                                                    GParamSpec* pspec,
                                                                                    gpointer self)
{
	break_timer_settings_application_on_main_window_is_active_changed ((BreakTimerSettingsApplication*) self);
}

static void
break_timer_settings_application_real_activate (GApplication* base)
{
	BreakTimerSettingsApplication * self;
	BreakTimerSettingsMainWindow* _tmp0_;
	BreakTimerSettingsBreakManager* _tmp7_;
	GError* _inner_error0_ = NULL;
	self = (BreakTimerSettingsApplication*) base;
	G_APPLICATION_CLASS (break_timer_settings_application_parent_class)->activate ((GApplication*) G_TYPE_CHECK_INSTANCE_CAST (self, adw_application_get_type (), AdwApplication));
	_tmp0_ = self->priv->main_window;
	if (_tmp0_ == NULL) {
		BreakTimerSettingsBreakManager* _tmp1_;
		BreakTimerSettingsMainWindow* _tmp2_;
		BreakTimerSettingsMainWindow* _tmp6_;
		_tmp1_ = self->priv->break_manager;
		_tmp2_ = break_timer_settings_main_window_new (self, _tmp1_);
		g_object_ref_sink (_tmp2_);
		_g_object_unref0 (self->priv->main_window);
		self->priv->main_window = _tmp2_;
		{
			BreakTimerSettingsMainWindow* _tmp3_;
			_tmp3_ = self->priv->main_window;
			g_initable_init ((GInitable*) _tmp3_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* _error_ = NULL;
			const gchar* _tmp4_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp4_ = _error_->message;
			g_error ("Application.vala:43: Error initializing main_window: %s", _tmp4_);
			_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;
		}
		if (g_strcmp0 (BUILD_PROFILE, "development") == 0) {
			BreakTimerSettingsMainWindow* _tmp5_;
			_tmp5_ = self->priv->main_window;
			gtk_widget_add_css_class ((GtkWidget*) _tmp5_, "devel");
		}
		_tmp6_ = self->priv->main_window;
		g_signal_connect_object ((GObject*) _tmp6_, "notify::is-active", (GCallback) _break_timer_settings_application_on_main_window_is_active_changed_g_object_notify, self, 0);
	}
	_tmp7_ = self->priv->break_manager;
	if (break_timer_settings_break_manager_is_working (_tmp7_)) {
		BreakTimerSettingsMainWindow* _tmp8_;
		_tmp8_ = self->priv->main_window;
		gtk_window_present ((GtkWindow*) _tmp8_);
	} else {
		break_timer_settings_application_delayed_start (self);
	}
}

static void
_break_timer_settings_application_on_about_activate_cb_g_simple_action_activate (GSimpleAction* _sender,
                                                                                 GVariant* parameter,
                                                                                 gpointer self)
{
	break_timer_settings_application_on_about_activate_cb ((BreakTimerSettingsApplication*) self);
}

static void
_g_application_quit_g_simple_action_activate (GSimpleAction* _sender,
                                              GVariant* parameter,
                                              gpointer self)
{
	g_application_quit ((GApplication*) self);
}

static void
break_timer_settings_application_real_startup (GApplication* base)
{
	BreakTimerSettingsApplication * self;
	GSimpleAction* about_action = NULL;
	GSimpleAction* _tmp0_;
	GSimpleAction* quit_action = NULL;
	GSimpleAction* _tmp1_;
	gchar* _tmp2_;
	gchar** _tmp3_;
	gchar** _tmp4_;
	gint _tmp4__length1;
	BreakTimerSettingsBreakManager* _tmp5_;
	GError* _inner_error0_ = NULL;
	self = (BreakTimerSettingsApplication*) base;
	G_APPLICATION_CLASS (break_timer_settings_application_parent_class)->startup ((GApplication*) G_TYPE_CHECK_INSTANCE_CAST (self, adw_application_get_type (), AdwApplication));
	gtk_window_set_default_icon_name (SETTINGS_APPLICATION_ID);
	_tmp0_ = g_simple_action_new ("about", NULL);
	about_action = _tmp0_;
	g_action_map_add_action ((GActionMap*) self, (GAction*) about_action);
	g_signal_connect_object (about_action, "activate", (GCallback) _break_timer_settings_application_on_about_activate_cb_g_simple_action_activate, self, 0);
	_tmp1_ = g_simple_action_new ("quit", NULL);
	quit_action = _tmp1_;
	g_action_map_add_action ((GActionMap*) self, (GAction*) quit_action);
	g_signal_connect_object (quit_action, "activate", (GCallback) _g_application_quit_g_simple_action_activate, (GApplication*) self, 0);
	_tmp2_ = g_strdup ("<Primary>q");
	_tmp3_ = g_new0 (gchar*, 1 + 1);
	_tmp3_[0] = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp4__length1 = 1;
	gtk_application_set_accels_for_action ((GtkApplication*) self, "app.quit", _tmp4_);
	_tmp4_ = (_vala_array_free (_tmp4_, _tmp4__length1, (GDestroyNotify) g_free), NULL);
	_tmp5_ = break_timer_settings_break_manager_new ();
	_g_object_unref0 (self->priv->break_manager);
	self->priv->break_manager = _tmp5_;
	{
		BreakTimerSettingsBreakManager* _tmp6_;
		_tmp6_ = self->priv->break_manager;
		break_timer_settings_break_manager_init (_tmp6_, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		const gchar* _tmp7_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp7_ = _error_->message;
		g_error ("Application.vala:81: Error initializing break_manager: %s", _tmp7_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (quit_action);
		_g_object_unref0 (about_action);
		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;
	}
	_g_object_unref0 (quit_action);
	_g_object_unref0 (about_action);
}

static gboolean
___lambda8_ (BreakTimerSettingsApplication* self)
{
	BreakTimerSettingsBreakManager* _tmp0_;
	gboolean result;
	_tmp0_ = self->priv->break_manager;
	break_timer_settings_break_manager_refresh_permissions (_tmp0_);
	result = G_SOURCE_REMOVE;
	return result;
}

static gboolean
____lambda8__gsource_func (gpointer self)
{
	gboolean result;
	result = ___lambda8_ ((BreakTimerSettingsApplication*) self);
	return result;
}

static void
break_timer_settings_application_on_main_window_is_active_changed (BreakTimerSettingsApplication* self)
{
	BreakTimerSettingsMainWindow* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	gboolean _tmp3_ = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->main_window;
	g_object_get ((GtkWindow*) _tmp0_, "is-active", &_tmp1_, NULL);
	_tmp2_ = _tmp1_;
	if (!_tmp2_) {
		return;
	}
	if (self->priv->initial_focus) {
		BreakTimerSettingsBreakManager* _tmp4_;
		gboolean _tmp5_;
		gboolean _tmp6_;
		_tmp4_ = self->priv->break_manager;
		_tmp5_ = break_timer_settings_break_manager_get_master_enabled (_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp3_ = _tmp6_;
	} else {
		_tmp3_ = FALSE;
	}
	if (_tmp3_) {
		self->priv->initial_focus = FALSE;
		g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 500, ____lambda8__gsource_func, g_object_ref (self), g_object_unref);
	} else {
		BreakTimerSettingsBreakManager* _tmp7_;
		BreakTimerSettingsBreakManagerPermissionsError _tmp8_;
		BreakTimerSettingsBreakManagerPermissionsError _tmp9_;
		_tmp7_ = self->priv->break_manager;
		_tmp8_ = break_timer_settings_break_manager_get_permissions_error (_tmp7_);
		_tmp9_ = _tmp8_;
		if (_tmp9_ != BREAK_TIMER_SETTINGS_BREAK_MANAGER_PERMISSIONS_ERROR_NONE) {
			BreakTimerSettingsBreakManager* _tmp10_;
			_tmp10_ = self->priv->break_manager;
			break_timer_settings_break_manager_refresh_permissions (_tmp10_);
		}
	}
}

static void
_break_timer_settings_application_delayed_start_cb_break_timer_settings_break_manager_break_status_available (BreakTimerSettingsBreakManager* _sender,
                                                                                                              gpointer self)
{
	break_timer_settings_application_delayed_start_cb ((BreakTimerSettingsApplication*) self);
}

static gboolean
__lambda9_ (BreakTimerSettingsApplication* self)
{
	gboolean result;
	break_timer_settings_application_delayed_start_cb (self);
	result = G_SOURCE_REMOVE;
	return result;
}

static gboolean
___lambda9__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda9_ ((BreakTimerSettingsApplication*) self);
	return result;
}

static void
break_timer_settings_application_delayed_start (BreakTimerSettingsApplication* self)
{
	BreakTimerSettingsBreakManager* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->break_manager;
	g_signal_connect_object (_tmp0_, "break-status-available", (GCallback) _break_timer_settings_application_delayed_start_cb_break_timer_settings_break_manager_break_status_available, self, 0);
	g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 500, ___lambda9__gsource_func, g_object_ref (self), g_object_unref);
}

static void
break_timer_settings_application_delayed_start_cb (BreakTimerSettingsApplication* self)
{
	BreakTimerSettingsBreakManager* _tmp0_;
	guint _tmp1_;
	BreakTimerSettingsMainWindow* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->break_manager;
	g_signal_parse_name ("break-status-available", BREAK_TIMER_SETTINGS_TYPE_BREAK_MANAGER, &_tmp1_, NULL, FALSE);
	g_signal_handlers_disconnect_matched (_tmp0_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp1_, 0, NULL, (GCallback) _break_timer_settings_application_delayed_start_cb_break_timer_settings_break_manager_break_status_available, self);
	_tmp2_ = self->priv->main_window;
	if (!gtk_widget_is_visible ((GtkWidget*) _tmp2_)) {
		BreakTimerSettingsMainWindow* _tmp3_;
		_tmp3_ = self->priv->main_window;
		gtk_window_present ((GtkWindow*) _tmp3_);
	}
}

static void
break_timer_settings_application_on_about_activate_cb (BreakTimerSettingsApplication* self)
{
	g_return_if_fail (self != NULL);
	break_timer_settings_application_show_about_dialog (self);
}

static void
break_timer_settings_application_show_about_dialog (BreakTimerSettingsApplication* self)
{
	AdwAboutWindow* dialog = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	AdwAboutWindow* _tmp2_;
	AdwAboutWindow* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar** _tmp6_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gchar* _tmp8_;
	gchar** _tmp9_;
	gchar** _tmp10_;
	gint _tmp10__length1;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_strdup_printf ("/org/gnome/BreakTimer/metainfo/%s.metainfo.xml", APPLICATION_ID);
	_tmp1_ = _tmp0_;
	_tmp2_ = (AdwAboutWindow*) adw_about_window_new_from_appdata (_tmp1_, NULL);
	g_object_ref_sink (_tmp2_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	dialog = _tmp3_;
	_tmp4_ = g_strdup ("Dylan McCall <dylan@dylanmccall.ca>");
	_tmp5_ = g_strdup ("Jasper St. Pierre <jstpierre@mecheye.net>");
	_tmp6_ = g_new0 (gchar*, 2 + 1);
	_tmp6_[0] = _tmp4_;
	_tmp6_[1] = _tmp5_;
	_tmp7_ = _tmp6_;
	_tmp7__length1 = 2;
	adw_about_window_set_developers (dialog, _tmp7_);
	_tmp7_ = (_vala_array_free (_tmp7_, _tmp7__length1, (GDestroyNotify) g_free), NULL);
	_tmp8_ = g_strdup ("Allan Day <aday@gnome.org>");
	_tmp9_ = g_new0 (gchar*, 1 + 1);
	_tmp9_[0] = _tmp8_;
	_tmp10_ = _tmp9_;
	_tmp10__length1 = 1;
	adw_about_window_set_designers (dialog, _tmp10_);
	_tmp10_ = (_vala_array_free (_tmp10_, _tmp10__length1, (GDestroyNotify) g_free), NULL);
	adw_about_window_set_translator_credits (dialog, _ ("translator-credits"));
	gtk_window_present ((GtkWindow*) dialog);
	_g_object_unref0 (dialog);
}

static void
break_timer_settings_application_class_init (BreakTimerSettingsApplicationClass * klass,
                                             gpointer klass_data)
{
	break_timer_settings_application_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BreakTimerSettingsApplication_private_offset);
	((GApplicationClass *) klass)->activate = (void (*) (GApplication*)) break_timer_settings_application_real_activate;
	((GApplicationClass *) klass)->startup = (void (*) (GApplication*)) break_timer_settings_application_real_startup;
	G_OBJECT_CLASS (klass)->finalize = break_timer_settings_application_finalize;
}

static void
break_timer_settings_application_instance_init (BreakTimerSettingsApplication * self,
                                                gpointer klass)
{
	self->priv = break_timer_settings_application_get_instance_private (self);
	self->priv->initial_focus = TRUE;
}

static void
break_timer_settings_application_finalize (GObject * obj)
{
	BreakTimerSettingsApplication * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BREAK_TIMER_SETTINGS_TYPE_APPLICATION, BreakTimerSettingsApplication);
	_g_object_unref0 (self->priv->break_manager);
	_g_object_unref0 (self->priv->main_window);
	G_OBJECT_CLASS (break_timer_settings_application_parent_class)->finalize (obj);
}

static GType
break_timer_settings_application_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BreakTimerSettingsApplicationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) break_timer_settings_application_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BreakTimerSettingsApplication), 0, (GInstanceInitFunc) break_timer_settings_application_instance_init, NULL };
	GType break_timer_settings_application_type_id;
	break_timer_settings_application_type_id = g_type_register_static (adw_application_get_type (), "BreakTimerSettingsApplication", &g_define_type_info, 0);
	BreakTimerSettingsApplication_private_offset = g_type_add_instance_private (break_timer_settings_application_type_id, sizeof (BreakTimerSettingsApplicationPrivate));
	return break_timer_settings_application_type_id;
}

GType
break_timer_settings_application_get_type (void)
{
	static volatile gsize break_timer_settings_application_type_id__once = 0;
	if (g_once_init_enter (&break_timer_settings_application_type_id__once)) {
		GType break_timer_settings_application_type_id;
		break_timer_settings_application_type_id = break_timer_settings_application_get_type_once ();
		g_once_init_leave (&break_timer_settings_application_type_id__once, break_timer_settings_application_type_id);
	}
	return break_timer_settings_application_type_id__once;
}

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

