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

#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gobject/gvaluecollector.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 DC_TYPE_SAVE_FOLDER (dc_save_folder_get_type ())
#define DC_SAVE_FOLDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DC_TYPE_SAVE_FOLDER, DcSaveFolder))
#define DC_SAVE_FOLDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DC_TYPE_SAVE_FOLDER, DcSaveFolderClass))
#define DC_IS_SAVE_FOLDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DC_TYPE_SAVE_FOLDER))
#define DC_IS_SAVE_FOLDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DC_TYPE_SAVE_FOLDER))
#define DC_SAVE_FOLDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DC_TYPE_SAVE_FOLDER, DcSaveFolderClass))

typedef struct _DcSaveFolder DcSaveFolder;
typedef struct _DcSaveFolderClass DcSaveFolderClass;
typedef struct _DcSaveFolderPrivate DcSaveFolderPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _DcParamSpecSaveFolder DcParamSpecSaveFolder;

struct _DcSaveFolder {
	GTypeInstance parent_instance;
	volatile int ref_count;
	DcSaveFolderPrivate * priv;
};

struct _DcSaveFolderClass {
	GTypeClass parent_class;
	void (*finalize) (DcSaveFolder *self);
};

struct _DcParamSpecSaveFolder {
	GParamSpec parent_instance;
};

static gpointer dc_save_folder_parent_class = NULL;

VALA_EXTERN gpointer dc_save_folder_ref (gpointer instance);
VALA_EXTERN void dc_save_folder_unref (gpointer instance);
VALA_EXTERN GParamSpec* dc_param_spec_save_folder (const gchar* name,
                                       const gchar* nick,
                                       const gchar* blurb,
                                       GType object_type,
                                       GParamFlags flags);
VALA_EXTERN void dc_value_set_save_folder (GValue* value,
                               gpointer v_object);
VALA_EXTERN void dc_value_take_save_folder (GValue* value,
                                gpointer v_object);
VALA_EXTERN gpointer dc_value_get_save_folder (const GValue* value);
VALA_EXTERN GType dc_save_folder_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DcSaveFolder, dc_save_folder_unref)
VALA_EXTERN GFile* dc_save_folder_unique_destination (GFile* folder,
                                          const gchar* name);
VALA_EXTERN DcSaveFolder* dc_save_folder_new (void);
VALA_EXTERN DcSaveFolder* dc_save_folder_construct (GType object_type);
static void dc_save_folder_finalize (DcSaveFolder * obj);
static GType dc_save_folder_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

/** folder/name, adding " (2)", " (3)", … before the extension when
            a file with that name already exists. Multi-part extensions use
            the last dot: "a.tar.gz" collides into "a.tar (2).gz". */
static gint
string_last_index_of_char (const gchar* self,
                           gunichar c,
                           gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = g_utf8_strrchr (((gchar*) self) + start_index, (gssize) -1, c);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static gchar*
string_slice (const gchar* self,
              glong start,
              glong end)
{
	glong string_length = 0L;
	gint _tmp0_;
	gint _tmp1_;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	if (start < ((glong) 0)) {
		start = string_length + start;
	}
	if (end < ((glong) 0)) {
		end = string_length + end;
	}
	if (start >= ((glong) 0)) {
		_tmp2_ = start <= string_length;
	} else {
		_tmp2_ = FALSE;
	}
	g_return_val_if_fail (_tmp2_, NULL);
	if (end >= ((glong) 0)) {
		_tmp3_ = end <= string_length;
	} else {
		_tmp3_ = FALSE;
	}
	g_return_val_if_fail (_tmp3_, NULL);
	g_return_val_if_fail (start <= end, NULL);
	_tmp4_ = g_strndup (((gchar*) self) + start, (gsize) (end - start));
	result = _tmp4_;
	return result;
}

GFile*
dc_save_folder_unique_destination (GFile* folder,
                                   const gchar* name)
{
	GFile* dest = NULL;
	GFile* _tmp0_;
	GFile* _tmp1_;
	gchar* stem = NULL;
	gchar* _tmp2_;
	gchar* ext = NULL;
	gchar* _tmp3_;
	gint dot = 0;
	GFile* result;
	g_return_val_if_fail (folder != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = g_file_get_child (folder, name);
	dest = _tmp0_;
	_tmp1_ = dest;
	if (!g_file_query_exists (_tmp1_, NULL)) {
		result = dest;
		return result;
	}
	_tmp2_ = g_strdup (name);
	stem = _tmp2_;
	_tmp3_ = g_strdup ("");
	ext = _tmp3_;
	dot = string_last_index_of_char (name, (gunichar) '.', 0);
	if (dot > 0) {
		gchar* _tmp4_;
		gint _tmp5_;
		gint _tmp6_;
		gchar* _tmp7_;
		_tmp4_ = string_slice (name, (glong) 0, (glong) dot);
		_g_free0 (stem);
		stem = _tmp4_;
		_tmp5_ = strlen (name);
		_tmp6_ = _tmp5_;
		_tmp7_ = string_slice (name, (glong) dot, (glong) _tmp6_);
		_g_free0 (ext);
		ext = _tmp7_;
	}
	{
		gint i = 0;
		i = 2;
		{
			gboolean _tmp8_ = FALSE;
			_tmp8_ = TRUE;
			while (TRUE) {
				GFile* candidate = NULL;
				const gchar* _tmp10_;
				const gchar* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_;
				GFile* _tmp14_;
				GFile* _tmp15_;
				GFile* _tmp16_;
				if (!_tmp8_) {
					gint _tmp9_;
					_tmp9_ = i;
					i = _tmp9_ + 1;
				}
				_tmp8_ = FALSE;
				_tmp10_ = stem;
				_tmp11_ = ext;
				_tmp12_ = g_strdup_printf ("%s (%d)%s", _tmp10_, i, _tmp11_);
				_tmp13_ = _tmp12_;
				_tmp14_ = g_file_get_child (folder, _tmp13_);
				_tmp15_ = _tmp14_;
				_g_free0 (_tmp13_);
				candidate = _tmp15_;
				_tmp16_ = candidate;
				if (!g_file_query_exists (_tmp16_, NULL)) {
					result = candidate;
					_g_free0 (ext);
					_g_free0 (stem);
					_g_object_unref0 (dest);
					return result;
				}
				_g_object_unref0 (candidate);
			}
		}
	}
}

DcSaveFolder*
dc_save_folder_construct (GType object_type)
{
	DcSaveFolder* self = NULL;
	self = (DcSaveFolder*) g_type_create_instance (object_type);
	return self;
}

DcSaveFolder*
dc_save_folder_new (void)
{
	return dc_save_folder_construct (DC_TYPE_SAVE_FOLDER);
}

static void
dc_value_save_folder_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
dc_value_save_folder_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		dc_save_folder_unref (value->data[0].v_pointer);
	}
}

static void
dc_value_save_folder_copy_value (const GValue* src_value,
                                 GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = dc_save_folder_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
dc_value_save_folder_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
dc_value_save_folder_collect_value (GValue* value,
                                    guint n_collect_values,
                                    GTypeCValue* collect_values,
                                    guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		DcSaveFolder * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = dc_save_folder_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
dc_value_save_folder_lcopy_value (const GValue* value,
                                  guint n_collect_values,
                                  GTypeCValue* collect_values,
                                  guint collect_flags)
{
	DcSaveFolder ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = dc_save_folder_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
dc_param_spec_save_folder (const gchar* name,
                           const gchar* nick,
                           const gchar* blurb,
                           GType object_type,
                           GParamFlags flags)
{
	DcParamSpecSaveFolder* spec;
	g_return_val_if_fail (g_type_is_a (object_type, DC_TYPE_SAVE_FOLDER), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
dc_value_get_save_folder (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DC_TYPE_SAVE_FOLDER), NULL);
	return value->data[0].v_pointer;
}

void
dc_value_set_save_folder (GValue* value,
                          gpointer v_object)
{
	DcSaveFolder * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DC_TYPE_SAVE_FOLDER));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, DC_TYPE_SAVE_FOLDER));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		dc_save_folder_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		dc_save_folder_unref (old);
	}
}

void
dc_value_take_save_folder (GValue* value,
                           gpointer v_object)
{
	DcSaveFolder * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DC_TYPE_SAVE_FOLDER));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, DC_TYPE_SAVE_FOLDER));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		dc_save_folder_unref (old);
	}
}

static void
dc_save_folder_class_init (DcSaveFolderClass * klass,
                           gpointer klass_data)
{
	dc_save_folder_parent_class = g_type_class_peek_parent (klass);
	((DcSaveFolderClass *) klass)->finalize = dc_save_folder_finalize;
}

static void
dc_save_folder_instance_init (DcSaveFolder * self,
                              gpointer klass)
{
	self->ref_count = 1;
}

static void
dc_save_folder_finalize (DcSaveFolder * obj)
{
	DcSaveFolder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DC_TYPE_SAVE_FOLDER, DcSaveFolder);
	g_signal_handlers_destroy (self);
}

/** Helpers for saving attachments into a user-chosen folder. */
static GType
dc_save_folder_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { dc_value_save_folder_init, dc_value_save_folder_free_value, dc_value_save_folder_copy_value, dc_value_save_folder_peek_pointer, "p", dc_value_save_folder_collect_value, "p", dc_value_save_folder_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (DcSaveFolderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dc_save_folder_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DcSaveFolder), 0, (GInstanceInitFunc) dc_save_folder_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType dc_save_folder_type_id;
	dc_save_folder_type_id = g_type_register_fundamental (g_type_fundamental_next (), "DcSaveFolder", &g_define_type_info, &g_define_type_fundamental_info, 0);
	return dc_save_folder_type_id;
}

GType
dc_save_folder_get_type (void)
{
	static volatile gsize dc_save_folder_type_id__once = 0;
	if (g_once_init_enter (&dc_save_folder_type_id__once)) {
		GType dc_save_folder_type_id;
		dc_save_folder_type_id = dc_save_folder_get_type_once ();
		g_once_init_leave (&dc_save_folder_type_id__once, dc_save_folder_type_id);
	}
	return dc_save_folder_type_id__once;
}

gpointer
dc_save_folder_ref (gpointer instance)
{
	DcSaveFolder * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
dc_save_folder_unref (gpointer instance)
{
	DcSaveFolder * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		DC_SAVE_FOLDER_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

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;
}

