/* rygel-media-export-image-extractor.c generated by valac 0.44.9, the Vala compiler
 * generated from rygel-media-export-image-extractor.vala, do not modify */

/*
 * Copyright (C) 2016 Jens Georg <mail@jensge.org>
 *
 * 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 <gio/gio.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
#include <string.h>

#define RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR (rygel_media_export_extractor_get_type ())
#define RYGEL_MEDIA_EXPORT_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor))
#define RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractorClass))
#define RYGEL_MEDIA_EXPORT_IS_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_IS_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_EXTRACTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractorClass))

typedef struct _RygelMediaExportExtractor RygelMediaExportExtractor;
typedef struct _RygelMediaExportExtractorClass RygelMediaExportExtractorClass;
typedef struct _RygelMediaExportExtractorPrivate RygelMediaExportExtractorPrivate;

#define RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR (rygel_media_export_image_extractor_get_type ())
#define RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR, RygelMediaExportImageExtractor))
#define RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR, RygelMediaExportImageExtractorClass))
#define RYGEL_MEDIA_EXPORT_IS_IMAGE_EXTRACTOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_IS_IMAGE_EXTRACTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR))
#define RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR, RygelMediaExportImageExtractorClass))

typedef struct _RygelMediaExportImageExtractor RygelMediaExportImageExtractor;
typedef struct _RygelMediaExportImageExtractorClass RygelMediaExportImageExtractorClass;
typedef struct _RygelMediaExportImageExtractorPrivate RygelMediaExportImageExtractorPrivate;
enum  {
	RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR_0_PROPERTY,
	RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR_NUM_PROPERTIES
};
static GParamSpec* rygel_media_export_image_extractor_properties[RYGEL_MEDIA_EXPORT_IMAGE_EXTRACTOR_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define __vala_GdkPixbufFormat_free0(var) ((var == NULL) ? NULL : (var = (_vala_GdkPixbufFormat_free (var), NULL)))
typedef struct _RygelMediaExportImageExtractorRunData RygelMediaExportImageExtractorRunData;

struct _RygelMediaExportExtractor {
	GObject parent_instance;
	RygelMediaExportExtractorPrivate * priv;
	GVariantDict* serialized_info;
};

struct _RygelMediaExportExtractorClass {
	GObjectClass parent_class;
	void (*run) (RygelMediaExportExtractor* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*run_finish) (RygelMediaExportExtractor* self, GAsyncResult* _res_, GError** error);
};

struct _RygelMediaExportImageExtractor {
	RygelMediaExportExtractor parent_instance;
	RygelMediaExportImageExtractorPrivate * priv;
};

struct _RygelMediaExportImageExtractorClass {
	RygelMediaExportExtractorClass parent_class;
};

struct _RygelMediaExportImageExtractorRunData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	RygelMediaExportImageExtractor* self;
	gint width;
	gint height;
	GdkPixbufFormat* format;
	GFile* _tmp0_;
	GFile* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	GdkPixbufFormat* _tmp6_;
	GdkPixbufFormat* _tmp7_;
	GdkPixbufFormat* _tmp8_;
	gchar* mime;
	GdkPixbufFormat* _tmp9_;
	gchar** _tmp10_;
	gchar** _tmp11_;
	gchar** _tmp12_;
	gint _tmp12__length1;
	const gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	GVariantDict* _tmp16_;
	GVariantDict* _tmp17_;
	const gchar* _tmp18_;
	GVariantDict* _tmp19_;
	GVariantDict* _tmp20_;
	gchar* profile;
	const gchar* _tmp21_;
	gboolean _tmp22_;
	gchar* _tmp23_;
	gchar* _tmp24_;
	gboolean _tmp25_;
	gchar* _tmp26_;
	gboolean _tmp27_;
	gchar* _tmp28_;
	gboolean _tmp29_;
	gchar* _tmp30_;
	gchar* _tmp31_;
	GVariantDict* _tmp32_;
	const gchar* _tmp33_;
	GError* _inner_error0_;
};

static gpointer rygel_media_export_image_extractor_parent_class = NULL;

GType rygel_media_export_extractor_get_type (void) G_GNUC_CONST;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (RygelMediaExportExtractor, g_object_unref)
GType rygel_media_export_image_extractor_get_type (void) G_GNUC_CONST;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (RygelMediaExportImageExtractor, g_object_unref)
RygelMediaExportImageExtractor* rygel_media_export_image_extractor_new (GFile* file);
RygelMediaExportImageExtractor* rygel_media_export_image_extractor_construct (GType object_type,
                                                                              GFile* file);
static void rygel_media_export_image_extractor_real_run_data_free (gpointer _data);
static void rygel_media_export_image_extractor_real_run (RygelMediaExportExtractor* base,
                                                  GAsyncReadyCallback _callback_,
                                                  gpointer _user_data_);
static gboolean rygel_media_export_image_extractor_real_run_co (RygelMediaExportImageExtractorRunData* _data_);
void rygel_media_export_extractor_run (RygelMediaExportExtractor* self,
                                       GAsyncReadyCallback _callback_,
                                       gpointer _user_data_);
void rygel_media_export_extractor_run_finish (RygelMediaExportExtractor* self,
                                              GAsyncResult* _res_,
                                              GError** error);
static void rygel_media_export_image_extractor_run_ready (GObject* source_object,
                                                   GAsyncResult* _res_,
                                                   gpointer _user_data_);
GFile* rygel_media_export_extractor_get_file (RygelMediaExportExtractor* self);
static GdkPixbufFormat* _vala_GdkPixbufFormat_copy (GdkPixbufFormat* self);
#define RYGEL_MEDIA_EXPORT_SERIALIZER_UPNP_CLASS "UPnPClass"
#define UPNP_CLASS_PHOTO "object.item.imageItem.photo"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_MIME_TYPE "MimeType"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_VIDEO_WIDTH "VideoWidth"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_VIDEO_HEIGHT "VideoHeight"
#define RYGEL_MEDIA_EXPORT_SERIALIZER_DLNA_PROFILE "DLNAProfile"
static void _vala_GdkPixbufFormat_free (GdkPixbufFormat* self);
static void _vala_array_destroy (gpointer array,
                          gint array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gint array_length,
                       GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);

RygelMediaExportImageExtractor*
rygel_media_export_image_extractor_construct (GType object_type,
                                              GFile* file)
{
	RygelMediaExportImageExtractor * self = NULL;
	g_return_val_if_fail (file != NULL, NULL);
	self = (RygelMediaExportImageExtractor*) g_object_new (object_type, "file", file, NULL);
	return self;
}

RygelMediaExportImageExtractor*
rygel_media_export_image_extractor_new (GFile* file)
{
	return rygel_media_export_image_extractor_construct (RYGEL_MEDIA_EXPORT_TYPE_IMAGE_EXTRACTOR, file);
}

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

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

static void
rygel_media_export_image_extractor_real_run (RygelMediaExportExtractor* base,
                                             GAsyncReadyCallback _callback_,
                                             gpointer _user_data_)
{
	RygelMediaExportImageExtractor * self;
	RygelMediaExportImageExtractorRunData* _data_;
	RygelMediaExportImageExtractor* _tmp0_;
	self = (RygelMediaExportImageExtractor*) base;
	_data_ = g_slice_new0 (RygelMediaExportImageExtractorRunData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, rygel_media_export_image_extractor_real_run_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	rygel_media_export_image_extractor_real_run_co (_data_);
}

static void
rygel_media_export_image_extractor_run_finish (RygelMediaExportExtractor* base,
                                               GAsyncResult* _res_,
                                               GError** error)
{
	RygelMediaExportImageExtractorRunData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static void
rygel_media_export_image_extractor_run_ready (GObject* source_object,
                                              GAsyncResult* _res_,
                                              gpointer _user_data_)
{
	RygelMediaExportImageExtractorRunData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	rygel_media_export_image_extractor_real_run_co (_data_);
}

static GdkPixbufFormat*
_vala_GdkPixbufFormat_copy (GdkPixbufFormat* self)
{
	return g_boxed_copy (gdk_pixbuf_format_get_type (), self);
}

static gpointer
__vala_GdkPixbufFormat_copy0 (gpointer self)
{
	return self ? _vala_GdkPixbufFormat_copy (self) : NULL;
}

static void
_vala_GdkPixbufFormat_free (GdkPixbufFormat* self)
{
	g_boxed_free (gdk_pixbuf_format_get_type (), self);
}

static gboolean
rygel_media_export_image_extractor_real_run_co (RygelMediaExportImageExtractorRunData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_state_ = 1;
	RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS (rygel_media_export_image_extractor_parent_class)->run (G_TYPE_CHECK_INSTANCE_CAST (_data_->self, RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor), rygel_media_export_image_extractor_run_ready, _data_);
	return FALSE;
	_state_1:
	RYGEL_MEDIA_EXPORT_EXTRACTOR_CLASS (rygel_media_export_image_extractor_parent_class)->run_finish (G_TYPE_CHECK_INSTANCE_CAST (_data_->self, RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, RygelMediaExportExtractor), _data_->_res_, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp0_ = rygel_media_export_extractor_get_file ((RygelMediaExportExtractor*) _data_->self);
	_data_->_tmp1_ = _data_->_tmp0_;
	_data_->_tmp2_ = g_file_get_path (_data_->_tmp1_);
	_data_->_tmp3_ = _data_->_tmp2_;
	_data_->_tmp4_ = 0;
	_data_->_tmp5_ = 0;
	_data_->_state_ = 2;
	gdk_pixbuf_get_file_info_async (_data_->_tmp3_, NULL, rygel_media_export_image_extractor_run_ready, _data_);
	return FALSE;
	_state_2:
	_data_->_tmp6_ = gdk_pixbuf_get_file_info_finish (_data_->_res_, &_data_->_tmp4_, &_data_->_tmp5_, &_data_->_inner_error0_);
	_data_->width = _data_->_tmp4_;
	_data_->height = _data_->_tmp5_;
	_data_->_tmp7_ = __vala_GdkPixbufFormat_copy0 (_data_->_tmp6_);
	_data_->_tmp8_ = _data_->_tmp7_;
	_g_free0 (_data_->_tmp3_);
	_data_->format = _data_->_tmp8_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp9_ = _data_->format;
	_data_->_tmp11_ = _data_->_tmp10_ = gdk_pixbuf_format_get_mime_types (_data_->_tmp9_);
	_data_->_tmp12_ = _data_->_tmp11_;
	_data_->_tmp12__length1 = _vala_array_length (_data_->_tmp10_);
	_data_->_tmp13_ = _data_->_tmp12_[0];
	_data_->_tmp14_ = g_strdup (_data_->_tmp13_);
	_data_->_tmp15_ = _data_->_tmp14_;
	_data_->_tmp12_ = (_vala_array_free (_data_->_tmp12_, _data_->_tmp12__length1, (GDestroyNotify) g_free), NULL);
	_data_->mime = _data_->_tmp15_;
	_data_->_tmp16_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	g_variant_dict_insert (_data_->_tmp16_, RYGEL_MEDIA_EXPORT_SERIALIZER_UPNP_CLASS, "s", UPNP_CLASS_PHOTO, NULL);
	_data_->_tmp17_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	_data_->_tmp18_ = _data_->mime;
	g_variant_dict_insert (_data_->_tmp17_, RYGEL_MEDIA_EXPORT_SERIALIZER_MIME_TYPE, "s", _data_->_tmp18_, NULL);
	_data_->_tmp19_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	g_variant_dict_insert (_data_->_tmp19_, RYGEL_MEDIA_EXPORT_SERIALIZER_VIDEO_WIDTH, "i", _data_->width, NULL);
	_data_->_tmp20_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	g_variant_dict_insert (_data_->_tmp20_, RYGEL_MEDIA_EXPORT_SERIALIZER_VIDEO_HEIGHT, "i", _data_->height, NULL);
	_data_->profile = NULL;
	_data_->_tmp21_ = _data_->mime;
	if (g_strcmp0 (_data_->_tmp21_, "image/png") == 0) {
		if (_data_->width <= 4096) {
			_data_->_tmp22_ = _data_->height <= 4096;
		} else {
			_data_->_tmp22_ = FALSE;
		}
		if (_data_->_tmp22_) {
			_data_->_tmp23_ = g_strdup ("PNG_LRG");
			_g_free0 (_data_->profile);
			_data_->profile = _data_->_tmp23_;
		} else {
			_data_->_tmp24_ = g_strdup_printf ("PNG_RES_%d_%d", _data_->width, _data_->height);
			_g_free0 (_data_->profile);
			_data_->profile = _data_->_tmp24_;
		}
	} else {
		if (_data_->width <= 640) {
			_data_->_tmp25_ = _data_->height <= 480;
		} else {
			_data_->_tmp25_ = FALSE;
		}
		if (_data_->_tmp25_) {
			_data_->_tmp26_ = g_strdup ("JPEG_SM");
			_g_free0 (_data_->profile);
			_data_->profile = _data_->_tmp26_;
		} else {
			if (_data_->width <= 1024) {
				_data_->_tmp27_ = _data_->height <= 768;
			} else {
				_data_->_tmp27_ = FALSE;
			}
			if (_data_->_tmp27_) {
				_data_->_tmp28_ = g_strdup ("JPEG_MED");
				_g_free0 (_data_->profile);
				_data_->profile = _data_->_tmp28_;
			} else {
				if (_data_->width <= 4096) {
					_data_->_tmp29_ = _data_->height <= 4096;
				} else {
					_data_->_tmp29_ = FALSE;
				}
				if (_data_->_tmp29_) {
					_data_->_tmp30_ = g_strdup ("JPEG_LRG");
					_g_free0 (_data_->profile);
					_data_->profile = _data_->_tmp30_;
				} else {
					_data_->_tmp31_ = g_strdup_printf ("JPEG_RES_%d_%d", _data_->width, _data_->height);
					_g_free0 (_data_->profile);
					_data_->profile = _data_->_tmp31_;
				}
			}
		}
	}
	_data_->_tmp32_ = ((RygelMediaExportExtractor*) _data_->self)->serialized_info;
	_data_->_tmp33_ = _data_->profile;
	g_variant_dict_insert (_data_->_tmp32_, RYGEL_MEDIA_EXPORT_SERIALIZER_DLNA_PROFILE, "s", _data_->_tmp33_, NULL);
	_g_free0 (_data_->profile);
	_g_free0 (_data_->mime);
	__vala_GdkPixbufFormat_free0 (_data_->format);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
rygel_media_export_image_extractor_class_init (RygelMediaExportImageExtractorClass * klass,
                                               gpointer klass_data)
{
	rygel_media_export_image_extractor_parent_class = g_type_class_peek_parent (klass);
	((RygelMediaExportExtractorClass *) klass)->run = (void (*) (RygelMediaExportExtractor*, GAsyncReadyCallback, gpointer)) rygel_media_export_image_extractor_real_run;
	((RygelMediaExportExtractorClass *) klass)->run_finish = (void (*) (RygelMediaExportExtractor*, GAsyncResult*, GError**)) rygel_media_export_image_extractor_run_finish;
}

static void
rygel_media_export_image_extractor_instance_init (RygelMediaExportImageExtractor * self,
                                                  gpointer klass)
{
}

GType
rygel_media_export_image_extractor_get_type (void)
{
	static volatile gsize rygel_media_export_image_extractor_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_media_export_image_extractor_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelMediaExportImageExtractorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_media_export_image_extractor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelMediaExportImageExtractor), 0, (GInstanceInitFunc) rygel_media_export_image_extractor_instance_init, NULL };
		GType rygel_media_export_image_extractor_type_id;
		rygel_media_export_image_extractor_type_id = g_type_register_static (RYGEL_MEDIA_EXPORT_TYPE_EXTRACTOR, "RygelMediaExportImageExtractor", &g_define_type_info, 0);
		g_once_init_leave (&rygel_media_export_image_extractor_type_id__volatile, rygel_media_export_image_extractor_type_id);
	}
	return rygel_media_export_image_extractor_type_id__volatile;
}

static void
_vala_array_destroy (gpointer array,
                     gint array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gint array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gint
_vala_array_length (gpointer array)
{
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

