/* pkcs11-deleter.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from pkcs11-deleter.vala, do not modify */

/*
 * Seahorse
 *
 * Copyright (C) 2013 Red Hat Inc.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Author: Stef Walter <stefw@redhat.com>
 */

#include "seahorse-pkcs11.h"
#include "seahorse-common.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <gck/gck.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <p11-kit/pkcs11.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  {
	SEAHORSE_PKCS11_DELETER_0_PROPERTY,
	SEAHORSE_PKCS11_DELETER_NUM_PROPERTIES
};
static GParamSpec* seahorse_pkcs11_deleter_properties[SEAHORSE_PKCS11_DELETER_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)))
typedef struct _SeahorsePkcs11DeleterDeleteData SeahorsePkcs11DeleterDeleteData;

struct _SeahorsePkcs11DeleterDeleteData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	SeahorsePkcs11Deleter* self;
	GCancellable* cancellable;
	gboolean result;
	GList* objects;
	GList* _tmp0_;
	GList* _tmp1_;
	GList* _tmp2_;
	GList* object_collection;
	GList* object_it;
	GckObject* object;
	GckObject* _tmp3_;
	SeahorsePkcs11Token* token;
	GckObject* _tmp4_;
	SeahorsePkcs11Token* _tmp5_;
	SeahorsePkcs11Token* _tmp6_;
	SeahorsePkcs11Token* _tmp7_;
	GckObject* _tmp8_;
	GError* e;
	gboolean _tmp9_;
	GError* _tmp10_;
	GError* _tmp11_;
	GError* _tmp12_;
	GError* _tmp13_;
	GError* _inner_error0_;
};

static gpointer seahorse_pkcs11_deleter_parent_class = NULL;

static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
static GtkDialog* seahorse_pkcs11_deleter_real_create_confirm (SeahorseDeleter* base,
                                                        GtkWindow* parent);
static GList* seahorse_pkcs11_deleter_real_get_objects (SeahorseDeleter* base);
static gboolean seahorse_pkcs11_deleter_real_add_object (SeahorseDeleter* base,
                                                  GObject* obj);
static void seahorse_pkcs11_deleter_real_delete_data_free (gpointer _data);
static void seahorse_pkcs11_deleter_real_delete (SeahorseDeleter* base,
                                          GCancellable* cancellable,
                                          GAsyncReadyCallback _callback_,
                                          gpointer _user_data_);
static gboolean seahorse_pkcs11_deleter_real_delete_co (SeahorsePkcs11DeleterDeleteData* _data_);
static void seahorse_pkcs11_deleter_delete_ready (GObject* source_object,
                                           GAsyncResult* _res_,
                                           gpointer _user_data_);
static void seahorse_pkcs11_deleter_finalize (GObject * obj);
static GType seahorse_pkcs11_deleter_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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 GtkDialog*
seahorse_pkcs11_deleter_real_create_confirm (SeahorseDeleter* base,
                                             GtkWindow* parent)
{
	SeahorsePkcs11Deleter * self;
	guint num = 0U;
	GList* _tmp0_;
	GtkDialog* result;
	self = (SeahorsePkcs11Deleter*) base;
	_tmp0_ = self->objects;
	num = g_list_length (_tmp0_);
	if (num == ((guint) 1)) {
		gchar* label = NULL;
		GList* _tmp1_;
		gconstpointer _tmp2_;
		gchar* _tmp3_ = NULL;
		const gchar* _tmp4_;
		SeahorseDeleteDialog* _tmp5_;
		_tmp1_ = self->objects;
		_tmp2_ = _tmp1_->data;
		g_object_get ((GObject*) ((GckObject*) _tmp2_), "label", &_tmp3_, NULL);
		_g_free0 (label);
		label = _tmp3_;
		_tmp4_ = label;
		_tmp5_ = (SeahorseDeleteDialog*) seahorse_delete_dialog_new (parent, _ ("Are you sure you want to permanently delete %s?"), _tmp4_, NULL);
		g_object_ref_sink (_tmp5_);
		result = (GtkDialog*) _tmp5_;
		_g_free0 (label);
		return result;
	} else {
		SeahorseDeleteDialog* _tmp6_;
		_tmp6_ = (SeahorseDeleteDialog*) seahorse_delete_dialog_new (parent, ngettext ("Are you sure you want to permanently delete %d certificate?", "Are you sure you want to permanently delete %d certificates?", (gulong) num), num, NULL);
		g_object_ref_sink (_tmp6_);
		result = (GtkDialog*) _tmp6_;
		return result;
	}
}

SeahorsePkcs11Deleter*
seahorse_pkcs11_deleter_construct (GType object_type,
                                   GckObject* object)
{
	SeahorsePkcs11Deleter * self = NULL;
	g_return_val_if_fail (object != NULL, NULL);
	self = (SeahorsePkcs11Deleter*) seahorse_deleter_construct (object_type);
	if (!seahorse_deleter_add_object ((SeahorseDeleter*) self, (GObject*) object)) {
		g_assert_not_reached ();
	}
	return self;
}

SeahorsePkcs11Deleter*
seahorse_pkcs11_deleter_new (GckObject* object)
{
	return seahorse_pkcs11_deleter_construct (SEAHORSE_PKCS11_TYPE_DELETER, object);
}

static GList*
seahorse_pkcs11_deleter_real_get_objects (SeahorseDeleter* base)
{
	SeahorsePkcs11Deleter * self;
	GList* _tmp0_;
	GList* result;
	self = (SeahorsePkcs11Deleter*) base;
	_tmp0_ = self->objects;
	result = _tmp0_;
	return result;
}

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

static gboolean
seahorse_pkcs11_deleter_real_add_object (SeahorseDeleter* base,
                                         GObject* obj)
{
	SeahorsePkcs11Deleter * self;
	gboolean result;
	self = (SeahorsePkcs11Deleter*) base;
	g_return_val_if_fail (obj != NULL, FALSE);
	if (SEAHORSE_PKCS11_IS_CERTIFICATE (obj)) {
		GckObject* _tmp0_;
		_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (obj, gck_object_get_type (), GckObject));
		self->objects = g_list_append (self->objects, _tmp0_);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

static void
seahorse_pkcs11_deleter_real_delete_data_free (gpointer _data)
{
	SeahorsePkcs11DeleterDeleteData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->cancellable);
	_g_object_unref0 (_data_->self);
	g_slice_free (SeahorsePkcs11DeleterDeleteData, _data_);
}

static void
seahorse_pkcs11_deleter_real_delete (SeahorseDeleter* base,
                                     GCancellable* cancellable,
                                     GAsyncReadyCallback _callback_,
                                     gpointer _user_data_)
{
	SeahorsePkcs11Deleter * self;
	SeahorsePkcs11DeleterDeleteData* _data_;
	SeahorsePkcs11Deleter* _tmp0_;
	GCancellable* _tmp1_;
	self = (SeahorsePkcs11Deleter*) base;
	_data_ = g_slice_new0 (SeahorsePkcs11DeleterDeleteData);
	_data_->_async_result = g_task_new (G_OBJECT (self), cancellable, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, seahorse_pkcs11_deleter_real_delete_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp1_;
	seahorse_pkcs11_deleter_real_delete_co (_data_);
}

static gboolean
seahorse_pkcs11_deleter_delete_finish (SeahorseDeleter* base,
                                       GAsyncResult* _res_,
                                       GError** error)
{
	gboolean result;
	SeahorsePkcs11DeleterDeleteData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		gboolean _tmp0_ = FALSE;
		return _tmp0_;
	}
	result = _data_->result;
	return result;
}

static void
seahorse_pkcs11_deleter_delete_ready (GObject* source_object,
                                      GAsyncResult* _res_,
                                      gpointer _user_data_)
{
	SeahorsePkcs11DeleterDeleteData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	seahorse_pkcs11_deleter_real_delete_co (_data_);
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

static gboolean
seahorse_pkcs11_deleter_real_delete_co (SeahorsePkcs11DeleterDeleteData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = _data_->self->objects;
	_data_->_tmp1_ = g_list_copy (_data_->_tmp0_);
	_data_->objects = _data_->_tmp1_;
	_data_->_tmp2_ = _data_->objects;
	{
		_data_->object_collection = _data_->_tmp2_;
		for (_data_->object_it = _data_->object_collection; _data_->object_it != NULL; _data_->object_it = _data_->object_it->next) {
			_data_->object = (GckObject*) _data_->object_it->data;
			{
				{
					_data_->_tmp3_ = _data_->object;
					_data_->_state_ = 1;
					gck_object_destroy_async (_data_->_tmp3_, _data_->cancellable, seahorse_pkcs11_deleter_delete_ready, _data_);
					return FALSE;
					_state_1:
					gck_object_destroy_finish (_data_->_tmp3_, _data_->_res_, &_data_->_inner_error0_);
					if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
						goto __catch0_g_error;
					}
					_data_->_tmp4_ = _data_->object;
					_data_->_tmp5_ = NULL;
					g_object_get ((GObject*) _data_->_tmp4_, "place", &_data_->_tmp5_, NULL);
					_g_object_unref0 (_data_->token);
					_data_->token = _data_->_tmp5_;
					_data_->_tmp6_ = _data_->token;
					if (_data_->_tmp6_ != NULL) {
						_data_->_tmp7_ = _data_->token;
						_data_->_tmp8_ = _data_->object;
						seahorse_pkcs11_token_remove_object (_data_->_tmp7_, _data_->_tmp8_);
					}
					_g_object_unref0 (_data_->token);
				}
				goto __finally0;
				__catch0_g_error:
				{
					_data_->e = _data_->_inner_error0_;
					_data_->_inner_error0_ = NULL;
					_data_->_tmp10_ = _data_->e;
					if (_data_->_tmp10_->domain != gck_error_get_quark ()) {
						_data_->_tmp9_ = TRUE;
					} else {
						_data_->_tmp11_ = _data_->e;
						_data_->_tmp9_ = ((gulong) _data_->_tmp11_->code) != CKR_OBJECT_HANDLE_INVALID;
					}
					if (_data_->_tmp9_) {
						_data_->_tmp12_ = _data_->e;
						_data_->_tmp13_ = _g_error_copy0 (_data_->_tmp12_);
						_data_->_inner_error0_ = _data_->_tmp13_;
						_g_error_free0 (_data_->e);
						goto __finally0;
					}
					_g_error_free0 (_data_->e);
				}
				__finally0:
				if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
					g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
					(_data_->objects == NULL) ? NULL : (_data_->objects = (g_list_free (_data_->objects), NULL));
					g_object_unref (_data_->_async_result);
					return FALSE;
				}
			}
		}
	}
	_data_->result = TRUE;
	(_data_->objects == NULL) ? NULL : (_data_->objects = (g_list_free (_data_->objects), NULL));
	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
seahorse_pkcs11_deleter_class_init (SeahorsePkcs11DeleterClass * klass,
                                    gpointer klass_data)
{
	seahorse_pkcs11_deleter_parent_class = g_type_class_peek_parent (klass);
	((SeahorseDeleterClass *) klass)->create_confirm = (GtkDialog* (*) (SeahorseDeleter*, GtkWindow*)) seahorse_pkcs11_deleter_real_create_confirm;
	((SeahorseDeleterClass *) klass)->get_objects = (GList* (*) (SeahorseDeleter*)) seahorse_pkcs11_deleter_real_get_objects;
	((SeahorseDeleterClass *) klass)->add_object = (gboolean (*) (SeahorseDeleter*, GObject*)) seahorse_pkcs11_deleter_real_add_object;
	((SeahorseDeleterClass *) klass)->delete = (void (*) (SeahorseDeleter*, GCancellable*, GAsyncReadyCallback, gpointer)) seahorse_pkcs11_deleter_real_delete;
	((SeahorseDeleterClass *) klass)->delete_finish = (gboolean (*) (SeahorseDeleter*, GAsyncResult*, GError**)) seahorse_pkcs11_deleter_delete_finish;
	G_OBJECT_CLASS (klass)->finalize = seahorse_pkcs11_deleter_finalize;
}

static void
seahorse_pkcs11_deleter_instance_init (SeahorsePkcs11Deleter * self,
                                       gpointer klass)
{
}

static void
seahorse_pkcs11_deleter_finalize (GObject * obj)
{
	SeahorsePkcs11Deleter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SEAHORSE_PKCS11_TYPE_DELETER, SeahorsePkcs11Deleter);
	(self->objects == NULL) ? NULL : (self->objects = (_g_list_free__g_object_unref0_ (self->objects), NULL));
	G_OBJECT_CLASS (seahorse_pkcs11_deleter_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
seahorse_pkcs11_deleter_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (SeahorsePkcs11DeleterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) seahorse_pkcs11_deleter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SeahorsePkcs11Deleter), 0, (GInstanceInitFunc) seahorse_pkcs11_deleter_instance_init, NULL };
	GType seahorse_pkcs11_deleter_type_id;
	seahorse_pkcs11_deleter_type_id = g_type_register_static (SEAHORSE_TYPE_DELETER, "SeahorsePkcs11Deleter", &g_define_type_info, 0);
	return seahorse_pkcs11_deleter_type_id;
}

GType
seahorse_pkcs11_deleter_get_type (void)
{
	static volatile gsize seahorse_pkcs11_deleter_type_id__once = 0;
	if (g_once_init_enter (&seahorse_pkcs11_deleter_type_id__once)) {
		GType seahorse_pkcs11_deleter_type_id;
		seahorse_pkcs11_deleter_type_id = seahorse_pkcs11_deleter_get_type_once ();
		g_once_init_leave (&seahorse_pkcs11_deleter_type_id__once, seahorse_pkcs11_deleter_type_id);
	}
	return seahorse_pkcs11_deleter_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;
}

