/* PageLink.c generated by valac 0.56.19, the Vala compiler
 * generated from PageLink.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 <gtk/gtk.h>
#include <gee.h>
#include <glib/gi18n-lib.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 ENOTES_TYPE_PLUGIN (enotes_plugin_get_type ())
#define ENOTES_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_PLUGIN, ENotesPlugin))
#define ENOTES_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_PLUGIN, ENotesPluginClass))
#define ENOTES_IS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_PLUGIN))
#define ENOTES_IS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_PLUGIN))
#define ENOTES_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_PLUGIN, ENotesPluginClass))

typedef struct _ENotesPlugin ENotesPlugin;
typedef struct _ENotesPluginClass ENotesPluginClass;
typedef struct _ENotesPluginPrivate ENotesPluginPrivate;

#define TYPE_BL_MEMBER (bl_member_get_type ())
#define BL_MEMBER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_BL_MEMBER, BLMember))
#define BL_MEMBER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_BL_MEMBER, BLMemberClass))
#define IS_BL_MEMBER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_BL_MEMBER))
#define IS_BL_MEMBER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_BL_MEMBER))
#define BL_MEMBER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_BL_MEMBER, BLMemberClass))

typedef struct _BLMember BLMember;
typedef struct _BLMemberClass BLMemberClass;

#define ENOTES_TYPE_PAGE_LINK (enotes_page_link_get_type ())
#define ENOTES_PAGE_LINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_PAGE_LINK, ENotesPageLink))
#define ENOTES_PAGE_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_PAGE_LINK, ENotesPageLinkClass))
#define ENOTES_IS_PAGE_LINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_PAGE_LINK))
#define ENOTES_IS_PAGE_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_PAGE_LINK))
#define ENOTES_PAGE_LINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_PAGE_LINK, ENotesPageLinkClass))

typedef struct _ENotesPageLink ENotesPageLink;
typedef struct _ENotesPageLinkClass ENotesPageLinkClass;
typedef struct _ENotesPageLinkPrivate ENotesPageLinkPrivate;
enum  {
	ENOTES_PAGE_LINK_0_PROPERTY,
	ENOTES_PAGE_LINK_NUM_PROPERTIES
};
static GParamSpec* enotes_page_link_properties[ENOTES_PAGE_LINK_NUM_PROPERTIES];
#define _g_pattern_spec_free0(var) ((var == NULL) ? NULL : (var = (g_pattern_spec_free (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _bl_member_unref0(var) ((var == NULL) ? NULL : (var = (bl_member_unref (var), NULL)))

struct _ENotesPlugin {
	GObject parent_instance;
	ENotesPluginPrivate * priv;
	gboolean state;
	gchar* code_name;
};

struct _ENotesPluginClass {
	GObjectClass parent_class;
	gchar* (*get_desctiption) (ENotesPlugin* self);
	gchar* (*get_name) (ENotesPlugin* self);
	gboolean (*has_match) (ENotesPlugin* self, const gchar* text);
	gchar* (*convert) (ENotesPlugin* self, const gchar* line);
	GtkWidget* (*editor_button) (ENotesPlugin* self);
	gchar* (*get_button_desctiption) (ENotesPlugin* self);
	gchar* (*request_string) (ENotesPlugin* self, const gchar* selection);
	GeeList* (*get_blacklist_members) (ENotesPlugin* self);
};

struct _ENotesPageLink {
	ENotesPlugin parent_instance;
	ENotesPageLinkPrivate * priv;
};

struct _ENotesPageLinkClass {
	ENotesPluginClass parent_class;
};

struct _ENotesPageLinkPrivate {
	GPatternSpec* spec;
	GRegex* regex;
};

static gint ENotesPageLink_private_offset;
static gpointer enotes_page_link_parent_class = NULL;

VALA_EXTERN GType enotes_plugin_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesPlugin, g_object_unref)
VALA_EXTERN gpointer bl_member_ref (gpointer instance);
VALA_EXTERN void bl_member_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_bl_member (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_bl_member (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_bl_member (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_bl_member (const GValue* value);
VALA_EXTERN GType bl_member_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BLMember, bl_member_unref)
VALA_EXTERN GType enotes_page_link_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesPageLink, g_object_unref)
VALA_EXTERN gchar* enotes_plugin_get_desctiption (ENotesPlugin* self);
VALA_EXTERN gchar* enotes_plugin_get_name (ENotesPlugin* self);
VALA_EXTERN GtkWidget* enotes_plugin_editor_button (ENotesPlugin* self);
VALA_EXTERN gchar* enotes_plugin_request_string (ENotesPlugin* self,
                                     const gchar* selection);
VALA_EXTERN gchar* enotes_plugin_get_button_desctiption (ENotesPlugin* self);
VALA_EXTERN gboolean enotes_plugin_has_match (ENotesPlugin* self,
                                  const gchar* text);
VALA_EXTERN gchar* enotes_plugin_convert (ENotesPlugin* self,
                              const gchar* line);
VALA_EXTERN GeeList* enotes_plugin_get_blacklist_members (ENotesPlugin* self);
static gchar* enotes_page_link_real_get_desctiption (ENotesPlugin* base);
static gchar* enotes_page_link_real_get_name (ENotesPlugin* base);
static GtkWidget* enotes_page_link_real_editor_button (ENotesPlugin* base);
static gchar* enotes_page_link_real_request_string (ENotesPlugin* base,
                                             const gchar* selection);
static gchar* enotes_page_link_real_get_button_desctiption (ENotesPlugin* base);
static gboolean enotes_page_link_real_has_match (ENotesPlugin* base,
                                          const gchar* text);
static gchar* enotes_page_link_real_convert (ENotesPlugin* base,
                                      const gchar* _line);
static gboolean ___lambda7_ (ENotesPageLink* self,
                      GMatchInfo* match_info,
                      GString* data);
static gboolean ____lambda7__gregex_eval_callback (GMatchInfo* match_info,
                                            GString* _result_,
                                            gpointer self);
static GeeList* enotes_page_link_real_get_blacklist_members (ENotesPlugin* base);
VALA_EXTERN BLMember* bl_member_new (GRegex* reg,
                         const gchar* replace);
VALA_EXTERN BLMember* bl_member_construct (GType object_type,
                               GRegex* reg,
                               const gchar* replace);
VALA_EXTERN ENotesPageLink* enotes_page_link_new (void);
VALA_EXTERN ENotesPageLink* enotes_page_link_construct (GType object_type);
VALA_EXTERN ENotesPlugin* enotes_plugin_construct (GType object_type);
static GObject * enotes_page_link_constructor (GType type,
                                        guint n_construct_properties,
                                        GObjectConstructParam * construct_properties);
static void enotes_page_link_finalize (GObject * obj);
static GType enotes_page_link_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static GRegex* _tmp_regex_0 = NULL;

static inline gpointer
enotes_page_link_get_instance_private (ENotesPageLink* self)
{
	return G_STRUCT_MEMBER_P (self, ENotesPageLink_private_offset);
}

static gchar*
enotes_page_link_real_get_desctiption (ENotesPlugin* base)
{
	ENotesPageLink * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesPageLink*) base;
	_tmp0_ = g_strdup (_ ("Open a page in Notes-Up"));
	result = _tmp0_;
	return result;
}

static gchar*
enotes_page_link_real_get_name (ENotesPlugin* base)
{
	ENotesPageLink * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesPageLink*) base;
	_tmp0_ = g_strdup (_ ("Link to Page"));
	result = _tmp0_;
	return result;
}

static GtkWidget*
enotes_page_link_real_editor_button (ENotesPlugin* base)
{
	ENotesPageLink * self;
	GtkWidget* result;
	self = (ENotesPageLink*) base;
	result = NULL;
	return result;
}

static gchar*
enotes_page_link_real_request_string (ENotesPlugin* base,
                                      const gchar* selection)
{
	ENotesPageLink * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesPageLink*) base;
	g_return_val_if_fail (selection != NULL, NULL);
	_tmp0_ = g_strdup (selection);
	result = _tmp0_;
	return result;
}

static gchar*
enotes_page_link_real_get_button_desctiption (ENotesPlugin* base)
{
	ENotesPageLink * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesPageLink*) base;
	_tmp0_ = g_strdup ("");
	result = _tmp0_;
	return result;
}

static gboolean
enotes_page_link_real_has_match (ENotesPlugin* base,
                                 const gchar* text)
{
	ENotesPageLink * self;
	GPatternSpec* _tmp0_;
	gboolean result;
	self = (ENotesPageLink*) base;
	g_return_val_if_fail (text != NULL, FALSE);
	_tmp0_ = self->priv->spec;
	result = g_pattern_match_string (_tmp0_, text);
	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;
}

static gboolean
___lambda7_ (ENotesPageLink* self,
             GMatchInfo* match_info,
             GString* data)
{
	gint64 page_id = 0LL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint64 _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gboolean result;
	g_return_val_if_fail (match_info != NULL, FALSE);
	g_return_val_if_fail (data != NULL, FALSE);
	_tmp0_ = g_match_info_fetch (match_info, 1);
	_tmp1_ = _tmp0_;
	_tmp2_ = int64_parse (_tmp1_, (guint) 0);
	_g_free0 (_tmp1_);
	page_id = _tmp2_;
	_tmp3_ = g_strdup_printf ("<a href=\"notes-up:///%lld\">", page_id);
	_tmp4_ = _tmp3_;
	g_string_append (data, _tmp4_);
	_g_free0 (_tmp4_);
	_tmp5_ = g_match_info_fetch (match_info, 2);
	_tmp6_ = _tmp5_;
	g_string_append (data, _tmp6_);
	_g_free0 (_tmp6_);
	g_string_append (data, "</a>");
	result = FALSE;
	return result;
}

static gboolean
____lambda7__gregex_eval_callback (GMatchInfo* match_info,
                                   GString* _result_,
                                   gpointer self)
{
	gboolean result;
	result = ___lambda7_ ((ENotesPageLink*) self, match_info, _result_);
	return result;
}

static gchar*
enotes_page_link_real_convert (ENotesPlugin* base,
                               const gchar* _line)
{
	ENotesPageLink * self;
	GRegex* _tmp0_;
	gchar* _tmp7_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	self = (ENotesPageLink*) base;
	g_return_val_if_fail (_line != NULL, NULL);
	_tmp0_ = self->priv->regex;
	if (g_regex_match (_tmp0_, _line, 0, NULL)) {
		gchar* _tmp1_ = NULL;
		GRegex* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		_tmp2_ = self->priv->regex;
		_tmp3_ = strlen (_line);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_replace_eval (_tmp2_, _line, (gssize) _tmp4_, 0, 0, ____lambda7__gregex_eval_callback, self, &_inner_error0_);
		_tmp1_ = _tmp5_;
		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 NULL;
		}
		_tmp6_ = _tmp1_;
		_tmp1_ = NULL;
		result = _tmp6_;
		_g_free0 (_tmp1_);
		return result;
	}
	_tmp7_ = g_strdup (_line);
	result = _tmp7_;
	return result;
}

static inline GRegex*
_thread_safe_regex_init (GRegex** re,
                         const gchar * pattern,
                         GRegexCompileFlags compile_flags)
{
	if (g_once_init_enter ((volatile gsize*) re)) {
		GRegex* val = g_regex_new (pattern, compile_flags, 0, NULL);
		g_once_init_leave ((volatile gsize*) re, (gsize) val);
	}
	return *re;
}

static GeeList*
enotes_page_link_real_get_blacklist_members (ENotesPlugin* base)
{
	ENotesPageLink * self;
	GeeLinkedList* list = NULL;
	GeeLinkedList* _tmp0_;
	BLMember* _tmp1_;
	BLMember* _tmp2_;
	GeeList* result;
	self = (ENotesPageLink*) base;
	_tmp0_ = gee_linked_list_new (TYPE_BL_MEMBER, (GBoxedCopyFunc) bl_member_ref, (GDestroyNotify) bl_member_unref, NULL, NULL, NULL);
	list = _tmp0_;
	_tmp1_ = bl_member_new (_thread_safe_regex_init (&_tmp_regex_0, "<page \\d+>", 0), "");
	_tmp2_ = _tmp1_;
	gee_abstract_collection_add ((GeeAbstractCollection*) list, _tmp2_);
	_bl_member_unref0 (_tmp2_);
	result = (GeeList*) list;
	return result;
}

ENotesPageLink*
enotes_page_link_construct (GType object_type)
{
	ENotesPageLink * self = NULL;
	self = (ENotesPageLink*) enotes_plugin_construct (object_type);
	return self;
}

ENotesPageLink*
enotes_page_link_new (void)
{
	return enotes_page_link_construct (ENOTES_TYPE_PAGE_LINK);
}

static GObject *
enotes_page_link_constructor (GType type,
                              guint n_construct_properties,
                              GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ENotesPageLink * self;
	GRegex* _tmp0_ = NULL;
	GRegex* _tmp1_;
	GRegex* _tmp2_;
	GError* _inner_error0_ = NULL;
	parent_class = G_OBJECT_CLASS (enotes_page_link_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_PAGE_LINK, ENotesPageLink);
	_tmp1_ = g_regex_new ("<page\\s([0-9]+)\\s([^>]{1,})>", 0, 0, &_inner_error0_);
	_tmp0_ = _tmp1_;
	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_);
	}
	_tmp2_ = _tmp0_;
	_tmp0_ = NULL;
	_g_regex_unref0 (self->priv->regex);
	self->priv->regex = _tmp2_;
	_g_regex_unref0 (_tmp0_);
	return obj;
}

static void
enotes_page_link_class_init (ENotesPageLinkClass * klass,
                             gpointer klass_data)
{
	enotes_page_link_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ENotesPageLink_private_offset);
	((ENotesPluginClass *) klass)->get_desctiption = (gchar* (*) (ENotesPlugin*)) enotes_page_link_real_get_desctiption;
	((ENotesPluginClass *) klass)->get_name = (gchar* (*) (ENotesPlugin*)) enotes_page_link_real_get_name;
	((ENotesPluginClass *) klass)->editor_button = (GtkWidget* (*) (ENotesPlugin*)) enotes_page_link_real_editor_button;
	((ENotesPluginClass *) klass)->request_string = (gchar* (*) (ENotesPlugin*, const gchar*)) enotes_page_link_real_request_string;
	((ENotesPluginClass *) klass)->get_button_desctiption = (gchar* (*) (ENotesPlugin*)) enotes_page_link_real_get_button_desctiption;
	((ENotesPluginClass *) klass)->has_match = (gboolean (*) (ENotesPlugin*, const gchar*)) enotes_page_link_real_has_match;
	((ENotesPluginClass *) klass)->convert = (gchar* (*) (ENotesPlugin*, const gchar*)) enotes_page_link_real_convert;
	((ENotesPluginClass *) klass)->get_blacklist_members = (GeeList* (*) (ENotesPlugin*)) enotes_page_link_real_get_blacklist_members;
	G_OBJECT_CLASS (klass)->constructor = enotes_page_link_constructor;
	G_OBJECT_CLASS (klass)->finalize = enotes_page_link_finalize;
}

static void
enotes_page_link_instance_init (ENotesPageLink * self,
                                gpointer klass)
{
	GPatternSpec* _tmp0_;
	self->priv = enotes_page_link_get_instance_private (self);
	_tmp0_ = g_pattern_spec_new ("*<page *>*");
	self->priv->spec = _tmp0_;
}

static void
enotes_page_link_finalize (GObject * obj)
{
	ENotesPageLink * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_PAGE_LINK, ENotesPageLink);
	_g_pattern_spec_free0 (self->priv->spec);
	_g_regex_unref0 (self->priv->regex);
	G_OBJECT_CLASS (enotes_page_link_parent_class)->finalize (obj);
}

static GType
enotes_page_link_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ENotesPageLinkClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) enotes_page_link_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ENotesPageLink), 0, (GInstanceInitFunc) enotes_page_link_instance_init, NULL };
	GType enotes_page_link_type_id;
	enotes_page_link_type_id = g_type_register_static (ENOTES_TYPE_PLUGIN, "ENotesPageLink", &g_define_type_info, 0);
	ENotesPageLink_private_offset = g_type_add_instance_private (enotes_page_link_type_id, sizeof (ENotesPageLinkPrivate));
	return enotes_page_link_type_id;
}

GType
enotes_page_link_get_type (void)
{
	static volatile gsize enotes_page_link_type_id__once = 0;
	if (g_once_init_enter (&enotes_page_link_type_id__once)) {
		GType enotes_page_link_type_id;
		enotes_page_link_type_id = enotes_page_link_get_type_once ();
		g_once_init_leave (&enotes_page_link_type_id__once, enotes_page_link_type_id);
	}
	return enotes_page_link_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;
}

