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

/*
 * Copyright © 2025 Alain M. (https://github.com/alainm23/planify)
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include "planify.h"
#include <libsoup/soup.h>
#include <json-glib/json-glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>

#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

enum  {
	SERVICES_CAL_DAV_PROVIDERS_NEXTCLOUD_0_PROPERTY,
	SERVICES_CAL_DAV_PROVIDERS_NEXTCLOUD_NUM_PROPERTIES
};
static GParamSpec* services_cal_dav_providers_nextcloud_properties[SERVICES_CAL_DAV_PROVIDERS_NEXTCLOUD_NUM_PROPERTIES];
#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_uri_unref0(var) ((var == NULL) ? NULL : (var = (g_uri_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _http_response_unref0(var) ((var == NULL) ? NULL : (var = (http_response_unref (var), NULL)))
#define _g_bytes_unref0(var) ((var == NULL) ? NULL : (var = (g_bytes_unref (var), NULL)))
#define _json_object_unref0(var) ((var == NULL) ? NULL : (var = (json_object_unref (var), NULL)))
#define __vala_JsonNode_free0(var) ((var == NULL) ? NULL : (var = (_vala_JsonNode_free (var), NULL)))
typedef struct _ServicesCalDavProvidersNextcloudStartLoginFlowData ServicesCalDavProvidersNextcloudStartLoginFlowData;

struct _ServicesCalDAVProvidersNextcloudPrivate {
	SoupSession* session;
	JsonParser* parser;
};

struct _ServicesCalDavProvidersNextcloudStartLoginFlowData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ServicesCalDAVProvidersNextcloud* self;
	gchar* server_url;
	GCancellable* cancellable;
	gboolean ignore_ssl;
	HttpResponse* result;
	ServicesLogService* _tmp0_;
	ServicesLogService* _tmp1_;
	HttpResponse* response;
	HttpResponse* _tmp2_;
	gchar* login_url;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	SoupMessage* message;
	const gchar* _tmp7_;
	SoupMessage* _tmp8_;
	SoupMessage* _tmp9_;
	SoupMessageHeaders* _tmp10_;
	SoupMessageHeaders* _tmp11_;
	SoupMessage* _tmp12_;
	GBytes* stream;
	SoupSession* _tmp13_;
	SoupMessage* _tmp14_;
	GBytes* _tmp15_;
	JsonParser* _tmp16_;
	GBytes* _tmp17_;
	gsize _tmp18_;
	guint8* _tmp19_;
	JsonObject* root;
	JsonParser* _tmp20_;
	JsonNode* _tmp21_;
	JsonObject* _tmp22_;
	JsonObject* _tmp23_;
	gchar* login_link;
	JsonObject* _tmp24_;
	const gchar* _tmp25_;
	gchar* _tmp26_;
	JsonObject* poll;
	JsonObject* _tmp27_;
	JsonObject* _tmp28_;
	JsonObject* _tmp29_;
	gchar* poll_token;
	JsonObject* _tmp30_;
	const gchar* _tmp31_;
	gchar* _tmp32_;
	gchar* poll_endpoint;
	JsonObject* _tmp33_;
	const gchar* _tmp34_;
	gchar* _tmp35_;
	const gchar* _tmp36_;
	ServicesLogService* _tmp37_;
	ServicesLogService* _tmp38_;
	gint timeout;
	gint interval;
	gboolean _tmp39_;
	SoupMessage* poll_msg;
	const gchar* _tmp40_;
	SoupMessage* _tmp41_;
	SoupMessage* _tmp42_;
	SoupMessageHeaders* _tmp43_;
	SoupMessageHeaders* _tmp44_;
	SoupMessage* _tmp45_;
	const gchar* _tmp46_;
	gchar* _tmp47_;
	gchar* _tmp48_;
	guint8* _tmp49_;
	gint _tmp49__length1;
	gint _tmp50_;
	guint8* _tmp51_;
	gint _tmp51__length1;
	GBytes* _tmp52_;
	GBytes* _tmp53_;
	GBytes* poll_response;
	SoupSession* _tmp54_;
	SoupMessage* _tmp55_;
	GBytes* _tmp56_;
	gchar* poll_str;
	GBytes* _tmp57_;
	gsize _tmp58_;
	guint8* _tmp59_;
	gchar* _tmp60_;
	JsonParser* poll_parser;
	JsonParser* _tmp61_;
	JsonParser* _tmp62_;
	const gchar* _tmp63_;
	JsonNode* poll_root;
	JsonParser* _tmp64_;
	JsonNode* _tmp65_;
	JsonNode* _tmp66_;
	JsonNode* _tmp67_;
	JsonObject* poll_object;
	JsonNode* _tmp68_;
	JsonObject* _tmp69_;
	JsonObject* _tmp70_;
	JsonObject* _tmp71_;
	ServicesLogService* _tmp72_;
	ServicesLogService* _tmp73_;
	gchar* server;
	JsonObject* _tmp74_;
	const gchar* _tmp75_;
	gchar* _tmp76_;
	gchar* login_name;
	JsonObject* _tmp77_;
	const gchar* _tmp78_;
	gchar* _tmp79_;
	gchar* app_password;
	JsonObject* _tmp80_;
	const gchar* _tmp81_;
	gchar* _tmp82_;
	gchar* dav_endpoint;
	ServicesCalDAVCore* _tmp83_;
	ServicesCalDAVCore* _tmp84_;
	SoupSession* _tmp85_;
	const gchar* _tmp86_;
	gchar* _tmp87_;
	gchar* _tmp88_;
	ServicesLogService* _tmp89_;
	ServicesLogService* _tmp90_;
	gchar* calendar_home;
	ServicesCalDAVCore* _tmp91_;
	ServicesCalDAVCore* _tmp92_;
	const gchar* _tmp93_;
	const gchar* _tmp94_;
	const gchar* _tmp95_;
	gchar* _tmp96_;
	gchar* _tmp97_;
	ServicesLogService* _tmp98_;
	ServicesLogService* _tmp99_;
	HttpResponse* login_response;
	ServicesCalDAVCore* _tmp100_;
	ServicesCalDAVCore* _tmp101_;
	const gchar* _tmp102_;
	const gchar* _tmp103_;
	const gchar* _tmp104_;
	const gchar* _tmp105_;
	HttpResponse* _tmp106_;
	HttpResponse* _tmp107_;
	GError* err;
	ServicesLogService* _tmp108_;
	ServicesLogService* _tmp109_;
	GError* _tmp110_;
	const gchar* _tmp111_;
	gchar* _tmp112_;
	gchar* _tmp113_;
	HttpResponse* _tmp114_;
	GError* _tmp115_;
	HttpResponse* _tmp116_;
	GError* _tmp117_;
	const gchar* _tmp118_;
	gchar* _tmp119_;
	gchar* _tmp120_;
	GError* e;
	ServicesLogService* _tmp121_;
	ServicesLogService* _tmp122_;
	GError* _tmp123_;
	const gchar* _tmp124_;
	gchar* _tmp125_;
	gchar* _tmp126_;
	HttpResponse* _tmp127_;
	GError* _tmp128_;
	HttpResponse* _tmp129_;
	GError* _tmp130_;
	const gchar* _tmp131_;
	gchar* _tmp132_;
	gchar* _tmp133_;
	GError* _inner_error0_;
};

static gint ServicesCalDAVProvidersNextcloud_private_offset;
static gpointer services_cal_dav_providers_nextcloud_parent_class = NULL;

static gchar* services_cal_dav_providers_nextcloud_validate_server_url (ServicesCalDAVProvidersNextcloud* self,
                                                                 const gchar* url);
static void services_cal_dav_providers_nextcloud_start_login_flow_data_free (gpointer _data);
static gboolean services_cal_dav_providers_nextcloud_start_login_flow_co (ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_);
static gboolean ___lambda351_ (ServicesCalDAVProvidersNextcloud* self);
static gboolean ____lambda351__soup_message_accept_certificate (SoupMessage* _sender,
                                                         GTlsCertificate* tls_peer_certificate,
                                                         GTlsCertificateFlags tls_peer_errors,
                                                         gpointer self);
static void services_cal_dav_providers_nextcloud_start_login_flow_ready (GObject* source_object,
                                                                  GAsyncResult* _res_,
                                                                  gpointer _user_data_);
static JsonNode* _vala_JsonNode_copy (JsonNode* self);
static void _vala_JsonNode_free (JsonNode* self);
static void services_cal_dav_providers_nextcloud_finalize (GObject * obj);
static GType services_cal_dav_providers_nextcloud_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
services_cal_dav_providers_nextcloud_get_instance_private (ServicesCalDAVProvidersNextcloud* self)
{
	return G_STRUCT_MEMBER_P (self, ServicesCalDAVProvidersNextcloud_private_offset);
}

ServicesCalDAVProvidersNextcloud*
services_cal_dav_providers_nextcloud_construct (GType object_type)
{
	ServicesCalDAVProvidersNextcloud * self = NULL;
	SoupSession* _tmp0_;
	JsonParser* _tmp1_;
	self = (ServicesCalDAVProvidersNextcloud*) g_object_new (object_type, NULL);
	_tmp0_ = soup_session_new ();
	_g_object_unref0 (self->priv->session);
	self->priv->session = _tmp0_;
	_tmp1_ = json_parser_new ();
	_g_object_unref0 (self->priv->parser);
	self->priv->parser = _tmp1_;
	return self;
}

ServicesCalDAVProvidersNextcloud*
services_cal_dav_providers_nextcloud_new (void)
{
	return services_cal_dav_providers_nextcloud_construct (SERVICES_CAL_DAV_PROVIDERS_TYPE_NEXTCLOUD);
}

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*
services_cal_dav_providers_nextcloud_validate_server_url (ServicesCalDAVProvidersNextcloud* self,
                                                          const gchar* url)
{
	ServicesLogService* _tmp0_;
	ServicesLogService* _tmp1_;
	gchar* server_url = NULL;
	gchar* _tmp2_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (url != NULL, NULL);
	_tmp0_ = services_log_service_get_default ();
	_tmp1_ = _tmp0_;
	services_log_service_info (_tmp1_, "Nextcloud", "Validating server URL");
	_g_object_unref0 (_tmp1_);
	_tmp2_ = g_strdup ("");
	server_url = _tmp2_;
	{
		GUri* _uri = NULL;
		GUri* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		gchar* path = NULL;
		const gchar* _tmp10_;
		gchar* _tmp11_;
		const gchar* _tmp12_;
		const gchar* _tmp18_;
		const gchar* _tmp19_;
		gchar* _tmp20_;
		_tmp3_ = g_uri_parse (url, G_URI_FLAGS_NONE, &_inner_error0_);
		_uri = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp4_ = g_uri_get_scheme (_uri);
		_tmp5_ = g_uri_get_host (_uri);
		_tmp6_ = g_strdup_printf ("%s://%s", _tmp4_, _tmp5_);
		_g_free0 (server_url);
		server_url = _tmp6_;
		if (g_uri_get_port (_uri) > 0) {
			const gchar* _tmp7_;
			const gchar* _tmp8_;
			gchar* _tmp9_;
			_tmp7_ = g_uri_get_scheme (_uri);
			_tmp8_ = g_uri_get_host (_uri);
			_tmp9_ = g_strdup_printf ("%s://%s:%d", _tmp7_, _tmp8_, g_uri_get_port (_uri));
			_g_free0 (server_url);
			server_url = _tmp9_;
		}
		_tmp10_ = g_uri_get_path (_uri);
		_tmp11_ = g_strdup (_tmp10_);
		path = _tmp11_;
		_tmp12_ = path;
		if (g_str_has_suffix (_tmp12_, "/")) {
			const gchar* _tmp13_;
			const gchar* _tmp14_;
			gint _tmp15_;
			gint _tmp16_;
			gchar* _tmp17_;
			_tmp13_ = path;
			_tmp14_ = path;
			_tmp15_ = strlen (_tmp14_);
			_tmp16_ = _tmp15_;
			_tmp17_ = string_substring (_tmp13_, (glong) 0, (glong) (_tmp16_ - 1));
			_g_free0 (path);
			path = _tmp17_;
		}
		_tmp18_ = server_url;
		_tmp19_ = path;
		_tmp20_ = g_strconcat (_tmp18_, _tmp19_, NULL);
		_g_free0 (server_url);
		server_url = _tmp20_;
		_g_free0 (path);
		_g_uri_unref0 (_uri);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		ServicesLogService* _tmp21_;
		ServicesLogService* _tmp22_;
		GError* _tmp23_;
		const gchar* _tmp24_;
		gchar* _tmp25_;
		gchar* _tmp26_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp21_ = services_log_service_get_default ();
		_tmp22_ = _tmp21_;
		_tmp23_ = e;
		_tmp24_ = _tmp23_->message;
		_tmp25_ = g_strdup_printf ("Failed to validate server URL: %s", _tmp24_);
		_tmp26_ = _tmp25_;
		services_log_service_error (_tmp22_, "Nextcloud", _tmp26_);
		_g_free0 (_tmp26_);
		_g_object_unref0 (_tmp22_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (server_url);
		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 = server_url;
	return result;
}

static void
services_cal_dav_providers_nextcloud_start_login_flow_data_free (gpointer _data)
{
	ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_;
	_data_ = _data;
	_g_free0 (_data_->server_url);
	_g_object_unref0 (_data_->cancellable);
	_http_response_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (ServicesCalDavProvidersNextcloudStartLoginFlowData, _data_);
}

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

void
services_cal_dav_providers_nextcloud_start_login_flow (ServicesCalDAVProvidersNextcloud* self,
                                                       const gchar* server_url,
                                                       GCancellable* cancellable,
                                                       gboolean ignore_ssl,
                                                       GAsyncReadyCallback _callback_,
                                                       gpointer _user_data_)
{
	ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_;
	ServicesCalDAVProvidersNextcloud* _tmp0_;
	gchar* _tmp1_;
	GCancellable* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (server_url != NULL);
	g_return_if_fail (cancellable != NULL);
	_data_ = g_slice_new0 (ServicesCalDavProvidersNextcloudStartLoginFlowData);
	_data_->_async_result = g_task_new (G_OBJECT (self), cancellable, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, services_cal_dav_providers_nextcloud_start_login_flow_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = g_strdup (server_url);
	_g_free0 (_data_->server_url);
	_data_->server_url = _tmp1_;
	_tmp2_ = _g_object_ref0 (cancellable);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp2_;
	_data_->ignore_ssl = ignore_ssl;
	services_cal_dav_providers_nextcloud_start_login_flow_co (_data_);
}

HttpResponse*
services_cal_dav_providers_nextcloud_start_login_flow_finish (ServicesCalDAVProvidersNextcloud* self,
                                                              GAsyncResult* _res_)
{
	HttpResponse* result;
	ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	if (NULL == _data_) {
		return NULL;
	}
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static gboolean
___lambda351_ (ServicesCalDAVProvidersNextcloud* self)
{
	gboolean result;
	result = TRUE;
	return result;
}

static gboolean
____lambda351__soup_message_accept_certificate (SoupMessage* _sender,
                                                GTlsCertificate* tls_peer_certificate,
                                                GTlsCertificateFlags tls_peer_errors,
                                                gpointer self)
{
	gboolean result;
	result = ___lambda351_ ((ServicesCalDAVProvidersNextcloud*) self);
	return result;
}

static void
services_cal_dav_providers_nextcloud_start_login_flow_ready (GObject* source_object,
                                                             GAsyncResult* _res_,
                                                             gpointer _user_data_)
{
	ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	services_cal_dav_providers_nextcloud_start_login_flow_co (_data_);
}

static gpointer
_json_object_ref0 (gpointer self)
{
	return self ? json_object_ref (self) : NULL;
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static JsonNode*
_vala_JsonNode_copy (JsonNode* self)
{
	return g_boxed_copy (json_node_get_type (), self);
}

static gpointer
__vala_JsonNode_copy0 (gpointer self)
{
	return self ? _vala_JsonNode_copy (self) : NULL;
}

static void
_vala_JsonNode_free (JsonNode* self)
{
	g_boxed_free (json_node_get_type (), self);
}

static gboolean
services_cal_dav_providers_nextcloud_start_login_flow_co (ServicesCalDavProvidersNextcloudStartLoginFlowData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		case 3:
		goto _state_3;
		case 4:
		goto _state_4;
		case 5:
		goto _state_5;
		case 6:
		goto _state_6;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = services_log_service_get_default ();
	_data_->_tmp1_ = _data_->_tmp0_;
	services_log_service_info (_data_->_tmp1_, "Nextcloud", "Starting login flow");
	_g_object_unref0 (_data_->_tmp1_);
	_data_->_tmp2_ = http_response_new ();
	_data_->response = _data_->_tmp2_;
	_data_->_tmp3_ = services_cal_dav_providers_nextcloud_validate_server_url (_data_->self, _data_->server_url);
	_data_->_tmp4_ = _data_->_tmp3_;
	_data_->_tmp5_ = g_strdup_printf ("%s/index.php/login/v2", _data_->_tmp4_);
	_data_->_tmp6_ = _data_->_tmp5_;
	_g_free0 (_data_->_tmp4_);
	_data_->login_url = _data_->_tmp6_;
	_data_->_tmp7_ = _data_->login_url;
	_data_->_tmp8_ = soup_message_new ("POST", _data_->_tmp7_);
	_data_->message = _data_->_tmp8_;
	_data_->_tmp9_ = _data_->message;
	_data_->_tmp10_ = soup_message_get_request_headers (_data_->_tmp9_);
	_data_->_tmp11_ = _data_->_tmp10_;
	soup_message_headers_append (_data_->_tmp11_, "User-Agent", CONSTANTS_SOUP_USER_AGENT);
	if (_data_->ignore_ssl) {
		_data_->_tmp12_ = _data_->message;
		g_signal_connect_object (_data_->_tmp12_, "accept-certificate", (GCallback) ____lambda351__soup_message_accept_certificate, _data_->self, 0);
	}
	{
		_data_->_tmp13_ = _data_->self->priv->session;
		_data_->_tmp14_ = _data_->message;
		_data_->_state_ = 1;
		soup_session_send_and_read_async (_data_->_tmp13_, _data_->_tmp14_, G_PRIORITY_HIGH, _data_->cancellable, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp15_ = soup_session_send_and_read_finish (_data_->_tmp13_, _data_->_res_, &_data_->_inner_error0_);
		_data_->stream = _data_->_tmp15_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp16_ = _data_->self->priv->parser;
		_data_->_tmp17_ = _data_->stream;
		_data_->_tmp18_ = 0;
		_data_->_tmp19_ = g_bytes_get_data (_data_->_tmp17_, &_data_->_tmp18_);
		json_parser_load_from_data (_data_->_tmp16_, (const gchar*) _data_->_tmp19_, (gssize) -1, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_bytes_unref0 (_data_->stream);
			goto __catch0_g_error;
		}
		_data_->_tmp20_ = _data_->self->priv->parser;
		_data_->_tmp21_ = json_parser_get_root (_data_->_tmp20_);
		_data_->_tmp22_ = json_node_get_object (_data_->_tmp21_);
		_data_->_tmp23_ = _json_object_ref0 (_data_->_tmp22_);
		_data_->root = _data_->_tmp23_;
		_data_->_tmp24_ = _data_->root;
		_data_->_tmp25_ = json_object_get_string_member (_data_->_tmp24_, "login");
		_data_->_tmp26_ = g_strdup (_data_->_tmp25_);
		_data_->login_link = _data_->_tmp26_;
		_data_->_tmp27_ = _data_->root;
		_data_->_tmp28_ = json_object_get_object_member (_data_->_tmp27_, "poll");
		_data_->_tmp29_ = _json_object_ref0 (_data_->_tmp28_);
		_data_->poll = _data_->_tmp29_;
		_data_->_tmp30_ = _data_->poll;
		_data_->_tmp31_ = json_object_get_string_member (_data_->_tmp30_, "token");
		_data_->_tmp32_ = g_strdup (_data_->_tmp31_);
		_data_->poll_token = _data_->_tmp32_;
		_data_->_tmp33_ = _data_->poll;
		_data_->_tmp34_ = json_object_get_string_member (_data_->_tmp33_, "endpoint");
		_data_->_tmp35_ = g_strdup (_data_->_tmp34_);
		_data_->poll_endpoint = _data_->_tmp35_;
		_data_->_tmp36_ = _data_->login_link;
		g_app_info_launch_default_for_uri (_data_->_tmp36_, NULL, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_free0 (_data_->poll_endpoint);
			_g_free0 (_data_->poll_token);
			_json_object_unref0 (_data_->poll);
			_g_free0 (_data_->login_link);
			_json_object_unref0 (_data_->root);
			_g_bytes_unref0 (_data_->stream);
			goto __catch0_g_error;
		}
		_data_->_tmp37_ = services_log_service_get_default ();
		_data_->_tmp38_ = _data_->_tmp37_;
		services_log_service_info (_data_->_tmp38_, "Nextcloud", "Login page opened in browser, waiting for authentication");
		_g_object_unref0 (_data_->_tmp38_);
		_data_->timeout = 20 * 60;
		_data_->interval = 5;
		while (TRUE) {
			if (_data_->timeout > 0) {
				_data_->_tmp39_ = !g_cancellable_is_cancelled (_data_->cancellable);
			} else {
				_data_->_tmp39_ = FALSE;
			}
			if (!_data_->_tmp39_) {
				break;
			}
			_data_->_tmp40_ = _data_->poll_endpoint;
			_data_->_tmp41_ = soup_message_new ("POST", _data_->_tmp40_);
			_data_->poll_msg = _data_->_tmp41_;
			_data_->_tmp42_ = _data_->poll_msg;
			_data_->_tmp43_ = soup_message_get_request_headers (_data_->_tmp42_);
			_data_->_tmp44_ = _data_->_tmp43_;
			soup_message_headers_append (_data_->_tmp44_, "User-Agent", CONSTANTS_SOUP_USER_AGENT);
			_data_->_tmp45_ = _data_->poll_msg;
			_data_->_tmp46_ = _data_->poll_token;
			_data_->_tmp47_ = g_strdup_printf ("{ \"token\": \"%s\" }", _data_->_tmp46_);
			_data_->_tmp48_ = _data_->_tmp47_;
			_data_->_tmp50_ = 0;
			_data_->_tmp49_ = string_get_data (_data_->_tmp48_, &_data_->_tmp50_);
			_data_->_tmp49__length1 = _data_->_tmp50_;
			_data_->_tmp51_ = _data_->_tmp49_;
			_data_->_tmp51__length1 = _data_->_tmp49__length1;
			_data_->_tmp52_ = g_bytes_new (_data_->_tmp51_, _data_->_tmp51__length1);
			_data_->_tmp53_ = _data_->_tmp52_;
			soup_message_set_request_body_from_bytes (_data_->_tmp45_, "application/json", _data_->_tmp53_);
			_g_bytes_unref0 (_data_->_tmp53_);
			_g_free0 (_data_->_tmp48_);
			{
				_data_->_tmp54_ = _data_->self->priv->session;
				_data_->_tmp55_ = _data_->poll_msg;
				_data_->_state_ = 2;
				soup_session_send_and_read_async (_data_->_tmp54_, _data_->_tmp55_, G_PRIORITY_HIGH, _data_->cancellable, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
				return FALSE;
				_state_2:
				_data_->_tmp56_ = soup_session_send_and_read_finish (_data_->_tmp54_, _data_->_res_, &_data_->_inner_error0_);
				_data_->poll_response = _data_->_tmp56_;
				if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
					goto __catch1_g_error;
				}
				_data_->_tmp57_ = _data_->poll_response;
				_data_->_tmp58_ = 0;
				_data_->_tmp59_ = g_bytes_get_data (_data_->_tmp57_, &_data_->_tmp58_);
				_data_->_tmp60_ = g_strdup ((const gchar*) _data_->_tmp59_);
				_data_->poll_str = _data_->_tmp60_;
				_data_->_tmp61_ = json_parser_new ();
				_data_->poll_parser = _data_->_tmp61_;
				_data_->_tmp62_ = _data_->poll_parser;
				_data_->_tmp63_ = _data_->poll_str;
				json_parser_load_from_data (_data_->_tmp62_, _data_->_tmp63_, (gssize) -1, &_data_->_inner_error0_);
				if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
					_g_object_unref0 (_data_->poll_parser);
					_g_free0 (_data_->poll_str);
					_g_bytes_unref0 (_data_->poll_response);
					goto __catch1_g_error;
				}
				_data_->_tmp64_ = _data_->poll_parser;
				_data_->_tmp65_ = json_parser_get_root (_data_->_tmp64_);
				_data_->_tmp66_ = __vala_JsonNode_copy0 (_data_->_tmp65_);
				_data_->poll_root = _data_->_tmp66_;
				_data_->_tmp67_ = _data_->poll_root;
				if (json_node_get_node_type (_data_->_tmp67_) == JSON_NODE_OBJECT) {
					_data_->_tmp68_ = _data_->poll_root;
					_data_->_tmp69_ = json_node_get_object (_data_->_tmp68_);
					_data_->_tmp70_ = _json_object_ref0 (_data_->_tmp69_);
					_data_->poll_object = _data_->_tmp70_;
					_data_->_tmp71_ = _data_->poll_object;
					if (json_object_has_member (_data_->_tmp71_, "loginName")) {
						_data_->_tmp72_ = services_log_service_get_default ();
						_data_->_tmp73_ = _data_->_tmp72_;
						services_log_service_info (_data_->_tmp73_, "Nextcloud", "Authentication successful, resolving CalDAV endpoint");
						_g_object_unref0 (_data_->_tmp73_);
						_data_->_tmp74_ = _data_->poll_object;
						_data_->_tmp75_ = json_object_get_string_member (_data_->_tmp74_, "server");
						_data_->_tmp76_ = g_strdup (_data_->_tmp75_);
						_data_->server = _data_->_tmp76_;
						_data_->_tmp77_ = _data_->poll_object;
						_data_->_tmp78_ = json_object_get_string_member (_data_->_tmp77_, "loginName");
						_data_->_tmp79_ = g_strdup (_data_->_tmp78_);
						_data_->login_name = _data_->_tmp79_;
						_data_->_tmp80_ = _data_->poll_object;
						_data_->_tmp81_ = json_object_get_string_member (_data_->_tmp80_, "appPassword");
						_data_->_tmp82_ = g_strdup (_data_->_tmp81_);
						_data_->app_password = _data_->_tmp82_;
						_data_->_tmp83_ = services_cal_dav_core_get_default ();
						_data_->_tmp84_ = _data_->_tmp83_;
						_data_->_tmp85_ = _data_->self->priv->session;
						_data_->_tmp86_ = _data_->server;
						_data_->_state_ = 3;
						services_cal_dav_core_resolve_well_known_caldav (_data_->_tmp84_, _data_->_tmp85_, _data_->_tmp86_, FALSE, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
						return FALSE;
						_state_3:
						_data_->_tmp87_ = services_cal_dav_core_resolve_well_known_caldav_finish (_data_->_tmp84_, _data_->_res_, &_data_->_inner_error0_);
						_data_->_tmp88_ = _data_->_tmp87_;
						_g_object_unref0 (_data_->_tmp84_);
						_data_->dav_endpoint = _data_->_tmp88_;
						if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
							_g_free0 (_data_->app_password);
							_g_free0 (_data_->login_name);
							_g_free0 (_data_->server);
							_json_object_unref0 (_data_->poll_object);
							__vala_JsonNode_free0 (_data_->poll_root);
							_g_object_unref0 (_data_->poll_parser);
							_g_free0 (_data_->poll_str);
							_g_bytes_unref0 (_data_->poll_response);
							goto __catch1_g_error;
						}
						_data_->_tmp89_ = services_log_service_get_default ();
						_data_->_tmp90_ = _data_->_tmp89_;
						services_log_service_info (_data_->_tmp90_, "Nextcloud", "Resolved well-known CalDAV endpoint");
						_g_object_unref0 (_data_->_tmp90_);
						_data_->_tmp91_ = services_cal_dav_core_get_default ();
						_data_->_tmp92_ = _data_->_tmp91_;
						_data_->_tmp93_ = _data_->dav_endpoint;
						_data_->_tmp94_ = _data_->login_name;
						_data_->_tmp95_ = _data_->app_password;
						_data_->_state_ = 4;
						services_cal_dav_core_resolve_calendar_home (_data_->_tmp92_, CAL_DAV_TYPE_NEXTCLOUD, _data_->_tmp93_, _data_->_tmp94_, _data_->_tmp95_, _data_->cancellable, _data_->ignore_ssl, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
						return FALSE;
						_state_4:
						_data_->_tmp96_ = services_cal_dav_core_resolve_calendar_home_finish (_data_->_tmp92_, _data_->_res_, &_data_->_inner_error0_);
						_data_->_tmp97_ = _data_->_tmp96_;
						_g_object_unref0 (_data_->_tmp92_);
						_data_->calendar_home = _data_->_tmp97_;
						if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
							_g_free0 (_data_->dav_endpoint);
							_g_free0 (_data_->app_password);
							_g_free0 (_data_->login_name);
							_g_free0 (_data_->server);
							_json_object_unref0 (_data_->poll_object);
							__vala_JsonNode_free0 (_data_->poll_root);
							_g_object_unref0 (_data_->poll_parser);
							_g_free0 (_data_->poll_str);
							_g_bytes_unref0 (_data_->poll_response);
							goto __catch1_g_error;
						}
						_data_->_tmp98_ = services_log_service_get_default ();
						_data_->_tmp99_ = _data_->_tmp98_;
						services_log_service_info (_data_->_tmp99_, "Nextcloud", "Resolved calendar home");
						_g_object_unref0 (_data_->_tmp99_);
						_data_->_tmp100_ = services_cal_dav_core_get_default ();
						_data_->_tmp101_ = _data_->_tmp100_;
						_data_->_tmp102_ = _data_->dav_endpoint;
						_data_->_tmp103_ = _data_->login_name;
						_data_->_tmp104_ = _data_->app_password;
						_data_->_tmp105_ = _data_->calendar_home;
						_data_->_state_ = 5;
						services_cal_dav_core_login (_data_->_tmp101_, CAL_DAV_TYPE_NEXTCLOUD, _data_->_tmp102_, _data_->_tmp103_, _data_->_tmp104_, _data_->_tmp105_, _data_->cancellable, _data_->ignore_ssl, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
						return FALSE;
						_state_5:
						_data_->_tmp106_ = services_cal_dav_core_login_finish (_data_->_tmp101_, _data_->_res_);
						_data_->_tmp107_ = _data_->_tmp106_;
						_g_object_unref0 (_data_->_tmp101_);
						_data_->login_response = _data_->_tmp107_;
						_data_->result = _data_->login_response;
						_g_free0 (_data_->calendar_home);
						_g_free0 (_data_->dav_endpoint);
						_g_free0 (_data_->app_password);
						_g_free0 (_data_->login_name);
						_g_free0 (_data_->server);
						_json_object_unref0 (_data_->poll_object);
						__vala_JsonNode_free0 (_data_->poll_root);
						_g_object_unref0 (_data_->poll_parser);
						_g_free0 (_data_->poll_str);
						_g_bytes_unref0 (_data_->poll_response);
						_g_object_unref0 (_data_->poll_msg);
						_g_free0 (_data_->poll_endpoint);
						_g_free0 (_data_->poll_token);
						_json_object_unref0 (_data_->poll);
						_g_free0 (_data_->login_link);
						_json_object_unref0 (_data_->root);
						_g_bytes_unref0 (_data_->stream);
						_g_object_unref0 (_data_->message);
						_g_free0 (_data_->login_url);
						_http_response_unref0 (_data_->response);
						g_task_return_pointer (_data_->_async_result, _data_, NULL);
						if (_data_->_state_ != 0) {
							while (!g_task_get_completed (_data_->_async_result)) {
								g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
							}
						}
						g_object_unref (_data_->_async_result);
						return FALSE;
					}
					_json_object_unref0 (_data_->poll_object);
				}
				__vala_JsonNode_free0 (_data_->poll_root);
				_g_object_unref0 (_data_->poll_parser);
				_g_free0 (_data_->poll_str);
				_g_bytes_unref0 (_data_->poll_response);
			}
			goto __finally1;
			__catch1_g_error:
			{
				_data_->err = _data_->_inner_error0_;
				_data_->_inner_error0_ = NULL;
				_data_->_tmp108_ = services_log_service_get_default ();
				_data_->_tmp109_ = _data_->_tmp108_;
				_data_->_tmp110_ = _data_->err;
				_data_->_tmp111_ = _data_->_tmp110_->message;
				_data_->_tmp112_ = g_strdup_printf ("Polling error: %s", _data_->_tmp111_);
				_data_->_tmp113_ = _data_->_tmp112_;
				services_log_service_error (_data_->_tmp109_, "Nextcloud", _data_->_tmp113_);
				_g_free0 (_data_->_tmp113_);
				_g_object_unref0 (_data_->_tmp109_);
				_data_->_tmp114_ = _data_->response;
				_data_->_tmp115_ = _data_->err;
				http_response_set_error_code (_data_->_tmp114_, _data_->_tmp115_->code);
				_data_->_tmp116_ = _data_->response;
				_data_->_tmp117_ = _data_->err;
				_data_->_tmp118_ = _data_->_tmp117_->message;
				_data_->_tmp119_ = g_strdup_printf ("Polling error: %s", _data_->_tmp118_);
				_data_->_tmp120_ = _data_->_tmp119_;
				http_response_set_error (_data_->_tmp116_, _data_->_tmp120_);
				_g_free0 (_data_->_tmp120_);
				_g_error_free0 (_data_->err);
				_g_object_unref0 (_data_->poll_msg);
				break;
			}
			__finally1:
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				_g_object_unref0 (_data_->poll_msg);
				_g_free0 (_data_->poll_endpoint);
				_g_free0 (_data_->poll_token);
				_json_object_unref0 (_data_->poll);
				_g_free0 (_data_->login_link);
				_json_object_unref0 (_data_->root);
				_g_bytes_unref0 (_data_->stream);
				goto __catch0_g_error;
			}
			_data_->_state_ = 6;
			util_nap ((guint) (_data_->interval * 1000), G_PRIORITY_DEFAULT, services_cal_dav_providers_nextcloud_start_login_flow_ready, _data_);
			return FALSE;
			_state_6:
			util_nap_finish (_data_->_res_);
			_data_->timeout -= _data_->interval;
			_g_object_unref0 (_data_->poll_msg);
		}
		_g_free0 (_data_->poll_endpoint);
		_g_free0 (_data_->poll_token);
		_json_object_unref0 (_data_->poll);
		_g_free0 (_data_->login_link);
		_json_object_unref0 (_data_->root);
		_g_bytes_unref0 (_data_->stream);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp121_ = services_log_service_get_default ();
		_data_->_tmp122_ = _data_->_tmp121_;
		_data_->_tmp123_ = _data_->e;
		_data_->_tmp124_ = _data_->_tmp123_->message;
		_data_->_tmp125_ = g_strdup_printf ("Login flow error: %s", _data_->_tmp124_);
		_data_->_tmp126_ = _data_->_tmp125_;
		services_log_service_error (_data_->_tmp122_, "Nextcloud", _data_->_tmp126_);
		_g_free0 (_data_->_tmp126_);
		_g_object_unref0 (_data_->_tmp122_);
		_data_->_tmp127_ = _data_->response;
		_data_->_tmp128_ = _data_->e;
		http_response_set_error_code (_data_->_tmp127_, _data_->_tmp128_->code);
		_data_->_tmp129_ = _data_->response;
		_data_->_tmp130_ = _data_->e;
		_data_->_tmp131_ = _data_->_tmp130_->message;
		_data_->_tmp132_ = g_strdup_printf ("login error: %s", _data_->_tmp131_);
		_data_->_tmp133_ = _data_->_tmp132_;
		http_response_set_error (_data_->_tmp129_, _data_->_tmp133_);
		_g_free0 (_data_->_tmp133_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_g_object_unref0 (_data_->message);
		_g_free0 (_data_->login_url);
		_http_response_unref0 (_data_->response);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->result = _data_->response;
	_g_object_unref0 (_data_->message);
	_g_free0 (_data_->login_url);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
services_cal_dav_providers_nextcloud_class_init (ServicesCalDAVProvidersNextcloudClass * klass,
                                                 gpointer klass_data)
{
	services_cal_dav_providers_nextcloud_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ServicesCalDAVProvidersNextcloud_private_offset);
	G_OBJECT_CLASS (klass)->finalize = services_cal_dav_providers_nextcloud_finalize;
}

static void
services_cal_dav_providers_nextcloud_instance_init (ServicesCalDAVProvidersNextcloud * self,
                                                    gpointer klass)
{
	self->priv = services_cal_dav_providers_nextcloud_get_instance_private (self);
}

static void
services_cal_dav_providers_nextcloud_finalize (GObject * obj)
{
	ServicesCalDAVProvidersNextcloud * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_CAL_DAV_PROVIDERS_TYPE_NEXTCLOUD, ServicesCalDAVProvidersNextcloud);
	_g_object_unref0 (self->priv->session);
	_g_object_unref0 (self->priv->parser);
	G_OBJECT_CLASS (services_cal_dav_providers_nextcloud_parent_class)->finalize (obj);
}

static GType
services_cal_dav_providers_nextcloud_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesCalDAVProvidersNextcloudClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_cal_dav_providers_nextcloud_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesCalDAVProvidersNextcloud), 0, (GInstanceInitFunc) services_cal_dav_providers_nextcloud_instance_init, NULL };
	GType services_cal_dav_providers_nextcloud_type_id;
	services_cal_dav_providers_nextcloud_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesCalDAVProvidersNextcloud", &g_define_type_info, 0);
	ServicesCalDAVProvidersNextcloud_private_offset = g_type_add_instance_private (services_cal_dav_providers_nextcloud_type_id, sizeof (ServicesCalDAVProvidersNextcloudPrivate));
	return services_cal_dav_providers_nextcloud_type_id;
}

GType
services_cal_dav_providers_nextcloud_get_type (void)
{
	static volatile gsize services_cal_dav_providers_nextcloud_type_id__once = 0;
	if (g_once_init_enter (&services_cal_dav_providers_nextcloud_type_id__once)) {
		GType services_cal_dav_providers_nextcloud_type_id;
		services_cal_dav_providers_nextcloud_type_id = services_cal_dav_providers_nextcloud_get_type_once ();
		g_once_init_leave (&services_cal_dav_providers_nextcloud_type_id__once, services_cal_dav_providers_nextcloud_type_id);
	}
	return services_cal_dav_providers_nextcloud_type_id__once;
}

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;
}

