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

#include <glib-object.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#define DC_STORAGE_QUOTA_WARNING_FRACTION 0.75
#define DC_STORAGE_QUOTA_ERROR_FRACTION 0.95
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define DC_TYPE_STORAGE_QUOTA (dc_storage_quota_get_type ())
#define DC_STORAGE_QUOTA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DC_TYPE_STORAGE_QUOTA, DcStorageQuota))
#define DC_STORAGE_QUOTA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DC_TYPE_STORAGE_QUOTA, DcStorageQuotaClass))
#define DC_IS_STORAGE_QUOTA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DC_TYPE_STORAGE_QUOTA))
#define DC_IS_STORAGE_QUOTA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DC_TYPE_STORAGE_QUOTA))
#define DC_STORAGE_QUOTA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DC_TYPE_STORAGE_QUOTA, DcStorageQuotaClass))

typedef struct _DcStorageQuota DcStorageQuota;
typedef struct _DcStorageQuotaClass DcStorageQuotaClass;
typedef struct _DcStorageQuotaPrivate DcStorageQuotaPrivate;
enum  {
	DC_STORAGE_QUOTA_0_PROPERTY,
	DC_STORAGE_QUOTA_AVAILABLE_PROPERTY,
	DC_STORAGE_QUOTA_USED_BYTES_PROPERTY,
	DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY,
	DC_STORAGE_QUOTA_NUM_PROPERTIES
};
static GParamSpec* dc_storage_quota_properties[DC_STORAGE_QUOTA_NUM_PROPERTIES];
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _DcStorageQuota {
	GObject parent_instance;
	DcStorageQuotaPrivate * priv;
};

struct _DcStorageQuotaClass {
	GObjectClass parent_class;
};

struct _DcStorageQuotaPrivate {
	gboolean _available;
	gint64 _used_bytes;
	gint64 _limit_bytes;
};

static gint DcStorageQuota_private_offset;
static gpointer dc_storage_quota_parent_class = NULL;
static GRegex* dc_storage_quota_quota_re;
static GRegex* dc_storage_quota_quota_re = NULL;

VALA_EXTERN GType dc_storage_quota_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DcStorageQuota, g_object_unref)
VALA_EXTERN DcStorageQuota* dc_storage_quota_parse_connectivity_report (const gchar* report);
VALA_EXTERN DcStorageQuota* dc_storage_quota_new (void);
VALA_EXTERN DcStorageQuota* dc_storage_quota_construct (GType object_type);
static gchar* dc_storage_quota_html_to_text (const gchar* html);
static void dc_storage_quota_ensure_regex (GError** error);
static gdouble dc_storage_quota_parse_number (const gchar* s);
static gint64 dc_storage_quota_unit_to_bytes (gdouble value,
                                       const gchar* unit);
VALA_EXTERN void dc_storage_quota_set_used_bytes (DcStorageQuota* self,
                                      gint64 value);
VALA_EXTERN void dc_storage_quota_set_limit_bytes (DcStorageQuota* self,
                                       gint64 value);
VALA_EXTERN gint64 dc_storage_quota_get_limit_bytes (DcStorageQuota* self);
VALA_EXTERN void dc_storage_quota_set_available (DcStorageQuota* self,
                                     gboolean value);
VALA_EXTERN gboolean dc_storage_quota_has_limit (DcStorageQuota* self);
VALA_EXTERN gboolean dc_storage_quota_get_available (DcStorageQuota* self);
VALA_EXTERN gint64 dc_storage_quota_left_bytes (DcStorageQuota* self);
VALA_EXTERN gint64 dc_storage_quota_get_used_bytes (DcStorageQuota* self);
VALA_EXTERN gdouble dc_storage_quota_used_fraction (DcStorageQuota* self);
VALA_EXTERN gboolean dc_storage_quota_is_warning (DcStorageQuota* self);
VALA_EXTERN gboolean dc_storage_quota_is_error (DcStorageQuota* self);
VALA_EXTERN gchar* dc_storage_quota_summary_text (DcStorageQuota* self);
VALA_EXTERN gchar* dc_storage_quota_format_mb (gint64 bytes);
static void dc_storage_quota_append_space (GString* sb,
                                    gboolean* last_space);
static gchar* dc_storage_quota_decode_entity (const gchar* entity);
static void dc_storage_quota_append_text (GString* sb,
                                   const gchar* text,
                                   gboolean* last_space);
static gboolean dc_storage_quota_is_space (gchar c);
static gchar* dc_storage_quota_decode_numeric_entity (const gchar* entity);
static gint dc_storage_quota_digit_for_base (gchar c,
                                      gint radix);
static void dc_storage_quota_finalize (GObject * obj);
static GType dc_storage_quota_get_type_once (void);
static void _vala_dc_storage_quota_get_property (GObject * object,
                                          guint property_id,
                                          GValue * value,
                                          GParamSpec * pspec);
static void _vala_dc_storage_quota_set_property (GObject * object,
                                          guint property_id,
                                          const GValue * value,
                                          GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
dc_storage_quota_get_instance_private (DcStorageQuota* self)
{
	return G_STRUCT_MEMBER_P (self, DcStorageQuota_private_offset);
}

DcStorageQuota*
dc_storage_quota_parse_connectivity_report (const gchar* report)
{
	DcStorageQuota* quota = NULL;
	DcStorageQuota* _tmp0_;
	gchar* text = NULL;
	gchar* _tmp1_;
	GError* _inner_error0_ = NULL;
	DcStorageQuota* result;
	g_return_val_if_fail (report != NULL, NULL);
	_tmp0_ = dc_storage_quota_new ();
	quota = _tmp0_;
	_tmp1_ = dc_storage_quota_html_to_text (report);
	text = _tmp1_;
	{
		GMatchInfo* match = NULL;
		GRegex* _tmp2_;
		GMatchInfo* _tmp3_ = NULL;
		gboolean _tmp4_;
		dc_storage_quota_ensure_regex (&_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = dc_storage_quota_quota_re;
		_tmp4_ = g_regex_match (_tmp2_, text, 0, &_tmp3_);
		_g_match_info_unref0 (match);
		match = _tmp3_;
		if (_tmp4_) {
			gdouble used = 0.0;
			GMatchInfo* _tmp5_;
			gchar* _tmp6_;
			gchar* _tmp7_;
			gdouble _tmp8_;
			gchar* used_unit = NULL;
			GMatchInfo* _tmp9_;
			gchar* _tmp10_;
			gdouble limit = 0.0;
			GMatchInfo* _tmp11_;
			gchar* _tmp12_;
			gchar* _tmp13_;
			gdouble _tmp14_;
			gchar* limit_unit = NULL;
			GMatchInfo* _tmp15_;
			gchar* _tmp16_;
			const gchar* _tmp17_;
			const gchar* _tmp18_;
			gint64 _tmp19_;
			_tmp5_ = match;
			_tmp6_ = g_match_info_fetch (_tmp5_, 1);
			_tmp7_ = _tmp6_;
			_tmp8_ = dc_storage_quota_parse_number (_tmp7_);
			_g_free0 (_tmp7_);
			used = _tmp8_;
			_tmp9_ = match;
			_tmp10_ = g_match_info_fetch (_tmp9_, 2);
			used_unit = _tmp10_;
			_tmp11_ = match;
			_tmp12_ = g_match_info_fetch (_tmp11_, 3);
			_tmp13_ = _tmp12_;
			_tmp14_ = dc_storage_quota_parse_number (_tmp13_);
			_g_free0 (_tmp13_);
			limit = _tmp14_;
			_tmp15_ = match;
			_tmp16_ = g_match_info_fetch (_tmp15_, 4);
			limit_unit = _tmp16_;
			_tmp17_ = used_unit;
			dc_storage_quota_set_used_bytes (quota, dc_storage_quota_unit_to_bytes (used, _tmp17_));
			_tmp18_ = limit_unit;
			dc_storage_quota_set_limit_bytes (quota, dc_storage_quota_unit_to_bytes (limit, _tmp18_));
			_tmp19_ = quota->priv->_limit_bytes;
			dc_storage_quota_set_available (quota, _tmp19_ > ((gint64) 0));
			_g_free0 (limit_unit);
			_g_free0 (used_unit);
		}
		_g_match_info_unref0 (match);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (text);
		_g_object_unref0 (quota);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	result = quota;
	_g_free0 (text);
	return result;
}

gboolean
dc_storage_quota_has_limit (DcStorageQuota* self)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp1_ = self->priv->_available;
	if (_tmp1_) {
		gint64 _tmp2_;
		_tmp2_ = self->priv->_limit_bytes;
		_tmp0_ = _tmp2_ > ((gint64) 0);
	} else {
		_tmp0_ = FALSE;
	}
	result = _tmp0_;
	return result;
}

gint64
dc_storage_quota_left_bytes (DcStorageQuota* self)
{
	gint64 left = 0LL;
	gint64 _tmp0_;
	gint64 _tmp1_;
	gint64 _tmp2_ = 0LL;
	gint64 result;
	g_return_val_if_fail (self != NULL, -1LL);
	_tmp0_ = self->priv->_limit_bytes;
	_tmp1_ = self->priv->_used_bytes;
	left = _tmp0_ - _tmp1_;
	if (left > ((gint64) 0)) {
		_tmp2_ = left;
	} else {
		_tmp2_ = (gint64) 0;
	}
	result = _tmp2_;
	return result;
}

gdouble
dc_storage_quota_used_fraction (DcStorageQuota* self)
{
	gdouble fraction = 0.0;
	gint64 _tmp0_;
	gint64 _tmp1_;
	gdouble result;
	g_return_val_if_fail (self != NULL, -1.0);
	if (!dc_storage_quota_has_limit (self)) {
		result = 0.0;
		return result;
	}
	_tmp0_ = self->priv->_used_bytes;
	_tmp1_ = self->priv->_limit_bytes;
	fraction = ((gdouble) _tmp0_) / ((gdouble) _tmp1_);
	if (fraction < 0.0) {
		result = 0.0;
		return result;
	}
	if (fraction > 1.0) {
		result = 1.0;
		return result;
	}
	result = fraction;
	return result;
}

gboolean
dc_storage_quota_is_warning (DcStorageQuota* self)
{
	gdouble fraction = 0.0;
	gboolean _tmp0_ = FALSE;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	fraction = dc_storage_quota_used_fraction (self);
	if (fraction >= DC_STORAGE_QUOTA_WARNING_FRACTION) {
		_tmp0_ = fraction < DC_STORAGE_QUOTA_ERROR_FRACTION;
	} else {
		_tmp0_ = FALSE;
	}
	result = _tmp0_;
	return result;
}

gboolean
dc_storage_quota_is_error (DcStorageQuota* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = dc_storage_quota_used_fraction (self) >= DC_STORAGE_QUOTA_ERROR_FRACTION;
	return result;
}

gchar*
dc_storage_quota_summary_text (DcStorageQuota* self)
{
	gint64 _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gint64 _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (!dc_storage_quota_has_limit (self)) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup ("Server quota has not been reported yet.");
		result = _tmp0_;
		return result;
	}
	_tmp1_ = self->priv->_used_bytes;
	_tmp2_ = dc_storage_quota_format_mb (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = dc_storage_quota_format_mb (dc_storage_quota_left_bytes (self));
	_tmp5_ = _tmp4_;
	_tmp6_ = self->priv->_limit_bytes;
	_tmp7_ = dc_storage_quota_format_mb (_tmp6_);
	_tmp8_ = _tmp7_;
	_tmp9_ = g_strdup_printf ("%s used · %s left of %s", _tmp3_, _tmp5_, _tmp8_);
	_tmp10_ = _tmp9_;
	_g_free0 (_tmp8_);
	_g_free0 (_tmp5_);
	_g_free0 (_tmp3_);
	result = _tmp10_;
	return result;
}

gchar*
dc_storage_quota_format_mb (gint64 bytes)
{
	gdouble mb = 0.0;
	gchar* _tmp0_;
	gchar* result;
	mb = ((gdouble) bytes) / (1024.0 * 1024.0);
	_tmp0_ = g_strdup_printf ("%.1f MB", mb);
	result = _tmp0_;
	return result;
}

static void
dc_storage_quota_ensure_regex (GError** error)
{
	GRegex* _tmp0_;
	GRegex* _tmp1_ = NULL;
	GRegex* _tmp2_;
	GRegex* _tmp3_;
	GError* _inner_error0_ = NULL;
	_tmp0_ = dc_storage_quota_quota_re;
	if (_tmp0_ != NULL) {
		return;
	}
	_tmp2_ = g_regex_new ("([0-9]+(?:[\\.,][0-9]+)?)\\s*([KMGT]?i?B)\\s+of\\s+" "([0-9]+(?:[\\.,][0-9]+)?)\\s*([KMGT]?i?B)\\s+used", G_REGEX_CASELESS, 0, &_inner_error0_);
	_tmp1_ = _tmp2_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == G_REGEX_ERROR) {
			g_propagate_error (error, _inner_error0_);
			return;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	_tmp3_ = _tmp1_;
	_tmp1_ = NULL;
	_g_regex_unref0 (dc_storage_quota_quota_re);
	dc_storage_quota_quota_re = _tmp3_;
	_g_regex_unref0 (_tmp1_);
}

static gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

static gint
string_index_of_char (const gchar* self,
                      gunichar c,
                      gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = g_utf8_strchr (((gchar*) self) + start_index, (gssize) -1, c);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static gchar*
dc_storage_quota_html_to_text (const gchar* html)
{
	GString* sb = NULL;
	GString* _tmp0_;
	gboolean in_tag = FALSE;
	gboolean last_space = FALSE;
	GString* _tmp17_;
	const gchar* _tmp18_;
	gchar* _tmp19_;
	gchar* result;
	g_return_val_if_fail (html != NULL, NULL);
	_tmp0_ = g_string_new ("");
	sb = _tmp0_;
	in_tag = FALSE;
	last_space = FALSE;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				gint _tmp3_;
				gint _tmp4_;
				gchar c = '\0';
				if (!_tmp1_) {
					gint _tmp2_;
					_tmp2_ = i;
					i = _tmp2_ + 1;
				}
				_tmp1_ = FALSE;
				_tmp3_ = strlen (html);
				_tmp4_ = _tmp3_;
				if (!(i < _tmp4_)) {
					break;
				}
				c = string_get (html, (glong) i);
				if (in_tag) {
					if (c == '>') {
						GString* _tmp5_;
						in_tag = FALSE;
						_tmp5_ = sb;
						dc_storage_quota_append_space (_tmp5_, &last_space);
					}
					continue;
				}
				if (c == '<') {
					GString* _tmp6_;
					in_tag = TRUE;
					_tmp6_ = sb;
					dc_storage_quota_append_space (_tmp6_, &last_space);
					continue;
				}
				if (c == '&') {
					gint semi = 0;
					gboolean _tmp7_ = FALSE;
					semi = string_index_of_char (html, (gunichar) ';', i + 1);
					if (semi > i) {
						_tmp7_ = (semi - i) <= 16;
					} else {
						_tmp7_ = FALSE;
					}
					if (_tmp7_) {
						gchar* decoded = NULL;
						gchar* _tmp8_;
						gchar* _tmp9_;
						gchar* _tmp10_;
						gchar* _tmp11_;
						const gchar* _tmp12_;
						_tmp8_ = string_substring (html, (glong) (i + 1), (glong) ((semi - i) - 1));
						_tmp9_ = _tmp8_;
						_tmp10_ = dc_storage_quota_decode_entity (_tmp9_);
						_tmp11_ = _tmp10_;
						_g_free0 (_tmp9_);
						decoded = _tmp11_;
						_tmp12_ = decoded;
						if (_tmp12_ != NULL) {
							GString* _tmp13_;
							const gchar* _tmp14_;
							_tmp13_ = sb;
							_tmp14_ = decoded;
							dc_storage_quota_append_text (_tmp13_, _tmp14_, &last_space);
							i = semi;
							_g_free0 (decoded);
							continue;
						}
						_g_free0 (decoded);
					}
				}
				if (dc_storage_quota_is_space (c)) {
					GString* _tmp15_;
					_tmp15_ = sb;
					dc_storage_quota_append_space (_tmp15_, &last_space);
				} else {
					GString* _tmp16_;
					_tmp16_ = sb;
					g_string_append_c (_tmp16_, c);
					last_space = FALSE;
				}
			}
		}
	}
	_tmp17_ = sb;
	_tmp18_ = _tmp17_->str;
	_tmp19_ = string_strip (_tmp18_);
	result = _tmp19_;
	_g_string_free0 (sb);
	return result;
}

static gchar*
dc_storage_quota_decode_entity (const gchar* entity)
{
	gchar* e = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	GQuark _tmp4_ = 0U;
	static GQuark _tmp3_label0 = 0;
	static GQuark _tmp3_label1 = 0;
	static GQuark _tmp3_label2 = 0;
	static GQuark _tmp3_label3 = 0;
	static GQuark _tmp3_label4 = 0;
	static GQuark _tmp3_label5 = 0;
	gchar* result;
	g_return_val_if_fail (entity != NULL, NULL);
	_tmp0_ = g_utf8_strdown (entity, (gssize) -1);
	e = _tmp0_;
	_tmp1_ = e;
	_tmp2_ = _tmp1_;
	_tmp4_ = (NULL == _tmp2_) ? 0 : g_quark_from_string (_tmp2_);
	if (_tmp4_ == ((0 != _tmp3_label0) ? _tmp3_label0 : (_tmp3_label0 = g_quark_from_static_string ("amp")))) {
		switch (0) {
			default:
			{
				gchar* _tmp5_;
				_tmp5_ = g_strdup ("&");
				result = _tmp5_;
				_g_free0 (e);
				return result;
			}
		}
	} else if (_tmp4_ == ((0 != _tmp3_label1) ? _tmp3_label1 : (_tmp3_label1 = g_quark_from_static_string ("lt")))) {
		switch (0) {
			default:
			{
				gchar* _tmp6_;
				_tmp6_ = g_strdup ("<");
				result = _tmp6_;
				_g_free0 (e);
				return result;
			}
		}
	} else if (_tmp4_ == ((0 != _tmp3_label2) ? _tmp3_label2 : (_tmp3_label2 = g_quark_from_static_string ("gt")))) {
		switch (0) {
			default:
			{
				gchar* _tmp7_;
				_tmp7_ = g_strdup (">");
				result = _tmp7_;
				_g_free0 (e);
				return result;
			}
		}
	} else if (_tmp4_ == ((0 != _tmp3_label3) ? _tmp3_label3 : (_tmp3_label3 = g_quark_from_static_string ("quot")))) {
		switch (0) {
			default:
			{
				gchar* _tmp8_;
				_tmp8_ = g_strdup ("\"");
				result = _tmp8_;
				_g_free0 (e);
				return result;
			}
		}
	} else if (_tmp4_ == ((0 != _tmp3_label4) ? _tmp3_label4 : (_tmp3_label4 = g_quark_from_static_string ("apos")))) {
		switch (0) {
			default:
			{
				gchar* _tmp9_;
				_tmp9_ = g_strdup ("'");
				result = _tmp9_;
				_g_free0 (e);
				return result;
			}
		}
	} else if (_tmp4_ == ((0 != _tmp3_label5) ? _tmp3_label5 : (_tmp3_label5 = g_quark_from_static_string ("nbsp")))) {
		switch (0) {
			default:
			{
				gchar* _tmp10_;
				_tmp10_ = g_strdup (" ");
				result = _tmp10_;
				_g_free0 (e);
				return result;
			}
		}
	} else {
		switch (0) {
			default:
			{
				const gchar* _tmp11_;
				_tmp11_ = e;
				if (g_str_has_prefix (_tmp11_, "#")) {
					const gchar* _tmp12_;
					gchar* _tmp13_;
					_tmp12_ = e;
					_tmp13_ = dc_storage_quota_decode_numeric_entity (_tmp12_);
					result = _tmp13_;
					_g_free0 (e);
					return result;
				}
				result = NULL;
				_g_free0 (e);
				return result;
			}
		}
	}
}

static gchar*
g_unichar_to_string (gunichar self)
{
	gchar* str = NULL;
	gchar* _tmp0_;
	gchar* result;
	_tmp0_ = g_new0 (gchar, 7);
	str = (gchar*) _tmp0_;
	g_unichar_to_utf8 (self, str);
	result = str;
	return result;
}

static gchar*
dc_storage_quota_decode_numeric_entity (const gchar* entity)
{
	gint start = 0;
	gint radix = 0;
	gboolean _tmp0_ = FALSE;
	gint _tmp1_;
	gint _tmp2_;
	guint code = 0U;
	gunichar ch = 0U;
	gchar* _tmp7_;
	gchar* result;
	g_return_val_if_fail (entity != NULL, NULL);
	start = 1;
	radix = 10;
	_tmp1_ = strlen (entity);
	_tmp2_ = _tmp1_;
	if (_tmp2_ > 2) {
		_tmp0_ = string_get (entity, (glong) 1) == 'x';
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		start = 2;
		radix = 16;
	}
	code = (guint) 0;
	{
		gint i = 0;
		i = start;
		{
			gboolean _tmp3_ = FALSE;
			_tmp3_ = TRUE;
			while (TRUE) {
				gint _tmp5_;
				gint _tmp6_;
				gint digit = 0;
				if (!_tmp3_) {
					gint _tmp4_;
					_tmp4_ = i;
					i = _tmp4_ + 1;
				}
				_tmp3_ = FALSE;
				_tmp5_ = strlen (entity);
				_tmp6_ = _tmp5_;
				if (!(i < _tmp6_)) {
					break;
				}
				digit = dc_storage_quota_digit_for_base (string_get (entity, (glong) i), radix);
				if (digit < 0) {
					result = NULL;
					return result;
				}
				code = (code * radix) + digit;
				if (code > ((guint) 0x10ffff)) {
					result = NULL;
					return result;
				}
			}
		}
	}
	if (code == ((guint) 0)) {
		result = NULL;
		return result;
	}
	ch = (gunichar) code;
	_tmp7_ = g_unichar_to_string (ch);
	result = _tmp7_;
	return result;
}

static gint
dc_storage_quota_digit_for_base (gchar c,
                                 gint radix)
{
	gint digit = 0;
	gboolean _tmp0_ = FALSE;
	gint _tmp3_ = 0;
	gboolean _tmp4_ = FALSE;
	gint result;
	digit = -1;
	if (c >= '0') {
		_tmp0_ = c <= '9';
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		digit = (gint) (c - '0');
	} else {
		gboolean _tmp1_ = FALSE;
		if (c >= 'a') {
			_tmp1_ = c <= 'f';
		} else {
			_tmp1_ = FALSE;
		}
		if (_tmp1_) {
			digit = (c - 'a') + 10;
		} else {
			gboolean _tmp2_ = FALSE;
			if (c >= 'A') {
				_tmp2_ = c <= 'F';
			} else {
				_tmp2_ = FALSE;
			}
			if (_tmp2_) {
				digit = (c - 'A') + 10;
			}
		}
	}
	if (digit >= 0) {
		_tmp4_ = digit < radix;
	} else {
		_tmp4_ = FALSE;
	}
	if (_tmp4_) {
		_tmp3_ = digit;
	} else {
		_tmp3_ = -1;
	}
	result = _tmp3_;
	return result;
}

static void
dc_storage_quota_append_text (GString* sb,
                              const gchar* text,
                              gboolean* last_space)
{
	gint _tmp0_;
	gint _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	gboolean _tmp6_;
	g_return_if_fail (sb != NULL);
	g_return_if_fail (text != NULL);
	_tmp0_ = strlen (text);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == 0) {
		return;
	}
	_tmp2_ = string_strip (text);
	_tmp3_ = _tmp2_;
	_tmp4_ = strlen (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = _tmp5_ == 0;
	_g_free0 (_tmp3_);
	if (_tmp6_) {
		dc_storage_quota_append_space (sb, last_space);
		return;
	}
	g_string_append (sb, text);
	*last_space = FALSE;
}

static void
dc_storage_quota_append_space (GString* sb,
                               gboolean* last_space)
{
	gboolean _tmp0_ = FALSE;
	g_return_if_fail (sb != NULL);
	if (*last_space) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = sb->len == ((gssize) 0);
	}
	if (_tmp0_) {
		return;
	}
	g_string_append_c (sb, ' ');
	*last_space = TRUE;
}

static gboolean
dc_storage_quota_is_space (gchar c)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gboolean result;
	if (c == ' ') {
		_tmp3_ = TRUE;
	} else {
		_tmp3_ = c == '\t';
	}
	if (_tmp3_) {
		_tmp2_ = TRUE;
	} else {
		_tmp2_ = c == '\n';
	}
	if (_tmp2_) {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = c == '\r';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = c == '\f';
	}
	result = _tmp0_;
	return result;
}

static gdouble
double_parse (const gchar* str)
{
	gdouble result;
	g_return_val_if_fail (str != NULL, -1.0);
	result = g_ascii_strtod (str, NULL);
	return result;
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static gdouble
dc_storage_quota_parse_number (const gchar* s)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	gdouble _tmp2_;
	gdouble result;
	g_return_val_if_fail (s != NULL, -1.0);
	_tmp0_ = string_replace (s, ",", ".");
	_tmp1_ = _tmp0_;
	_tmp2_ = double_parse (_tmp1_);
	_g_free0 (_tmp1_);
	result = _tmp2_;
	return result;
}

static gint64
dc_storage_quota_unit_to_bytes (gdouble value,
                                const gchar* unit)
{
	gchar* u = NULL;
	gchar* _tmp0_;
	gdouble mult = 0.0;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	gint64 result;
	g_return_val_if_fail (unit != NULL, -1LL);
	_tmp0_ = g_utf8_strup (unit, (gssize) -1);
	u = _tmp0_;
	mult = 1.0;
	_tmp2_ = u;
	if (g_strcmp0 (_tmp2_, "KB") == 0) {
		_tmp1_ = TRUE;
	} else {
		const gchar* _tmp3_;
		_tmp3_ = u;
		_tmp1_ = g_strcmp0 (_tmp3_, "KIB") == 0;
	}
	if (_tmp1_) {
		mult = 1024.0;
	} else {
		gboolean _tmp4_ = FALSE;
		const gchar* _tmp5_;
		_tmp5_ = u;
		if (g_strcmp0 (_tmp5_, "MB") == 0) {
			_tmp4_ = TRUE;
		} else {
			const gchar* _tmp6_;
			_tmp6_ = u;
			_tmp4_ = g_strcmp0 (_tmp6_, "MIB") == 0;
		}
		if (_tmp4_) {
			mult = 1024.0 * 1024.0;
		} else {
			gboolean _tmp7_ = FALSE;
			const gchar* _tmp8_;
			_tmp8_ = u;
			if (g_strcmp0 (_tmp8_, "GB") == 0) {
				_tmp7_ = TRUE;
			} else {
				const gchar* _tmp9_;
				_tmp9_ = u;
				_tmp7_ = g_strcmp0 (_tmp9_, "GIB") == 0;
			}
			if (_tmp7_) {
				mult = (1024.0 * 1024.0) * 1024.0;
			} else {
				gboolean _tmp10_ = FALSE;
				const gchar* _tmp11_;
				_tmp11_ = u;
				if (g_strcmp0 (_tmp11_, "TB") == 0) {
					_tmp10_ = TRUE;
				} else {
					const gchar* _tmp12_;
					_tmp12_ = u;
					_tmp10_ = g_strcmp0 (_tmp12_, "TIB") == 0;
				}
				if (_tmp10_) {
					mult = ((1024.0 * 1024.0) * 1024.0) * 1024.0;
				}
			}
		}
	}
	result = (gint64) (value * mult);
	_g_free0 (u);
	return result;
}

DcStorageQuota*
dc_storage_quota_construct (GType object_type)
{
	DcStorageQuota * self = NULL;
	self = (DcStorageQuota*) g_object_new (object_type, NULL);
	return self;
}

DcStorageQuota*
dc_storage_quota_new (void)
{
	return dc_storage_quota_construct (DC_TYPE_STORAGE_QUOTA);
}

gboolean
dc_storage_quota_get_available (DcStorageQuota* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_available;
	return result;
}

void
dc_storage_quota_set_available (DcStorageQuota* self,
                                gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = dc_storage_quota_get_available (self);
	if (old_value != value) {
		self->priv->_available = value;
		g_object_notify_by_pspec ((GObject *) self, dc_storage_quota_properties[DC_STORAGE_QUOTA_AVAILABLE_PROPERTY]);
	}
}

gint64
dc_storage_quota_get_used_bytes (DcStorageQuota* self)
{
	gint64 result;
	g_return_val_if_fail (self != NULL, -1LL);
	result = self->priv->_used_bytes;
	return result;
}

void
dc_storage_quota_set_used_bytes (DcStorageQuota* self,
                                 gint64 value)
{
	gint64 old_value;
	g_return_if_fail (self != NULL);
	old_value = dc_storage_quota_get_used_bytes (self);
	if (old_value != value) {
		self->priv->_used_bytes = value;
		g_object_notify_by_pspec ((GObject *) self, dc_storage_quota_properties[DC_STORAGE_QUOTA_USED_BYTES_PROPERTY]);
	}
}

gint64
dc_storage_quota_get_limit_bytes (DcStorageQuota* self)
{
	gint64 result;
	g_return_val_if_fail (self != NULL, -1LL);
	result = self->priv->_limit_bytes;
	return result;
}

void
dc_storage_quota_set_limit_bytes (DcStorageQuota* self,
                                  gint64 value)
{
	gint64 old_value;
	g_return_if_fail (self != NULL);
	old_value = dc_storage_quota_get_limit_bytes (self);
	if (old_value != value) {
		self->priv->_limit_bytes = value;
		g_object_notify_by_pspec ((GObject *) self, dc_storage_quota_properties[DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY]);
	}
}

static void
dc_storage_quota_class_init (DcStorageQuotaClass * klass,
                             gpointer klass_data)
{
	dc_storage_quota_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DcStorageQuota_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_dc_storage_quota_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_dc_storage_quota_set_property;
	G_OBJECT_CLASS (klass)->finalize = dc_storage_quota_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DC_STORAGE_QUOTA_AVAILABLE_PROPERTY, dc_storage_quota_properties[DC_STORAGE_QUOTA_AVAILABLE_PROPERTY] = g_param_spec_boolean ("available", "available", "available", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DC_STORAGE_QUOTA_USED_BYTES_PROPERTY, dc_storage_quota_properties[DC_STORAGE_QUOTA_USED_BYTES_PROPERTY] = g_param_spec_int64 ("used-bytes", "used-bytes", "used-bytes", G_MININT64, G_MAXINT64, (gint64) 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY, dc_storage_quota_properties[DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY] = g_param_spec_int64 ("limit-bytes", "limit-bytes", "limit-bytes", G_MININT64, G_MAXINT64, (gint64) 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
dc_storage_quota_instance_init (DcStorageQuota * self,
                                gpointer klass)
{
	self->priv = dc_storage_quota_get_instance_private (self);
	self->priv->_available = FALSE;
	self->priv->_used_bytes = (gint64) 0;
	self->priv->_limit_bytes = (gint64) 0;
}

static void
dc_storage_quota_finalize (GObject * obj)
{
	DcStorageQuota * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DC_TYPE_STORAGE_QUOTA, DcStorageQuota);
	G_OBJECT_CLASS (dc_storage_quota_parent_class)->finalize (obj);
}

static GType
dc_storage_quota_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DcStorageQuotaClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dc_storage_quota_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DcStorageQuota), 0, (GInstanceInitFunc) dc_storage_quota_instance_init, NULL };
	GType dc_storage_quota_type_id;
	dc_storage_quota_type_id = g_type_register_static (G_TYPE_OBJECT, "DcStorageQuota", &g_define_type_info, 0);
	DcStorageQuota_private_offset = g_type_add_instance_private (dc_storage_quota_type_id, sizeof (DcStorageQuotaPrivate));
	return dc_storage_quota_type_id;
}

GType
dc_storage_quota_get_type (void)
{
	static volatile gsize dc_storage_quota_type_id__once = 0;
	if (g_once_init_enter (&dc_storage_quota_type_id__once)) {
		GType dc_storage_quota_type_id;
		dc_storage_quota_type_id = dc_storage_quota_get_type_once ();
		g_once_init_leave (&dc_storage_quota_type_id__once, dc_storage_quota_type_id);
	}
	return dc_storage_quota_type_id__once;
}

static void
_vala_dc_storage_quota_get_property (GObject * object,
                                     guint property_id,
                                     GValue * value,
                                     GParamSpec * pspec)
{
	DcStorageQuota * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DC_TYPE_STORAGE_QUOTA, DcStorageQuota);
	switch (property_id) {
		case DC_STORAGE_QUOTA_AVAILABLE_PROPERTY:
		g_value_set_boolean (value, dc_storage_quota_get_available (self));
		break;
		case DC_STORAGE_QUOTA_USED_BYTES_PROPERTY:
		g_value_set_int64 (value, dc_storage_quota_get_used_bytes (self));
		break;
		case DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY:
		g_value_set_int64 (value, dc_storage_quota_get_limit_bytes (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_dc_storage_quota_set_property (GObject * object,
                                     guint property_id,
                                     const GValue * value,
                                     GParamSpec * pspec)
{
	DcStorageQuota * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DC_TYPE_STORAGE_QUOTA, DcStorageQuota);
	switch (property_id) {
		case DC_STORAGE_QUOTA_AVAILABLE_PROPERTY:
		dc_storage_quota_set_available (self, g_value_get_boolean (value));
		break;
		case DC_STORAGE_QUOTA_USED_BYTES_PROPERTY:
		dc_storage_quota_set_used_bytes (self, g_value_get_int64 (value));
		break;
		case DC_STORAGE_QUOTA_LIMIT_BYTES_PROPERTY:
		dc_storage_quota_set_limit_bytes (self, g_value_get_int64 (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

