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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    For copyright information, see AUTHORS.

    Déjà Dup 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 3 of the License, or
    (at your option) any later version.

    Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <common.h>
#include <stdlib.h>
#include <string.h>


#define DEJA_DUP_TYPE_CONFIG_WIDGET (deja_dup_config_widget_get_type ())
#define DEJA_DUP_CONFIG_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_CONFIG_WIDGET, DejaDupConfigWidget))
#define DEJA_DUP_CONFIG_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_CONFIG_WIDGET, DejaDupConfigWidgetClass))
#define DEJA_DUP_IS_CONFIG_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_CONFIG_WIDGET))
#define DEJA_DUP_IS_CONFIG_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_CONFIG_WIDGET))
#define DEJA_DUP_CONFIG_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_CONFIG_WIDGET, DejaDupConfigWidgetClass))

typedef struct _DejaDupConfigWidget DejaDupConfigWidget;
typedef struct _DejaDupConfigWidgetClass DejaDupConfigWidgetClass;
typedef struct _DejaDupConfigWidgetPrivate DejaDupConfigWidgetPrivate;

#define DEJA_DUP_TYPE_CONFIG_CHOICE (deja_dup_config_choice_get_type ())
#define DEJA_DUP_CONFIG_CHOICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_CONFIG_CHOICE, DejaDupConfigChoice))
#define DEJA_DUP_CONFIG_CHOICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_CONFIG_CHOICE, DejaDupConfigChoiceClass))
#define DEJA_DUP_IS_CONFIG_CHOICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_CONFIG_CHOICE))
#define DEJA_DUP_IS_CONFIG_CHOICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_CONFIG_CHOICE))
#define DEJA_DUP_CONFIG_CHOICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_CONFIG_CHOICE, DejaDupConfigChoiceClass))

typedef struct _DejaDupConfigChoice DejaDupConfigChoice;
typedef struct _DejaDupConfigChoiceClass DejaDupConfigChoiceClass;
typedef struct _DejaDupConfigChoicePrivate DejaDupConfigChoicePrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define __vala_GValue_free0(var) ((var == NULL) ? NULL : (var = (_vala_GValue_free (var), NULL)))
typedef struct _DejaDupConfigChoiceSetFromConfigData DejaDupConfigChoiceSetFromConfigData;

struct _DejaDupConfigWidget {
	GtkEventBox parent_instance;
	DejaDupConfigWidgetPrivate * priv;
	gboolean syncing;
	DejaDupSimpleSettings* settings;
	GList* all_settings;
};

struct _DejaDupConfigWidgetClass {
	GtkEventBoxClass parent_class;
	void (*set_from_config) (DejaDupConfigWidget* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*set_from_config_finish) (DejaDupConfigWidget* self, GAsyncResult* _res_);
};

struct _DejaDupConfigChoice {
	DejaDupConfigWidget parent_instance;
	DejaDupConfigChoicePrivate * priv;
	GtkComboBox* combo;
	gchar* default_val;
	gint settings_col;
};

struct _DejaDupConfigChoiceClass {
	DejaDupConfigWidgetClass parent_class;
	void (*handle_changed) (DejaDupConfigChoice* self);
};

struct _DejaDupConfigChoiceSetFromConfigData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	DejaDupConfigChoice* self;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* confval;
	gboolean _tmp2_;
	gchar* _tmp3_;
	gboolean valid;
	GtkTreeIter iter;
	GtkTreeModel* _tmp4_;
	GtkTreeIter _tmp5_;
	gboolean _tmp6_;
	GValue val;
	GtkTreeModel* _tmp7_;
	GValue _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* strval;
	GtkTreeModel* _tmp11_;
	gboolean _tmp12_;
};


static gpointer deja_dup_config_choice_parent_class = NULL;

GType deja_dup_config_widget_get_type (void) G_GNUC_CONST;
GType deja_dup_config_choice_get_type (void) G_GNUC_CONST;
enum  {
	DEJA_DUP_CONFIG_CHOICE_DUMMY_PROPERTY
};
void deja_dup_config_choice_init (DejaDupConfigChoice* self, GtkTreeModel* model, gint settings_col);
void deja_dup_config_widget_set_from_config (DejaDupConfigWidget* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
void deja_dup_config_widget_set_from_config_finish (DejaDupConfigWidget* self, GAsyncResult* _res_);
void deja_dup_config_choice_handle_changed (DejaDupConfigChoice* self);
static void _deja_dup_config_choice_handle_changed_gtk_combo_box_changed (GtkComboBox* _sender, gpointer self);
GValue* deja_dup_config_choice_get_current_value (DejaDupConfigChoice* self);
static GValue* _g_value_dup (GValue* self);
static void deja_dup_config_choice_real_handle_changed (DejaDupConfigChoice* self);
const gchar* deja_dup_config_widget_get_key (DejaDupConfigWidget* self);
static void _vala_GValue_free (GValue* self);
static void deja_dup_config_choice_real_set_from_config_data_free (gpointer _data);
static void deja_dup_config_choice_real_set_from_config (DejaDupConfigWidget* base, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean deja_dup_config_choice_real_set_from_config_co (DejaDupConfigChoiceSetFromConfigData* data);
DejaDupConfigChoice* deja_dup_config_choice_new (void);
DejaDupConfigChoice* deja_dup_config_choice_construct (GType object_type);
DejaDupConfigWidget* deja_dup_config_widget_construct (GType object_type);
static GObject * deja_dup_config_choice_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void deja_dup_config_choice_finalize (GObject* obj);


static void _deja_dup_config_choice_handle_changed_gtk_combo_box_changed (GtkComboBox* _sender, gpointer self) {
	deja_dup_config_choice_handle_changed (self);
}


void deja_dup_config_choice_init (DejaDupConfigChoice* self, GtkTreeModel* model, gint settings_col) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (model != NULL);
	gtk_combo_box_set_model (self->combo, model);
	self->settings_col = settings_col;
	deja_dup_config_widget_set_from_config ((DejaDupConfigWidget*) self, NULL, NULL);
	g_signal_connect_object (self->combo, "changed", (GCallback) _deja_dup_config_choice_handle_changed_gtk_combo_box_changed, self, 0);
}


static GValue* _g_value_dup (GValue* self) {
	return g_boxed_copy (G_TYPE_VALUE, self);
}


static gpointer __g_value_dup0 (gpointer self) {
	return self ? _g_value_dup (self) : NULL;
}


GValue* deja_dup_config_choice_get_current_value (DejaDupConfigChoice* self) {
	GValue* result = NULL;
	GtkTreeIter iter = {0};
	GtkTreeIter _tmp0_ = {0};
	gboolean _tmp1_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp1_ = gtk_combo_box_get_active_iter (self->combo, &_tmp0_);
	iter = _tmp0_;
	if (_tmp1_) {
		GValue val = {0};
		GtkTreeModel* _tmp2_ = NULL;
		GValue _tmp3_ = {0};
		GValue _tmp4_;
		GValue* _tmp5_;
		GValue* _tmp6_;
		_tmp2_ = gtk_combo_box_get_model (self->combo);
		gtk_tree_model_get_value (_tmp2_, &iter, self->settings_col, &_tmp3_);
		G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
		val = _tmp3_;
		_tmp4_ = val;
		_tmp5_ = __g_value_dup0 (&_tmp4_);
		_tmp6_ = _tmp5_;
		G_IS_VALUE (&_tmp4_) ? (g_value_unset (&_tmp4_), NULL) : NULL;
		result = _tmp6_;
		return result;
	}
	result = NULL;
	return result;
}


static void _vala_GValue_free (GValue* self) {
	g_value_unset (self);
	g_free (self);
}


static void deja_dup_config_choice_real_handle_changed (DejaDupConfigChoice* self) {
	GValue* _tmp0_ = NULL;
	GValue* val;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp3_;
	gchar* strval;
	const gchar* _tmp4_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = deja_dup_config_choice_get_current_value (self);
	val = _tmp0_;
	if (val == NULL) {
		_tmp1_ = "";
	} else {
		const gchar* _tmp2_ = NULL;
		_tmp2_ = g_value_get_string (val);
		_tmp1_ = _tmp2_;
	}
	_tmp3_ = g_strdup (_tmp1_);
	strval = _tmp3_;
	_tmp4_ = deja_dup_config_widget_get_key ((DejaDupConfigWidget*) self);
	deja_dup_simple_settings_set_string (((DejaDupConfigWidget*) self)->settings, _tmp4_, strval);
	g_signal_emit_by_name (self, "choice-changed", strval);
	_g_free0 (strval);
	__vala_GValue_free0 (val);
}


void deja_dup_config_choice_handle_changed (DejaDupConfigChoice* self) {
	DEJA_DUP_CONFIG_CHOICE_GET_CLASS (self)->handle_changed (self);
}


static void deja_dup_config_choice_real_set_from_config_data_free (gpointer _data) {
	DejaDupConfigChoiceSetFromConfigData* data;
	data = _data;
	_g_object_unref0 (data->self);
	g_slice_free (DejaDupConfigChoiceSetFromConfigData, data);
}


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


static void deja_dup_config_choice_real_set_from_config (DejaDupConfigWidget* base, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	DejaDupConfigChoice * self;
	DejaDupConfigChoiceSetFromConfigData* _data_;
	self = (DejaDupConfigChoice*) base;
	_data_ = g_slice_new0 (DejaDupConfigChoiceSetFromConfigData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, deja_dup_config_choice_real_set_from_config);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, deja_dup_config_choice_real_set_from_config_data_free);
	_data_->self = _g_object_ref0 (self);
	deja_dup_config_choice_real_set_from_config_co (_data_);
}


static void deja_dup_config_choice_real_set_from_config_finish (DejaDupConfigWidget* base, GAsyncResult* _res_) {
	DejaDupConfigChoiceSetFromConfigData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static gboolean deja_dup_config_choice_real_set_from_config_co (DejaDupConfigChoiceSetFromConfigData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	data->_tmp0_ = NULL;
	data->_tmp0_ = deja_dup_config_widget_get_key ((DejaDupConfigWidget*) data->self);
	data->_tmp1_ = NULL;
	data->_tmp1_ = g_settings_get_string ((GSettings*) ((DejaDupConfigWidget*) data->self)->settings, data->_tmp0_);
	data->confval = data->_tmp1_;
	if (data->confval == NULL) {
		data->_tmp2_ = TRUE;
	} else {
		data->_tmp2_ = g_strcmp0 (data->confval, "") == 0;
	}
	if (data->_tmp2_) {
		data->_tmp3_ = g_strdup (data->self->default_val);
		_g_free0 (data->confval);
		data->confval = data->_tmp3_;
	}
	if (data->confval == NULL) {
		_g_free0 (data->confval);
		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;
	}
	data->_tmp4_ = NULL;
	data->_tmp4_ = gtk_combo_box_get_model (data->self->combo);
	memset (&data->_tmp5_, 0, sizeof (GtkTreeIter));
	data->_tmp6_ = gtk_tree_model_get_iter_first (data->_tmp4_, &data->_tmp5_);
	data->iter = data->_tmp5_;
	data->valid = data->_tmp6_;
	while (TRUE) {
		if (!data->valid) {
			break;
		}
		data->_tmp7_ = NULL;
		data->_tmp7_ = gtk_combo_box_get_model (data->self->combo);
		memset (&data->_tmp8_, 0, sizeof (GValue));
		gtk_tree_model_get_value (data->_tmp7_, &data->iter, data->self->settings_col, &data->_tmp8_);
		G_IS_VALUE (&data->val) ? (g_value_unset (&data->val), NULL) : NULL;
		data->val = data->_tmp8_;
		data->_tmp9_ = NULL;
		data->_tmp9_ = g_value_get_string (&data->val);
		data->_tmp10_ = g_strdup (data->_tmp9_);
		data->strval = data->_tmp10_;
		if (g_strcmp0 (data->strval, data->confval) == 0) {
			gtk_combo_box_set_active_iter (data->self->combo, &data->iter);
			_g_free0 (data->strval);
			G_IS_VALUE (&data->val) ? (g_value_unset (&data->val), NULL) : NULL;
			break;
		}
		data->_tmp11_ = NULL;
		data->_tmp11_ = gtk_combo_box_get_model (data->self->combo);
		data->_tmp12_ = gtk_tree_model_iter_next (data->_tmp11_, &data->iter);
		data->valid = data->_tmp12_;
		_g_free0 (data->strval);
		G_IS_VALUE (&data->val) ? (g_value_unset (&data->val), NULL) : NULL;
	}
	_g_free0 (data->confval);
	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;
}


DejaDupConfigChoice* deja_dup_config_choice_construct (GType object_type) {
	DejaDupConfigChoice * self = NULL;
	self = (DejaDupConfigChoice*) deja_dup_config_widget_construct (object_type);
	return self;
}


DejaDupConfigChoice* deja_dup_config_choice_new (void) {
	return deja_dup_config_choice_construct (DEJA_DUP_TYPE_CONFIG_CHOICE);
}


static GObject * deja_dup_config_choice_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupConfigChoice * self;
	GtkComboBoxText* _tmp0_ = NULL;
	parent_class = G_OBJECT_CLASS (deja_dup_config_choice_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DEJA_DUP_CONFIG_CHOICE (obj);
	_tmp0_ = (GtkComboBoxText*) gtk_combo_box_text_new ();
	_g_object_unref0 (self->combo);
	self->combo = (GtkComboBox*) g_object_ref_sink (_tmp0_);
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) self->combo);
	return obj;
}


static void deja_dup_config_choice_class_init (DejaDupConfigChoiceClass * klass) {
	deja_dup_config_choice_parent_class = g_type_class_peek_parent (klass);
	DEJA_DUP_CONFIG_CHOICE_CLASS (klass)->handle_changed = deja_dup_config_choice_real_handle_changed;
	DEJA_DUP_CONFIG_WIDGET_CLASS (klass)->set_from_config = deja_dup_config_choice_real_set_from_config;
	DEJA_DUP_CONFIG_WIDGET_CLASS (klass)->set_from_config_finish = deja_dup_config_choice_real_set_from_config_finish;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_config_choice_constructor;
	G_OBJECT_CLASS (klass)->finalize = deja_dup_config_choice_finalize;
	g_signal_new ("choice_changed", DEJA_DUP_TYPE_CONFIG_CHOICE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
}


static void deja_dup_config_choice_instance_init (DejaDupConfigChoice * self) {
	self->default_val = NULL;
}


static void deja_dup_config_choice_finalize (GObject* obj) {
	DejaDupConfigChoice * self;
	self = DEJA_DUP_CONFIG_CHOICE (obj);
	_g_object_unref0 (self->combo);
	_g_free0 (self->default_val);
	G_OBJECT_CLASS (deja_dup_config_choice_parent_class)->finalize (obj);
}


GType deja_dup_config_choice_get_type (void) {
	static volatile gsize deja_dup_config_choice_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_config_choice_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupConfigChoiceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_config_choice_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupConfigChoice), 0, (GInstanceInitFunc) deja_dup_config_choice_instance_init, NULL };
		GType deja_dup_config_choice_type_id;
		deja_dup_config_choice_type_id = g_type_register_static (DEJA_DUP_TYPE_CONFIG_WIDGET, "DejaDupConfigChoice", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_config_choice_type_id__volatile, deja_dup_config_choice_type_id);
	}
	return deja_dup_config_choice_type_id__volatile;
}



