/* ContractMenuItem.c generated by valac 0.56.19, the Vala compiler
 * generated from ContractMenuItem.vala, do not modify */

/*
* Copyright (c) 2009-2013 Yorba Foundation
*
* This program 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.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#include "io.elementary.photos.h"
#include <granite.h>
#include <gee.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	CONTRACT_MENU_ITEM_0_PROPERTY,
	CONTRACT_MENU_ITEM_CONTRACT_PROPERTY,
	CONTRACT_MENU_ITEM_SOURCES_PROPERTY,
	CONTRACT_MENU_ITEM_NUM_PROPERTIES
};
static GParamSpec* contract_menu_item_properties[CONTRACT_MENU_ITEM_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))

struct _ContractMenuItemPrivate {
	GraniteServicesContract* _contract;
	GeeList* _sources;
};

static gint ContractMenuItem_private_offset;
static gpointer contract_menu_item_parent_class = NULL;

static void contract_menu_item_real_activate (GtkMenuItem* base);
static void _vala_array_add224 (GFile** * array,
                         gint* length,
                         gint* size,
                         GFile* value);
static void _vala_array_add225 (GFile** * array,
                         gint* length,
                         gint* size,
                         GFile* value);
static GObject * contract_menu_item_constructor (GType type,
                                          guint n_construct_properties,
                                          GObjectConstructParam * construct_properties);
static void contract_menu_item_finalize (GObject * obj);
static GType contract_menu_item_get_type_once (void);
static void _vala_contract_menu_item_get_property (GObject * object,
                                            guint property_id,
                                            GValue * value,
                                            GParamSpec * pspec);
static void _vala_contract_menu_item_set_property (GObject * object,
                                            guint property_id,
                                            const GValue * value,
                                            GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
contract_menu_item_get_instance_private (ContractMenuItem* self)
{
	return G_STRUCT_MEMBER_P (self, ContractMenuItem_private_offset);
}

ContractMenuItem*
contract_menu_item_construct (GType object_type,
                              GraniteServicesContract* contract,
                              GeeList* sources)
{
	ContractMenuItem * self = NULL;
	g_return_val_if_fail (contract != NULL, NULL);
	g_return_val_if_fail (sources != NULL, NULL);
	self = (ContractMenuItem*) g_object_new (object_type, "contract", contract, "sources", sources, NULL);
	return self;
}

ContractMenuItem*
contract_menu_item_new (GraniteServicesContract* contract,
                        GeeList* sources)
{
	return contract_menu_item_construct (TYPE_CONTRACT_MENU_ITEM, contract, sources);
}

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

static void
_vala_array_add224 (GFile** * array,
                    gint* length,
                    gint* size,
                    GFile* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (GFile*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add225 (GFile** * array,
                    gint* length,
                    gint* size,
                    GFile* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (GFile*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
contract_menu_item_real_activate (GtkMenuItem* base)
{
	ContractMenuItem * self;
	GError* _inner_error0_ = NULL;
	self = (ContractMenuItem*) base;
	{
		GFile** modified_files = NULL;
		gint modified_files_length1;
		gint _modified_files_size_;
		GraniteServicesContract* _tmp19_;
		GFile** _tmp20_;
		gint _tmp20__length1;
		modified_files = NULL;
		modified_files_length1 = 0;
		_modified_files_size_ = modified_files_length1;
		{
			GeeList* _source_list = NULL;
			GeeList* _tmp0_;
			gint _source_size = 0;
			GeeList* _tmp1_;
			gint _tmp2_;
			gint _tmp3_;
			gint _source_index = 0;
			_tmp0_ = self->priv->_sources;
			_source_list = _tmp0_;
			_tmp1_ = _source_list;
			_tmp2_ = gee_collection_get_size ((GeeCollection*) _tmp1_);
			_tmp3_ = _tmp2_;
			_source_size = _tmp3_;
			_source_index = -1;
			while (TRUE) {
				gint _tmp4_;
				gint _tmp5_;
				DataSource* source = NULL;
				GeeList* _tmp6_;
				gpointer _tmp7_;
				Photo* modified_file = NULL;
				DataSource* _tmp8_;
				Photo* _tmp9_;
				gboolean _tmp10_ = FALSE;
				Photo* _tmp11_;
				_source_index = _source_index + 1;
				_tmp4_ = _source_index;
				_tmp5_ = _source_size;
				if (!(_tmp4_ < _tmp5_)) {
					break;
				}
				_tmp6_ = _source_list;
				_tmp7_ = gee_list_get (_tmp6_, _source_index);
				source = (DataSource*) _tmp7_;
				_tmp8_ = source;
				_tmp9_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp8_, TYPE_PHOTO, Photo));
				modified_file = _tmp9_;
				_tmp11_ = modified_file;
				if (photo_get_file_format (_tmp11_) == PHOTO_FILE_FORMAT_RAW) {
					_tmp10_ = TRUE;
				} else {
					Photo* _tmp12_;
					_tmp12_ = modified_file;
					_tmp10_ = !photo_has_alterations (_tmp12_);
				}
				if (_tmp10_) {
					Photo* _tmp13_;
					GFile* _tmp14_;
					_tmp13_ = modified_file;
					_tmp14_ = media_source_get_file ((MediaSource*) _tmp13_);
					_vala_array_add224 (&modified_files, &modified_files_length1, &_modified_files_size_, _tmp14_);
				} else {
					GFile* _tmp15_ = NULL;
					Photo* _tmp16_;
					GFile* _tmp17_;
					GFile* _tmp18_;
					_tmp16_ = modified_file;
					_tmp17_ = photo_get_modified_file (_tmp16_, &_inner_error0_);
					_tmp15_ = _tmp17_;
					if (G_UNLIKELY (_inner_error0_ != NULL)) {
						_g_object_unref0 (modified_file);
						_g_object_unref0 (source);
						modified_files = (_vala_array_free (modified_files, modified_files_length1, (GDestroyNotify) g_object_unref), NULL);
						goto __catch0_g_error;
					}
					_tmp18_ = _tmp15_;
					_tmp15_ = NULL;
					_vala_array_add225 (&modified_files, &modified_files_length1, &_modified_files_size_, _tmp18_);
					_g_object_unref0 (_tmp15_);
				}
				_g_object_unref0 (modified_file);
				_g_object_unref0 (source);
			}
		}
		_tmp19_ = self->priv->_contract;
		_tmp20_ = modified_files;
		_tmp20__length1 = modified_files_length1;
		granite_services_contract_execute_with_files (_tmp19_, _tmp20_, (gint) _tmp20__length1, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			modified_files = (_vala_array_free (modified_files, modified_files_length1, (GDestroyNotify) g_object_unref), NULL);
			goto __catch0_g_error;
		}
		modified_files = (_vala_array_free (modified_files, modified_files_length1, (GDestroyNotify) g_object_unref), NULL);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		GError* _tmp21_;
		const gchar* _tmp22_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp21_ = err;
		_tmp22_ = _tmp21_->message;
		g_warning ("ContractMenuItem.vala:48: %s", _tmp22_);
		_g_error_free0 (err);
	}
	__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;
	}
}

GraniteServicesContract*
contract_menu_item_get_contract (ContractMenuItem* self)
{
	GraniteServicesContract* result;
	GraniteServicesContract* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_contract;
	result = _tmp0_;
	return result;
}

static void
contract_menu_item_set_contract (ContractMenuItem* self,
                                 GraniteServicesContract* value)
{
	GraniteServicesContract* old_value;
	g_return_if_fail (self != NULL);
	old_value = contract_menu_item_get_contract (self);
	if (old_value != value) {
		GraniteServicesContract* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_contract);
		self->priv->_contract = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, contract_menu_item_properties[CONTRACT_MENU_ITEM_CONTRACT_PROPERTY]);
	}
}

GeeList*
contract_menu_item_get_sources (ContractMenuItem* self)
{
	GeeList* result;
	GeeList* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_sources;
	result = _tmp0_;
	return result;
}

static void
contract_menu_item_set_sources (ContractMenuItem* self,
                                GeeList* value)
{
	GeeList* old_value;
	g_return_if_fail (self != NULL);
	old_value = contract_menu_item_get_sources (self);
	if (old_value != value) {
		GeeList* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_sources);
		self->priv->_sources = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, contract_menu_item_properties[CONTRACT_MENU_ITEM_SOURCES_PROPERTY]);
	}
}

static GObject *
contract_menu_item_constructor (GType type,
                                guint n_construct_properties,
                                GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ContractMenuItem * self;
	GraniteServicesContract* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	GraniteServicesContract* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	parent_class = G_OBJECT_CLASS (contract_menu_item_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_CONTRACT_MENU_ITEM, ContractMenuItem);
	_tmp0_ = self->priv->_contract;
	_tmp1_ = granite_services_contract_get_display_name (_tmp0_);
	_tmp2_ = _tmp1_;
	gtk_menu_item_set_label ((GtkMenuItem*) self, _tmp2_);
	_g_free0 (_tmp2_);
	_tmp3_ = self->priv->_contract;
	_tmp4_ = granite_services_contract_get_description (_tmp3_);
	_tmp5_ = _tmp4_;
	gtk_widget_set_tooltip_text ((GtkWidget*) self, _tmp5_);
	_g_free0 (_tmp5_);
	return obj;
}

static void
contract_menu_item_class_init (ContractMenuItemClass * klass,
                               gpointer klass_data)
{
	contract_menu_item_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContractMenuItem_private_offset);
	((GtkMenuItemClass *) klass)->activate = (void (*) (GtkMenuItem*)) contract_menu_item_real_activate;
	G_OBJECT_CLASS (klass)->get_property = _vala_contract_menu_item_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_contract_menu_item_set_property;
	G_OBJECT_CLASS (klass)->constructor = contract_menu_item_constructor;
	G_OBJECT_CLASS (klass)->finalize = contract_menu_item_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTRACT_MENU_ITEM_CONTRACT_PROPERTY, contract_menu_item_properties[CONTRACT_MENU_ITEM_CONTRACT_PROPERTY] = g_param_spec_object ("contract", "contract", "contract", GRANITE_SERVICES_TYPE_CONTRACT, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTRACT_MENU_ITEM_SOURCES_PROPERTY, contract_menu_item_properties[CONTRACT_MENU_ITEM_SOURCES_PROPERTY] = g_param_spec_object ("sources", "sources", "sources", GEE_TYPE_LIST, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}

static void
contract_menu_item_instance_init (ContractMenuItem * self,
                                  gpointer klass)
{
	self->priv = contract_menu_item_get_instance_private (self);
}

static void
contract_menu_item_finalize (GObject * obj)
{
	ContractMenuItem * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_CONTRACT_MENU_ITEM, ContractMenuItem);
	_g_object_unref0 (self->priv->_contract);
	_g_object_unref0 (self->priv->_sources);
	G_OBJECT_CLASS (contract_menu_item_parent_class)->finalize (obj);
}

static GType
contract_menu_item_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContractMenuItemClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contract_menu_item_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContractMenuItem), 0, (GInstanceInitFunc) contract_menu_item_instance_init, NULL };
	GType contract_menu_item_type_id;
	contract_menu_item_type_id = g_type_register_static (gtk_menu_item_get_type (), "ContractMenuItem", &g_define_type_info, 0);
	ContractMenuItem_private_offset = g_type_add_instance_private (contract_menu_item_type_id, sizeof (ContractMenuItemPrivate));
	return contract_menu_item_type_id;
}

GType
contract_menu_item_get_type (void)
{
	static volatile gsize contract_menu_item_type_id__once = 0;
	if (g_once_init_enter (&contract_menu_item_type_id__once)) {
		GType contract_menu_item_type_id;
		contract_menu_item_type_id = contract_menu_item_get_type_once ();
		g_once_init_leave (&contract_menu_item_type_id__once, contract_menu_item_type_id);
	}
	return contract_menu_item_type_id__once;
}

static void
_vala_contract_menu_item_get_property (GObject * object,
                                       guint property_id,
                                       GValue * value,
                                       GParamSpec * pspec)
{
	ContractMenuItem * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CONTRACT_MENU_ITEM, ContractMenuItem);
	switch (property_id) {
		case CONTRACT_MENU_ITEM_CONTRACT_PROPERTY:
		g_value_set_object (value, contract_menu_item_get_contract (self));
		break;
		case CONTRACT_MENU_ITEM_SOURCES_PROPERTY:
		g_value_set_object (value, contract_menu_item_get_sources (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_contract_menu_item_set_property (GObject * object,
                                       guint property_id,
                                       const GValue * value,
                                       GParamSpec * pspec)
{
	ContractMenuItem * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CONTRACT_MENU_ITEM, ContractMenuItem);
	switch (property_id) {
		case CONTRACT_MENU_ITEM_CONTRACT_PROPERTY:
		contract_menu_item_set_contract (self, g_value_get_object (value));
		break;
		case CONTRACT_MENU_ITEM_SOURCES_PROPERTY:
		contract_menu_item_set_sources (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize 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,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

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

