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

/*
trayclock
copyright 2010 jezra lickter http://www.jezra.net

trayclock 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.

trayclock 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 trayclock.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include <stdio.h>
#include <glib/gi18n-lib.h>
#include <config.h>
#include <gobject/gvaluecollector.h>


#define TYPE_MAIN_CONTROLLER (main_controller_get_type ())
#define MAIN_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAIN_CONTROLLER, main_controller))
#define MAIN_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAIN_CONTROLLER, main_controllerClass))
#define IS_MAIN_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAIN_CONTROLLER))
#define IS_MAIN_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAIN_CONTROLLER))
#define MAIN_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAIN_CONTROLLER, main_controllerClass))

typedef struct _main_controller main_controller;
typedef struct _main_controllerClass main_controllerClass;
typedef struct _main_controllerPrivate main_controllerPrivate;

#define TYPE_ICON_MENU (icon_menu_get_type ())
#define ICON_MENU(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ICON_MENU, IconMenu))
#define ICON_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ICON_MENU, IconMenuClass))
#define IS_ICON_MENU(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ICON_MENU))
#define IS_ICON_MENU_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ICON_MENU))
#define ICON_MENU_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ICON_MENU, IconMenuClass))

typedef struct _IconMenu IconMenu;
typedef struct _IconMenuClass IconMenuClass;

#define TYPE_CONFIGURATION (configuration_get_type ())
#define CONFIGURATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CONFIGURATION, Configuration))
#define CONFIGURATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CONFIGURATION, ConfigurationClass))
#define IS_CONFIGURATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CONFIGURATION))
#define IS_CONFIGURATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CONFIGURATION))
#define CONFIGURATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CONFIGURATION, ConfigurationClass))

typedef struct _Configuration Configuration;
typedef struct _ConfigurationClass ConfigurationClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _configuration_unref0(var) ((var == NULL) ? NULL : (var = (configuration_unref (var), NULL)))

#define TYPE_PREFERENCES (preferences_get_type ())
#define PREFERENCES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PREFERENCES, Preferences))
#define PREFERENCES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PREFERENCES, PreferencesClass))
#define IS_PREFERENCES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PREFERENCES))
#define IS_PREFERENCES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PREFERENCES))
#define PREFERENCES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PREFERENCES, PreferencesClass))

typedef struct _Preferences Preferences;
typedef struct _PreferencesClass PreferencesClass;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ParamSpecmain_controller ParamSpecmain_controller;
#define _main_controller_unref0(var) ((var == NULL) ? NULL : (var = (main_controller_unref (var), NULL)))

struct _main_controller {
	GTypeInstance parent_instance;
	volatile int ref_count;
	main_controllerPrivate * priv;
};

struct _main_controllerClass {
	GTypeClass parent_class;
	void (*finalize) (main_controller *self);
};

struct _main_controllerPrivate {
	GtkStatusIcon* si;
	char* app_name;
	char* app_version;
	struct tm now;
	IconMenu* iconMenu;
	Configuration* config;
	gboolean displaying_preferences;
	char* color_background;
	char* color_ticks;
	char* color_border;
	char* color_hours;
	char* color_minutes;
	char* datetime_format;
};

typedef enum  {
	CONFIG_ERROR_CREATE_DIR_FAILED,
	CONFIG_ERROR_FILE_NOT_FOUND,
	CONFIG_ERROR_DOCUMENT_EMPTY,
	CONFIG_ERROR_READ_FAILED,
	CONFIG_ERROR_WRITE_FAILED,
	CONFIG_ERROR_NO_KEY_VALUE
} ConfigError;
#define CONFIG_ERROR config_error_quark ()
struct _ParamSpecmain_controller {
	GParamSpec parent_instance;
};


static gpointer main_controller_parent_class = NULL;

gpointer main_controller_ref (gpointer instance);
void main_controller_unref (gpointer instance);
GParamSpec* param_spec_main_controller (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_main_controller (GValue* value, gpointer v_object);
void value_take_main_controller (GValue* value, gpointer v_object);
gpointer value_get_main_controller (const GValue* value);
GType main_controller_get_type (void);
GType icon_menu_get_type (void);
gpointer configuration_ref (gpointer instance);
void configuration_unref (gpointer instance);
GParamSpec* param_spec_configuration (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_configuration (GValue* value, gpointer v_object);
void value_take_configuration (GValue* value, gpointer v_object);
gpointer value_get_configuration (const GValue* value);
GType configuration_get_type (void);
#define MAIN_CONTROLLER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_MAIN_CONTROLLER, main_controllerPrivate))
enum  {
	MAIN_CONTROLLER_DUMMY_PROPERTY
};
Configuration* configuration_new (const char* config_file_path);
Configuration* configuration_construct (GType object_type, const char* config_file_path);
char* configuration_get_value (Configuration* self, const char* group, const char* key);
static void main_controller_status_icon_menu (main_controller* self, guint button, guint time);
static void _lambda2_ (GtkStatusIcon* source, guint button, guint time, main_controller* self);
static void __lambda2__gtk_status_icon_popup_menu (GtkStatusIcon* _sender, guint button, guint activate_time, gpointer self);
IconMenu* icon_menu_new (void);
IconMenu* icon_menu_construct (GType object_type);
static void main_controller_display_about_dialog (main_controller* self);
static void _main_controller_display_about_dialog_icon_menu_show_about (IconMenu* _sender, gpointer self);
static void main_controller_display_preferences_dialog (main_controller* self);
static void _main_controller_display_preferences_dialog_icon_menu_show_preferences (IconMenu* _sender, gpointer self);
static gboolean main_controller_get_time (main_controller* self);
static gboolean _main_controller_get_time_gsource_func (gpointer self);
main_controller* main_controller_new (const char* name, const char* version);
main_controller* main_controller_construct (GType object_type, const char* name, const char* version);
static void main_controller_update_status_icon (main_controller* self);
void main_controller_run (main_controller* self);
static void main_controller_preferences_destroyed (main_controller* self);
Preferences* preferences_new (void);
Preferences* preferences_construct (GType object_type);
GType preferences_get_type (void);
void preferences_set_colors (Preferences* self, const char* background, const char* ticks, const char* border, const char* hours, const char* minutes);
void preferences_set_datetime_format (Preferences* self, const char* datetime_format);
static void _main_controller_preferences_destroyed_preferences_window_destroyed (Preferences* _sender, gpointer self);
static void main_controller_update_and_save_preferences (main_controller* self, GdkColor* background, GdkColor* ticks, GdkColor* border, GdkColor* hours, GdkColor* minutes, const char* format);
static void _lambda3_ (GdkColor* bg, GdkColor* t, GdkColor* b, GdkColor* h, GdkColor* m, const char* f, main_controller* self);
static void __lambda3__preferences_emit_preferences (Preferences* _sender, GdkColor* background, GdkColor* ticks, GdkColor* border, GdkColor* hours, GdkColor* minutes, const char* format, gpointer self);
void preferences_display (Preferences* self);
static char* main_controller_hex_color (main_controller* self, GdkColor* c);
void configuration_set_key_value (Configuration* self, const char* group, const char* key, const char* val);
GQuark config_error_quark (void);
void configuration_save_config (Configuration* self, GError** error);
static void main_controller_die (main_controller* self, const char* str);
static void main_controller_finalize (main_controller* obj);
void _vala_main (char** args, int args_length1);



static void _lambda2_ (GtkStatusIcon* source, guint button, guint time, main_controller* self) {
	g_return_if_fail (source != NULL);
	main_controller_status_icon_menu (self, button, time);
}


static void __lambda2__gtk_status_icon_popup_menu (GtkStatusIcon* _sender, guint button, guint activate_time, gpointer self) {
	_lambda2_ (_sender, button, activate_time, self);
}


static void _main_controller_display_about_dialog_icon_menu_show_about (IconMenu* _sender, gpointer self) {
	main_controller_display_about_dialog (self);
}


static void _main_controller_display_preferences_dialog_icon_menu_show_preferences (IconMenu* _sender, gpointer self) {
	main_controller_display_preferences_dialog (self);
}


static gboolean _main_controller_get_time_gsource_func (gpointer self) {
	return main_controller_get_time (self);
}


main_controller* main_controller_construct (GType object_type, const char* name, const char* version) {
	main_controller* self;
	char* default_background;
	char* default_ticks;
	char* default_border;
	char* default_hours;
	char* default_minutes;
	char* default_format;
	char* _tmp0_;
	char* _tmp1_;
	char* config_file_path;
	Configuration* _tmp2_;
	char* _tmp3_;
	char* _tmp5_;
	char* _tmp7_;
	char* _tmp9_;
	char* _tmp11_;
	char* _tmp13_;
	GtkStatusIcon* _tmp15_;
	IconMenu* _tmp16_;
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (version != NULL, NULL);
	self = (main_controller*) g_type_create_instance (object_type);
	default_background = g_strdup ("#DDDDDD");
	default_ticks = g_strdup ("#000000");
	default_border = g_strdup ("#000000");
	default_hours = g_strdup ("#000000");
	default_minutes = g_strdup ("#000000");
	default_format = g_strdup ("%I:%M");
	self->priv->displaying_preferences = FALSE;
	self->priv->app_name = (_tmp0_ = g_strdup (name), _g_free0 (self->priv->app_name), _tmp0_);
	self->priv->app_version = (_tmp1_ = g_strdup (version), _g_free0 (self->priv->app_version), _tmp1_);
	config_file_path = g_build_filename (g_get_user_config_dir (), name, "config", NULL);
	self->priv->config = (_tmp2_ = configuration_new (config_file_path), _configuration_unref0 (self->priv->config), _tmp2_);
	self->priv->color_background = (_tmp3_ = configuration_get_value (self->priv->config, "colors", "background"), _g_free0 (self->priv->color_background), _tmp3_);
	if (self->priv->color_background == NULL) {
		char* _tmp4_;
		self->priv->color_background = (_tmp4_ = g_strdup (default_background), _g_free0 (self->priv->color_background), _tmp4_);
	}
	self->priv->color_ticks = (_tmp5_ = configuration_get_value (self->priv->config, "colors", "ticks"), _g_free0 (self->priv->color_ticks), _tmp5_);
	if (self->priv->color_ticks == NULL) {
		char* _tmp6_;
		self->priv->color_ticks = (_tmp6_ = g_strdup (default_ticks), _g_free0 (self->priv->color_ticks), _tmp6_);
	}
	self->priv->color_border = (_tmp7_ = configuration_get_value (self->priv->config, "colors", "border"), _g_free0 (self->priv->color_border), _tmp7_);
	if (self->priv->color_border == NULL) {
		char* _tmp8_;
		self->priv->color_border = (_tmp8_ = g_strdup (default_border), _g_free0 (self->priv->color_border), _tmp8_);
	}
	self->priv->color_hours = (_tmp9_ = configuration_get_value (self->priv->config, "colors", "hours"), _g_free0 (self->priv->color_hours), _tmp9_);
	if (self->priv->color_hours == NULL) {
		char* _tmp10_;
		self->priv->color_hours = (_tmp10_ = g_strdup (default_hours), _g_free0 (self->priv->color_hours), _tmp10_);
	}
	self->priv->color_minutes = (_tmp11_ = configuration_get_value (self->priv->config, "colors", "minutes"), _g_free0 (self->priv->color_minutes), _tmp11_);
	if (self->priv->color_minutes == NULL) {
		char* _tmp12_;
		self->priv->color_minutes = (_tmp12_ = g_strdup (default_minutes), _g_free0 (self->priv->color_minutes), _tmp12_);
	}
	self->priv->datetime_format = (_tmp13_ = configuration_get_value (self->priv->config, "datetime", "format"), _g_free0 (self->priv->datetime_format), _tmp13_);
	if (self->priv->datetime_format == NULL) {
		char* _tmp14_;
		self->priv->datetime_format = (_tmp14_ = g_strdup (default_format), _g_free0 (self->priv->datetime_format), _tmp14_);
	}
	self->priv->si = (_tmp15_ = gtk_status_icon_new (), _g_object_unref0 (self->priv->si), _tmp15_);
	g_signal_connect (self->priv->si, "popup-menu", (GCallback) __lambda2__gtk_status_icon_popup_menu, self);
	self->priv->iconMenu = (_tmp16_ = g_object_ref_sink (icon_menu_new ()), _g_object_unref0 (self->priv->iconMenu), _tmp16_);
	g_signal_connect (self->priv->iconMenu, "show-about", (GCallback) _main_controller_display_about_dialog_icon_menu_show_about, self);
	g_signal_connect (self->priv->iconMenu, "show-preferences", (GCallback) _main_controller_display_preferences_dialog_icon_menu_show_preferences, self);
	g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 2000, _main_controller_get_time_gsource_func, main_controller_ref (self), main_controller_unref);
	main_controller_get_time (self);
	_g_free0 (default_background);
	_g_free0 (default_ticks);
	_g_free0 (default_border);
	_g_free0 (default_hours);
	_g_free0 (default_minutes);
	_g_free0 (default_format);
	_g_free0 (config_file_path);
	return self;
}


main_controller* main_controller_new (const char* name, const char* version) {
	return main_controller_construct (TYPE_MAIN_CONTROLLER, name, version);
}


static void g_time_local (time_t time, struct tm* result) {
	struct tm _result_ = {0};
	localtime_r (&time, &_result_);
	*result = _result_;
	return;
}


static gboolean main_controller_get_time (main_controller* self) {
	gboolean result = FALSE;
	struct tm _tmp0_ = {0};
	g_return_val_if_fail (self != NULL, FALSE);
	self->priv->now = (g_time_local (time (NULL), &_tmp0_), _tmp0_);
	main_controller_update_status_icon (self);
	result = TRUE;
	return result;
}


void main_controller_run (main_controller* self) {
	g_return_if_fail (self != NULL);
	gtk_main ();
}


static void main_controller_preferences_destroyed (main_controller* self) {
	g_return_if_fail (self != NULL);
	self->priv->displaying_preferences = FALSE;
}


static void _main_controller_preferences_destroyed_preferences_window_destroyed (Preferences* _sender, gpointer self) {
	main_controller_preferences_destroyed (self);
}


static void _lambda3_ (GdkColor* bg, GdkColor* t, GdkColor* b, GdkColor* h, GdkColor* m, const char* f, main_controller* self) {
	g_return_if_fail (f != NULL);
	main_controller_update_and_save_preferences (self, bg, t, b, h, m, f);
}


static void __lambda3__preferences_emit_preferences (Preferences* _sender, GdkColor* background, GdkColor* ticks, GdkColor* border, GdkColor* hours, GdkColor* minutes, const char* format, gpointer self) {
	_lambda3_ (background, ticks, border, hours, minutes, format, self);
}


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


static void main_controller_display_preferences_dialog (main_controller* self) {
	g_return_if_fail (self != NULL);
	if (!self->priv->displaying_preferences) {
		Preferences* preferences;
		GdkPixbuf* pb;
		preferences = g_object_ref_sink (preferences_new ());
		self->priv->displaying_preferences = TRUE;
		preferences_set_colors (preferences, self->priv->color_background, self->priv->color_ticks, self->priv->color_border, self->priv->color_hours, self->priv->color_minutes);
		preferences_set_datetime_format (preferences, self->priv->datetime_format);
		g_signal_connect (preferences, "window-destroyed", (GCallback) _main_controller_preferences_destroyed_preferences_window_destroyed, self);
		g_signal_connect (preferences, "emit-preferences", (GCallback) __lambda3__preferences_emit_preferences, self);
		pb = _g_object_ref0 (gtk_status_icon_get_pixbuf (self->priv->si));
		gtk_window_set_icon ((GtkWindow*) preferences, pb);
		preferences_display (preferences);
		_g_object_unref0 (preferences);
		_g_object_unref0 (pb);
	}
}


static void main_controller_update_and_save_preferences (main_controller* self, GdkColor* background, GdkColor* ticks, GdkColor* border, GdkColor* hours, GdkColor* minutes, const char* format) {
	GError * _inner_error_;
	char* _tmp0_;
	char* _tmp1_;
	char* _tmp2_;
	char* _tmp3_;
	char* _tmp4_;
	char* _tmp5_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (format != NULL);
	_inner_error_ = NULL;
	fprintf (stdout, "saving prefs");
	self->priv->color_ticks = (_tmp0_ = main_controller_hex_color (self, ticks), _g_free0 (self->priv->color_ticks), _tmp0_);
	configuration_set_key_value (self->priv->config, "colors", "ticks", self->priv->color_ticks);
	self->priv->color_hours = (_tmp1_ = main_controller_hex_color (self, hours), _g_free0 (self->priv->color_hours), _tmp1_);
	configuration_set_key_value (self->priv->config, "colors", "hours", self->priv->color_hours);
	self->priv->color_minutes = (_tmp2_ = main_controller_hex_color (self, minutes), _g_free0 (self->priv->color_minutes), _tmp2_);
	configuration_set_key_value (self->priv->config, "colors", "minutes", self->priv->color_minutes);
	self->priv->color_border = (_tmp3_ = main_controller_hex_color (self, border), _g_free0 (self->priv->color_border), _tmp3_);
	configuration_set_key_value (self->priv->config, "colors", "border", self->priv->color_border);
	self->priv->color_background = (_tmp4_ = main_controller_hex_color (self, background), _g_free0 (self->priv->color_background), _tmp4_);
	configuration_set_key_value (self->priv->config, "colors", "background", self->priv->color_background);
	self->priv->datetime_format = (_tmp5_ = g_strdup (format), _g_free0 (self->priv->datetime_format), _tmp5_);
	configuration_set_key_value (self->priv->config, "datetime", "format", self->priv->datetime_format);
	{
		configuration_save_config (self->priv->config, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == CONFIG_ERROR) {
				goto __catch0_config_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;
		}
	}
	goto __finally0;
	__catch0_config_error:
	{
		GError * err;
		err = _inner_error_;
		_inner_error_ = NULL;
		{
			fprintf (stderr, "%s", err->message);
			_g_error_free0 (err);
		}
	}
	__finally0:
	if (_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;
	}
	main_controller_update_status_icon (self);
}


static void main_controller_display_about_dialog (main_controller* self) {
	GtkAboutDialog* aboutDialog;
	GdkPixbuf* pb;
	g_return_if_fail (self != NULL);
	aboutDialog = g_object_ref_sink ((GtkAboutDialog*) gtk_about_dialog_new ());
	pb = _g_object_ref0 (gtk_status_icon_get_pixbuf (self->priv->si));
	gtk_window_set_icon ((GtkWindow*) aboutDialog, pb);
	gtk_about_dialog_set_program_name (aboutDialog, self->priv->app_name);
	gtk_about_dialog_set_version (aboutDialog, self->priv->app_version);
	gtk_about_dialog_set_comments (aboutDialog, _ ("An analog clock for the system tray."));
	gtk_about_dialog_set_website (aboutDialog, "http://www.jezra.net/projects/trayclock");
	gtk_about_dialog_set_copyright (aboutDialog, "©2010 Jezra J Lickter");
	gtk_widget_show_all ((GtkWidget*) aboutDialog);
	gtk_dialog_run ((GtkDialog*) aboutDialog);
	gtk_object_destroy ((GtkObject*) aboutDialog);
	_g_object_unref0 (aboutDialog);
	_g_object_unref0 (pb);
}


static char* string_substring (const char* self, glong offset, glong len) {
	char* result = NULL;
	glong string_length;
	const char* start;
	g_return_val_if_fail (self != NULL, NULL);
	string_length = g_utf8_strlen (self, -1);
	if (offset < 0) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= 0, NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < 0) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	start = g_utf8_offset_to_pointer (self, offset);
	result = g_strndup (start, ((gchar*) g_utf8_offset_to_pointer (start, len)) - ((gchar*) start));
	return result;
}


static char* main_controller_hex_color (main_controller* self, GdkColor* c) {
	char* result = NULL;
	char* ret;
	char* color;
	char* red;
	char* blue;
	char* green;
	char* _tmp2_;
	char* _tmp1_;
	char* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	ret = g_strdup ("#");
	color = gdk_color_to_string (c);
	red = string_substring (color, (glong) 1, (glong) 2);
	blue = string_substring (color, (glong) 5, (glong) 2);
	green = string_substring (color, (glong) 9, (glong) 2);
	ret = (_tmp2_ = g_strconcat (ret, _tmp1_ = g_strconcat (_tmp0_ = g_strconcat (red, blue, NULL), green, NULL), NULL), _g_free0 (ret), _tmp2_);
	_g_free0 (_tmp1_);
	_g_free0 (_tmp0_);
	result = ret;
	_g_free0 (color);
	_g_free0 (red);
	_g_free0 (blue);
	_g_free0 (green);
	return result;
}


static void main_controller_status_icon_menu (main_controller* self, guint button, guint time) {
	g_return_if_fail (self != NULL);
	gtk_menu_popup ((GtkMenu*) self->priv->iconMenu, NULL, NULL, NULL, NULL, button, (guint32) time);
}


static char* g_time_format (struct tm *self, const char* format) {
	char* result = NULL;
	gchar* _tmp0_;
	gint _buffer_size_;
	gint buffer_length1;
	gchar* buffer;
	g_return_val_if_fail (format != NULL, NULL);
	buffer = (_tmp0_ = g_new0 (gchar, 64), buffer_length1 = 64, _buffer_size_ = buffer_length1, _tmp0_);
	strftime (buffer, buffer_length1, format, &(*self));
	result = g_strdup ((const char*) buffer);
	buffer = (g_free (buffer), NULL);
	return result;
}


static glong string_get_length (const char* self) {
	glong result;
	g_return_val_if_fail (self != NULL, 0L);
	result = g_utf8_strlen (self, -1);
	return result;
}


static void main_controller_update_status_icon (main_controller* self) {
	GError * _inner_error_;
	GdkPixbufLoader* ploader;
	GdkPixbuf* pb;
	char* svg_str;
	char* shours;
	char* sminutes;
	char* datetime;
	gint iminutes = 0;
	gint ihours = 0;
	gint deg_hours = 0;
	gint deg_minutes = 0;
	char* _tmp0_;
	char* _tmp1_;
	char* _tmp2_;
	char* _tmp3_;
	char* _tmp5_;
	char* _tmp4_;
	char* _tmp7_;
	char* _tmp6_;
	char* _tmp9_;
	char* _tmp8_;
	char* _tmp11_;
	char* _tmp10_;
	char* _tmp12_;
	guchar* _tmp14_;
	gint _svg_data_size_;
	gint svg_data_length1;
	gint _tmp13_;
	guchar* svg_data;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	ploader = NULL;
	pb = NULL;
	svg_str = NULL;
	shours = g_strdup ("");
	sminutes = g_strdup ("");
	datetime = g_strdup ("");
	shours = (_tmp0_ = g_time_format (&self->priv->now, "%I"), _g_free0 (shours), _tmp0_);
	sminutes = (_tmp1_ = g_time_format (&self->priv->now, "%M"), _g_free0 (sminutes), _tmp1_);
	datetime = (_tmp2_ = g_time_format (&self->priv->now, self->priv->datetime_format), _g_free0 (datetime), _tmp2_);
	iminutes = atoi (sminutes);
	deg_minutes = iminutes * 6;
	ihours = atoi (shours);
	if (ihours == 12) {
		ihours = 0;
	}
	deg_hours = (ihours * 30) + (iminutes / 2);
	svg_str = (_tmp3_ = g_strdup ("<?xml version=\"1.0\" standalone=\"no\"?>\n" \
"        <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www." \
"w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" \
"        <svg width=\"100px\" height=\"100px\" version=\"1.1\" xmlns=\"" \
"http://www.w3.org/2000/svg\">"), _g_free0 (svg_str), _tmp3_);
	svg_str = (_tmp5_ = g_strconcat (svg_str, _tmp4_ = g_strdup_printf ("<circle cx=\"50\" cy=\"50\" r=\"50\" fill=\"%s\" stroke=\"%s\" stroke-" \
"width=\"1\" />", self->priv->color_background, self->priv->color_border), NULL), _g_free0 (svg_str), _tmp5_);
	_g_free0 (_tmp4_);
	svg_str = (_tmp7_ = g_strconcat (svg_str, _tmp6_ = g_strdup_printf ("<circle id=\"hour_dashes\" cx=\"50\" cy=\"50\" r=\"44\" fill=\"#fff\" " \
"fill-opacity=\"0\"\n" \
"        stroke=\"%s\" stroke-width=\"7\" stroke-dasharray=\"3, 20\"\n" \
"            transform=\"rotate(-1,50,50)\" />", self->priv->color_ticks), NULL), _g_free0 (svg_str), _tmp7_);
	_g_free0 (_tmp6_);
	svg_str = (_tmp9_ = g_strconcat (svg_str, _tmp8_ = g_strdup_printf ("<polygon id=\"minutes_hand\" points=\"45,52 55,52 52,5 48,5 \" fill=\"" \
"%s\"\n" \
"            stroke=\"#FFF\" stroke-width=\"0\"\n" \
"            transform=\"rotate(%d,50,50)\" />", self->priv->color_minutes, deg_minutes), NULL), _g_free0 (svg_str), _tmp9_);
	_g_free0 (_tmp8_);
	svg_str = (_tmp11_ = g_strconcat (svg_str, _tmp10_ = g_strdup_printf ("<polygon id =\"hours_hand\" points=\"45,52 55,52 52,25 48,25\" fill=\"" \
"%s\"\n" \
"            stroke=\"#FFF\" stroke-width=\"0\"\n" \
"            transform=\"rotate(%d,50,50)\" />", self->priv->color_hours, deg_hours), NULL), _g_free0 (svg_str), _tmp11_);
	_g_free0 (_tmp10_);
	svg_str = (_tmp12_ = g_strconcat (svg_str, "</svg>\n", NULL), _g_free0 (svg_str), _tmp12_);
	svg_data = (_tmp14_ = g_new0 (guchar, _tmp13_ = string_get_length (svg_str)), svg_data_length1 = _tmp13_, _svg_data_size_ = svg_data_length1, _tmp14_);
	{
		gint i;
		i = 0;
		{
			gboolean _tmp15_;
			_tmp15_ = TRUE;
			while (TRUE) {
				if (!_tmp15_) {
					i++;
				}
				_tmp15_ = FALSE;
				if (!(i < string_get_length (svg_str))) {
					break;
				}
				svg_data[i] = (guchar) g_utf8_get_char (g_utf8_offset_to_pointer (svg_str, i));
			}
		}
	}
	{
		GdkPixbufLoader* _tmp16_;
		GdkPixbufLoader* _tmp17_;
		GdkPixbuf* _tmp18_;
		_tmp16_ = gdk_pixbuf_loader_new_with_type ("svg", &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch1_g_error;
		}
		ploader = (_tmp17_ = _tmp16_, _g_object_unref0 (ploader), _tmp17_);
		gdk_pixbuf_loader_write (ploader, svg_data, (gsize) string_get_length (svg_str), &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch1_g_error;
		}
		gdk_pixbuf_loader_close (ploader, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch1_g_error;
		}
		pb = (_tmp18_ = _g_object_ref0 (gdk_pixbuf_loader_get_pixbuf (ploader)), _g_object_unref0 (pb), _tmp18_);
		gtk_status_icon_set_from_pixbuf (self->priv->si, pb);
		gtk_status_icon_set_tooltip_text (self->priv->si, datetime);
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError * er;
		er = _inner_error_;
		_inner_error_ = NULL;
		{
			char* _tmp21_;
			char* _tmp20_;
			char* _tmp19_;
			main_controller_die (self, _tmp21_ = g_strconcat (_tmp19_ = g_strconcat ("main pixbuf error: ", er->message, NULL), _tmp20_ = g_strdup_printf ("\n%s\n", svg_str), NULL));
			_g_free0 (_tmp21_);
			_g_free0 (_tmp20_);
			_g_free0 (_tmp19_);
			_g_error_free0 (er);
		}
	}
	__finally1:
	if (_inner_error_ != NULL) {
		_g_object_unref0 (ploader);
		_g_object_unref0 (pb);
		_g_free0 (svg_str);
		_g_free0 (shours);
		_g_free0 (sminutes);
		_g_free0 (datetime);
		svg_data = (g_free (svg_data), 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;
	}
	_g_object_unref0 (ploader);
	_g_object_unref0 (pb);
	_g_free0 (svg_str);
	_g_free0 (shours);
	_g_free0 (sminutes);
	_g_free0 (datetime);
	svg_data = (g_free (svg_data), NULL);
}


static void main_controller_die (main_controller* self, const char* str) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (str != NULL);
	fprintf (stdout, "%s v%s: %s\n", self->priv->app_name, self->priv->app_version, str);
	gtk_main_quit ();
}


static void value_main_controller_init (GValue* value) {
	value->data[0].v_pointer = NULL;
}


static void value_main_controller_free_value (GValue* value) {
	if (value->data[0].v_pointer) {
		main_controller_unref (value->data[0].v_pointer);
	}
}


static void value_main_controller_copy_value (const GValue* src_value, GValue* dest_value) {
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = main_controller_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}


static gpointer value_main_controller_peek_pointer (const GValue* value) {
	return value->data[0].v_pointer;
}


static gchar* value_main_controller_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		main_controller* object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = main_controller_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* value_main_controller_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	main_controller** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags && G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = main_controller_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* param_spec_main_controller (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	ParamSpecmain_controller* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_MAIN_CONTROLLER), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}


gpointer value_get_main_controller (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_MAIN_CONTROLLER), NULL);
	return value->data[0].v_pointer;
}


void value_set_main_controller (GValue* value, gpointer v_object) {
	main_controller* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_MAIN_CONTROLLER));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_MAIN_CONTROLLER));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		main_controller_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		main_controller_unref (old);
	}
}


void value_take_main_controller (GValue* value, gpointer v_object) {
	main_controller* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_MAIN_CONTROLLER));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_MAIN_CONTROLLER));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		main_controller_unref (old);
	}
}


static void main_controller_class_init (main_controllerClass * klass) {
	main_controller_parent_class = g_type_class_peek_parent (klass);
	MAIN_CONTROLLER_CLASS (klass)->finalize = main_controller_finalize;
	g_type_class_add_private (klass, sizeof (main_controllerPrivate));
}


static void main_controller_instance_init (main_controller * self) {
	self->priv = MAIN_CONTROLLER_GET_PRIVATE (self);
	self->ref_count = 1;
}


static void main_controller_finalize (main_controller* obj) {
	main_controller * self;
	self = MAIN_CONTROLLER (obj);
	_g_object_unref0 (self->priv->si);
	_g_free0 (self->priv->app_name);
	_g_free0 (self->priv->app_version);
	_g_object_unref0 (self->priv->iconMenu);
	_configuration_unref0 (self->priv->config);
	_g_free0 (self->priv->color_background);
	_g_free0 (self->priv->color_ticks);
	_g_free0 (self->priv->color_border);
	_g_free0 (self->priv->color_hours);
	_g_free0 (self->priv->color_minutes);
	_g_free0 (self->priv->datetime_format);
}


GType main_controller_get_type (void) {
	static volatile gsize main_controller_type_id__volatile = 0;
	if (g_once_init_enter (&main_controller_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_main_controller_init, value_main_controller_free_value, value_main_controller_copy_value, value_main_controller_peek_pointer, "p", value_main_controller_collect_value, "p", value_main_controller_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (main_controllerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) main_controller_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (main_controller), 0, (GInstanceInitFunc) main_controller_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType main_controller_type_id;
		main_controller_type_id = g_type_register_fundamental (g_type_fundamental_next (), "main_controller", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&main_controller_type_id__volatile, main_controller_type_id);
	}
	return main_controller_type_id__volatile;
}


gpointer main_controller_ref (gpointer instance) {
	main_controller* self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}


void main_controller_unref (gpointer instance) {
	main_controller* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		MAIN_CONTROLLER_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}


void _vala_main (char** args, int args_length1) {
	char* app_name;
	char* app_version;
	main_controller* mc;
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
	app_name = g_strdup (PACKAGE_NAME);
	app_version = g_strdup (PACKAGE_VERSION);
	gtk_init (&args_length1, &args);
	mc = main_controller_new (app_name, app_version);
	main_controller_run (mc);
	_g_free0 (app_name);
	_g_free0 (app_version);
	_main_controller_unref0 (mc);
}


int main (int argc, char ** argv) {
	g_type_init ();
	_vala_main (argv, argc);
	return 0;
}




