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

/**/
/*  Copyright (C) 2014 Tom Beckmann*/
/**/
/*  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 <glib-object.h>
#include <meta/meta-background-actor.h>
#include <clutter/clutter.h>
#include <glib.h>
#include <meta/meta-background.h>
#include <meta/display.h>
#include <meta/meta-background-image.h>
#include <gio/gio.h>
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <gsettings-desktop-schemas/gdesktop-enums.h>
#include <meta/meta-background-content.h>

#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define GALA_TYPE_SYSTEM_BACKGROUND (gala_system_background_get_type ())
#define GALA_SYSTEM_BACKGROUND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackground))
#define GALA_SYSTEM_BACKGROUND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackgroundClass))
#define GALA_IS_SYSTEM_BACKGROUND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GALA_TYPE_SYSTEM_BACKGROUND))
#define GALA_IS_SYSTEM_BACKGROUND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GALA_TYPE_SYSTEM_BACKGROUND))
#define GALA_SYSTEM_BACKGROUND_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackgroundClass))

typedef struct _GalaSystemBackground GalaSystemBackground;
typedef struct _GalaSystemBackgroundClass GalaSystemBackgroundClass;
typedef struct _GalaSystemBackgroundPrivate GalaSystemBackgroundPrivate;
enum  {
	GALA_SYSTEM_BACKGROUND_0_PROPERTY,
	GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY,
	GALA_SYSTEM_BACKGROUND_NUM_PROPERTIES
};
static GParamSpec* gala_system_background_properties[GALA_SYSTEM_BACKGROUND_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block52Data Block52Data;
typedef struct _Block53Data Block53Data;
#define _g_free0(var) (var = (g_free (var), NULL))
enum  {
	GALA_SYSTEM_BACKGROUND_LOADED_SIGNAL,
	GALA_SYSTEM_BACKGROUND_NUM_SIGNALS
};
static guint gala_system_background_signals[GALA_SYSTEM_BACKGROUND_NUM_SIGNALS] = {0};

struct _GalaSystemBackground {
	GObject parent_instance;
	GalaSystemBackgroundPrivate * priv;
};

struct _GalaSystemBackgroundClass {
	GObjectClass parent_class;
};

struct _GalaSystemBackgroundPrivate {
	MetaBackgroundActor* _background_actor;
};

struct _Block52Data {
	int _ref_count_;
	GalaSystemBackground* self;
	MetaBackgroundImage* image;
};

struct _Block53Data {
	int _ref_count_;
	Block52Data * _data52_;
	gulong handler;
};

static gint GalaSystemBackground_private_offset;
static gpointer gala_system_background_parent_class = NULL;
static MetaBackground* gala_system_background_system_background;
static MetaBackground* gala_system_background_system_background = NULL;

VALA_EXTERN GType gala_system_background_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GalaSystemBackground, g_object_unref)
VALA_EXTERN GalaSystemBackground* gala_system_background_new (MetaDisplay* display);
VALA_EXTERN GalaSystemBackground* gala_system_background_construct (GType object_type,
                                                        MetaDisplay* display);
VALA_EXTERN void gala_system_background_refresh (void);
VALA_EXTERN MetaBackgroundActor* gala_system_background_get_background_actor (GalaSystemBackground* self);
static void gala_system_background_set_background_actor (GalaSystemBackground* self,
                                                  MetaBackgroundActor* value);
static GObject * gala_system_background_constructor (GType type,
                                              guint n_construct_properties,
                                              GObjectConstructParam * construct_properties);
static Block52Data* block52_data_ref (Block52Data* _data52_);
static void block52_data_unref (void * _userdata_);
static gboolean _gala_system_background____lambda15_ (GalaSystemBackground* self);
static gboolean __gala_system_background____lambda15__gsource_func (gpointer self);
static Block53Data* block53_data_ref (Block53Data* _data53_);
static void block53_data_unref (void * _userdata_);
static void _gala_system_background____lambda16_ (Block53Data* _data53_);
static void __gala_system_background____lambda16__meta_background_image_loaded (MetaBackgroundImage* _sender,
                                                                         gpointer self);
static void gala_system_background_finalize (GObject * obj);
static GType gala_system_background_get_type_once (void);
static void _vala_gala_system_background_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec);
static void _vala_gala_system_background_set_property (GObject * object,
                                                guint property_id,
                                                const GValue * value,
                                                GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static const ClutterColor GALA_SYSTEM_BACKGROUND_DEFAULT_BACKGROUND_COLOR = {(guint8) 0x2e, (guint8) 0x34, (guint8) 0x36, (guint8) 0xff};

static inline gpointer
gala_system_background_get_instance_private (GalaSystemBackground* self)
{
	return G_STRUCT_MEMBER_P (self, GalaSystemBackground_private_offset);
}

GalaSystemBackground*
gala_system_background_construct (GType object_type,
                                  MetaDisplay* display)
{
	GalaSystemBackground * self = NULL;
	MetaBackgroundActor* _tmp0_;
	MetaBackgroundActor* _tmp1_;
	g_return_val_if_fail (display != NULL, NULL);
	_tmp0_ = (MetaBackgroundActor*) meta_background_actor_new (display, 0);
	g_object_ref_sink (_tmp0_);
	_tmp1_ = _tmp0_;
	self = (GalaSystemBackground*) g_object_new (object_type, "background-actor", _tmp1_, NULL);
	_g_object_unref0 (_tmp1_);
	return self;
}

GalaSystemBackground*
gala_system_background_new (MetaDisplay* display)
{
	return gala_system_background_construct (GALA_TYPE_SYSTEM_BACKGROUND, display);
}

void
gala_system_background_refresh (void)
{
	MetaBackground* _tmp0_;
	_tmp0_ = gala_system_background_system_background;
	if (_tmp0_ != NULL) {
		MetaBackground* _tmp1_;
		_tmp1_ = gala_system_background_system_background;
		meta_background_set_color (_tmp1_, &GALA_SYSTEM_BACKGROUND_DEFAULT_BACKGROUND_COLOR);
	}
}

MetaBackgroundActor*
gala_system_background_get_background_actor (GalaSystemBackground* self)
{
	MetaBackgroundActor* result;
	MetaBackgroundActor* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_background_actor;
	result = _tmp0_;
	return result;
}

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

static void
gala_system_background_set_background_actor (GalaSystemBackground* self,
                                             MetaBackgroundActor* value)
{
	MetaBackgroundActor* old_value;
	g_return_if_fail (self != NULL);
	old_value = gala_system_background_get_background_actor (self);
	if (old_value != value) {
		MetaBackgroundActor* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_background_actor);
		self->priv->_background_actor = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, gala_system_background_properties[GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY]);
	}
}

static Block52Data*
block52_data_ref (Block52Data* _data52_)
{
	g_atomic_int_inc (&_data52_->_ref_count_);
	return _data52_;
}

static void
block52_data_unref (void * _userdata_)
{
	Block52Data* _data52_;
	_data52_ = (Block52Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data52_->_ref_count_)) {
		GalaSystemBackground* self;
		self = _data52_->self;
		_g_object_unref0 (_data52_->image);
		_g_object_unref0 (self);
		g_slice_free (Block52Data, _data52_);
	}
}

static gboolean
_gala_system_background____lambda15_ (GalaSystemBackground* self)
{
	gboolean result;
	g_signal_emit (self, gala_system_background_signals[GALA_SYSTEM_BACKGROUND_LOADED_SIGNAL], 0);
	result = FALSE;
	return result;
}

static gboolean
__gala_system_background____lambda15__gsource_func (gpointer self)
{
	gboolean result;
	result = _gala_system_background____lambda15_ ((GalaSystemBackground*) self);
	return result;
}

static Block53Data*
block53_data_ref (Block53Data* _data53_)
{
	g_atomic_int_inc (&_data53_->_ref_count_);
	return _data53_;
}

static void
block53_data_unref (void * _userdata_)
{
	Block53Data* _data53_;
	_data53_ = (Block53Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data53_->_ref_count_)) {
		GalaSystemBackground* self;
		self = _data53_->_data52_->self;
		block52_data_unref (_data53_->_data52_);
		_data53_->_data52_ = NULL;
		g_slice_free (Block53Data, _data53_);
	}
}

static void
_gala_system_background____lambda16_ (Block53Data* _data53_)
{
	Block52Data* _data52_;
	GalaSystemBackground* self;
	MetaBackgroundImage* _tmp0_;
	_data52_ = _data53_->_data52_;
	self = _data52_->self;
	g_signal_emit (self, gala_system_background_signals[GALA_SYSTEM_BACKGROUND_LOADED_SIGNAL], 0);
	_tmp0_ = _data52_->image;
	g_signal_handler_disconnect (_tmp0_, _data53_->handler);
	_g_object_unref0 (_data52_->image);
	_data52_->image = NULL;
}

static void
__gala_system_background____lambda16__meta_background_image_loaded (MetaBackgroundImage* _sender,
                                                                    gpointer self)
{
	_gala_system_background____lambda16_ (self);
}

static GObject *
gala_system_background_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GalaSystemBackground * self;
	Block52Data* _data52_;
	GFile* background_file = NULL;
	GFile* _tmp0_;
	GSettings* appearance_settings = NULL;
	GSettings* _tmp1_;
	gchar* custom_path = NULL;
	GSettings* _tmp2_;
	gchar* _tmp3_;
	gboolean _tmp4_ = FALSE;
	const gchar* _tmp5_;
	MetaBackground* _tmp9_;
	MetaBackgroundActor* _tmp18_;
	ClutterContent* _tmp19_;
	ClutterContent* _tmp20_;
	MetaBackground* _tmp21_;
	MetaBackgroundImageCache* cache = NULL;
	MetaBackgroundImageCache* _tmp22_;
	MetaBackgroundImageCache* _tmp23_;
	MetaBackgroundImageCache* _tmp24_;
	GFile* _tmp25_;
	MetaBackgroundImage* _tmp26_;
	MetaBackgroundImage* _tmp27_;
	parent_class = G_OBJECT_CLASS (gala_system_background_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackground);
	_data52_ = g_slice_new0 (Block52Data);
	_data52_->_ref_count_ = 1;
	_data52_->self = g_object_ref (self);
	_tmp0_ = g_file_new_for_uri ("resource:///io/elementary/desktop/gala/texture.png");
	background_file = _tmp0_;
	_tmp1_ = g_settings_new (SCHEMA ".appearance");
	appearance_settings = _tmp1_;
	_tmp2_ = appearance_settings;
	_tmp3_ = g_settings_get_string (_tmp2_, "workspace-switcher-background");
	custom_path = _tmp3_;
	_tmp5_ = custom_path;
	if (g_strcmp0 (_tmp5_, "") != 0) {
		const gchar* _tmp6_;
		_tmp6_ = custom_path;
		_tmp4_ = g_file_test (_tmp6_, G_FILE_TEST_IS_REGULAR);
	} else {
		_tmp4_ = FALSE;
	}
	if (_tmp4_) {
		const gchar* _tmp7_;
		GFile* _tmp8_;
		_tmp7_ = custom_path;
		_tmp8_ = g_file_new_for_path (_tmp7_);
		_g_object_unref0 (background_file);
		background_file = _tmp8_;
	}
	_tmp9_ = gala_system_background_system_background;
	if (_tmp9_ == NULL) {
		MetaBackgroundActor* _tmp10_;
		MetaDisplay* _tmp11_;
		MetaDisplay* _tmp12_;
		MetaDisplay* _tmp13_;
		MetaBackground* _tmp14_;
		MetaBackground* _tmp15_;
		MetaBackground* _tmp16_;
		GFile* _tmp17_;
		_tmp10_ = self->priv->_background_actor;
		g_object_get (_tmp10_, "meta-display", &_tmp11_, NULL);
		_tmp12_ = _tmp11_;
		_tmp13_ = _tmp12_;
		_tmp14_ = meta_background_new (_tmp13_);
		_g_object_unref0 (gala_system_background_system_background);
		gala_system_background_system_background = _tmp14_;
		_g_object_unref0 (_tmp13_);
		_tmp15_ = gala_system_background_system_background;
		meta_background_set_color (_tmp15_, &GALA_SYSTEM_BACKGROUND_DEFAULT_BACKGROUND_COLOR);
		_tmp16_ = gala_system_background_system_background;
		_tmp17_ = background_file;
		meta_background_set_file (_tmp16_, _tmp17_, G_DESKTOP_BACKGROUND_STYLE_WALLPAPER);
	}
	_tmp18_ = self->priv->_background_actor;
	_tmp19_ = clutter_actor_get_content ((ClutterActor*) _tmp18_);
	_tmp20_ = _tmp19_;
	_tmp21_ = gala_system_background_system_background;
	g_object_set (G_TYPE_CHECK_INSTANCE_CAST (_tmp20_, meta_background_content_get_type (), MetaBackgroundContent), "background", _tmp21_, NULL);
	_tmp22_ = meta_background_image_cache_get_default ();
	_tmp23_ = _g_object_ref0 (_tmp22_);
	cache = _tmp23_;
	_tmp24_ = cache;
	_tmp25_ = background_file;
	_tmp26_ = meta_background_image_cache_load (_tmp24_, _tmp25_);
	_data52_->image = _tmp26_;
	_tmp27_ = _data52_->image;
	if (meta_background_image_is_loaded (_tmp27_)) {
		_g_object_unref0 (_data52_->image);
		_data52_->image = NULL;
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, __gala_system_background____lambda15__gsource_func, g_object_ref (self), g_object_unref);
	} else {
		Block53Data* _data53_;
		MetaBackgroundImage* _tmp28_;
		gulong _tmp29_;
		_data53_ = g_slice_new0 (Block53Data);
		_data53_->_ref_count_ = 1;
		_data53_->_data52_ = block52_data_ref (_data52_);
		_data53_->handler = (gulong) 0;
		_tmp28_ = _data52_->image;
		_tmp29_ = g_signal_connect_data (_tmp28_, "loaded", (GCallback) __gala_system_background____lambda16__meta_background_image_loaded, block53_data_ref (_data53_), (GClosureNotify) block53_data_unref, 0);
		_data53_->handler = _tmp29_;
		block53_data_unref (_data53_);
		_data53_ = NULL;
	}
	_g_object_unref0 (cache);
	_g_free0 (custom_path);
	_g_object_unref0 (appearance_settings);
	_g_object_unref0 (background_file);
	block52_data_unref (_data52_);
	_data52_ = NULL;
	return obj;
}

static void
gala_system_background_class_init (GalaSystemBackgroundClass * klass,
                                   gpointer klass_data)
{
	gala_system_background_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GalaSystemBackground_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_gala_system_background_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_gala_system_background_set_property;
	G_OBJECT_CLASS (klass)->constructor = gala_system_background_constructor;
	G_OBJECT_CLASS (klass)->finalize = gala_system_background_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY, gala_system_background_properties[GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY] = g_param_spec_object ("background-actor", "background-actor", "background-actor", meta_background_actor_get_type (), G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	gala_system_background_signals[GALA_SYSTEM_BACKGROUND_LOADED_SIGNAL] = g_signal_new ("loaded", GALA_TYPE_SYSTEM_BACKGROUND, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
gala_system_background_instance_init (GalaSystemBackground * self,
                                      gpointer klass)
{
	self->priv = gala_system_background_get_instance_private (self);
}

static void
gala_system_background_finalize (GObject * obj)
{
	GalaSystemBackground * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackground);
	_g_object_unref0 (self->priv->_background_actor);
	G_OBJECT_CLASS (gala_system_background_parent_class)->finalize (obj);
}

static GType
gala_system_background_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GalaSystemBackgroundClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gala_system_background_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GalaSystemBackground), 0, (GInstanceInitFunc) gala_system_background_instance_init, NULL };
	GType gala_system_background_type_id;
	gala_system_background_type_id = g_type_register_static (G_TYPE_OBJECT, "GalaSystemBackground", &g_define_type_info, 0);
	GalaSystemBackground_private_offset = g_type_add_instance_private (gala_system_background_type_id, sizeof (GalaSystemBackgroundPrivate));
	return gala_system_background_type_id;
}

GType
gala_system_background_get_type (void)
{
	static volatile gsize gala_system_background_type_id__once = 0;
	if (g_once_init_enter (&gala_system_background_type_id__once)) {
		GType gala_system_background_type_id;
		gala_system_background_type_id = gala_system_background_get_type_once ();
		g_once_init_leave (&gala_system_background_type_id__once, gala_system_background_type_id);
	}
	return gala_system_background_type_id__once;
}

static void
_vala_gala_system_background_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec)
{
	GalaSystemBackground * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackground);
	switch (property_id) {
		case GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY:
		g_value_set_object (value, gala_system_background_get_background_actor (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_gala_system_background_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec)
{
	GalaSystemBackground * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_SYSTEM_BACKGROUND, GalaSystemBackground);
	switch (property_id) {
		case GALA_SYSTEM_BACKGROUND_BACKGROUND_ACTOR_PROPERTY:
		gala_system_background_set_background_actor (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

