/* grilo-cover.c generated by valac 0.36.6.6-c7bee, the Vala compiler
 * generated from grilo-cover.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <grilo.h>
#include <libsoup/soup.h>
#include <glib/gstdio.h>


#define GAMES_TYPE_COVER (games_cover_get_type ())
#define GAMES_COVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_COVER, GamesCover))
#define GAMES_IS_COVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_COVER))
#define GAMES_COVER_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GAMES_TYPE_COVER, GamesCoverIface))

typedef struct _GamesCover GamesCover;
typedef struct _GamesCoverIface GamesCoverIface;

#define GAMES_TYPE_GRILO_COVER (games_grilo_cover_get_type ())
#define GAMES_GRILO_COVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GRILO_COVER, GamesGriloCover))
#define GAMES_GRILO_COVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GRILO_COVER, GamesGriloCoverClass))
#define GAMES_IS_GRILO_COVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GRILO_COVER))
#define GAMES_IS_GRILO_COVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GRILO_COVER))
#define GAMES_GRILO_COVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GRILO_COVER, GamesGriloCoverClass))

typedef struct _GamesGriloCover GamesGriloCover;
typedef struct _GamesGriloCoverClass GamesGriloCoverClass;
typedef struct _GamesGriloCoverPrivate GamesGriloCoverPrivate;

#define GAMES_TYPE_GRILO_MEDIA (games_grilo_media_get_type ())
#define GAMES_GRILO_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GRILO_MEDIA, GamesGriloMedia))
#define GAMES_GRILO_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GRILO_MEDIA, GamesGriloMediaClass))
#define GAMES_IS_GRILO_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GRILO_MEDIA))
#define GAMES_IS_GRILO_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GRILO_MEDIA))
#define GAMES_GRILO_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GRILO_MEDIA, GamesGriloMediaClass))

typedef struct _GamesGriloMedia GamesGriloMedia;
typedef struct _GamesGriloMediaClass GamesGriloMediaClass;

#define GAMES_TYPE_UID (games_uid_get_type ())
#define GAMES_UID(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_UID, GamesUid))
#define GAMES_IS_UID(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_UID))
#define GAMES_UID_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GAMES_TYPE_UID, GamesUidIface))

typedef struct _GamesUid GamesUid;
typedef struct _GamesUidIface GamesUidIface;
#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)))
typedef struct _GamesGriloCoverTryFetchCoverData GamesGriloCoverTryFetchCoverData;
typedef struct _Block1Data Block1Data;
typedef struct _GamesGriloCoverFetchCoverData GamesGriloCoverFetchCoverData;

struct _GamesCoverIface {
	GTypeInterface parent_iface;
	GIcon* (*get_cover) (GamesCover* self);
};

struct _GamesGriloCover {
	GObject parent_instance;
	GamesGriloCoverPrivate * priv;
};

struct _GamesGriloCoverClass {
	GObjectClass parent_class;
};

struct _GamesUidIface {
	GTypeInterface parent_iface;
	gchar* (*get_uid) (GamesUid* self, GError** error);
};

struct _GamesGriloCoverPrivate {
	GamesGriloMedia* media;
	GamesUid* uid;
	GIcon* icon;
	gboolean resolving;
	gchar* cover_path;
};

struct _GamesGriloCoverTryFetchCoverData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GAsyncReadyCallback _callback_;
	gboolean _task_complete_;
	GamesGriloCover* self;
	gchar* uri;
	const gchar* _tmp0_;
	GError* e;
	GError* _tmp1_;
	const gchar* _tmp2_;
	GError * _inner_error_;
};

struct _Block1Data {
	int _ref_count_;
	GamesGriloCover* self;
	gchar* cover_path;
	gchar* uri;
	gpointer _async_data_;
};

struct _GamesGriloCoverFetchCoverData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GAsyncReadyCallback _callback_;
	gboolean _task_complete_;
	GamesGriloCover* self;
	gchar* uri;
	Block1Data* _data1_;
	const gchar* _tmp0_;
	gchar* dir;
	gchar* _tmp1_;
	gchar* _tmp2_;
	SoupSession* session;
	SoupSession* _tmp3_;
	SoupMessage* message;
	const gchar* _tmp4_;
	SoupMessage* _tmp5_;
	SoupSession* _tmp6_;
	SoupMessage* _tmp7_;
	SoupMessage* _tmp8_;
	GError * _inner_error_;
};


static gpointer games_grilo_cover_parent_class = NULL;
static GamesCoverIface * games_grilo_cover_games_cover_parent_iface = NULL;

GType games_cover_get_type (void) G_GNUC_CONST;
GType games_grilo_cover_get_type (void) G_GNUC_CONST;
GType games_grilo_media_get_type (void) G_GNUC_CONST;
GType games_uid_get_type (void) G_GNUC_CONST;
#define GAMES_GRILO_COVER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_GRILO_COVER, GamesGriloCoverPrivate))
enum  {
	GAMES_GRILO_COVER_0_PROPERTY
};
GamesGriloCover* games_grilo_cover_new (GamesGriloMedia* media, GamesUid* uid);
GamesGriloCover* games_grilo_cover_construct (GType object_type, GamesGriloMedia* media, GamesUid* uid);
static void games_grilo_cover_on_media_resolved (GamesGriloCover* self);
static void _games_grilo_cover_on_media_resolved_games_grilo_media_resolved (GamesGriloMedia* _sender, gpointer self);
static GIcon* games_grilo_cover_real_get_cover (GamesCover* base);
static void games_grilo_cover_load_cover (GamesGriloCover* self, GError** error);
void games_grilo_media_try_resolve_media (GamesGriloMedia* self);
GrlMedia* games_grilo_media_get_media (GamesGriloMedia* self);
static void games_grilo_cover_try_fetch_cover (GamesGriloCover* self, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void games_grilo_cover_try_fetch_cover_finish (GamesGriloCover* self, GAsyncResult* _res_);
static gchar* games_grilo_cover_get_cover_path (GamesGriloCover* self, GError** error);
gchar* games_application_get_covers_dir (void);
gchar* games_uid_get_uid (GamesUid* self, GError** error);
static void games_grilo_cover_try_fetch_cover_data_free (gpointer _data);
static void games_grilo_cover_try_fetch_cover_async_ready_wrapper (GObject *source_object, GAsyncResult *res, void *user_data);
static gboolean games_grilo_cover_try_fetch_cover_co (GamesGriloCoverTryFetchCoverData* _data_);
static void games_grilo_cover_fetch_cover (GamesGriloCover* self, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void games_grilo_cover_fetch_cover_finish (GamesGriloCover* self, GAsyncResult* _res_, GError** error);
static void games_grilo_cover_try_fetch_cover_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void games_grilo_cover_fetch_cover_data_free (gpointer _data);
static void games_grilo_cover_fetch_cover_async_ready_wrapper (GObject *source_object, GAsyncResult *res, void *user_data);
static gboolean games_grilo_cover_fetch_cover_co (GamesGriloCoverFetchCoverData* _data_);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
void games_application_try_make_dir (const gchar* path);
static void __lambda22_ (Block1Data* _data1_, SoupSession* sess, SoupMessage* mess);
static void ___lambda22__soup_session_callback (SoupSession* session, SoupMessage* msg, gpointer self);
static void games_grilo_cover_finalize (GObject * obj);


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


static void _games_grilo_cover_on_media_resolved_games_grilo_media_resolved (GamesGriloMedia* _sender, gpointer self) {
	games_grilo_cover_on_media_resolved ((GamesGriloCover*) self);
}


GamesGriloCover* games_grilo_cover_construct (GType object_type, GamesGriloMedia* media, GamesUid* uid) {
	GamesGriloCover * self = NULL;
	GamesGriloMedia* _tmp0_;
	GamesGriloMedia* _tmp1_;
	GamesUid* _tmp2_;
	GamesUid* _tmp3_;
	GamesGriloMedia* _tmp4_;
	g_return_val_if_fail (media != NULL, NULL);
	g_return_val_if_fail (uid != NULL, NULL);
	self = (GamesGriloCover*) g_object_new (object_type, NULL);
	_tmp0_ = media;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->media);
	self->priv->media = _tmp1_;
	_tmp2_ = uid;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	_g_object_unref0 (self->priv->uid);
	self->priv->uid = _tmp3_;
	_tmp4_ = media;
	g_signal_connect_object (_tmp4_, "resolved", (GCallback) _games_grilo_cover_on_media_resolved_games_grilo_media_resolved, self, 0);
	self->priv->resolving = FALSE;
	return self;
}


GamesGriloCover* games_grilo_cover_new (GamesGriloMedia* media, GamesUid* uid) {
	return games_grilo_cover_construct (GAMES_TYPE_GRILO_COVER, media, uid);
}


static GIcon* games_grilo_cover_real_get_cover (GamesCover* base) {
	GamesGriloCover * self;
	GIcon* result = NULL;
	gboolean _tmp0_;
	GIcon* _tmp3_;
	GIcon* _tmp10_;
	GamesGriloMedia* _tmp13_;
	GIcon* _tmp14_;
	GIcon* _tmp15_;
	GError * _inner_error_ = NULL;
	self = (GamesGriloCover*) base;
	_tmp0_ = self->priv->resolving;
	if (_tmp0_) {
		GIcon* _tmp1_;
		GIcon* _tmp2_;
		_tmp1_ = self->priv->icon;
		_tmp2_ = _g_object_ref0 (_tmp1_);
		result = _tmp2_;
		return result;
	}
	_tmp3_ = self->priv->icon;
	if (_tmp3_ != NULL) {
		GIcon* _tmp4_;
		GIcon* _tmp5_;
		_tmp4_ = self->priv->icon;
		_tmp5_ = _g_object_ref0 (_tmp4_);
		result = _tmp5_;
		return result;
	}
	{
		games_grilo_cover_load_cover (self, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch9_g_error;
		}
	}
	goto __finally9;
	__catch9_g_error:
	{
		GError* e = NULL;
		GError* _tmp6_;
		const gchar* _tmp7_;
		GIcon* _tmp8_;
		GIcon* _tmp9_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp6_ = e;
		_tmp7_ = _tmp6_->message;
		g_warning ("grilo-cover.vala:28: %s", _tmp7_);
		_tmp8_ = self->priv->icon;
		_tmp9_ = _g_object_ref0 (_tmp8_);
		result = _tmp9_;
		_g_error_free0 (e);
		return result;
	}
	__finally9:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		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 NULL;
	}
	_tmp10_ = self->priv->icon;
	if (_tmp10_ != NULL) {
		GIcon* _tmp11_;
		GIcon* _tmp12_;
		_tmp11_ = self->priv->icon;
		_tmp12_ = _g_object_ref0 (_tmp11_);
		result = _tmp12_;
		return result;
	}
	self->priv->resolving = TRUE;
	_tmp13_ = self->priv->media;
	games_grilo_media_try_resolve_media (_tmp13_);
	_tmp14_ = self->priv->icon;
	_tmp15_ = _g_object_ref0 (_tmp14_);
	result = _tmp15_;
	return result;
}


static void games_grilo_cover_on_media_resolved (GamesGriloCover* self) {
	GrlMedia* grl_media = NULL;
	GamesGriloMedia* _tmp0_;
	GrlMedia* _tmp1_;
	GrlMedia* _tmp2_;
	GrlMedia* _tmp3_;
	GrlKeyID _tmp4_;
	guint _tmp5_;
	gchar* uri = NULL;
	GrlMedia* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->media;
	_tmp1_ = games_grilo_media_get_media (_tmp0_);
	grl_media = _tmp1_;
	_tmp2_ = grl_media;
	if (_tmp2_ == NULL) {
		_g_object_unref0 (grl_media);
		return;
	}
	_tmp3_ = grl_media;
	_tmp4_ = GRL_METADATA_KEY_THUMBNAIL;
	_tmp5_ = grl_data_length ((GrlData*) _tmp3_, _tmp4_);
	if (_tmp5_ == ((guint) 0)) {
		_g_object_unref0 (grl_media);
		return;
	}
	_tmp6_ = grl_media;
	_tmp7_ = grl_media_get_thumbnail_nth (_tmp6_, (guint) 0);
	_tmp8_ = g_strdup (_tmp7_);
	uri = _tmp8_;
	_tmp9_ = uri;
	games_grilo_cover_try_fetch_cover (self, _tmp9_, NULL, NULL);
	_g_free0 (uri);
	_g_object_unref0 (grl_media);
}


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


static gchar* games_grilo_cover_get_cover_path (GamesGriloCover* self, GError** error) {
	gchar* result = NULL;
	const gchar* _tmp0_;
	gchar* dir = NULL;
	gchar* _tmp3_;
	gchar* uid = NULL;
	GamesUid* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	const gchar* _tmp11_;
	gchar* _tmp12_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->cover_path;
	if (_tmp0_ != NULL) {
		const gchar* _tmp1_;
		gchar* _tmp2_;
		_tmp1_ = self->priv->cover_path;
		_tmp2_ = g_strdup (_tmp1_);
		result = _tmp2_;
		return result;
	}
	_tmp3_ = games_application_get_covers_dir ();
	dir = _tmp3_;
	_tmp4_ = self->priv->uid;
	_tmp5_ = games_uid_get_uid (_tmp4_, &_inner_error_);
	uid = _tmp5_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (dir);
		return NULL;
	}
	_tmp6_ = dir;
	_tmp7_ = string_to_string (_tmp6_);
	_tmp8_ = uid;
	_tmp9_ = string_to_string (_tmp8_);
	_tmp10_ = g_strconcat (_tmp7_, "/", _tmp9_, ".cover", NULL);
	_g_free0 (self->priv->cover_path);
	self->priv->cover_path = _tmp10_;
	_tmp11_ = self->priv->cover_path;
	_tmp12_ = g_strdup (_tmp11_);
	result = _tmp12_;
	_g_free0 (uid);
	_g_free0 (dir);
	return result;
}


static void games_grilo_cover_try_fetch_cover_data_free (gpointer _data) {
	GamesGriloCoverTryFetchCoverData* _data_;
	_data_ = _data;
	_g_free0 (_data_->uri);
	_g_object_unref0 (_data_->self);
	g_slice_free (GamesGriloCoverTryFetchCoverData, _data_);
}


static void games_grilo_cover_try_fetch_cover_async_ready_wrapper (GObject *source_object, GAsyncResult *res, void *user_data) {
	GamesGriloCoverTryFetchCoverData* _task_data_;
	_task_data_ = g_task_get_task_data (G_TASK (res));
	if (_task_data_->_callback_ != NULL) {
		_task_data_->_callback_ (source_object, res, user_data);
	}
	_task_data_->_task_complete_ = TRUE;
}


static void games_grilo_cover_try_fetch_cover (GamesGriloCover* self, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	GamesGriloCoverTryFetchCoverData* _data_;
	GamesGriloCover* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	_data_ = g_slice_new0 (GamesGriloCoverTryFetchCoverData);
	_data_->_callback_ = _callback_;
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, games_grilo_cover_try_fetch_cover_async_ready_wrapper, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, games_grilo_cover_try_fetch_cover_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = uri;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (_data_->uri);
	_data_->uri = _tmp2_;
	games_grilo_cover_try_fetch_cover_co (_data_);
}


static void games_grilo_cover_try_fetch_cover_finish (GamesGriloCover* self, GAsyncResult* _res_) {
	GamesGriloCoverTryFetchCoverData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}


static void games_grilo_cover_try_fetch_cover_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	GamesGriloCoverTryFetchCoverData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	_data_->_task_complete_ = TRUE;
	games_grilo_cover_try_fetch_cover_co (_data_);
}


static gboolean games_grilo_cover_try_fetch_cover_co (GamesGriloCoverTryFetchCoverData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp0_ = _data_->uri;
		_data_->_state_ = 1;
		games_grilo_cover_fetch_cover (_data_->self, _data_->_tmp0_, games_grilo_cover_try_fetch_cover_ready, _data_);
		return FALSE;
		_state_1:
		games_grilo_cover_fetch_cover_finish (_data_->self, _data_->_res_, &_data_->_inner_error_);
		if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
			goto __catch10_g_error;
		}
	}
	goto __finally10;
	__catch10_g_error:
	{
		_data_->e = _data_->_inner_error_;
		_data_->_inner_error_ = NULL;
		_data_->_tmp1_ = _data_->e;
		_data_->_tmp2_ = _data_->_tmp1_->message;
		g_warning ("grilo-cover.vala:72: %s", _data_->_tmp2_);
		_g_error_free0 (_data_->e);
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (_data_->_task_complete_ != TRUE) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	__finally10:
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
		g_clear_error (&_data_->_inner_error_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void games_grilo_cover_fetch_cover_data_free (gpointer _data) {
	GamesGriloCoverFetchCoverData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (GamesGriloCoverFetchCoverData, _data_);
}


static void games_grilo_cover_fetch_cover_async_ready_wrapper (GObject *source_object, GAsyncResult *res, void *user_data) {
	GamesGriloCoverFetchCoverData* _task_data_;
	_task_data_ = g_task_get_task_data (G_TASK (res));
	if (_task_data_->_callback_ != NULL) {
		_task_data_->_callback_ (source_object, res, user_data);
	}
	_task_data_->_task_complete_ = TRUE;
}


static void games_grilo_cover_fetch_cover (GamesGriloCover* self, const gchar* uri, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	GamesGriloCoverFetchCoverData* _data_;
	GamesGriloCover* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	_data_ = g_slice_new0 (GamesGriloCoverFetchCoverData);
	_data_->_callback_ = _callback_;
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, games_grilo_cover_fetch_cover_async_ready_wrapper, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, games_grilo_cover_fetch_cover_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = uri;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (_data_->uri);
	_data_->uri = _tmp2_;
	games_grilo_cover_fetch_cover_co (_data_);
}


static void games_grilo_cover_fetch_cover_finish (GamesGriloCover* self, GAsyncResult* _res_, GError** error) {
	GamesGriloCoverFetchCoverData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}


static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		GamesGriloCover* self;
		self = _data1_->self;
		_g_free0 (_data1_->cover_path);
		_g_free0 (_data1_->uri);
		_g_object_unref0 (self);
		g_slice_free (Block1Data, _data1_);
	}
}


static void __lambda22_ (Block1Data* _data1_, SoupSession* sess, SoupMessage* mess) {
	GamesGriloCover* self;
	SoupMessage* _tmp0_;
	guint _tmp1_;
	guint _tmp2_;
	GError * _inner_error_ = NULL;
	self = _data1_->self;
	g_return_if_fail (sess != NULL);
	g_return_if_fail (mess != NULL);
	_tmp0_ = mess;
	g_object_get (_tmp0_, "status-code", &_tmp1_, NULL);
	_tmp2_ = _tmp1_;
	if (_tmp2_ != ((guint) SOUP_STATUS_OK)) {
		const gchar* _tmp3_;
		SoupMessage* _tmp4_;
		guint _tmp5_;
		guint _tmp6_;
		SoupMessage* _tmp7_;
		gchar* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = _data1_->uri;
		_tmp4_ = mess;
		g_object_get (_tmp4_, "status-code", &_tmp5_, NULL);
		_tmp6_ = _tmp5_;
		_tmp7_ = mess;
		g_object_get (_tmp7_, "reason-phrase", &_tmp8_, NULL);
		_tmp9_ = _tmp8_;
		_tmp10_ = _tmp9_;
		g_debug ("grilo-cover.vala:89: Failed to load %s: %u %s.", _tmp3_, _tmp6_, _tmp10_);
		_g_free0 (_tmp10_);
		return;
	}
	{
		SoupMessage* _tmp11_;
		SoupMessageBody* _tmp12_;
		guint8* _tmp13_;
		gint _tmp13__length1;
		_tmp11_ = mess;
		_tmp12_ = _tmp11_->response_body;
		_tmp13_ = _tmp12_->data;
		_tmp13__length1 = (gint) _tmp12_->length;
		g_file_set_contents (_data1_->cover_path, (const char*) _tmp13_, (size_t) _tmp13__length1, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch11_g_error;
		}
		games_grilo_cover_load_cover (self, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch11_g_error;
		}
	}
	goto __finally11;
	__catch11_g_error:
	{
		GError* e = NULL;
		GError* _tmp14_;
		const gchar* _tmp15_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp14_ = e;
		_tmp15_ = _tmp14_->message;
		g_warning ("grilo-cover.vala:98: %s", _tmp15_);
		_g_error_free0 (e);
	}
	__finally11:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		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;
	}
}


static void ___lambda22__soup_session_callback (SoupSession* session, SoupMessage* msg, gpointer self) {
	__lambda22_ (self, session, msg);
	block1_data_unref (self);
}


static gboolean games_grilo_cover_fetch_cover_co (GamesGriloCoverFetchCoverData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data1_ = g_slice_new0 (Block1Data);
	_data_->_data1_->_ref_count_ = 1;
	_data_->_data1_->self = g_object_ref (_data_->self);
	_data_->_tmp0_ = _data_->uri;
	_g_free0 (_data_->_data1_->uri);
	_data_->_data1_->uri = _data_->_tmp0_;
	_data_->_data1_->_async_data_ = _data_;
	_data_->_tmp1_ = games_application_get_covers_dir ();
	_data_->dir = _data_->_tmp1_;
	games_application_try_make_dir (_data_->dir);
	_data_->_tmp2_ = games_grilo_cover_get_cover_path (_data_->self, &_data_->_inner_error_);
	_data_->_data1_->cover_path = _data_->_tmp2_;
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_g_free0 (_data_->dir);
		block1_data_unref (_data_->_data1_);
		_data_->_data1_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp3_ = soup_session_new ();
	_data_->session = _data_->_tmp3_;
	_data_->_tmp4_ = _data_->_data1_->uri;
	_data_->_tmp5_ = soup_message_new ("GET", _data_->_tmp4_);
	_data_->message = _data_->_tmp5_;
	_data_->_tmp6_ = _data_->session;
	_data_->_tmp7_ = _data_->message;
	_data_->_tmp8_ = _g_object_ref0 (_data_->_tmp7_);
	soup_session_queue_message (_data_->_tmp6_, _data_->_tmp8_, ___lambda22__soup_session_callback, block1_data_ref (_data_->_data1_));
	_g_object_unref0 (_data_->message);
	_g_object_unref0 (_data_->session);
	_g_free0 (_data_->dir);
	block1_data_unref (_data_->_data1_);
	_data_->_data1_ = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void games_grilo_cover_load_cover (GamesGriloCover* self, GError** error) {
	gchar* cover_path = NULL;
	gchar* _tmp0_;
	gboolean _tmp1_;
	GFile* file = NULL;
	GFile* _tmp2_;
	GFile* _tmp3_;
	GFileIcon* _tmp4_;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = games_grilo_cover_get_cover_path (self, &_inner_error_);
	cover_path = _tmp0_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		return;
	}
	_tmp1_ = g_file_test (cover_path, G_FILE_TEST_EXISTS);
	if (!_tmp1_) {
		_g_free0 (cover_path);
		return;
	}
	_tmp2_ = g_file_new_for_path (cover_path);
	file = _tmp2_;
	_tmp3_ = file;
	_tmp4_ = (GFileIcon*) g_file_icon_new (_tmp3_);
	_g_object_unref0 (self->priv->icon);
	self->priv->icon = (GIcon*) _tmp4_;
	g_signal_emit_by_name ((GamesCover*) self, "changed");
	_g_object_unref0 (file);
	_g_free0 (cover_path);
}


static void games_grilo_cover_class_init (GamesGriloCoverClass * klass) {
	games_grilo_cover_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesGriloCoverPrivate));
	G_OBJECT_CLASS (klass)->finalize = games_grilo_cover_finalize;
}


static void games_grilo_cover_games_cover_interface_init (GamesCoverIface * iface) {
	games_grilo_cover_games_cover_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_cover = (GIcon* (*) (GamesCover *)) games_grilo_cover_real_get_cover;
}


static void games_grilo_cover_instance_init (GamesGriloCover * self) {
	self->priv = GAMES_GRILO_COVER_GET_PRIVATE (self);
}


static void games_grilo_cover_finalize (GObject * obj) {
	GamesGriloCover * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_GRILO_COVER, GamesGriloCover);
	_g_object_unref0 (self->priv->media);
	_g_object_unref0 (self->priv->uid);
	_g_object_unref0 (self->priv->icon);
	_g_free0 (self->priv->cover_path);
	G_OBJECT_CLASS (games_grilo_cover_parent_class)->finalize (obj);
}


GType games_grilo_cover_get_type (void) {
	static volatile gsize games_grilo_cover_type_id__volatile = 0;
	if (g_once_init_enter (&games_grilo_cover_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GamesGriloCoverClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_grilo_cover_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesGriloCover), 0, (GInstanceInitFunc) games_grilo_cover_instance_init, NULL };
		static const GInterfaceInfo games_cover_info = { (GInterfaceInitFunc) games_grilo_cover_games_cover_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType games_grilo_cover_type_id;
		games_grilo_cover_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesGriloCover", &g_define_type_info, 0);
		g_type_add_interface_static (games_grilo_cover_type_id, GAMES_TYPE_COVER, &games_cover_info);
		g_once_init_leave (&games_grilo_cover_type_id__volatile, games_grilo_cover_type_id);
	}
	return games_grilo_cover_type_id__volatile;
}



