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

/*
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.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>
#include "rygel-core.h"

#define RYGEL_PANASONIC_HACKS_AGENT ".*Panasonic MIL DLNA CP.*"

#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_PANASONIC_HACKS (rygel_panasonic_hacks_get_type ())
#define RYGEL_PANASONIC_HACKS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_PANASONIC_HACKS, RygelPanasonicHacks))
#define RYGEL_PANASONIC_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_PANASONIC_HACKS, RygelPanasonicHacksClass))
#define RYGEL_IS_PANASONIC_HACKS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_PANASONIC_HACKS))
#define RYGEL_IS_PANASONIC_HACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_PANASONIC_HACKS))
#define RYGEL_PANASONIC_HACKS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_PANASONIC_HACKS, RygelPanasonicHacksClass))

typedef struct _RygelPanasonicHacks RygelPanasonicHacks;
typedef struct _RygelPanasonicHacksClass RygelPanasonicHacksClass;
typedef struct _RygelPanasonicHacksPrivate RygelPanasonicHacksPrivate;
enum  {
	RYGEL_PANASONIC_HACKS_0_PROPERTY,
	RYGEL_PANASONIC_HACKS_NUM_PROPERTIES
};
static GParamSpec* rygel_panasonic_hacks_properties[RYGEL_PANASONIC_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)))
#define _rygel_icon_info_unref0(var) ((var == NULL) ? NULL : (var = (rygel_icon_info_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_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 _RygelPanasonicHacks {
	RygelClientHacks parent_instance;
	RygelPanasonicHacksPrivate * priv;
};

struct _RygelPanasonicHacksClass {
	RygelClientHacksClass parent_class;
};

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

static gpointer rygel_panasonic_hacks_parent_class = NULL;
static GRegex* rygel_panasonic_hacks_mime_regex;
static GRegex* rygel_panasonic_hacks_mime_regex = NULL;
static GRegex* rygel_panasonic_hacks_dlna_regex;
static GRegex* rygel_panasonic_hacks_dlna_regex = 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_panasonic_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 RygelPanasonicHacks* rygel_panasonic_hacks_new (SoupMessage* message,
                                                GError** error);
VALA_EXTERN RygelPanasonicHacks* rygel_panasonic_hacks_construct (GType object_type,
                                                      SoupMessage* message,
                                                      GError** error);
VALA_EXTERN RygelClientHacks* rygel_client_hacks_construct (GType object_type,
                                                const gchar* agent,
                                                SoupMessage* message,
                                                GError** error);
static void rygel_panasonic_hacks_real_apply (RygelClientHacks* base,
                                       RygelMediaObject* object);
static void rygel_panasonic_hacks_finalize (GObject * obj);
static GType rygel_panasonic_hacks_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

RygelPanasonicHacks*
rygel_panasonic_hacks_construct (GType object_type,
                                 SoupMessage* message,
                                 GError** error)
{
	RygelPanasonicHacks * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (RygelPanasonicHacks*) rygel_client_hacks_construct (object_type, RYGEL_PANASONIC_HACKS_AGENT, message, &_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;
}

RygelPanasonicHacks*
rygel_panasonic_hacks_new (SoupMessage* message,
                           GError** error)
{
	return rygel_panasonic_hacks_construct (RYGEL_TYPE_PANASONIC_HACKS, message, error);
}

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

static void
rygel_panasonic_hacks_real_apply (RygelClientHacks* base,
                                  RygelMediaObject* object)
{
	RygelPanasonicHacks * self;
	RygelVisualItem* item = NULL;
	RygelVisualItem* _tmp0_;
	RygelVisualItem* _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (RygelPanasonicHacks*) base;
	g_return_if_fail (object != NULL);
	_tmp0_ = _g_object_ref0 (RYGEL_IS_VISUAL_ITEM (object) ? ((RygelVisualItem*) object) : NULL);
	item = _tmp0_;
	_tmp1_ = item;
	if (_tmp1_ == NULL) {
		_g_object_unref0 (item);
		return;
	}
	{
		GeeArrayList* _thumbnail_list = NULL;
		RygelVisualItem* _tmp2_;
		GeeArrayList* _tmp3_;
		GeeArrayList* _tmp4_;
		gint _thumbnail_size = 0;
		GeeArrayList* _tmp5_;
		gint _tmp6_;
		gint _tmp7_;
		gint _thumbnail_index = 0;
		_tmp2_ = item;
		_tmp3_ = rygel_visual_item_get_thumbnails (_tmp2_);
		_tmp4_ = _tmp3_;
		_thumbnail_list = _tmp4_;
		_tmp5_ = _thumbnail_list;
		_tmp6_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp5_);
		_tmp7_ = _tmp6_;
		_thumbnail_size = _tmp7_;
		_thumbnail_index = -1;
		while (TRUE) {
			gint _tmp8_;
			gint _tmp9_;
			RygelThumbnail* thumbnail = NULL;
			GeeArrayList* _tmp10_;
			gpointer _tmp11_;
			_thumbnail_index = _thumbnail_index + 1;
			_tmp8_ = _thumbnail_index;
			_tmp9_ = _thumbnail_size;
			if (!(_tmp8_ < _tmp9_)) {
				break;
			}
			_tmp10_ = _thumbnail_list;
			_tmp11_ = gee_abstract_list_get ((GeeAbstractList*) _tmp10_, _thumbnail_index);
			thumbnail = (RygelThumbnail*) _tmp11_;
			{
				gchar* _tmp12_ = NULL;
				GRegex* _tmp13_;
				RygelThumbnail* _tmp14_;
				const gchar* _tmp15_;
				gchar* _tmp16_;
				RygelThumbnail* _tmp17_;
				gchar* _tmp18_;
				gchar* _tmp19_ = NULL;
				GRegex* _tmp20_;
				RygelThumbnail* _tmp21_;
				const gchar* _tmp22_;
				gchar* _tmp23_;
				RygelThumbnail* _tmp24_;
				gchar* _tmp25_;
				_tmp13_ = rygel_panasonic_hacks_mime_regex;
				_tmp14_ = thumbnail;
				_tmp15_ = ((RygelIconInfo*) _tmp14_)->mime_type;
				_tmp16_ = g_regex_replace_literal (_tmp13_, _tmp15_, (gssize) -1, 0, "jpeg", 0, &_inner_error0_);
				_tmp12_ = _tmp16_;
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					if (_inner_error0_->domain == G_REGEX_ERROR) {
						goto __catch0_g_regex_error;
					}
					_rygel_icon_info_unref0 (thumbnail);
					_g_object_unref0 (item);
					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;
				}
				_tmp17_ = thumbnail;
				_tmp18_ = _tmp12_;
				_tmp12_ = NULL;
				_g_free0 (((RygelIconInfo*) _tmp17_)->mime_type);
				((RygelIconInfo*) _tmp17_)->mime_type = _tmp18_;
				_tmp20_ = rygel_panasonic_hacks_dlna_regex;
				_tmp21_ = thumbnail;
				_tmp22_ = _tmp21_->dlna_profile;
				_tmp23_ = g_regex_replace_literal (_tmp20_, _tmp22_, (gssize) -1, 0, "JPEG", 0, &_inner_error0_);
				_tmp19_ = _tmp23_;
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					_g_free0 (_tmp12_);
					if (_inner_error0_->domain == G_REGEX_ERROR) {
						goto __catch0_g_regex_error;
					}
					_rygel_icon_info_unref0 (thumbnail);
					_g_object_unref0 (item);
					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;
				}
				_tmp24_ = thumbnail;
				_tmp25_ = _tmp19_;
				_tmp19_ = NULL;
				_g_free0 (_tmp24_->dlna_profile);
				_tmp24_->dlna_profile = _tmp25_;
				_g_free0 (_tmp19_);
				_g_free0 (_tmp12_);
			}
			goto __finally0;
			__catch0_g_regex_error:
			{
				g_clear_error (&_inner_error0_);
				g_assert_not_reached ();
			}
			__finally0:
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_rygel_icon_info_unref0 (thumbnail);
				_g_object_unref0 (item);
				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;
			}
			_rygel_icon_info_unref0 (thumbnail);
		}
	}
	_g_object_unref0 (item);
}

static void
rygel_panasonic_hacks_class_init (RygelPanasonicHacksClass * klass,
                                  gpointer klass_data)
{
	GError* _inner_error0_ = NULL;
	rygel_panasonic_hacks_parent_class = g_type_class_peek_parent (klass);
	((RygelClientHacksClass *) klass)->apply = (void (*) (RygelClientHacks*, RygelMediaObject*)) rygel_panasonic_hacks_real_apply;
	G_OBJECT_CLASS (klass)->finalize = rygel_panasonic_hacks_finalize;
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		GRegex* _tmp3_ = NULL;
		GRegex* _tmp4_;
		GRegex* _tmp5_;
		_tmp1_ = g_regex_new ("png", 0, 0, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			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_);
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_regex_unref0 (rygel_panasonic_hacks_mime_regex);
		rygel_panasonic_hacks_mime_regex = _tmp2_;
		_tmp4_ = g_regex_new ("PNG", 0, 0, &_inner_error0_);
		_tmp3_ = _tmp4_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (_tmp0_);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			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_);
		}
		_tmp5_ = _tmp3_;
		_tmp3_ = NULL;
		_g_regex_unref0 (rygel_panasonic_hacks_dlna_regex);
		rygel_panasonic_hacks_dlna_regex = _tmp5_;
		_g_regex_unref0 (_tmp3_);
		_g_regex_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__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_);
	}
}

static void
rygel_panasonic_hacks_instance_init (RygelPanasonicHacks * self,
                                     gpointer klass)
{
}

static void
rygel_panasonic_hacks_finalize (GObject * obj)
{
	RygelPanasonicHacks * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_PANASONIC_HACKS, RygelPanasonicHacks);
	G_OBJECT_CLASS (rygel_panasonic_hacks_parent_class)->finalize (obj);
}

static GType
rygel_panasonic_hacks_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelPanasonicHacksClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_panasonic_hacks_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelPanasonicHacks), 0, (GInstanceInitFunc) rygel_panasonic_hacks_instance_init, NULL };
	GType rygel_panasonic_hacks_type_id;
	rygel_panasonic_hacks_type_id = g_type_register_static (RYGEL_TYPE_CLIENT_HACKS, "RygelPanasonicHacks", &g_define_type_info, 0);
	return rygel_panasonic_hacks_type_id;
}

GType
rygel_panasonic_hacks_get_type (void)
{
	static volatile gsize rygel_panasonic_hacks_type_id__once = 0;
	if (g_once_init_enter (&rygel_panasonic_hacks_type_id__once)) {
		GType rygel_panasonic_hacks_type_id;
		rygel_panasonic_hacks_type_id = rygel_panasonic_hacks_get_type_once ();
		g_once_init_leave (&rygel_panasonic_hacks_type_id__once, rygel_panasonic_hacks_type_id);
	}
	return rygel_panasonic_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;
}

