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

/*  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 3 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, see <http://www.gnu.org/licenses/>.*/
/**/

#include "elementary-calendar.h"
#include <libecal/libecal.h>
#include <glib.h>
#include <libical-glib/libical-glib.h>
#include <stdlib.h>
#include <string.h>
#include <libedataserver/libedataserver.h>
#include <glib/gi18n-lib.h>
#include <gee.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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define __vala_ECalComponentText_free0(var) ((var == NULL) ? NULL : (var = (_vala_ECalComponentText_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

static void _vala_ECalComponentText_free (ECalComponentText* self);

static void
_vala_ECalComponentText_free (ECalComponentText* self)
{
	g_boxed_free (e_cal_component_text_get_type (), self);
}

gint
maya_util_compare_events (ECalComponent* comp1,
                          ECalComponent* comp2)
{
	gint res = 0;
	ICalComponent* _tmp0_;
	ICalTime* _tmp1_;
	ICalTime* _tmp2_;
	ICalComponent* _tmp3_;
	ICalTime* _tmp4_;
	ICalTime* _tmp5_;
	gint _tmp6_;
	ECalComponentText* _tmp7_;
	ECalComponentText* _tmp8_;
	const gchar* _tmp9_;
	ECalComponentText* _tmp10_;
	ECalComponentText* _tmp11_;
	const gchar* _tmp12_;
	gint _tmp13_;
	gint result;
	g_return_val_if_fail (comp1 != NULL, -1);
	g_return_val_if_fail (comp2 != NULL, -1);
	_tmp0_ = e_cal_component_get_icalcomponent (comp1);
	_tmp1_ = i_cal_component_get_dtstart (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = e_cal_component_get_icalcomponent (comp2);
	_tmp4_ = i_cal_component_get_dtstart (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = i_cal_time_compare (_tmp2_, _tmp5_);
	_g_object_unref0 (_tmp5_);
	_g_object_unref0 (_tmp2_);
	res = _tmp6_;
	if (res != 0) {
		result = res;
		return result;
	}
	_tmp7_ = e_cal_component_get_summary (comp1);
	_tmp8_ = _tmp7_;
	_tmp9_ = e_cal_component_text_get_value (_tmp8_);
	_tmp10_ = e_cal_component_get_summary (comp2);
	_tmp11_ = _tmp10_;
	_tmp12_ = e_cal_component_text_get_value (_tmp11_);
	_tmp13_ = g_utf8_collate (_tmp9_, _tmp12_);
	__vala_ECalComponentText_free0 (_tmp11_);
	__vala_ECalComponentText_free0 (_tmp8_);
	result = _tmp13_;
	return result;
}

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

gchar*
maya_util_get_source_location (ESource* source)
{
	ESourceRegistry* registry = NULL;
	CalendarEventStore* _tmp0_;
	CalendarEventStore* _tmp1_;
	ESourceRegistry* _tmp2_;
	ESourceRegistry* _tmp3_;
	ESourceRegistry* _tmp4_;
	ESourceRegistry* _tmp5_;
	gchar* parent_uid = NULL;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	ESource* parent_source = NULL;
	ESource* _tmp9_;
	const gchar* _tmp41_;
	const gchar* _tmp42_;
	gchar* _tmp44_;
	gchar* result;
	g_return_val_if_fail (source != NULL, NULL);
	_tmp0_ = calendar_event_store_get_default ();
	_tmp1_ = _tmp0_;
	_tmp2_ = calendar_event_store_get_registry (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	_tmp5_ = _tmp4_;
	_g_object_unref0 (_tmp1_);
	registry = _tmp5_;
	_tmp6_ = e_source_get_parent (source);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_strdup (_tmp7_);
	parent_uid = _tmp8_;
	_tmp9_ = _g_object_ref0 (source);
	parent_source = _tmp9_;
	while (TRUE) {
		ESource* _tmp10_;
		ESource* _tmp11_;
		const gchar* _tmp12_;
		const gchar* _tmp13_;
		gchar* _tmp14_;
		ESource* _tmp15_;
		ESource* _tmp26_;
		const gchar* _tmp37_;
		ESourceRegistry* _tmp38_;
		const gchar* _tmp39_;
		ESource* _tmp40_;
		_tmp10_ = parent_source;
		if (!(_tmp10_ != NULL)) {
			break;
		}
		_tmp11_ = parent_source;
		_tmp12_ = e_source_get_parent (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = g_strdup (_tmp13_);
		_g_free0 (parent_uid);
		parent_uid = _tmp14_;
		_tmp15_ = parent_source;
		if (e_source_has_extension (_tmp15_, E_SOURCE_EXTENSION_AUTHENTICATION)) {
			ESourceAuthentication* collection = NULL;
			ESource* _tmp16_;
			ESourceExtension* _tmp17_;
			ESourceAuthentication* _tmp18_;
			ESourceAuthentication* _tmp19_;
			const gchar* _tmp20_;
			const gchar* _tmp21_;
			_tmp16_ = parent_source;
			_tmp17_ = e_source_get_extension (_tmp16_, E_SOURCE_EXTENSION_AUTHENTICATION);
			_tmp18_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp17_, e_source_authentication_get_type (), ESourceAuthentication));
			collection = _tmp18_;
			_tmp19_ = collection;
			_tmp20_ = e_source_authentication_get_user (_tmp19_);
			_tmp21_ = _tmp20_;
			if (_tmp21_ != NULL) {
				ESourceAuthentication* _tmp22_;
				const gchar* _tmp23_;
				const gchar* _tmp24_;
				gchar* _tmp25_;
				_tmp22_ = collection;
				_tmp23_ = e_source_authentication_get_user (_tmp22_);
				_tmp24_ = _tmp23_;
				_tmp25_ = g_strdup (_tmp24_);
				result = _tmp25_;
				_g_object_unref0 (collection);
				_g_object_unref0 (parent_source);
				_g_free0 (parent_uid);
				_g_object_unref0 (registry);
				return result;
			}
			_g_object_unref0 (collection);
		}
		_tmp26_ = parent_source;
		if (e_source_has_extension (_tmp26_, E_SOURCE_EXTENSION_COLLECTION)) {
			ESourceCollection* collection = NULL;
			ESource* _tmp27_;
			ESourceExtension* _tmp28_;
			ESourceCollection* _tmp29_;
			ESourceCollection* _tmp30_;
			const gchar* _tmp31_;
			const gchar* _tmp32_;
			_tmp27_ = parent_source;
			_tmp28_ = e_source_get_extension (_tmp27_, E_SOURCE_EXTENSION_COLLECTION);
			_tmp29_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp28_, e_source_collection_get_type (), ESourceCollection));
			collection = _tmp29_;
			_tmp30_ = collection;
			_tmp31_ = e_source_collection_get_identity (_tmp30_);
			_tmp32_ = _tmp31_;
			if (_tmp32_ != NULL) {
				ESourceCollection* _tmp33_;
				const gchar* _tmp34_;
				const gchar* _tmp35_;
				gchar* _tmp36_;
				_tmp33_ = collection;
				_tmp34_ = e_source_collection_get_identity (_tmp33_);
				_tmp35_ = _tmp34_;
				_tmp36_ = g_strdup (_tmp35_);
				result = _tmp36_;
				_g_object_unref0 (collection);
				_g_object_unref0 (parent_source);
				_g_free0 (parent_uid);
				_g_object_unref0 (registry);
				return result;
			}
			_g_object_unref0 (collection);
		}
		_tmp37_ = parent_uid;
		if (_tmp37_ == NULL) {
			break;
		}
		_tmp38_ = registry;
		_tmp39_ = parent_uid;
		_tmp40_ = e_source_registry_ref_source (_tmp38_, _tmp39_);
		_g_object_unref0 (parent_source);
		parent_source = _tmp40_;
	}
	_tmp41_ = e_source_get_parent (source);
	_tmp42_ = _tmp41_;
	if (g_strcmp0 (_tmp42_, "webcal-stub") == 0) {
		gchar* _tmp43_;
		_tmp43_ = g_strdup (_ ("On the web"));
		result = _tmp43_;
		_g_object_unref0 (parent_source);
		_g_free0 (parent_uid);
		_g_object_unref0 (registry);
		return result;
	}
	_tmp44_ = g_strdup (_ ("On this computer"));
	result = _tmp44_;
	_g_object_unref0 (parent_source);
	_g_free0 (parent_uid);
	_g_object_unref0 (registry);
	return result;
}

static guint8*
g_string_get_data (GString* self,
                   gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	const gchar* _tmp0_;
	gint res_length1;
	gint _res_size_;
	guint8* _tmp1_;
	gint _tmp1__length1;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->str;
	res = (guint8*) _tmp0_;
	res_length1 = -1;
	_res_size_ = res_length1;
	res_length1 = (gint) self->len;
	_tmp1_ = res;
	_tmp1__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp1__length1;
	}
	result = _tmp1_;
	return result;
}

void
maya_util_save_temp_selected_calendars (void)
{
	CalendarEventStore* calmodel = NULL;
	CalendarEventStore* _tmp0_;
	GeeCollection* events = NULL;
	CalendarEventStore* _tmp1_;
	GeeCollection* _tmp2_;
	GString* builder = NULL;
	GString* _tmp3_;
	GString* _tmp4_;
	GString* _tmp5_;
	GString* _tmp15_;
	gchar* file_path = NULL;
	const gchar* _tmp16_;
	gchar* _tmp17_;
	GError* _inner_error0_ = NULL;
	_tmp0_ = calendar_event_store_get_default ();
	calmodel = _tmp0_;
	_tmp1_ = calmodel;
	_tmp2_ = calendar_event_store_get_events (_tmp1_);
	events = _tmp2_;
	_tmp3_ = g_string_new ("");
	builder = _tmp3_;
	_tmp4_ = builder;
	g_string_append (_tmp4_, "BEGIN:VCALENDAR\n");
	_tmp5_ = builder;
	g_string_append (_tmp5_, "VERSION:2.0\n");
	{
		GeeIterator* _event_it = NULL;
		GeeCollection* _tmp6_;
		GeeIterator* _tmp7_;
		_tmp6_ = events;
		_tmp7_ = gee_iterable_iterator ((GeeIterable*) _tmp6_);
		_event_it = _tmp7_;
		while (TRUE) {
			GeeIterator* _tmp8_;
			ECalComponent* event = NULL;
			GeeIterator* _tmp9_;
			gpointer _tmp10_;
			GString* _tmp11_;
			ECalComponent* _tmp12_;
			gchar* _tmp13_;
			gchar* _tmp14_;
			_tmp8_ = _event_it;
			if (!gee_iterator_next (_tmp8_)) {
				break;
			}
			_tmp9_ = _event_it;
			_tmp10_ = gee_iterator_get (_tmp9_);
			event = (ECalComponent*) _tmp10_;
			_tmp11_ = builder;
			_tmp12_ = event;
			_tmp13_ = e_cal_component_get_as_string (_tmp12_);
			_tmp14_ = _tmp13_;
			g_string_append (_tmp11_, _tmp14_);
			_g_free0 (_tmp14_);
			_g_object_unref0 (event);
		}
		_g_object_unref0 (_event_it);
	}
	_tmp15_ = builder;
	g_string_append (_tmp15_, "END:VCALENDAR");
	_tmp16_ = g_get_tmp_dir ();
	_tmp17_ = g_strconcat (_tmp16_, "/calendar.ics", NULL);
	file_path = _tmp17_;
	{
		GFile* file = NULL;
		GFile* _tmp18_;
		GString* _tmp19_;
		guint8* _tmp20_;
		gint _tmp20__length1;
		gint _tmp21_ = 0;
		guint8* _tmp22_;
		gint _tmp22__length1;
		_tmp18_ = g_file_new_for_path (file_path);
		file = _tmp18_;
		_tmp19_ = builder;
		_tmp20_ = g_string_get_data (_tmp19_, &_tmp21_);
		_tmp20__length1 = _tmp21_;
		_tmp22_ = _tmp20_;
		_tmp22__length1 = _tmp20__length1;
		g_file_replace_contents (file, (const char*) _tmp22_, (gsize) _tmp22__length1, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp23_;
		const gchar* _tmp24_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp23_ = e;
		_tmp24_ = _tmp23_->message;
		g_warning ("Utils.vala:83: %s\n", _tmp24_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (file_path);
		_g_string_free0 (builder);
		_g_object_unref0 (events);
		_g_object_unref0 (calmodel);
		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;
	}
	_g_free0 (file_path);
	_g_string_free0 (builder);
	_g_object_unref0 (events);
	_g_object_unref0 (calmodel);
}

ECalComponent*
maya_util_copy_ecal_component (ECalComponent* original)
{
	ECalComponent* copy = NULL;
	ECalComponent* _tmp0_;
	ESource* source = NULL;
	gconstpointer _tmp1_;
	ESource* _tmp2_;
	ECalComponent* _tmp3_;
	ESource* _tmp4_;
	ESource* _tmp5_;
	ECalComponent* result;
	if (original == NULL) {
		result = NULL;
		return result;
	}
	_tmp0_ = e_cal_component_clone (original);
	copy = _tmp0_;
	_tmp1_ = g_object_get_data ((GObject*) original, "source");
	_tmp2_ = _g_object_ref0 ((ESource*) _tmp1_);
	source = _tmp2_;
	_tmp3_ = copy;
	_tmp4_ = source;
	_tmp5_ = _g_object_ref0 (_tmp4_);
	g_object_set_data_full ((GObject*) _tmp3_, "source", _tmp5_, g_object_unref);
	result = copy;
	_g_object_unref0 (source);
	return result;
}

gchar*
maya_util_mangle_uid (const gchar* original_uid)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (original_uid != NULL, NULL);
	_tmp0_ = g_strdup (original_uid);
	_result_ = _tmp0_;
	_tmp1_ = g_utf8_strreverse (_result_, (gssize) -1);
	result = _tmp1_;
	_g_free0 (_result_);
	return result;
}

