/* rygel-media-export-plugin.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-media-export-plugin.vala, do not modify */

/*
 * Copyright (C) 2008-2009 Jens Georg <mail@jensge.org>.
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "rygel-media-export.h"
#include "rygel-core.h"
#include <gee.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include "rygel-server.h"

#define TRACKER_PLUGIN "Tracker"
#define TRACKER3_PLUGIN "Tracker3"

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block1Data Block1Data;
typedef struct _Block2Data Block2Data;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _Block3Data Block3Data;
enum  {
	RYGEL_MEDIA_EXPORT_PLUGIN_0_PROPERTY,
	RYGEL_MEDIA_EXPORT_PLUGIN_NUM_PROPERTIES
};
static GParamSpec* rygel_media_export_plugin_properties[RYGEL_MEDIA_EXPORT_PLUGIN_NUM_PROPERTIES];

struct _Block1Data {
	int _ref_count_;
	RygelPluginLoader* loader;
};

struct _Block2Data {
	int _ref_count_;
	Block1Data * _data1_;
	RygelMediaExportPlugin* plugin;
};

struct _Block3Data {
	int _ref_count_;
	RygelPlugin* plugin;
	RygelPlugin* our_plugin;
};

static gpointer rygel_media_export_plugin_parent_class = NULL;

static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static gboolean ___lambda10_ (Block2Data* _data2_);
static void __lambda12_ (Block2Data* _data2_,
                  RygelPlugin* new_plugin);
static void ___lambda12__rygel_plugin_loader_plugin_available (RygelPluginLoader* _sender,
                                                        RygelPlugin* plugin,
                                                        gpointer self);
static gboolean ____lambda10__gsource_func (gpointer self);
static Block3Data* block3_data_ref (Block3Data* _data3_);
static void block3_data_unref (void * _userdata_);
static void ____lambda11_ (Block3Data* _data3_);
VALA_EXTERN void shutdown_media_export (void);
static void _____lambda11__g_object_notify (GObject* _sender,
                                     GParamSpec* pspec,
                                     gpointer self);
static GType rygel_media_export_plugin_get_type_once (void);

/**
 * Simple plugin which exposes the media contents of a directory via UPnP.
 *
 */
static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

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_)) {
		_g_object_unref0 (_data1_->loader);
		g_slice_free (Block1Data, _data1_);
	}
}

static Block2Data*
block2_data_ref (Block2Data* _data2_)
{
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}

static void
block2_data_unref (void * _userdata_)
{
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		_g_object_unref0 (_data2_->plugin);
		block1_data_unref (_data2_->_data1_);
		_data2_->_data1_ = NULL;
		g_slice_free (Block2Data, _data2_);
	}
}

static void
__lambda12_ (Block2Data* _data2_,
             RygelPlugin* new_plugin)
{
	Block1Data* _data1_;
	_data1_ = _data2_->_data1_;
	g_return_if_fail (new_plugin != NULL);
	on_plugin_available (new_plugin, (RygelPlugin*) _data2_->plugin);
}

static void
___lambda12__rygel_plugin_loader_plugin_available (RygelPluginLoader* _sender,
                                                   RygelPlugin* plugin,
                                                   gpointer self)
{
	__lambda12_ (self, plugin);
}

static gboolean
___lambda10_ (Block2Data* _data2_)
{
	Block1Data* _data1_;
	gboolean result;
	_data1_ = _data2_->_data1_;
	{
		GeeIterator* _loaded_plugin_it = NULL;
		GeeCollection* _tmp0_;
		GeeCollection* _tmp1_;
		GeeIterator* _tmp2_;
		GeeIterator* _tmp3_;
		_tmp0_ = rygel_plugin_loader_list_plugins (_data1_->loader);
		_tmp1_ = _tmp0_;
		_tmp2_ = gee_iterable_iterator ((GeeIterable*) _tmp1_);
		_tmp3_ = _tmp2_;
		_g_object_unref0 (_tmp1_);
		_loaded_plugin_it = _tmp3_;
		while (TRUE) {
			GeeIterator* _tmp4_;
			RygelPlugin* loaded_plugin = NULL;
			GeeIterator* _tmp5_;
			gpointer _tmp6_;
			RygelPlugin* _tmp7_;
			_tmp4_ = _loaded_plugin_it;
			if (!gee_iterator_next (_tmp4_)) {
				break;
			}
			_tmp5_ = _loaded_plugin_it;
			_tmp6_ = gee_iterator_get (_tmp5_);
			loaded_plugin = (RygelPlugin*) _tmp6_;
			_tmp7_ = loaded_plugin;
			on_plugin_available (_tmp7_, (RygelPlugin*) _data2_->plugin);
			_g_object_unref0 (loaded_plugin);
		}
		_g_object_unref0 (_loaded_plugin_it);
	}
	g_signal_connect_data (_data1_->loader, "plugin-available", (GCallback) ___lambda12__rygel_plugin_loader_plugin_available, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	result = FALSE;
	return result;
}

static gboolean
____lambda10__gsource_func (gpointer self)
{
	gboolean result;
	result = ___lambda10_ (self);
	return result;
}

void
module_init (RygelPluginLoader* loader)
{
	Block1Data* _data1_;
	RygelPluginLoader* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (loader != NULL);
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (loader);
	_g_object_unref0 (_data1_->loader);
	_data1_->loader = _tmp0_;
	{
		Block2Data* _data2_;
		RygelMediaExportPlugin* _tmp1_;
		_data2_ = g_slice_new0 (Block2Data);
		_data2_->_ref_count_ = 1;
		_data2_->_data1_ = block1_data_ref (_data1_);
		_tmp1_ = rygel_media_export_plugin_new (&_inner_error0_);
		_data2_->plugin = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			block2_data_unref (_data2_);
			_data2_ = NULL;
			goto __catch0_g_error;
		}
		g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, ____lambda10__gsource_func, block2_data_ref (_data2_), block2_data_unref);
		rygel_plugin_loader_add_plugin (_data1_->loader, (RygelPlugin*) _data2_->plugin);
		block2_data_unref (_data2_);
		_data2_ = NULL;
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp2_;
		const gchar* _tmp3_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp2_ = _error_;
		_tmp3_ = _tmp2_->message;
		g_warning (_ ("Failed to load plugin %s: %s"), RYGEL_MEDIA_EXPORT_PLUGIN_NAME, _tmp3_);
		_g_error_free0 (_error_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		block1_data_unref (_data1_);
		_data1_ = NULL;
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	block1_data_unref (_data1_);
	_data1_ = NULL;
}

static Block3Data*
block3_data_ref (Block3Data* _data3_)
{
	g_atomic_int_inc (&_data3_->_ref_count_);
	return _data3_;
}

static void
block3_data_unref (void * _userdata_)
{
	Block3Data* _data3_;
	_data3_ = (Block3Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data3_->_ref_count_)) {
		_g_object_unref0 (_data3_->plugin);
		_g_object_unref0 (_data3_->our_plugin);
		g_slice_free (Block3Data, _data3_);
	}
}

static void
____lambda11_ (Block3Data* _data3_)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	_tmp0_ = rygel_plugin_get_active (_data3_->plugin);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		gboolean _tmp2_;
		gboolean _tmp3_;
		shutdown_media_export ();
		_tmp2_ = rygel_plugin_get_active (_data3_->plugin);
		_tmp3_ = _tmp2_;
		rygel_plugin_set_active (_data3_->our_plugin, !_tmp3_);
	}
}

static void
_____lambda11__g_object_notify (GObject* _sender,
                                GParamSpec* pspec,
                                gpointer self)
{
	____lambda11_ (self);
}

void
on_plugin_available (RygelPlugin* plugin,
                     RygelPlugin* our_plugin)
{
	Block3Data* _data3_;
	RygelPlugin* _tmp0_;
	RygelPlugin* _tmp1_;
	gboolean _tmp2_ = FALSE;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	g_return_if_fail (plugin != NULL);
	g_return_if_fail (our_plugin != NULL);
	_data3_ = g_slice_new0 (Block3Data);
	_data3_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (plugin);
	_g_object_unref0 (_data3_->plugin);
	_data3_->plugin = _tmp0_;
	_tmp1_ = _g_object_ref0 (our_plugin);
	_g_object_unref0 (_data3_->our_plugin);
	_data3_->our_plugin = _tmp1_;
	_tmp3_ = rygel_plugin_get_name (_data3_->plugin);
	_tmp4_ = _tmp3_;
	if (g_strcmp0 (_tmp4_, TRACKER_PLUGIN) == 0) {
		_tmp2_ = TRUE;
	} else {
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		_tmp5_ = rygel_plugin_get_name (_data3_->plugin);
		_tmp6_ = _tmp5_;
		_tmp2_ = g_strcmp0 (_tmp6_, TRACKER3_PLUGIN) == 0;
	}
	if (_tmp2_) {
		gboolean _tmp7_ = FALSE;
		gboolean _tmp8_;
		gboolean _tmp9_;
		_tmp8_ = rygel_plugin_get_active (_data3_->our_plugin);
		_tmp9_ = _tmp8_;
		if (_tmp9_) {
			gboolean _tmp10_;
			gboolean _tmp11_;
			_tmp10_ = rygel_plugin_get_active (_data3_->plugin);
			_tmp11_ = _tmp10_;
			_tmp7_ = !_tmp11_;
		} else {
			_tmp7_ = FALSE;
		}
		if (_tmp7_) {
			g_signal_connect_data ((GObject*) _data3_->plugin, "notify::active", (GCallback) _____lambda11__g_object_notify, block3_data_ref (_data3_), (GClosureNotify) block3_data_unref, 0);
		} else {
			gboolean _tmp12_;
			gboolean _tmp13_;
			gboolean _tmp14_;
			gboolean _tmp15_;
			_tmp12_ = rygel_plugin_get_active (_data3_->our_plugin);
			_tmp13_ = _tmp12_;
			_tmp14_ = rygel_plugin_get_active (_data3_->plugin);
			_tmp15_ = _tmp14_;
			if (_tmp13_ == _tmp15_) {
				gboolean _tmp16_;
				gboolean _tmp17_;
				gboolean _tmp20_;
				gboolean _tmp21_;
				_tmp16_ = rygel_plugin_get_active (_data3_->plugin);
				_tmp17_ = _tmp16_;
				if (_tmp17_) {
					shutdown_media_export ();
				} else {
					const gchar* _tmp18_;
					const gchar* _tmp19_;
					_tmp18_ = rygel_plugin_get_name (_data3_->plugin);
					_tmp19_ = _tmp18_;
					g_message ("rygel-media-export-plugin.vala:81: Plugin '%s' inactivate, activating " \
"'%s' plugin", _tmp19_, RYGEL_MEDIA_EXPORT_PLUGIN_NAME);
				}
				_tmp20_ = rygel_plugin_get_active (_data3_->plugin);
				_tmp21_ = _tmp20_;
				rygel_plugin_set_active (_data3_->our_plugin, !_tmp21_);
			}
		}
	}
	block3_data_unref (_data3_);
	_data3_ = NULL;
}

void
shutdown_media_export (void)
{
	GError* _inner_error0_ = NULL;
	g_message ("rygel-media-export-plugin.vala:91: Deactivating plugin '%s' in favor o" \
"f plugin '%s'", RYGEL_MEDIA_EXPORT_PLUGIN_NAME, TRACKER_PLUGIN);
	{
		RygelMetaConfig* config = NULL;
		RygelMetaConfig* _tmp0_;
		gboolean enabled = FALSE;
		_tmp0_ = rygel_meta_config_get_default ();
		config = _tmp0_;
		enabled = rygel_configuration_get_bool ((RygelConfiguration*) config, "MediaExport", "enabled", &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (config);
			goto __catch0_g_error;
		}
		if (enabled) {
			RygelMediaExportRootContainer* root = NULL;
			RygelMediaExportRootContainer* _tmp1_;
			RygelMediaExportRootContainer* _tmp2_;
			_tmp1_ = rygel_media_export_root_container_get_instance ();
			root = _tmp1_;
			_tmp2_ = root;
			rygel_media_export_root_container_shutdown (_tmp2_);
			_g_object_unref0 (root);
		}
		_g_object_unref0 (config);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

/**
     * Instantiate the plugin.
     */
RygelMediaExportPlugin*
rygel_media_export_plugin_construct (GType object_type,
                                     GError** error)
{
	RygelMediaExportPlugin * self = NULL;
	RygelMediaExportRootContainer* _tmp0_;
	RygelMediaExportRootContainer* _tmp1_;
	GError* _inner_error0_ = NULL;
	rygel_media_export_root_container_ensure_exists (&_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp0_ = rygel_media_export_root_container_get_instance ();
	_tmp1_ = _tmp0_;
	self = (RygelMediaExportPlugin*) rygel_media_server_plugin_construct (object_type, (RygelMediaContainer*) _tmp1_, RYGEL_MEDIA_EXPORT_PLUGIN_NAME, NULL, RYGEL_PLUGIN_CAPABILITIES_UPLOAD | RYGEL_PLUGIN_CAPABILITIES_TRACK_CHANGES);
	_g_object_unref0 (_tmp1_);
	return self;
}

RygelMediaExportPlugin*
rygel_media_export_plugin_new (GError** error)
{
	return rygel_media_export_plugin_construct (RYGEL_MEDIA_EXPORT_TYPE_PLUGIN, error);
}

static void
rygel_media_export_plugin_class_init (RygelMediaExportPluginClass * klass,
                                      gpointer klass_data)
{
	rygel_media_export_plugin_parent_class = g_type_class_peek_parent (klass);
}

static void
rygel_media_export_plugin_instance_init (RygelMediaExportPlugin * self,
                                         gpointer klass)
{
}

static GType
rygel_media_export_plugin_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelMediaExportPluginClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_media_export_plugin_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelMediaExportPlugin), 0, (GInstanceInitFunc) rygel_media_export_plugin_instance_init, NULL };
	GType rygel_media_export_plugin_type_id;
	rygel_media_export_plugin_type_id = g_type_register_static (RYGEL_TYPE_MEDIA_SERVER_PLUGIN, "RygelMediaExportPlugin", &g_define_type_info, 0);
	return rygel_media_export_plugin_type_id;
}

GType
rygel_media_export_plugin_get_type (void)
{
	static volatile gsize rygel_media_export_plugin_type_id__once = 0;
	if (g_once_init_enter (&rygel_media_export_plugin_type_id__once)) {
		GType rygel_media_export_plugin_type_id;
		rygel_media_export_plugin_type_id = rygel_media_export_plugin_get_type_once ();
		g_once_init_leave (&rygel_media_export_plugin_type_id__once, rygel_media_export_plugin_type_id);
	}
	return rygel_media_export_plugin_type_id__once;
}

