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

/*-
 * Copyright (c) 2014 elementary, Inc. (https://elementary.io)
 *
 * 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/>.
 *
 * Authored by: Corentin Noël <corentin@elementaryos.org>
 */

#include "datetime.h"
#include <glib.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

enum  {
	DATE_TIME_CURRENT_TIME_MANAGER_0_PROPERTY,
	DATE_TIME_CURRENT_TIME_MANAGER_NUM_PROPERTIES
};
static GParamSpec* date_time_current_time_manager_properties[DATE_TIME_CURRENT_TIME_MANAGER_NUM_PROPERTIES];
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
enum  {
	DATE_TIME_CURRENT_TIME_MANAGER_TIME_HAS_CHANGED_SIGNAL,
	DATE_TIME_CURRENT_TIME_MANAGER_NUM_SIGNALS
};
static guint date_time_current_time_manager_signals[DATE_TIME_CURRENT_TIME_MANAGER_NUM_SIGNALS] = {0};

struct _DateTimeCurrentTimeManagerPrivate {
	guint timeout;
	GDateTime* very_next_minute;
};

static gint DateTimeCurrentTimeManager_private_offset;
static gpointer date_time_current_time_manager_parent_class = NULL;

static void date_time_current_time_manager_create_next_minute_timeout (DateTimeCurrentTimeManager* self);
static gboolean __lambda6_ (DateTimeCurrentTimeManager* self);
static gboolean ___lambda6__gsource_func (gpointer self);
static void date_time_current_time_manager_finalize (GObject * obj);
static GType date_time_current_time_manager_get_type_once (void);

static inline gpointer
date_time_current_time_manager_get_instance_private (DateTimeCurrentTimeManager* self)
{
	return G_STRUCT_MEMBER_P (self, DateTimeCurrentTimeManager_private_offset);
}

DateTimeCurrentTimeManager*
date_time_current_time_manager_construct (GType object_type)
{
	DateTimeCurrentTimeManager * self = NULL;
	self = (DateTimeCurrentTimeManager*) g_object_new (object_type, NULL);
	date_time_current_time_manager_create_next_minute_timeout (self);
	return self;
}

DateTimeCurrentTimeManager*
date_time_current_time_manager_new (void)
{
	return date_time_current_time_manager_construct (DATE_TIME_TYPE_CURRENT_TIME_MANAGER);
}

void
date_time_current_time_manager_timezone_has_changed (DateTimeCurrentTimeManager* self)
{
	GDateTime* now_local = NULL;
	GDateTime* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_date_time_new_now_local ();
	now_local = _tmp0_;
	g_signal_emit (self, date_time_current_time_manager_signals[DATE_TIME_CURRENT_TIME_MANAGER_TIME_HAS_CHANGED_SIGNAL], 0, now_local);
	date_time_current_time_manager_create_next_minute_timeout (self);
	_g_date_time_unref0 (now_local);
}

void
date_time_current_time_manager_datetime_has_changed (DateTimeCurrentTimeManager* self,
                                                     gboolean update_immediately)
{
	g_return_if_fail (self != NULL);
	if (update_immediately) {
		GDateTime* now_local = NULL;
		GDateTime* _tmp0_;
		GDateTime* _tmp1_;
		_tmp0_ = g_date_time_new_now_local ();
		now_local = _tmp0_;
		_tmp1_ = now_local;
		g_signal_emit (self, date_time_current_time_manager_signals[DATE_TIME_CURRENT_TIME_MANAGER_TIME_HAS_CHANGED_SIGNAL], 0, _tmp1_);
		_g_date_time_unref0 (now_local);
	}
	date_time_current_time_manager_create_next_minute_timeout (self);
}

static gboolean
__lambda6_ (DateTimeCurrentTimeManager* self)
{
	GDateTime* _tmp0_;
	gboolean result;
	_tmp0_ = self->priv->very_next_minute;
	g_signal_emit (self, date_time_current_time_manager_signals[DATE_TIME_CURRENT_TIME_MANAGER_TIME_HAS_CHANGED_SIGNAL], 0, _tmp0_);
	self->priv->timeout = (guint) 0;
	date_time_current_time_manager_create_next_minute_timeout (self);
	result = FALSE;
	return result;
}

static gboolean
___lambda6__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda6_ ((DateTimeCurrentTimeManager*) self);
	return result;
}

static void
date_time_current_time_manager_create_next_minute_timeout (DateTimeCurrentTimeManager* self)
{
	GDateTime* now_local = NULL;
	GDateTime* _tmp0_;
	GDateTime* _tmp1_;
	GDateTime* _tmp2_;
	GDateTime* _tmp3_;
	GTimeSpan timespan = 0LL;
	GDateTime* _tmp4_;
	g_return_if_fail (self != NULL);
	if (self->priv->timeout != ((guint) 0)) {
		g_source_remove (self->priv->timeout);
	}
	_tmp0_ = g_date_time_new_now_local ();
	now_local = _tmp0_;
	_tmp1_ = g_date_time_add_seconds (now_local, -g_date_time_get_seconds (now_local));
	_tmp2_ = _tmp1_;
	_tmp3_ = g_date_time_add_minutes (_tmp2_, 1);
	_g_date_time_unref0 (self->priv->very_next_minute);
	self->priv->very_next_minute = _tmp3_;
	_g_date_time_unref0 (_tmp2_);
	_tmp4_ = self->priv->very_next_minute;
	timespan = g_date_time_difference (_tmp4_, now_local);
	self->priv->timeout = g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) (timespan / 1000), ___lambda6__gsource_func, g_object_ref (self), g_object_unref);
	_g_date_time_unref0 (now_local);
}

static void
date_time_current_time_manager_class_init (DateTimeCurrentTimeManagerClass * klass,
                                           gpointer klass_data)
{
	date_time_current_time_manager_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DateTimeCurrentTimeManager_private_offset);
	G_OBJECT_CLASS (klass)->finalize = date_time_current_time_manager_finalize;
	date_time_current_time_manager_signals[DATE_TIME_CURRENT_TIME_MANAGER_TIME_HAS_CHANGED_SIGNAL] = g_signal_new ("time-has-changed", DATE_TIME_TYPE_CURRENT_TIME_MANAGER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1, G_TYPE_DATE_TIME);
}

static void
date_time_current_time_manager_instance_init (DateTimeCurrentTimeManager * self,
                                              gpointer klass)
{
	self->priv = date_time_current_time_manager_get_instance_private (self);
	self->priv->timeout = (guint) 0;
}

static void
date_time_current_time_manager_finalize (GObject * obj)
{
	DateTimeCurrentTimeManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DATE_TIME_TYPE_CURRENT_TIME_MANAGER, DateTimeCurrentTimeManager);
	_g_date_time_unref0 (self->priv->very_next_minute);
	G_OBJECT_CLASS (date_time_current_time_manager_parent_class)->finalize (obj);
}

static GType
date_time_current_time_manager_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DateTimeCurrentTimeManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) date_time_current_time_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DateTimeCurrentTimeManager), 0, (GInstanceInitFunc) date_time_current_time_manager_instance_init, NULL };
	GType date_time_current_time_manager_type_id;
	date_time_current_time_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "DateTimeCurrentTimeManager", &g_define_type_info, 0);
	DateTimeCurrentTimeManager_private_offset = g_type_add_instance_private (date_time_current_time_manager_type_id, sizeof (DateTimeCurrentTimeManagerPrivate));
	return date_time_current_time_manager_type_id;
}

GType
date_time_current_time_manager_get_type (void)
{
	static volatile gsize date_time_current_time_manager_type_id__once = 0;
	if (g_once_init_enter (&date_time_current_time_manager_type_id__once)) {
		GType date_time_current_time_manager_type_id;
		date_time_current_time_manager_type_id = date_time_current_time_manager_get_type_once ();
		g_once_init_leave (&date_time_current_time_manager_type_id__once, date_time_current_time_manager_type_id);
	}
	return date_time_current_time_manager_type_id__once;
}

