/* application.c generated by valac 0.31.1.19-d5143, the Vala compiler
 * generated from application.vala, do not modify */

/*
 * Copyright (C) 2013  Paolo Borelli <pborelli@gnome.org>
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <libgweather/gweather.h>


#define CLOCKS_TYPE_APPLICATION (clocks_application_get_type ())
#define CLOCKS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLOCKS_TYPE_APPLICATION, ClocksApplication))
#define CLOCKS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLOCKS_TYPE_APPLICATION, ClocksApplicationClass))
#define CLOCKS_IS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLOCKS_TYPE_APPLICATION))
#define CLOCKS_IS_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLOCKS_TYPE_APPLICATION))
#define CLOCKS_APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLOCKS_TYPE_APPLICATION, ClocksApplicationClass))

typedef struct _ClocksApplication ClocksApplication;
typedef struct _ClocksApplicationClass ClocksApplicationClass;
typedef struct _ClocksApplicationPrivate ClocksApplicationPrivate;

#define CLOCKS_TYPE_SEARCH_PROVIDER (clocks_search_provider_get_type ())
#define CLOCKS_SEARCH_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLOCKS_TYPE_SEARCH_PROVIDER, ClocksSearchProvider))
#define CLOCKS_SEARCH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLOCKS_TYPE_SEARCH_PROVIDER, ClocksSearchProviderClass))
#define CLOCKS_IS_SEARCH_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLOCKS_TYPE_SEARCH_PROVIDER))
#define CLOCKS_IS_SEARCH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLOCKS_TYPE_SEARCH_PROVIDER))
#define CLOCKS_SEARCH_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLOCKS_TYPE_SEARCH_PROVIDER, ClocksSearchProviderClass))

typedef struct _ClocksSearchProvider ClocksSearchProvider;
typedef struct _ClocksSearchProviderClass ClocksSearchProviderClass;

#define CLOCKS_TYPE_WINDOW (clocks_window_get_type ())
#define CLOCKS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLOCKS_TYPE_WINDOW, ClocksWindow))
#define CLOCKS_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLOCKS_TYPE_WINDOW, ClocksWindowClass))
#define CLOCKS_IS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLOCKS_TYPE_WINDOW))
#define CLOCKS_IS_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLOCKS_TYPE_WINDOW))
#define CLOCKS_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLOCKS_TYPE_WINDOW, ClocksWindowClass))

typedef struct _ClocksWindow ClocksWindow;
typedef struct _ClocksWindowClass ClocksWindowClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (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)))
typedef struct _Block1Data Block1Data;
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _gweather_location_unref0(var) ((var == NULL) ? NULL : (var = (gweather_location_unref (var), NULL)))

struct _ClocksApplication {
	GtkApplication parent_instance;
	ClocksApplicationPrivate * priv;
};

struct _ClocksApplicationClass {
	GtkApplicationClass parent_class;
};

struct _ClocksApplicationPrivate {
	ClocksSearchProvider* search_provider;
	guint search_provider_id;
	ClocksWindow* window;
};

struct _Block1Data {
	int _ref_count_;
	ClocksApplication* self;
	GtkSettings* settings;
};


static gpointer clocks_application_parent_class = NULL;

GType clocks_application_get_type (void) G_GNUC_CONST;
GType clocks_search_provider_get_type (void) G_GNUC_CONST;
guint clocks_search_provider_register_object (void* object, GDBusConnection* connection, const gchar* path, GError** error);
GType clocks_window_get_type (void) G_GNUC_CONST;
#define CLOCKS_APPLICATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CLOCKS_TYPE_APPLICATION, ClocksApplicationPrivate))
enum  {
	CLOCKS_APPLICATION_DUMMY_PROPERTY
};
static void clocks_application_on_quit_activate (ClocksApplication* self);
static void _clocks_application_on_quit_activate_gsimple_action_activate_callback (GSimpleAction* action, GVariant* parameter, gpointer self);
void clocks_application_on_add_location_activate (ClocksApplication* self, GSimpleAction* action, GVariant* parameter);
static void _clocks_application_on_add_location_activate_gsimple_action_activate_callback (GSimpleAction* action, GVariant* parameter, gpointer self);
static void clocks_application_ensure_window (ClocksApplication* self);
ClocksWindow* clocks_window_new (ClocksApplication* app);
ClocksWindow* clocks_window_construct (GType object_type, ClocksApplication* app);
static void ___lambda56_ (ClocksApplication* self);
static void ____lambda56__gtk_widget_destroy (GtkWidget* _sender, gpointer self);
ClocksApplication* clocks_application_new (void);
ClocksApplication* clocks_application_construct (GType object_type);
ClocksSearchProvider* clocks_search_provider_new (void);
ClocksSearchProvider* clocks_search_provider_construct (GType object_type);
static void __lambda57_ (ClocksApplication* self, guint32 timestamp);
void clocks_window_show_world (ClocksWindow* self);
static void ___lambda57__clocks_search_provider_activate (ClocksSearchProvider* _sender, guint32 timestamp, gpointer self);
static gboolean clocks_application_real_dbus_register (GApplication* base, GDBusConnection* connection, const gchar* object_path, GError** error);
static void clocks_application_real_dbus_unregister (GApplication* base, GDBusConnection* connection, const gchar* object_path);
static void clocks_application_real_activate (GApplication* base);
static void clocks_application_update_theme (ClocksApplication* self, GtkSettings* settings);
void clocks_utils_load_theme_css (const gchar* theme_name);
static void clocks_application_real_startup (GApplication* base);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
void clocks_utils_load_main_css (void);
static void __lambda58_ (Block1Data* _data1_);
static void ___lambda58__g_object_notify (GObject* _sender, GParamSpec* pspec, gpointer self);
static gint clocks_application_real_handle_local_options (GApplication* base, GVariantDict* options);
void clocks_window_add_world_location (ClocksWindow* self, GWeatherLocation* location);
static void clocks_application_finalize (GObject* obj);

static const GOptionEntry CLOCKS_APPLICATION_option_entries[2] = {{"version", 'v', 0, G_OPTION_ARG_NONE, NULL, "Print version information and exit", NULL}, {NULL}};
static const GActionEntry CLOCKS_APPLICATION_action_entries[4] = {{"stop-alarm", NULL, "s"}, {"snooze-alarm", NULL, "s"}, {"quit", _clocks_application_on_quit_activate_gsimple_action_activate_callback}, {"add-location", _clocks_application_on_add_location_activate_gsimple_action_activate_callback, "v"}};

static void _clocks_application_on_quit_activate_gsimple_action_activate_callback (GSimpleAction* action, GVariant* parameter, gpointer self) {
	clocks_application_on_quit_activate ((ClocksApplication*) self);
}


static void _clocks_application_on_add_location_activate_gsimple_action_activate_callback (GSimpleAction* action, GVariant* parameter, gpointer self) {
	clocks_application_on_add_location_activate ((ClocksApplication*) self, action, parameter);
}


static void ___lambda56_ (ClocksApplication* self) {
	_g_object_unref0 (self->priv->window);
	self->priv->window = NULL;
}


static void ____lambda56__gtk_widget_destroy (GtkWidget* _sender, gpointer self) {
	___lambda56_ ((ClocksApplication*) self);
}


static void clocks_application_ensure_window (ClocksApplication* self) {
	ClocksWindow* _tmp0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->window;
	if (_tmp0_ == NULL) {
		ClocksWindow* _tmp1_ = NULL;
		ClocksWindow* _tmp2_ = NULL;
		_tmp1_ = clocks_window_new (self);
		g_object_ref_sink (_tmp1_);
		_g_object_unref0 (self->priv->window);
		self->priv->window = _tmp1_;
		_tmp2_ = self->priv->window;
		g_signal_connect_object ((GtkWidget*) _tmp2_, "destroy", (GCallback) ____lambda56__gtk_widget_destroy, self, 0);
	}
}


static void __lambda57_ (ClocksApplication* self, guint32 timestamp) {
	ClocksWindow* _tmp0_ = NULL;
	ClocksWindow* _tmp1_ = NULL;
	guint32 _tmp2_ = 0U;
	clocks_application_ensure_window (self);
	_tmp0_ = self->priv->window;
	clocks_window_show_world (_tmp0_);
	_tmp1_ = self->priv->window;
	_tmp2_ = timestamp;
	gtk_window_present_with_time ((GtkWindow*) _tmp1_, _tmp2_);
}


static void ___lambda57__clocks_search_provider_activate (ClocksSearchProvider* _sender, guint32 timestamp, gpointer self) {
	__lambda57_ ((ClocksApplication*) self, timestamp);
}


ClocksApplication* clocks_application_construct (GType object_type) {
	ClocksApplication * self = NULL;
	ClocksSearchProvider* _tmp0_ = NULL;
	ClocksSearchProvider* _tmp1_ = NULL;
	self = (ClocksApplication*) g_object_new (object_type, "application-id", "org.gnome.clocks", NULL);
	gtk_window_set_default_icon_name ("org.gnome.clocks");
	g_application_add_main_option_entries ((GApplication*) self, CLOCKS_APPLICATION_option_entries);
	g_action_map_add_action_entries ((GActionMap*) self, CLOCKS_APPLICATION_action_entries, G_N_ELEMENTS (CLOCKS_APPLICATION_action_entries), self);
	_tmp0_ = clocks_search_provider_new ();
	_g_object_unref0 (self->priv->search_provider);
	self->priv->search_provider = _tmp0_;
	_tmp1_ = self->priv->search_provider;
	g_signal_connect_object (_tmp1_, "activate", (GCallback) ___lambda57__clocks_search_provider_activate, self, 0);
	return self;
}


ClocksApplication* clocks_application_new (void) {
	return clocks_application_construct (CLOCKS_TYPE_APPLICATION);
}


static gboolean clocks_application_real_dbus_register (GApplication* base, GDBusConnection* connection, const gchar* object_path, GError** error) {
	ClocksApplication * self;
	gboolean result = FALSE;
	GError * _inner_error_ = NULL;
	self = (ClocksApplication*) base;
	g_return_val_if_fail (connection != NULL, FALSE);
	g_return_val_if_fail (object_path != NULL, FALSE);
	{
		guint _tmp0_ = 0U;
		GDBusConnection* _tmp1_ = NULL;
		const gchar* _tmp2_ = NULL;
		gchar* _tmp3_ = NULL;
		gchar* _tmp4_ = NULL;
		ClocksSearchProvider* _tmp5_ = NULL;
		guint _tmp6_ = 0U;
		guint _tmp7_ = 0U;
		_tmp1_ = connection;
		_tmp2_ = object_path;
		_tmp3_ = g_strconcat (_tmp2_, "/SearchProvider", NULL);
		_tmp4_ = _tmp3_;
		_tmp5_ = self->priv->search_provider;
		_tmp6_ = clocks_search_provider_register_object (_tmp5_, _tmp1_, _tmp4_, &_inner_error_);
		_tmp7_ = _tmp6_;
		_g_free0 (_tmp4_);
		_tmp0_ = _tmp7_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			if (_inner_error_->domain == G_IO_ERROR) {
				goto __catch0_g_io_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return FALSE;
		}
		self->priv->search_provider_id = _tmp0_;
	}
	goto __finally0;
	__catch0_g_io_error:
	{
		GError* _error_ = NULL;
		GError* _tmp8_ = NULL;
		const gchar* _tmp9_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_tmp8_ = _error_;
		_tmp9_ = _tmp8_->message;
		g_printerr ("Could not register search provider service: %s\n", _tmp9_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return FALSE;
	}
	result = TRUE;
	return result;
}


static void clocks_application_real_dbus_unregister (GApplication* base, GDBusConnection* connection, const gchar* object_path) {
	ClocksApplication * self;
	guint _tmp0_ = 0U;
	self = (ClocksApplication*) base;
	g_return_if_fail (connection != NULL);
	g_return_if_fail (object_path != NULL);
	_tmp0_ = self->priv->search_provider_id;
	if (_tmp0_ != ((guint) 0)) {
		GDBusConnection* _tmp1_ = NULL;
		guint _tmp2_ = 0U;
		_tmp1_ = connection;
		_tmp2_ = self->priv->search_provider_id;
		g_dbus_connection_unregister_object (_tmp1_, _tmp2_);
		self->priv->search_provider_id = (guint) 0;
	}
}


static void clocks_application_real_activate (GApplication* base) {
	ClocksApplication * self;
	ClocksWindow* _tmp0_ = NULL;
	self = (ClocksApplication*) base;
	G_APPLICATION_CLASS (clocks_application_parent_class)->activate ((GApplication*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_application_get_type (), GtkApplication));
	clocks_application_ensure_window (self);
	_tmp0_ = self->priv->window;
	gtk_window_present ((GtkWindow*) _tmp0_);
}


static void clocks_application_update_theme (ClocksApplication* self, GtkSettings* settings) {
	gchar* theme_name = NULL;
	GtkSettings* _tmp0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (settings != NULL);
	_tmp0_ = settings;
	g_object_get ((GObject*) _tmp0_, "gtk-theme-name", &theme_name, NULL);
	clocks_utils_load_theme_css (theme_name);
	_g_free0 (theme_name);
}


static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		ClocksApplication* self;
		self = _data1_->self;
		_g_object_unref0 (_data1_->settings);
		_g_object_unref0 (self);
		g_slice_free (Block1Data, _data1_);
	}
}


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


static void __lambda58_ (Block1Data* _data1_) {
	ClocksApplication* self;
	self = _data1_->self;
	clocks_application_update_theme (self, _data1_->settings);
}


static void ___lambda58__g_object_notify (GObject* _sender, GParamSpec* pspec, gpointer self) {
	__lambda58_ (self);
}


static void clocks_application_real_startup (GApplication* base) {
	ClocksApplication * self;
	Block1Data* _data1_;
	GtkSettings* _tmp0_ = NULL;
	GtkSettings* _tmp1_ = NULL;
	self = (ClocksApplication*) base;
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	_data1_->self = g_object_ref (self);
	G_APPLICATION_CLASS (clocks_application_parent_class)->startup ((GApplication*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_application_get_type (), GtkApplication));
	clocks_utils_load_main_css ();
	_tmp0_ = gtk_settings_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_data1_->settings = _tmp1_;
	g_signal_connect_data ((GObject*) _data1_->settings, "notify::gtk-theme-name", (GCallback) ___lambda58__g_object_notify, block1_data_ref (_data1_), (GClosureNotify) block1_data_unref, 0);
	clocks_application_update_theme (self, _data1_->settings);
	gtk_application_add_accelerator ((GtkApplication*) self, "<Primary>n", "win.new", NULL);
	gtk_application_add_accelerator ((GtkApplication*) self, "<Primary>a", "win.select-all", NULL);
	block1_data_unref (_data1_);
	_data1_ = NULL;
}


static gint clocks_application_real_handle_local_options (GApplication* base, GVariantDict* options) {
	ClocksApplication * self;
	gint result = 0;
	GVariantDict* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	self = (ClocksApplication*) base;
	g_return_val_if_fail (options != NULL, 0);
	_tmp0_ = options;
	_tmp1_ = g_variant_dict_contains (_tmp0_, "version");
	if (_tmp1_) {
		const gchar* _tmp2_ = NULL;
		_tmp2_ = g_get_application_name ();
		g_print ("%s %s\n", _tmp2_, VERSION);
		result = 0;
		return result;
	}
	result = -1;
	return result;
}


static gpointer _gweather_location_ref0 (gpointer self) {
	return self ? gweather_location_ref (self) : NULL;
}


void clocks_application_on_add_location_activate (ClocksApplication* self, GSimpleAction* action, GVariant* parameter) {
	GVariant* _tmp0_ = NULL;
	ClocksWindow* _tmp1_ = NULL;
	ClocksWindow* _tmp2_ = NULL;
	GWeatherLocation* world = NULL;
	GWeatherLocation* _tmp3_ = NULL;
	GWeatherLocation* _tmp4_ = NULL;
	GWeatherLocation* location = NULL;
	GWeatherLocation* _tmp5_ = NULL;
	GVariant* _tmp6_ = NULL;
	GVariant* _tmp7_ = NULL;
	GVariant* _tmp8_ = NULL;
	GWeatherLocation* _tmp9_ = NULL;
	GWeatherLocation* _tmp10_ = NULL;
	GWeatherLocation* _tmp11_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (action != NULL);
	_tmp0_ = parameter;
	if (_tmp0_ == NULL) {
		return;
	}
	clocks_application_ensure_window (self);
	_tmp1_ = self->priv->window;
	clocks_window_show_world (_tmp1_);
	_tmp2_ = self->priv->window;
	gtk_window_present ((GtkWindow*) _tmp2_);
	_tmp3_ = gweather_location_get_world ();
	_tmp4_ = _gweather_location_ref0 (_tmp3_);
	world = _tmp4_;
	_tmp5_ = world;
	_tmp6_ = parameter;
	_tmp7_ = g_variant_get_child_value (_tmp6_, (gsize) 0);
	_tmp8_ = _tmp7_;
	_tmp9_ = gweather_location_deserialize (_tmp5_, _tmp8_);
	_tmp10_ = _tmp9_;
	_g_variant_unref0 (_tmp8_);
	location = _tmp10_;
	_tmp11_ = location;
	if (_tmp11_ != NULL) {
		ClocksWindow* _tmp12_ = NULL;
		GWeatherLocation* _tmp13_ = NULL;
		_tmp12_ = self->priv->window;
		_tmp13_ = location;
		clocks_window_add_world_location (_tmp12_, _tmp13_);
	}
	_gweather_location_unref0 (location);
	_gweather_location_unref0 (world);
}


static void clocks_application_on_quit_activate (ClocksApplication* self) {
	g_return_if_fail (self != NULL);
	g_application_quit ((GApplication*) self);
}


static void clocks_application_class_init (ClocksApplicationClass * klass) {
	clocks_application_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ClocksApplicationPrivate));
	((GApplicationClass *) klass)->dbus_register = clocks_application_real_dbus_register;
	((GApplicationClass *) klass)->dbus_unregister = clocks_application_real_dbus_unregister;
	((GApplicationClass *) klass)->activate = clocks_application_real_activate;
	((GApplicationClass *) klass)->startup = clocks_application_real_startup;
	((GApplicationClass *) klass)->handle_local_options = clocks_application_real_handle_local_options;
	G_OBJECT_CLASS (klass)->finalize = clocks_application_finalize;
}


static void clocks_application_instance_init (ClocksApplication * self) {
	self->priv = CLOCKS_APPLICATION_GET_PRIVATE (self);
	self->priv->search_provider_id = (guint) 0;
}


static void clocks_application_finalize (GObject* obj) {
	ClocksApplication * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CLOCKS_TYPE_APPLICATION, ClocksApplication);
	_g_object_unref0 (self->priv->search_provider);
	_g_object_unref0 (self->priv->window);
	G_OBJECT_CLASS (clocks_application_parent_class)->finalize (obj);
}


GType clocks_application_get_type (void) {
	static volatile gsize clocks_application_type_id__volatile = 0;
	if (g_once_init_enter (&clocks_application_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ClocksApplicationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) clocks_application_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ClocksApplication), 0, (GInstanceInitFunc) clocks_application_instance_init, NULL };
		GType clocks_application_type_id;
		clocks_application_type_id = g_type_register_static (gtk_application_get_type (), "ClocksApplication", &g_define_type_info, 0);
		g_once_init_leave (&clocks_application_type_id__volatile, clocks_application_type_id);
	}
	return clocks_application_type_id__volatile;
}



