/* ConfigLabelPolicy.c generated by valac 0.12.1, the Vala compiler
 * generated from ConfigLabelPolicy.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>
#include <glib/gi18n-lib.h>
#include <config.h>
#include <pango/pango.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_LABEL (deja_dup_config_label_get_type ())
#define DEJA_DUP_CONFIG_LABEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_CONFIG_LABEL, DejaDupConfigLabel))
#define DEJA_DUP_CONFIG_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_CONFIG_LABEL, DejaDupConfigLabelClass))
#define DEJA_DUP_IS_CONFIG_LABEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_CONFIG_LABEL))
#define DEJA_DUP_IS_CONFIG_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_CONFIG_LABEL))
#define DEJA_DUP_CONFIG_LABEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_CONFIG_LABEL, DejaDupConfigLabelClass))

typedef struct _DejaDupConfigLabel DejaDupConfigLabel;
typedef struct _DejaDupConfigLabelClass DejaDupConfigLabelClass;
typedef struct _DejaDupConfigLabelPrivate DejaDupConfigLabelPrivate;

#define DEJA_DUP_TYPE_CONFIG_LABEL_POLICY (deja_dup_config_label_policy_get_type ())
#define DEJA_DUP_CONFIG_LABEL_POLICY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_CONFIG_LABEL_POLICY, DejaDupConfigLabelPolicy))
#define DEJA_DUP_CONFIG_LABEL_POLICY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_CONFIG_LABEL_POLICY, DejaDupConfigLabelPolicyClass))
#define DEJA_DUP_IS_CONFIG_LABEL_POLICY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_CONFIG_LABEL_POLICY))
#define DEJA_DUP_IS_CONFIG_LABEL_POLICY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_CONFIG_LABEL_POLICY))
#define DEJA_DUP_CONFIG_LABEL_POLICY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_CONFIG_LABEL_POLICY, DejaDupConfigLabelPolicyClass))

typedef struct _DejaDupConfigLabelPolicy DejaDupConfigLabelPolicy;
typedef struct _DejaDupConfigLabelPolicyClass DejaDupConfigLabelPolicyClass;
typedef struct _DejaDupConfigLabelPolicyPrivate DejaDupConfigLabelPolicyPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _DejaDupConfigLabelPolicySetFromConfigData DejaDupConfigLabelPolicySetFromConfigData;
#define _pango_attr_list_unref0(var) ((var == NULL) ? NULL : (var = (pango_attr_list_unref (var), NULL)))

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 _DejaDupConfigLabel {
	DejaDupConfigWidget parent_instance;
	DejaDupConfigLabelPrivate * priv;
	GtkBox* hbox;
	GtkLabel* label;
};

struct _DejaDupConfigLabelClass {
	DejaDupConfigWidgetClass parent_class;
	void (*fill_box) (DejaDupConfigLabel* self);
};

struct _DejaDupConfigLabelPolicy {
	DejaDupConfigLabel parent_instance;
	DejaDupConfigLabelPolicyPrivate * priv;
};

struct _DejaDupConfigLabelPolicyClass {
	DejaDupConfigLabelClass parent_class;
};

struct _DejaDupConfigLabelPolicySetFromConfigData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	DejaDupConfigLabelPolicy* self;
	DejaDupBackend* _tmp0_;
	DejaDupBackend* backend;
	gint _tmp1_;
	gint delete_after;
	gboolean _tmp2_;
	gboolean infinite;
	gchar* policy;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	const gchar* _tmp11_;
	gchar* _tmp12_;
	const gchar* _tmp13_;
	gchar* _tmp14_;
	const gchar* _tmp15_;
	gchar* _tmp16_;
	const gchar* _tmp17_;
	gchar* _tmp18_;
};


static gpointer deja_dup_config_label_policy_parent_class = NULL;
extern gint deja_dup_config_delete_FOREVER;

GType deja_dup_config_widget_get_type (void) G_GNUC_CONST;
GType deja_dup_config_label_get_type (void) G_GNUC_CONST;
GType deja_dup_config_label_policy_get_type (void) G_GNUC_CONST;
enum  {
	DEJA_DUP_CONFIG_LABEL_POLICY_DUMMY_PROPERTY
};
DejaDupConfigLabelPolicy* deja_dup_config_label_policy_new (void);
DejaDupConfigLabelPolicy* deja_dup_config_label_policy_construct (GType object_type);
static void deja_dup_config_label_policy_real_set_from_config_data_free (gpointer _data);
static void deja_dup_config_label_policy_real_set_from_config (DejaDupConfigWidget* base, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean deja_dup_config_label_policy_real_set_from_config_co (DejaDupConfigLabelPolicySetFromConfigData* data);
#define DEJA_DUP_CONFIG_DELETE_WEEKLY 7
#define DEJA_DUP_CONFIG_DELETE_MONTHLY 28
#define DEJA_DUP_CONFIG_DELETE_BIMONTHLY (28 * 2)
#define DEJA_DUP_CONFIG_DELETE_TRIMONTHLY (28 * 3)
#define DEJA_DUP_CONFIG_DELETE_SEMIANNUALLY (365 / 2)
#define DEJA_DUP_CONFIG_DELETE_ANNUALLY 365
static GObject * deja_dup_config_label_policy_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
void deja_dup_config_widget_watch_key (DejaDupConfigWidget* self, const gchar* key, DejaDupSimpleSettings* s);


DejaDupConfigLabelPolicy* deja_dup_config_label_policy_construct (GType object_type) {
	DejaDupConfigLabelPolicy * self = NULL;
	self = (DejaDupConfigLabelPolicy*) g_object_new (object_type, "key", NULL, NULL);
	return self;
}


DejaDupConfigLabelPolicy* deja_dup_config_label_policy_new (void) {
	return deja_dup_config_label_policy_construct (DEJA_DUP_TYPE_CONFIG_LABEL_POLICY);
}


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


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


static void deja_dup_config_label_policy_real_set_from_config (DejaDupConfigWidget* base, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	DejaDupConfigLabelPolicy * self;
	DejaDupConfigLabelPolicySetFromConfigData* _data_;
	self = (DejaDupConfigLabelPolicy*) base;
	_data_ = g_slice_new0 (DejaDupConfigLabelPolicySetFromConfigData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, deja_dup_config_label_policy_real_set_from_config);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, deja_dup_config_label_policy_real_set_from_config_data_free);
	_data_->self = _g_object_ref0 (self);
	deja_dup_config_label_policy_real_set_from_config_co (_data_);
}


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


static gboolean deja_dup_config_label_policy_real_set_from_config_co (DejaDupConfigLabelPolicySetFromConfigData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	data->_tmp0_ = NULL;
	data->_tmp0_ = deja_dup_backend_get_default ();
	data->backend = data->_tmp0_;
	data->_tmp1_ = g_settings_get_int ((GSettings*) ((DejaDupConfigWidget*) data->self)->settings, DEJA_DUP_DELETE_AFTER_KEY);
	data->delete_after = data->_tmp1_;
	data->_tmp2_ = deja_dup_backend_space_can_be_infinite (data->backend);
	data->infinite = data->_tmp2_;
	if (data->infinite) {
		gtk_label_set_label (((DejaDupConfigLabel*) data->self)->label, "");
		_g_object_unref0 (data->backend);
		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->delete_after <= 0) {
		data->delete_after = deja_dup_config_delete_FOREVER;
	}
	if (data->delete_after == DEJA_DUP_CONFIG_DELETE_WEEKLY) {
		data->_tmp3_ = NULL;
		data->_tmp3_ = _ ("Old backups will be kept for at least a week or until the backup locat" \
"ion is low on space.");
		data->_tmp4_ = g_strdup (data->_tmp3_);
		_g_free0 (data->policy);
		data->policy = data->_tmp4_;
	} else {
		if (data->delete_after == DEJA_DUP_CONFIG_DELETE_MONTHLY) {
			data->_tmp5_ = NULL;
			data->_tmp5_ = _ ("Old backups will be kept for at least a month or until the backup loca" \
"tion is low on space.");
			data->_tmp6_ = g_strdup (data->_tmp5_);
			_g_free0 (data->policy);
			data->policy = data->_tmp6_;
		} else {
			if (data->delete_after == DEJA_DUP_CONFIG_DELETE_BIMONTHLY) {
				data->_tmp7_ = NULL;
				data->_tmp7_ = _ ("Old backups will be kept for at least two months or until the backup l" \
"ocation is low on space.");
				data->_tmp8_ = g_strdup (data->_tmp7_);
				_g_free0 (data->policy);
				data->policy = data->_tmp8_;
			} else {
				if (data->delete_after == DEJA_DUP_CONFIG_DELETE_TRIMONTHLY) {
					data->_tmp9_ = NULL;
					data->_tmp9_ = _ ("Old backups will be kept for at least three months or until the backup" \
" location is low on space.");
					data->_tmp10_ = g_strdup (data->_tmp9_);
					_g_free0 (data->policy);
					data->policy = data->_tmp10_;
				} else {
					if (data->delete_after == DEJA_DUP_CONFIG_DELETE_SEMIANNUALLY) {
						data->_tmp11_ = NULL;
						data->_tmp11_ = _ ("Old backups will be kept for at least six months or until the backup l" \
"ocation is low on space.");
						data->_tmp12_ = g_strdup (data->_tmp11_);
						_g_free0 (data->policy);
						data->policy = data->_tmp12_;
					} else {
						if (data->delete_after == DEJA_DUP_CONFIG_DELETE_ANNUALLY) {
							data->_tmp13_ = NULL;
							data->_tmp13_ = _ ("Old backups will be kept for at least a year or until the backup locat" \
"ion is low on space.");
							data->_tmp14_ = g_strdup (data->_tmp13_);
							_g_free0 (data->policy);
							data->policy = data->_tmp14_;
						} else {
							if (data->delete_after == deja_dup_config_delete_FOREVER) {
								data->_tmp15_ = NULL;
								data->_tmp15_ = _ ("Old backups will be kept until the backup location is low on space.");
								data->_tmp16_ = g_strdup (data->_tmp15_);
								_g_free0 (data->policy);
								data->policy = data->_tmp16_;
							} else {
								data->_tmp17_ = NULL;
								data->_tmp17_ = g_dngettext (GETTEXT_PACKAGE, "Old backups will be kept at least %d day or until the backup location " \
"is low on space.", "Old backups will be kept at least %d days or until the backup location" \
" is low on space.", (gulong) data->delete_after);
								data->_tmp18_ = NULL;
								data->_tmp18_ = g_strdup_printf (data->_tmp17_, data->delete_after);
								_g_free0 (data->policy);
								data->policy = data->_tmp18_;
							}
						}
					}
				}
			}
		}
	}
	gtk_label_set_label (((DejaDupConfigLabel*) data->self)->label, data->policy);
	_g_free0 (data->policy);
	_g_object_unref0 (data->backend);
	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 GObject * deja_dup_config_label_policy_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupConfigLabelPolicy * self;
	DejaDupSimpleSettings* _tmp0_ = NULL;
	DejaDupSimpleSettings* _tmp1_;
	PangoAttrList* _tmp2_ = NULL;
	PangoAttrList* attrs;
	PangoAttribute* _tmp3_ = NULL;
	parent_class = G_OBJECT_CLASS (deja_dup_config_label_policy_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DEJA_DUP_CONFIG_LABEL_POLICY (obj);
	deja_dup_config_widget_watch_key ((DejaDupConfigWidget*) self, DEJA_DUP_BACKEND_KEY, NULL);
	_tmp0_ = deja_dup_get_settings (DEJA_DUP_FILE_ROOT);
	_tmp1_ = _tmp0_;
	deja_dup_config_widget_watch_key ((DejaDupConfigWidget*) self, DEJA_DUP_FILE_PATH_KEY, _tmp1_);
	_g_object_unref0 (_tmp1_);
	deja_dup_config_widget_watch_key ((DejaDupConfigWidget*) self, DEJA_DUP_DELETE_AFTER_KEY, NULL);
	_tmp2_ = pango_attr_list_new ();
	attrs = _tmp2_;
	_tmp3_ = pango_attr_style_new (PANGO_STYLE_ITALIC);
	pango_attr_list_insert (attrs, _tmp3_);
	gtk_label_set_attributes (((DejaDupConfigLabel*) self)->label, attrs);
	g_object_set (((DejaDupConfigLabel*) self)->label, "wrap", TRUE, NULL);
	gtk_label_set_max_width_chars (((DejaDupConfigLabel*) self)->label, 25);
	g_object_set ((GtkMisc*) ((DejaDupConfigLabel*) self)->label, "yalign", 1.0f, NULL);
	_pango_attr_list_unref0 (attrs);
	return obj;
}


static void deja_dup_config_label_policy_class_init (DejaDupConfigLabelPolicyClass * klass) {
	deja_dup_config_label_policy_parent_class = g_type_class_peek_parent (klass);
	DEJA_DUP_CONFIG_WIDGET_CLASS (klass)->set_from_config = deja_dup_config_label_policy_real_set_from_config;
	DEJA_DUP_CONFIG_WIDGET_CLASS (klass)->set_from_config_finish = deja_dup_config_label_policy_real_set_from_config_finish;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_config_label_policy_constructor;
}


static void deja_dup_config_label_policy_instance_init (DejaDupConfigLabelPolicy * self) {
}


GType deja_dup_config_label_policy_get_type (void) {
	static volatile gsize deja_dup_config_label_policy_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_config_label_policy_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupConfigLabelPolicyClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_config_label_policy_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupConfigLabelPolicy), 0, (GInstanceInitFunc) deja_dup_config_label_policy_instance_init, NULL };
		GType deja_dup_config_label_policy_type_id;
		deja_dup_config_label_policy_type_id = g_type_register_static (DEJA_DUP_TYPE_CONFIG_LABEL, "DejaDupConfigLabelPolicy", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_config_label_policy_type_id__volatile, deja_dup_config_label_policy_type_id);
	}
	return deja_dup_config_label_policy_type_id__volatile;
}



