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

/*
* Copyright (c) 2011-2014 Yorba Foundation
*               2016 elementary LLC (https://github.com/elementary/photos)
*
* 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 <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <gee.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

#define TYPE_BASIC_PROPERTIES (basic_properties_get_type ())
#define BASIC_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_BASIC_PROPERTIES, BasicProperties))
#define BASIC_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_BASIC_PROPERTIES, BasicPropertiesClass))
#define IS_BASIC_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_BASIC_PROPERTIES))
#define IS_BASIC_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_BASIC_PROPERTIES))
#define BASIC_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_BASIC_PROPERTIES, BasicPropertiesClass))

typedef struct _BasicProperties BasicProperties;
typedef struct _BasicPropertiesClass BasicPropertiesClass;
enum  {
	METADATA_VIEW_0_PROPERTY,
	METADATA_VIEW_NUM_PROPERTIES
};
static GParamSpec* metadata_view_properties[METADATA_VIEW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_LIBRARY_PROPERTIES (library_properties_get_type ())
#define LIBRARY_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_LIBRARY_PROPERTIES, LibraryProperties))
#define LIBRARY_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_LIBRARY_PROPERTIES, LibraryPropertiesClass))
#define IS_LIBRARY_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_LIBRARY_PROPERTIES))
#define IS_LIBRARY_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_LIBRARY_PROPERTIES))
#define LIBRARY_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_LIBRARY_PROPERTIES, LibraryPropertiesClass))

typedef struct _LibraryProperties LibraryProperties;
typedef struct _LibraryPropertiesClass LibraryPropertiesClass;

#define TYPE_EXTENDED_PROPERTIES (extended_properties_get_type ())
#define EXTENDED_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_EXTENDED_PROPERTIES, ExtendedProperties))
#define EXTENDED_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_EXTENDED_PROPERTIES, ExtendedPropertiesClass))
#define IS_EXTENDED_PROPERTIES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_EXTENDED_PROPERTIES))
#define IS_EXTENDED_PROPERTIES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_EXTENDED_PROPERTIES))
#define EXTENDED_PROPERTIES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_EXTENDED_PROPERTIES, ExtendedPropertiesClass))

typedef struct _ExtendedProperties ExtendedProperties;
typedef struct _ExtendedPropertiesClass ExtendedPropertiesClass;
#define _data_collection_unref0(var) ((var == NULL) ? NULL : (var = (data_collection_unref (var), NULL)))

struct _MetadataViewPrivate {
	GList* properties_collection;
	GtkGrid* grid;
	GtkLabel* no_items_label;
	GtkStack* stack;
	gint line_count;
	BasicProperties* collection_page_properties;
};

static gint MetadataView_private_offset;
static gpointer metadata_view_parent_class = NULL;

VALA_EXTERN GType basic_properties_get_type (void) G_GNUC_CONST ;
static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
VALA_EXTERN BasicProperties* basic_properties_new (void);
VALA_EXTERN BasicProperties* basic_properties_construct (GType object_type);
VALA_EXTERN LibraryProperties* library_properties_new (void);
VALA_EXTERN LibraryProperties* library_properties_construct (GType object_type);
VALA_EXTERN GType library_properties_get_type (void) G_GNUC_CONST ;
VALA_EXTERN ExtendedProperties* extended_properties_new (void);
VALA_EXTERN ExtendedProperties* extended_properties_construct (GType object_type);
VALA_EXTERN GType extended_properties_get_type (void) G_GNUC_CONST ;
static void metadata_view_finalize (GObject * obj);
static GType metadata_view_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
metadata_view_get_instance_private (MetadataView* self)
{
	return G_STRUCT_MEMBER_P (self, MetadataView_private_offset);
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static inline void
_g_list_free__g_object_unref0_ (GList* self)
{
	g_list_free_full (self, (GDestroyNotify) _g_object_unref0_);
}

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

MetadataView*
metadata_view_construct (GType object_type)
{
	MetadataView * self = NULL;
	GtkGrid* _tmp0_;
	GtkGrid* _tmp1_;
	GtkGrid* _tmp2_;
	GtkGrid* _tmp3_;
	BasicProperties* _tmp4_;
	LibraryProperties* _tmp5_;
	ExtendedProperties* _tmp6_;
	GList* _tmp7_;
	BasicProperties* _tmp12_;
	BasicProperties* _tmp13_;
	GtkLabel* _tmp14_;
	GtkStack* _tmp15_;
	GtkStack* _tmp16_;
	GtkGrid* _tmp17_;
	GtkStack* _tmp18_;
	BasicProperties* _tmp19_;
	GtkStack* _tmp20_;
	GtkLabel* _tmp21_;
	GtkStack* _tmp22_;
	self = (MetadataView*) g_object_new (object_type, NULL);
	g_object_set ((GtkScrolledWindow*) self, "hscrollbar-policy", GTK_POLICY_NEVER, NULL);
	_tmp0_ = (GtkGrid*) gtk_grid_new ();
	g_object_ref_sink (_tmp0_);
	_g_object_unref0 (self->priv->grid);
	self->priv->grid = _tmp0_;
	_tmp1_ = self->priv->grid;
	gtk_widget_set_hexpand ((GtkWidget*) _tmp1_, TRUE);
	_tmp2_ = self->priv->grid;
	gtk_grid_set_row_spacing (_tmp2_, 12);
	_tmp3_ = self->priv->grid;
	g_object_set ((GtkWidget*) _tmp3_, "margin", 12, NULL);
	_tmp4_ = basic_properties_new ();
	g_object_ref_sink (_tmp4_);
	self->priv->properties_collection = g_list_append (self->priv->properties_collection, (Properties*) _tmp4_);
	_tmp5_ = library_properties_new ();
	g_object_ref_sink (_tmp5_);
	self->priv->properties_collection = g_list_append (self->priv->properties_collection, (Properties*) _tmp5_);
	_tmp6_ = extended_properties_new ();
	g_object_ref_sink (_tmp6_);
	self->priv->properties_collection = g_list_append (self->priv->properties_collection, (Properties*) _tmp6_);
	_tmp7_ = self->priv->properties_collection;
	{
		GList* properties_collection = NULL;
		GList* properties_it = NULL;
		properties_collection = _tmp7_;
		for (properties_it = properties_collection; properties_it != NULL; properties_it = properties_it->next) {
			Properties* _tmp8_;
			Properties* properties = NULL;
			_tmp8_ = _g_object_ref0 ((Properties*) properties_it->data);
			properties = _tmp8_;
			{
				GtkGrid* _tmp9_;
				Properties* _tmp10_;
				gint _tmp11_;
				_tmp9_ = self->priv->grid;
				_tmp10_ = properties;
				gtk_grid_attach (_tmp9_, (GtkWidget*) _tmp10_, 0, self->priv->line_count, 1, 1);
				_tmp11_ = self->priv->line_count;
				self->priv->line_count = _tmp11_ + 1;
				_g_object_unref0 (properties);
			}
		}
	}
	_tmp12_ = basic_properties_new ();
	g_object_ref_sink (_tmp12_);
	_g_object_unref0 (self->priv->collection_page_properties);
	self->priv->collection_page_properties = _tmp12_;
	_tmp13_ = self->priv->collection_page_properties;
	g_object_set ((GtkWidget*) _tmp13_, "margin", 12, NULL);
	_tmp14_ = (GtkLabel*) gtk_label_new (_ ("No items selected"));
	g_object_ref_sink (_tmp14_);
	_g_object_unref0 (self->priv->no_items_label);
	self->priv->no_items_label = _tmp14_;
	_tmp15_ = (GtkStack*) gtk_stack_new ();
	g_object_ref_sink (_tmp15_);
	_g_object_unref0 (self->priv->stack);
	self->priv->stack = _tmp15_;
	_tmp16_ = self->priv->stack;
	_tmp17_ = self->priv->grid;
	gtk_container_add ((GtkContainer*) _tmp16_, (GtkWidget*) _tmp17_);
	_tmp18_ = self->priv->stack;
	_tmp19_ = self->priv->collection_page_properties;
	gtk_container_add ((GtkContainer*) _tmp18_, (GtkWidget*) _tmp19_);
	_tmp20_ = self->priv->stack;
	_tmp21_ = self->priv->no_items_label;
	gtk_container_add ((GtkContainer*) _tmp20_, (GtkWidget*) _tmp21_);
	_tmp22_ = self->priv->stack;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp22_);
	return self;
}

MetadataView*
metadata_view_new (void)
{
	return metadata_view_construct (TYPE_METADATA_VIEW);
}

void
metadata_view_update_properties (MetadataView* self,
                                 Page* page)
{
	ViewCollection* view = NULL;
	ViewCollection* _tmp0_;
	gboolean display_single = FALSE;
	ViewCollection* _tmp1_;
	gint count = 0;
	ViewCollection* _tmp4_;
	GeeIterable* iter = NULL;
	gboolean _tmp10_ = FALSE;
	GeeIterable* _tmp11_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (page != NULL);
	_tmp0_ = page_get_view (page);
	view = _tmp0_;
	display_single = FALSE;
	_tmp1_ = view;
	if (_tmp1_ == NULL) {
		GtkStack* _tmp2_;
		GtkLabel* _tmp3_;
		_tmp2_ = self->priv->stack;
		_tmp3_ = self->priv->no_items_label;
		gtk_stack_set_visible_child (_tmp2_, (GtkWidget*) _tmp3_);
		metadata_view_save_changes (self);
		_data_collection_unref0 (view);
		return;
	}
	_tmp4_ = view;
	count = view_collection_get_selected_count (_tmp4_);
	iter = NULL;
	if (count != 0) {
		ViewCollection* _tmp5_;
		GeeList* _tmp6_;
		_tmp5_ = view;
		_tmp6_ = view_collection_get_selected (_tmp5_);
		_g_object_unref0 (iter);
		iter = (GeeIterable*) _tmp6_;
	} else {
		ViewCollection* _tmp7_;
		ViewCollection* _tmp8_;
		GeeCollection* _tmp9_;
		_tmp7_ = view;
		count = data_collection_get_count ((DataCollection*) _tmp7_);
		_tmp8_ = view;
		_tmp9_ = data_collection_get_all ((DataCollection*) _tmp8_);
		_g_object_unref0 (iter);
		iter = G_TYPE_CHECK_INSTANCE_CAST (_tmp9_, GEE_TYPE_ITERABLE, GeeIterable);
	}
	_tmp11_ = iter;
	if (_tmp11_ == NULL) {
		_tmp10_ = TRUE;
	} else {
		_tmp10_ = count == 0;
	}
	if (_tmp10_) {
		GtkStack* _tmp12_;
		GtkLabel* _tmp13_;
		_tmp12_ = self->priv->stack;
		_tmp13_ = self->priv->no_items_label;
		gtk_stack_set_visible_child (_tmp12_, (GtkWidget*) _tmp13_);
		metadata_view_save_changes (self);
		_g_object_unref0 (iter);
		_data_collection_unref0 (view);
		return;
	}
	if (count == 1) {
		{
			GeeIterator* _item_it = NULL;
			GeeIterable* _tmp14_;
			GeeIterator* _tmp15_;
			_tmp14_ = iter;
			_tmp15_ = gee_iterable_iterator (_tmp14_);
			_item_it = _tmp15_;
			while (TRUE) {
				GeeIterator* _tmp16_;
				DataView* item = NULL;
				GeeIterator* _tmp17_;
				gpointer _tmp18_;
				MediaSource* source = NULL;
				DataView* _tmp19_;
				DataSource* _tmp20_;
				DataSource* _tmp21_;
				MediaSource* _tmp22_;
				MediaSource* _tmp23_;
				_tmp16_ = _item_it;
				if (!gee_iterator_next (_tmp16_)) {
					break;
				}
				_tmp17_ = _item_it;
				_tmp18_ = gee_iterator_get (_tmp17_);
				item = (DataView*) _tmp18_;
				_tmp19_ = item;
				_tmp20_ = data_view_get_source (_tmp19_);
				_tmp21_ = _tmp20_;
				_tmp22_ = _g_object_ref0 (IS_MEDIA_SOURCE (_tmp21_) ? ((MediaSource*) _tmp21_) : NULL);
				source = _tmp22_;
				_tmp23_ = source;
				if (_tmp23_ == NULL) {
					display_single = TRUE;
				}
				_g_object_unref0 (source);
				_g_object_unref0 (item);
				break;
			}
			_g_object_unref0 (_item_it);
		}
	} else {
		display_single = TRUE;
	}
	if (display_single) {
		BasicProperties* _tmp24_;
		GtkStack* _tmp25_;
		BasicProperties* _tmp26_;
		metadata_view_save_changes (self);
		_tmp24_ = self->priv->collection_page_properties;
		properties_update_properties ((Properties*) _tmp24_, page);
		_tmp25_ = self->priv->stack;
		_tmp26_ = self->priv->collection_page_properties;
		gtk_stack_set_visible_child (_tmp25_, (GtkWidget*) _tmp26_);
	} else {
		GList* _tmp27_;
		GtkStack* _tmp30_;
		GtkGrid* _tmp31_;
		_tmp27_ = self->priv->properties_collection;
		{
			GList* properties_collection = NULL;
			GList* properties_it = NULL;
			properties_collection = _tmp27_;
			for (properties_it = properties_collection; properties_it != NULL; properties_it = properties_it->next) {
				Properties* _tmp28_;
				Properties* properties = NULL;
				_tmp28_ = _g_object_ref0 ((Properties*) properties_it->data);
				properties = _tmp28_;
				{
					Properties* _tmp29_;
					_tmp29_ = properties;
					properties_update_properties (_tmp29_, page);
					_g_object_unref0 (properties);
				}
			}
		}
		_tmp30_ = self->priv->stack;
		_tmp31_ = self->priv->grid;
		gtk_stack_set_visible_child (_tmp30_, (GtkWidget*) _tmp31_);
	}
	_g_object_unref0 (iter);
	_data_collection_unref0 (view);
}

void
metadata_view_save_changes (MetadataView* self)
{
	GList* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->properties_collection;
	{
		GList* properties_collection = NULL;
		GList* properties_it = NULL;
		properties_collection = _tmp0_;
		for (properties_it = properties_collection; properties_it != NULL; properties_it = properties_it->next) {
			Properties* _tmp1_;
			Properties* properties = NULL;
			_tmp1_ = _g_object_ref0 ((Properties*) properties_it->data);
			properties = _tmp1_;
			{
				Properties* _tmp2_;
				_tmp2_ = properties;
				properties_save_changes_to_source (_tmp2_);
				_g_object_unref0 (properties);
			}
		}
	}
}

static void
metadata_view_class_init (MetadataViewClass * klass,
                          gpointer klass_data)
{
	metadata_view_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &MetadataView_private_offset);
	G_OBJECT_CLASS (klass)->finalize = metadata_view_finalize;
}

static void
metadata_view_instance_init (MetadataView * self,
                             gpointer klass)
{
	self->priv = metadata_view_get_instance_private (self);
	self->priv->properties_collection = NULL;
	self->priv->line_count = 0;
}

static void
metadata_view_finalize (GObject * obj)
{
	MetadataView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_METADATA_VIEW, MetadataView);
	(self->priv->properties_collection == NULL) ? NULL : (self->priv->properties_collection = (_g_list_free__g_object_unref0_ (self->priv->properties_collection), NULL));
	_g_object_unref0 (self->priv->grid);
	_g_object_unref0 (self->priv->no_items_label);
	_g_object_unref0 (self->priv->stack);
	_g_object_unref0 (self->priv->collection_page_properties);
	G_OBJECT_CLASS (metadata_view_parent_class)->finalize (obj);
}

static GType
metadata_view_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (MetadataViewClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) metadata_view_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MetadataView), 0, (GInstanceInitFunc) metadata_view_instance_init, NULL };
	GType metadata_view_type_id;
	metadata_view_type_id = g_type_register_static (gtk_scrolled_window_get_type (), "MetadataView", &g_define_type_info, 0);
	MetadataView_private_offset = g_type_add_instance_private (metadata_view_type_id, sizeof (MetadataViewPrivate));
	return metadata_view_type_id;
}

GType
metadata_view_get_type (void)
{
	static volatile gsize metadata_view_type_id__once = 0;
	if (g_once_init_enter (&metadata_view_type_id__once)) {
		GType metadata_view_type_id;
		metadata_view_type_id = metadata_view_get_type_once ();
		g_once_init_leave (&metadata_view_type_id__once, metadata_view_type_id);
	}
	return metadata_view_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;
}

