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

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include "protonplus_core.h"
#include <gio/gio.h>
#include <adwaita.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.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  {
	PROTON_PLUS_UTILS_THEME_MANAGER_0_PROPERTY,
	PROTON_PLUS_UTILS_THEME_MANAGER_NUM_PROPERTIES
};
static GParamSpec* proton_plus_utils_theme_manager_properties[PROTON_PLUS_UTILS_THEME_MANAGER_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 _ProtonPlusUtilsThemeManagerPrivate {
	GtkCssProvider* custom_theme_provider;
};

static gint ProtonPlusUtilsThemeManager_private_offset;
static gpointer proton_plus_utils_theme_manager_parent_class = NULL;
static ProtonPlusUtilsThemeManager* proton_plus_utils_theme_manager__instance;
static ProtonPlusUtilsThemeManager* proton_plus_utils_theme_manager__instance = NULL;

static gint proton_plus_utils_theme_manager_get_effective_theme (ProtonPlusUtilsThemeManager* self,
                                                          gint theme);
static void proton_plus_utils_theme_manager_apply_custom_theme (ProtonPlusUtilsThemeManager* self,
                                                         gint theme);
static GObject * proton_plus_utils_theme_manager_constructor (GType type,
                                                       guint n_construct_properties,
                                                       GObjectConstructParam * construct_properties);
static void _proton_plus_utils_theme_manager___lambda405_ (ProtonPlusUtilsThemeManager* self);
static void __proton_plus_utils_theme_manager___lambda405__g_settings_changed (GSettings* _sender,
                                                                        const gchar* key,
                                                                        gpointer self);
static void _proton_plus_utils_theme_manager___lambda406_ (ProtonPlusUtilsThemeManager* self);
static void __proton_plus_utils_theme_manager___lambda406__g_object_notify (GObject* _sender,
                                                                     GParamSpec* pspec,
                                                                     gpointer self);
static void proton_plus_utils_theme_manager_finalize (GObject * obj);
static GType proton_plus_utils_theme_manager_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
proton_plus_utils_theme_manager_get_instance_private (ProtonPlusUtilsThemeManager* self)
{
	return G_STRUCT_MEMBER_P (self, ProtonPlusUtilsThemeManager_private_offset);
}

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

ProtonPlusUtilsThemeManager*
proton_plus_utils_theme_manager_get_default (void)
{
	ProtonPlusUtilsThemeManager* _tmp0_;
	ProtonPlusUtilsThemeManager* _tmp2_;
	ProtonPlusUtilsThemeManager* _tmp3_;
	ProtonPlusUtilsThemeManager* result;
	_tmp0_ = proton_plus_utils_theme_manager__instance;
	if (_tmp0_ == NULL) {
		ProtonPlusUtilsThemeManager* _tmp1_;
		_tmp1_ = proton_plus_utils_theme_manager_new ();
		_g_object_unref0 (proton_plus_utils_theme_manager__instance);
		proton_plus_utils_theme_manager__instance = _tmp1_;
	}
	_tmp2_ = proton_plus_utils_theme_manager__instance;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

void
proton_plus_utils_theme_manager_apply_theme (ProtonPlusUtilsThemeManager* self)
{
	gint theme = 0;
	GSettings* _tmp0_;
	AdwStyleManager* style_manager = NULL;
	AdwStyleManager* _tmp1_;
	AdwStyleManager* _tmp2_;
	AdwColorScheme _tmp3_ = 0;
	gboolean _tmp4_ = FALSE;
	AdwStyleManager* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = proton_plus_globals_SETTINGS;
	theme = proton_plus_utils_theme_manager_get_effective_theme (self, g_settings_get_enum (_tmp0_, "theme"));
	_tmp1_ = adw_style_manager_get_default ();
	_tmp2_ = _g_object_ref0 (_tmp1_);
	style_manager = _tmp2_;
	if (theme == 3) {
		_tmp4_ = TRUE;
	} else {
		_tmp4_ = theme == 4;
	}
	if (_tmp4_) {
		_tmp3_ = ADW_COLOR_SCHEME_FORCE_DARK;
	} else {
		_tmp3_ = ADW_COLOR_SCHEME_DEFAULT;
	}
	_tmp5_ = style_manager;
	adw_style_manager_set_color_scheme (_tmp5_, _tmp3_);
	proton_plus_utils_theme_manager_apply_custom_theme (self, theme);
	_g_object_unref0 (style_manager);
}

static gint
proton_plus_utils_theme_manager_get_effective_theme (ProtonPlusUtilsThemeManager* self,
                                                     gint theme)
{
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	if (theme != ((gint) ADW_COLOR_SCHEME_DEFAULT)) {
		result = theme;
		return result;
	}
	if (proton_plus_globals_IS_STEAM_OS) {
		result = 3;
		return result;
	}
	if (proton_plus_utils_system_is_kde ()) {
		result = 2;
		return result;
	}
	result = 1;
	return result;
}

static void
proton_plus_utils_theme_manager_apply_custom_theme (ProtonPlusUtilsThemeManager* self,
                                                    gint theme)
{
	GdkDisplay* display = NULL;
	GdkDisplay* _tmp0_;
	GdkDisplay* _tmp1_;
	GdkDisplay* _tmp2_;
	GtkCssProvider* _tmp3_;
	gchar* stylesheet = NULL;
	const gchar* _tmp11_;
	GtkCssProvider* _tmp12_;
	GtkCssProvider* _tmp13_;
	const gchar* _tmp14_;
	GdkDisplay* _tmp15_;
	GtkCssProvider* _tmp16_;
	g_return_if_fail (self != NULL);
	_tmp0_ = gdk_display_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	display = _tmp1_;
	_tmp2_ = display;
	if (_tmp2_ == NULL) {
		_g_object_unref0 (display);
		return;
	}
	_tmp3_ = self->priv->custom_theme_provider;
	if (_tmp3_ != NULL) {
		GdkDisplay* _tmp4_;
		GtkCssProvider* _tmp5_;
		_tmp4_ = display;
		_tmp5_ = self->priv->custom_theme_provider;
		gtk_style_context_remove_provider_for_display (_tmp4_, (GtkStyleProvider*) _tmp5_);
		_g_object_unref0 (self->priv->custom_theme_provider);
		self->priv->custom_theme_provider = NULL;
	}
	stylesheet = NULL;
	switch (theme) {
		case 2:
		{
			const gchar* _tmp6_ = NULL;
			AdwStyleManager* _tmp7_;
			gchar* _tmp8_;
			_tmp7_ = adw_style_manager_get_default ();
			if (adw_style_manager_get_dark (_tmp7_)) {
				_tmp6_ = "/com/vysp3r/ProtonPlus/breeze-dark.css";
			} else {
				_tmp6_ = "/com/vysp3r/ProtonPlus/breeze-light.css";
			}
			_tmp8_ = g_strdup (_tmp6_);
			_g_free0 (stylesheet);
			stylesheet = _tmp8_;
			break;
		}
		case 3:
		{
			gchar* _tmp9_;
			_tmp9_ = g_strdup ("/com/vysp3r/ProtonPlus/steamos.css");
			_g_free0 (stylesheet);
			stylesheet = _tmp9_;
			break;
		}
		case 4:
		{
			gchar* _tmp10_;
			_tmp10_ = g_strdup ("/com/vysp3r/ProtonPlus/oled.css");
			_g_free0 (stylesheet);
			stylesheet = _tmp10_;
			break;
		}
		default:
		break;
	}
	_tmp11_ = stylesheet;
	if (_tmp11_ == NULL) {
		_g_free0 (stylesheet);
		_g_object_unref0 (display);
		return;
	}
	_tmp12_ = gtk_css_provider_new ();
	_g_object_unref0 (self->priv->custom_theme_provider);
	self->priv->custom_theme_provider = _tmp12_;
	_tmp13_ = self->priv->custom_theme_provider;
	_tmp14_ = stylesheet;
	gtk_css_provider_load_from_resource (_tmp13_, _tmp14_);
	_tmp15_ = display;
	_tmp16_ = self->priv->custom_theme_provider;
	gtk_style_context_add_provider_for_display (_tmp15_, (GtkStyleProvider*) _tmp16_, (guint) (GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1));
	_g_free0 (stylesheet);
	_g_object_unref0 (display);
}

ProtonPlusUtilsThemeManager*
proton_plus_utils_theme_manager_construct (GType object_type)
{
	ProtonPlusUtilsThemeManager * self = NULL;
	self = (ProtonPlusUtilsThemeManager*) g_object_new (object_type, NULL);
	return self;
}

ProtonPlusUtilsThemeManager*
proton_plus_utils_theme_manager_new (void)
{
	return proton_plus_utils_theme_manager_construct (PROTON_PLUS_UTILS_TYPE_THEME_MANAGER);
}

static void
_proton_plus_utils_theme_manager___lambda405_ (ProtonPlusUtilsThemeManager* self)
{
	proton_plus_utils_theme_manager_apply_theme (self);
}

static void
__proton_plus_utils_theme_manager___lambda405__g_settings_changed (GSettings* _sender,
                                                                   const gchar* key,
                                                                   gpointer self)
{
	_proton_plus_utils_theme_manager___lambda405_ ((ProtonPlusUtilsThemeManager*) self);
}

static void
_proton_plus_utils_theme_manager___lambda406_ (ProtonPlusUtilsThemeManager* self)
{
	GSettings* _tmp0_;
	_tmp0_ = proton_plus_globals_SETTINGS;
	if (proton_plus_utils_theme_manager_get_effective_theme (self, g_settings_get_enum (_tmp0_, "theme")) == 2) {
		proton_plus_utils_theme_manager_apply_custom_theme (self, 2);
	}
}

static void
__proton_plus_utils_theme_manager___lambda406__g_object_notify (GObject* _sender,
                                                                GParamSpec* pspec,
                                                                gpointer self)
{
	_proton_plus_utils_theme_manager___lambda406_ ((ProtonPlusUtilsThemeManager*) self);
}

static GObject *
proton_plus_utils_theme_manager_constructor (GType type,
                                             guint n_construct_properties,
                                             GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ProtonPlusUtilsThemeManager * self;
	AdwApplication* application = NULL;
	GApplication* _tmp0_;
	AdwApplication* _tmp1_;
	AdwStyleManager* style_manager = NULL;
	AdwStyleManager* _tmp2_;
	AdwStyleManager* _tmp3_;
	GSettings* _tmp4_;
	AdwStyleManager* _tmp5_;
	parent_class = G_OBJECT_CLASS (proton_plus_utils_theme_manager_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PROTON_PLUS_UTILS_TYPE_THEME_MANAGER, ProtonPlusUtilsThemeManager);
	_tmp0_ = g_application_get_default ();
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, adw_application_get_type (), AdwApplication));
	application = _tmp1_;
	_tmp2_ = adw_style_manager_get_default ();
	_tmp3_ = _g_object_ref0 (_tmp2_);
	style_manager = _tmp3_;
	_tmp4_ = proton_plus_globals_SETTINGS;
	g_signal_connect_object (_tmp4_, "changed::theme", (GCallback) __proton_plus_utils_theme_manager___lambda405__g_settings_changed, self, 0);
	_tmp5_ = style_manager;
	g_signal_connect_object ((GObject*) _tmp5_, "notify::dark", (GCallback) __proton_plus_utils_theme_manager___lambda406__g_object_notify, self, 0);
	_g_object_unref0 (style_manager);
	_g_object_unref0 (application);
	return obj;
}

static void
proton_plus_utils_theme_manager_class_init (ProtonPlusUtilsThemeManagerClass * klass,
                                            gpointer klass_data)
{
	proton_plus_utils_theme_manager_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ProtonPlusUtilsThemeManager_private_offset);
	G_OBJECT_CLASS (klass)->constructor = proton_plus_utils_theme_manager_constructor;
	G_OBJECT_CLASS (klass)->finalize = proton_plus_utils_theme_manager_finalize;
}

static void
proton_plus_utils_theme_manager_instance_init (ProtonPlusUtilsThemeManager * self,
                                               gpointer klass)
{
	self->priv = proton_plus_utils_theme_manager_get_instance_private (self);
}

static void
proton_plus_utils_theme_manager_finalize (GObject * obj)
{
	ProtonPlusUtilsThemeManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PROTON_PLUS_UTILS_TYPE_THEME_MANAGER, ProtonPlusUtilsThemeManager);
	_g_object_unref0 (self->priv->custom_theme_provider);
	G_OBJECT_CLASS (proton_plus_utils_theme_manager_parent_class)->finalize (obj);
}

static GType
proton_plus_utils_theme_manager_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ProtonPlusUtilsThemeManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) proton_plus_utils_theme_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ProtonPlusUtilsThemeManager), 0, (GInstanceInitFunc) proton_plus_utils_theme_manager_instance_init, NULL };
	GType proton_plus_utils_theme_manager_type_id;
	proton_plus_utils_theme_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "ProtonPlusUtilsThemeManager", &g_define_type_info, 0);
	ProtonPlusUtilsThemeManager_private_offset = g_type_add_instance_private (proton_plus_utils_theme_manager_type_id, sizeof (ProtonPlusUtilsThemeManagerPrivate));
	return proton_plus_utils_theme_manager_type_id;
}

GType
proton_plus_utils_theme_manager_get_type (void)
{
	static volatile gsize proton_plus_utils_theme_manager_type_id__once = 0;
	if (g_once_init_enter (&proton_plus_utils_theme_manager_type_id__once)) {
		GType proton_plus_utils_theme_manager_type_id;
		proton_plus_utils_theme_manager_type_id = proton_plus_utils_theme_manager_get_type_once ();
		g_once_init_leave (&proton_plus_utils_theme_manager_type_id__once, proton_plus_utils_theme_manager_type_id);
	}
	return proton_plus_utils_theme_manager_type_id__once;
}

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

