/* sent-sound.c generated by valac 0.56.3, the Vala compiler
 * generated from sent-sound.vala, do not modify */

/*
 * Copyright © 2020 Michael Gratton <mike@vee.net>.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later). See the COPYING file in this distribution.
 */

#include "sent-sound.h"
#include <glib-object.h>
#include <glib.h>
#include <libpeas/peas.h>
#include "geary-client-43.0.h"
#include <gsound.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include "geary-engine.h"

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	PLUGIN_SENT_SOUND_0_PROPERTY,
	PLUGIN_SENT_SOUND_EMAIL_PROPERTY,
	PLUGIN_SENT_SOUND_NUM_PROPERTIES
};
static GParamSpec* plugin_sent_sound_properties[PLUGIN_SENT_SOUND_NUM_PROPERTIES];
typedef struct _PluginSentSoundActivateData PluginSentSoundActivateData;
typedef struct _PluginSentSoundDeactivateData PluginSentSoundDeactivateData;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _PluginSentSoundPrivate {
	PluginEmailContext* _email;
	GSoundContext* context;
	PluginEmailStore* store;
};

struct _PluginSentSoundActivateData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	PluginSentSound* self;
	gboolean is_startup;
	GSoundContext* _tmp0_;
	GSoundContext* _tmp1_;
	GSoundContext* _tmp2_;
	GSoundContext* _tmp3_;
	PluginEmailStore* _tmp4_;
	PluginEmailContext* _tmp5_;
	PluginEmailContext* _tmp6_;
	PluginEmailStore* _tmp7_;
	PluginEmailStore* _tmp8_;
	PluginEmailStore* _tmp9_;
	GError* _inner_error0_;
};

struct _PluginSentSoundDeactivateData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	PluginSentSound* self;
	gboolean is_shutdown;
	PluginEmailStore* _tmp0_;
	guint _tmp1_;
};

static gint PluginSentSound_private_offset;
static gpointer plugin_sent_sound_parent_class = NULL;
static PluginEmailExtensionIface * plugin_sent_sound_plugin_email_extension_parent_iface = NULL;
static GType plugin_sent_sound_type_id = 0;

static void plugin_sent_sound_real_activate_data_free (gpointer _data);
static void plugin_sent_sound_real_activate (PluginPluginBase* base,
                                      gboolean is_startup,
                                      GAsyncReadyCallback _callback_,
                                      gpointer _user_data_);
static gboolean plugin_sent_sound_real_activate_co (PluginSentSoundActivateData* _data_);
static void plugin_sent_sound_activate_ready (GObject* source_object,
                                       GAsyncResult* _res_,
                                       gpointer _user_data_);
static void plugin_sent_sound_on_sent (PluginSentSound* self);
static void _plugin_sent_sound_on_sent_plugin_email_store_email_sent (PluginEmailStore* _sender,
                                                               PluginEmail* sent,
                                                               gpointer self);
static void plugin_sent_sound_play_sound (PluginSentSound* self);
static void plugin_sent_sound_real_deactivate_data_free (gpointer _data);
static void plugin_sent_sound_real_deactivate (PluginPluginBase* base,
                                        gboolean is_shutdown,
                                        GAsyncReadyCallback _callback_,
                                        gpointer _user_data_);
static gboolean plugin_sent_sound_real_deactivate_co (PluginSentSoundDeactivateData* _data_);
static void plugin_sent_sound_finalize (GObject * obj);
static void _vala_plugin_sent_sound_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec);
static void _vala_plugin_sent_sound_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec);

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

void
peas_register_types (GTypeModule* module)
{
	PeasObjectModule* obj = NULL;
	PeasObjectModule* _tmp0_;
	g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (module, g_type_module_get_type ()));
	plugin_sent_sound_register_type (module);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (module, peas_object_module_get_type ()) ? ((PeasObjectModule*) module) : NULL);
	obj = _tmp0_;
	peas_object_module_register_extension_type (obj, PLUGIN_TYPE_PLUGIN_BASE, PLUGIN_TYPE_SENT_SOUND);
	_g_object_unref0 (obj);
}

static inline gpointer
plugin_sent_sound_get_instance_private (PluginSentSound* self)
{
	return G_STRUCT_MEMBER_P (self, PluginSentSound_private_offset);
}

static PluginEmailContext*
plugin_sent_sound_real_get_email (PluginEmailExtension* base)
{
	PluginEmailContext* result;
	PluginSentSound* self;
	PluginEmailContext* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	_tmp0_ = self->priv->_email;
	result = _tmp0_;
	return result;
}

static void
plugin_sent_sound_real_set_email (PluginEmailExtension* base,
                                  PluginEmailContext* value)
{
	PluginSentSound* self;
	PluginEmailContext* old_value;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	old_value = plugin_sent_sound_real_get_email (base);
	if (old_value != value) {
		PluginEmailContext* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_email);
		self->priv->_email = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, plugin_sent_sound_properties[PLUGIN_SENT_SOUND_EMAIL_PROPERTY]);
	}
}

static void
plugin_sent_sound_real_activate_data_free (gpointer _data)
{
	PluginSentSoundActivateData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (PluginSentSoundActivateData, _data_);
}

static void
plugin_sent_sound_real_activate (PluginPluginBase* base,
                                 gboolean is_startup,
                                 GAsyncReadyCallback _callback_,
                                 gpointer _user_data_)
{
	PluginSentSound * self;
	PluginSentSoundActivateData* _data_;
	PluginSentSound* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	_data_ = g_slice_new0 (PluginSentSoundActivateData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, plugin_sent_sound_real_activate_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_data_->is_startup = is_startup;
	plugin_sent_sound_real_activate_co (_data_);
}

static void
plugin_sent_sound_activate_finish (PluginPluginBase* base,
                                   GAsyncResult* _res_,
                                   GError** error)
{
	PluginSentSoundActivateData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static void
plugin_sent_sound_activate_ready (GObject* source_object,
                                  GAsyncResult* _res_,
                                  gpointer _user_data_)
{
	PluginSentSoundActivateData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	plugin_sent_sound_real_activate_co (_data_);
}

static void
_plugin_sent_sound_on_sent_plugin_email_store_email_sent (PluginEmailStore* _sender,
                                                          PluginEmail* sent,
                                                          gpointer self)
{
	plugin_sent_sound_on_sent ((PluginSentSound*) self);
}

static gboolean
plugin_sent_sound_real_activate_co (PluginSentSoundActivateData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp1_ = gsound_context_new (NULL, &_data_->_inner_error0_);
	_data_->_tmp0_ = _data_->_tmp1_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp2_ = _data_->_tmp0_;
	_data_->_tmp0_ = NULL;
	_g_object_unref0 (_data_->self->priv->context);
	_data_->self->priv->context = _data_->_tmp2_;
	_data_->_tmp3_ = _data_->self->priv->context;
	g_initable_init (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp3_, g_initable_get_type (), GInitable), NULL, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_object_unref0 (_data_->_tmp0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp5_ = plugin_email_extension_get_email (G_TYPE_CHECK_INSTANCE_CAST (_data_->self, PLUGIN_TYPE_EMAIL_EXTENSION, PluginEmailExtension));
	_data_->_tmp6_ = _data_->_tmp5_;
	_data_->_state_ = 1;
	plugin_email_context_get_email_store (_data_->_tmp6_, plugin_sent_sound_activate_ready, _data_);
	return FALSE;
	_state_1:
	_data_->_tmp7_ = plugin_email_context_get_email_store_finish (_data_->_tmp6_, _data_->_res_, &_data_->_inner_error0_);
	_data_->_tmp4_ = _data_->_tmp7_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_object_unref0 (_data_->_tmp0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp8_ = _data_->_tmp4_;
	_data_->_tmp4_ = NULL;
	_g_object_unref0 (_data_->self->priv->store);
	_data_->self->priv->store = _data_->_tmp8_;
	_data_->_tmp9_ = _data_->self->priv->store;
	g_signal_connect_object (_data_->_tmp9_, "email-sent", (GCallback) _plugin_sent_sound_on_sent_plugin_email_store_email_sent, _data_->self, 0);
	if (!_data_->is_startup) {
		plugin_sent_sound_play_sound (_data_->self);
	}
	_g_object_unref0 (_data_->_tmp4_);
	_g_object_unref0 (_data_->_tmp0_);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
plugin_sent_sound_real_deactivate_data_free (gpointer _data)
{
	PluginSentSoundDeactivateData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (PluginSentSoundDeactivateData, _data_);
}

static void
plugin_sent_sound_real_deactivate (PluginPluginBase* base,
                                   gboolean is_shutdown,
                                   GAsyncReadyCallback _callback_,
                                   gpointer _user_data_)
{
	PluginSentSound * self;
	PluginSentSoundDeactivateData* _data_;
	PluginSentSound* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	_data_ = g_slice_new0 (PluginSentSoundDeactivateData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, plugin_sent_sound_real_deactivate_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_data_->is_shutdown = is_shutdown;
	plugin_sent_sound_real_deactivate_co (_data_);
}

static void
plugin_sent_sound_deactivate_finish (PluginPluginBase* base,
                                     GAsyncResult* _res_,
                                     GError** error)
{
	PluginSentSoundDeactivateData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static gboolean
plugin_sent_sound_real_deactivate_co (PluginSentSoundDeactivateData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = _data_->self->priv->store;
	g_signal_parse_name ("email-sent", PLUGIN_TYPE_EMAIL_STORE, &_data_->_tmp1_, NULL, FALSE);
	g_signal_handlers_disconnect_matched (_data_->_tmp0_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _data_->_tmp1_, 0, NULL, (GCallback) _plugin_sent_sound_on_sent_plugin_email_store_email_sent, _data_->self);
	_g_object_unref0 (_data_->self->priv->store);
	_data_->self->priv->store = NULL;
	_g_object_unref0 (_data_->self->priv->context);
	_data_->self->priv->context = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
plugin_sent_sound_play_sound (PluginSentSound* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (PLUGIN_IS_SENT_SOUND (self));
	{
		GSoundContext* _tmp0_;
		_tmp0_ = self->priv->context;
		gsound_context_play_simple (_tmp0_, NULL, &_inner_error0_, GSOUND_ATTR_EVENT_ID, "message-sent-email", NULL);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		GError* _tmp1_;
		const gchar* _tmp2_;
		PluginApplication* _tmp3_;
		PluginApplication* _tmp4_;
		GError* _tmp5_;
		GearyProblemReport* _tmp6_;
		GearyProblemReport* _tmp7_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp1_ = err;
		_tmp2_ = _tmp1_->message;
		g_warning ("sent-sound.vala:55: Failed to play sent mail sound: %s", _tmp2_);
		_tmp3_ = plugin_plugin_base_get_plugin_application (G_TYPE_CHECK_INSTANCE_CAST (self, PLUGIN_TYPE_PLUGIN_BASE, PluginPluginBase));
		_tmp4_ = _tmp3_;
		_tmp5_ = err;
		_tmp6_ = geary_problem_report_new (_tmp5_);
		_tmp7_ = _tmp6_;
		plugin_application_report_problem (_tmp4_, _tmp7_);
		_g_object_unref0 (_tmp7_);
		_g_error_free0 (err);
	}
	__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
plugin_sent_sound_on_sent (PluginSentSound* self)
{
	g_return_if_fail (PLUGIN_IS_SENT_SOUND (self));
	plugin_sent_sound_play_sound (self);
}

PluginSentSound*
plugin_sent_sound_construct (GType object_type)
{
	PluginSentSound * self = NULL;
	self = (PluginSentSound*) plugin_plugin_base_construct (object_type);
	return self;
}

PluginSentSound*
plugin_sent_sound_new (void)
{
	return plugin_sent_sound_construct (PLUGIN_TYPE_SENT_SOUND);
}

static void
plugin_sent_sound_class_init (PluginSentSoundClass * klass,
                              gpointer klass_data)
{
	plugin_sent_sound_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PluginSentSound_private_offset);
	((PluginPluginBaseClass *) klass)->activate = (void (*) (PluginPluginBase*, gboolean, GAsyncReadyCallback, gpointer)) plugin_sent_sound_real_activate;
	((PluginPluginBaseClass *) klass)->activate_finish = (void (*) (PluginPluginBase*, GAsyncResult*, GError**)) plugin_sent_sound_activate_finish;
	((PluginPluginBaseClass *) klass)->deactivate = (void (*) (PluginPluginBase*, gboolean, GAsyncReadyCallback, gpointer)) plugin_sent_sound_real_deactivate;
	((PluginPluginBaseClass *) klass)->deactivate_finish = (void (*) (PluginPluginBase*, GAsyncResult*, GError**)) plugin_sent_sound_deactivate_finish;
	G_OBJECT_CLASS (klass)->get_property = _vala_plugin_sent_sound_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_plugin_sent_sound_set_property;
	G_OBJECT_CLASS (klass)->finalize = plugin_sent_sound_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), PLUGIN_SENT_SOUND_EMAIL_PROPERTY, plugin_sent_sound_properties[PLUGIN_SENT_SOUND_EMAIL_PROPERTY] = g_param_spec_object ("email", "email", "email", PLUGIN_TYPE_EMAIL_CONTEXT, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
plugin_sent_sound_plugin_email_extension_interface_init (PluginEmailExtensionIface * iface,
                                                         gpointer iface_data)
{
	plugin_sent_sound_plugin_email_extension_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_email = plugin_sent_sound_real_get_email;
	iface->set_email = plugin_sent_sound_real_set_email;
}

static void
plugin_sent_sound_instance_init (PluginSentSound * self,
                                 gpointer klass)
{
	self->priv = plugin_sent_sound_get_instance_private (self);
	self->priv->context = NULL;
	self->priv->store = NULL;
}

static void
plugin_sent_sound_finalize (GObject * obj)
{
	PluginSentSound * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	_g_object_unref0 (self->priv->_email);
	_g_object_unref0 (self->priv->context);
	_g_object_unref0 (self->priv->store);
	G_OBJECT_CLASS (plugin_sent_sound_parent_class)->finalize (obj);
}

/** Plays the desktop sent-mail sound when an email is sent. */
GType
plugin_sent_sound_get_type (void)
{
	return plugin_sent_sound_type_id;
}

GType
plugin_sent_sound_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (PluginSentSoundClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) plugin_sent_sound_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PluginSentSound), 0, (GInstanceInitFunc) plugin_sent_sound_instance_init, NULL };
	static const GInterfaceInfo plugin_email_extension_info = { (GInterfaceInitFunc) plugin_sent_sound_plugin_email_extension_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	plugin_sent_sound_type_id = g_type_module_register_type (module, PLUGIN_TYPE_PLUGIN_BASE, "PluginSentSound", &g_define_type_info, 0);
	g_type_module_add_interface (module, plugin_sent_sound_type_id, PLUGIN_TYPE_EMAIL_EXTENSION, &plugin_email_extension_info);
	PluginSentSound_private_offset = sizeof (PluginSentSoundPrivate);
	return plugin_sent_sound_type_id;
}

static void
_vala_plugin_sent_sound_get_property (GObject * object,
                                      guint property_id,
                                      GValue * value,
                                      GParamSpec * pspec)
{
	PluginSentSound * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	switch (property_id) {
		case PLUGIN_SENT_SOUND_EMAIL_PROPERTY:
		g_value_set_object (value, plugin_email_extension_get_email (G_TYPE_CHECK_INSTANCE_CAST (self, PLUGIN_TYPE_EMAIL_EXTENSION, PluginEmailExtension)));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_plugin_sent_sound_set_property (GObject * object,
                                      guint property_id,
                                      const GValue * value,
                                      GParamSpec * pspec)
{
	PluginSentSound * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, PLUGIN_TYPE_SENT_SOUND, PluginSentSound);
	switch (property_id) {
		case PLUGIN_SENT_SOUND_EMAIL_PROPERTY:
		plugin_email_extension_set_email (G_TYPE_CHECK_INSTANCE_CAST (self, PLUGIN_TYPE_EMAIL_EXTENSION, PluginEmailExtension), g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

