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

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

#include "graphs.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.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)))
typedef struct _Block22Data Block22Data;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _Block22Data {
	int _ref_count_;
	GraphsData* data;
	GtkFileDialog* dialog;
	GraphsWindow* window;
};

static Block22Data* block22_data_ref (Block22Data* _data22_);
static void block22_data_unref (void * _userdata_);
static void ___lambda63_ (Block22Data* _data22_,
                   GObject* d,
                   GAsyncResult* response);
static void ____lambda63__gasync_ready_callback (GObject* source_object,
                                          GAsyncResult* res,
                                          gpointer self);
static void ___lambda64_ (Block22Data* _data22_,
                   GObject* d,
                   GAsyncResult* response);
static void ____lambda64__gasync_ready_callback (GObject* source_object,
                                          GAsyncResult* res,
                                          gpointer self);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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

static Block22Data*
block22_data_ref (Block22Data* _data22_)
{
	g_atomic_int_inc (&_data22_->_ref_count_);
	return _data22_;
}

static void
block22_data_unref (void * _userdata_)
{
	Block22Data* _data22_;
	_data22_ = (Block22Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data22_->_ref_count_)) {
		_g_object_unref0 (_data22_->dialog);
		_g_object_unref0 (_data22_->data);
		_g_object_unref0 (_data22_->window);
		g_slice_free (Block22Data, _data22_);
	}
}

static void
___lambda63_ (Block22Data* _data22_,
              GObject* d,
              GAsyncResult* response)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (response != NULL);
	{
		GFile* _tmp0_ = NULL;
		GtkFileDialog* _tmp1_;
		GFile* _tmp2_;
		GraphsData* _tmp3_;
		gint _tmp4_ = 0;
		GraphsItem** _tmp5_;
		GraphsItem** _tmp6_;
		gint _tmp6__length1;
		_tmp1_ = _data22_->dialog;
		_tmp2_ = gtk_file_dialog_select_folder_finish (_tmp1_, response, &_inner_error0_);
		_tmp0_ = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = _data22_->data;
		_tmp5_ = graphs_data_get_items (_tmp3_, &_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp6__length1 = _tmp4_;
		graphs_python_helper_export_items (_data22_->window, "columns", _tmp0_, _tmp6_, (gint) _tmp4_);
		_tmp6_ = (_vala_array_free (_tmp6_, _tmp6__length1, (GDestroyNotify) g_object_unref), NULL);
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	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;
	}
}

static void
____lambda63__gasync_ready_callback (GObject* source_object,
                                     GAsyncResult* res,
                                     gpointer self)
{
	___lambda63_ (self, source_object, res);
	block22_data_unref (self);
}

static void
___lambda64_ (Block22Data* _data22_,
              GObject* d,
              GAsyncResult* response)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (response != NULL);
	{
		GFile* _tmp0_ = NULL;
		GtkFileDialog* _tmp1_;
		GFile* _tmp2_;
		GraphsData* _tmp3_;
		gint _tmp4_ = 0;
		GraphsItem** _tmp5_;
		GraphsItem** _tmp6_;
		gint _tmp6__length1;
		_tmp1_ = _data22_->dialog;
		_tmp2_ = gtk_file_dialog_save_finish (_tmp1_, response, &_inner_error0_);
		_tmp0_ = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = _data22_->data;
		_tmp5_ = graphs_data_get_items (_tmp3_, &_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp6__length1 = _tmp4_;
		graphs_python_helper_export_items (_data22_->window, "columns", _tmp0_, _tmp6_, (gint) _tmp4_);
		_tmp6_ = (_vala_array_free (_tmp6_, _tmp6__length1, (GDestroyNotify) g_object_unref), NULL);
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	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;
	}
}

static void
____lambda64__gasync_ready_callback (GObject* source_object,
                                     GAsyncResult* res,
                                     gpointer self)
{
	___lambda64_ (self, source_object, res);
	block22_data_unref (self);
}

void
graphs_export_export_items (GraphsWindow* window)
{
	Block22Data* _data22_;
	GraphsWindow* _tmp0_;
	GraphsData* _tmp1_;
	GraphsData* _tmp2_;
	GraphsData* _tmp3_;
	GraphsData* _tmp4_;
	GtkFileDialog* _tmp5_;
	GraphsData* _tmp6_;
	g_return_if_fail (window != NULL);
	_data22_ = g_slice_new0 (Block22Data);
	_data22_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data22_->window);
	_data22_->window = _tmp0_;
	_tmp1_ = graphs_window_get_data (_data22_->window);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	_data22_->data = _tmp3_;
	_tmp4_ = _data22_->data;
	if (graphs_data_is_empty (_tmp4_)) {
		graphs_window_add_toast_string (_data22_->window, _ ("No data to export"));
		block22_data_unref (_data22_);
		_data22_ = NULL;
		return;
	}
	_tmp5_ = gtk_file_dialog_new ();
	_data22_->dialog = _tmp5_;
	_tmp6_ = _data22_->data;
	if (g_list_model_get_n_items ((GListModel*) _tmp6_) > ((guint) 1)) {
		GtkFileDialog* _tmp7_;
		_tmp7_ = _data22_->dialog;
		gtk_file_dialog_select_folder (_tmp7_, (GtkWindow*) _data22_->window, NULL, ____lambda63__gasync_ready_callback, block22_data_ref (_data22_));
	} else {
		GraphsItem* item = NULL;
		GraphsData* _tmp8_;
		GObject* _tmp9_;
		GtkFileDialog* _tmp10_;
		GraphsItem* _tmp11_;
		const gchar* _tmp12_;
		const gchar* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		GtkFileDialog* _tmp16_;
		GtkFileFilter* _tmp17_;
		GtkFileFilter* _tmp18_;
		GListStore* _tmp19_;
		GListStore* _tmp20_;
		GtkFileDialog* _tmp21_;
		_tmp8_ = _data22_->data;
		_tmp9_ = g_list_model_get_item ((GListModel*) _tmp8_, (guint) 0);
		item = G_TYPE_CHECK_INSTANCE_CAST (_tmp9_, GRAPHS_TYPE_ITEM, GraphsItem);
		_tmp10_ = _data22_->dialog;
		_tmp11_ = item;
		_tmp12_ = graphs_item_get_name (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = g_strconcat (_tmp13_, ".txt", NULL);
		_tmp15_ = _tmp14_;
		gtk_file_dialog_set_initial_name (_tmp10_, _tmp15_);
		_g_free0 (_tmp15_);
		_tmp16_ = _data22_->dialog;
		_tmp17_ = graphs_tools_create_file_filter (C_ ("file-filter", "Text Files"), "txt", NULL);
		_tmp18_ = _tmp17_;
		_tmp19_ = graphs_tools_create_file_filters (TRUE, _tmp18_, NULL);
		_tmp20_ = _tmp19_;
		gtk_file_dialog_set_filters (_tmp16_, (GListModel*) _tmp20_);
		_g_object_unref0 (_tmp20_);
		_g_object_unref0 (_tmp18_);
		_tmp21_ = _data22_->dialog;
		gtk_file_dialog_save (_tmp21_, (GtkWindow*) _data22_->window, NULL, ____lambda64__gasync_ready_callback, block22_data_ref (_data22_));
		_g_object_unref0 (item);
	}
	block22_data_unref (_data22_);
	_data22_ = NULL;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

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

