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

/* -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-*/
/*-
 * Copyright (c) 2019 Adam Bieńkowski (https://github.com/elementary/switchboard-plug-parental-controls)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Adam Bieńkowski <donadigos159@gmail.com>
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define PC_DAEMON_TYPE_RESTRICTION (pc_daemon_restriction_get_type ())
#define PC_DAEMON_RESTRICTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_RESTRICTION, PCDaemonRestriction))
#define PC_DAEMON_RESTRICTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_RESTRICTION, PCDaemonRestrictionClass))
#define PC_DAEMON_IS_RESTRICTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_RESTRICTION))
#define PC_DAEMON_IS_RESTRICTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_RESTRICTION))
#define PC_DAEMON_RESTRICTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_RESTRICTION, PCDaemonRestrictionClass))

typedef struct _PCDaemonRestriction PCDaemonRestriction;
typedef struct _PCDaemonRestrictionClass PCDaemonRestrictionClass;
typedef struct _PCDaemonRestrictionPrivate PCDaemonRestrictionPrivate;

#define PC_DAEMON_TYPE_USER_CONFIG (pc_daemon_user_config_get_type ())
#define PC_DAEMON_USER_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_USER_CONFIG, PCDaemonUserConfig))
#define PC_DAEMON_USER_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_USER_CONFIG, PCDaemonUserConfigClass))
#define PC_DAEMON_IS_USER_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_USER_CONFIG))
#define PC_DAEMON_IS_USER_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_USER_CONFIG))
#define PC_DAEMON_USER_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_USER_CONFIG, PCDaemonUserConfigClass))

typedef struct _PCDaemonUserConfig PCDaemonUserConfig;
typedef struct _PCDaemonUserConfigClass PCDaemonUserConfigClass;

#define PC_DAEMON_TYPE_APP_RESTRICTION (pc_daemon_app_restriction_get_type ())
#define PC_DAEMON_APP_RESTRICTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_APP_RESTRICTION, PCDaemonAppRestriction))
#define PC_DAEMON_APP_RESTRICTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_APP_RESTRICTION, PCDaemonAppRestrictionClass))
#define PC_DAEMON_IS_APP_RESTRICTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_APP_RESTRICTION))
#define PC_DAEMON_IS_APP_RESTRICTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_APP_RESTRICTION))
#define PC_DAEMON_APP_RESTRICTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_APP_RESTRICTION, PCDaemonAppRestrictionClass))

typedef struct _PCDaemonAppRestriction PCDaemonAppRestriction;
typedef struct _PCDaemonAppRestrictionClass PCDaemonAppRestrictionClass;
typedef struct _PCDaemonAppRestrictionPrivate PCDaemonAppRestrictionPrivate;
enum  {
	PC_DAEMON_APP_RESTRICTION_0_PROPERTY,
	PC_DAEMON_APP_RESTRICTION_NUM_PROPERTIES
};
static GParamSpec* pc_daemon_app_restriction_properties[PC_DAEMON_APP_RESTRICTION_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block5Data Block5Data;

struct _PCDaemonRestriction {
	GObject parent_instance;
	PCDaemonRestrictionPrivate * priv;
	PCDaemonUserConfig* config;
};

struct _PCDaemonRestrictionClass {
	GObjectClass parent_class;
	void (*start) (PCDaemonRestriction* self);
	void (*stop) (PCDaemonRestriction* self);
};

struct _PCDaemonAppRestriction {
	PCDaemonRestriction parent_instance;
	PCDaemonAppRestrictionPrivate * priv;
};

struct _PCDaemonAppRestrictionClass {
	PCDaemonRestrictionClass parent_class;
};

struct _PCDaemonAppRestrictionPrivate {
	gchar** old_targets;
	gint old_targets_length1;
	gint _old_targets_size_;
	gboolean old_admin;
};

struct _Block5Data {
	int _ref_count_;
	PCDaemonAppRestriction* self;
	PCDaemonUserConfig* config;
};

static gint PCDaemonAppRestriction_private_offset;
static gpointer pc_daemon_app_restriction_parent_class = NULL;

VALA_EXTERN GType pc_daemon_restriction_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonRestriction, g_object_unref)
VALA_EXTERN GType pc_daemon_user_config_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonUserConfig, g_object_unref)
VALA_EXTERN GType pc_daemon_app_restriction_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonAppRestriction, g_object_unref)
VALA_EXTERN void pc_daemon_restriction_start (PCDaemonRestriction* self);
VALA_EXTERN void pc_daemon_restriction_stop (PCDaemonRestriction* self);
VALA_EXTERN gboolean pc_daemon_app_restriction_get_supported (void);
VALA_EXTERN gboolean pc_access_control_lists_get_supported (void);
VALA_EXTERN PCDaemonAppRestriction* pc_daemon_app_restriction_new (PCDaemonUserConfig* config);
VALA_EXTERN PCDaemonAppRestriction* pc_daemon_app_restriction_construct (GType object_type,
                                                             PCDaemonUserConfig* config);
static Block5Data* block5_data_ref (Block5Data* _data5_);
static void block5_data_unref (void * _userdata_);
VALA_EXTERN PCDaemonRestriction* pc_daemon_restriction_construct (GType object_type,
                                                      PCDaemonUserConfig* config);
static void pc_daemon_app_restriction_update (PCDaemonAppRestriction* self);
static void _pc_daemon_app_restriction_update_g_object_notify (GObject* _sender,
                                                        GParamSpec* pspec,
                                                        gpointer self);
static void __lambda4_ (Block5Data* _data5_);
VALA_EXTERN gboolean pc_daemon_user_config_get_active (PCDaemonUserConfig* self);
static void pc_daemon_app_restriction_remove_restrictions (PCDaemonAppRestriction* self);
static void ___lambda4__g_object_notify (GObject* _sender,
                                  GParamSpec* pspec,
                                  gpointer self);
static void pc_daemon_app_restriction_real_start (PCDaemonRestriction* base);
static void pc_daemon_app_restriction_real_stop (PCDaemonRestriction* base);
VALA_EXTERN gboolean pc_daemon_user_config_get_admin (PCDaemonUserConfig* self);
VALA_EXTERN void pc_access_control_lists_apply_targets (const gchar* username,
                                            gchar** old_targets,
                                            gint old_targets_length1,
                                            gchar** targets,
                                            gint targets_length1,
                                            gboolean old_admin,
                                            gboolean admin);
VALA_EXTERN const gchar* pc_daemon_user_config_get_username (PCDaemonUserConfig* self);
VALA_EXTERN gchar** pc_daemon_user_config_get_targets (PCDaemonUserConfig* self,
                                           gint* result_length1);
static gchar** _vala_array_dup1 (gchar** self,
                          gssize length);
static void pc_daemon_app_restriction_finalize (GObject * obj);
static GType pc_daemon_app_restriction_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
pc_daemon_app_restriction_get_instance_private (PCDaemonAppRestriction* self)
{
	return G_STRUCT_MEMBER_P (self, PCDaemonAppRestriction_private_offset);
}

gboolean
pc_daemon_app_restriction_get_supported (void)
{
	gboolean result;
	result = pc_access_control_lists_get_supported ();
	return result;
}

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

static Block5Data*
block5_data_ref (Block5Data* _data5_)
{
	g_atomic_int_inc (&_data5_->_ref_count_);
	return _data5_;
}

static void
block5_data_unref (void * _userdata_)
{
	Block5Data* _data5_;
	_data5_ = (Block5Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data5_->_ref_count_)) {
		PCDaemonAppRestriction* self;
		self = _data5_->self;
		_g_object_unref0 (_data5_->config);
		_g_object_unref0 (self);
		g_slice_free (Block5Data, _data5_);
	}
}

static void
_pc_daemon_app_restriction_update_g_object_notify (GObject* _sender,
                                                   GParamSpec* pspec,
                                                   gpointer self)
{
	pc_daemon_app_restriction_update ((PCDaemonAppRestriction*) self);
}

static void
__lambda4_ (Block5Data* _data5_)
{
	PCDaemonAppRestriction* self;
	gboolean _tmp0_;
	gboolean _tmp1_;
	self = _data5_->self;
	_tmp0_ = pc_daemon_user_config_get_active (_data5_->config);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		pc_daemon_app_restriction_update (self);
	} else {
		pc_daemon_app_restriction_remove_restrictions (self);
	}
}

static void
___lambda4__g_object_notify (GObject* _sender,
                             GParamSpec* pspec,
                             gpointer self)
{
	__lambda4_ (self);
}

PCDaemonAppRestriction*
pc_daemon_app_restriction_construct (GType object_type,
                                     PCDaemonUserConfig* config)
{
	PCDaemonAppRestriction * self = NULL;
	Block5Data* _data5_;
	PCDaemonUserConfig* _tmp0_;
	g_return_val_if_fail (config != NULL, NULL);
	_data5_ = g_slice_new0 (Block5Data);
	_data5_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (config);
	_g_object_unref0 (_data5_->config);
	_data5_->config = _tmp0_;
	self = (PCDaemonAppRestriction*) pc_daemon_restriction_construct (object_type, _data5_->config);
	_data5_->self = g_object_ref (self);
	g_signal_connect_object ((GObject*) _data5_->config, "notify::targets", (GCallback) _pc_daemon_app_restriction_update_g_object_notify, self, 0);
	g_signal_connect_object ((GObject*) _data5_->config, "notify::admin", (GCallback) _pc_daemon_app_restriction_update_g_object_notify, self, 0);
	g_signal_connect_data ((GObject*) _data5_->config, "notify::active", (GCallback) ___lambda4__g_object_notify, block5_data_ref (_data5_), (GClosureNotify) block5_data_unref, 0);
	block5_data_unref (_data5_);
	_data5_ = NULL;
	return self;
}

PCDaemonAppRestriction*
pc_daemon_app_restriction_new (PCDaemonUserConfig* config)
{
	return pc_daemon_app_restriction_construct (PC_DAEMON_TYPE_APP_RESTRICTION, config);
}

static void
pc_daemon_app_restriction_real_start (PCDaemonRestriction* base)
{
	PCDaemonAppRestriction * self;
	self = (PCDaemonAppRestriction*) base;
}

static void
pc_daemon_app_restriction_real_stop (PCDaemonRestriction* base)
{
	PCDaemonAppRestriction * self;
	self = (PCDaemonAppRestriction*) base;
}

static void
pc_daemon_app_restriction_remove_restrictions (PCDaemonAppRestriction* self)
{
	gboolean admin = FALSE;
	PCDaemonUserConfig* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	PCDaemonUserConfig* _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	PCDaemonUserConfig* _tmp6_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	gint _tmp8_ = 0;
	gchar** _tmp9_;
	gint _tmp9__length1;
	gchar** _tmp10_;
	gint _tmp10__length1;
	gchar** _tmp11_;
	gchar** _tmp12_;
	gint _tmp12__length1;
	g_return_if_fail (self != NULL);
	_tmp0_ = ((PCDaemonRestriction*) self)->config;
	_tmp1_ = pc_daemon_user_config_get_admin (_tmp0_);
	_tmp2_ = _tmp1_;
	admin = _tmp2_;
	_tmp3_ = ((PCDaemonRestriction*) self)->config;
	_tmp4_ = pc_daemon_user_config_get_username (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = ((PCDaemonRestriction*) self)->config;
	_tmp7_ = pc_daemon_user_config_get_targets (_tmp6_, &_tmp8_);
	_tmp7__length1 = _tmp8_;
	_tmp9_ = _tmp7_;
	_tmp9__length1 = _tmp7__length1;
	_tmp10_ = _tmp9_;
	_tmp10__length1 = _tmp9__length1;
	_tmp11_ = g_new0 (gchar*, 0 + 1);
	_tmp12_ = _tmp11_;
	_tmp12__length1 = 0;
	pc_access_control_lists_apply_targets (_tmp5_, _tmp10_, (gint) _tmp9__length1, _tmp12_, (gint) 0, admin, admin);
	_tmp12_ = (_vala_array_free (_tmp12_, _tmp12__length1, (GDestroyNotify) g_free), NULL);
	_tmp10_ = (_vala_array_free (_tmp10_, _tmp10__length1, (GDestroyNotify) g_free), NULL);
}

static gchar**
_vala_array_dup1 (gchar** self,
                  gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

static void
pc_daemon_app_restriction_update (PCDaemonAppRestriction* self)
{
	gchar** new_targets = NULL;
	PCDaemonUserConfig* _tmp0_;
	gchar** _tmp1_;
	gint _tmp1__length1;
	gint _tmp2_ = 0;
	gchar** _tmp3_;
	gint _tmp3__length1;
	gint new_targets_length1;
	gint _new_targets_size_;
	gboolean new_admin = FALSE;
	PCDaemonUserConfig* _tmp4_;
	gboolean _tmp5_;
	gboolean _tmp6_;
	PCDaemonUserConfig* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar** _tmp10_;
	gint _tmp10__length1;
	gchar** _tmp11_;
	gint _tmp11__length1;
	g_return_if_fail (self != NULL);
	_tmp0_ = ((PCDaemonRestriction*) self)->config;
	_tmp1_ = pc_daemon_user_config_get_targets (_tmp0_, &_tmp2_);
	_tmp1__length1 = _tmp2_;
	_tmp3_ = _tmp1_;
	_tmp3__length1 = _tmp1__length1;
	new_targets = _tmp3_;
	new_targets_length1 = _tmp3__length1;
	_new_targets_size_ = new_targets_length1;
	_tmp4_ = ((PCDaemonRestriction*) self)->config;
	_tmp5_ = pc_daemon_user_config_get_admin (_tmp4_);
	_tmp6_ = _tmp5_;
	new_admin = _tmp6_;
	_tmp7_ = ((PCDaemonRestriction*) self)->config;
	_tmp8_ = pc_daemon_user_config_get_username (_tmp7_);
	_tmp9_ = _tmp8_;
	_tmp10_ = self->priv->old_targets;
	_tmp10__length1 = self->priv->old_targets_length1;
	pc_access_control_lists_apply_targets (_tmp9_, _tmp10_, (gint) _tmp10__length1, new_targets, (gint) new_targets_length1, self->priv->old_admin, new_admin);
	_tmp11_ = (new_targets != NULL) ? _vala_array_dup1 (new_targets, new_targets_length1) : new_targets;
	_tmp11__length1 = new_targets_length1;
	self->priv->old_targets = (_vala_array_free (self->priv->old_targets, self->priv->old_targets_length1, (GDestroyNotify) g_free), NULL);
	self->priv->old_targets = _tmp11_;
	self->priv->old_targets_length1 = _tmp11__length1;
	self->priv->_old_targets_size_ = self->priv->old_targets_length1;
	self->priv->old_admin = new_admin;
	new_targets = (_vala_array_free (new_targets, new_targets_length1, (GDestroyNotify) g_free), NULL);
}

static void
pc_daemon_app_restriction_class_init (PCDaemonAppRestrictionClass * klass,
                                      gpointer klass_data)
{
	pc_daemon_app_restriction_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PCDaemonAppRestriction_private_offset);
	((PCDaemonRestrictionClass *) klass)->start = (void (*) (PCDaemonRestriction*)) pc_daemon_app_restriction_real_start;
	((PCDaemonRestrictionClass *) klass)->stop = (void (*) (PCDaemonRestriction*)) pc_daemon_app_restriction_real_stop;
	G_OBJECT_CLASS (klass)->finalize = pc_daemon_app_restriction_finalize;
}

static void
pc_daemon_app_restriction_instance_init (PCDaemonAppRestriction * self,
                                         gpointer klass)
{
	gchar** _tmp0_;
	self->priv = pc_daemon_app_restriction_get_instance_private (self);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	self->priv->old_targets = _tmp0_;
	self->priv->old_targets_length1 = 0;
	self->priv->_old_targets_size_ = self->priv->old_targets_length1;
	self->priv->old_admin = FALSE;
}

static void
pc_daemon_app_restriction_finalize (GObject * obj)
{
	PCDaemonAppRestriction * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PC_DAEMON_TYPE_APP_RESTRICTION, PCDaemonAppRestriction);
	self->priv->old_targets = (_vala_array_free (self->priv->old_targets, self->priv->old_targets_length1, (GDestroyNotify) g_free), NULL);
	G_OBJECT_CLASS (pc_daemon_app_restriction_parent_class)->finalize (obj);
}

static GType
pc_daemon_app_restriction_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PCDaemonAppRestrictionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pc_daemon_app_restriction_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PCDaemonAppRestriction), 0, (GInstanceInitFunc) pc_daemon_app_restriction_instance_init, NULL };
	GType pc_daemon_app_restriction_type_id;
	pc_daemon_app_restriction_type_id = g_type_register_static (PC_DAEMON_TYPE_RESTRICTION, "PCDaemonAppRestriction", &g_define_type_info, 0);
	PCDaemonAppRestriction_private_offset = g_type_add_instance_private (pc_daemon_app_restriction_type_id, sizeof (PCDaemonAppRestrictionPrivate));
	return pc_daemon_app_restriction_type_id;
}

GType
pc_daemon_app_restriction_get_type (void)
{
	static volatile gsize pc_daemon_app_restriction_type_id__once = 0;
	if (g_once_init_enter (&pc_daemon_app_restriction_type_id__once)) {
		GType pc_daemon_app_restriction_type_id;
		pc_daemon_app_restriction_type_id = pc_daemon_app_restriction_get_type_once ();
		g_once_init_leave (&pc_daemon_app_restriction_type_id__once, pc_daemon_app_restriction_type_id);
	}
	return pc_daemon_app_restriction_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);
}

