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

/* -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-*/
/*-
 * Copyright (c) 2015 Adam Bieńkowski (https://launchpad.net/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 <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <act/act.h>

#define PC_CONSTANTS_PARENTAL_CONTROLS_OBJECT_PATH "/org/pantheon/ParentalControls"
#define PC_CONSTANTS_PARENTAL_CONTROLS_IFACE "org.pantheon.ParentalControls"
#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_DAEMON (pc_daemon_daemon_get_type ())
#define PC_DAEMON_DAEMON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_DAEMON, PCDaemonDaemon))
#define PC_DAEMON_DAEMON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_DAEMON, PCDaemonDaemonClass))
#define PC_DAEMON_IS_DAEMON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_DAEMON))
#define PC_DAEMON_IS_DAEMON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_DAEMON))
#define PC_DAEMON_DAEMON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_DAEMON, PCDaemonDaemonClass))

typedef struct _PCDaemonDaemon PCDaemonDaemon;
typedef struct _PCDaemonDaemonClass PCDaemonDaemonClass;
typedef struct _PCDaemonDaemonPrivate PCDaemonDaemonPrivate;
enum  {
	PC_DAEMON_DAEMON_0_PROPERTY,
	PC_DAEMON_DAEMON_NUM_PROPERTIES
};
static GParamSpec* pc_daemon_daemon_properties[PC_DAEMON_DAEMON_NUM_PROPERTIES];

#define PC_DAEMON_TYPE_SESSION_MANAGER (pc_daemon_session_manager_get_type ())
#define PC_DAEMON_SESSION_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_SESSION_MANAGER, PCDaemonSessionManager))
#define PC_DAEMON_SESSION_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_SESSION_MANAGER, PCDaemonSessionManagerClass))
#define PC_DAEMON_IS_SESSION_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_SESSION_MANAGER))
#define PC_DAEMON_IS_SESSION_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_SESSION_MANAGER))
#define PC_DAEMON_SESSION_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_SESSION_MANAGER, PCDaemonSessionManagerClass))

typedef struct _PCDaemonSessionManager PCDaemonSessionManager;
typedef struct _PCDaemonSessionManagerClass PCDaemonSessionManagerClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define PC_DAEMON_TYPE_SERVER (pc_daemon_server_get_type ())
#define PC_DAEMON_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PC_DAEMON_TYPE_SERVER, PCDaemonServer))
#define PC_DAEMON_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PC_DAEMON_TYPE_SERVER, PCDaemonServerClass))
#define PC_DAEMON_IS_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PC_DAEMON_TYPE_SERVER))
#define PC_DAEMON_IS_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PC_DAEMON_TYPE_SERVER))
#define PC_DAEMON_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PC_DAEMON_TYPE_SERVER, PCDaemonServerClass))

typedef struct _PCDaemonServer PCDaemonServer;
typedef struct _PCDaemonServerClass PCDaemonServerClass;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _PCDaemonDaemon {
	GApplication parent_instance;
	PCDaemonDaemonPrivate * priv;
};

struct _PCDaemonDaemonClass {
	GApplicationClass parent_class;
};

static gpointer pc_daemon_daemon_parent_class = NULL;
static PCDaemonDaemon* pc_daemon_daemon_instance;
static PCDaemonDaemon* pc_daemon_daemon_instance = NULL;
static PCDaemonSessionManager* pc_daemon_daemon_session_manager;
static PCDaemonSessionManager* pc_daemon_daemon_session_manager = NULL;

VALA_EXTERN GType pc_daemon_daemon_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonDaemon, g_object_unref)
VALA_EXTERN GType pc_daemon_session_manager_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonSessionManager, g_object_unref)
VALA_EXTERN PCDaemonDaemon* pc_daemon_daemon_get_instance (void);
VALA_EXTERN PCDaemonDaemon* pc_daemon_daemon_new (void);
VALA_EXTERN PCDaemonDaemon* pc_daemon_daemon_construct (GType object_type);
static gint pc_daemon_daemon_main (gchar** args,
                            gint args_length1);
VALA_EXTERN void pc_daemon_daemon_on_exit (gint signum);
static void _pc_daemon_daemon_on_exit___sighandler_t (gint signum);
VALA_EXTERN void pc_daemon_session_manager_stop (PCDaemonSessionManager* self);
static void pc_daemon_daemon_real_activate (GApplication* base);
VALA_EXTERN ActUserManager* pc_utils_get_usermanager (void);
static void pc_daemon_daemon_on_usermanager_loaded (PCDaemonDaemon* self);
static void _pc_daemon_daemon_on_usermanager_loaded_g_object_notify (GObject* _sender,
                                                              GParamSpec* pspec,
                                                              gpointer self);
static void pc_daemon_daemon_on_bus_lost (PCDaemonDaemon* self,
                                   GDBusConnection* connection,
                                   const gchar* name);
static void pc_daemon_daemon_on_bus_acquired (PCDaemonDaemon* self,
                                       GDBusConnection* connection);
VALA_EXTERN GType pc_daemon_server_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PCDaemonServer, g_object_unref)
VALA_EXTERN guint pc_daemon_server_register_object (void* object,
                                        GDBusConnection* connection,
                                        const gchar* path,
                                        GError** error);
VALA_EXTERN PCDaemonServer* pc_daemon_server_get_default (void);
static void _pc_daemon_daemon_on_bus_acquired_gbus_acquired_callback (GDBusConnection* connection,
                                                               const gchar* name,
                                                               gpointer self);
static void __lambda14_ (PCDaemonDaemon* self);
static void ___lambda14__gbus_name_acquired_callback (GDBusConnection* connection,
                                               const gchar* name,
                                               gpointer self);
static void _pc_daemon_daemon_on_bus_lost_gbus_name_lost_callback (GDBusConnection* connection,
                                                            const gchar* name,
                                                            gpointer self);
VALA_EXTERN PCDaemonSessionManager* pc_daemon_session_manager_get_default (void);
VALA_EXTERN void pc_daemon_session_manager_start (PCDaemonSessionManager* self);
static void pc_daemon_daemon_finalize (GObject * obj);
static GType pc_daemon_daemon_get_type_once (void);

PCDaemonDaemon*
pc_daemon_daemon_get_instance (void)
{
	PCDaemonDaemon* _tmp0_;
	PCDaemonDaemon* _tmp2_;
	PCDaemonDaemon* result;
	_tmp0_ = pc_daemon_daemon_instance;
	if (_tmp0_ == NULL) {
		PCDaemonDaemon* _tmp1_;
		_tmp1_ = pc_daemon_daemon_new ();
		_g_object_unref0 (pc_daemon_daemon_instance);
		pc_daemon_daemon_instance = _tmp1_;
	}
	_tmp2_ = pc_daemon_daemon_instance;
	result = _tmp2_;
	return result;
}

static void
_pc_daemon_daemon_on_exit___sighandler_t (gint signum)
{
	pc_daemon_daemon_on_exit (signum);
}

static gint
pc_daemon_daemon_main (gchar** args,
                       gint args_length1)
{
	PCDaemonDaemon* _tmp0_;
	gint result;
	signal (SIGINT, _pc_daemon_daemon_on_exit___sighandler_t);
	signal (SIGTERM, _pc_daemon_daemon_on_exit___sighandler_t);
	_tmp0_ = pc_daemon_daemon_get_instance ();
	result = g_application_run ((GApplication*) _tmp0_, (gint) args_length1, args);
	return result;
}

int
main (int argc,
      char ** argv)
{
	return pc_daemon_daemon_main (argv, argc);
}

void
pc_daemon_daemon_on_exit (gint signum)
{
	PCDaemonSessionManager* _tmp0_;
	GApplication* _tmp2_;
	_tmp0_ = pc_daemon_daemon_session_manager;
	if (_tmp0_ != NULL) {
		PCDaemonSessionManager* _tmp1_;
		_tmp1_ = pc_daemon_daemon_session_manager;
		pc_daemon_session_manager_stop (_tmp1_);
	}
	_tmp2_ = g_application_get_default ();
	g_application_release (_tmp2_);
}

static void
_pc_daemon_daemon_on_usermanager_loaded_g_object_notify (GObject* _sender,
                                                         GParamSpec* pspec,
                                                         gpointer self)
{
	pc_daemon_daemon_on_usermanager_loaded ((PCDaemonDaemon*) self);
}

static void
pc_daemon_daemon_real_activate (GApplication* base)
{
	PCDaemonDaemon * self;
	ActUserManager* _tmp0_;
	self = (PCDaemonDaemon*) base;
	_tmp0_ = pc_utils_get_usermanager ();
	g_signal_connect_object ((GObject*) _tmp0_, "notify::is-loaded", (GCallback) _pc_daemon_daemon_on_usermanager_loaded_g_object_notify, self, 0);
	g_application_hold ((GApplication*) self);
}

static void
pc_daemon_daemon_on_bus_lost (PCDaemonDaemon* self,
                              GDBusConnection* connection,
                              const gchar* name)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (connection != NULL);
	g_return_if_fail (name != NULL);
	g_warning ("Daemon.vala:58: Could not acquire name: %s\n", name);
}

static void
pc_daemon_daemon_on_bus_acquired (PCDaemonDaemon* self,
                                  GDBusConnection* connection)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (connection != NULL);
	{
		PCDaemonServer* _tmp0_;
		PCDaemonServer* _tmp1_;
		_tmp0_ = pc_daemon_server_get_default ();
		_tmp1_ = _tmp0_;
		pc_daemon_server_register_object (_tmp1_, connection, PC_CONSTANTS_PARENTAL_CONTROLS_OBJECT_PATH, &_inner_error0_);
		_g_object_unref0 (_tmp1_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_IO_ERROR) {
				goto __catch0_g_io_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;
		}
	}
	goto __finally0;
	__catch0_g_io_error:
	{
		GError* e = NULL;
		GError* _tmp2_;
		const gchar* _tmp3_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = e;
		_tmp3_ = _tmp2_->message;
		g_warning ("Daemon.vala:65: %s\n", _tmp3_);
		_g_error_free0 (e);
	}
	__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
_pc_daemon_daemon_on_bus_acquired_gbus_acquired_callback (GDBusConnection* connection,
                                                          const gchar* name,
                                                          gpointer self)
{
	pc_daemon_daemon_on_bus_acquired ((PCDaemonDaemon*) self, connection);
}

static void
__lambda14_ (PCDaemonDaemon* self)
{
}

static void
___lambda14__gbus_name_acquired_callback (GDBusConnection* connection,
                                          const gchar* name,
                                          gpointer self)
{
	__lambda14_ ((PCDaemonDaemon*) self);
}

static void
_pc_daemon_daemon_on_bus_lost_gbus_name_lost_callback (GDBusConnection* connection,
                                                       const gchar* name,
                                                       gpointer self)
{
	pc_daemon_daemon_on_bus_lost ((PCDaemonDaemon*) self, connection, name);
}

static void
pc_daemon_daemon_on_usermanager_loaded (PCDaemonDaemon* self)
{
	ActUserManager* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	PCDaemonSessionManager* _tmp3_;
	PCDaemonSessionManager* _tmp4_;
	g_return_if_fail (self != NULL);
	_tmp0_ = pc_utils_get_usermanager ();
	g_object_get (_tmp0_, "is-loaded", &_tmp1_, NULL);
	_tmp2_ = _tmp1_;
	if (!_tmp2_) {
		return;
	}
	g_bus_own_name_with_closures (G_BUS_TYPE_SYSTEM, PC_CONSTANTS_PARENTAL_CONTROLS_IFACE, G_BUS_NAME_OWNER_FLAGS_REPLACE, (GClosure*) ((_pc_daemon_daemon_on_bus_acquired_gbus_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) _pc_daemon_daemon_on_bus_acquired_gbus_acquired_callback, g_object_ref (self), (GClosureNotify) g_object_unref)), (GClosure*) ((___lambda14__gbus_name_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) ___lambda14__gbus_name_acquired_callback, g_object_ref (self), (GClosureNotify) g_object_unref)), (GClosure*) ((_pc_daemon_daemon_on_bus_lost_gbus_name_lost_callback == NULL) ? NULL : g_cclosure_new ((GCallback) _pc_daemon_daemon_on_bus_lost_gbus_name_lost_callback, g_object_ref (self), (GClosureNotify) g_object_unref)));
	_tmp3_ = pc_daemon_session_manager_get_default ();
	_g_object_unref0 (pc_daemon_daemon_session_manager);
	pc_daemon_daemon_session_manager = _tmp3_;
	_tmp4_ = pc_daemon_daemon_session_manager;
	pc_daemon_session_manager_start (_tmp4_);
}

PCDaemonDaemon*
pc_daemon_daemon_construct (GType object_type)
{
	PCDaemonDaemon * self = NULL;
	self = (PCDaemonDaemon*) g_object_new (object_type, NULL);
	return self;
}

PCDaemonDaemon*
pc_daemon_daemon_new (void)
{
	return pc_daemon_daemon_construct (PC_DAEMON_TYPE_DAEMON);
}

static void
pc_daemon_daemon_class_init (PCDaemonDaemonClass * klass,
                             gpointer klass_data)
{
	pc_daemon_daemon_parent_class = g_type_class_peek_parent (klass);
	((GApplicationClass *) klass)->activate = (void (*) (GApplication*)) pc_daemon_daemon_real_activate;
	G_OBJECT_CLASS (klass)->finalize = pc_daemon_daemon_finalize;
}

static void
pc_daemon_daemon_instance_init (PCDaemonDaemon * self,
                                gpointer klass)
{
}

static void
pc_daemon_daemon_finalize (GObject * obj)
{
	PCDaemonDaemon * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PC_DAEMON_TYPE_DAEMON, PCDaemonDaemon);
	G_OBJECT_CLASS (pc_daemon_daemon_parent_class)->finalize (obj);
}

static GType
pc_daemon_daemon_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PCDaemonDaemonClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pc_daemon_daemon_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PCDaemonDaemon), 0, (GInstanceInitFunc) pc_daemon_daemon_instance_init, NULL };
	GType pc_daemon_daemon_type_id;
	pc_daemon_daemon_type_id = g_type_register_static (g_application_get_type (), "PCDaemonDaemon", &g_define_type_info, 0);
	return pc_daemon_daemon_type_id;
}

GType
pc_daemon_daemon_get_type (void)
{
	static volatile gsize pc_daemon_daemon_type_id__once = 0;
	if (g_once_init_enter (&pc_daemon_daemon_type_id__once)) {
		GType pc_daemon_daemon_type_id;
		pc_daemon_daemon_type_id = pc_daemon_daemon_get_type_once ();
		g_once_init_leave (&pc_daemon_daemon_type_id__once, pc_daemon_daemon_type_id);
	}
	return pc_daemon_daemon_type_id__once;
}

