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

/*-
 * Copyright 2021 elementary, Inc. <https://elementary.io>
 *
 * 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 2 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-1335 USA.
 *
 * Authored by: Corentin Noël <corentin@elementary.io>
 */

#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#define BUILD_VERSION "8.5.0"
#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 SETTINGS_DAEMON_TYPE_SETTINGS (settings_daemon_settings_get_type ())
#define SETTINGS_DAEMON_SETTINGS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SETTINGS_DAEMON_TYPE_SETTINGS, SettingsDaemonSettings))
#define SETTINGS_DAEMON_SETTINGS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SETTINGS_DAEMON_TYPE_SETTINGS, SettingsDaemonSettingsClass))
#define SETTINGS_DAEMON_IS_SETTINGS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SETTINGS_DAEMON_TYPE_SETTINGS))
#define SETTINGS_DAEMON_IS_SETTINGS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SETTINGS_DAEMON_TYPE_SETTINGS))
#define SETTINGS_DAEMON_SETTINGS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SETTINGS_DAEMON_TYPE_SETTINGS, SettingsDaemonSettingsClass))

typedef struct _SettingsDaemonSettings SettingsDaemonSettings;
typedef struct _SettingsDaemonSettingsClass SettingsDaemonSettingsClass;
#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_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))

VALA_EXTERN gboolean opt_replace;
gboolean opt_replace = FALSE;
VALA_EXTERN gboolean show_version;
gboolean show_version = FALSE;
VALA_EXTERN GMainLoop* loop;
GMainLoop* loop = NULL;

VALA_EXTERN void on_bus_acquired (GDBusConnection* connection,
                      const gchar* name);
VALA_EXTERN SettingsDaemonSettings* settings_daemon_settings_new (void);
VALA_EXTERN SettingsDaemonSettings* settings_daemon_settings_construct (GType object_type);
VALA_EXTERN GType settings_daemon_settings_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SettingsDaemonSettings, g_object_unref)
VALA_EXTERN guint settings_daemon_settings_register_object (void* object,
                                                GDBusConnection* connection,
                                                const gchar* path,
                                                GError** error);
static gint _vala_main (gchar** args,
                 gint args_length1);
static void _on_bus_acquired_gbus_acquired_callback (GDBusConnection* connection,
                                              const gchar* name,
                                              gpointer self);
static void ___lambda10_ (void);
static void ____lambda10__gbus_name_acquired_callback (GDBusConnection* connection,
                                                const gchar* name,
                                                gpointer self);
static void ___lambda11_ (void);
static void ____lambda11__gbus_name_lost_callback (GDBusConnection* connection,
                                            const gchar* name,
                                            gpointer self);

const GOptionEntry ENTRIES[3] = {{"replace", 'r', (gint) 0, G_OPTION_ARG_NONE, &opt_replace, "Replace a running instance", NULL}, {"version", (gchar) 0, (gint) 0, G_OPTION_ARG_NONE, &show_version, "Show program version.", NULL}, {NULL}};

void
on_bus_acquired (GDBusConnection* connection,
                 const gchar* name)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (connection != NULL);
	g_return_if_fail (name != NULL);
	{
		SettingsDaemonSettings* _tmp0_;
		SettingsDaemonSettings* _tmp1_;
		_tmp0_ = settings_daemon_settings_new ();
		_tmp1_ = _tmp0_;
		settings_daemon_settings_register_object (_tmp1_, connection, "/org/freedesktop/portal/desktop", &_inner_error0_);
		_g_object_unref0 (_tmp1_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp2_;
		const gchar* _tmp3_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = e;
		_tmp3_ = _tmp2_->message;
		g_critical ("Main.vala:37: Unable to register the object: %s", _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
_on_bus_acquired_gbus_acquired_callback (GDBusConnection* connection,
                                         const gchar* name,
                                         gpointer self)
{
	on_bus_acquired (connection, name);
}

static void
___lambda10_ (void)
{
	g_debug ("Main.vala:68: org.freedesktop.impl.portal.desktop.elementary.settings " \
"acquired");
}

static void
____lambda10__gbus_name_acquired_callback (GDBusConnection* connection,
                                           const gchar* name,
                                           gpointer self)
{
	___lambda10_ ();
}

static void
___lambda11_ (void)
{
	GMainLoop* _tmp0_;
	_tmp0_ = loop;
	g_main_loop_quit (_tmp0_);
}

static void
____lambda11__gbus_name_lost_callback (GDBusConnection* connection,
                                       const gchar* name,
                                       gpointer self)
{
	___lambda11_ ();
}

static gint
_vala_main (gchar** args,
            gint args_length1)
{
	GOptionContext* context = NULL;
	GOptionContext* _tmp0_;
	GOptionContext* _tmp1_;
	GMainLoop* _tmp8_;
	GError* _inner_error0_ = NULL;
	gint result;
	_tmp0_ = g_option_context_new ("- Settings portal");
	context = _tmp0_;
	_tmp1_ = context;
	g_option_context_add_main_entries (_tmp1_, ENTRIES, NULL);
	{
		GOptionContext* _tmp2_;
		_tmp2_ = context;
		g_option_context_parse (_tmp2_, (gint*) (&args_length1), &args, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		const gchar* _tmp3_;
		GError* _tmp4_;
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = g_get_application_name ();
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_printerr ("%s: %s", _tmp3_, _tmp5_);
		g_printerr ("\n");
		_tmp6_ = g_get_prgname ();
		g_printerr ("Try \"%s --help\" for more information.", _tmp6_);
		g_printerr ("\n");
		result = 1;
		_g_error_free0 (e);
		_g_option_context_free0 (context);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gint _tmp7_ = -1;
		_g_option_context_free0 (context);
		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 _tmp7_;
	}
	if (show_version) {
		g_print ("%s \n", BUILD_VERSION);
		result = 0;
		_g_option_context_free0 (context);
		return result;
	}
	_tmp8_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (loop);
	loop = _tmp8_;
	{
		GDBusConnection* session_bus = NULL;
		GDBusConnection* _tmp9_;
		GBusNameOwnerFlags _tmp10_ = 0U;
		guint owner_id = 0U;
		GMainLoop* _tmp11_;
		_tmp9_ = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &_inner_error0_);
		session_bus = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch1_g_error;
		}
		if (opt_replace) {
			_tmp10_ = G_BUS_NAME_OWNER_FLAGS_REPLACE;
		} else {
			_tmp10_ = 0;
		}
		owner_id = g_bus_own_name_with_closures (G_BUS_TYPE_SESSION, "org.freedesktop.impl.portal.desktop.elementary.settings-daemon", G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | _tmp10_, (GClosure*) g_cclosure_new ((GCallback) _on_bus_acquired_gbus_acquired_callback, NULL, (GClosureNotify) NULL), (GClosure*) ((____lambda10__gbus_name_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) ____lambda10__gbus_name_acquired_callback, NULL, (GClosureNotify) NULL)), (GClosure*) ((____lambda11__gbus_name_lost_callback == NULL) ? NULL : g_cclosure_new ((GCallback) ____lambda11__gbus_name_lost_callback, NULL, (GClosureNotify) NULL)));
		_tmp11_ = loop;
		g_main_loop_run (_tmp11_);
		g_bus_unown_name (owner_id);
		_g_object_unref0 (session_bus);
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError* e = NULL;
		GError* _tmp12_;
		const gchar* _tmp13_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp12_ = e;
		_tmp13_ = _tmp12_->message;
		g_printerr ("No session bus: %s\n", _tmp13_);
		result = 2;
		_g_error_free0 (e);
		_g_option_context_free0 (context);
		return result;
	}
	__finally1:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gint _tmp14_ = -1;
		_g_option_context_free0 (context);
		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 _tmp14_;
	}
	result = 0;
	_g_option_context_free0 (context);
	return result;
}

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

