/* rygel-ruih-service.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-ruih-service.vala, do not modify */

/*
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: Neha Shanbhag <N.Shanbhag@cablelabs.com>
 * Contact: http://www.cablelabs.com/
 *
 * 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 <libgupnp/gupnp.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "rygel-ruih.h"
#include <glib/gi18n-lib.h>
#include "rygel-core.h"

#define RYGEL_RUIH_SERVICE_UPNP_ID "urn:upnp-org:serviceId:RemoteUIServer"
#define RYGEL_RUIH_SERVICE_UPNP_TYPE "urn:schemas-upnp-org:service:RemoteUIServer:1"
#define RYGEL_RUIH_SERVICE_DESCRIPTION_PATH "xml/RemoteUIServerService.xml"

#define RYGEL_TYPE_RUIH_SERVICE (rygel_ruih_service_get_type ())
#define RYGEL_RUIH_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_RUIH_SERVICE, RygelRuihService))
#define RYGEL_RUIH_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_RUIH_SERVICE, RygelRuihServiceClass))
#define RYGEL_IS_RUIH_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_RUIH_SERVICE))
#define RYGEL_IS_RUIH_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_RUIH_SERVICE))
#define RYGEL_RUIH_SERVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_RUIH_SERVICE, RygelRuihServiceClass))

typedef struct _RygelRuihService RygelRuihService;
typedef struct _RygelRuihServiceClass RygelRuihServiceClass;
typedef struct _RygelRuihServicePrivate RygelRuihServicePrivate;
enum  {
	RYGEL_RUIH_SERVICE_0_PROPERTY,
	RYGEL_RUIH_SERVICE_NUM_PROPERTIES
};
static GParamSpec* rygel_ruih_service_properties[RYGEL_RUIH_SERVICE_NUM_PROPERTIES];
#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)))

struct _RygelRuihService {
	GUPnPService parent_instance;
	RygelRuihServicePrivate * priv;
};

struct _RygelRuihServiceClass {
	GUPnPServiceClass parent_class;
};

static gpointer rygel_ruih_service_parent_class = NULL;

VALA_EXTERN GType rygel_ruih_service_get_type (void) G_GNUC_CONST ;
static void rygel_ruih_service_real_constructed (GObject* base);
static void rygel_ruih_service_query_ui_listing (RygelRuihService* self,
                                          GUPnPService* ruih_service,
                                          const gchar* variable,
                                          GValue* value);
static void _rygel_ruih_service_query_ui_listing_gupnp_service_query_variable (GUPnPService* _sender,
                                                                        const gchar* variable,
                                                                        GValue* value,
                                                                        gpointer self);
static void rygel_ruih_service_get_compatible_uis_cb (RygelRuihService* self,
                                               GUPnPService* content_dir,
                                               GUPnPServiceAction* action);
static void _rygel_ruih_service_get_compatible_uis_cb_gupnp_service_action_invoked (GUPnPService* _sender,
                                                                             GUPnPServiceAction* action,
                                                                             gpointer self);
static void rygel_ruih_service_update_plugin_availability (RygelRuihService* self);
static void _rygel_ruih_service_update_plugin_availability_rygel_ruih_service_manager_updated (RygelRuihServiceManager* _sender,
                                                                                        gpointer self);
VALA_EXTERN RygelRuihService* rygel_ruih_service_new (void);
VALA_EXTERN RygelRuihService* rygel_ruih_service_construct (GType object_type);
static GType rygel_ruih_service_get_type_once (void);

static void
_rygel_ruih_service_query_ui_listing_gupnp_service_query_variable (GUPnPService* _sender,
                                                                   const gchar* variable,
                                                                   GValue* value,
                                                                   gpointer self)
{
	rygel_ruih_service_query_ui_listing ((RygelRuihService*) self, _sender, variable, value);
}

static void
_rygel_ruih_service_get_compatible_uis_cb_gupnp_service_action_invoked (GUPnPService* _sender,
                                                                        GUPnPServiceAction* action,
                                                                        gpointer self)
{
	rygel_ruih_service_get_compatible_uis_cb ((RygelRuihService*) self, _sender, action);
}

static void
_rygel_ruih_service_update_plugin_availability_rygel_ruih_service_manager_updated (RygelRuihServiceManager* _sender,
                                                                                   gpointer self)
{
	rygel_ruih_service_update_plugin_availability ((RygelRuihService*) self);
}

static void
rygel_ruih_service_real_constructed (GObject* base)
{
	RygelRuihService * self;
	RygelRuihServiceManager* manager = NULL;
	RygelRuihServiceManager* _tmp0_;
	self = (RygelRuihService*) base;
	G_OBJECT_CLASS (rygel_ruih_service_parent_class)->constructed ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, gupnp_service_get_type (), GUPnPService));
	g_signal_connect_object ((GUPnPService*) self, "query-variable::UIListingUpdate", (GCallback) _rygel_ruih_service_query_ui_listing_gupnp_service_query_variable, self, 0);
	g_signal_connect_object ((GUPnPService*) self, "action-invoked::GetCompatibleUIs", (GCallback) _rygel_ruih_service_get_compatible_uis_cb_gupnp_service_action_invoked, self, 0);
	_tmp0_ = rygel_ruih_service_manager_get_default ();
	manager = _tmp0_;
	g_signal_connect_object (manager, "updated", (GCallback) _rygel_ruih_service_update_plugin_availability_rygel_ruih_service_manager_updated, self, 0);
	rygel_ruih_service_update_plugin_availability (self);
	_g_object_unref0 (manager);
}

static void
rygel_ruih_service_get_compatible_uis_cb (RygelRuihService* self,
                                          GUPnPService* content_dir,
                                          GUPnPServiceAction* action)
{
	gchar* input_device_profile = NULL;
	gchar* input_ui_filter = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (content_dir != NULL);
	g_return_if_fail (action != NULL);
	gupnp_service_action_get (action, "InputDeviceProfile", G_TYPE_STRING, &_tmp0_, NULL);
	_g_free0 (input_device_profile);
	input_device_profile = _tmp0_;
	gupnp_service_action_get (action, "UIFilter", G_TYPE_STRING, &_tmp1_, NULL);
	_g_free0 (input_ui_filter);
	input_ui_filter = _tmp1_;
	if (gupnp_service_action_get_argument_count (action) < ((guint) 2)) {
		gupnp_service_action_return_error (action, (guint) 402, _ ("Invalid argument"));
		_g_free0 (input_ui_filter);
		_g_free0 (input_device_profile);
		return;
	}
	{
		RygelRuihServiceManager* manager = NULL;
		RygelRuihServiceManager* _tmp2_;
		gchar* compat_ui = NULL;
		RygelRuihServiceManager* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		const gchar* _tmp7_;
		_tmp2_ = rygel_ruih_service_manager_get_default ();
		manager = _tmp2_;
		_tmp3_ = manager;
		_tmp4_ = input_device_profile;
		_tmp5_ = input_ui_filter;
		_tmp6_ = rygel_ruih_service_manager_get_compatible_uis (_tmp3_, _tmp4_, _tmp5_, &_inner_error0_);
		compat_ui = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (manager);
			if (_inner_error0_->domain == RYGEL_RUIH_SERVICE_ERROR) {
				goto __catch0_rygel_ruih_service_error;
			}
			_g_free0 (input_ui_filter);
			_g_free0 (input_device_profile);
			g_critical ("file %s: line %d: unexpected 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;
		}
		_tmp7_ = compat_ui;
		gupnp_service_action_set (action, "UIListing", G_TYPE_STRING, _tmp7_, NULL);
		gupnp_service_action_return (action);
		_g_free0 (compat_ui);
		_g_object_unref0 (manager);
	}
	goto __finally0;
	__catch0_rygel_ruih_service_error:
	{
		GError* e = NULL;
		GError* _tmp8_;
		GError* _tmp9_;
		const gchar* _tmp10_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp8_ = e;
		_tmp9_ = e;
		_tmp10_ = _tmp9_->message;
		gupnp_service_action_return_error (action, (guint) _tmp8_->code, _tmp10_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (input_ui_filter);
		_g_free0 (input_device_profile);
		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;
	}
	_g_free0 (input_ui_filter);
	_g_free0 (input_device_profile);
}

static void
rygel_ruih_service_query_ui_listing (RygelRuihService* self,
                                     GUPnPService* ruih_service,
                                     const gchar* variable,
                                     GValue* value)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (ruih_service != NULL);
	g_return_if_fail (variable != NULL);
	g_return_if_fail (value != NULL);
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value, "");
}

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

static void
rygel_ruih_service_update_plugin_availability (RygelRuihService* self)
{
	RygelRuihServiceManager* manager = NULL;
	RygelRuihServiceManager* _tmp0_;
	RygelPlugin* plugin = NULL;
	GUPnPRootDevice* _tmp1_;
	GUPnPRootDevice* _tmp2_;
	GUPnPRootDevice* _tmp3_;
	GUPnPResourceFactory* _tmp4_;
	GUPnPResourceFactory* _tmp5_;
	RygelPlugin* _tmp6_;
	RygelPlugin* _tmp7_;
	g_return_if_fail (self != NULL);
	_tmp0_ = rygel_ruih_service_manager_get_default ();
	manager = _tmp0_;
	g_object_get ((GUPnPService*) self, "root-device", &_tmp1_, NULL);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_;
	_tmp4_ = gupnp_device_info_get_resource_factory ((GUPnPDeviceInfo*) _tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (_tmp5_, RYGEL_TYPE_PLUGIN) ? ((RygelPlugin*) _tmp5_) : NULL);
	_tmp7_ = _tmp6_;
	_g_object_unref0 (_tmp3_);
	plugin = _tmp7_;
	rygel_plugin_set_active (plugin, rygel_ruih_service_manager_ui_list_available (manager));
	_g_object_unref0 (plugin);
	_g_object_unref0 (manager);
}

RygelRuihService*
rygel_ruih_service_construct (GType object_type)
{
	RygelRuihService * self = NULL;
	self = (RygelRuihService*) g_object_new (object_type, NULL);
	return self;
}

RygelRuihService*
rygel_ruih_service_new (void)
{
	return rygel_ruih_service_construct (RYGEL_TYPE_RUIH_SERVICE);
}

static void
rygel_ruih_service_class_init (RygelRuihServiceClass * klass,
                               gpointer klass_data)
{
	rygel_ruih_service_parent_class = g_type_class_peek_parent (klass);
	((GObjectClass *) klass)->constructed = (void (*) (GObject*)) rygel_ruih_service_real_constructed;
}

static void
rygel_ruih_service_instance_init (RygelRuihService * self,
                                  gpointer klass)
{
}

/**
 * Basic implementation of UPnP RemoteUIServer service version 1.
 */
static GType
rygel_ruih_service_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelRuihServiceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_ruih_service_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelRuihService), 0, (GInstanceInitFunc) rygel_ruih_service_instance_init, NULL };
	GType rygel_ruih_service_type_id;
	rygel_ruih_service_type_id = g_type_register_static (gupnp_service_get_type (), "RygelRuihService", &g_define_type_info, 0);
	return rygel_ruih_service_type_id;
}

GType
rygel_ruih_service_get_type (void)
{
	static volatile gsize rygel_ruih_service_type_id__once = 0;
	if (g_once_init_enter (&rygel_ruih_service_type_id__once)) {
		GType rygel_ruih_service_type_id;
		rygel_ruih_service_type_id = rygel_ruih_service_get_type_once ();
		g_once_init_leave (&rygel_ruih_service_type_id__once, rygel_ruih_service_type_id);
	}
	return rygel_ruih_service_type_id__once;
}

