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

/*-
 * Copyright (c) 2021 elementary, Inc. (https://elementary.io)
 *
 * 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/>.
*/

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include "pantheon-files-send-by-email.h"
#include <gio/gunixfdlist.h>

#define PORTAL_DBUS_DESKTOP_PATH "/org/freedesktop/portal/desktop"
#define PORTAL_DBUS_DESKTOP_NAME "org.freedesktop.portal.Desktop"
#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

#define PORTAL_TYPE_EMAIL (portal_email_get_type ())
#define PORTAL_EMAIL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PORTAL_TYPE_EMAIL, PortalEmail))
#define PORTAL_IS_EMAIL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PORTAL_TYPE_EMAIL))
#define PORTAL_EMAIL_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), PORTAL_TYPE_EMAIL, PortalEmailIface))

typedef struct _PortalEmail PortalEmail;
typedef struct _PortalEmailIface PortalEmailIface;

#define PORTAL_TYPE_EMAIL_PROXY (portal_email_proxy_get_type ())
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block4Data Block4Data;
#define _g_variant_builder_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_builder_unref (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
typedef GDBusProxy PortalEmailProxy;
typedef GDBusProxyClass PortalEmailProxyClass;

struct _PortalEmailIface {
	GTypeInterface parent_iface;
	guint (*get_version) (PortalEmail* self);
};

struct _Block4Data {
	int _ref_count_;
	PortalEmail* self;
	GVariantBuilder* options_builder;
};

VALA_EXTERN PortalEmail* portal_email;
PortalEmail* portal_email = NULL;

VALA_EXTERN GType portal_email_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType portal_email_proxy_get_type (void) G_GNUC_CONST ;
VALA_EXTERN guint portal_email_register_object (void* object,
                                    GDBusConnection* connection,
                                    const gchar* path,
                                    GError** error);
VALA_EXTERN PortalEmail* portal_email_get (GError** error);
VALA_EXTERN char* portal_email_compose_email (PortalEmail* self,
                                  const gchar* window_handle,
                                  GHashTable* options,
                                  GUnixFDList* attachments,
                                  GError** error);
static Block4Data* block4_data_ref (Block4Data* _data4_);
static void block4_data_unref (void * _userdata_);
static void __lambda4_ (Block4Data* _data4_,
                 const gchar* key,
                 GVariant* val);
static void ___lambda4__gh_func (gconstpointer key,
                          gconstpointer value,
                          gpointer self);
VALA_EXTERN guint portal_email_get_version (PortalEmail* self);
static GType portal_email_get_type_once (void);
static void portal_email_proxy_g_signal (GDBusProxy* proxy,
                                  const gchar* sender_name,
                                  const gchar* signal_name,
                                  GVariant* parameters);
static guint portal_email_dbus_proxy_get_version (PortalEmail* self);
static void portal_email_proxy_portal_email_interface_init (PortalEmailIface* iface);
static void portal_email_dbus_interface_method_call (GDBusConnection* connection,
                                              const gchar* sender,
                                              const gchar* object_path,
                                              const gchar* interface_name,
                                              const gchar* method_name,
                                              GVariant* parameters,
                                              GDBusMethodInvocation* invocation,
                                              gpointer user_data);
static GVariant* portal_email_dbus_interface_get_property (GDBusConnection* connection,
                                                    const gchar* sender,
                                                    const gchar* object_path,
                                                    const gchar* interface_name,
                                                    const gchar* property_name,
                                                    GError** error,
                                                    gpointer user_data);
static GVariant* _dbus_portal_email_get_version (PortalEmail* self);
static gboolean portal_email_dbus_interface_set_property (GDBusConnection* connection,
                                                   const gchar* sender,
                                                   const gchar* object_path,
                                                   const gchar* interface_name,
                                                   const gchar* property_name,
                                                   GVariant* value,
                                                   GError** error,
                                                   gpointer user_data);
static void _portal_email_unregister_object (gpointer user_data);

static const GDBusMethodInfo * const _portal_email_dbus_method_info[] = {NULL};
static const GDBusSignalInfo * const _portal_email_dbus_signal_info[] = {NULL};
static const GDBusPropertyInfo _portal_email_dbus_property_info_version = {-1, "version", "u", G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL};
static const GDBusPropertyInfo * const _portal_email_dbus_property_info[] = {&_portal_email_dbus_property_info_version, NULL};
static const GDBusInterfaceInfo _portal_email_dbus_interface_info = {-1, "org.freedesktop.portal.Email", (GDBusMethodInfo **) (&_portal_email_dbus_method_info), (GDBusSignalInfo **) (&_portal_email_dbus_signal_info), (GDBusPropertyInfo **) (&_portal_email_dbus_property_info), NULL};
static const GDBusInterfaceVTable _portal_email_dbus_interface_vtable = {portal_email_dbus_interface_method_call, portal_email_dbus_interface_get_property, portal_email_dbus_interface_set_property};

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_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 NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_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 NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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 NULL;
}

gchar*
portal_generate_token (void)
{
	GApplication* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* result;
	_tmp0_ = g_application_get_default ();
	_tmp1_ = g_application_get_application_id (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = string_replace (_tmp2_, ".", "_");
	_tmp4_ = _tmp3_;
	_tmp5_ = g_strdup_printf ("%s_%i", _tmp4_, (gint) g_random_int_range ((gint32) 0, G_MAXINT32));
	_tmp6_ = _tmp5_;
	_g_free0 (_tmp4_);
	result = _tmp6_;
	return result;
}

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

PortalEmail*
portal_email_get (GError** error)
{
	PortalEmail* _tmp0_;
	PortalEmail* _tmp8_;
	PortalEmail* _tmp9_;
	GError* _inner_error0_ = NULL;
	PortalEmail* result;
	_tmp0_ = portal_email;
	if (_tmp0_ == NULL) {
		GDBusConnection* connection = NULL;
		GApplication* _tmp1_;
		GDBusConnection* _tmp2_;
		GDBusConnection* _tmp3_;
		PortalEmail* _tmp4_ = NULL;
		GDBusConnection* _tmp5_;
		PortalEmail* _tmp6_;
		PortalEmail* _tmp7_;
		_tmp1_ = g_application_get_default ();
		_tmp2_ = g_application_get_dbus_connection (_tmp1_);
		_tmp3_ = _g_object_ref0 (_tmp2_);
		connection = _tmp3_;
		_tmp5_ = connection;
		_tmp6_ = (PortalEmail*) g_initable_new (PORTAL_TYPE_EMAIL_PROXY, NULL, &_inner_error0_, "g-flags", 0, "g-name", PORTAL_DBUS_DESKTOP_NAME, "g-connection", _tmp5_, "g-object-path", PORTAL_DBUS_DESKTOP_PATH, "g-interface-name", "org.freedesktop.portal.Email", NULL);
		_tmp4_ = (PortalEmail*) _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if ((_inner_error0_->domain == G_IO_ERROR) || (_inner_error0_->domain == G_DBUS_ERROR)) {
				g_propagate_error (error, _inner_error0_);
				_g_object_unref0 (connection);
				return NULL;
			} else {
				_g_object_unref0 (connection);
				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 NULL;
			}
		}
		_tmp7_ = _tmp4_;
		_tmp4_ = NULL;
		_g_object_unref0 (portal_email);
		portal_email = _tmp7_;
		_g_object_unref0 (_tmp4_);
		_g_object_unref0 (connection);
	}
	_tmp8_ = portal_email;
	_tmp9_ = _g_object_ref0 (_tmp8_);
	result = _tmp9_;
	return result;
}

static Block4Data*
block4_data_ref (Block4Data* _data4_)
{
	g_atomic_int_inc (&_data4_->_ref_count_);
	return _data4_;
}

static void
block4_data_unref (void * _userdata_)
{
	Block4Data* _data4_;
	_data4_ = (Block4Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data4_->_ref_count_)) {
		PortalEmail* self;
		self = _data4_->self;
		_g_variant_builder_unref0 (_data4_->options_builder);
		_g_object_unref0 (self);
		g_slice_free (Block4Data, _data4_);
	}
}

static void
__lambda4_ (Block4Data* _data4_,
            const gchar* key,
            GVariant* val)
{
	PortalEmail* self;
	self = _data4_->self;
	g_return_if_fail (key != NULL);
	g_return_if_fail (val != NULL);
	g_variant_builder_add (_data4_->options_builder, "{sv}", key, val, NULL);
}

static void
___lambda4__gh_func (gconstpointer key,
                     gconstpointer value,
                     gpointer self)
{
	__lambda4_ (self, (const gchar*) key, (GVariant*) value);
}

char*
portal_email_compose_email (PortalEmail* self,
                            const gchar* window_handle,
                            GHashTable* options,
                            GUnixFDList* attachments,
                            GError** error)
{
	Block4Data* _data4_;
	const GVariantType* _tmp0_;
	GVariantBuilder* _tmp1_;
	GVariant* response = NULL;
	GVariant* _tmp2_;
	GVariant* _tmp3_;
	GVariant* _tmp4_;
	GVariant* _tmp5_;
	GVariant* _tmp6_;
	GVariant* _tmp7_;
	const gchar* _tmp8_;
	char* _tmp9_;
	char* _tmp10_;
	GError* _inner_error0_ = NULL;
	char* result;
	g_return_val_if_fail (window_handle != NULL, NULL);
	g_return_val_if_fail (options != NULL, NULL);
	_data4_ = g_slice_new0 (Block4Data);
	_data4_->_ref_count_ = 1;
	_data4_->self = g_object_ref (self);
	_tmp0_ = G_VARIANT_TYPE_VARDICT;
	_tmp1_ = g_variant_builder_new (_tmp0_);
	_data4_->options_builder = _tmp1_;
	g_hash_table_foreach (options, ___lambda4__gh_func, _data4_);
	_tmp2_ = g_variant_new ("(sa{sv})", window_handle, _data4_->options_builder, NULL);
	g_variant_ref_sink (_tmp2_);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_dbus_proxy_call_with_unix_fd_list_sync ((GDBusProxy*) self, "ComposeEmail", _tmp3_, G_DBUS_CALL_FLAGS_NONE, -1, attachments, NULL, NULL, &_inner_error0_);
	_tmp5_ = _tmp4_;
	_g_variant_unref0 (_tmp3_);
	response = _tmp5_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		block4_data_unref (_data4_);
		_data4_ = NULL;
		return NULL;
	}
	_tmp6_ = g_variant_get_child_value (response, (gsize) 0);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_variant_get_string (_tmp7_, NULL);
	_tmp9_ = g_strdup ((const char*) _tmp8_);
	_tmp10_ = _tmp9_;
	_g_variant_unref0 (_tmp7_);
	result = _tmp10_;
	_g_variant_unref0 (response);
	block4_data_unref (_data4_);
	_data4_ = NULL;
	return result;
}

guint
portal_email_get_version (PortalEmail* self)
{
	PortalEmailIface* _iface_;
	g_return_val_if_fail (self != NULL, 0U);
	_iface_ = PORTAL_EMAIL_GET_INTERFACE (self);
	if (_iface_->get_version) {
		return _iface_->get_version (self);
	}
	return 0U;
}

static void
portal_email_default_init (PortalEmailIface * iface,
                           gpointer iface_data)
{
}

 G_GNUC_NO_INLINE static GType
portal_email_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PortalEmailIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) portal_email_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType portal_email_type_id;
	portal_email_type_id = g_type_register_static (G_TYPE_INTERFACE, "PortalEmail", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (portal_email_type_id, g_dbus_proxy_get_type ());
	g_type_set_qdata (portal_email_type_id, g_quark_from_static_string ("vala-dbus-proxy-type"), (void*) portal_email_proxy_get_type);
	g_type_set_qdata (portal_email_type_id, g_quark_from_static_string ("vala-dbus-interface-name"), "org.freedesktop.portal.Email");
	g_type_set_qdata (portal_email_type_id, g_quark_from_static_string ("vala-dbus-interface-info"), (void*) (&_portal_email_dbus_interface_info));
	g_type_set_qdata (portal_email_type_id, g_quark_from_static_string ("vala-dbus-register-object"), (void*) portal_email_register_object);
	return portal_email_type_id;
}

GType
portal_email_get_type (void)
{
	static volatile gsize portal_email_type_id__once = 0;
	if (g_once_init_enter (&portal_email_type_id__once)) {
		GType portal_email_type_id;
		portal_email_type_id = portal_email_get_type_once ();
		g_once_init_leave (&portal_email_type_id__once, portal_email_type_id);
	}
	return portal_email_type_id__once;
}

G_DEFINE_TYPE_EXTENDED (PortalEmailProxy, portal_email_proxy, G_TYPE_DBUS_PROXY, 0, G_IMPLEMENT_INTERFACE (PORTAL_TYPE_EMAIL, portal_email_proxy_portal_email_interface_init) )
static void
portal_email_proxy_class_init (PortalEmailProxyClass* klass)
{
	G_DBUS_PROXY_CLASS (klass)->g_signal = portal_email_proxy_g_signal;
}

static void
portal_email_proxy_g_signal (GDBusProxy* proxy,
                             const gchar* sender_name,
                             const gchar* signal_name,
                             GVariant* parameters)
{
}

static void
portal_email_proxy_init (PortalEmailProxy* self)
{
	g_dbus_proxy_set_interface_info (G_DBUS_PROXY (self), (GDBusInterfaceInfo *) (&_portal_email_dbus_interface_info));
}

static guint
portal_email_dbus_proxy_get_version (PortalEmail* self)
{
	GVariant *_inner_reply;
	guint _result;
	_inner_reply = g_dbus_proxy_get_cached_property ((GDBusProxy *) self, "version");
	if (!_inner_reply) {
		GVariant *_arguments;
		GVariant *_reply;
		GVariantBuilder _arguments_builder;
		g_variant_builder_init (&_arguments_builder, G_VARIANT_TYPE_TUPLE);
		g_variant_builder_add_value (&_arguments_builder, g_variant_new_string ("org.freedesktop.portal.Email"));
		g_variant_builder_add_value (&_arguments_builder, g_variant_new_string ("version"));
		_arguments = g_variant_builder_end (&_arguments_builder);
		_reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Get", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
		if (!_reply) {
			guint _tmp0_ = 0U;
			return _tmp0_;
		}
		g_variant_get (_reply, "(v)", &_inner_reply);
		g_variant_unref (_reply);
	}
	_result = g_variant_get_uint32 (_inner_reply);
	g_variant_unref (_inner_reply);
	return _result;
}

static void
portal_email_proxy_portal_email_interface_init (PortalEmailIface* iface)
{
	iface->get_version = portal_email_dbus_proxy_get_version;
}

static void
portal_email_dbus_interface_method_call (GDBusConnection* connection,
                                         const gchar* sender,
                                         const gchar* object_path,
                                         const gchar* interface_name,
                                         const gchar* method_name,
                                         GVariant* parameters,
                                         GDBusMethodInvocation* invocation,
                                         gpointer user_data)
{
	g_object_unref (invocation);
}

static GVariant*
_dbus_portal_email_get_version (PortalEmail* self)
{
	guint result;
	GVariant* _reply;
	result = portal_email_get_version (self);
	_reply = g_variant_new_uint32 (result);
	return _reply;
}

static GVariant*
portal_email_dbus_interface_get_property (GDBusConnection* connection,
                                          const gchar* sender,
                                          const gchar* object_path,
                                          const gchar* interface_name,
                                          const gchar* property_name,
                                          GError** error,
                                          gpointer user_data)
{
	gpointer* data;
	gpointer object;
	data = user_data;
	object = data[0];
	if (strcmp (property_name, "version") == 0) {
		return _dbus_portal_email_get_version (object);
	}
	return NULL;
}

static gboolean
portal_email_dbus_interface_set_property (GDBusConnection* connection,
                                          const gchar* sender,
                                          const gchar* object_path,
                                          const gchar* interface_name,
                                          const gchar* property_name,
                                          GVariant* value,
                                          GError** error,
                                          gpointer user_data)
{
	return FALSE;
}

guint
portal_email_register_object (gpointer object,
                              GDBusConnection* connection,
                              const gchar* path,
                              GError** error)
{
	guint result;
	gpointer *data;
	data = g_new (gpointer, 3);
	data[0] = g_object_ref (object);
	data[1] = g_object_ref (connection);
	data[2] = g_strdup (path);
	result = g_dbus_connection_register_object (connection, path, (GDBusInterfaceInfo *) (&_portal_email_dbus_interface_info), &_portal_email_dbus_interface_vtable, data, _portal_email_unregister_object, error);
	if (!result) {
		return 0;
	}
	return result;
}

static void
_portal_email_unregister_object (gpointer user_data)
{
	gpointer* data;
	data = user_data;
	g_object_unref (data[0]);
	g_object_unref (data[1]);
	g_free (data[2]);
	g_free (data);
}

