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

/* SPDX-License-Identifier: GPL-3.0-or-later*/

#include "graphs.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <adwaita.h>
#include <glib-object.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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _GraphsProjectSaveData GraphsProjectSaveData;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _GraphsProjectLoadData GraphsProjectLoadData;
typedef struct _GraphsProjectOpenData GraphsProjectOpenData;
typedef struct _Block24Data Block24Data;

struct _GraphsProjectSaveData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GraphsWindow* window;
	gboolean require_dialog;
	gboolean result;
	gboolean _tmp0_;
	GraphsData* _tmp1_;
	GraphsData* _tmp2_;
	GFile* _tmp3_;
	GFile* _tmp4_;
	GtkFileDialog* dialog;
	GtkFileDialog* _tmp5_;
	GtkFileDialog* _tmp6_;
	GListModel* _tmp7_;
	GListModel* _tmp8_;
	GtkFileDialog* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	GFile* _tmp12_;
	GtkFileDialog* _tmp13_;
	GFile* _tmp14_;
	GraphsData* _tmp15_;
	GraphsData* _tmp16_;
	GError* _inner_error0_;
};

struct _GraphsProjectLoadData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GraphsWindow* window;
	GraphsData* data;
	GFile* file;
	GraphsProjectParseFlags flags;
	gboolean result;
	GError* e;
	gchar* dialog_name;
	GraphsProjectParseFlags new_flags;
	GError* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	AdwAlertDialog* error_dialog;
	GObject* _tmp3_;
	AdwAlertDialog* _tmp4_;
	GError* _tmp5_;
	const gchar* _tmp6_;
	AdwAlertDialog* _tmp7_;
	AdwAlertDialog* dialog;
	const gchar* _tmp8_;
	GObject* _tmp9_;
	gchar* response;
	AdwAlertDialog* _tmp10_;
	const gchar* _tmp11_;
	gchar* _tmp12_;
	const gchar* _tmp13_;
	GError* _inner_error0_;
};

struct _GraphsProjectOpenData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GraphsWindow* window;
	GtkFileDialog* dialog;
	GtkFileDialog* _tmp0_;
	GtkFileDialog* _tmp1_;
	GListModel* _tmp2_;
	GListModel* _tmp3_;
	GFile* file;
	GtkFileDialog* _tmp4_;
	GFile* _tmp5_;
	gboolean _tmp6_;
	GraphsData* _tmp7_;
	GraphsData* _tmp8_;
	gboolean _tmp9_;
	gboolean _tmp10_;
	GraphsData* _tmp11_;
	GraphsData* _tmp12_;
	GFile* _tmp13_;
	GFile* _tmp14_;
	GraphsData* _tmp15_;
	GraphsData* _tmp16_;
	GFile* _tmp17_;
	GraphsApplication* application;
	GApplication* _tmp18_;
	GraphsApplication* _tmp19_;
	GraphsWindow* new_window;
	GraphsApplication* _tmp20_;
	GraphsWindow* _tmp21_;
	GraphsWindow* _tmp22_;
	GraphsData* _tmp23_;
	GraphsData* _tmp24_;
	GFile* _tmp25_;
	GraphsWindow* _tmp26_;
	GraphsApplication* _tmp27_;
	GraphsWindow* _tmp28_;
	GError* _inner_error0_;
};

struct _Block24Data {
	int _ref_count_;
	GraphsWindow* window;
};

VALA_EXTERN void _graphs_project_save (GraphsWindow* window);
VALA_EXTERN GListModel* graphs_project_get_project_file_filters (void);
static void graphs_project_save_data_free (gpointer _data);
static gboolean graphs_project_save_co (GraphsProjectSaveData* _data_);
static void graphs_project_save_ready (GObject* source_object,
                                GAsyncResult* _res_,
                                gpointer _user_data_);
static void graphs_project_load_data_free (gpointer _data);
static gboolean graphs_project_load_co (GraphsProjectLoadData* _data_);
static void graphs_project_load_ready (GObject* source_object,
                                GAsyncResult* _res_,
                                gpointer _user_data_);
static void graphs_project_open_data_free (gpointer _data);
static gboolean graphs_project_open_co (GraphsProjectOpenData* _data_);
static void graphs_project_open_ready (GObject* source_object,
                                GAsyncResult* _res_,
                                gpointer _user_data_);
static Block24Data* block24_data_ref (Block24Data* _data24_);
static void block24_data_unref (void * _userdata_);
static void __lambda58_ (Block24Data* _data24_,
                  AdwAlertDialog* d,
                  const gchar* response);
static void ____lambda59_ (Block24Data* _data24_,
                    GObject* o,
                    GAsyncResult* _result_);
static void _____lambda59__gasync_ready_callback (GObject* source_object,
                                           GAsyncResult* res,
                                           gpointer self);
static void ___lambda58__adw_alert_dialog_response (AdwAlertDialog* _sender,
                                             const gchar* response,
                                             gpointer self);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

GQuark
graphs_project_parse_error_quark (void)
{
	return g_quark_from_static_string ("graphs-project-parse-error-quark");
}

static GType
graphs_project_parse_error_get_type_once (void)
{
	static const GEnumValue values[] = {{GRAPHS_PROJECT_PARSE_ERROR_INVALID_PROJECT, "GRAPHS_PROJECT_PARSE_ERROR_INVALID_PROJECT", "invalid-project"}, {GRAPHS_PROJECT_PARSE_ERROR_LEGACY_MIGRATION_DISALLOWED, "GRAPHS_PROJECT_PARSE_ERROR_LEGACY_MIGRATION_DISALLOWED", "legacy-migration-disallowed"}, {GRAPHS_PROJECT_PARSE_ERROR_BETA_DISALLOWED, "GRAPHS_PROJECT_PARSE_ERROR_BETA_DISALLOWED", "beta-disallowed"}, {0, NULL, NULL}};
	GType graphs_project_parse_error_type_id;
	graphs_project_parse_error_type_id = g_enum_register_static ("GraphsProjectParseError", values);
	return graphs_project_parse_error_type_id;
}

GType
graphs_project_parse_error_get_type (void)
{
	static volatile gsize graphs_project_parse_error_type_id__once = 0;
	if (g_once_init_enter (&graphs_project_parse_error_type_id__once)) {
		GType graphs_project_parse_error_type_id;
		graphs_project_parse_error_type_id = graphs_project_parse_error_get_type_once ();
		g_once_init_leave (&graphs_project_parse_error_type_id__once, graphs_project_parse_error_type_id);
	}
	return graphs_project_parse_error_type_id__once;
}

static GType
graphs_project_parse_flags_get_type_once (void)
{
	static const GFlagsValue values[] = {{GRAPHS_PROJECT_PARSE_FLAGS_NONE, "GRAPHS_PROJECT_PARSE_FLAGS_NONE", "none"}, {GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_LEGACY_MIGRATION, "GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_LEGACY_MIGRATION", "allow-legacy-migration"}, {GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_BETA, "GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_BETA", "allow-beta"}, {0, NULL, NULL}};
	GType graphs_project_parse_flags_type_id;
	graphs_project_parse_flags_type_id = g_flags_register_static ("GraphsProjectParseFlags", values);
	return graphs_project_parse_flags_type_id;
}

GType
graphs_project_parse_flags_get_type (void)
{
	static volatile gsize graphs_project_parse_flags_type_id__once = 0;
	if (g_once_init_enter (&graphs_project_parse_flags_type_id__once)) {
		GType graphs_project_parse_flags_type_id;
		graphs_project_parse_flags_type_id = graphs_project_parse_flags_get_type_once ();
		g_once_init_leave (&graphs_project_parse_flags_type_id__once, graphs_project_parse_flags_type_id);
	}
	return graphs_project_parse_flags_type_id__once;
}

GtkFileFilter*
graphs_project_get_project_file_filter (void)
{
	GtkFileFilter* _tmp0_;
	GtkFileFilter* result;
	_tmp0_ = graphs_tools_create_file_filter (C_ ("file-filter", "Graphs Project File"), "graphs", NULL);
	result = _tmp0_;
	return result;
}

void
_graphs_project_save (GraphsWindow* window)
{
	GraphsData* _tmp0_;
	GraphsData* _tmp1_;
	GraphsData* _tmp2_;
	GraphsData* _tmp3_;
	GFile* _tmp4_;
	GFile* _tmp5_;
	g_return_if_fail (window != NULL);
	_tmp0_ = graphs_window_get_data (window);
	_tmp1_ = _tmp0_;
	graphs_data_save (_tmp1_);
	_tmp2_ = graphs_window_get_data (window);
	_tmp3_ = _tmp2_;
	_tmp4_ = graphs_data_get_file (_tmp3_);
	_tmp5_ = _tmp4_;
	graphs_window_add_toast_string_with_file (window, _ ("Saved Project"), _tmp5_);
}

GListModel*
graphs_project_get_project_file_filters (void)
{
	GtkFileFilter* _tmp0_;
	GtkFileFilter* _tmp1_;
	GListStore* _tmp2_;
	GListModel* _tmp3_;
	GListModel* result;
	_tmp0_ = graphs_project_get_project_file_filter ();
	_tmp1_ = _tmp0_;
	_tmp2_ = graphs_tools_create_file_filters (FALSE, _tmp1_, NULL);
	_tmp3_ = (GListModel*) _tmp2_;
	_g_object_unref0 (_tmp1_);
	result = _tmp3_;
	return result;
}

static void
graphs_project_save_data_free (gpointer _data)
{
	GraphsProjectSaveData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->window);
	g_slice_free (GraphsProjectSaveData, _data_);
}

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

void
graphs_project_save (GraphsWindow* window,
                     gboolean require_dialog,
                     GAsyncReadyCallback _callback_,
                     gpointer _user_data_)
{
	GraphsProjectSaveData* _data_;
	GraphsWindow* _tmp0_;
	g_return_if_fail (window != NULL);
	_data_ = g_slice_new0 (GraphsProjectSaveData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, graphs_project_save_data_free);
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data_->window);
	_data_->window = _tmp0_;
	_data_->require_dialog = require_dialog;
	graphs_project_save_co (_data_);
}

gboolean
graphs_project_save_finish (GAsyncResult* _res_)
{
	gboolean result;
	GraphsProjectSaveData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	result = _data_->result;
	return result;
}

static void
graphs_project_save_ready (GObject* source_object,
                           GAsyncResult* _res_,
                           gpointer _user_data_)
{
	GraphsProjectSaveData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	graphs_project_save_co (_data_);
}

static gboolean
graphs_project_save_co (GraphsProjectSaveData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp1_ = graphs_window_get_data (_data_->window);
	_data_->_tmp2_ = _data_->_tmp1_;
	_data_->_tmp3_ = graphs_data_get_file (_data_->_tmp2_);
	_data_->_tmp4_ = _data_->_tmp3_;
	if (_data_->_tmp4_ != NULL) {
		_data_->_tmp0_ = !_data_->require_dialog;
	} else {
		_data_->_tmp0_ = FALSE;
	}
	if (_data_->_tmp0_) {
		_graphs_project_save (_data_->window);
		_data_->result = TRUE;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp5_ = gtk_file_dialog_new ();
	_data_->dialog = _data_->_tmp5_;
	_data_->_tmp6_ = _data_->dialog;
	_data_->_tmp7_ = graphs_project_get_project_file_filters ();
	_data_->_tmp8_ = _data_->_tmp7_;
	gtk_file_dialog_set_filters (_data_->_tmp6_, _data_->_tmp8_);
	_g_object_unref0 (_data_->_tmp8_);
	_data_->_tmp9_ = _data_->dialog;
	_data_->_tmp10_ = g_strconcat (_ ("Project"), ".graphs", NULL);
	_data_->_tmp11_ = _data_->_tmp10_;
	gtk_file_dialog_set_initial_name (_data_->_tmp9_, _data_->_tmp11_);
	_g_free0 (_data_->_tmp11_);
	{
		_data_->_tmp13_ = _data_->dialog;
		_data_->_state_ = 1;
		gtk_file_dialog_save (_data_->_tmp13_, (GtkWindow*) _data_->window, NULL, graphs_project_save_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp14_ = gtk_file_dialog_save_finish (_data_->_tmp13_, _data_->_res_, &_data_->_inner_error0_);
		_data_->_tmp12_ = _data_->_tmp14_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp15_ = graphs_window_get_data (_data_->window);
		_data_->_tmp16_ = _data_->_tmp15_;
		graphs_data_set_file (_data_->_tmp16_, _data_->_tmp12_);
		_graphs_project_save (_data_->window);
		_data_->result = TRUE;
		_g_object_unref0 (_data_->_tmp12_);
		_g_object_unref0 (_data_->dialog);
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_data_->_inner_error0_);
		_data_->result = FALSE;
		_g_object_unref0 (_data_->dialog);
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	__finally0:
	_g_object_unref0 (_data_->dialog);
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
	g_clear_error (&_data_->_inner_error0_);
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
graphs_project_load_data_free (gpointer _data)
{
	GraphsProjectLoadData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->window);
	_g_object_unref0 (_data_->data);
	_g_object_unref0 (_data_->file);
	g_slice_free (GraphsProjectLoadData, _data_);
}

void
graphs_project_load (GraphsWindow* window,
                     GraphsData* data,
                     GFile* file,
                     GraphsProjectParseFlags flags,
                     GAsyncReadyCallback _callback_,
                     gpointer _user_data_)
{
	GraphsProjectLoadData* _data_;
	GraphsWindow* _tmp0_;
	GraphsData* _tmp1_;
	GFile* _tmp2_;
	g_return_if_fail (window != NULL);
	g_return_if_fail (data != NULL);
	g_return_if_fail (file != NULL);
	_data_ = g_slice_new0 (GraphsProjectLoadData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, graphs_project_load_data_free);
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data_->window);
	_data_->window = _tmp0_;
	_tmp1_ = _g_object_ref0 (data);
	_g_object_unref0 (_data_->data);
	_data_->data = _tmp1_;
	_tmp2_ = _g_object_ref0 (file);
	_g_object_unref0 (_data_->file);
	_data_->file = _tmp2_;
	_data_->flags = flags;
	graphs_project_load_co (_data_);
}

gboolean
graphs_project_load_finish (GAsyncResult* _res_)
{
	gboolean result;
	GraphsProjectLoadData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	result = _data_->result;
	return result;
}

static void
graphs_project_load_ready (GObject* source_object,
                           GAsyncResult* _res_,
                           gpointer _user_data_)
{
	GraphsProjectLoadData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	graphs_project_load_co (_data_);
}

static gboolean
graphs_project_load_co (GraphsProjectLoadData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		graphs_data_load (_data_->data, _data_->file, _data_->flags, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			if (_data_->_inner_error0_->domain == GRAPHS_PROJECT_PARSE_ERROR) {
				goto __catch0_graphs_project_parse_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->result = TRUE;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	goto __finally0;
	__catch0_graphs_project_parse_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp0_ = _data_->e;
		switch (_data_->_tmp0_->code) {
			case GRAPHS_PROJECT_PARSE_ERROR_LEGACY_MIGRATION_DISALLOWED:
			{
				_data_->_tmp1_ = g_strdup ("legacy_migration_disallowed");
				_g_free0 (_data_->dialog_name);
				_data_->dialog_name = _data_->_tmp1_;
				_data_->new_flags = _data_->flags | GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_LEGACY_MIGRATION;
				break;
			}
			case GRAPHS_PROJECT_PARSE_ERROR_BETA_DISALLOWED:
			{
				_data_->_tmp2_ = g_strdup ("beta_disallowed");
				_g_free0 (_data_->dialog_name);
				_data_->dialog_name = _data_->_tmp2_;
				_data_->new_flags = _data_->flags | GRAPHS_PROJECT_PARSE_FLAGS_ALLOW_BETA;
				break;
			}
			default:
			{
				_data_->_tmp3_ = graphs_tools_build_dialog ("invalid_project");
				_data_->error_dialog = G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp3_, adw_alert_dialog_get_type (), AdwAlertDialog);
				_data_->_tmp4_ = _data_->error_dialog;
				_data_->_tmp5_ = _data_->e;
				_data_->_tmp6_ = _data_->_tmp5_->message;
				adw_alert_dialog_set_body (_data_->_tmp4_, _data_->_tmp6_);
				_data_->_tmp7_ = _data_->error_dialog;
				adw_dialog_present ((AdwDialog*) _data_->_tmp7_, (GtkWidget*) _data_->window);
				_data_->result = FALSE;
				_g_object_unref0 (_data_->error_dialog);
				_g_free0 (_data_->dialog_name);
				_g_error_free0 (_data_->e);
				g_task_return_pointer (_data_->_async_result, _data_, NULL);
				if (_data_->_state_ != 0) {
					while (!g_task_get_completed (_data_->_async_result)) {
						g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
					}
				}
				g_object_unref (_data_->_async_result);
				return FALSE;
			}
		}
		_data_->_tmp8_ = _data_->dialog_name;
		_data_->_tmp9_ = graphs_tools_build_dialog (_data_->_tmp8_);
		_data_->dialog = G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp9_, adw_alert_dialog_get_type (), AdwAlertDialog);
		_data_->_tmp10_ = _data_->dialog;
		_data_->_state_ = 1;
		adw_alert_dialog_choose (_data_->_tmp10_, (GtkWidget*) _data_->window, NULL, graphs_project_load_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp11_ = adw_alert_dialog_choose_finish (_data_->_tmp10_, _data_->_res_);
		_data_->_tmp12_ = g_strdup (_data_->_tmp11_);
		_data_->response = _data_->_tmp12_;
		_data_->_tmp13_ = _data_->response;
		if (g_strcmp0 (_data_->_tmp13_, "continue") != 0) {
			_data_->result = FALSE;
			_g_free0 (_data_->response);
			_g_object_unref0 (_data_->dialog);
			_g_free0 (_data_->dialog_name);
			_g_error_free0 (_data_->e);
			g_task_return_pointer (_data_->_async_result, _data_, NULL);
			if (_data_->_state_ != 0) {
				while (!g_task_get_completed (_data_->_async_result)) {
					g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
				}
			}
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_state_ = 2;
		graphs_project_load (_data_->window, _data_->data, _data_->file, _data_->new_flags, graphs_project_load_ready, _data_);
		return FALSE;
		_state_2:
		_data_->result = graphs_project_load_finish (_data_->_res_);
		_g_free0 (_data_->response);
		_g_object_unref0 (_data_->dialog);
		_g_free0 (_data_->dialog_name);
		_g_error_free0 (_data_->e);
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
	g_clear_error (&_data_->_inner_error0_);
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
graphs_project_open_data_free (gpointer _data)
{
	GraphsProjectOpenData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->window);
	g_slice_free (GraphsProjectOpenData, _data_);
}

void
graphs_project_open (GraphsWindow* window,
                     GAsyncReadyCallback _callback_,
                     gpointer _user_data_)
{
	GraphsProjectOpenData* _data_;
	GraphsWindow* _tmp0_;
	g_return_if_fail (window != NULL);
	_data_ = g_slice_new0 (GraphsProjectOpenData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, graphs_project_open_data_free);
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data_->window);
	_data_->window = _tmp0_;
	graphs_project_open_co (_data_);
}

void
graphs_project_open_finish (GAsyncResult* _res_)
{
	GraphsProjectOpenData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
graphs_project_open_ready (GObject* source_object,
                           GAsyncResult* _res_,
                           gpointer _user_data_)
{
	GraphsProjectOpenData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	graphs_project_open_co (_data_);
}

static gboolean
graphs_project_open_co (GraphsProjectOpenData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		case 3:
		goto _state_3;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = gtk_file_dialog_new ();
	_data_->dialog = _data_->_tmp0_;
	_data_->_tmp1_ = _data_->dialog;
	_data_->_tmp2_ = graphs_project_get_project_file_filters ();
	_data_->_tmp3_ = _data_->_tmp2_;
	gtk_file_dialog_set_filters (_data_->_tmp1_, _data_->_tmp3_);
	_g_object_unref0 (_data_->_tmp3_);
	{
		_data_->_tmp4_ = _data_->dialog;
		_data_->_state_ = 1;
		gtk_file_dialog_open (_data_->_tmp4_, (GtkWindow*) _data_->window, NULL, graphs_project_open_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp5_ = gtk_file_dialog_open_finish (_data_->_tmp4_, _data_->_res_, &_data_->_inner_error0_);
		_data_->file = _data_->_tmp5_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp7_ = graphs_window_get_data (_data_->window);
		_data_->_tmp8_ = _data_->_tmp7_;
		_data_->_tmp9_ = graphs_data_get_unsaved (_data_->_tmp8_);
		_data_->_tmp10_ = _data_->_tmp9_;
		if (!_data_->_tmp10_) {
			_data_->_tmp11_ = graphs_window_get_data (_data_->window);
			_data_->_tmp12_ = _data_->_tmp11_;
			_data_->_tmp13_ = graphs_data_get_file (_data_->_tmp12_);
			_data_->_tmp14_ = _data_->_tmp13_;
			_data_->_tmp6_ = _data_->_tmp14_ == NULL;
		} else {
			_data_->_tmp6_ = FALSE;
		}
		if (_data_->_tmp6_) {
			_data_->_tmp15_ = graphs_window_get_data (_data_->window);
			_data_->_tmp16_ = _data_->_tmp15_;
			_data_->_tmp17_ = _data_->file;
			_data_->_state_ = 2;
			graphs_project_load (_data_->window, _data_->_tmp16_, _data_->_tmp17_, GRAPHS_PROJECT_PARSE_FLAGS_NONE, graphs_project_open_ready, _data_);
			return FALSE;
			_state_2:
			graphs_project_load_finish (_data_->_res_);
			_g_object_unref0 (_data_->file);
			_g_object_unref0 (_data_->dialog);
			g_task_return_pointer (_data_->_async_result, _data_, NULL);
			if (_data_->_state_ != 0) {
				while (!g_task_get_completed (_data_->_async_result)) {
					g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
				}
			}
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_tmp18_ = g_application_get_default ();
		_data_->_tmp19_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp18_, GRAPHS_TYPE_APPLICATION, GraphsApplication));
		_data_->application = _data_->_tmp19_;
		_data_->_tmp20_ = _data_->application;
		_data_->_tmp21_ = graphs_application_create_main_window (_data_->_tmp20_);
		_data_->new_window = _data_->_tmp21_;
		_data_->_tmp22_ = _data_->new_window;
		_data_->_tmp23_ = graphs_window_get_data (_data_->_tmp22_);
		_data_->_tmp24_ = _data_->_tmp23_;
		_data_->_tmp25_ = _data_->file;
		_data_->_state_ = 3;
		graphs_project_load (_data_->window, _data_->_tmp24_, _data_->_tmp25_, GRAPHS_PROJECT_PARSE_FLAGS_NONE, graphs_project_open_ready, _data_);
		return FALSE;
		_state_3:
		if (graphs_project_load_finish (_data_->_res_)) {
			_data_->_tmp26_ = _data_->new_window;
			gtk_window_present ((GtkWindow*) _data_->_tmp26_);
			_g_object_unref0 (_data_->new_window);
			_g_object_unref0 (_data_->application);
			_g_object_unref0 (_data_->file);
			_g_object_unref0 (_data_->dialog);
			g_task_return_pointer (_data_->_async_result, _data_, NULL);
			if (_data_->_state_ != 0) {
				while (!g_task_get_completed (_data_->_async_result)) {
					g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
				}
			}
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_tmp27_ = _data_->application;
		_data_->_tmp28_ = _data_->new_window;
		graphs_application_on_main_window_closed (_data_->_tmp27_, _data_->_tmp28_);
		_g_object_unref0 (_data_->new_window);
		_g_object_unref0 (_data_->application);
		_g_object_unref0 (_data_->file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_data_->_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_g_object_unref0 (_data_->dialog);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_g_object_unref0 (_data_->dialog);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static Block24Data*
block24_data_ref (Block24Data* _data24_)
{
	g_atomic_int_inc (&_data24_->_ref_count_);
	return _data24_;
}

static void
block24_data_unref (void * _userdata_)
{
	Block24Data* _data24_;
	_data24_ = (Block24Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data24_->_ref_count_)) {
		_g_object_unref0 (_data24_->window);
		g_slice_free (Block24Data, _data24_);
	}
}

static void
____lambda59_ (Block24Data* _data24_,
               GObject* o,
               GAsyncResult* _result_)
{
	g_return_if_fail (_result_ != NULL);
	if (graphs_project_save_finish (_result_)) {
		GraphsData* _tmp0_;
		GraphsData* _tmp1_;
		_tmp0_ = graphs_window_get_data (_data24_->window);
		_tmp1_ = _tmp0_;
		graphs_data_clear (_tmp1_);
	}
}

static void
_____lambda59__gasync_ready_callback (GObject* source_object,
                                      GAsyncResult* res,
                                      gpointer self)
{
	____lambda59_ (self, source_object, res);
	block24_data_unref (self);
}

static void
__lambda58_ (Block24Data* _data24_,
             AdwAlertDialog* d,
             const gchar* response)
{
	const gchar* _tmp0_;
	GQuark _tmp2_ = 0U;
	static GQuark _tmp1_label0 = 0;
	static GQuark _tmp1_label1 = 0;
	g_return_if_fail (d != NULL);
	g_return_if_fail (response != NULL);
	_tmp0_ = response;
	_tmp2_ = (NULL == _tmp0_) ? 0 : g_quark_from_string (_tmp0_);
	if (_tmp2_ == ((0 != _tmp1_label0) ? _tmp1_label0 : (_tmp1_label0 = g_quark_from_static_string ("discard")))) {
		switch (0) {
			default:
			{
				{
					GraphsData* _tmp3_;
					GraphsData* _tmp4_;
					_tmp3_ = graphs_window_get_data (_data24_->window);
					_tmp4_ = _tmp3_;
					graphs_data_clear (_tmp4_);
					break;
				}
			}
		}
	} else if (_tmp2_ == ((0 != _tmp1_label1) ? _tmp1_label1 : (_tmp1_label1 = g_quark_from_static_string ("save")))) {
		switch (0) {
			default:
			{
				{
					graphs_project_save (_data24_->window, FALSE, _____lambda59__gasync_ready_callback, block24_data_ref (_data24_));
					break;
				}
			}
		}
	}
}

static void
___lambda58__adw_alert_dialog_response (AdwAlertDialog* _sender,
                                        const gchar* response,
                                        gpointer self)
{
	__lambda58_ (self, _sender, response);
}

void
graphs_project_close (GraphsWindow* window)
{
	Block24Data* _data24_;
	GraphsWindow* _tmp0_;
	GraphsData* _tmp1_;
	GraphsData* _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp4_;
	AdwAlertDialog* dialog = NULL;
	GObject* _tmp7_;
	AdwAlertDialog* _tmp8_;
	AdwAlertDialog* _tmp9_;
	g_return_if_fail (window != NULL);
	_data24_ = g_slice_new0 (Block24Data);
	_data24_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data24_->window);
	_data24_->window = _tmp0_;
	_tmp1_ = graphs_window_get_data (_data24_->window);
	_tmp2_ = _tmp1_;
	_tmp3_ = graphs_data_get_unsaved (_tmp2_);
	_tmp4_ = _tmp3_;
	if (!_tmp4_) {
		GraphsData* _tmp5_;
		GraphsData* _tmp6_;
		_tmp5_ = graphs_window_get_data (_data24_->window);
		_tmp6_ = _tmp5_;
		graphs_data_clear (_tmp6_);
		block24_data_unref (_data24_);
		_data24_ = NULL;
		return;
	}
	_tmp7_ = graphs_tools_build_dialog ("save_project_changes");
	dialog = G_TYPE_CHECK_INSTANCE_CAST (_tmp7_, adw_alert_dialog_get_type (), AdwAlertDialog);
	_tmp8_ = dialog;
	g_signal_connect_data (_tmp8_, "response", (GCallback) ___lambda58__adw_alert_dialog_response, block24_data_ref (_data24_), (GClosureNotify) block24_data_unref, 0);
	_tmp9_ = dialog;
	adw_dialog_present ((AdwDialog*) _tmp9_, (GtkWidget*) _data24_->window);
	_g_object_unref0 (dialog);
	block24_data_unref (_data24_);
	_data24_ = NULL;
}

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

