/* ConfigChoice.c generated by valac, 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.
    © 2008,2009 Michael Terry <mike@mterry.name>

    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 <gconf/gconf-client.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 _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _DejaDupConfigWidget {
	GtkEventBox parent_instance;
	DejaDupConfigWidgetPrivate * priv;
	GConfClient* client;
};

struct _DejaDupConfigWidgetClass {
	GtkEventBoxClass parent_class;
	void (*set_from_config) (DejaDupConfigWidget* self);
};

struct _DejaDupConfigChoice {
	DejaDupConfigWidget parent_instance;
	DejaDupConfigChoicePrivate * priv;
	GtkComboBox* combo;
	char* default_val;
	gint gconf_col;
};

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


static gpointer deja_dup_config_choice_parent_class = NULL;

GType deja_dup_config_widget_get_type (void);
GType deja_dup_config_choice_get_type (void);
enum  {
	DEJA_DUP_CONFIG_CHOICE_DUMMY_PROPERTY
};
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);
void deja_dup_config_widget_set_from_config (DejaDupConfigWidget* self);
void deja_dup_config_choice_init (DejaDupConfigChoice* self, GtkTreeModel* model, gint gconf_col);
static GValue* _g_value_dup (GValue* self);
GValue* deja_dup_config_choice_get_current_value (DejaDupConfigChoice* self);
const char* deja_dup_config_widget_get_key (DejaDupConfigWidget* self);
static void deja_dup_config_choice_real_handle_changed (DejaDupConfigChoice* self);
static void deja_dup_config_choice_real_set_from_config (DejaDupConfigWidget* base);
DejaDupConfigChoice* deja_dup_config_choice_new (void);
DejaDupConfigChoice* deja_dup_config_choice_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 int _vala_strcmp0 (const char * str1, const char * str2);



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 gconf_col) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (model != NULL);
	gtk_combo_box_set_model (self->combo, model);
	self->gconf_col = gconf_col;
	g_signal_connect_object (self->combo, "changed", (GCallback) _deja_dup_config_choice_handle_changed_gtk_combo_box_changed, self, 0);
	deja_dup_config_widget_set_from_config ((DejaDupConfigWidget*) self);
}


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};
	g_return_val_if_fail (self != NULL, NULL);
	if (gtk_combo_box_get_active_iter (self->combo, &iter)) {
		GValue val = {0};
		GValue _tmp1_;
		GValue _tmp0_ = {0};
		GValue _tmp3_;
		GValue _tmp2_;
		GValue* _tmp4_;
		gtk_tree_model_get_value (gtk_combo_box_get_model (self->combo), &iter, self->gconf_col, &_tmp0_);
		val = (_tmp1_ = _tmp0_, G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL, _tmp1_);
		result = (_tmp4_ = __g_value_dup0 ((_tmp3_ = _tmp2_ = val, &_tmp3_)), G_IS_VALUE (&_tmp2_) ? (g_value_unset (&_tmp2_), NULL) : NULL, _tmp4_);
		return result;
		G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
	}
	result = NULL;
	return result;
}


static void deja_dup_config_choice_real_handle_changed (DejaDupConfigChoice* self) {
	GError * _inner_error_;
	GValue* val;
	const char* _tmp0_;
	char* strval;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	val = deja_dup_config_choice_get_current_value (self);
	_tmp0_ = NULL;
	if (val == NULL) {
		_tmp0_ = "";
	} else {
		_tmp0_ = g_value_get_string (val);
	}
	strval = g_strdup (_tmp0_);
	{
		gconf_client_set_string (((DejaDupConfigWidget*) self)->client, deja_dup_config_widget_get_key ((DejaDupConfigWidget*) self), strval, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch3_g_error;
		}
	}
	goto __finally3;
	__catch3_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("ConfigChoice.vala:67: %s\n", e->message);
			_g_error_free0 (e);
		}
	}
	__finally3:
	if (_inner_error_ != NULL) {
		_g_free0 (val);
		_g_free0 (strval);
		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;
	}
	g_signal_emit_by_name (self, "choice-changed", strval);
	_g_free0 (val);
	_g_free0 (strval);
}


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 (DejaDupConfigWidget* base) {
	DejaDupConfigChoice * self;
	GError * _inner_error_;
	char* confval;
	gboolean _tmp2_ = FALSE;
	gboolean valid = FALSE;
	GtkTreeIter iter = {0};
	self = (DejaDupConfigChoice*) base;
	_inner_error_ = NULL;
	confval = NULL;
	{
		char* _tmp0_;
		char* _tmp1_;
		_tmp0_ = gconf_client_get_string (((DejaDupConfigWidget*) self)->client, deja_dup_config_widget_get_key ((DejaDupConfigWidget*) self), &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch4_g_error;
		}
		confval = (_tmp1_ = _tmp0_, _g_free0 (confval), _tmp1_);
	}
	goto __finally4;
	__catch4_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("ConfigChoice.vala:80: %s\n", e->message);
			_g_error_free0 (e);
		}
	}
	__finally4:
	if (_inner_error_ != NULL) {
		_g_free0 (confval);
		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;
	}
	if (confval == NULL) {
		_tmp2_ = TRUE;
	} else {
		_tmp2_ = _vala_strcmp0 (confval, "") == 0;
	}
	if (_tmp2_) {
		char* _tmp3_;
		confval = (_tmp3_ = g_strdup (self->default_val), _g_free0 (confval), _tmp3_);
	}
	if (confval == NULL) {
		_g_free0 (confval);
		return;
	}
	valid = gtk_tree_model_get_iter_first (gtk_combo_box_get_model (self->combo), &iter);
	while (TRUE) {
		GValue val = {0};
		GValue _tmp5_;
		GValue _tmp4_ = {0};
		char* strval;
		if (!valid) {
			break;
		}
		gtk_tree_model_get_value (gtk_combo_box_get_model (self->combo), &iter, self->gconf_col, &_tmp4_);
		val = (_tmp5_ = _tmp4_, G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL, _tmp5_);
		strval = g_strdup (g_value_get_string (&val));
		if (_vala_strcmp0 (strval, confval) == 0) {
			gtk_combo_box_set_active_iter (self->combo, &iter);
			G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
			_g_free0 (strval);
			break;
		}
		valid = gtk_tree_model_iter_next (gtk_combo_box_get_model (self->combo), &iter);
		G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
		_g_free0 (strval);
	}
	_g_free0 (confval);
}


DejaDupConfigChoice* deja_dup_config_choice_construct (GType object_type) {
	DejaDupConfigChoice * self;
	self = g_object_newv (object_type, 0, NULL);
	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;
	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);
	{
		GtkComboBox* _tmp0_;
		self->combo = (_tmp0_ = g_object_ref_sink ((GtkComboBox*) gtk_combo_box_new_text ()), _g_object_unref0 (self->combo), _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;
	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;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}




