/* rygel-media-art-store.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-media-art-store.vala, do not modify */

/*
 * Copyright (C) 2010-2014 Jens Georg <mail@jensge.org>.
 * Copyright (C) 2012 Intel Corporation.
 *
 * 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.h>
#include <glib-object.h>
#include "rygel-server.h"
#include <libmediaart/mediaart.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include "rygel-core.h"
#include <glib/gi18n-lib.h>

#define TYPE_MEDIA_ART_STORE_ERROR (media_art_store_error_get_type ())
enum  {
	RYGEL_MEDIA_ART_STORE_0_PROPERTY,
	RYGEL_MEDIA_ART_STORE_NUM_PROPERTIES
};
static GParamSpec* rygel_media_art_store_properties[RYGEL_MEDIA_ART_STORE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

typedef enum  {
	MEDIA_ART_STORE_ERROR_NO_DIR,
	MEDIA_ART_STORE_ERROR_NO_MEDIA_ART
} MediaArtStoreError;
#define MEDIA_ART_STORE_ERROR media_art_store_error_quark ()

struct _RygelMediaArtStorePrivate {
	MediaArtProcess* media_art_process;
};

static gint RygelMediaArtStore_private_offset;
static gpointer rygel_media_art_store_parent_class = NULL;
static RygelMediaArtStore* rygel_media_art_store_media_art_store;
static RygelMediaArtStore* rygel_media_art_store_media_art_store = NULL;
static gboolean rygel_media_art_store_first_time;
static gboolean rygel_media_art_store_first_time = TRUE;

VALA_EXTERN GQuark media_art_store_error_quark (void);
GType media_art_store_error_get_type (void) G_GNUC_CONST ;
static RygelMediaArtStore* rygel_media_art_store_new (GError** error);
static RygelMediaArtStore* rygel_media_art_store_construct (GType object_type,
                                                     GError** error);
static void rygel_media_art_store_finalize (GObject * obj);
static GType rygel_media_art_store_get_type_once (void);

static const gchar* RYGEL_MEDIA_ART_STORE_types[6] = {"track", "album", "artist", "podcast", "radio", "video"};

GQuark
media_art_store_error_quark (void)
{
	return g_quark_from_static_string ("media-art-store-error-quark");
}

static GType
media_art_store_error_get_type_once (void)
{
	static const GEnumValue values[] = {{MEDIA_ART_STORE_ERROR_NO_DIR, "MEDIA_ART_STORE_ERROR_NO_DIR", "no-dir"}, {MEDIA_ART_STORE_ERROR_NO_MEDIA_ART, "MEDIA_ART_STORE_ERROR_NO_MEDIA_ART", "no-media-art"}, {0, NULL, NULL}};
	GType media_art_store_error_type_id;
	media_art_store_error_type_id = g_enum_register_static ("MediaArtStoreError", values);
	return media_art_store_error_type_id;
}

GType
media_art_store_error_get_type (void)
{
	static volatile gsize media_art_store_error_type_id__once = 0;
	if (g_once_init_enter (&media_art_store_error_type_id__once)) {
		GType media_art_store_error_type_id;
		media_art_store_error_type_id = media_art_store_error_get_type_once ();
		g_once_init_leave (&media_art_store_error_type_id__once, media_art_store_error_type_id);
	}
	return media_art_store_error_type_id__once;
}

static inline gpointer
rygel_media_art_store_get_instance_private (RygelMediaArtStore* self)
{
	return G_STRUCT_MEMBER_P (self, RygelMediaArtStore_private_offset);
}

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

RygelMediaArtStore*
rygel_media_art_store_get_default (void)
{
	RygelMediaArtStore* _tmp5_;
	RygelMediaArtStore* _tmp6_;
	GError* _inner_error0_ = NULL;
	RygelMediaArtStore* result;
	if (rygel_media_art_store_first_time) {
		{
			RygelMediaArtStore* _tmp0_ = NULL;
			RygelMediaArtStore* _tmp1_;
			RygelMediaArtStore* _tmp2_;
			media_art_plugin_init (128);
			_tmp1_ = rygel_media_art_store_new (&_inner_error0_);
			_tmp0_ = _tmp1_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				if (_inner_error0_->domain == MEDIA_ART_STORE_ERROR) {
					goto __catch0_media_art_store_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_);
				return NULL;
			}
			_tmp2_ = _tmp0_;
			_tmp0_ = NULL;
			_g_object_unref0 (rygel_media_art_store_media_art_store);
			rygel_media_art_store_media_art_store = _tmp2_;
			_g_object_unref0 (_tmp0_);
		}
		goto __finally0;
		__catch0_media_art_store_error:
		{
			GError* _error_ = NULL;
			GError* _tmp3_;
			const gchar* _tmp4_;
			_error_ = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp3_ = _error_;
			_tmp4_ = _tmp3_->message;
			g_warning ("rygel-media-art-store.vala:47: No media art available: %s", _tmp4_);
			_g_error_free0 (_error_);
		}
		__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_);
			return NULL;
		}
	}
	rygel_media_art_store_first_time = FALSE;
	_tmp5_ = rygel_media_art_store_media_art_store;
	_tmp6_ = _g_object_ref0 (_tmp5_);
	result = _tmp6_;
	return result;
}

RygelThumbnail*
rygel_media_art_store_lookup_media_art (RygelMediaArtStore* self,
                                        RygelMusicItem* item,
                                        GError** error)
{
	GFile* file = NULL;
	GFile* _tmp25_;
	GFileInfo* info = NULL;
	GFile* _tmp26_;
	GFileInfo* _tmp27_;
	GFileInfo* _tmp28_;
	RygelThumbnail* thumb = NULL;
	RygelThumbnail* _tmp29_;
	RygelThumbnail* _tmp30_;
	GFile* _tmp31_;
	gchar* _tmp32_;
	RygelThumbnail* _tmp33_;
	GFileInfo* _tmp34_;
	GError* _inner_error0_ = NULL;
	RygelThumbnail* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (item != NULL, NULL);
	file = NULL;
	{
		const gchar** type_collection = NULL;
		gint type_collection_length1 = 0;
		gint _type_collection_size_ = 0;
		gint type_it = 0;
		type_collection = RYGEL_MEDIA_ART_STORE_types;
		type_collection_length1 = G_N_ELEMENTS (RYGEL_MEDIA_ART_STORE_types);
		for (type_it = 0; type_it < type_collection_length1; type_it = type_it + 1) {
			const gchar* type = NULL;
			type = type_collection[type_it];
			{
				gboolean _tmp0_ = FALSE;
				gboolean _tmp1_ = FALSE;
				const gchar* _tmp2_;
				const gchar* _tmp12_ = NULL;
				const gchar* _tmp13_;
				const gchar* _tmp18_;
				const gchar* _tmp19_;
				const gchar* _tmp20_;
				GFile* _tmp21_ = NULL;
				gboolean _tmp22_ = FALSE;
				GFile* _tmp23_;
				_tmp2_ = type;
				if (g_strcmp0 (_tmp2_, "album") == 0) {
					const gchar* _tmp3_;
					const gchar* _tmp4_;
					_tmp3_ = rygel_audio_item_get_album ((RygelAudioItem*) item);
					_tmp4_ = _tmp3_;
					_tmp1_ = _tmp4_ == NULL;
				} else {
					_tmp1_ = FALSE;
				}
				if (_tmp1_) {
					const gchar* _tmp5_;
					const gchar* _tmp6_;
					_tmp5_ = rygel_media_object_get_artist ((RygelMediaObject*) item);
					_tmp6_ = _tmp5_;
					_tmp0_ = _tmp6_ == NULL;
				} else {
					_tmp0_ = FALSE;
				}
				if (_tmp0_) {
					continue;
				} else {
					gboolean _tmp7_ = FALSE;
					const gchar* _tmp8_;
					const gchar* _tmp9_;
					_tmp8_ = rygel_media_object_get_artist ((RygelMediaObject*) item);
					_tmp9_ = _tmp8_;
					if (_tmp9_ == NULL) {
						const gchar* _tmp10_;
						const gchar* _tmp11_;
						_tmp10_ = rygel_media_object_get_title ((RygelMediaObject*) item);
						_tmp11_ = _tmp10_;
						_tmp7_ = _tmp11_ == NULL;
					} else {
						_tmp7_ = FALSE;
					}
					if (_tmp7_) {
						continue;
					}
				}
				_tmp13_ = type;
				if (g_strcmp0 (_tmp13_, "album") == 0) {
					const gchar* _tmp14_;
					const gchar* _tmp15_;
					_tmp14_ = rygel_audio_item_get_album ((RygelAudioItem*) item);
					_tmp15_ = _tmp14_;
					_tmp12_ = _tmp15_;
				} else {
					const gchar* _tmp16_;
					const gchar* _tmp17_;
					_tmp16_ = rygel_media_object_get_title ((RygelMediaObject*) item);
					_tmp17_ = _tmp16_;
					_tmp12_ = _tmp17_;
				}
				_tmp18_ = rygel_media_object_get_artist ((RygelMediaObject*) item);
				_tmp19_ = _tmp18_;
				_tmp20_ = type;
				media_art_get_file (_tmp19_, _tmp12_, _tmp20_, &_tmp21_);
				_g_object_unref0 (file);
				file = _tmp21_;
				_tmp23_ = file;
				if (_tmp23_ != NULL) {
					GFile* _tmp24_;
					_tmp24_ = file;
					_tmp22_ = g_file_query_exists (_tmp24_, NULL);
				} else {
					_tmp22_ = FALSE;
				}
				if (_tmp22_) {
					break;
				} else {
					_g_object_unref0 (file);
					file = NULL;
				}
			}
		}
	}
	_tmp25_ = file;
	if (_tmp25_ == NULL) {
		result = NULL;
		_g_object_unref0 (file);
		return result;
	}
	_tmp26_ = file;
	_tmp27_ = g_file_query_info (_tmp26_, G_FILE_ATTRIBUTE_ACCESS_CAN_READ "," G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
	info = _tmp27_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (file);
		return NULL;
	}
	_tmp28_ = info;
	if (!g_file_info_get_attribute_boolean (_tmp28_, G_FILE_ATTRIBUTE_ACCESS_CAN_READ)) {
		result = NULL;
		_g_object_unref0 (info);
		_g_object_unref0 (file);
		return result;
	}
	_tmp29_ = rygel_thumbnail_new ("image/jpeg", "JPEG_TN", "jpg");
	thumb = _tmp29_;
	_tmp30_ = thumb;
	_tmp31_ = file;
	_tmp32_ = g_file_get_uri (_tmp31_);
	_g_free0 (((RygelIconInfo*) _tmp30_)->uri);
	((RygelIconInfo*) _tmp30_)->uri = _tmp32_;
	_tmp33_ = thumb;
	_tmp34_ = info;
	((RygelIconInfo*) _tmp33_)->size = (gint64) g_file_info_get_size (_tmp34_);
	result = thumb;
	_g_object_unref0 (info);
	_g_object_unref0 (file);
	return result;
}

void
rygel_media_art_store_add (RygelMediaArtStore* self,
                           RygelMusicItem* item,
                           GFile* file,
                           guint8* data,
                           gint data_length1,
                           const gchar* mime)
{
	MediaArtProcess* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (item != NULL);
	g_return_if_fail (file != NULL);
	g_return_if_fail (mime != NULL);
	_tmp0_ = self->priv->media_art_process;
	if (_tmp0_ == NULL) {
		return;
	}
	{
		MediaArtProcess* _tmp1_;
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		_tmp1_ = self->priv->media_art_process;
		_tmp2_ = rygel_media_object_get_artist ((RygelMediaObject*) item);
		_tmp3_ = _tmp2_;
		_tmp4_ = rygel_audio_item_get_album ((RygelAudioItem*) item);
		_tmp5_ = _tmp4_;
		media_art_process_buffer (_tmp1_, MEDIA_ART_ALBUM, MEDIA_ART_PROCESS_FLAGS_NONE, file, data, (gsize) data_length1, mime, _tmp3_, _tmp5_, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		gchar* _tmp6_;
		gchar* _tmp7_;
		GError* _tmp8_;
		const gchar* _tmp9_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp6_ = g_file_get_uri (file);
		_tmp7_ = _tmp6_;
		_tmp8_ = _error_;
		_tmp9_ = _tmp8_->message;
		g_warning (_ ("Failed to add album art for %s: %s"), _tmp7_, _tmp9_);
		_g_free0 (_tmp7_);
		_g_error_free0 (_error_);
	}
	__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_);
		return;
	}
}

void
rygel_media_art_store_search_media_art_for_file (RygelMediaArtStore* self,
                                                 RygelMusicItem* item,
                                                 GFile* file)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (item != NULL);
	g_return_if_fail (file != NULL);
	{
		MediaArtProcess* _tmp0_;
		const gchar* _tmp1_;
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		_tmp0_ = self->priv->media_art_process;
		_tmp1_ = rygel_media_object_get_artist ((RygelMediaObject*) item);
		_tmp2_ = _tmp1_;
		_tmp3_ = rygel_audio_item_get_album ((RygelAudioItem*) item);
		_tmp4_ = _tmp3_;
		media_art_process_file (_tmp0_, MEDIA_ART_ALBUM, MEDIA_ART_PROCESS_FLAGS_NONE, file, _tmp2_, _tmp4_, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		gchar* _tmp5_;
		gchar* _tmp6_;
		GError* _tmp7_;
		const gchar* _tmp8_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = g_file_get_uri (file);
		_tmp6_ = _tmp5_;
		_tmp7_ = _error_;
		_tmp8_ = _tmp7_->message;
		g_warning (_ ("Failed to find media art for %s: %s"), _tmp6_, _tmp8_);
		_g_free0 (_tmp6_);
		_g_error_free0 (_error_);
	}
	__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_);
		return;
	}
}

static RygelMediaArtStore*
rygel_media_art_store_construct (GType object_type,
                                 GError** error)
{
	RygelMediaArtStore * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (RygelMediaArtStore*) g_object_new (object_type, NULL);
	{
		MediaArtProcess* _tmp0_ = NULL;
		MediaArtProcess* _tmp1_;
		MediaArtProcess* _tmp2_;
		_tmp1_ = media_art_process_new (&_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->media_art_process);
		self->priv->media_art_process = _tmp2_;
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _error_ = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		GError* _tmp5_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_g_object_unref0 (self->priv->media_art_process);
		self->priv->media_art_process = NULL;
		_tmp3_ = _error_;
		_tmp4_ = _tmp3_->message;
		_tmp5_ = g_error_new (MEDIA_ART_STORE_ERROR, MEDIA_ART_STORE_ERROR_NO_MEDIA_ART, "%s", _tmp4_);
		_inner_error0_ = _tmp5_;
		_g_error_free0 (_error_);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == MEDIA_ART_STORE_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;
}

static RygelMediaArtStore*
rygel_media_art_store_new (GError** error)
{
	return rygel_media_art_store_construct (RYGEL_TYPE_MEDIA_ART_STORE, error);
}

static void
rygel_media_art_store_class_init (RygelMediaArtStoreClass * klass,
                                  gpointer klass_data)
{
	rygel_media_art_store_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelMediaArtStore_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_media_art_store_finalize;
}

static void
rygel_media_art_store_instance_init (RygelMediaArtStore * self,
                                     gpointer klass)
{
	self->priv = rygel_media_art_store_get_instance_private (self);
}

static void
rygel_media_art_store_finalize (GObject * obj)
{
	RygelMediaArtStore * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_MEDIA_ART_STORE, RygelMediaArtStore);
	_g_object_unref0 (self->priv->media_art_process);
	G_OBJECT_CLASS (rygel_media_art_store_parent_class)->finalize (obj);
}

/**
 * This maps RygelMusicItem objects to their cached cover art,
 * implementing the GNOME
 * [[https://live.gnome.org/MediaArtStorageSpec|MediaArt storage specification]].
 */
static GType
rygel_media_art_store_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelMediaArtStoreClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_media_art_store_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelMediaArtStore), 0, (GInstanceInitFunc) rygel_media_art_store_instance_init, NULL };
	GType rygel_media_art_store_type_id;
	rygel_media_art_store_type_id = g_type_register_static (G_TYPE_OBJECT, "RygelMediaArtStore", &g_define_type_info, 0);
	RygelMediaArtStore_private_offset = g_type_add_instance_private (rygel_media_art_store_type_id, sizeof (RygelMediaArtStorePrivate));
	return rygel_media_art_store_type_id;
}

GType
rygel_media_art_store_get_type (void)
{
	static volatile gsize rygel_media_art_store_type_id__once = 0;
	if (g_once_init_enter (&rygel_media_art_store_type_id__once)) {
		GType rygel_media_art_store_type_id;
		rygel_media_art_store_type_id = rygel_media_art_store_get_type_once ();
		g_once_init_leave (&rygel_media_art_store_type_id__once, rygel_media_art_store_type_id);
	}
	return rygel_media_art_store_type_id__once;
}

