/* rygel-ruih-service.c generated by valac 0.27.2.2-e6bbc, 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 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 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 <libgupnp/gupnp.h>
#include <stdlib.h>
#include <string.h>
#include <rygel-core.h>


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

#define RYGEL_TYPE_RUIH_SERVICE_MANAGER (rygel_ruih_service_manager_get_type ())
#define RYGEL_RUIH_SERVICE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_RUIH_SERVICE_MANAGER, RygelRuihServiceManager))
#define RYGEL_RUIH_SERVICE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_RUIH_SERVICE_MANAGER, RygelRuihServiceManagerClass))
#define RYGEL_IS_RUIH_SERVICE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_RUIH_SERVICE_MANAGER))
#define RYGEL_IS_RUIH_SERVICE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_RUIH_SERVICE_MANAGER))
#define RYGEL_RUIH_SERVICE_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_RUIH_SERVICE_MANAGER, RygelRuihServiceManagerClass))

typedef struct _RygelRuihServiceManager RygelRuihServiceManager;
typedef struct _RygelRuihServiceManagerClass RygelRuihServiceManagerClass;
#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;
};

typedef enum  {
	RYGEL_RUIH_SERVICE_ERROR_OPERATION_REJECTED = 701,
	RYGEL_RUIH_SERVICE_ERROR_INVALID_FILTER = 702
} RygelRuihServiceError;
#define RYGEL_RUIH_SERVICE_ERROR rygel_ruih_service_error_quark ()

static gpointer rygel_ruih_service_parent_class = NULL;

GType rygel_ruih_service_get_type (void) G_GNUC_CONST;
enum  {
	RYGEL_RUIH_SERVICE_DUMMY_PROPERTY
};
#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"
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);
GType rygel_ruih_service_manager_get_type (void) G_GNUC_CONST;
RygelRuihServiceManager* rygel_ruih_service_manager_get_default (void);
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);
GQuark rygel_ruih_service_error_quark (void);
gchar* rygel_ruih_service_manager_get_compatible_uis (RygelRuihServiceManager* self, const gchar* device_profile, const gchar* filter, GError** error);
gboolean rygel_ruih_service_manager_ui_list_available (RygelRuihServiceManager* self);
RygelRuihService* rygel_ruih_service_new (void);
RygelRuihService* rygel_ruih_service_construct (GType object_type);


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_ = NULL;
	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;
	GUPnPServiceAction* _tmp0_ = NULL;
	GUPnPServiceAction* _tmp1_ = NULL;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (content_dir != NULL);
	g_return_if_fail (action != NULL);
	_tmp0_ = action;
	gupnp_service_action_get (_tmp0_, "InputDeviceProfile", G_TYPE_STRING, &input_device_profile, NULL);
	_tmp1_ = action;
	gupnp_service_action_get (_tmp1_, "UIFilter", G_TYPE_STRING, &input_ui_filter, NULL);
	{
		RygelRuihServiceManager* manager = NULL;
		RygelRuihServiceManager* _tmp2_ = NULL;
		gchar* compat_ui = NULL;
		gchar* _tmp3_ = NULL;
		GUPnPServiceAction* _tmp4_ = NULL;
		GUPnPServiceAction* _tmp5_ = NULL;
		_tmp2_ = rygel_ruih_service_manager_get_default ();
		manager = _tmp2_;
		_tmp3_ = rygel_ruih_service_manager_get_compatible_uis (manager, input_device_profile, input_ui_filter, &_inner_error_);
		compat_ui = _tmp3_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_object_unref0 (manager);
			if (_inner_error_->domain == RYGEL_RUIH_SERVICE_ERROR) {
				goto __catch1_rygel_ruih_service_error;
			}
			_g_object_unref0 (manager);
			_g_free0 (input_ui_filter);
			_g_free0 (input_device_profile);
			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;
		}
		_tmp4_ = action;
		gupnp_service_action_set (_tmp4_, "UIListing", G_TYPE_STRING, compat_ui, NULL);
		_tmp5_ = action;
		gupnp_service_action_return (_tmp5_);
		_g_free0 (compat_ui);
		_g_object_unref0 (manager);
	}
	goto __finally1;
	__catch1_rygel_ruih_service_error:
	{
		GError* e = NULL;
		GUPnPServiceAction* _tmp6_ = NULL;
		GError* _tmp7_ = NULL;
		gint _tmp8_ = 0;
		GError* _tmp9_ = NULL;
		const gchar* _tmp10_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp6_ = action;
		_tmp7_ = e;
		_tmp8_ = _tmp7_->code;
		_tmp9_ = e;
		_tmp10_ = _tmp9_->message;
		gupnp_service_action_return_error (_tmp6_, (guint) _tmp8_, _tmp10_);
		_g_error_free0 (e);
	}
	__finally1:
	if (G_UNLIKELY (_inner_error_ != 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_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		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_ = NULL;
	RygelPlugin* plugin = NULL;
	GUPnPRootDevice* _tmp1_ = NULL;
	GUPnPRootDevice* _tmp2_ = NULL;
	GUPnPRootDevice* _tmp3_ = NULL;
	GUPnPResourceFactory* _tmp4_ = NULL;
	GUPnPResourceFactory* _tmp5_ = NULL;
	RygelPlugin* _tmp6_ = NULL;
	RygelPlugin* _tmp7_ = NULL;
	gboolean _tmp8_ = FALSE;
	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_;
	_tmp8_ = rygel_ruih_service_manager_ui_list_available (manager);
	rygel_plugin_set_active (plugin, _tmp8_);
	_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) {
	rygel_ruih_service_parent_class = g_type_class_peek_parent (klass);
	((GObjectClass *) klass)->constructed = rygel_ruih_service_real_constructed;
}


static void rygel_ruih_service_instance_init (RygelRuihService * self) {
}


/**
 * Basic implementation of UPnP RemoteUIServer service version 1.
 */
GType rygel_ruih_service_get_type (void) {
	static volatile gsize rygel_ruih_service_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_ruih_service_type_id__volatile)) {
		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);
		g_once_init_leave (&rygel_ruih_service_type_id__volatile, rygel_ruih_service_type_id);
	}
	return rygel_ruih_service_type_id__volatile;
}



