/* gkr-item-add.c generated by valac 0.28.0, the Vala compiler
 * generated from gkr-item-add.vala, do not modify */

/*
 * Seahorse
 *
 * Copyright (C) 2008 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/>.
 */

#include <glib.h>
#include <glib-object.h>
#include "seahorse-gkr.h"
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include "seahorse-common.h"
#include <stdlib.h>
#include <string.h>
#include <libsecret/secret.h>
#include <ui/gcr-ui.h>
#include <gio/gio.h>

typedef struct _Block2Data Block2Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_list_free0(var) ((var == NULL) ? NULL : (var = (g_list_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _secret_schema_unref0(var) ((var == NULL) ? NULL : (var = (secret_schema_unref (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _secret_value_unref0(var) ((var == NULL) ? NULL : (var = (secret_value_unref (var), NULL)))

struct _Block2Data {
	int _ref_count_;
	SeahorseGkrItemAdd* self;
	GtkComboBox* combo;
	GtkEntry* label;
	GtkEntry* entry;
	GtkToggleButton* check;
};


static gpointer seahorse_gkr_item_add_parent_class = NULL;

enum  {
	SEAHORSE_GKR_ITEM_ADD_DUMMY_PROPERTY
};
static GObject * seahorse_gkr_item_add_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static void _seahorse_gkr_item_add___lambda10_ (Block2Data* _data2_, GtkEditable* editable);
static void __seahorse_gkr_item_add___lambda10__gtk_editable_changed (GtkEditable* _sender, gpointer self);
static void _seahorse_gkr_item_add___lambda11_ (Block2Data* _data2_);
static void __seahorse_gkr_item_add___lambda11__gtk_toggle_button_toggled (GtkToggleButton* _sender, gpointer self);
static void _seahorse_gkr_item_add___lambda12_ (Block2Data* _data2_, gint resp);
static void _g_free0_ (gpointer var);
static void __lambda13_ (SeahorseGkrItemAdd* self, GObject* obj, GAsyncResult* res);
static void ___lambda13__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self);
static void __seahorse_gkr_item_add___lambda12__gtk_dialog_response (GtkDialog* _sender, gint response_id, gpointer self);


SeahorseGkrItemAdd* seahorse_gkr_item_add_construct (GType object_type, GtkWindow* parent) {
	SeahorseGkrItemAdd * self = NULL;
	GtkWindow* _tmp0_ = NULL;
	_tmp0_ = parent;
	self = (SeahorseGkrItemAdd*) g_object_new (object_type, "transient-for", _tmp0_, NULL);
	gtk_widget_show ((GtkWidget*) self);
	gtk_window_present ((GtkWindow*) self);
	return self;
}


SeahorseGkrItemAdd* seahorse_gkr_item_add_new (GtkWindow* parent) {
	return seahorse_gkr_item_add_construct (SEAHORSE_GKR_TYPE_ITEM_ADD, parent);
}


static Block2Data* block2_data_ref (Block2Data* _data2_) {
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}


static void block2_data_unref (void * _userdata_) {
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		SeahorseGkrItemAdd* self;
		self = _data2_->self;
		_g_object_unref0 (_data2_->check);
		_g_object_unref0 (_data2_->entry);
		_g_object_unref0 (_data2_->label);
		_g_object_unref0 (_data2_->combo);
		_g_object_unref0 (self);
		g_slice_free (Block2Data, _data2_);
	}
}


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


static void _seahorse_gkr_item_add___lambda10_ (Block2Data* _data2_, GtkEditable* editable) {
	SeahorseGkrItemAdd* self;
	gchar* value = NULL;
	GtkEntry* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	self = _data2_->self;
	g_return_if_fail (editable != NULL);
	_tmp0_ = _data2_->label;
	_tmp1_ = gtk_entry_get_text (_tmp0_);
	_tmp2_ = g_strdup (_tmp1_);
	value = _tmp2_;
	_tmp3_ = value;
	gtk_dialog_set_response_sensitive ((GtkDialog*) self, (gint) GTK_RESPONSE_ACCEPT, g_strcmp0 (_tmp3_, "") != 0);
	_g_free0 (value);
}


static void __seahorse_gkr_item_add___lambda10__gtk_editable_changed (GtkEditable* _sender, gpointer self) {
	_seahorse_gkr_item_add___lambda10_ (self, _sender);
}


static void _seahorse_gkr_item_add___lambda11_ (Block2Data* _data2_) {
	SeahorseGkrItemAdd* self;
	GtkEntry* _tmp0_ = NULL;
	GtkToggleButton* _tmp1_ = NULL;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	self = _data2_->self;
	_tmp0_ = _data2_->entry;
	_tmp1_ = _data2_->check;
	_tmp2_ = gtk_toggle_button_get_active (_tmp1_);
	_tmp3_ = _tmp2_;
	gtk_entry_set_visibility (_tmp0_, _tmp3_);
}


static void __seahorse_gkr_item_add___lambda11__gtk_toggle_button_toggled (GtkToggleButton* _sender, gpointer self) {
	_seahorse_gkr_item_add___lambda11_ (self);
}


static void _g_free0_ (gpointer var) {
	var = (g_free (var), NULL);
}


static void __lambda13_ (SeahorseGkrItemAdd* self, GObject* obj, GAsyncResult* res) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (res != NULL);
	{
		GAsyncResult* _tmp0_ = NULL;
		SecretItem* _tmp1_ = NULL;
		SecretItem* _tmp2_ = NULL;
		seahorse_gkr_dialog_complete_request ((GtkWidget*) self, FALSE);
		_tmp0_ = res;
		_tmp1_ = secret_item_create_finish (_tmp0_, &_inner_error_);
		_tmp2_ = _tmp1_;
		_g_object_unref0 (_tmp2_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch7_g_error;
		}
	}
	goto __finally7;
	__catch7_g_error:
	{
		GError* err = NULL;
		const gchar* _tmp3_ = NULL;
		GError* _tmp4_ = NULL;
		const gchar* _tmp5_ = NULL;
		err = _inner_error_;
		_inner_error_ = NULL;
		_tmp3_ = _ ("Couldn't add item");
		_tmp4_ = err;
		_tmp5_ = _tmp4_->message;
		seahorse_util_show_error ((GtkWidget*) self, _tmp3_, _tmp5_);
		_g_error_free0 (err);
	}
	__finally7:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	gtk_widget_destroy ((GtkWidget*) self);
}


static void ___lambda13__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self) {
	__lambda13_ ((SeahorseGkrItemAdd*) self, source_object, res);
	g_object_unref (self);
}


static void _seahorse_gkr_item_add___lambda12_ (Block2Data* _data2_, gint resp) {
	SeahorseGkrItemAdd* self;
	gint _tmp0_ = 0;
	GtkTreeIter iter = {0};
	GtkComboBox* _tmp1_ = NULL;
	GtkTreeIter _tmp2_ = {0};
	gboolean _tmp3_ = FALSE;
	SecretCollection* collection = NULL;
	GtkComboBox* _tmp4_ = NULL;
	GtkTreeModel* _tmp5_ = NULL;
	GtkTreeModel* _tmp6_ = NULL;
	GtkTreeIter _tmp7_ = {0};
	SecretValue* secret = NULL;
	GtkEntry* _tmp8_ = NULL;
	const gchar* _tmp9_ = NULL;
	SecretValue* _tmp10_ = NULL;
	GCancellable* cancellable = NULL;
	GCancellable* _tmp11_ = NULL;
	GHashTable* attributes = NULL;
	GHashFunc _tmp12_ = NULL;
	GEqualFunc _tmp13_ = NULL;
	GHashTable* _tmp14_ = NULL;
	SecretSchema* schema = NULL;
	SecretSchema* _tmp15_ = NULL;
	SecretCollection* _tmp16_ = NULL;
	SecretSchema* _tmp17_ = NULL;
	GHashTable* _tmp18_ = NULL;
	GtkEntry* _tmp19_ = NULL;
	const gchar* _tmp20_ = NULL;
	SecretValue* _tmp21_ = NULL;
	GCancellable* _tmp22_ = NULL;
	self = _data2_->self;
	_tmp0_ = resp;
	if (_tmp0_ != ((gint) GTK_RESPONSE_ACCEPT)) {
		gtk_widget_destroy ((GtkWidget*) self);
		return;
	}
	_tmp1_ = _data2_->combo;
	_tmp3_ = gtk_combo_box_get_active_iter (_tmp1_, &_tmp2_);
	iter = _tmp2_;
	if (!_tmp3_) {
		return;
	}
	_tmp4_ = _data2_->combo;
	_tmp5_ = gtk_combo_box_get_model (_tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = iter;
	gtk_tree_model_get (_tmp6_, &_tmp7_, 1, &collection, -1, -1);
	_tmp8_ = _data2_->entry;
	_tmp9_ = gtk_entry_get_text (_tmp8_);
	_tmp10_ = secret_value_new (_tmp9_, (gssize) (-1), "text/plain");
	secret = _tmp10_;
	_tmp11_ = seahorse_gkr_dialog_begin_request ((GtkWidget*) self);
	cancellable = _tmp11_;
	_tmp12_ = g_str_hash;
	_tmp13_ = g_str_equal;
	_tmp14_ = g_hash_table_new_full (_tmp12_, _tmp13_, _g_free0_, _g_free0_);
	attributes = _tmp14_;
	_tmp15_ = secret_schema_new ("org.gnome.keyring.Note", SECRET_SCHEMA_NONE, NULL);
	schema = _tmp15_;
	_tmp16_ = collection;
	_tmp17_ = schema;
	_tmp18_ = attributes;
	_tmp19_ = _data2_->label;
	_tmp20_ = gtk_entry_get_text (_tmp19_);
	_tmp21_ = secret;
	_tmp22_ = cancellable;
	secret_item_create (_tmp16_, _tmp17_, _tmp18_, _tmp20_, _tmp21_, SECRET_ITEM_CREATE_NONE, _tmp22_, ___lambda13__gasync_ready_callback, g_object_ref (self));
	_secret_schema_unref0 (schema);
	_g_hash_table_unref0 (attributes);
	_g_object_unref0 (cancellable);
	_secret_value_unref0 (secret);
	_g_object_unref0 (collection);
}


static void __seahorse_gkr_item_add___lambda12__gtk_dialog_response (GtkDialog* _sender, gint response_id, gpointer self) {
	_seahorse_gkr_item_add___lambda12_ (self, response_id);
}


static GObject * seahorse_gkr_item_add_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	SeahorseGkrItemAdd * self;
	Block2Data* _data2_;
	const gchar* _tmp0_ = NULL;
	GtkBuilder* builder = NULL;
	GtkBuilder* _tmp1_ = NULL;
	GtkBuilder* _tmp2_ = NULL;
	GObject* _tmp3_ = NULL;
	GtkComboBox* _tmp4_ = NULL;
	GtkListStore* store = NULL;
	GtkListStore* _tmp5_ = NULL;
	GtkComboBox* _tmp6_ = NULL;
	GtkListStore* _tmp7_ = NULL;
	GtkCellRendererText* cell = NULL;
	GtkCellRendererText* _tmp8_ = NULL;
	GtkComboBox* _tmp9_ = NULL;
	GtkCellRendererText* _tmp10_ = NULL;
	GtkComboBox* _tmp11_ = NULL;
	GtkCellRendererText* _tmp12_ = NULL;
	SeahorseGkrBackend* _tmp13_ = NULL;
	SeahorseGkrBackend* _tmp14_ = NULL;
	GList* _tmp15_ = NULL;
	GList* _tmp16_ = NULL;
	GtkBuilder* _tmp31_ = NULL;
	GObject* _tmp32_ = NULL;
	GtkEntry* _tmp33_ = NULL;
	GtkEntry* _tmp34_ = NULL;
	GtkContainer* area = NULL;
	GtkBuilder* _tmp35_ = NULL;
	GObject* _tmp36_ = NULL;
	GtkContainer* _tmp37_ = NULL;
	GcrSecureEntryBuffer* buffer = NULL;
	GcrSecureEntryBuffer* _tmp38_ = NULL;
	GcrSecureEntryBuffer* _tmp39_ = NULL;
	GtkEntry* _tmp40_ = NULL;
	GtkEntry* _tmp41_ = NULL;
	GtkContainer* _tmp42_ = NULL;
	GtkEntry* _tmp43_ = NULL;
	GtkEntry* _tmp44_ = NULL;
	GtkBuilder* _tmp45_ = NULL;
	GObject* _tmp46_ = NULL;
	GtkToggleButton* _tmp47_ = NULL;
	GtkToggleButton* _tmp48_ = NULL;
	parent_class = G_OBJECT_CLASS (seahorse_gkr_item_add_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SEAHORSE_GKR_TYPE_ITEM_ADD, SeahorseGkrItemAdd);
	_data2_ = g_slice_new0 (Block2Data);
	_data2_->_ref_count_ = 1;
	_data2_->self = g_object_ref (self);
	_tmp0_ = _ ("Add Password");
	gtk_window_set_title ((GtkWindow*) self, _tmp0_);
	gtk_window_set_modal ((GtkWindow*) self, TRUE);
	g_object_set ((GtkWindow*) self, "window-position", GTK_WIN_POS_CENTER_ON_PARENT, NULL);
	gtk_container_set_border_width ((GtkContainer*) self, (guint) 5);
	_tmp1_ = seahorse_util_load_built_contents ((GtkContainer*) self, "gkr-add-item");
	builder = _tmp1_;
	gtk_dialog_add_buttons ((GtkDialog*) self, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
	_tmp2_ = builder;
	_tmp3_ = gtk_builder_get_object (_tmp2_, "item-keyring");
	_tmp4_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp3_, gtk_combo_box_get_type (), GtkComboBox));
	_data2_->combo = _tmp4_;
	_tmp5_ = gtk_list_store_new (2, G_TYPE_STRING, secret_collection_get_type (), -1);
	store = _tmp5_;
	_tmp6_ = _data2_->combo;
	_tmp7_ = store;
	gtk_combo_box_set_model (_tmp6_, (GtkTreeModel*) _tmp7_);
	_tmp8_ = (GtkCellRendererText*) gtk_cell_renderer_text_new ();
	g_object_ref_sink (_tmp8_);
	cell = _tmp8_;
	_tmp9_ = _data2_->combo;
	_tmp10_ = cell;
	gtk_cell_layout_pack_start ((GtkCellLayout*) _tmp9_, (GtkCellRenderer*) _tmp10_, TRUE);
	_tmp11_ = _data2_->combo;
	_tmp12_ = cell;
	gtk_cell_layout_add_attribute ((GtkCellLayout*) _tmp11_, (GtkCellRenderer*) _tmp12_, "text", 0);
	_tmp13_ = seahorse_gkr_backend_instance ();
	_tmp14_ = _tmp13_;
	_tmp15_ = seahorse_gkr_backend_get_keyrings (_tmp14_);
	_tmp16_ = _tmp15_;
	_g_object_unref0 (_tmp14_);
	{
		GList* keyring_collection = NULL;
		GList* keyring_it = NULL;
		keyring_collection = _tmp16_;
		for (keyring_it = keyring_collection; keyring_it != NULL; keyring_it = keyring_it->next) {
			SeahorseGkrKeyring* keyring = NULL;
			keyring = (SeahorseGkrKeyring*) keyring_it->data;
			{
				GtkTreeIter iter = {0};
				GtkListStore* _tmp17_ = NULL;
				GtkTreeIter _tmp18_ = {0};
				GtkListStore* _tmp19_ = NULL;
				GtkTreeIter _tmp20_ = {0};
				SeahorseGkrKeyring* _tmp21_ = NULL;
				gchar* _tmp22_ = NULL;
				gchar* _tmp23_ = NULL;
				gchar* _tmp24_ = NULL;
				SeahorseGkrKeyring* _tmp25_ = NULL;
				SeahorseGkrKeyring* _tmp26_ = NULL;
				gboolean _tmp27_ = FALSE;
				gboolean _tmp28_ = FALSE;
				_tmp17_ = store;
				gtk_list_store_append (_tmp17_, &_tmp18_);
				iter = _tmp18_;
				_tmp19_ = store;
				_tmp20_ = iter;
				_tmp21_ = keyring;
				_tmp22_ = seahorse_place_get_label ((SeahorsePlace*) _tmp21_);
				_tmp23_ = _tmp22_;
				_tmp24_ = _tmp23_;
				_tmp25_ = keyring;
				gtk_list_store_set (_tmp19_, &_tmp20_, 0, _tmp24_, 1, _tmp25_, -1, -1);
				_g_free0 (_tmp24_);
				_tmp26_ = keyring;
				_tmp27_ = seahorse_gkr_keyring_get_is_default (_tmp26_);
				_tmp28_ = _tmp27_;
				if (_tmp28_) {
					GtkComboBox* _tmp29_ = NULL;
					GtkTreeIter _tmp30_ = {0};
					_tmp29_ = _data2_->combo;
					_tmp30_ = iter;
					gtk_combo_box_set_active_iter (_tmp29_, &_tmp30_);
				}
			}
		}
		_g_list_free0 (keyring_collection);
	}
	_tmp31_ = builder;
	_tmp32_ = gtk_builder_get_object (_tmp31_, "item-label");
	_tmp33_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp32_, gtk_entry_get_type (), GtkEntry));
	_data2_->label = _tmp33_;
	gtk_dialog_set_response_sensitive ((GtkDialog*) self, (gint) GTK_RESPONSE_ACCEPT, FALSE);
	_tmp34_ = _data2_->label;
	g_signal_connect_data ((GtkEditable*) _tmp34_, "changed", (GCallback) __seahorse_gkr_item_add___lambda10__gtk_editable_changed, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	_tmp35_ = builder;
	_tmp36_ = gtk_builder_get_object (_tmp35_, "password-area");
	_tmp37_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp36_, gtk_container_get_type (), GtkContainer));
	area = _tmp37_;
	_tmp38_ = (GcrSecureEntryBuffer*) gcr_secure_entry_buffer_new ();
	buffer = _tmp38_;
	_tmp39_ = buffer;
	_tmp40_ = (GtkEntry*) gtk_entry_new_with_buffer ((GtkEntryBuffer*) _tmp39_);
	g_object_ref_sink (_tmp40_);
	_data2_->entry = _tmp40_;
	_tmp41_ = _data2_->entry;
	gtk_entry_set_visibility (_tmp41_, FALSE);
	_tmp42_ = area;
	_tmp43_ = _data2_->entry;
	gtk_container_add (_tmp42_, (GtkWidget*) _tmp43_);
	_tmp44_ = _data2_->entry;
	gtk_widget_show ((GtkWidget*) _tmp44_);
	_tmp45_ = builder;
	_tmp46_ = gtk_builder_get_object (_tmp45_, "show-password");
	_tmp47_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp46_, gtk_toggle_button_get_type (), GtkToggleButton));
	_data2_->check = _tmp47_;
	_tmp48_ = _data2_->check;
	g_signal_connect_data (_tmp48_, "toggled", (GCallback) __seahorse_gkr_item_add___lambda11__gtk_toggle_button_toggled, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	g_signal_connect_data ((GtkDialog*) self, "response", (GCallback) __seahorse_gkr_item_add___lambda12__gtk_dialog_response, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	_g_object_unref0 (buffer);
	_g_object_unref0 (area);
	_g_object_unref0 (cell);
	_g_object_unref0 (store);
	_g_object_unref0 (builder);
	block2_data_unref (_data2_);
	_data2_ = NULL;
	return obj;
}


static void seahorse_gkr_item_add_class_init (SeahorseGkrItemAddClass * klass) {
	seahorse_gkr_item_add_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->constructor = seahorse_gkr_item_add_constructor;
}


static void seahorse_gkr_item_add_instance_init (SeahorseGkrItemAdd * self) {
}


GType seahorse_gkr_item_add_get_type (void) {
	static volatile gsize seahorse_gkr_item_add_type_id__volatile = 0;
	if (g_once_init_enter (&seahorse_gkr_item_add_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SeahorseGkrItemAddClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) seahorse_gkr_item_add_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SeahorseGkrItemAdd), 0, (GInstanceInitFunc) seahorse_gkr_item_add_instance_init, NULL };
		GType seahorse_gkr_item_add_type_id;
		seahorse_gkr_item_add_type_id = g_type_register_static (gtk_dialog_get_type (), "SeahorseGkrItemAdd", &g_define_type_info, 0);
		g_once_init_leave (&seahorse_gkr_item_add_type_id__volatile, seahorse_gkr_item_add_type_id);
	}
	return seahorse_gkr_item_add_type_id__volatile;
}



