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

/*
Copyright (C) 2014 Johan Mattsson

This library is free software; you can redistribute it and/or modify 
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation; either version 3 of the 
License, or (at your option) any later version.

This library 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 
Lesser General Public License for more details.
*/

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

enum  {
	BIRD_FONT_SAVE_CALLBACK_0_PROPERTY,
	BIRD_FONT_SAVE_CALLBACK_NUM_PROPERTIES
};
static GParamSpec* bird_font_save_callback_properties[BIRD_FONT_SAVE_CALLBACK_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	BIRD_FONT_SAVE_CALLBACK_FILE_SAVED_SIGNAL,
	BIRD_FONT_SAVE_CALLBACK_NUM_SIGNALS
};
static guint bird_font_save_callback_signals[BIRD_FONT_SAVE_CALLBACK_NUM_SIGNALS] = {0};

static gpointer bird_font_save_callback_parent_class = NULL;

static void __lambda10_ (BirdFontSaveCallback* self);
static void ___lambda10__bird_font_save_callback_file_saved (BirdFontSaveCallback* _sender,
                                                      gpointer self);
static void __lambda11_ (BirdFontSaveCallback* self,
                  const gchar* fn);
static void ___lambda11__bird_font_file_chooser_file_selected (BirdFontFileChooser* _sender,
                                                        const gchar* path,
                                                        gpointer self);
static void bird_font_save_callback_finalize (GObject * obj);
static GType bird_font_save_callback_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static void
__lambda10_ (BirdFontSaveCallback* self)
{
	self->is_done = TRUE;
}

static void
___lambda10__bird_font_save_callback_file_saved (BirdFontSaveCallback* _sender,
                                                 gpointer self)
{
	__lambda10_ ((BirdFontSaveCallback*) self);
}

BirdFontSaveCallback*
bird_font_save_callback_construct (GType object_type)
{
	BirdFontSaveCallback * self = NULL;
	self = (BirdFontSaveCallback*) g_object_new (object_type, NULL);
	g_signal_connect_object (self, "file-saved", (GCallback) ___lambda10__bird_font_save_callback_file_saved, self, 0);
	return self;
}

BirdFontSaveCallback*
bird_font_save_callback_new (void)
{
	return bird_font_save_callback_construct (BIRD_FONT_TYPE_SAVE_CALLBACK);
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static void
__lambda11_ (BirdFontSaveCallback* self,
             const gchar* fn)
{
	gchar* f = NULL;
	GFile* file = NULL;
	BirdFontOverwriteBfFile* dialog = NULL;
	gchar* file_name = NULL;
	self->is_done = TRUE;
	if (fn != NULL) {
		gchar* _tmp0_;
		gboolean _tmp1_ = FALSE;
		const gchar* _tmp2_;
		const gchar* _tmp7_;
		const gchar* _tmp8_;
		gchar* _tmp9_;
		const gchar* _tmp10_;
		GFile* _tmp11_;
		GFile* _tmp12_;
		gchar* _tmp13_;
		GFile* _tmp14_;
		_tmp0_ = g_strdup ((const gchar*) fn);
		_g_free0 (f);
		f = _tmp0_;
		_tmp2_ = f;
		if (!g_str_has_suffix (_tmp2_, ".bf")) {
			const gchar* _tmp3_;
			_tmp3_ = f;
			_tmp1_ = !g_str_has_suffix (_tmp3_, ".birdfont");
		} else {
			_tmp1_ = FALSE;
		}
		if (_tmp1_) {
			const gchar* _tmp4_;
			const gchar* _tmp5_;
			gchar* _tmp6_;
			_tmp4_ = f;
			_tmp5_ = string_to_string (_tmp4_);
			_tmp6_ = g_strconcat (_tmp5_, ".birdfont", NULL);
			_g_free0 (f);
			f = _tmp6_;
		}
		_tmp7_ = f;
		_tmp8_ = string_to_string (_tmp7_);
		_tmp9_ = g_strdup (_tmp8_);
		_g_free0 (file_name);
		file_name = _tmp9_;
		_tmp10_ = file_name;
		_tmp11_ = g_file_new_for_path (_tmp10_);
		_g_object_unref0 (file);
		file = _tmp11_;
		_tmp12_ = file;
		_tmp13_ = g_file_get_path (_tmp12_);
		_g_free0 (self->font_file_path);
		self->font_file_path = (gchar*) _tmp13_;
		_tmp14_ = file;
		if (!g_file_query_exists (_tmp14_, NULL)) {
			bird_font_save_callback_save (self);
		} else {
			BirdFontOverwriteBfFile* _tmp15_;
			BirdFontOverwriteBfFile* _tmp16_;
			_tmp15_ = bird_font_overwrite_bf_file_new (self);
			_g_object_unref0 (dialog);
			dialog = _tmp15_;
			_tmp16_ = dialog;
			bird_font_main_window_show_dialog ((BirdFontDialog*) _tmp16_);
		}
	}
	_g_free0 (file_name);
	_g_object_unref0 (dialog);
	_g_object_unref0 (file);
	_g_free0 (f);
}

static void
___lambda11__bird_font_file_chooser_file_selected (BirdFontFileChooser* _sender,
                                                   const gchar* path,
                                                   gpointer self)
{
	__lambda11_ ((BirdFontSaveCallback*) self, path);
}

void
bird_font_save_callback_save_as (BirdFontSaveCallback* self)
{
	BirdFontFileChooser* fc = NULL;
	BirdFontFileChooser* _tmp0_;
	BirdFontFileChooser* _tmp1_;
	BirdFontFileChooser* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	BirdFontFileChooser* _tmp5_;
	g_return_if_fail (self != NULL);
	if (bird_font_menu_tab_has_suppress_event ()) {
		bird_font_warn_if_test ("Event suppressed");
		return;
	}
	_tmp0_ = bird_font_file_chooser_new ();
	fc = _tmp0_;
	_tmp1_ = fc;
	g_signal_connect_object (_tmp1_, "file-selected", (GCallback) ___lambda11__bird_font_file_chooser_file_selected, self, 0);
	_tmp2_ = fc;
	bird_font_file_chooser_add_extension (_tmp2_, "bf");
	_tmp3_ = bird_font_t_ ("Save");
	_tmp4_ = _tmp3_;
	_tmp5_ = fc;
	bird_font_main_window_file_chooser (_tmp4_, _tmp5_, BIRD_FONT_FILE_CHOOSER_SAVE);
	_g_free0 (_tmp4_);
	_g_object_unref0 (fc);
}

void
bird_font_save_callback_save (BirdFontSaveCallback* self)
{
	BirdFontFont* f = NULL;
	gchar* fn = NULL;
	BirdFontFont* _tmp0_;
	const gchar* _tmp1_;
	BirdFontFont* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	BirdFontFont* _tmp8_;
	g_return_if_fail (self != NULL);
	if (bird_font_menu_tab_has_suppress_event ()) {
		bird_font_warn_if_test ("Event suppressed");
		_g_free0 (fn);
		_g_object_unref0 (f);
		return;
	}
	_tmp0_ = bird_font_bird_font_get_current_font ();
	_g_object_unref0 (f);
	f = _tmp0_;
	_tmp1_ = self->font_file_path;
	if (g_strcmp0 (_tmp1_, "") != 0) {
		BirdFontFont* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp2_ = f;
		_tmp3_ = self->font_file_path;
		_tmp4_ = g_strdup (_tmp3_);
		_g_free0 (_tmp2_->font_file);
		_tmp2_->font_file = _tmp4_;
	}
	_tmp5_ = f;
	_tmp6_ = bird_font_font_get_path (_tmp5_);
	_tmp7_ = _tmp6_;
	bird_font_preferences_add_recent_files (_tmp7_);
	_g_free0 (_tmp7_);
	_tmp8_ = f;
	if (bird_font_font_is_bfp (_tmp8_)) {
		BirdFontNativeWindow* _tmp9_;
		_tmp9_ = bird_font_main_window_native_window;
		bird_font_native_window_save (_tmp9_);
	} else {
		BirdFontFont* _tmp10_;
		gchar* _tmp11_;
		gboolean _tmp12_ = FALSE;
		BirdFontFont* _tmp13_;
		const gchar* _tmp14_;
		_tmp10_ = f;
		_tmp11_ = bird_font_font_get_path (_tmp10_);
		_g_free0 (fn);
		fn = _tmp11_;
		_tmp13_ = f;
		_tmp14_ = _tmp13_->font_file;
		if (_tmp14_ != NULL) {
			gboolean _tmp15_ = FALSE;
			const gchar* _tmp16_;
			_tmp16_ = fn;
			if (g_str_has_suffix (_tmp16_, ".bf")) {
				_tmp15_ = TRUE;
			} else {
				const gchar* _tmp17_;
				_tmp17_ = fn;
				_tmp15_ = g_str_has_suffix (_tmp17_, ".birdfont");
			}
			_tmp12_ = _tmp15_;
		} else {
			_tmp12_ = FALSE;
		}
		if (_tmp12_) {
			BirdFontFont* _tmp18_;
			const gchar* _tmp19_;
			BirdFontNativeWindow* _tmp20_;
			_tmp18_ = f;
			_tmp19_ = fn;
			bird_font_font_set_font_file (_tmp18_, _tmp19_);
			_tmp20_ = bird_font_main_window_native_window;
			bird_font_native_window_save (_tmp20_);
		} else {
			bird_font_save_callback_save_as (self);
		}
	}
	_g_free0 (fn);
	_g_object_unref0 (f);
}

static void
bird_font_save_callback_class_init (BirdFontSaveCallbackClass * klass,
                                    gpointer klass_data)
{
	bird_font_save_callback_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = bird_font_save_callback_finalize;
	bird_font_save_callback_signals[BIRD_FONT_SAVE_CALLBACK_FILE_SAVED_SIGNAL] = g_signal_new ("file-saved", BIRD_FONT_TYPE_SAVE_CALLBACK, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
bird_font_save_callback_instance_init (BirdFontSaveCallback * self,
                                       gpointer klass)
{
	gchar* _tmp0_;
	self->is_done = FALSE;
	_tmp0_ = g_strdup ("");
	self->font_file_path = _tmp0_;
}

static void
bird_font_save_callback_finalize (GObject * obj)
{
	BirdFontSaveCallback * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_SAVE_CALLBACK, BirdFontSaveCallback);
	_g_free0 (self->font_file_path);
	G_OBJECT_CLASS (bird_font_save_callback_parent_class)->finalize (obj);
}

static GType
bird_font_save_callback_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontSaveCallbackClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_save_callback_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontSaveCallback), 0, (GInstanceInitFunc) bird_font_save_callback_instance_init, NULL };
	GType bird_font_save_callback_type_id;
	bird_font_save_callback_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontSaveCallback", &g_define_type_info, 0);
	return bird_font_save_callback_type_id;
}

GType
bird_font_save_callback_get_type (void)
{
	static volatile gsize bird_font_save_callback_type_id__once = 0;
	if (g_once_init_enter (&bird_font_save_callback_type_id__once)) {
		GType bird_font_save_callback_type_id;
		bird_font_save_callback_type_id = bird_font_save_callback_get_type_once ();
		g_once_init_leave (&bird_font_save_callback_type_id__once, bird_font_save_callback_type_id);
	}
	return bird_font_save_callback_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;
}

