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

/*-
 * Copyright (c) 2017-2018 elementary LLC. (https://elementary.io),
 *               2013 Julien Spautz <spautz.julien@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation, either version 3 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 warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com>
 */

#include "codecore.h"
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.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  {
	SCRATCH_FOLDER_MANAGER_FILE_ITEM_0_PROPERTY,
	SCRATCH_FOLDER_MANAGER_FILE_ITEM_NUM_PROPERTIES
};
static GParamSpec* scratch_folder_manager_file_item_properties[SCRATCH_FOLDER_MANAGER_FILE_ITEM_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#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_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))

static gpointer scratch_folder_manager_file_item_parent_class = NULL;

static void scratch_folder_manager_file_item_real_activated (CodeWidgetsSourceListItem* base);
static GMenu* scratch_folder_manager_file_item_real_get_context_menu (CodeWidgetsSourceListItem* base);
static GMenuItem* scratch_folder_manager_file_item_create_submenu_for_open_in (ScratchFolderManagerFileItem* self,
                                                                        const gchar* file_type);
static GVariant* _variant_new15 (gchar* value);
static GVariant* _variant_new16 (gchar* value);
static GType scratch_folder_manager_file_item_get_type_once (void);

ScratchFolderManagerFileItem*
scratch_folder_manager_file_item_construct (GType object_type,
                                            ScratchFolderManagerFile* file,
                                            ScratchFolderManagerFileView* view)
{
	ScratchFolderManagerFileItem * self = NULL;
	g_return_val_if_fail (file != NULL, NULL);
	g_return_val_if_fail (view != NULL, NULL);
	self = (ScratchFolderManagerFileItem*) g_object_new (object_type, "file", file, "view", view, NULL);
	return self;
}

ScratchFolderManagerFileItem*
scratch_folder_manager_file_item_new (ScratchFolderManagerFile* file,
                                      ScratchFolderManagerFileView* view)
{
	return scratch_folder_manager_file_item_construct (SCRATCH_FOLDER_MANAGER_TYPE_FILE_ITEM, file, view);
}

static void
scratch_folder_manager_file_item_real_activated (CodeWidgetsSourceListItem* base)
{
	ScratchFolderManagerFileItem * self;
	ScratchFolderManagerFileView* _tmp0_;
	ScratchFolderManagerFileView* _tmp1_;
	ScratchFolderManagerFile* _tmp2_;
	ScratchFolderManagerFile* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	self = (ScratchFolderManagerFileItem*) base;
	_tmp0_ = scratch_folder_manager_item_get_view ((ScratchFolderManagerItem*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp3_ = _tmp2_;
	_tmp4_ = scratch_folder_manager_file_get_path (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = _tmp5_;
	g_signal_emit_by_name (_tmp1_, "activate", _tmp6_);
	_g_free0 (_tmp6_);
}

static GMenu*
scratch_folder_manager_file_item_real_get_context_menu (CodeWidgetsSourceListItem* base)
{
	ScratchFolderManagerFileItem * self;
	GFileInfo* info = NULL;
	gchar* file_type = NULL;
	GFileInfo* _tmp9_;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	GMenu* contractor_items = NULL;
	ScratchFolderManagerFile* _tmp12_;
	ScratchFolderManagerFile* _tmp13_;
	GFile* _tmp14_;
	GFile* _tmp15_;
	GMenu* _tmp16_;
	GMenu* external_actions_section = NULL;
	GMenu* _tmp17_;
	GMenu* _tmp18_;
	const gchar* _tmp19_;
	GMenuItem* _tmp20_;
	GMenuItem* _tmp21_;
	GMenu* _tmp22_;
	GMenuItem* rename_item = NULL;
	ScratchFolderManagerFile* _tmp25_;
	ScratchFolderManagerFile* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	gchar* _tmp29_;
	GVariant* _tmp30_;
	GVariant* _tmp31_;
	gchar* _tmp32_;
	gchar* _tmp33_;
	GMenuItem* _tmp34_;
	GMenuItem* _tmp35_;
	GSimpleAction* rename_action = NULL;
	ScratchFolderManagerFileView* _tmp36_;
	ScratchFolderManagerFileView* _tmp37_;
	GSimpleActionGroup* _tmp38_;
	GSimpleActionGroup* _tmp39_;
	GSimpleAction* _tmp40_;
	ScratchFolderManagerFileView* _tmp41_;
	ScratchFolderManagerFileView* _tmp42_;
	ScratchFolderManagerFile* _tmp43_;
	ScratchFolderManagerFile* _tmp44_;
	gboolean _tmp45_ = FALSE;
	GMenuItem* delete_item = NULL;
	ScratchFolderManagerFile* _tmp46_;
	ScratchFolderManagerFile* _tmp47_;
	gchar* _tmp48_;
	gchar* _tmp49_;
	gchar* _tmp50_;
	GVariant* _tmp51_;
	GVariant* _tmp52_;
	gchar* _tmp53_;
	gchar* _tmp54_;
	GMenuItem* _tmp55_;
	GMenuItem* _tmp56_;
	GMenu* direct_actions_section = NULL;
	GMenu* _tmp57_;
	GMenu* menu_model = NULL;
	GMenu* _tmp58_;
	GMenu* _tmp59_;
	GError* _inner_error0_ = NULL;
	GMenu* result;
	self = (ScratchFolderManagerFileItem*) base;
	info = NULL;
	{
		GFileInfo* _tmp0_ = NULL;
		ScratchFolderManagerFile* _tmp1_;
		ScratchFolderManagerFile* _tmp2_;
		GFile* _tmp3_;
		GFile* _tmp4_;
		GFileInfo* _tmp5_;
		GFileInfo* _tmp6_;
		_tmp1_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
		_tmp2_ = _tmp1_;
		_tmp3_ = scratch_folder_manager_file_get_file (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_file_query_info (_tmp4_, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL, &_inner_error0_);
		_tmp0_ = _tmp5_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp6_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (info);
		info = _tmp6_;
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp7_;
		const gchar* _tmp8_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp7_ = e;
		_tmp8_ = _tmp7_->message;
		g_warning ("FileItem.vala:40: %s", _tmp8_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (info);
		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;
	}
	_tmp9_ = info;
	_tmp10_ = g_file_info_get_attribute_string (_tmp9_, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
	_tmp11_ = g_strdup (_tmp10_);
	file_type = _tmp11_;
	_tmp12_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp13_ = _tmp12_;
	_tmp14_ = scratch_folder_manager_file_get_file (_tmp13_);
	_tmp15_ = _tmp14_;
	_tmp16_ = scratch_utils_create_contract_items_for_file (_tmp15_);
	contractor_items = _tmp16_;
	_tmp17_ = g_menu_new ();
	external_actions_section = _tmp17_;
	_tmp18_ = external_actions_section;
	_tmp19_ = file_type;
	_tmp20_ = scratch_folder_manager_file_item_create_submenu_for_open_in (self, _tmp19_);
	_tmp21_ = _tmp20_;
	g_menu_append_item (_tmp18_, _tmp21_);
	_g_object_unref0 (_tmp21_);
	_tmp22_ = contractor_items;
	if (g_menu_model_get_n_items ((GMenuModel*) _tmp22_) > 0) {
		GMenu* _tmp23_;
		GMenu* _tmp24_;
		_tmp23_ = external_actions_section;
		_tmp24_ = contractor_items;
		g_menu_append_submenu (_tmp23_, _ ("Other Actions"), (GMenuModel*) _tmp24_);
	}
	_tmp25_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp26_ = _tmp25_;
	_tmp27_ = scratch_folder_manager_file_get_path (_tmp26_);
	_tmp28_ = _tmp27_;
	_tmp29_ = _tmp28_;
	_tmp30_ = g_variant_new_string (_tmp29_);
	g_variant_ref_sink (_tmp30_);
	_tmp31_ = _tmp30_;
	_tmp32_ = g_action_print_detailed_name (SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_PREFIX SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_RENAME_FILE, _tmp31_);
	_tmp33_ = _tmp32_;
	_tmp34_ = g_menu_item_new (_ ("Rename"), _tmp33_);
	_tmp35_ = _tmp34_;
	_g_free0 (_tmp33_);
	_g_variant_unref0 (_tmp31_);
	_g_free0 (_tmp29_);
	rename_item = _tmp35_;
	_tmp36_ = scratch_folder_manager_item_get_view ((ScratchFolderManagerItem*) self);
	_tmp37_ = _tmp36_;
	_tmp38_ = scratch_folder_manager_file_view_get_actions (_tmp37_);
	_tmp39_ = _tmp38_;
	_tmp40_ = scratch_utils_action_from_group (SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_RENAME_FILE, _tmp39_);
	rename_action = _tmp40_;
	_tmp41_ = scratch_folder_manager_item_get_view ((ScratchFolderManagerItem*) self);
	_tmp42_ = _tmp41_;
	_tmp43_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp44_ = _tmp43_;
	g_signal_emit_by_name (_tmp42_, "rename-request", _tmp44_, &_tmp45_);
	g_simple_action_set_enabled (rename_action, _tmp45_);
	_tmp46_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp47_ = _tmp46_;
	_tmp48_ = scratch_folder_manager_file_get_path (_tmp47_);
	_tmp49_ = _tmp48_;
	_tmp50_ = _tmp49_;
	_tmp51_ = g_variant_new_string (_tmp50_);
	g_variant_ref_sink (_tmp51_);
	_tmp52_ = _tmp51_;
	_tmp53_ = g_action_print_detailed_name (SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_PREFIX SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_DELETE, _tmp52_);
	_tmp54_ = _tmp53_;
	_tmp55_ = g_menu_item_new (_ ("Move to Trash"), _tmp54_);
	_tmp56_ = _tmp55_;
	_g_free0 (_tmp54_);
	_g_variant_unref0 (_tmp52_);
	_g_free0 (_tmp50_);
	delete_item = _tmp56_;
	_tmp57_ = g_menu_new ();
	direct_actions_section = _tmp57_;
	g_menu_append_item (direct_actions_section, rename_item);
	g_menu_append_item (direct_actions_section, delete_item);
	_tmp58_ = g_menu_new ();
	menu_model = _tmp58_;
	_tmp59_ = external_actions_section;
	g_menu_append_section (menu_model, NULL, (GMenuModel*) _tmp59_);
	g_menu_append_section (menu_model, NULL, (GMenuModel*) direct_actions_section);
	result = menu_model;
	_g_object_unref0 (direct_actions_section);
	_g_object_unref0 (delete_item);
	_g_object_unref0 (rename_action);
	_g_object_unref0 (rename_item);
	_g_object_unref0 (external_actions_section);
	_g_object_unref0 (contractor_items);
	_g_free0 (file_type);
	_g_object_unref0 (info);
	return result;
}

static GVariant*
_variant_new15 (gchar* value)
{
	return g_variant_ref_sink (g_variant_new_string (value));
}

static GVariant*
_variant_new16 (gchar* value)
{
	return g_variant_ref_sink (g_variant_new_string (value));
}

static GMenuItem*
scratch_folder_manager_file_item_create_submenu_for_open_in (ScratchFolderManagerFileItem* self,
                                                             const gchar* file_type)
{
	GMenuItem* new_window_menu_item = NULL;
	ScratchFolderManagerFile* _tmp0_;
	ScratchFolderManagerFile* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GVariant* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GMenuItem* _tmp8_;
	GMenuItem* _tmp9_;
	GMenu* top_section = NULL;
	GMenu* _tmp10_;
	ScratchFolderManagerFile* _tmp11_;
	ScratchFolderManagerFile* _tmp12_;
	gboolean _tmp13_;
	gboolean _tmp14_;
	GMenuItem* other_menu_item = NULL;
	ScratchFolderManagerFile* _tmp17_;
	ScratchFolderManagerFile* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	GVariant* _tmp22_;
	gchar* _tmp23_;
	gchar* _tmp24_;
	GMenuItem* _tmp25_;
	GMenuItem* _tmp26_;
	GMenu* extra_section = NULL;
	GMenu* _tmp27_;
	GMenu* open_in_menu = NULL;
	GMenu* _tmp28_;
	GMenu* _tmp29_;
	ScratchFolderManagerFile* _tmp30_;
	ScratchFolderManagerFile* _tmp31_;
	GFile* _tmp32_;
	GFile* _tmp33_;
	GMenu* _tmp34_;
	GMenu* _tmp35_;
	GMenuItem* open_in_menu_item = NULL;
	GMenuItem* _tmp36_;
	GMenuItem* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = scratch_folder_manager_file_get_path (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp5_ = _variant_new15 (_tmp4_);
	_tmp6_ = g_action_print_detailed_name (SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_OPEN_IN_NEW_WINDOW, _tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_menu_item_new (_ ("New Window"), _tmp7_);
	_tmp9_ = _tmp8_;
	_g_free0 (_tmp7_);
	_g_variant_unref0 (_tmp5_);
	_g_free0 (_tmp4_);
	new_window_menu_item = _tmp9_;
	_tmp10_ = g_menu_new ();
	top_section = _tmp10_;
	_tmp11_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp12_ = _tmp11_;
	_tmp13_ = scratch_folder_manager_file_get_is_valid_textfile (_tmp12_);
	_tmp14_ = _tmp13_;
	if (_tmp14_) {
		GMenu* _tmp15_;
		GMenuItem* _tmp16_;
		_tmp15_ = top_section;
		_tmp16_ = new_window_menu_item;
		g_menu_append_item (_tmp15_, _tmp16_);
	}
	_tmp17_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp18_ = _tmp17_;
	_tmp19_ = scratch_folder_manager_file_get_path (_tmp18_);
	_tmp20_ = _tmp19_;
	_tmp21_ = _tmp20_;
	_tmp22_ = _variant_new16 (_tmp21_);
	_tmp23_ = g_action_print_detailed_name (SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_PREFIX SCRATCH_FOLDER_MANAGER_FILE_VIEW_ACTION_SHOW_APP_CHOOSER, _tmp22_);
	_tmp24_ = _tmp23_;
	_tmp25_ = g_menu_item_new (_ ("Other Application…"), _tmp24_);
	_tmp26_ = _tmp25_;
	_g_free0 (_tmp24_);
	_g_variant_unref0 (_tmp22_);
	_g_free0 (_tmp21_);
	other_menu_item = _tmp26_;
	_tmp27_ = g_menu_new ();
	extra_section = _tmp27_;
	g_menu_append_item (extra_section, other_menu_item);
	_tmp28_ = g_menu_new ();
	open_in_menu = _tmp28_;
	_tmp29_ = top_section;
	g_menu_append_section (open_in_menu, NULL, (GMenuModel*) _tmp29_);
	_tmp30_ = scratch_folder_manager_item_get_file ((ScratchFolderManagerItem*) self);
	_tmp31_ = _tmp30_;
	_tmp32_ = scratch_folder_manager_file_get_file (_tmp31_);
	_tmp33_ = _tmp32_;
	_tmp34_ = scratch_utils_create_executable_app_items_for_file (_tmp33_, file_type);
	_tmp35_ = _tmp34_;
	g_menu_append_section (open_in_menu, NULL, (GMenuModel*) _tmp35_);
	_g_object_unref0 (_tmp35_);
	g_menu_append_section (open_in_menu, NULL, (GMenuModel*) extra_section);
	_tmp36_ = g_menu_item_new_submenu (_ ("Open In"), (GMenuModel*) open_in_menu);
	open_in_menu_item = _tmp36_;
	result = open_in_menu_item;
	_g_object_unref0 (open_in_menu);
	_g_object_unref0 (extra_section);
	_g_object_unref0 (other_menu_item);
	_g_object_unref0 (top_section);
	_g_object_unref0 (new_window_menu_item);
	return result;
}

static void
scratch_folder_manager_file_item_class_init (ScratchFolderManagerFileItemClass * klass,
                                             gpointer klass_data)
{
	scratch_folder_manager_file_item_parent_class = g_type_class_peek_parent (klass);
	((CodeWidgetsSourceListItemClass *) klass)->activated = (void (*) (CodeWidgetsSourceListItem*)) scratch_folder_manager_file_item_real_activated;
	((CodeWidgetsSourceListItemClass *) klass)->get_context_menu = (GMenu* (*) (CodeWidgetsSourceListItem*)) scratch_folder_manager_file_item_real_get_context_menu;
}

static void
scratch_folder_manager_file_item_instance_init (ScratchFolderManagerFileItem * self,
                                                gpointer klass)
{
}

/**
     * Normal item in the source list, represents a textfile.
     */
 G_GNUC_NO_INLINE static GType
scratch_folder_manager_file_item_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ScratchFolderManagerFileItemClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) scratch_folder_manager_file_item_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScratchFolderManagerFileItem), 0, (GInstanceInitFunc) scratch_folder_manager_file_item_instance_init, NULL };
	GType scratch_folder_manager_file_item_type_id;
	scratch_folder_manager_file_item_type_id = g_type_register_static (SCRATCH_FOLDER_MANAGER_TYPE_ITEM, "ScratchFolderManagerFileItem", &g_define_type_info, G_TYPE_FLAG_NONE);
	return scratch_folder_manager_file_item_type_id;
}

GType
scratch_folder_manager_file_item_get_type (void)
{
	static gsize scratch_folder_manager_file_item_type_id__once = 0;
	if (g_once_init_enter (&scratch_folder_manager_file_item_type_id__once)) {
		GType scratch_folder_manager_file_item_type_id;
		scratch_folder_manager_file_item_type_id = scratch_folder_manager_file_item_get_type_once ();
		g_once_init_leave (&scratch_folder_manager_file_item_type_id__once, scratch_folder_manager_file_item_type_id);
	}
	return scratch_folder_manager_file_item_type_id__once;
}

