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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    © 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>
#include <glib/gi18n-lib.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 DEJA_DUP_TYPE_CONFIG_DELETE (deja_dup_config_delete_get_type ())
#define DEJA_DUP_CONFIG_DELETE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_CONFIG_DELETE, DejaDupConfigDelete))
#define DEJA_DUP_CONFIG_DELETE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_CONFIG_DELETE, DejaDupConfigDeleteClass))
#define DEJA_DUP_IS_CONFIG_DELETE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_CONFIG_DELETE))
#define DEJA_DUP_IS_CONFIG_DELETE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_CONFIG_DELETE))
#define DEJA_DUP_CONFIG_DELETE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_CONFIG_DELETE, DejaDupConfigDeleteClass))

typedef struct _DejaDupConfigDelete DejaDupConfigDelete;
typedef struct _DejaDupConfigDeleteClass DejaDupConfigDeleteClass;
typedef struct _DejaDupConfigDeletePrivate DejaDupConfigDeletePrivate;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (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);
};

struct _DejaDupConfigDelete {
	DejaDupConfigChoice parent_instance;
	DejaDupConfigDeletePrivate * priv;
};

struct _DejaDupConfigDeleteClass {
	DejaDupConfigChoiceClass parent_class;
};


static gpointer deja_dup_config_delete_parent_class = NULL;

GType deja_dup_config_widget_get_type (void);
GType deja_dup_config_choice_get_type (void);
GType deja_dup_config_delete_get_type (void);
enum  {
	DEJA_DUP_CONFIG_DELETE_DUMMY_PROPERTY
};
DejaDupConfigDelete* deja_dup_config_delete_new (const char* key);
DejaDupConfigDelete* deja_dup_config_delete_construct (GType object_type, const char* key);
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_delete_real_handle_changed (DejaDupConfigChoice* base);
static void deja_dup_config_delete_real_set_from_config (DejaDupConfigWidget* base);
void deja_dup_config_choice_init (DejaDupConfigChoice* self, GtkTreeModel* model, gint gconf_col);
static GObject * deja_dup_config_delete_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);



DejaDupConfigDelete* deja_dup_config_delete_construct (GType object_type, const char* key) {
	DejaDupConfigDelete * self;
	g_return_val_if_fail (key != NULL, NULL);
	self = (DejaDupConfigDelete*) g_object_new (object_type, "key", key, NULL);
	return self;
}


DejaDupConfigDelete* deja_dup_config_delete_new (const char* key) {
	return deja_dup_config_delete_construct (DEJA_DUP_TYPE_CONFIG_DELETE, key);
}


static void deja_dup_config_delete_real_handle_changed (DejaDupConfigChoice* base) {
	DejaDupConfigDelete * self;
	GError * _inner_error_;
	GValue* val;
	gint _tmp0_ = 0;
	gint intval;
	char* _tmp1_;
	self = (DejaDupConfigDelete*) base;
	_inner_error_ = NULL;
	val = deja_dup_config_choice_get_current_value ((DejaDupConfigChoice*) self);
	if (val == NULL) {
		_tmp0_ = 0;
	} else {
		_tmp0_ = g_value_get_int (val);
	}
	intval = _tmp0_;
	if (intval == G_MAXINT) {
		intval = 0;
	}
	{
		gconf_client_set_int (((DejaDupConfigWidget*) self)->client, deja_dup_config_widget_get_key ((DejaDupConfigWidget*) self), intval, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch5_g_error;
		}
	}
	goto __finally5;
	__catch5_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("ConfigDelete.vala:60: %s\n", e->message);
			_g_error_free0 (e);
		}
	}
	__finally5:
	if (_inner_error_ != NULL) {
		_g_free0 (val);
		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 ((DejaDupConfigChoice*) self, "choice-changed", _tmp1_ = g_strdup_printf ("%i", intval));
	_g_free0 (_tmp1_);
	_g_free0 (val);
}


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


static void deja_dup_config_delete_real_set_from_config (DejaDupConfigWidget* base) {
	DejaDupConfigDelete * self;
	GError * _inner_error_;
	gint confval = 0;
	gboolean valid = FALSE;
	GtkTreeIter iter = {0};
	self = (DejaDupConfigDelete*) base;
	_inner_error_ = NULL;
	{
		gint _tmp0_;
		_tmp0_ = gconf_client_get_int (((DejaDupConfigWidget*) self)->client, deja_dup_config_widget_get_key ((DejaDupConfigWidget*) self), &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch6_g_error;
		}
		confval = _tmp0_;
	}
	goto __finally6;
	__catch6_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("ConfigDelete.vala:73: %s\n", e->message);
			_g_error_free0 (e);
			return;
		}
	}
	__finally6:
	if (_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;
	}
	if (confval <= 0) {
		confval = G_MAXINT;
	}
	valid = gtk_tree_model_get_iter_first (gtk_combo_box_get_model (((DejaDupConfigChoice*) self)->combo), &iter);
	while (TRUE) {
		GValue val = {0};
		GValue _tmp2_;
		GValue _tmp1_ = {0};
		gint intval;
		if (!valid) {
			break;
		}
		gtk_tree_model_get_value (gtk_combo_box_get_model (((DejaDupConfigChoice*) self)->combo), &iter, ((DejaDupConfigChoice*) self)->gconf_col, &_tmp1_);
		val = (_tmp2_ = _tmp1_, G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL, _tmp2_);
		intval = g_value_get_int (&val);
		if (intval == confval) {
			gtk_combo_box_set_active_iter (((DejaDupConfigChoice*) self)->combo, &iter);
			G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
			break;
		}
		valid = gtk_tree_model_iter_next (gtk_combo_box_get_model (((DejaDupConfigChoice*) self)->combo), &iter);
		G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
	}
	if (!valid) {
		GtkListStore* store;
		char* _tmp3_;
		store = _g_object_ref0 (GTK_LIST_STORE (gtk_combo_box_get_model (((DejaDupConfigChoice*) self)->combo)));
		gtk_list_store_insert_with_values (store, &iter, 0, 0, _tmp3_ = g_strdup_printf (ngettext ("At least %d day", "At least %d days", (gulong) confval), confval), 1, confval, -1);
		_g_free0 (_tmp3_);
		gtk_combo_box_set_active_iter (((DejaDupConfigChoice*) self)->combo, &iter);
		_g_object_unref0 (store);
	}
}


static GObject * deja_dup_config_delete_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupConfigDelete * self;
	parent_class = G_OBJECT_CLASS (deja_dup_config_delete_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DEJA_DUP_CONFIG_DELETE (obj);
	{
		GtkListStore* store;
		GtkTreeIter iter = {0};
		gint i;
		store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT, NULL);
		i = 0;
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least a week"), 1, 7, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least a month"), 1, 28, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least two months"), 1, 28 * 2, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least three months"), 1, 28 * 3, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least six months"), 1, 365 / 2, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("At least a year"), 1, 365, -1);
		gtk_list_store_insert_with_values (store, &iter, i++, 0, _ ("Forever"), 1, G_MAXINT, -1);
		gtk_tree_sortable_set_sort_column_id ((GtkTreeSortable*) store, 1, GTK_SORT_ASCENDING);
		deja_dup_config_choice_init ((DejaDupConfigChoice*) self, (GtkTreeModel*) store, 1);
		_g_object_unref0 (store);
	}
	return obj;
}


static void deja_dup_config_delete_class_init (DejaDupConfigDeleteClass * klass) {
	deja_dup_config_delete_parent_class = g_type_class_peek_parent (klass);
	DEJA_DUP_CONFIG_CHOICE_CLASS (klass)->handle_changed = deja_dup_config_delete_real_handle_changed;
	DEJA_DUP_CONFIG_WIDGET_CLASS (klass)->set_from_config = deja_dup_config_delete_real_set_from_config;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_config_delete_constructor;
}


static void deja_dup_config_delete_instance_init (DejaDupConfigDelete * self) {
}


GType deja_dup_config_delete_get_type (void) {
	static volatile gsize deja_dup_config_delete_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_config_delete_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupConfigDeleteClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_config_delete_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupConfigDelete), 0, (GInstanceInitFunc) deja_dup_config_delete_instance_init, NULL };
		GType deja_dup_config_delete_type_id;
		deja_dup_config_delete_type_id = g_type_register_static (DEJA_DUP_TYPE_CONFIG_CHOICE, "DejaDupConfigDelete", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_config_delete_type_id__volatile, deja_dup_config_delete_type_id);
	}
	return deja_dup_config_delete_type_id__volatile;
}




