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

/*
* Copyright (c) 2019 Felipe Escoto (https://github.com/Philip-Scott/Notes-up)
*
* 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 2 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: Felipe Escoto <felescoto95@hotmail.com>
*/

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <sqlite3.h>
#include <granite.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

typedef enum  {
	FILE_DATA_TYPE_FILE_NAME = 0,
	FILE_DATA_TYPE_LAST_NOTEBOOK = 10,
	FILE_DATA_TYPE_LAST_PAGE = 11,
	FILE_DATA_TYPE_STYLESHEET = 21,
	FILE_DATA_TYPE_CUSTOM_CSS = 22
} FileDataType;

#define TYPE_FILE_DATA_TYPE (file_data_type_get_type ())

#define TYPE_DATABASE_TABLE (database_table_get_type ())
#define DATABASE_TABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DATABASE_TABLE, DatabaseTable))
#define DATABASE_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DATABASE_TABLE, DatabaseTableClass))
#define IS_DATABASE_TABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DATABASE_TABLE))
#define IS_DATABASE_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DATABASE_TABLE))
#define DATABASE_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DATABASE_TABLE, DatabaseTableClass))

typedef struct _DatabaseTable DatabaseTable;
typedef struct _DatabaseTableClass DatabaseTableClass;
typedef struct _DatabaseTablePrivate DatabaseTablePrivate;

#define ENOTES_TYPE_FILE_DATA_TABLE (enotes_file_data_table_get_type ())
#define ENOTES_FILE_DATA_TABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_FILE_DATA_TABLE, ENotesFileDataTable))
#define ENOTES_FILE_DATA_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_FILE_DATA_TABLE, ENotesFileDataTableClass))
#define ENOTES_IS_FILE_DATA_TABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_FILE_DATA_TABLE))
#define ENOTES_IS_FILE_DATA_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_FILE_DATA_TABLE))
#define ENOTES_FILE_DATA_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_FILE_DATA_TABLE, ENotesFileDataTableClass))

typedef struct _ENotesFileDataTable ENotesFileDataTable;
typedef struct _ENotesFileDataTableClass ENotesFileDataTableClass;
typedef struct _ENotesFileDataTablePrivate ENotesFileDataTablePrivate;
#define _sqlite3_finalize0(var) ((var == NULL) ? NULL : (var = (sqlite3_finalize (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define ENOTES_TYPE_APPLICATION (enotes_application_get_type ())
#define ENOTES_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_APPLICATION, ENotesApplication))
#define ENOTES_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_APPLICATION, ENotesApplicationClass))
#define ENOTES_IS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_APPLICATION))
#define ENOTES_IS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_APPLICATION))
#define ENOTES_APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_APPLICATION, ENotesApplicationClass))

typedef struct _ENotesApplication ENotesApplication;
typedef struct _ENotesApplicationClass ENotesApplicationClass;
typedef struct _ENotesApplicationPrivate ENotesApplicationPrivate;

#define ENOTES_APPLICATION_TYPE_STATE (enotes_application_state_get_type ())
#define ENOTES_APPLICATION_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_APPLICATION_TYPE_STATE, ENotesApplicationState))
#define ENOTES_APPLICATION_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_APPLICATION_TYPE_STATE, ENotesApplicationStateClass))
#define ENOTES_APPLICATION_IS_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_APPLICATION_TYPE_STATE))
#define ENOTES_APPLICATION_IS_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_APPLICATION_TYPE_STATE))
#define ENOTES_APPLICATION_STATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_APPLICATION_TYPE_STATE, ENotesApplicationStateClass))

typedef struct _ENotesApplicationState ENotesApplicationState;
typedef struct _ENotesApplicationStateClass ENotesApplicationStateClass;
#define _database_table_unref0(var) ((var == NULL) ? NULL : (var = (database_table_unref (var), NULL)))

struct _DatabaseTable {
	GTypeInstance parent_instance;
	volatile int ref_count;
	DatabaseTablePrivate * priv;
	gchar* table_name;
};

struct _DatabaseTableClass {
	GTypeClass parent_class;
	void (*finalize) (DatabaseTable *self);
};

struct _ENotesFileDataTable {
	DatabaseTable parent_instance;
	ENotesFileDataTablePrivate * priv;
};

struct _ENotesFileDataTableClass {
	DatabaseTableClass parent_class;
};

struct _ENotesApplication {
	GraniteApplication parent_instance;
	ENotesApplicationPrivate * priv;
	gboolean running;
	ENotesApplicationState* state;
};

struct _ENotesApplicationClass {
	GraniteApplicationClass parent_class;
};

static gpointer enotes_file_data_table_parent_class = NULL;
static ENotesFileDataTable* enotes_file_data_table__instance;
static ENotesFileDataTable* enotes_file_data_table__instance = NULL;
VALA_EXTERN ENotesApplication* enotes_app;

VALA_EXTERN GType file_data_type_get_type (void) G_GNUC_CONST ;
VALA_EXTERN gpointer database_table_ref (gpointer instance);
VALA_EXTERN void database_table_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_database_table (const gchar* name,
                                       const gchar* nick,
                                       const gchar* blurb,
                                       GType object_type,
                                       GParamFlags flags);
VALA_EXTERN void value_set_database_table (GValue* value,
                               gpointer v_object);
VALA_EXTERN void value_take_database_table (GValue* value,
                                gpointer v_object);
VALA_EXTERN gpointer value_get_database_table (const GValue* value);
VALA_EXTERN GType database_table_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DatabaseTable, database_table_unref)
VALA_EXTERN GType enotes_file_data_table_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesFileDataTable, database_table_unref)
static ENotesFileDataTable* enotes_file_data_table_new (void);
static ENotesFileDataTable* enotes_file_data_table_construct (GType object_type);
VALA_EXTERN DatabaseTable* database_table_construct (GType object_type);
VALA_EXTERN sqlite3_stmt* database_table_create_stmt (const gchar* data);
VALA_EXTERN void database_table_fatal (const gchar* op,
                           gint res);
VALA_EXTERN void database_table_set_table_name (DatabaseTable* self,
                                    const gchar* table_name);
VALA_EXTERN gchar* enotes_file_data_table_get_value (ENotesFileDataTable* self,
                                         FileDataType type);
VALA_EXTERN void database_table_bind_int (sqlite3_stmt* stmt,
                              gint column,
                              gint64 data);
VALA_EXTERN gint64 enotes_file_data_table_get_int64 (ENotesFileDataTable* self,
                                         FileDataType type);
VALA_EXTERN void enotes_file_data_table_set_value (ENotesFileDataTable* self,
                                       FileDataType type,
                                       const gchar* value);
VALA_EXTERN void enotes_file_data_table_set_value_silent (ENotesFileDataTable* self,
                                              FileDataType type,
                                              const gchar* value);
VALA_EXTERN GType enotes_application_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesApplication, g_object_unref)
VALA_EXTERN GType enotes_application_state_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesApplicationState, g_object_unref)
VALA_EXTERN void database_table_bind_text (sqlite3_stmt* stmt,
                               gint column,
                               const gchar* data);
VALA_EXTERN void enotes_file_data_table_reset_instance (void);
VALA_EXTERN ENotesFileDataTable* enotes_file_data_table_get_instance (void);
static void enotes_file_data_table_finalize (DatabaseTable * obj);
static GType enotes_file_data_table_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static GType
file_data_type_get_type_once (void)
{
	static const GEnumValue values[] = {{FILE_DATA_TYPE_FILE_NAME, "FILE_DATA_TYPE_FILE_NAME", "file-name"}, {FILE_DATA_TYPE_LAST_NOTEBOOK, "FILE_DATA_TYPE_LAST_NOTEBOOK", "last-notebook"}, {FILE_DATA_TYPE_LAST_PAGE, "FILE_DATA_TYPE_LAST_PAGE", "last-page"}, {FILE_DATA_TYPE_STYLESHEET, "FILE_DATA_TYPE_STYLESHEET", "stylesheet"}, {FILE_DATA_TYPE_CUSTOM_CSS, "FILE_DATA_TYPE_CUSTOM_CSS", "custom-css"}, {0, NULL, NULL}};
	GType file_data_type_type_id;
	file_data_type_type_id = g_enum_register_static ("FileDataType", values);
	return file_data_type_type_id;
}

GType
file_data_type_get_type (void)
{
	static volatile gsize file_data_type_type_id__once = 0;
	if (g_once_init_enter (&file_data_type_type_id__once)) {
		GType file_data_type_type_id;
		file_data_type_type_id = file_data_type_get_type_once ();
		g_once_init_leave (&file_data_type_type_id__once, file_data_type_type_id);
	}
	return file_data_type_type_id__once;
}

static ENotesFileDataTable*
enotes_file_data_table_construct (GType object_type)
{
	ENotesFileDataTable* self = NULL;
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	gint res = 0;
	sqlite3_stmt* _tmp1_;
	self = (ENotesFileDataTable*) database_table_construct (object_type);
	_tmp0_ = database_table_create_stmt ("CREATE TABLE IF NOT EXISTS FileData (" "id INTEGER UNIQUE PRIMARY KEY, " "value TEXT)");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	res = sqlite3_step (_tmp1_);
	if (res != SQLITE_DONE) {
		database_table_fatal ("create bookmark table", res);
	}
	database_table_set_table_name ((DatabaseTable*) self, "FileData");
	_sqlite3_finalize0 (stmt);
	return self;
}

static ENotesFileDataTable*
enotes_file_data_table_new (void)
{
	return enotes_file_data_table_construct (ENOTES_TYPE_FILE_DATA_TABLE);
}

gchar*
enotes_file_data_table_get_value (ENotesFileDataTable* self,
                                  FileDataType type)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	sqlite3_stmt* _tmp1_;
	gint res = 0;
	sqlite3_stmt* _tmp2_;
	sqlite3_stmt* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = database_table_create_stmt ("SELECT value FROM FileData WHERE id=?");
	stmt = _tmp0_;
	_tmp1_ = stmt;
	database_table_bind_int (_tmp1_, 1, (gint64) type);
	_tmp2_ = stmt;
	res = sqlite3_step (_tmp2_);
	if (res == SQLITE_DONE) {
		result = NULL;
		_sqlite3_finalize0 (stmt);
		return result;
	}
	_tmp3_ = stmt;
	_tmp4_ = (const gchar*) sqlite3_column_text (_tmp3_, 0);
	_tmp5_ = g_strdup (_tmp4_);
	result = _tmp5_;
	_sqlite3_finalize0 (stmt);
	return result;
}

static gint64
int64_parse (const gchar* str,
             guint _base)
{
	gint64 result;
	g_return_val_if_fail (str != NULL, -1LL);
	result = g_ascii_strtoll (str, NULL, _base);
	return result;
}

gint64
enotes_file_data_table_get_int64 (ENotesFileDataTable* self,
                                  FileDataType type)
{
	gchar* value = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	gint64 result;
	g_return_val_if_fail (self != NULL, -1LL);
	_tmp0_ = enotes_file_data_table_get_value (self, type);
	value = _tmp0_;
	_tmp1_ = value;
	if (_tmp1_ == NULL) {
		result = (gint64) 0;
		_g_free0 (value);
		return result;
	} else {
		const gchar* _tmp2_;
		_tmp2_ = value;
		result = int64_parse (_tmp2_, (guint) 0);
		_g_free0 (value);
		return result;
	}
}

void
enotes_file_data_table_set_value (ENotesFileDataTable* self,
                                  FileDataType type,
                                  const gchar* value)
{
	ENotesApplication* _tmp0_;
	ENotesApplicationState* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (value != NULL);
	enotes_file_data_table_set_value_silent (self, type, value);
	_tmp0_ = enotes_app;
	_tmp1_ = _tmp0_->state;
	g_signal_emit_by_name (_tmp1_, "file-data-changed", type, value);
}

void
enotes_file_data_table_set_value_silent (ENotesFileDataTable* self,
                                         FileDataType type,
                                         const gchar* value)
{
	sqlite3_stmt* stmt = NULL;
	sqlite3_stmt* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (value != NULL);
	_tmp0_ = database_table_create_stmt ("INSERT OR REPLACE INTO FileData" "(id, value) VALUES(?, ?)");
	stmt = _tmp0_;
	database_table_bind_int (stmt, 1, (gint64) type);
	database_table_bind_text (stmt, 2, value);
	sqlite3_step (stmt);
	_sqlite3_finalize0 (stmt);
}

void
enotes_file_data_table_reset_instance (void)
{
	_database_table_unref0 (enotes_file_data_table__instance);
	enotes_file_data_table__instance = NULL;
}

ENotesFileDataTable*
enotes_file_data_table_get_instance (void)
{
	ENotesFileDataTable* result;
	ENotesFileDataTable* _tmp0_;
	ENotesFileDataTable* _tmp2_;
	_tmp0_ = enotes_file_data_table__instance;
	if (_tmp0_ == NULL) {
		ENotesFileDataTable* _tmp1_;
		_tmp1_ = enotes_file_data_table_new ();
		_database_table_unref0 (enotes_file_data_table__instance);
		enotes_file_data_table__instance = _tmp1_;
	}
	_tmp2_ = enotes_file_data_table__instance;
	result = _tmp2_;
	return result;
}

static void
enotes_file_data_table_class_init (ENotesFileDataTableClass * klass,
                                   gpointer klass_data)
{
	enotes_file_data_table_parent_class = g_type_class_peek_parent (klass);
	((DatabaseTableClass *) klass)->finalize = enotes_file_data_table_finalize;
}

static void
enotes_file_data_table_instance_init (ENotesFileDataTable * self,
                                      gpointer klass)
{
}

static void
enotes_file_data_table_finalize (DatabaseTable * obj)
{
	ENotesFileDataTable * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_FILE_DATA_TABLE, ENotesFileDataTable);
	DATABASE_TABLE_CLASS (enotes_file_data_table_parent_class)->finalize (obj);
}

static GType
enotes_file_data_table_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ENotesFileDataTableClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) enotes_file_data_table_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ENotesFileDataTable), 0, (GInstanceInitFunc) enotes_file_data_table_instance_init, NULL };
	GType enotes_file_data_table_type_id;
	enotes_file_data_table_type_id = g_type_register_static (TYPE_DATABASE_TABLE, "ENotesFileDataTable", &g_define_type_info, 0);
	return enotes_file_data_table_type_id;
}

GType
enotes_file_data_table_get_type (void)
{
	static volatile gsize enotes_file_data_table_type_id__once = 0;
	if (g_once_init_enter (&enotes_file_data_table_type_id__once)) {
		GType enotes_file_data_table_type_id;
		enotes_file_data_table_type_id = enotes_file_data_table_get_type_once ();
		g_once_init_leave (&enotes_file_data_table_type_id__once, enotes_file_data_table_type_id);
	}
	return enotes_file_data_table_type_id__once;
}

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

