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

#include <webkit2/webkit2.h>
#include <glib-object.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <float.h>
#include <math.h>

#define PDFPC_PATHS_SOURCE_PATH "/build/pdfpc/src/pdfpc-4.7.0"
#define PDFPC_PATHS_SHARE_PATH "/usr/share/pdfpc"
#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 PDFPC_VIEW_TYPE_MARKDOWN_VIEW (pdfpc_view_markdown_view_get_type ())
#define PDFPC_VIEW_MARKDOWN_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PDFPC_VIEW_TYPE_MARKDOWN_VIEW, pdfpcViewMarkdownView))
#define PDFPC_VIEW_MARKDOWN_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDFPC_VIEW_TYPE_MARKDOWN_VIEW, pdfpcViewMarkdownViewClass))
#define PDFPC_VIEW_IS_MARKDOWN_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PDFPC_VIEW_TYPE_MARKDOWN_VIEW))
#define PDFPC_VIEW_IS_MARKDOWN_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDFPC_VIEW_TYPE_MARKDOWN_VIEW))
#define PDFPC_VIEW_MARKDOWN_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDFPC_VIEW_TYPE_MARKDOWN_VIEW, pdfpcViewMarkdownViewClass))

typedef struct _pdfpcViewMarkdownView pdfpcViewMarkdownView;
typedef struct _pdfpcViewMarkdownViewClass pdfpcViewMarkdownViewClass;
typedef struct _pdfpcViewMarkdownViewPrivate pdfpcViewMarkdownViewPrivate;
enum  {
	PDFPC_VIEW_MARKDOWN_VIEW_0_PROPERTY,
	PDFPC_VIEW_MARKDOWN_VIEW_NUM_PROPERTIES
};
static GParamSpec* pdfpc_view_markdown_view_properties[PDFPC_VIEW_MARKDOWN_VIEW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _webkit_user_style_sheet_unref0(var) ((var == NULL) ? NULL : (var = (webkit_user_style_sheet_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _pdfpcViewMarkdownView {
	WebKitWebView parent_instance;
	pdfpcViewMarkdownViewPrivate * priv;
};

struct _pdfpcViewMarkdownViewClass {
	WebKitWebViewClass parent_class;
};

struct _pdfpcViewMarkdownViewPrivate {
	WebKitUserContentManager* ucm;
};

static gint pdfpcViewMarkdownView_private_offset;
static gpointer pdfpc_view_markdown_view_parent_class = NULL;
VALA_EXTERN gboolean pdfpc_options_no_install;

VALA_EXTERN GType pdfpc_view_markdown_view_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (pdfpcViewMarkdownView, g_object_unref)
VALA_EXTERN pdfpcViewMarkdownView* pdfpc_view_markdown_view_new (void);
VALA_EXTERN pdfpcViewMarkdownView* pdfpc_view_markdown_view_construct (GType object_type);
static gboolean pdfpc_view_markdown_view_real_context_menu (WebKitWebView* base,
                                                     WebKitContextMenu* m,
                                                     GdkEvent* e,
                                                     WebKitHitTestResult* r);
VALA_EXTERN void pdfpc_view_markdown_view_render (pdfpcViewMarkdownView* self,
                                      const gchar* text,
                                      gboolean plain_text);
VALA_EXTERN gchar* pdfpc_renderer_md_render (const gchar* text,
                                 gboolean plain_text);
VALA_EXTERN void pdfpc_view_markdown_view_apply_zoom (pdfpcViewMarkdownView* self,
                                          gdouble level);
static void pdfpc_view_markdown_view_finalize (GObject * obj);
static GType pdfpc_view_markdown_view_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
pdfpc_view_markdown_view_get_instance_private (pdfpcViewMarkdownView* self)
{
	return G_STRUCT_MEMBER_P (self, pdfpcViewMarkdownView_private_offset);
}

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

pdfpcViewMarkdownView*
pdfpc_view_markdown_view_construct (GType object_type)
{
	pdfpcViewMarkdownView * self = NULL;
	WebKitUserContentManager* _tmp0_;
	WebKitUserContentManager* _tmp1_;
	gchar* css_path = NULL;
	WebKitSettings* mdsettings = NULL;
	WebKitSettings* _tmp14_;
	WebKitSettings* _tmp15_;
	GError* _inner_error0_ = NULL;
	self = (pdfpcViewMarkdownView*) g_object_new (object_type, NULL);
	_tmp0_ = webkit_web_view_get_user_content_manager ((WebKitWebView*) self);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->ucm);
	self->priv->ucm = _tmp1_;
	if (pdfpc_options_no_install) {
		gchar* _tmp2_;
		_tmp2_ = g_build_filename (PDFPC_PATHS_SOURCE_PATH, "css/notes.css", NULL);
		_g_free0 (css_path);
		css_path = _tmp2_;
	} else {
		gchar* _tmp3_;
		_tmp3_ = g_build_filename (PDFPC_PATHS_SHARE_PATH, "css/notes.css", NULL);
		_g_free0 (css_path);
		css_path = _tmp3_;
	}
	{
		GFile* css_file = NULL;
		const gchar* _tmp4_;
		GFile* _tmp5_;
		guint8* css_contents = NULL;
		gint css_contents_length1 = 0;
		gint _css_contents_size_ = 0;
		guint8* _tmp6_ = NULL;
		gsize _tmp7_ = 0;
		WebKitUserStyleSheet* ss = NULL;
		WebKitUserStyleSheet* _tmp8_;
		WebKitUserContentManager* _tmp9_;
		WebKitUserStyleSheet* _tmp10_;
		_tmp4_ = css_path;
		_tmp5_ = g_file_new_for_path (_tmp4_);
		css_file = _tmp5_;
		g_file_load_contents (css_file, NULL, &_tmp6_, &_tmp7_, NULL, &_inner_error0_);
		css_contents = (g_free (css_contents), NULL);
		css_contents = _tmp6_;
		css_contents_length1 = _tmp7_;
		_css_contents_size_ = css_contents_length1;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			css_contents = (g_free (css_contents), NULL);
			_g_object_unref0 (css_file);
			goto __catch0_g_error;
		}
		_tmp8_ = webkit_user_style_sheet_new ((const gchar*) css_contents, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL);
		ss = _tmp8_;
		_tmp9_ = self->priv->ucm;
		_tmp10_ = ss;
		webkit_user_content_manager_add_style_sheet (_tmp9_, _tmp10_);
		_webkit_user_style_sheet_unref0 (ss);
		css_contents = (g_free (css_contents), NULL);
		_g_object_unref0 (css_file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		const gchar* _tmp11_;
		GError* _tmp12_;
		const gchar* _tmp13_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp11_ = css_path;
		_tmp12_ = e;
		_tmp13_ = _tmp12_->message;
		g_printerr ("Warning: Could not load CSS %s (%s)\n", _tmp11_, _tmp13_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (css_path);
		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;
	}
	_tmp14_ = webkit_web_view_get_settings ((WebKitWebView*) self);
	_tmp15_ = _g_object_ref0 (_tmp14_);
	mdsettings = _tmp15_;
	webkit_settings_set_enable_javascript (mdsettings, FALSE);
	_g_object_unref0 (mdsettings);
	_g_free0 (css_path);
	return self;
}

pdfpcViewMarkdownView*
pdfpc_view_markdown_view_new (void)
{
	return pdfpc_view_markdown_view_construct (PDFPC_VIEW_TYPE_MARKDOWN_VIEW);
}

static gboolean
pdfpc_view_markdown_view_real_context_menu (WebKitWebView* base,
                                            WebKitContextMenu* m,
                                            GdkEvent* e,
                                            WebKitHitTestResult* r)
{
	pdfpcViewMarkdownView * self;
	gboolean result;
	self = (pdfpcViewMarkdownView*) base;
	g_return_val_if_fail (m != NULL, FALSE);
	g_return_val_if_fail (e != NULL, FALSE);
	g_return_val_if_fail (r != NULL, FALSE);
	result = TRUE;
	return result;
}

void
pdfpc_view_markdown_view_render (pdfpcViewMarkdownView* self,
                                 const gchar* text,
                                 gboolean plain_text)
{
	gchar* doc = NULL;
	gchar* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = pdfpc_renderer_md_render (text, plain_text);
	doc = _tmp0_;
	webkit_web_view_load_html ((WebKitWebView*) self, doc, NULL);
	_g_free0 (doc);
}

void
pdfpc_view_markdown_view_apply_zoom (pdfpcViewMarkdownView* self,
                                     gdouble level)
{
	gchar* css_contents = NULL;
	gchar* _tmp0_;
	WebKitUserStyleSheet* ss = NULL;
	WebKitUserStyleSheet* _tmp1_;
	WebKitUserContentManager* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_strdup_printf ("body {zoom: %d%%;}", (gint) (100 * level));
	css_contents = _tmp0_;
	_tmp1_ = webkit_user_style_sheet_new ((const gchar*) css_contents, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL);
	ss = _tmp1_;
	_tmp2_ = self->priv->ucm;
	webkit_user_content_manager_add_style_sheet (_tmp2_, ss);
	_webkit_user_style_sheet_unref0 (ss);
	_g_free0 (css_contents);
}

static void
pdfpc_view_markdown_view_class_init (pdfpcViewMarkdownViewClass * klass,
                                     gpointer klass_data)
{
	pdfpc_view_markdown_view_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &pdfpcViewMarkdownView_private_offset);
	((WebKitWebViewClass *) klass)->context_menu = (gboolean (*) (WebKitWebView*, WebKitContextMenu*, GdkEvent*, WebKitHitTestResult*)) pdfpc_view_markdown_view_real_context_menu;
	G_OBJECT_CLASS (klass)->finalize = pdfpc_view_markdown_view_finalize;
}

static void
pdfpc_view_markdown_view_instance_init (pdfpcViewMarkdownView * self,
                                        gpointer klass)
{
	self->priv = pdfpc_view_markdown_view_get_instance_private (self);
}

static void
pdfpc_view_markdown_view_finalize (GObject * obj)
{
	pdfpcViewMarkdownView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PDFPC_VIEW_TYPE_MARKDOWN_VIEW, pdfpcViewMarkdownView);
	_g_object_unref0 (self->priv->ucm);
	G_OBJECT_CLASS (pdfpc_view_markdown_view_parent_class)->finalize (obj);
}

static GType
pdfpc_view_markdown_view_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (pdfpcViewMarkdownViewClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pdfpc_view_markdown_view_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (pdfpcViewMarkdownView), 0, (GInstanceInitFunc) pdfpc_view_markdown_view_instance_init, NULL };
	GType pdfpc_view_markdown_view_type_id;
	pdfpc_view_markdown_view_type_id = g_type_register_static (webkit_web_view_get_type (), "pdfpcViewMarkdownView", &g_define_type_info, 0);
	pdfpcViewMarkdownView_private_offset = g_type_add_instance_private (pdfpc_view_markdown_view_type_id, sizeof (pdfpcViewMarkdownViewPrivate));
	return pdfpc_view_markdown_view_type_id;
}

GType
pdfpc_view_markdown_view_get_type (void)
{
	static volatile gsize pdfpc_view_markdown_view_type_id__once = 0;
	if (g_once_init_enter (&pdfpc_view_markdown_view_type_id__once)) {
		GType pdfpc_view_markdown_view_type_id;
		pdfpc_view_markdown_view_type_id = pdfpc_view_markdown_view_get_type_once ();
		g_once_init_leave (&pdfpc_view_markdown_view_type_id__once, pdfpc_view_markdown_view_type_id);
	}
	return pdfpc_view_markdown_view_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;
}

