/* certificate-der-exporter.c generated by valac 0.28.0, the Vala compiler
 * generated from certificate-der-exporter.vala, do not modify */

/*
 * Seahorse
 *
 * Copyright (C) 2011 Collabora Ltd.
 * Copyright (C) 2012 Stefan Walter
 *
 * 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/>.
 *
 * Author: Stef Walter <stefw@collabora.co.uk>
 */

#include <glib.h>
#include <glib-object.h>
#include "seahorse-pkcs11.h"
#include <gcr/gcr-base.h>
#include <gio/gio.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gi18n-lib.h>
#include "seahorse-common.h"
#include <gtk/gtk.h>

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_list_free0(var) ((var == NULL) ? NULL : (var = (g_list_free (var), NULL)))
typedef struct _SeahorseCertificateDerExporterExportData SeahorseCertificateDerExporterExportData;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _SeahorseCertificateDerExporterPrivate {
	GcrCertificate* _certificate;
	GList* _objects;
};

struct _SeahorseCertificateDerExporterExportData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	SeahorseCertificateDerExporter* self;
	GCancellable* cancellable;
	guchar* result;
	gint result_length1;
	GcrCertificate* _tmp0_;
	gsize _tmp1_;
	guint8* _tmp2_;
	guchar* _tmp3_;
	gint _tmp3__length1;
	guchar* _tmp4_;
	gint _tmp4__length1;
};


static gpointer seahorse_certificate_der_exporter_parent_class = NULL;
static SeahorseExporterIface* seahorse_certificate_der_exporter_seahorse_exporter_parent_iface = NULL;

#define SEAHORSE_CERTIFICATE_DER_EXPORTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SEAHORSE_TYPE_CERTIFICATE_DER_EXPORTER, SeahorseCertificateDerExporterPrivate))
enum  {
	SEAHORSE_CERTIFICATE_DER_EXPORTER_DUMMY_PROPERTY,
	SEAHORSE_CERTIFICATE_DER_EXPORTER_FILENAME,
	SEAHORSE_CERTIFICATE_DER_EXPORTER_CONTENT_TYPE,
	SEAHORSE_CERTIFICATE_DER_EXPORTER_FILE_FILTER
};
static GList* seahorse_certificate_der_exporter_real_get_objects (SeahorseExporter* base);
static gboolean seahorse_certificate_der_exporter_real_add_object (SeahorseExporter* base, GObject* obj);
static void seahorse_certificate_der_exporter_real_export_data_free (gpointer _data);
static void seahorse_certificate_der_exporter_real_export (SeahorseExporter* base, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean seahorse_certificate_der_exporter_real_export_co (SeahorseCertificateDerExporterExportData* _data_);
static guchar* _vala_array_dup1 (guchar* self, int length);
static void seahorse_certificate_der_exporter_finalize (GObject* obj);
static void _vala_seahorse_certificate_der_exporter_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);


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


SeahorseCertificateDerExporter* seahorse_certificate_der_exporter_construct (GType object_type, GcrCertificate* certificate) {
	SeahorseCertificateDerExporter * self = NULL;
	GcrCertificate* _tmp0_ = NULL;
	GcrCertificate* _tmp1_ = NULL;
	GcrCertificate* _tmp2_ = NULL;
	g_return_val_if_fail (certificate != NULL, NULL);
	self = (SeahorseCertificateDerExporter*) g_object_new (object_type, NULL);
	_tmp0_ = certificate;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->_certificate);
	self->priv->_certificate = _tmp1_;
	_tmp2_ = certificate;
	self->priv->_objects = g_list_append (self->priv->_objects, (GObject*) _tmp2_);
	return self;
}


SeahorseCertificateDerExporter* seahorse_certificate_der_exporter_new (GcrCertificate* certificate) {
	return seahorse_certificate_der_exporter_construct (SEAHORSE_TYPE_CERTIFICATE_DER_EXPORTER, certificate);
}


static GList* seahorse_certificate_der_exporter_real_get_objects (SeahorseExporter* base) {
	SeahorseCertificateDerExporter * self;
	GList* result = NULL;
	GList* _tmp0_ = NULL;
	self = (SeahorseCertificateDerExporter*) base;
	_tmp0_ = self->priv->_objects;
	result = _tmp0_;
	return result;
}


static gboolean seahorse_certificate_der_exporter_real_add_object (SeahorseExporter* base, GObject* obj) {
	SeahorseCertificateDerExporter * self;
	gboolean result = FALSE;
	self = (SeahorseCertificateDerExporter*) base;
	g_return_val_if_fail (obj != NULL, FALSE);
	result = FALSE;
	return result;
}


static void seahorse_certificate_der_exporter_real_export_data_free (gpointer _data) {
	SeahorseCertificateDerExporterExportData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->cancellable);
	_data_->result = (g_free (_data_->result), NULL);
	_g_object_unref0 (_data_->self);
	g_slice_free (SeahorseCertificateDerExporterExportData, _data_);
}


static void seahorse_certificate_der_exporter_real_export (SeahorseExporter* base, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	SeahorseCertificateDerExporter * self;
	SeahorseCertificateDerExporterExportData* _data_;
	SeahorseCertificateDerExporter* _tmp0_ = NULL;
	GCancellable* _tmp1_ = NULL;
	GCancellable* _tmp2_ = NULL;
	self = (SeahorseCertificateDerExporter*) base;
	_data_ = g_slice_new0 (SeahorseCertificateDerExporterExportData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, seahorse_certificate_der_exporter_real_export);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, seahorse_certificate_der_exporter_real_export_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = cancellable;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp2_;
	seahorse_certificate_der_exporter_real_export_co (_data_);
}


static guchar* seahorse_certificate_der_exporter_real_export_finish (SeahorseExporter* base, GAsyncResult* _res_, int* result_length1, GError** error) {
	guchar* result;
	SeahorseCertificateDerExporterExportData* _data_;
	if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (_res_), error)) {
		return NULL;
	}
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
	result = _data_->result;
	*result_length1 = _data_->result_length1;
	_data_->result = NULL;
	return result;
}


static guchar* _vala_array_dup1 (guchar* self, int length) {
	return g_memdup (self, length * sizeof (guchar));
}


static gboolean seahorse_certificate_der_exporter_real_export_co (SeahorseCertificateDerExporterExportData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = _data_->self->priv->_certificate;
	memset (&_data_->_tmp1_, 0, sizeof (gsize));
	_data_->_tmp2_ = NULL;
	_data_->_tmp2_ = gcr_certificate_get_der_data (_data_->_tmp0_, &_data_->_tmp1_);
	_data_->_tmp3_ = NULL;
	_data_->_tmp3__length1 = 0;
	_data_->_tmp3_ = (_data_->_tmp2_ != NULL) ? _vala_array_dup1 (_data_->_tmp2_, _data_->_tmp1_) : ((gpointer) _data_->_tmp2_);
	_data_->_tmp3__length1 = _data_->_tmp1_;
	_data_->_tmp4_ = NULL;
	_data_->_tmp4__length1 = 0;
	_data_->_tmp4_ = _data_->_tmp3_;
	_data_->_tmp4__length1 = _data_->_tmp3__length1;
	_data_->result_length1 = _data_->_tmp4__length1;
	_data_->result = _data_->_tmp4_;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static gchar* string_delimit (const gchar* self, const gchar* delimiters, gchar new_delimiter) {
	gchar* result = NULL;
	gchar* _result_ = NULL;
	gchar* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	gchar _tmp3_ = '\0';
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (delimiters != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	_tmp2_ = delimiters;
	_tmp3_ = new_delimiter;
	g_strdelimit (_tmp1_, _tmp2_, _tmp3_);
	result = _result_;
	return result;
}


static gchar* seahorse_certificate_der_exporter_real_get_filename (SeahorseExporter* base) {
	gchar* result;
	SeahorseCertificateDerExporter* self;
	gchar* label = NULL;
	GcrCertificate* _tmp0_ = NULL;
	const gchar* _tmp8_ = NULL;
	gchar* filename = NULL;
	const gchar* _tmp11_ = NULL;
	gchar* _tmp12_ = NULL;
	gchar* _tmp13_ = NULL;
	self = (SeahorseCertificateDerExporter*) base;
	label = NULL;
	_tmp0_ = self->priv->_certificate;
	if (_tmp0_ != NULL) {
		GcrCertificate* _tmp1_ = NULL;
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_ = NULL;
		const gchar* _tmp4_ = NULL;
		_tmp1_ = self->priv->_certificate;
		g_object_get (_tmp1_, "label", &_tmp2_, NULL);
		_tmp3_ = _tmp2_;
		_g_free0 (label);
		label = _tmp3_;
		_tmp4_ = label;
		if (_tmp4_ == NULL) {
			GcrCertificate* _tmp5_ = NULL;
			gchar* _tmp6_ = NULL;
			gchar* _tmp7_ = NULL;
			_tmp5_ = self->priv->_certificate;
			g_object_get (_tmp5_, "description", &_tmp6_, NULL);
			_tmp7_ = _tmp6_;
			_g_free0 (label);
			label = _tmp7_;
		}
	}
	_tmp8_ = label;
	if (_tmp8_ == NULL) {
		const gchar* _tmp9_ = NULL;
		gchar* _tmp10_ = NULL;
		_tmp9_ = _ ("Certificate");
		_tmp10_ = g_strdup (_tmp9_);
		_g_free0 (label);
		label = _tmp10_;
	}
	_tmp11_ = label;
	_tmp12_ = g_strconcat (_tmp11_, ".crt", NULL);
	filename = _tmp12_;
	_tmp13_ = string_delimit (filename, SEAHORSE_BAD_FILENAME_CHARS, '_');
	result = _tmp13_;
	_g_free0 (filename);
	_g_free0 (label);
	return result;
}


static const gchar* seahorse_certificate_der_exporter_real_get_content_type (SeahorseExporter* base) {
	const gchar* result;
	SeahorseCertificateDerExporter* self;
	self = (SeahorseCertificateDerExporter*) base;
	result = "application/pkix-cert";
	return result;
}


static GtkFileFilter* seahorse_certificate_der_exporter_real_get_file_filter (SeahorseExporter* base) {
	GtkFileFilter* result;
	SeahorseCertificateDerExporter* self;
	GtkFileFilter* filter = NULL;
	GtkFileFilter* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	self = (SeahorseCertificateDerExporter*) base;
	_tmp0_ = gtk_file_filter_new ();
	g_object_ref_sink (_tmp0_);
	filter = _tmp0_;
	_tmp1_ = _ ("Certificates (DER encoded)");
	gtk_buildable_set_name ((GtkBuildable*) filter, _tmp1_);
	gtk_file_filter_add_mime_type (filter, "application/pkix-cert");
	gtk_file_filter_add_mime_type (filter, "application/x-x509-ca-cert");
	gtk_file_filter_add_mime_type (filter, "application/x-x509-user-cert");
	gtk_file_filter_add_pattern (filter, "*.cer");
	gtk_file_filter_add_pattern (filter, "*.crt");
	gtk_file_filter_add_pattern (filter, "*.cert");
	result = filter;
	return result;
}


static void seahorse_certificate_der_exporter_class_init (SeahorseCertificateDerExporterClass * klass) {
	seahorse_certificate_der_exporter_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SeahorseCertificateDerExporterPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_seahorse_certificate_der_exporter_get_property;
	G_OBJECT_CLASS (klass)->finalize = seahorse_certificate_der_exporter_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SEAHORSE_CERTIFICATE_DER_EXPORTER_FILENAME, g_param_spec_string ("filename", "filename", "filename", NULL, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SEAHORSE_CERTIFICATE_DER_EXPORTER_CONTENT_TYPE, g_param_spec_string ("content-type", "content-type", "content-type", NULL, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SEAHORSE_CERTIFICATE_DER_EXPORTER_FILE_FILTER, g_param_spec_object ("file-filter", "file-filter", "file-filter", gtk_file_filter_get_type (), G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
}


static void seahorse_certificate_der_exporter_seahorse_exporter_interface_init (SeahorseExporterIface * iface) {
	seahorse_certificate_der_exporter_seahorse_exporter_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_objects = (GList* (*)(SeahorseExporter*)) seahorse_certificate_der_exporter_real_get_objects;
	iface->add_object = (gboolean (*)(SeahorseExporter*, GObject*)) seahorse_certificate_der_exporter_real_add_object;
	iface->export = (guint8* (*)(SeahorseExporter*, GCancellable*, size_t*, GError**)) seahorse_certificate_der_exporter_real_export;
	iface->export_finish = seahorse_certificate_der_exporter_real_export_finish;
	iface->get_filename = seahorse_certificate_der_exporter_real_get_filename;
	iface->get_content_type = seahorse_certificate_der_exporter_real_get_content_type;
	iface->get_file_filter = seahorse_certificate_der_exporter_real_get_file_filter;
}


static void seahorse_certificate_der_exporter_instance_init (SeahorseCertificateDerExporter * self) {
	self->priv = SEAHORSE_CERTIFICATE_DER_EXPORTER_GET_PRIVATE (self);
}


static void seahorse_certificate_der_exporter_finalize (GObject* obj) {
	SeahorseCertificateDerExporter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SEAHORSE_TYPE_CERTIFICATE_DER_EXPORTER, SeahorseCertificateDerExporter);
	_g_object_unref0 (self->priv->_certificate);
	_g_list_free0 (self->priv->_objects);
	G_OBJECT_CLASS (seahorse_certificate_der_exporter_parent_class)->finalize (obj);
}


GType seahorse_certificate_der_exporter_get_type (void) {
	static volatile gsize seahorse_certificate_der_exporter_type_id__volatile = 0;
	if (g_once_init_enter (&seahorse_certificate_der_exporter_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SeahorseCertificateDerExporterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) seahorse_certificate_der_exporter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SeahorseCertificateDerExporter), 0, (GInstanceInitFunc) seahorse_certificate_der_exporter_instance_init, NULL };
		static const GInterfaceInfo seahorse_exporter_info = { (GInterfaceInitFunc) seahorse_certificate_der_exporter_seahorse_exporter_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType seahorse_certificate_der_exporter_type_id;
		seahorse_certificate_der_exporter_type_id = g_type_register_static (G_TYPE_OBJECT, "SeahorseCertificateDerExporter", &g_define_type_info, 0);
		g_type_add_interface_static (seahorse_certificate_der_exporter_type_id, SEAHORSE_TYPE_EXPORTER, &seahorse_exporter_info);
		g_once_init_leave (&seahorse_certificate_der_exporter_type_id__volatile, seahorse_certificate_der_exporter_type_id);
	}
	return seahorse_certificate_der_exporter_type_id__volatile;
}


static void _vala_seahorse_certificate_der_exporter_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	SeahorseCertificateDerExporter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SEAHORSE_TYPE_CERTIFICATE_DER_EXPORTER, SeahorseCertificateDerExporter);
	switch (property_id) {
		case SEAHORSE_CERTIFICATE_DER_EXPORTER_FILENAME:
		g_value_take_string (value, seahorse_exporter_get_filename ((SeahorseExporter*) self));
		break;
		case SEAHORSE_CERTIFICATE_DER_EXPORTER_CONTENT_TYPE:
		g_value_set_string (value, seahorse_exporter_get_content_type ((SeahorseExporter*) self));
		break;
		case SEAHORSE_CERTIFICATE_DER_EXPORTER_FILE_FILTER:
		g_value_take_object (value, seahorse_exporter_get_file_filter ((SeahorseExporter*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



