/* gkr-keyring-permission.c generated by valac 0.56.3, the Vala compiler
 * generated from gkr-keyring-permission.vala, do not modify */

/*
 * Seahorse
 *
 * Copyright (C) 2018 Anukul Sangwan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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, see <http://www.gnu.org/licenses/>.
 */

#include "seahorse-gkr.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <libsecret/secret.h>
#include "seahorse-common.h"

enum  {
	SEAHORSE_GKR_KEYRING_PERMISSION_0_PROPERTY,
	SEAHORSE_GKR_KEYRING_PERMISSION_NUM_PROPERTIES
};
static GParamSpec* seahorse_gkr_keyring_permission_properties[SEAHORSE_GKR_KEYRING_PERMISSION_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _SeahorseGkrKeyringPermissionAcquireAsyncData SeahorseGkrKeyringPermissionAcquireAsyncData;
typedef struct _SeahorseGkrKeyringPermissionReleaseAsyncData SeahorseGkrKeyringPermissionReleaseAsyncData;

struct _SeahorseGkrKeyringPermissionPrivate {
	SeahorseGkrKeyring* keyring;
};

struct _SeahorseGkrKeyringPermissionAcquireAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	SeahorseGkrKeyringPermission* self;
	GCancellable* cancellable;
	gboolean result;
	SeahorseGkrKeyring* _tmp0_;
	GError* _inner_error0_;
};

struct _SeahorseGkrKeyringPermissionReleaseAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	SeahorseGkrKeyringPermission* self;
	GCancellable* cancellable;
	gboolean result;
	SeahorseGkrKeyring* _tmp0_;
	GError* _inner_error0_;
};

static gint SeahorseGkrKeyringPermission_private_offset;
static gpointer seahorse_gkr_keyring_permission_parent_class = NULL;

static void seahorse_gkr_keyring_permission_refresh (SeahorseGkrKeyringPermission* self);
static void seahorse_gkr_keyring_permission_disable (SeahorseGkrKeyringPermission* self);
static void seahorse_gkr_keyring_permission_real_acquire_async_data_free (gpointer _data);
static void seahorse_gkr_keyring_permission_real_acquire_async (GPermission* base,
                                                         GCancellable* cancellable,
                                                         GAsyncReadyCallback _callback_,
                                                         gpointer _user_data_);
static gboolean seahorse_gkr_keyring_permission_real_acquire_async_co (SeahorseGkrKeyringPermissionAcquireAsyncData* _data_);
static void seahorse_gkr_keyring_permission_acquire_async_ready (GObject* source_object,
                                                          GAsyncResult* _res_,
                                                          gpointer _user_data_);
static void seahorse_gkr_keyring_permission_real_release_async_data_free (gpointer _data);
static void seahorse_gkr_keyring_permission_real_release_async (GPermission* base,
                                                         GCancellable* cancellable,
                                                         GAsyncReadyCallback _callback_,
                                                         gpointer _user_data_);
static gboolean seahorse_gkr_keyring_permission_real_release_async_co (SeahorseGkrKeyringPermissionReleaseAsyncData* _data_);
static void seahorse_gkr_keyring_permission_release_async_ready (GObject* source_object,
                                                          GAsyncResult* _res_,
                                                          gpointer _user_data_);
static void seahorse_gkr_keyring_permission_finalize (GObject * obj);
static GType seahorse_gkr_keyring_permission_get_type_once (void);

static inline gpointer
seahorse_gkr_keyring_permission_get_instance_private (SeahorseGkrKeyringPermission* self)
{
	return G_STRUCT_MEMBER_P (self, SeahorseGkrKeyringPermission_private_offset);
}

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

SeahorseGkrKeyringPermission*
seahorse_gkr_keyring_permission_construct (GType object_type,
                                           SeahorseGkrKeyring* keyring)
{
	SeahorseGkrKeyringPermission * self = NULL;
	SeahorseGkrKeyring* _tmp0_;
	g_return_val_if_fail (keyring != NULL, NULL);
	self = (SeahorseGkrKeyringPermission*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (keyring);
	_g_object_unref0 (self->priv->keyring);
	self->priv->keyring = _tmp0_;
	seahorse_gkr_keyring_permission_refresh (self);
	return self;
}

SeahorseGkrKeyringPermission*
seahorse_gkr_keyring_permission_new (SeahorseGkrKeyring* keyring)
{
	return seahorse_gkr_keyring_permission_construct (SEAHORSE_GKR_TYPE_KEYRING_PERMISSION, keyring);
}

static void
seahorse_gkr_keyring_permission_disable (SeahorseGkrKeyringPermission* self)
{
	g_return_if_fail (self != NULL);
	g_permission_impl_update ((GPermission*) self, FALSE, FALSE, FALSE);
}

static void
seahorse_gkr_keyring_permission_refresh (SeahorseGkrKeyringPermission* self)
{
	SeahorseGkrKeyring* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	SeahorseGkrKeyring* _tmp3_;
	SeahorseGkrKeyring* _tmp4_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->keyring;
	_tmp1_ = secret_collection_get_locked ((SecretCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = self->priv->keyring;
	_tmp4_ = self->priv->keyring;
	g_permission_impl_update ((GPermission*) self, !_tmp2_, seahorse_lockable_can_unlock ((GObject*) _tmp3_), seahorse_lockable_can_lock ((GObject*) _tmp4_));
}

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

static void
seahorse_gkr_keyring_permission_real_acquire_async (GPermission* base,
                                                    GCancellable* cancellable,
                                                    GAsyncReadyCallback _callback_,
                                                    gpointer _user_data_)
{
	SeahorseGkrKeyringPermission * self;
	SeahorseGkrKeyringPermissionAcquireAsyncData* _data_;
	SeahorseGkrKeyringPermission* _tmp0_;
	GCancellable* _tmp1_;
	self = (SeahorseGkrKeyringPermission*) base;
	_data_ = g_slice_new0 (SeahorseGkrKeyringPermissionAcquireAsyncData);
	_data_->_async_result = g_task_new (G_OBJECT (self), cancellable, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, seahorse_gkr_keyring_permission_real_acquire_async_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp1_;
	seahorse_gkr_keyring_permission_real_acquire_async_co (_data_);
}

static gboolean
seahorse_gkr_keyring_permission_acquire_finish (GPermission* base,
                                                GAsyncResult* _res_,
                                                GError** error)
{
	gboolean result;
	SeahorseGkrKeyringPermissionAcquireAsyncData* _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_gkr_keyring_permission_acquire_async_ready (GObject* source_object,
                                                     GAsyncResult* _res_,
                                                     gpointer _user_data_)
{
	SeahorseGkrKeyringPermissionAcquireAsyncData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	seahorse_gkr_keyring_permission_real_acquire_async_co (_data_);
}

static gboolean
seahorse_gkr_keyring_permission_real_acquire_async_co (SeahorseGkrKeyringPermissionAcquireAsyncData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	seahorse_gkr_keyring_permission_disable (_data_->self);
	_data_->_tmp0_ = _data_->self->priv->keyring;
	_data_->_state_ = 1;
	seahorse_lockable_unlock ((SeahorseLockable*) _data_->_tmp0_, NULL, NULL, seahorse_gkr_keyring_permission_acquire_async_ready, _data_);
	return FALSE;
	_state_1:
	seahorse_lockable_unlock_finish ((SeahorseLockable*) _data_->_tmp0_, _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;
	}
	seahorse_gkr_keyring_permission_refresh (_data_->self);
	_data_->result = TRUE;
	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_gkr_keyring_permission_real_release_async_data_free (gpointer _data)
{
	SeahorseGkrKeyringPermissionReleaseAsyncData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->cancellable);
	_g_object_unref0 (_data_->self);
	g_slice_free (SeahorseGkrKeyringPermissionReleaseAsyncData, _data_);
}

static void
seahorse_gkr_keyring_permission_real_release_async (GPermission* base,
                                                    GCancellable* cancellable,
                                                    GAsyncReadyCallback _callback_,
                                                    gpointer _user_data_)
{
	SeahorseGkrKeyringPermission * self;
	SeahorseGkrKeyringPermissionReleaseAsyncData* _data_;
	SeahorseGkrKeyringPermission* _tmp0_;
	GCancellable* _tmp1_;
	self = (SeahorseGkrKeyringPermission*) base;
	_data_ = g_slice_new0 (SeahorseGkrKeyringPermissionReleaseAsyncData);
	_data_->_async_result = g_task_new (G_OBJECT (self), cancellable, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, seahorse_gkr_keyring_permission_real_release_async_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp1_;
	seahorse_gkr_keyring_permission_real_release_async_co (_data_);
}

static gboolean
seahorse_gkr_keyring_permission_release_finish (GPermission* base,
                                                GAsyncResult* _res_,
                                                GError** error)
{
	gboolean result;
	SeahorseGkrKeyringPermissionReleaseAsyncData* _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_gkr_keyring_permission_release_async_ready (GObject* source_object,
                                                     GAsyncResult* _res_,
                                                     gpointer _user_data_)
{
	SeahorseGkrKeyringPermissionReleaseAsyncData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	seahorse_gkr_keyring_permission_real_release_async_co (_data_);
}

static gboolean
seahorse_gkr_keyring_permission_real_release_async_co (SeahorseGkrKeyringPermissionReleaseAsyncData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	seahorse_gkr_keyring_permission_disable (_data_->self);
	_data_->_tmp0_ = _data_->self->priv->keyring;
	_data_->_state_ = 1;
	seahorse_lockable_lock ((SeahorseLockable*) _data_->_tmp0_, NULL, NULL, seahorse_gkr_keyring_permission_release_async_ready, _data_);
	return FALSE;
	_state_1:
	seahorse_lockable_lock_finish ((SeahorseLockable*) _data_->_tmp0_, _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;
	}
	seahorse_gkr_keyring_permission_refresh (_data_->self);
	_data_->result = TRUE;
	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_gkr_keyring_permission_class_init (SeahorseGkrKeyringPermissionClass * klass,
                                            gpointer klass_data)
{
	seahorse_gkr_keyring_permission_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &SeahorseGkrKeyringPermission_private_offset);
	((GPermissionClass *) klass)->acquire_async = (void (*) (GPermission*, GCancellable*, GAsyncReadyCallback, gpointer)) seahorse_gkr_keyring_permission_real_acquire_async;
	((GPermissionClass *) klass)->acquire_finish = (gboolean (*) (GPermission*, GAsyncResult*, GError**)) seahorse_gkr_keyring_permission_acquire_finish;
	((GPermissionClass *) klass)->release_async = (void (*) (GPermission*, GCancellable*, GAsyncReadyCallback, gpointer)) seahorse_gkr_keyring_permission_real_release_async;
	((GPermissionClass *) klass)->release_finish = (gboolean (*) (GPermission*, GAsyncResult*, GError**)) seahorse_gkr_keyring_permission_release_finish;
	G_OBJECT_CLASS (klass)->finalize = seahorse_gkr_keyring_permission_finalize;
}

static void
seahorse_gkr_keyring_permission_instance_init (SeahorseGkrKeyringPermission * self,
                                               gpointer klass)
{
	self->priv = seahorse_gkr_keyring_permission_get_instance_private (self);
}

static void
seahorse_gkr_keyring_permission_finalize (GObject * obj)
{
	SeahorseGkrKeyringPermission * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SEAHORSE_GKR_TYPE_KEYRING_PERMISSION, SeahorseGkrKeyringPermission);
	_g_object_unref0 (self->priv->keyring);
	G_OBJECT_CLASS (seahorse_gkr_keyring_permission_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
seahorse_gkr_keyring_permission_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (SeahorseGkrKeyringPermissionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) seahorse_gkr_keyring_permission_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SeahorseGkrKeyringPermission), 0, (GInstanceInitFunc) seahorse_gkr_keyring_permission_instance_init, NULL };
	GType seahorse_gkr_keyring_permission_type_id;
	seahorse_gkr_keyring_permission_type_id = g_type_register_static (g_permission_get_type (), "SeahorseGkrKeyringPermission", &g_define_type_info, 0);
	SeahorseGkrKeyringPermission_private_offset = g_type_add_instance_private (seahorse_gkr_keyring_permission_type_id, sizeof (SeahorseGkrKeyringPermissionPrivate));
	return seahorse_gkr_keyring_permission_type_id;
}

GType
seahorse_gkr_keyring_permission_get_type (void)
{
	static volatile gsize seahorse_gkr_keyring_permission_type_id__once = 0;
	if (g_once_init_enter (&seahorse_gkr_keyring_permission_type_id__once)) {
		GType seahorse_gkr_keyring_permission_type_id;
		seahorse_gkr_keyring_permission_type_id = seahorse_gkr_keyring_permission_get_type_once ();
		g_once_init_leave (&seahorse_gkr_keyring_permission_type_id__once, seahorse_gkr_keyring_permission_type_id);
	}
	return seahorse_gkr_keyring_permission_type_id__once;
}

