/* rygel-ruih-service.c generated by valac 0.40.8, 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 <glib.h>
#include <glib-object.h>
#include <libgupnp/gupnp.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.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;
enum  {
	RYGEL_RUIH_SERVICE_0_PROPERTY,
	RYGEL_RUIH_SERVICE_NUM_PROPERTIES
};
static GParamSpec* rygel_ruih_service_properties[RYGEL_RUIH_SERVICE_NUM_PROPERTIES];

#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;
#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_;
	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;
	GError * _inner_error_ = 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, &input_device_profile, NULL);
	gupnp_service_action_get (action, "UIFilter", G_TYPE_STRING, &input_ui_filter, NULL);
	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* _tmp0_;
		gchar* compat_ui = NULL;
		RygelRuihServiceManager* _tmp1_;
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		const gchar* _tmp5_;
		_tmp0_ = rygel_ruih_service_manager_get_default ();
		manager = _tmp0_;
		_tmp1_ = manager;
		_tmp2_ = input_device_profile;
		_tmp3_ = input_ui_filter;
		_tmp4_ = rygel_ruih_service_manager_get_compatible_uis (_tmp1_, _tmp2_, _tmp3_, &_inner_error_);
		compat_ui = _tmp4_;
		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;
		}
		_tmp5_ = compat_ui;
		gupnp_service_action_set (action, "UIListing", G_TYPE_STRING, _tmp5_, NULL);
		gupnp_service_action_return (action);
		_g_free0 (compat_ui);
		_g_object_unref0 (manager);
	}
	goto __finally1;
	__catch1_rygel_ruih_service_error:
	{
		GError* e = NULL;
		GError* _tmp6_;
		gint _tmp7_;
		GError* _tmp8_;
		const gchar* _tmp9_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp6_ = e;
		_tmp7_ = _tmp6_->code;
		_tmp8_ = e;
		_tmp9_ = _tmp8_->message;
		gupnp_service_action_return_error (action, (guint) _tmp7_, _tmp9_);
		_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_;
	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)
{
	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)
{
}


/**
 * 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;
}



