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

/*
 * Copyright (C) 2011-2017 elementary Developers
 *
 * 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/>.
 *
 * Authors: ammonkey <am.monkeyd@gmail.com>,
 *          lampe2 <michael@lazarski.me>,
 *          Akshay Shekher <voldyman666@gmail.com>,
 *          Victor Martinez <victoreduardm@gmail.com>
 */

#include <glib-object.h>
#include <gio/gio.h>
#include <glib.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>

#define CONTRACTOR_CONTRACT_DIRECTORY_DIRECTORY_RELOAD_TIMEOUT_MS 500
#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 CONTRACTOR_TYPE_CONTRACT_DIRECTORY (contractor_contract_directory_get_type ())
#define CONTRACTOR_CONTRACT_DIRECTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTRACTOR_TYPE_CONTRACT_DIRECTORY, ContractorContractDirectory))
#define CONTRACTOR_CONTRACT_DIRECTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTRACTOR_TYPE_CONTRACT_DIRECTORY, ContractorContractDirectoryClass))
#define CONTRACTOR_IS_CONTRACT_DIRECTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTRACTOR_TYPE_CONTRACT_DIRECTORY))
#define CONTRACTOR_IS_CONTRACT_DIRECTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTRACTOR_TYPE_CONTRACT_DIRECTORY))
#define CONTRACTOR_CONTRACT_DIRECTORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTRACTOR_TYPE_CONTRACT_DIRECTORY, ContractorContractDirectoryClass))

typedef struct _ContractorContractDirectory ContractorContractDirectory;
typedef struct _ContractorContractDirectoryClass ContractorContractDirectoryClass;
typedef struct _ContractorContractDirectoryPrivate ContractorContractDirectoryPrivate;
enum  {
	CONTRACTOR_CONTRACT_DIRECTORY_0_PROPERTY,
	CONTRACTOR_CONTRACT_DIRECTORY_NUM_PROPERTIES
};
static GParamSpec* contractor_contract_directory_properties[CONTRACTOR_CONTRACT_DIRECTORY_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR (contractor_contract_file_enumerator_get_type ())
#define CONTRACTOR_CONTRACT_FILE_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR, ContractorContractFileEnumerator))
#define CONTRACTOR_CONTRACT_FILE_ENUMERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR, ContractorContractFileEnumeratorClass))
#define CONTRACTOR_IS_CONTRACT_FILE_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR))
#define CONTRACTOR_IS_CONTRACT_FILE_ENUMERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR))
#define CONTRACTOR_CONTRACT_FILE_ENUMERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTRACTOR_TYPE_CONTRACT_FILE_ENUMERATOR, ContractorContractFileEnumeratorClass))

typedef struct _ContractorContractFileEnumerator ContractorContractFileEnumerator;
typedef struct _ContractorContractFileEnumeratorClass ContractorContractFileEnumeratorClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ContractorContractDirectoryOnChangeEventData ContractorContractDirectoryOnChangeEventData;
enum  {
	CONTRACTOR_CONTRACT_DIRECTORY_CHANGED_SIGNAL,
	CONTRACTOR_CONTRACT_DIRECTORY_NUM_SIGNALS
};
static guint contractor_contract_directory_signals[CONTRACTOR_CONTRACT_DIRECTORY_NUM_SIGNALS] = {0};

struct _ContractorContractDirectory {
	GObject parent_instance;
	ContractorContractDirectoryPrivate * priv;
};

struct _ContractorContractDirectoryClass {
	GObjectClass parent_class;
};

struct _ContractorContractDirectoryPrivate {
	GFile* contract_directory;
	GFileMonitor* monitor;
	gboolean update_pending;
};

struct _ContractorContractDirectoryOnChangeEventData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ContractorContractDirectory* self;
	GFile* file;
	GFile* other_file;
	GFileMonitorEvent event;
};

static gint ContractorContractDirectory_private_offset;
static gpointer contractor_contract_directory_parent_class = NULL;

VALA_EXTERN GType contractor_contract_directory_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContractorContractDirectory, g_object_unref)
VALA_EXTERN ContractorContractDirectory* contractor_contract_directory_new (GFile* directory);
VALA_EXTERN ContractorContractDirectory* contractor_contract_directory_construct (GType object_type,
                                                                      GFile* directory);
static void contractor_contract_directory_set_monitor (ContractorContractDirectory* self);
VALA_EXTERN GeeList* contractor_contract_directory_lookup_contract_files (ContractorContractDirectory* self);
VALA_EXTERN GType contractor_contract_file_enumerator_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ContractorContractFileEnumerator, g_object_unref)
VALA_EXTERN ContractorContractFileEnumerator* contractor_contract_file_enumerator_new (GFile* directory);
VALA_EXTERN ContractorContractFileEnumerator* contractor_contract_file_enumerator_construct (GType object_type,
                                                                                 GFile* directory);
VALA_EXTERN GeeList* contractor_contract_file_enumerator_enumerate_files (ContractorContractFileEnumerator* self);
static void contractor_contract_directory_on_change_event (ContractorContractDirectory* self,
                                                    GFile* file,
                                                    GFile* other_file,
                                                    GFileMonitorEvent event,
                                                    GAsyncReadyCallback _callback_,
                                                    gpointer _user_data_);
static void contractor_contract_directory_on_change_event_finish (ContractorContractDirectory* self,
                                                           GAsyncResult* _res_);
static void _contractor_contract_directory_on_change_event_g_file_monitor_changed (GFileMonitor* _sender,
                                                                            GFile* file,
                                                                            GFile* other_file,
                                                                            GFileMonitorEvent event_type,
                                                                            gpointer self);
static void contractor_contract_directory_on_change_event_data_free (gpointer _data);
static gboolean contractor_contract_directory_on_change_event_co (ContractorContractDirectoryOnChangeEventData* _data_);
static gboolean _contractor_contract_directory_on_change_event_co_gsource_func (gpointer self);
static void contractor_contract_directory_finalize (GObject * obj);
static GType contractor_contract_directory_get_type_once (void);

static inline gpointer
contractor_contract_directory_get_instance_private (ContractorContractDirectory* self)
{
	return G_STRUCT_MEMBER_P (self, ContractorContractDirectory_private_offset);
}

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

ContractorContractDirectory*
contractor_contract_directory_construct (GType object_type,
                                         GFile* directory)
{
	ContractorContractDirectory * self = NULL;
	GFile* _tmp0_;
	g_return_val_if_fail (directory != NULL, NULL);
	self = (ContractorContractDirectory*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (directory);
	_g_object_unref0 (self->priv->contract_directory);
	self->priv->contract_directory = _tmp0_;
	contractor_contract_directory_set_monitor (self);
	return self;
}

ContractorContractDirectory*
contractor_contract_directory_new (GFile* directory)
{
	return contractor_contract_directory_construct (CONTRACTOR_TYPE_CONTRACT_DIRECTORY, directory);
}

GeeList*
contractor_contract_directory_lookup_contract_files (ContractorContractDirectory* self)
{
	ContractorContractFileEnumerator* file_enumerator = NULL;
	GFile* _tmp0_;
	ContractorContractFileEnumerator* _tmp1_;
	GeeList* _tmp2_;
	GeeList* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->contract_directory;
	_tmp1_ = contractor_contract_file_enumerator_new (_tmp0_);
	file_enumerator = _tmp1_;
	_tmp2_ = contractor_contract_file_enumerator_enumerate_files (file_enumerator);
	result = _tmp2_;
	_g_object_unref0 (file_enumerator);
	return result;
}

static void
_contractor_contract_directory_on_change_event_g_file_monitor_changed (GFileMonitor* _sender,
                                                                       GFile* file,
                                                                       GFile* other_file,
                                                                       GFileMonitorEvent event_type,
                                                                       gpointer self)
{
	contractor_contract_directory_on_change_event ((ContractorContractDirectory*) self, file, other_file, event_type, NULL, NULL);
}

static void
contractor_contract_directory_set_monitor (ContractorContractDirectory* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		GFileMonitor* _tmp0_ = NULL;
		GFile* _tmp1_;
		GFileMonitor* _tmp2_;
		GFileMonitor* _tmp3_;
		GFileMonitor* _tmp4_;
		_tmp1_ = self->priv->contract_directory;
		_tmp2_ = g_file_monitor_directory (_tmp1_, G_FILE_MONITOR_NONE, NULL, &_inner_error0_);
		_tmp0_ = _tmp2_;
		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;
		}
		_tmp3_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->monitor);
		self->priv->monitor = _tmp3_;
		_tmp4_ = self->priv->monitor;
		g_signal_connect_object (_tmp4_, "changed", (GCallback) _contractor_contract_directory_on_change_event_g_file_monitor_changed, self, 0);
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_io_error:
	{
		GError* e = NULL;
		GFile* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		GError* _tmp8_;
		const gchar* _tmp9_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = self->priv->contract_directory;
		_tmp6_ = g_file_get_path (_tmp5_);
		_tmp7_ = _tmp6_;
		_tmp8_ = e;
		_tmp9_ = _tmp8_->message;
		g_critical ("ContractDirectory.vala:48: Could not set up monitor for '%s': %s", _tmp7_, _tmp9_);
		_g_free0 (_tmp7_);
		_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
contractor_contract_directory_on_change_event_data_free (gpointer _data)
{
	ContractorContractDirectoryOnChangeEventData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->file);
	_g_object_unref0 (_data_->other_file);
	_g_object_unref0 (_data_->self);
	g_slice_free (ContractorContractDirectoryOnChangeEventData, _data_);
}

static void
contractor_contract_directory_on_change_event (ContractorContractDirectory* self,
                                               GFile* file,
                                               GFile* other_file,
                                               GFileMonitorEvent event,
                                               GAsyncReadyCallback _callback_,
                                               gpointer _user_data_)
{
	ContractorContractDirectoryOnChangeEventData* _data_;
	ContractorContractDirectory* _tmp0_;
	GFile* _tmp1_;
	GFile* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (file != NULL);
	_data_ = g_slice_new0 (ContractorContractDirectoryOnChangeEventData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, contractor_contract_directory_on_change_event_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (file);
	_g_object_unref0 (_data_->file);
	_data_->file = _tmp1_;
	_tmp2_ = _g_object_ref0 (other_file);
	_g_object_unref0 (_data_->other_file);
	_data_->other_file = _tmp2_;
	_data_->event = event;
	contractor_contract_directory_on_change_event_co (_data_);
}

static void
contractor_contract_directory_on_change_event_finish (ContractorContractDirectory* self,
                                                      GAsyncResult* _res_)
{
	ContractorContractDirectoryOnChangeEventData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static gboolean
_contractor_contract_directory_on_change_event_co_gsource_func (gpointer self)
{
	gboolean result;
	result = contractor_contract_directory_on_change_event_co (self);
	return result;
}

static gboolean
contractor_contract_directory_on_change_event_co (ContractorContractDirectoryOnChangeEventData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	if (_data_->self->priv->update_pending) {
		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;
	}
	_data_->self->priv->update_pending = TRUE;
	g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) CONTRACTOR_CONTRACT_DIRECTORY_DIRECTORY_RELOAD_TIMEOUT_MS, _contractor_contract_directory_on_change_event_co_gsource_func, _data_, NULL);
	_data_->_state_ = 1;
	return FALSE;
	_state_1:
	;
	g_signal_emit (_data_->self, contractor_contract_directory_signals[CONTRACTOR_CONTRACT_DIRECTORY_CHANGED_SIGNAL], 0);
	_data_->self->priv->update_pending = 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
contractor_contract_directory_class_init (ContractorContractDirectoryClass * klass,
                                          gpointer klass_data)
{
	contractor_contract_directory_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContractorContractDirectory_private_offset);
	G_OBJECT_CLASS (klass)->finalize = contractor_contract_directory_finalize;
	contractor_contract_directory_signals[CONTRACTOR_CONTRACT_DIRECTORY_CHANGED_SIGNAL] = g_signal_new ("changed", CONTRACTOR_TYPE_CONTRACT_DIRECTORY, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
contractor_contract_directory_instance_init (ContractorContractDirectory * self,
                                             gpointer klass)
{
	self->priv = contractor_contract_directory_get_instance_private (self);
	self->priv->update_pending = FALSE;
}

static void
contractor_contract_directory_finalize (GObject * obj)
{
	ContractorContractDirectory * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTRACTOR_TYPE_CONTRACT_DIRECTORY, ContractorContractDirectory);
	_g_object_unref0 (self->priv->contract_directory);
	_g_object_unref0 (self->priv->monitor);
	G_OBJECT_CLASS (contractor_contract_directory_parent_class)->finalize (obj);
}

static GType
contractor_contract_directory_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContractorContractDirectoryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contractor_contract_directory_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContractorContractDirectory), 0, (GInstanceInitFunc) contractor_contract_directory_instance_init, NULL };
	GType contractor_contract_directory_type_id;
	contractor_contract_directory_type_id = g_type_register_static (G_TYPE_OBJECT, "ContractorContractDirectory", &g_define_type_info, 0);
	ContractorContractDirectory_private_offset = g_type_add_instance_private (contractor_contract_directory_type_id, sizeof (ContractorContractDirectoryPrivate));
	return contractor_contract_directory_type_id;
}

GType
contractor_contract_directory_get_type (void)
{
	static volatile gsize contractor_contract_directory_type_id__once = 0;
	if (g_once_init_enter (&contractor_contract_directory_type_id__once)) {
		GType contractor_contract_directory_type_id;
		contractor_contract_directory_type_id = contractor_contract_directory_get_type_once ();
		g_once_init_leave (&contractor_contract_directory_type_id__once, contractor_contract_directory_type_id);
	}
	return contractor_contract_directory_type_id__once;
}

