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

/*
 * Copyright (C) 2017 Samuel CUELLA
 *
 * Author: Samuel CUELLA <samuel.cuella@supinfo.com>
 * Author: 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 <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "rygel-server.h"
#include <gio/gio.h>
#include <libsoup/soup.h>
#include <gee.h>

#define RYGEL_XBM_C4_XBOX_HACKS_AGENT "(.*XBMC.*Xbox.*)|(Platinum/0.5.3.0)"

#define RYGEL_TYPE_CLIENT_HACKS (rygel_client_hacks_get_type ())
#define RYGEL_CLIENT_HACKS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_CLIENT_HACKS, RygelClientHacks))
#define RYGEL_CLIENT_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_CLIENT_HACKS, RygelClientHacksClass))
#define RYGEL_IS_CLIENT_HACKS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_CLIENT_HACKS))
#define RYGEL_IS_CLIENT_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_CLIENT_HACKS))
#define RYGEL_CLIENT_HACKS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_CLIENT_HACKS, RygelClientHacksClass))

typedef struct _RygelClientHacks RygelClientHacks;
typedef struct _RygelClientHacksClass RygelClientHacksClass;
typedef struct _RygelClientHacksPrivate RygelClientHacksPrivate;

#define RYGEL_TYPE_MEDIA_QUERY_ACTION (rygel_media_query_action_get_type ())
#define RYGEL_MEDIA_QUERY_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_MEDIA_QUERY_ACTION, RygelMediaQueryAction))
#define RYGEL_MEDIA_QUERY_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_MEDIA_QUERY_ACTION, RygelMediaQueryActionClass))
#define RYGEL_IS_MEDIA_QUERY_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_MEDIA_QUERY_ACTION))
#define RYGEL_IS_MEDIA_QUERY_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_MEDIA_QUERY_ACTION))
#define RYGEL_MEDIA_QUERY_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_MEDIA_QUERY_ACTION, RygelMediaQueryActionClass))

typedef struct _RygelMediaQueryAction RygelMediaQueryAction;
typedef struct _RygelMediaQueryActionClass RygelMediaQueryActionClass;

#define RYGEL_TYPE_XBMC_HACKS (rygel_xbmc_hacks_get_type ())
#define RYGEL_XBMC_HACKS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_XBMC_HACKS, RygelXBMCHacks))
#define RYGEL_XBMC_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_XBMC_HACKS, RygelXBMCHacksClass))
#define RYGEL_IS_XBMC_HACKS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_XBMC_HACKS))
#define RYGEL_IS_XBMC_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_XBMC_HACKS))
#define RYGEL_XBMC_HACKS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_XBMC_HACKS, RygelXBMCHacksClass))

typedef struct _RygelXBMCHacks RygelXBMCHacks;
typedef struct _RygelXBMCHacksClass RygelXBMCHacksClass;
typedef struct _RygelXBMCHacksPrivate RygelXBMCHacksPrivate;

#define RYGEL_TYPE_XBM_C4_XBOX_HACKS (rygel_xbm_c4_xbox_hacks_get_type ())
#define RYGEL_XBM_C4_XBOX_HACKS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_XBM_C4_XBOX_HACKS, RygelXBMC4XBoxHacks))
#define RYGEL_XBM_C4_XBOX_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_XBM_C4_XBOX_HACKS, RygelXBMC4XBoxHacksClass))
#define RYGEL_IS_XBM_C4_XBOX_HACKS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_XBM_C4_XBOX_HACKS))
#define RYGEL_IS_XBM_C4_XBOX_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_XBM_C4_XBOX_HACKS))
#define RYGEL_XBM_C4_XBOX_HACKS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_XBM_C4_XBOX_HACKS, RygelXBMC4XBoxHacksClass))

typedef struct _RygelXBMC4XBoxHacks RygelXBMC4XBoxHacks;
typedef struct _RygelXBMC4XBoxHacksClass RygelXBMC4XBoxHacksClass;
typedef struct _RygelXBMC4XBoxHacksPrivate RygelXBMC4XBoxHacksPrivate;
enum  {
	RYGEL_XBM_C4_XBOX_HACKS_0_PROPERTY,
	RYGEL_XBM_C4_XBOX_HACKS_NUM_PROPERTIES
};
static GParamSpec* rygel_xbm_c4_xbox_hacks_properties[RYGEL_XBM_C4_XBOX_HACKS_NUM_PROPERTIES];

#define RYGEL_TYPE_CLIENT_HACKS_ERROR (rygel_client_hacks_error_get_type ())
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _RygelClientHacks {
	GObject parent_instance;
	RygelClientHacksPrivate * priv;
	GRegex* agent_regex;
};

struct _RygelClientHacksClass {
	GObjectClass parent_class;
	void (*translate_container_id) (RygelClientHacks* self, RygelMediaQueryAction* action, gchar** container_id);
	void (*apply) (RygelClientHacks* self, RygelMediaObject* object);
	void (*filter_sort_criteria) (RygelClientHacks* self, gchar** sort_criteria);
	gboolean (*force_seek) (RygelClientHacks* self);
	void (*modify_headers) (RygelClientHacks* self, RygelHTTPRequest* request);
	void (*search) (RygelClientHacks* self, RygelSearchableContainer* container, RygelSearchExpression* expression, guint offset, guint max_count, const gchar* sort_criteria, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_);
	RygelMediaObjects* (*search_finish) (RygelClientHacks* self, GAsyncResult* _res_, guint* total_matches, GError** error);
};

struct _RygelXBMCHacks {
	RygelClientHacks parent_instance;
	RygelXBMCHacksPrivate * priv;
};

struct _RygelXBMCHacksClass {
	RygelClientHacksClass parent_class;
};

struct _RygelXBMC4XBoxHacks {
	RygelXBMCHacks parent_instance;
	RygelXBMC4XBoxHacksPrivate * priv;
};

struct _RygelXBMC4XBoxHacksClass {
	RygelXBMCHacksClass parent_class;
};

typedef enum  {
	RYGEL_CLIENT_HACKS_ERROR_NA
} RygelClientHacksError;
#define RYGEL_CLIENT_HACKS_ERROR rygel_client_hacks_error_quark ()

static gpointer rygel_xbm_c4_xbox_hacks_parent_class = NULL;

VALA_EXTERN GType rygel_client_hacks_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType rygel_media_query_action_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType rygel_xbmc_hacks_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType rygel_xbm_c4_xbox_hacks_get_type (void) G_GNUC_CONST ;
VALA_EXTERN void rygel_client_hacks_apply (RygelClientHacks* self,
                               RygelMediaObject* object);
VALA_EXTERN GQuark rygel_client_hacks_error_quark (void);
GType rygel_client_hacks_error_get_type (void) G_GNUC_CONST ;
VALA_EXTERN RygelXBMC4XBoxHacks* rygel_xbm_c4_xbox_hacks_new (SoupMessage* message,
                                                  GError** error);
VALA_EXTERN RygelXBMC4XBoxHacks* rygel_xbm_c4_xbox_hacks_construct (GType object_type,
                                                        SoupMessage* message,
                                                        GError** error);
VALA_EXTERN RygelXBMCHacks* rygel_xbmc_hacks_new (SoupMessage* message,
                                      const gchar* agent,
                                      GError** error);
VALA_EXTERN RygelXBMCHacks* rygel_xbmc_hacks_construct (GType object_type,
                                            SoupMessage* message,
                                            const gchar* agent,
                                            GError** error);
static void rygel_xbm_c4_xbox_hacks_real_apply (RygelClientHacks* base,
                                         RygelMediaObject* object);
static GType rygel_xbm_c4_xbox_hacks_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

RygelXBMC4XBoxHacks*
rygel_xbm_c4_xbox_hacks_construct (GType object_type,
                                   SoupMessage* message,
                                   GError** error)
{
	RygelXBMC4XBoxHacks * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (RygelXBMC4XBoxHacks*) rygel_xbmc_hacks_construct (object_type, message, RYGEL_XBM_C4_XBOX_HACKS_AGENT, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == RYGEL_CLIENT_HACKS_ERROR) {
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (self);
			return NULL;
		} else {
			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 NULL;
		}
	}
	return self;
}

RygelXBMC4XBoxHacks*
rygel_xbm_c4_xbox_hacks_new (SoupMessage* message,
                             GError** error)
{
	return rygel_xbm_c4_xbox_hacks_construct (RYGEL_TYPE_XBM_C4_XBOX_HACKS, message, error);
}

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

static void
rygel_xbm_c4_xbox_hacks_real_apply (RygelClientHacks* base,
                                    RygelMediaObject* object)
{
	RygelXBMC4XBoxHacks * self;
	GeeList* resources = NULL;
	GeeList* _tmp0_;
	RygelMediaResource* primary = NULL;
	GeeList* _tmp1_;
	gpointer _tmp2_;
	RygelMediaResource* _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	RygelMediaResource* _tmp6_;
	gint _tmp7_;
	gint _tmp8_;
	RygelMediaResource* _tmp9_;
	gint _tmp10_;
	gint _tmp11_;
	RygelMediaResource* _tmp12_;
	const gchar* _tmp13_;
	const gchar* _tmp14_;
	RygelMediaResource* _tmp15_;
	const gchar* _tmp16_;
	const gchar* _tmp17_;
	gboolean _tmp18_ = FALSE;
	RygelMediaResource* _tmp19_;
	gint _tmp20_;
	gint _tmp21_;
	RygelMediaResource* right_one = NULL;
	RygelMediaResource* _tmp38_;
	self = (RygelXBMC4XBoxHacks*) base;
	g_return_if_fail (object != NULL);
	RYGEL_CLIENT_HACKS_CLASS (rygel_xbm_c4_xbox_hacks_parent_class)->apply ((RygelClientHacks*) G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_TYPE_XBMC_HACKS, RygelXBMCHacks), object);
	_tmp0_ = rygel_media_object_get_resource_list (object);
	resources = _tmp0_;
	_tmp1_ = resources;
	_tmp2_ = gee_list_first (_tmp1_);
	primary = (RygelMediaResource*) _tmp2_;
	_tmp3_ = primary;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (primary);
		_g_object_unref0 (resources);
		return;
	}
	_tmp4_ = rygel_media_object_get_title (object);
	_tmp5_ = _tmp4_;
	_tmp6_ = primary;
	_tmp7_ = rygel_media_resource_get_width (_tmp6_);
	_tmp8_ = _tmp7_;
	_tmp9_ = primary;
	_tmp10_ = rygel_media_resource_get_height (_tmp9_);
	_tmp11_ = _tmp10_;
	_tmp12_ = primary;
	_tmp13_ = rygel_media_resource_get_extension (_tmp12_);
	_tmp14_ = _tmp13_;
	_tmp15_ = primary;
	_tmp16_ = rygel_media_resource_get_dlna_profile (_tmp15_);
	_tmp17_ = _tmp16_;
	g_debug ("rygel-xbmc4xbox-hacks.vala:46: %s primary resource is %dx%d, %s. DNLA " \
"profile is %s", _tmp5_, _tmp8_, _tmp11_, _tmp14_, _tmp17_);
	_tmp19_ = primary;
	_tmp20_ = rygel_media_resource_get_width (_tmp19_);
	_tmp21_ = _tmp20_;
	if (_tmp21_ > 720) {
		_tmp18_ = TRUE;
	} else {
		RygelMediaResource* _tmp22_;
		gint _tmp23_;
		gint _tmp24_;
		_tmp22_ = primary;
		_tmp23_ = rygel_media_resource_get_height (_tmp22_);
		_tmp24_ = _tmp23_;
		_tmp18_ = _tmp24_ > 480;
	}
	if (!_tmp18_) {
		_g_object_unref0 (primary);
		_g_object_unref0 (resources);
		return;
	}
	right_one = NULL;
	{
		GeeList* _resource_list = NULL;
		GeeList* _tmp25_;
		gint _resource_size = 0;
		GeeList* _tmp26_;
		gint _tmp27_;
		gint _tmp28_;
		gint _resource_index = 0;
		_tmp25_ = resources;
		_resource_list = _tmp25_;
		_tmp26_ = _resource_list;
		_tmp27_ = gee_collection_get_size ((GeeCollection*) _tmp26_);
		_tmp28_ = _tmp27_;
		_resource_size = _tmp28_;
		_resource_index = -1;
		while (TRUE) {
			gint _tmp29_;
			gint _tmp30_;
			RygelMediaResource* resource = NULL;
			GeeList* _tmp31_;
			gpointer _tmp32_;
			RygelMediaResource* _tmp33_;
			const gchar* _tmp34_;
			const gchar* _tmp35_;
			_resource_index = _resource_index + 1;
			_tmp29_ = _resource_index;
			_tmp30_ = _resource_size;
			if (!(_tmp29_ < _tmp30_)) {
				break;
			}
			_tmp31_ = _resource_list;
			_tmp32_ = gee_list_get (_tmp31_, _resource_index);
			resource = (RygelMediaResource*) _tmp32_;
			_tmp33_ = resource;
			_tmp34_ = rygel_media_resource_get_dlna_profile (_tmp33_);
			_tmp35_ = _tmp34_;
			if (g_strcmp0 (_tmp35_, "MPEG_TS_SD_EU_ISO") == 0) {
				RygelMediaResource* _tmp36_;
				RygelMediaResource* _tmp37_;
				_tmp36_ = resource;
				_tmp37_ = _g_object_ref0 (_tmp36_);
				_g_object_unref0 (right_one);
				right_one = _tmp37_;
				_g_object_unref0 (resource);
				break;
			}
			_g_object_unref0 (resource);
		}
	}
	_tmp38_ = right_one;
	if (_tmp38_ != NULL) {
		GeeList* _tmp39_;
		RygelMediaResource* _tmp40_;
		_tmp39_ = resources;
		_tmp40_ = right_one;
		gee_list_set (_tmp39_, 0, _tmp40_);
	}
	_g_object_unref0 (right_one);
	_g_object_unref0 (primary);
	_g_object_unref0 (resources);
}

static void
rygel_xbm_c4_xbox_hacks_class_init (RygelXBMC4XBoxHacksClass * klass,
                                    gpointer klass_data)
{
	rygel_xbm_c4_xbox_hacks_parent_class = g_type_class_peek_parent (klass);
	((RygelClientHacksClass *) klass)->apply = (void (*) (RygelClientHacks*, RygelMediaObject*)) rygel_xbm_c4_xbox_hacks_real_apply;
}

static void
rygel_xbm_c4_xbox_hacks_instance_init (RygelXBMC4XBoxHacks * self,
                                       gpointer klass)
{
}

static GType
rygel_xbm_c4_xbox_hacks_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelXBMC4XBoxHacksClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_xbm_c4_xbox_hacks_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelXBMC4XBoxHacks), 0, (GInstanceInitFunc) rygel_xbm_c4_xbox_hacks_instance_init, NULL };
	GType rygel_xbm_c4_xbox_hacks_type_id;
	rygel_xbm_c4_xbox_hacks_type_id = g_type_register_static (RYGEL_TYPE_XBMC_HACKS, "RygelXBMC4XBoxHacks", &g_define_type_info, 0);
	return rygel_xbm_c4_xbox_hacks_type_id;
}

GType
rygel_xbm_c4_xbox_hacks_get_type (void)
{
	static volatile gsize rygel_xbm_c4_xbox_hacks_type_id__once = 0;
	if (g_once_init_enter (&rygel_xbm_c4_xbox_hacks_type_id__once)) {
		GType rygel_xbm_c4_xbox_hacks_type_id;
		rygel_xbm_c4_xbox_hacks_type_id = rygel_xbm_c4_xbox_hacks_get_type_once ();
		g_once_init_leave (&rygel_xbm_c4_xbox_hacks_type_id__once, rygel_xbm_c4_xbox_hacks_type_id);
	}
	return rygel_xbm_c4_xbox_hacks_type_id__once;
}

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

