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

#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.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
#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 G4_TYPE_DIR_MONITOR (g4_dir_monitor_get_type ())
#define G4_DIR_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G4_TYPE_DIR_MONITOR, G4DirMonitor))
#define G4_DIR_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), G4_TYPE_DIR_MONITOR, G4DirMonitorClass))
#define G4_IS_DIR_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G4_TYPE_DIR_MONITOR))
#define G4_IS_DIR_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), G4_TYPE_DIR_MONITOR))
#define G4_DIR_MONITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G4_TYPE_DIR_MONITOR, G4DirMonitorClass))

typedef struct _G4DirMonitor G4DirMonitor;
typedef struct _G4DirMonitorClass G4DirMonitorClass;
typedef struct _G4DirMonitorPrivate G4DirMonitorPrivate;
enum  {
	G4_DIR_MONITOR_0_PROPERTY,
	G4_DIR_MONITOR_ENABLED_PROPERTY,
	G4_DIR_MONITOR_NUM_PROPERTIES
};
static GParamSpec* g4_dir_monitor_properties[G4_DIR_MONITOR_NUM_PROPERTIES];
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
enum  {
	G4_DIR_MONITOR_ADD_FILE_SIGNAL,
	G4_DIR_MONITOR_REMOVE_FILE_SIGNAL,
	G4_DIR_MONITOR_NUM_SIGNALS
};
static guint g4_dir_monitor_signals[G4_DIR_MONITOR_NUM_SIGNALS] = {0};

struct _G4DirMonitor {
	GObject parent_instance;
	G4DirMonitorPrivate * priv;
};

struct _G4DirMonitorClass {
	GObjectClass parent_class;
};

struct _G4DirMonitorPrivate {
	GHashTable* _monitors;
	GRecMutex __lock__monitors;
	gboolean _enabled;
};

static gint G4DirMonitor_private_offset;
static gpointer g4_dir_monitor_parent_class = NULL;

VALA_EXTERN GType g4_dir_monitor_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (G4DirMonitor, g_object_unref)
VALA_EXTERN void g4_dir_monitor_remove_all (G4DirMonitor* self);
static void _g_free0_ (gpointer var);
static void _g_object_unref0_ (gpointer var);
VALA_EXTERN void g4_dir_monitor_monitor (G4DirMonitor* self,
                             GPtrArray* dirs);
VALA_EXTERN void g4_dir_monitor_monitor_one (G4DirMonitor* self,
                                 GFile* dir);
static void g4_dir_monitor_monitor_func (G4DirMonitor* self,
                                  GFile* file,
                                  GFile* other_file,
                                  GFileMonitorEvent event);
static void _g4_dir_monitor_monitor_func_g_file_monitor_changed (GFileMonitor* _sender,
                                                          GFile* file,
                                                          GFile* other_file,
                                                          GFileMonitorEvent event_type,
                                                          gpointer self);
static void ___lambda5_ (G4DirMonitor* self,
                  const gchar* uri,
                  GFileMonitor* monitor);
static void ____lambda5__gh_func (gconstpointer key,
                           gconstpointer value,
                           gpointer self);
VALA_EXTERN G4DirMonitor* g4_dir_monitor_new (void);
VALA_EXTERN G4DirMonitor* g4_dir_monitor_construct (GType object_type);
VALA_EXTERN gboolean g4_dir_monitor_get_enabled (G4DirMonitor* self);
VALA_EXTERN void g4_dir_monitor_set_enabled (G4DirMonitor* self,
                                 gboolean value);
static void g4_dir_monitor_finalize (GObject * obj);
static GType g4_dir_monitor_get_type_once (void);
static void _vala_g4_dir_monitor_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec);
static void _vala_g4_dir_monitor_set_property (GObject * object,
                                        guint property_id,
                                        const GValue * value,
                                        GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
g4_dir_monitor_get_instance_private (G4DirMonitor* self)
{
	return G_STRUCT_MEMBER_P (self, G4DirMonitor_private_offset);
}

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

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

void
g4_dir_monitor_monitor (G4DirMonitor* self,
                        GPtrArray* dirs)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (dirs != NULL);
	{
		GPtrArray* dir_collection = NULL;
		guint dir_index = 0U;
		dir_collection = dirs;
		for (dir_index = 0; dir_index < dir_collection->len; dir_index = dir_index + 1) {
			GFile* _tmp0_;
			GFile* dir = NULL;
			_tmp0_ = _g_object_ref0 ((GFile*) g_ptr_array_index (dir_collection, dir_index));
			dir = _tmp0_;
			{
				GFile* _tmp1_;
				_tmp1_ = dir;
				if (g_file_is_native (_tmp1_)) {
					GFile* _tmp2_;
					_tmp2_ = dir;
					g4_dir_monitor_monitor_one (self, _tmp2_);
				}
				_g_object_unref0 (dir);
			}
		}
	}
}

static void
_g4_dir_monitor_monitor_func_g_file_monitor_changed (GFileMonitor* _sender,
                                                     GFile* file,
                                                     GFile* other_file,
                                                     GFileMonitorEvent event_type,
                                                     gpointer self)
{
	g4_dir_monitor_monitor_func ((G4DirMonitor*) self, file, other_file, event_type);
}

void
g4_dir_monitor_monitor_one (G4DirMonitor* self,
                            GFile* dir)
{
	gchar* uri = NULL;
	gchar* _tmp0_;
	const gchar* orig_key = NULL;
	GFileMonitor* monitor = NULL;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (dir != NULL);
	_tmp0_ = g_file_get_uri (dir);
	uri = _tmp0_;
	{
		GHashTable* _tmp1_;
		_tmp1_ = self->priv->_monitors;
		g_rec_mutex_lock (&self->priv->__lock__monitors);
		{
			GHashTable* _tmp2_;
			const gchar* _tmp3_;
			gconstpointer _tmp4_ = NULL;
			gconstpointer _tmp5_ = NULL;
			gboolean _tmp6_;
			GFileMonitor* _tmp7_;
			_tmp2_ = self->priv->_monitors;
			_tmp3_ = uri;
			_tmp6_ = g_hash_table_lookup_extended (_tmp2_, _tmp3_, &_tmp4_, &_tmp5_);
			orig_key = (const gchar*) _tmp4_;
			_g_object_unref0 (monitor);
			_tmp7_ = _g_object_ref0 ((GFileMonitor*) _tmp5_);
			monitor = _tmp7_;
			if (_tmp6_) {
				GFileMonitor* _tmp8_;
				_tmp8_ = monitor;
				g_file_monitor_cancel (_tmp8_);
			}
		}
		__finally0:
		{
			GHashTable* _tmp9_;
			_tmp9_ = self->priv->_monitors;
			g_rec_mutex_unlock (&self->priv->__lock__monitors);
		}
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (monitor);
			_g_free0 (uri);
			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;
		}
	}
	if (self->priv->_enabled) {
		{
			GFileMonitor* _tmp10_ = NULL;
			GFileMonitor* _tmp11_;
			GFileMonitor* _tmp12_;
			GFileMonitor* _tmp13_;
			_tmp11_ = g_file_monitor (dir, G_FILE_MONITOR_WATCH_MOVES, NULL, &_inner_error0_);
			_tmp10_ = _tmp11_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			_tmp12_ = _tmp10_;
			_tmp10_ = NULL;
			_g_object_unref0 (monitor);
			monitor = _tmp12_;
			_tmp13_ = monitor;
			g_signal_connect_object (_tmp13_, "changed", (GCallback) _g4_dir_monitor_monitor_func_g_file_monitor_changed, self, 0);
			{
				GHashTable* _tmp14_;
				_tmp14_ = self->priv->_monitors;
				g_rec_mutex_lock (&self->priv->__lock__monitors);
				{
					GHashTable* _tmp15_;
					const gchar* _tmp16_;
					gchar* _tmp17_;
					GFileMonitor* _tmp18_;
					GFileMonitor* _tmp19_;
					_tmp15_ = self->priv->_monitors;
					_tmp16_ = uri;
					_tmp17_ = g_strdup (_tmp16_);
					_tmp18_ = monitor;
					_tmp19_ = _g_object_ref0 (_tmp18_);
					g_hash_table_insert (_tmp15_, _tmp17_, _tmp19_);
				}
				__finally2:
				{
					GHashTable* _tmp20_;
					_tmp20_ = self->priv->_monitors;
					g_rec_mutex_unlock (&self->priv->__lock__monitors);
				}
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					_g_object_unref0 (_tmp10_);
					goto __catch1_g_error;
				}
			}
			_g_object_unref0 (_tmp10_);
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* e = NULL;
			GError* _tmp21_;
			const gchar* _tmp22_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp21_ = e;
			_tmp22_ = _tmp21_->message;
			g_print ("Monitor dir error: %s\n", _tmp22_);
			_g_error_free0 (e);
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (monitor);
			_g_free0 (uri);
			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;
		}
	}
	_g_object_unref0 (monitor);
	_g_free0 (uri);
}

static void
___lambda5_ (G4DirMonitor* self,
             const gchar* uri,
             GFileMonitor* monitor)
{
	g_return_if_fail (uri != NULL);
	g_return_if_fail (monitor != NULL);
	g_file_monitor_cancel (monitor);
}

static void
____lambda5__gh_func (gconstpointer key,
                      gconstpointer value,
                      gpointer self)
{
	___lambda5_ ((G4DirMonitor*) self, (const gchar*) key, (GFileMonitor*) value);
}

void
g4_dir_monitor_remove_all (G4DirMonitor* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		GHashTable* _tmp0_;
		_tmp0_ = self->priv->_monitors;
		g_rec_mutex_lock (&self->priv->__lock__monitors);
		{
			GHashTable* _tmp1_;
			GHashTable* _tmp2_;
			_tmp1_ = self->priv->_monitors;
			g_hash_table_foreach (_tmp1_, ____lambda5__gh_func, self);
			_tmp2_ = self->priv->_monitors;
			g_hash_table_remove_all (_tmp2_);
		}
		__finally0:
		{
			GHashTable* _tmp3_;
			_tmp3_ = self->priv->_monitors;
			g_rec_mutex_unlock (&self->priv->__lock__monitors);
		}
		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
g4_dir_monitor_monitor_func (G4DirMonitor* self,
                             GFile* file,
                             GFile* other_file,
                             GFileMonitorEvent event)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (file != NULL);
	switch (event) {
		case G_FILE_MONITOR_EVENT_CHANGED:
		{
			g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_REMOVE_FILE_SIGNAL], 0, file);
			g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_ADD_FILE_SIGNAL], 0, file);
			break;
		}
		case G_FILE_MONITOR_EVENT_MOVED_IN:
		{
			g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_ADD_FILE_SIGNAL], 0, file);
			break;
		}
		case G_FILE_MONITOR_EVENT_DELETED:
		case G_FILE_MONITOR_EVENT_MOVED_OUT:
		{
			g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_REMOVE_FILE_SIGNAL], 0, file);
			break;
		}
		case G_FILE_MONITOR_EVENT_RENAMED:
		{
			g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_REMOVE_FILE_SIGNAL], 0, file);
			if (other_file != NULL) {
				g_signal_emit (self, g4_dir_monitor_signals[G4_DIR_MONITOR_ADD_FILE_SIGNAL], 0, G_TYPE_CHECK_INSTANCE_CAST (other_file, g_file_get_type (), GFile));
			}
			break;
		}
		default:
		{
			break;
		}
	}
}

G4DirMonitor*
g4_dir_monitor_construct (GType object_type)
{
	G4DirMonitor * self = NULL;
	self = (G4DirMonitor*) g_object_new (object_type, NULL);
	return self;
}

G4DirMonitor*
g4_dir_monitor_new (void)
{
	return g4_dir_monitor_construct (G4_TYPE_DIR_MONITOR);
}

gboolean
g4_dir_monitor_get_enabled (G4DirMonitor* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_enabled;
	return result;
}

void
g4_dir_monitor_set_enabled (G4DirMonitor* self,
                            gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = g4_dir_monitor_get_enabled (self);
	if (old_value != value) {
		self->priv->_enabled = value;
		g_object_notify_by_pspec ((GObject *) self, g4_dir_monitor_properties[G4_DIR_MONITOR_ENABLED_PROPERTY]);
	}
}

static void
g4_dir_monitor_class_init (G4DirMonitorClass * klass,
                           gpointer klass_data)
{
	g4_dir_monitor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &G4DirMonitor_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_g4_dir_monitor_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_g4_dir_monitor_set_property;
	G_OBJECT_CLASS (klass)->finalize = g4_dir_monitor_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), G4_DIR_MONITOR_ENABLED_PROPERTY, g4_dir_monitor_properties[G4_DIR_MONITOR_ENABLED_PROPERTY] = g_param_spec_boolean ("enabled", "enabled", "enabled", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g4_dir_monitor_signals[G4_DIR_MONITOR_ADD_FILE_SIGNAL] = g_signal_new ("add-file", G4_TYPE_DIR_MONITOR, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, g_file_get_type ());
	g4_dir_monitor_signals[G4_DIR_MONITOR_REMOVE_FILE_SIGNAL] = g_signal_new ("remove-file", G4_TYPE_DIR_MONITOR, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, g_file_get_type ());
}

static void
g4_dir_monitor_instance_init (G4DirMonitor * self,
                              gpointer klass)
{
	GHashFunc _tmp0_;
	GEqualFunc _tmp1_;
	GHashTable* _tmp2_;
	self->priv = g4_dir_monitor_get_instance_private (self);
	g_rec_mutex_init (&self->priv->__lock__monitors);
	_tmp0_ = g_str_hash;
	_tmp1_ = g_str_equal;
	_tmp2_ = g_hash_table_new_full (_tmp0_, _tmp1_, _g_free0_, _g_object_unref0_);
	self->priv->_monitors = _tmp2_;
}

static void
g4_dir_monitor_finalize (GObject * obj)
{
	G4DirMonitor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, G4_TYPE_DIR_MONITOR, G4DirMonitor);
	g4_dir_monitor_remove_all (self);
	g_rec_mutex_clear (&self->priv->__lock__monitors);
	_g_hash_table_unref0 (self->priv->_monitors);
	G_OBJECT_CLASS (g4_dir_monitor_parent_class)->finalize (obj);
}

static GType
g4_dir_monitor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (G4DirMonitorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) g4_dir_monitor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (G4DirMonitor), 0, (GInstanceInitFunc) g4_dir_monitor_instance_init, NULL };
	GType g4_dir_monitor_type_id;
	g4_dir_monitor_type_id = g_type_register_static (G_TYPE_OBJECT, "G4DirMonitor", &g_define_type_info, 0);
	G4DirMonitor_private_offset = g_type_add_instance_private (g4_dir_monitor_type_id, sizeof (G4DirMonitorPrivate));
	return g4_dir_monitor_type_id;
}

GType
g4_dir_monitor_get_type (void)
{
	static volatile gsize g4_dir_monitor_type_id__once = 0;
	if (g_once_init_enter (&g4_dir_monitor_type_id__once)) {
		GType g4_dir_monitor_type_id;
		g4_dir_monitor_type_id = g4_dir_monitor_get_type_once ();
		g_once_init_leave (&g4_dir_monitor_type_id__once, g4_dir_monitor_type_id);
	}
	return g4_dir_monitor_type_id__once;
}

static void
_vala_g4_dir_monitor_get_property (GObject * object,
                                   guint property_id,
                                   GValue * value,
                                   GParamSpec * pspec)
{
	G4DirMonitor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, G4_TYPE_DIR_MONITOR, G4DirMonitor);
	switch (property_id) {
		case G4_DIR_MONITOR_ENABLED_PROPERTY:
		g_value_set_boolean (value, g4_dir_monitor_get_enabled (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_g4_dir_monitor_set_property (GObject * object,
                                   guint property_id,
                                   const GValue * value,
                                   GParamSpec * pspec)
{
	G4DirMonitor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, G4_TYPE_DIR_MONITOR, G4DirMonitor);
	switch (property_id) {
		case G4_DIR_MONITOR_ENABLED_PROPERTY:
		g4_dir_monitor_set_enabled (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

