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

/*
* Copyright 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/

#include "onlineaccounts.h"
#include <gio/gio.h>
#include <libedataserver/libedataserver.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.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

enum  {
	ONLINE_ACCOUNTS_ACCOUNTS_MODEL_0_PROPERTY,
	ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY,
	ONLINE_ACCOUNTS_ACCOUNTS_MODEL_NUM_PROPERTIES
};
static GParamSpec* online_accounts_accounts_model_properties[ONLINE_ACCOUNTS_ACCOUNTS_MODEL_NUM_PROPERTIES];
#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)))
typedef struct _OnlineAccountsAccountsModelInitRegistryData OnlineAccountsAccountsModelInitRegistryData;

struct _OnlineAccountsAccountsModelPrivate {
	GListStore* _accounts_liststore;
	ESourceRegistryWatcher* collection_extension_watcher;
	ESourceRegistryWatcher* mail_account_extension_watcher;
};

struct _OnlineAccountsAccountsModelInitRegistryData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	OnlineAccountsAccountsModel* self;
	ESourceRegistry* registry;
	ESourceRegistry* _tmp0_;
	ESourceRegistryWatcher* _tmp1_;
	ESourceRegistryWatcher* _tmp2_;
	ESourceRegistryWatcher* _tmp3_;
	ESourceRegistryWatcher* _tmp4_;
	ESourceRegistryWatcher* _tmp5_;
	ESourceRegistryWatcher* _tmp6_;
	ESourceRegistryWatcher* _tmp7_;
	ESourceRegistryWatcher* _tmp8_;
	GError* e;
	GError* _tmp9_;
	const gchar* _tmp10_;
	GError* _inner_error0_;
};

static gint OnlineAccountsAccountsModel_private_offset;
static gpointer online_accounts_accounts_model_parent_class = NULL;

static void online_accounts_accounts_model_init_registry_data_free (gpointer _data);
static void online_accounts_accounts_model_init_registry (OnlineAccountsAccountsModel* self,
                                                   GAsyncReadyCallback _callback_,
                                                   gpointer _user_data_);
static void online_accounts_accounts_model_init_registry_finish (OnlineAccountsAccountsModel* self,
                                                          GAsyncResult* _res_);
static gboolean online_accounts_accounts_model_init_registry_co (OnlineAccountsAccountsModelInitRegistryData* _data_);
static void online_accounts_accounts_model_init_registry_ready (GObject* source_object,
                                                         GAsyncResult* _res_,
                                                         gpointer _user_data_);
static void online_accounts_accounts_model_add_esource (OnlineAccountsAccountsModel* self,
                                                 ESource* e_source);
static void _online_accounts_accounts_model_add_esource_e_source_registry_watcher_appeared (ESourceRegistryWatcher* _sender,
                                                                                     ESource* source,
                                                                                     gpointer self);
static void online_accounts_accounts_model_remove_esource (OnlineAccountsAccountsModel* self,
                                                    ESource* e_source);
static void _online_accounts_accounts_model_remove_esource_e_source_registry_watcher_disappeared (ESourceRegistryWatcher* _sender,
                                                                                           ESource* source,
                                                                                           gpointer self);
static void online_accounts_accounts_model_set_accounts_liststore (OnlineAccountsAccountsModel* self,
                                                            GListStore* value);
static GObject * online_accounts_accounts_model_constructor (GType type,
                                                      guint n_construct_properties,
                                                      GObjectConstructParam * construct_properties);
static void online_accounts_accounts_model_finalize (GObject * obj);
static GType online_accounts_accounts_model_get_type_once (void);
static void _vala_online_accounts_accounts_model_get_property (GObject * object,
                                                        guint property_id,
                                                        GValue * value,
                                                        GParamSpec * pspec);
static void _vala_online_accounts_accounts_model_set_property (GObject * object,
                                                        guint property_id,
                                                        const GValue * value,
                                                        GParamSpec * pspec);

static inline gpointer
online_accounts_accounts_model_get_instance_private (OnlineAccountsAccountsModel* self)
{
	return G_STRUCT_MEMBER_P (self, OnlineAccountsAccountsModel_private_offset);
}

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

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

static void
online_accounts_accounts_model_init_registry (OnlineAccountsAccountsModel* self,
                                              GAsyncReadyCallback _callback_,
                                              gpointer _user_data_)
{
	OnlineAccountsAccountsModelInitRegistryData* _data_;
	OnlineAccountsAccountsModel* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (OnlineAccountsAccountsModelInitRegistryData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, online_accounts_accounts_model_init_registry_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	online_accounts_accounts_model_init_registry_co (_data_);
}

static void
online_accounts_accounts_model_init_registry_finish (OnlineAccountsAccountsModel* self,
                                                     GAsyncResult* _res_)
{
	OnlineAccountsAccountsModelInitRegistryData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
online_accounts_accounts_model_init_registry_ready (GObject* source_object,
                                                    GAsyncResult* _res_,
                                                    gpointer _user_data_)
{
	OnlineAccountsAccountsModelInitRegistryData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	online_accounts_accounts_model_init_registry_co (_data_);
}

static void
_online_accounts_accounts_model_add_esource_e_source_registry_watcher_appeared (ESourceRegistryWatcher* _sender,
                                                                                ESource* source,
                                                                                gpointer self)
{
	online_accounts_accounts_model_add_esource ((OnlineAccountsAccountsModel*) self, source);
}

static void
_online_accounts_accounts_model_remove_esource_e_source_registry_watcher_disappeared (ESourceRegistryWatcher* _sender,
                                                                                      ESource* source,
                                                                                      gpointer self)
{
	online_accounts_accounts_model_remove_esource ((OnlineAccountsAccountsModel*) self, source);
}

static gboolean
online_accounts_accounts_model_init_registry_co (OnlineAccountsAccountsModelInitRegistryData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_state_ = 1;
		e_source_registry_new (NULL, online_accounts_accounts_model_init_registry_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp0_ = e_source_registry_new_finish (_data_->_res_, &_data_->_inner_error0_);
		_data_->registry = _data_->_tmp0_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp1_ = e_source_registry_watcher_new (_data_->registry, E_SOURCE_EXTENSION_COLLECTION);
		_g_object_unref0 (_data_->self->priv->collection_extension_watcher);
		_data_->self->priv->collection_extension_watcher = _data_->_tmp1_;
		_data_->_tmp2_ = _data_->self->priv->collection_extension_watcher;
		g_signal_connect_object (_data_->_tmp2_, "appeared", (GCallback) _online_accounts_accounts_model_add_esource_e_source_registry_watcher_appeared, _data_->self, 0);
		_data_->_tmp3_ = _data_->self->priv->collection_extension_watcher;
		g_signal_connect_object (_data_->_tmp3_, "disappeared", (GCallback) _online_accounts_accounts_model_remove_esource_e_source_registry_watcher_disappeared, _data_->self, 0);
		_data_->_tmp4_ = _data_->self->priv->collection_extension_watcher;
		e_source_registry_watcher_reclaim (_data_->_tmp4_);
		_data_->_tmp5_ = e_source_registry_watcher_new (_data_->registry, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
		_g_object_unref0 (_data_->self->priv->mail_account_extension_watcher);
		_data_->self->priv->mail_account_extension_watcher = _data_->_tmp5_;
		_data_->_tmp6_ = _data_->self->priv->mail_account_extension_watcher;
		g_signal_connect_object (_data_->_tmp6_, "appeared", (GCallback) _online_accounts_accounts_model_add_esource_e_source_registry_watcher_appeared, _data_->self, 0);
		_data_->_tmp7_ = _data_->self->priv->mail_account_extension_watcher;
		g_signal_connect_object (_data_->_tmp7_, "disappeared", (GCallback) _online_accounts_accounts_model_remove_esource_e_source_registry_watcher_disappeared, _data_->self, 0);
		_data_->_tmp8_ = _data_->self->priv->mail_account_extension_watcher;
		e_source_registry_watcher_reclaim (_data_->_tmp8_);
		_g_object_unref0 (_data_->registry);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp9_ = _data_->e;
		_data_->_tmp10_ = _data_->_tmp9_->message;
		g_critical ("AccountsModel.vala:47: %s", _data_->_tmp10_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	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
online_accounts_accounts_model_add_esource (OnlineAccountsAccountsModel* self,
                                            ESource* e_source)
{
	guint position = 0U;
	GListStore* _tmp0_;
	guint _tmp1_ = 0U;
	gboolean _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	GListStore* _tmp17_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (e_source != NULL);
	_tmp0_ = self->priv->_accounts_liststore;
	_tmp2_ = g_list_store_find (_tmp0_, (GObject*) e_source, &_tmp1_);
	position = _tmp1_;
	if (_tmp2_) {
		return;
	}
	_tmp3_ = e_source_get_parent (e_source);
	_tmp4_ = _tmp3_;
	if (_tmp4_ != NULL) {
		return;
	}
	if (e_source_has_extension (e_source, E_SOURCE_EXTENSION_MAIL_ACCOUNT)) {
		ESourceMailAccount* mail_source = NULL;
		ESourceExtension* _tmp5_;
		gboolean _tmp6_ = FALSE;
		gboolean _tmp7_ = FALSE;
		ESourceMailAccount* _tmp8_;
		const gchar* _tmp9_;
		const gchar* _tmp10_;
		_tmp5_ = e_source_get_extension (e_source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
		mail_source = G_TYPE_CHECK_INSTANCE_CAST (_tmp5_, e_source_mail_account_get_type (), ESourceMailAccount);
		_tmp8_ = mail_source;
		_tmp9_ = e_source_backend_get_backend_name ((ESourceBackend*) _tmp8_);
		_tmp10_ = _tmp9_;
		if (g_strcmp0 (_tmp10_, "vfolder") == 0) {
			_tmp7_ = TRUE;
		} else {
			ESourceMailAccount* _tmp11_;
			const gchar* _tmp12_;
			const gchar* _tmp13_;
			_tmp11_ = mail_source;
			_tmp12_ = e_source_backend_get_backend_name ((ESourceBackend*) _tmp11_);
			_tmp13_ = _tmp12_;
			_tmp7_ = g_strcmp0 (_tmp13_, "maildir") == 0;
		}
		if (_tmp7_) {
			_tmp6_ = TRUE;
		} else {
			ESourceMailAccount* _tmp14_;
			const gchar* _tmp15_;
			const gchar* _tmp16_;
			_tmp14_ = mail_source;
			_tmp15_ = e_source_backend_get_backend_name ((ESourceBackend*) _tmp14_);
			_tmp16_ = _tmp15_;
			_tmp6_ = g_strcmp0 (_tmp16_, "mbox") == 0;
		}
		if (_tmp6_) {
			return;
		}
	}
	_tmp17_ = self->priv->_accounts_liststore;
	g_list_store_append (_tmp17_, (GObject*) e_source);
}

static void
online_accounts_accounts_model_remove_esource (OnlineAccountsAccountsModel* self,
                                               ESource* e_source)
{
	guint position = 0U;
	GListStore* _tmp0_;
	guint _tmp1_ = 0U;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (e_source != NULL);
	_tmp0_ = self->priv->_accounts_liststore;
	_tmp2_ = g_list_store_find (_tmp0_, (GObject*) e_source, &_tmp1_);
	position = _tmp1_;
	if (_tmp2_) {
		GListStore* _tmp3_;
		_tmp3_ = self->priv->_accounts_liststore;
		g_list_store_remove (_tmp3_, position);
	}
}

OnlineAccountsAccountsModel*
online_accounts_accounts_model_construct (GType object_type)
{
	OnlineAccountsAccountsModel * self = NULL;
	self = (OnlineAccountsAccountsModel*) g_object_new (object_type, NULL);
	return self;
}

OnlineAccountsAccountsModel*
online_accounts_accounts_model_new (void)
{
	return online_accounts_accounts_model_construct (ONLINE_ACCOUNTS_TYPE_ACCOUNTS_MODEL);
}

GListStore*
online_accounts_accounts_model_get_accounts_liststore (OnlineAccountsAccountsModel* self)
{
	GListStore* result;
	GListStore* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_accounts_liststore;
	result = _tmp0_;
	return result;
}

static void
online_accounts_accounts_model_set_accounts_liststore (OnlineAccountsAccountsModel* self,
                                                       GListStore* value)
{
	GListStore* old_value;
	g_return_if_fail (self != NULL);
	old_value = online_accounts_accounts_model_get_accounts_liststore (self);
	if (old_value != value) {
		GListStore* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_accounts_liststore);
		self->priv->_accounts_liststore = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, online_accounts_accounts_model_properties[ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY]);
	}
}

static GObject *
online_accounts_accounts_model_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	OnlineAccountsAccountsModel * self;
	GListStore* _tmp0_;
	GListStore* _tmp1_;
	parent_class = G_OBJECT_CLASS (online_accounts_accounts_model_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ONLINE_ACCOUNTS_TYPE_ACCOUNTS_MODEL, OnlineAccountsAccountsModel);
	_tmp0_ = g_list_store_new (e_source_get_type ());
	_tmp1_ = _tmp0_;
	online_accounts_accounts_model_set_accounts_liststore (self, _tmp1_);
	_g_object_unref0 (_tmp1_);
	online_accounts_accounts_model_init_registry (self, NULL, NULL);
	return obj;
}

static void
online_accounts_accounts_model_class_init (OnlineAccountsAccountsModelClass * klass,
                                           gpointer klass_data)
{
	online_accounts_accounts_model_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &OnlineAccountsAccountsModel_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_online_accounts_accounts_model_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_online_accounts_accounts_model_set_property;
	G_OBJECT_CLASS (klass)->constructor = online_accounts_accounts_model_constructor;
	G_OBJECT_CLASS (klass)->finalize = online_accounts_accounts_model_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY, online_accounts_accounts_model_properties[ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY] = g_param_spec_object ("accounts-liststore", "accounts-liststore", "accounts-liststore", g_list_store_get_type (), G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
online_accounts_accounts_model_instance_init (OnlineAccountsAccountsModel * self,
                                              gpointer klass)
{
	self->priv = online_accounts_accounts_model_get_instance_private (self);
}

static void
online_accounts_accounts_model_finalize (GObject * obj)
{
	OnlineAccountsAccountsModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ONLINE_ACCOUNTS_TYPE_ACCOUNTS_MODEL, OnlineAccountsAccountsModel);
	_g_object_unref0 (self->priv->_accounts_liststore);
	_g_object_unref0 (self->priv->collection_extension_watcher);
	_g_object_unref0 (self->priv->mail_account_extension_watcher);
	G_OBJECT_CLASS (online_accounts_accounts_model_parent_class)->finalize (obj);
}

static GType
online_accounts_accounts_model_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (OnlineAccountsAccountsModelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) online_accounts_accounts_model_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (OnlineAccountsAccountsModel), 0, (GInstanceInitFunc) online_accounts_accounts_model_instance_init, NULL };
	GType online_accounts_accounts_model_type_id;
	online_accounts_accounts_model_type_id = g_type_register_static (G_TYPE_OBJECT, "OnlineAccountsAccountsModel", &g_define_type_info, 0);
	OnlineAccountsAccountsModel_private_offset = g_type_add_instance_private (online_accounts_accounts_model_type_id, sizeof (OnlineAccountsAccountsModelPrivate));
	return online_accounts_accounts_model_type_id;
}

GType
online_accounts_accounts_model_get_type (void)
{
	static volatile gsize online_accounts_accounts_model_type_id__once = 0;
	if (g_once_init_enter (&online_accounts_accounts_model_type_id__once)) {
		GType online_accounts_accounts_model_type_id;
		online_accounts_accounts_model_type_id = online_accounts_accounts_model_get_type_once ();
		g_once_init_leave (&online_accounts_accounts_model_type_id__once, online_accounts_accounts_model_type_id);
	}
	return online_accounts_accounts_model_type_id__once;
}

static void
_vala_online_accounts_accounts_model_get_property (GObject * object,
                                                   guint property_id,
                                                   GValue * value,
                                                   GParamSpec * pspec)
{
	OnlineAccountsAccountsModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, ONLINE_ACCOUNTS_TYPE_ACCOUNTS_MODEL, OnlineAccountsAccountsModel);
	switch (property_id) {
		case ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY:
		g_value_set_object (value, online_accounts_accounts_model_get_accounts_liststore (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_online_accounts_accounts_model_set_property (GObject * object,
                                                   guint property_id,
                                                   const GValue * value,
                                                   GParamSpec * pspec)
{
	OnlineAccountsAccountsModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, ONLINE_ACCOUNTS_TYPE_ACCOUNTS_MODEL, OnlineAccountsAccountsModel);
	switch (property_id) {
		case ONLINE_ACCOUNTS_ACCOUNTS_MODEL_ACCOUNTS_LISTSTORE_PROPERTY:
		online_accounts_accounts_model_set_accounts_liststore (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

