/* application.c generated by valac 0.26.0.27-f1f8, 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 <gdk/gdk.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)))
#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;
};


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 ___lambda50_ (ClocksApplication* self);
static void ____lambda50__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 __lambda51_ (ClocksApplication* self, guint32 timestamp);
void clocks_window_show_world (ClocksWindow* self);
static void ___lambda51__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_real_startup (GApplication* base);
GtkCssProvider* clocks_utils_load_css (const gchar* css);
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 ___lambda50_ (ClocksApplication* self) {
	_g_object_unref0 (self->priv->window);
	self->priv->window = NULL;
}


static void ____lambda50__gtk_widget_destroy (GtkWidget* _sender, gpointer self) {
	___lambda50_ ((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) ____lambda50__gtk_widget_destroy, self, 0);
	}
}


static void __lambda51_ (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 ___lambda51__clocks_search_provider_activate (ClocksSearchProvider* _sender, guint32 timestamp, gpointer self) {
	__lambda51_ ((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);
	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) ___lambda51__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;
	clocks_application_ensure_window (self);
	_tmp0_ = self->priv->window;
	gtk_window_present ((GtkWindow*) _tmp0_);
}


static void clocks_application_real_startup (GApplication* base) {
	ClocksApplication * self;
	GtkCssProvider* css_provider = NULL;
	GtkCssProvider* _tmp0_ = NULL;
	GdkScreen* _tmp1_ = NULL;
	self = (ClocksApplication*) base;
	G_APPLICATION_CLASS (clocks_application_parent_class)->startup ((GApplication*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_application_get_type (), GtkApplication));
	_tmp0_ = clocks_utils_load_css ("gnome-clocks.css");
	css_provider = _tmp0_;
	_tmp1_ = gdk_screen_get_default ();
	gtk_style_context_add_provider_for_screen (_tmp1_, (GtkStyleProvider*) css_provider, (guint) GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	gtk_application_add_accelerator ((GtkApplication*) self, "<Primary>n", "win.new", NULL);
	gtk_application_add_accelerator ((GtkApplication*) self, "<Primary>a", "win.select-all", NULL);
	_g_object_unref0 (css_provider);
}


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



