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

/*
 * Copyright © 2023 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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <json-glib/json-glib.h>
#include <glib/gi18n-lib.h>
#include <gee.h>

#define SERVICES_TODOIST_AUTH_TODOIST_SYNC_URL "https://api.todoist.com/api/v1/sync"
#define SERVICES_TODOIST_AUTH_RESOURCE_TYPES "[\"user\", \"projects\", \"sections\", \"items\", \"labels\", \"remind" \
"ers\"]"
#define SERVICES_TODOIST_AUTH_PROJECTS_COLLECTION "projects"
#define SERVICES_TODOIST_AUTH_SECTIONS_COLLECTION "sections"
#define SERVICES_TODOIST_AUTH_ITEMS_COLLECTION "items"
#define SERVICES_TODOIST_AUTH_LABELS_COLLECTION "labels"
#define SERVICES_TODOIST_AUTH_REMINDERS_COLLECTION "reminders"
#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_TODOIST_AUTH_0_PROPERTY,
	SERVICES_TODOIST_AUTH_NUM_PROPERTIES
};
static GParamSpec* services_todoist_auth_properties[SERVICES_TODOIST_AUTH_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 _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 _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ServicesTodoistAuthLoginData ServicesTodoistAuthLoginData;
typedef struct _ServicesTodoistAuthLoginTokenData ServicesTodoistAuthLoginTokenData;
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
typedef struct _ServicesTodoistAuthAddTodoistAccountData ServicesTodoistAuthAddTodoistAccountData;

struct _ServicesTodoistAuthPrivate {
	SoupSession* session;
};

struct _ServicesTodoistAuthLoginData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ServicesTodoistAuth* self;
	gchar* _url;
	ObjectsSource* migrate_source;
	HttpResponse* result;
	gchar* code;
	gchar** _tmp0_;
	gchar** _tmp1_;
	gchar** _tmp2_;
	gint _tmp2__length1;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar** _tmp7_;
	gchar** _tmp8_;
	gchar** _tmp9_;
	gint _tmp9__length1;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* url;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	SoupMessage* message;
	const gchar* _tmp14_;
	SoupMessage* _tmp15_;
	HttpResponse* response;
	HttpResponse* _tmp16_;
	GBytes* stream;
	SoupSession* _tmp17_;
	SoupMessage* _tmp18_;
	GBytes* _tmp19_;
	JsonParser* parser;
	JsonParser* _tmp20_;
	JsonParser* _tmp21_;
	GBytes* _tmp22_;
	gsize _tmp23_;
	guint8* _tmp24_;
	JsonObject* root;
	JsonParser* _tmp25_;
	JsonNode* _tmp26_;
	JsonObject* _tmp27_;
	JsonObject* _tmp28_;
	gchar* token;
	JsonObject* _tmp29_;
	const gchar* _tmp30_;
	gchar* _tmp31_;
	const gchar* _tmp32_;
	HttpResponse* _tmp33_;
	GError* e;
	HttpResponse* _tmp34_;
	GError* _tmp35_;
	HttpResponse* _tmp36_;
	GError* _tmp37_;
	const gchar* _tmp38_;
	ServicesLogService* _tmp39_;
	ServicesLogService* _tmp40_;
	GError* _tmp41_;
	const gchar* _tmp42_;
	gchar* _tmp43_;
	gchar* _tmp44_;
	GError* _inner_error0_;
};

struct _ServicesTodoistAuthLoginTokenData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ServicesTodoistAuth* self;
	gchar* token;
	ObjectsSource* migrate_source;
	HttpResponse* result;
	ServicesLogService* _tmp0_;
	ServicesLogService* _tmp1_;
	HttpResponse* response;
	HttpResponse* _tmp2_;
	GError* e;
	ServicesLogService* _tmp3_;
	ServicesLogService* _tmp4_;
	GError* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	GError* _tmp9_;
	GError* _tmp10_;
	const gchar* _tmp11_;
	GError* _inner_error0_;
};

struct _ServicesTodoistAuthAddTodoistAccountData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ServicesTodoistAuth* self;
	gchar* token;
	HttpResponse* response;
	ObjectsSource* migrate_source;
	ServicesLogService* _tmp0_;
	ServicesLogService* _tmp1_;
	SoupMessage* message;
	SoupMessage* _tmp2_;
	SoupMessage* _tmp3_;
	SoupMessageHeaders* _tmp4_;
	SoupMessageHeaders* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* form_data;
	gchar* _tmp8_;
	SoupMessage* _tmp9_;
	const gchar* _tmp10_;
	guint8* _tmp11_;
	gint _tmp11__length1;
	gint _tmp12_;
	guint8* _tmp13_;
	gint _tmp13__length1;
	GBytes* _tmp14_;
	GBytes* _tmp15_;
	GBytes* stream;
	SoupSession* _tmp16_;
	SoupMessage* _tmp17_;
	GBytes* _tmp18_;
	JsonParser* parser;
	JsonParser* _tmp19_;
	SoupMessage* _tmp20_;
	guint _tmp21_;
	guint _tmp22_;
	ServicesLogService* _tmp23_;
	ServicesLogService* _tmp24_;
	SoupMessage* _tmp25_;
	guint _tmp26_;
	guint _tmp27_;
	gchar* _tmp28_;
	gchar* _tmp29_;
	SoupMessage* _tmp30_;
	guint _tmp31_;
	guint _tmp32_;
	ServicesLogService* _tmp33_;
	ServicesLogService* _tmp34_;
	JsonParser* _tmp35_;
	GBytes* _tmp36_;
	gsize _tmp37_;
	guint8* _tmp38_;
	ObjectsSource* source;
	ObjectsSource* _tmp39_;
	ObjectsSource* _tmp40_;
	Util* _tmp41_;
	Util* _tmp42_;
	gchar* _tmp43_;
	gchar* _tmp44_;
	ObjectsSource* _tmp45_;
	ObjectsSourceTodoistData* todoist_data;
	ObjectsSourceTodoistData* _tmp46_;
	ObjectsSourceTodoistData* _tmp47_;
	JsonParser* _tmp48_;
	JsonNode* _tmp49_;
	JsonObject* _tmp50_;
	const gchar* _tmp51_;
	ObjectsSourceTodoistData* _tmp52_;
	ObjectsSourceTodoistData* _tmp53_;
	ObjectsSource* _tmp54_;
	JsonObject* user_object;
	JsonParser* _tmp55_;
	JsonNode* _tmp56_;
	JsonObject* _tmp57_;
	JsonObject* _tmp58_;
	JsonObject* _tmp59_;
	ObjectsSourceTodoistData* _tmp60_;
	JsonObject* _tmp61_;
	const gchar* _tmp62_;
	JsonObject* _tmp63_;
	ObjectsSourceTodoistData* _tmp64_;
	JsonObject* _tmp65_;
	const gchar* _tmp66_;
	ObjectsSourceTodoistData* _tmp67_;
	JsonObject* _tmp68_;
	const gchar* _tmp69_;
	ObjectsSourceTodoistData* _tmp70_;
	JsonObject* _tmp71_;
	const gchar* _tmp72_;
	ObjectsSourceTodoistData* _tmp73_;
	JsonObject* _tmp74_;
	const gchar* _tmp75_;
	ObjectsSourceTodoistData* _tmp76_;
	JsonObject* _tmp77_;
	ObjectsSource* _tmp78_;
	JsonObject* _tmp79_;
	const gchar* _tmp80_;
	ObjectsSource* _tmp81_;
	ObjectsSourceTodoistData* _tmp82_;
	ServicesStore* _tmp83_;
	ObjectsSourceTodoistData* _tmp84_;
	const gchar* _tmp85_;
	const gchar* _tmp86_;
	gboolean _tmp87_;
	ObjectsSourceTodoistData* _tmp88_;
	ObjectsSourceTodoistData* _tmp89_;
	const gchar* _tmp90_;
	const gchar* _tmp91_;
	ObjectsSourceTodoistData* _tmp92_;
	const gchar* _tmp93_;
	const gchar* _tmp94_;
	ServicesStore* _tmp95_;
	ObjectsSource* _tmp96_;
	JsonArray* labels;
	JsonParser* _tmp97_;
	JsonNode* _tmp98_;
	JsonObject* _tmp99_;
	JsonArray* _tmp100_;
	JsonArray* _tmp101_;
	GList* _tmp102_;
	GList* _node_collection;
	GList* _node_it;
	JsonNode* _node;
	ObjectsLabel* _label;
	JsonNode* _tmp103_;
	ObjectsLabel* _tmp104_;
	ObjectsLabel* _tmp105_;
	ObjectsSource* _tmp106_;
	const gchar* _tmp107_;
	const gchar* _tmp108_;
	ServicesStore* _tmp109_;
	ObjectsLabel* _tmp110_;
	JsonArray* projects;
	JsonParser* _tmp111_;
	JsonNode* _tmp112_;
	JsonObject* _tmp113_;
	JsonArray* _tmp114_;
	JsonArray* _tmp115_;
	GList* _tmp116_;
	GList* _vala1__node_collection;
	GList* _vala1__node_it;
	JsonNode* _vala1__node;
	ObjectsProject* _project;
	JsonNode* _tmp117_;
	ObjectsProject* _tmp118_;
	ObjectsProject* _tmp119_;
	ObjectsSource* _tmp120_;
	const gchar* _tmp121_;
	const gchar* _tmp122_;
	JsonNode* _tmp123_;
	JsonObject* _tmp124_;
	ObjectsProject* project;
	ServicesStore* _tmp125_;
	JsonNode* _tmp126_;
	JsonObject* _tmp127_;
	const gchar* _tmp128_;
	ObjectsProject* _tmp129_;
	ObjectsProject* _tmp130_;
	ObjectsProject* _tmp131_;
	ObjectsProject* _tmp132_;
	ObjectsProject* _tmp133_;
	ObjectsProject* _tmp134_;
	ServicesStore* _tmp135_;
	ObjectsProject* _tmp136_;
	JsonArray* sections;
	JsonParser* _tmp137_;
	JsonNode* _tmp138_;
	JsonObject* _tmp139_;
	JsonArray* _tmp140_;
	JsonArray* _tmp141_;
	GList* _tmp142_;
	GList* _vala2__node_collection;
	GList* _vala2__node_it;
	JsonNode* _vala2__node;
	ServicesTodoist* _tmp143_;
	ServicesTodoist* _tmp144_;
	JsonNode* _tmp145_;
	JsonArray* items;
	JsonParser* _tmp146_;
	JsonNode* _tmp147_;
	JsonObject* _tmp148_;
	JsonArray* _tmp149_;
	JsonArray* _tmp150_;
	GList* _tmp151_;
	GList* _vala3__node_collection;
	GList* _vala3__node_it;
	JsonNode* _vala3__node;
	ServicesTodoist* _tmp152_;
	ServicesTodoist* _tmp153_;
	JsonNode* _tmp154_;
	JsonArray* reminders;
	JsonParser* _tmp155_;
	JsonNode* _tmp156_;
	JsonObject* _tmp157_;
	JsonArray* _tmp158_;
	JsonArray* _tmp159_;
	GList* _tmp160_;
	GList* _vala4__node_collection;
	GList* _vala4__node_it;
	JsonNode* _vala4__node;
	ObjectsReminder* reminder;
	JsonNode* _tmp161_;
	ObjectsReminder* _tmp162_;
	ObjectsItem* item;
	ServicesStore* _tmp163_;
	ObjectsReminder* _tmp164_;
	const gchar* _tmp165_;
	const gchar* _tmp166_;
	ObjectsItem* _tmp167_;
	ObjectsItem* _tmp168_;
	ObjectsItem* _tmp169_;
	ObjectsReminder* _tmp170_;
	ObjectsReminder* _tmp171_;
	ObjectsReminder* _tmp172_;
	JsonObject* _tmp173_;
	Util* _tmp174_;
	Util* _tmp175_;
	ObjectsSource* _tmp176_;
	ObjectsSourceTodoistData* _tmp177_;
	ObjectsSourceTodoistData* _tmp178_;
	const gchar* _tmp179_;
	const gchar* _tmp180_;
	JsonObject* _tmp181_;
	const gchar* _tmp182_;
	ObjectsSource* _tmp183_;
	GDateTime* _tmp184_;
	GDateTime* _tmp185_;
	gchar* _tmp186_;
	gchar* _tmp187_;
	ObjectsSource* _tmp188_;
	ServicesLogService* _tmp189_;
	ServicesLogService* _tmp190_;
	GError* e;
	ServicesLogService* _tmp191_;
	ServicesLogService* _tmp192_;
	GError* _tmp193_;
	const gchar* _tmp194_;
	gchar* _tmp195_;
	gchar* _tmp196_;
	GError* _tmp197_;
	GError* _tmp198_;
	const gchar* _tmp199_;
	GError* _inner_error0_;
};

static gint ServicesTodoistAuth_private_offset;
static gpointer services_todoist_auth_parent_class = NULL;

static void services_todoist_auth_login_data_free (gpointer _data);
static gboolean services_todoist_auth_login_co (ServicesTodoistAuthLoginData* _data_);
static void services_todoist_auth_login_ready (GObject* source_object,
                                        GAsyncResult* _res_,
                                        gpointer _user_data_);
static void services_todoist_auth_login_token_data_free (gpointer _data);
static gboolean services_todoist_auth_login_token_co (ServicesTodoistAuthLoginTokenData* _data_);
static void services_todoist_auth_login_token_ready (GObject* source_object,
                                              GAsyncResult* _res_,
                                              gpointer _user_data_);
static void services_todoist_auth_add_todoist_account_data_free (gpointer _data);
static gboolean services_todoist_auth_add_todoist_account_co (ServicesTodoistAuthAddTodoistAccountData* _data_);
static void services_todoist_auth_add_todoist_account_ready (GObject* source_object,
                                                      GAsyncResult* _res_,
                                                      gpointer _user_data_);
static void services_todoist_auth_check_and_update_inbox_project (ServicesTodoistAuth* self,
                                                           ObjectsSource* migrate_source);
static ObjectsProject* services_todoist_auth_get_local_inbox_project (ServicesTodoistAuth* self);
static void services_todoist_auth_finalize (GObject * obj);
static GType services_todoist_auth_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

static inline gpointer
services_todoist_auth_get_instance_private (ServicesTodoistAuth* self)
{
	return G_STRUCT_MEMBER_P (self, ServicesTodoistAuth_private_offset);
}

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

ServicesTodoistAuth*
services_todoist_auth_construct (GType object_type,
                                 SoupSession* session)
{
	ServicesTodoistAuth * self = NULL;
	SoupSession* _tmp0_;
	g_return_val_if_fail (session != NULL, NULL);
	self = (ServicesTodoistAuth*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (session);
	_g_object_unref0 (self->priv->session);
	self->priv->session = _tmp0_;
	return self;
}

ServicesTodoistAuth*
services_todoist_auth_new (SoupSession* session)
{
	return services_todoist_auth_construct (SERVICES_TYPE_TODOIST_AUTH, session);
}

static void
services_todoist_auth_login_data_free (gpointer _data)
{
	ServicesTodoistAuthLoginData* _data_;
	_data_ = _data;
	_g_free0 (_data_->_url);
	_g_object_unref0 (_data_->migrate_source);
	_http_response_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (ServicesTodoistAuthLoginData, _data_);
}

void
services_todoist_auth_login (ServicesTodoistAuth* self,
                             const gchar* _url,
                             ObjectsSource* migrate_source,
                             GAsyncReadyCallback _callback_,
                             gpointer _user_data_)
{
	ServicesTodoistAuthLoginData* _data_;
	ServicesTodoistAuth* _tmp0_;
	gchar* _tmp1_;
	ObjectsSource* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (_url != NULL);
	_data_ = g_slice_new0 (ServicesTodoistAuthLoginData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, services_todoist_auth_login_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = g_strdup (_url);
	_g_free0 (_data_->_url);
	_data_->_url = _tmp1_;
	_tmp2_ = _g_object_ref0 (migrate_source);
	_g_object_unref0 (_data_->migrate_source);
	_data_->migrate_source = _tmp2_;
	services_todoist_auth_login_co (_data_);
}

HttpResponse*
services_todoist_auth_login_finish (ServicesTodoistAuth* self,
                                    GAsyncResult* _res_)
{
	HttpResponse* result;
	ServicesTodoistAuthLoginData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static void
services_todoist_auth_login_ready (GObject* source_object,
                                   GAsyncResult* _res_,
                                   gpointer _user_data_)
{
	ServicesTodoistAuthLoginData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	services_todoist_auth_login_co (_data_);
}

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

static gboolean
services_todoist_auth_login_co (ServicesTodoistAuthLoginData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp1_ = _data_->_tmp0_ = g_strsplit (_data_->_url, "=", 0);
	_data_->_tmp2_ = _data_->_tmp1_;
	_data_->_tmp2__length1 = _vala_array_length (_data_->_tmp0_);
	_data_->_tmp3_ = _data_->_tmp2_[1];
	_data_->_tmp4_ = g_strdup (_data_->_tmp3_);
	_data_->_tmp5_ = _data_->_tmp4_;
	_data_->_tmp2_ = (_vala_array_free (_data_->_tmp2_, _data_->_tmp2__length1, (GDestroyNotify) g_free), NULL);
	_data_->code = _data_->_tmp5_;
	_data_->_tmp6_ = _data_->code;
	_data_->_tmp8_ = _data_->_tmp7_ = g_strsplit (_data_->_tmp6_, "&", 0);
	_data_->_tmp9_ = _data_->_tmp8_;
	_data_->_tmp9__length1 = _vala_array_length (_data_->_tmp7_);
	_data_->_tmp10_ = _data_->_tmp9_[0];
	_data_->_tmp11_ = g_strdup (_data_->_tmp10_);
	_g_free0 (_data_->code);
	_data_->code = _data_->_tmp11_;
	_data_->_tmp9_ = (_vala_array_free (_data_->_tmp9_, _data_->_tmp9__length1, (GDestroyNotify) g_free), NULL);
	_data_->_tmp12_ = _data_->code;
	_data_->_tmp13_ = g_strdup_printf ("https://todoist.com/oauth/access_token?client_id=%s&client_secret=%s&c" \
"ode=%s", CONSTANTS_TODOIST_CLIENT_ID, CONSTANTS_TODOIST_CLIENT_SECRET, _data_->_tmp12_);
	_data_->url = _data_->_tmp13_;
	_data_->_tmp14_ = _data_->url;
	_data_->_tmp15_ = soup_message_new ("POST", _data_->_tmp14_);
	_data_->message = _data_->_tmp15_;
	_data_->_tmp16_ = http_response_new ();
	_data_->response = _data_->_tmp16_;
	{
		_data_->_tmp17_ = _data_->self->priv->session;
		_data_->_tmp18_ = _data_->message;
		_data_->_state_ = 1;
		soup_session_send_and_read_async (_data_->_tmp17_, _data_->_tmp18_, G_PRIORITY_HIGH, NULL, services_todoist_auth_login_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp19_ = soup_session_send_and_read_finish (_data_->_tmp17_, _data_->_res_, &_data_->_inner_error0_);
		_data_->stream = _data_->_tmp19_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp20_ = json_parser_new ();
		_data_->parser = _data_->_tmp20_;
		_data_->_tmp21_ = _data_->parser;
		_data_->_tmp22_ = _data_->stream;
		_data_->_tmp23_ = 0;
		_data_->_tmp24_ = g_bytes_get_data (_data_->_tmp22_, &_data_->_tmp23_);
		json_parser_load_from_data (_data_->_tmp21_, (const gchar*) _data_->_tmp24_, (gssize) -1, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->parser);
			_g_bytes_unref0 (_data_->stream);
			goto __catch0_g_error;
		}
		_data_->_tmp25_ = _data_->parser;
		_data_->_tmp26_ = json_parser_get_root (_data_->_tmp25_);
		_data_->_tmp27_ = json_node_get_object (_data_->_tmp26_);
		_data_->_tmp28_ = _json_object_ref0 (_data_->_tmp27_);
		_data_->root = _data_->_tmp28_;
		_data_->_tmp29_ = _data_->root;
		_data_->_tmp30_ = json_object_get_string_member (_data_->_tmp29_, "access_token");
		_data_->_tmp31_ = g_strdup (_data_->_tmp30_);
		_data_->token = _data_->_tmp31_;
		_data_->_tmp32_ = _data_->token;
		_data_->_tmp33_ = _data_->response;
		_data_->_state_ = 2;
		services_todoist_auth_add_todoist_account (_data_->self, _data_->_tmp32_, _data_->_tmp33_, _data_->migrate_source, services_todoist_auth_login_ready, _data_);
		return FALSE;
		_state_2:
		services_todoist_auth_add_todoist_account_finish (_data_->self, _data_->_res_);
		_g_free0 (_data_->token);
		_json_object_unref0 (_data_->root);
		_g_object_unref0 (_data_->parser);
		_g_bytes_unref0 (_data_->stream);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp34_ = _data_->response;
		_data_->_tmp35_ = _data_->e;
		http_response_set_error_code (_data_->_tmp34_, _data_->_tmp35_->code);
		_data_->_tmp36_ = _data_->response;
		_data_->_tmp37_ = _data_->e;
		_data_->_tmp38_ = _data_->_tmp37_->message;
		http_response_set_error (_data_->_tmp36_, _data_->_tmp38_);
		_data_->_tmp39_ = services_log_service_get_default ();
		_data_->_tmp40_ = _data_->_tmp39_;
		_data_->_tmp41_ = _data_->e;
		_data_->_tmp42_ = _data_->_tmp41_->message;
		_data_->_tmp43_ = g_strdup_printf ("Login failed: %s", _data_->_tmp42_);
		_data_->_tmp44_ = _data_->_tmp43_;
		services_log_service_error (_data_->_tmp40_, "Todoist", _data_->_tmp44_);
		_g_free0 (_data_->_tmp44_);
		_g_object_unref0 (_data_->_tmp40_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_http_response_unref0 (_data_->response);
		_g_object_unref0 (_data_->message);
		_g_free0 (_data_->url);
		_g_free0 (_data_->code);
		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_->url);
	_g_free0 (_data_->code);
	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_todoist_auth_login_token_data_free (gpointer _data)
{
	ServicesTodoistAuthLoginTokenData* _data_;
	_data_ = _data;
	_g_free0 (_data_->token);
	_g_object_unref0 (_data_->migrate_source);
	_http_response_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (ServicesTodoistAuthLoginTokenData, _data_);
}

void
services_todoist_auth_login_token (ServicesTodoistAuth* self,
                                   const gchar* token,
                                   ObjectsSource* migrate_source,
                                   GAsyncReadyCallback _callback_,
                                   gpointer _user_data_)
{
	ServicesTodoistAuthLoginTokenData* _data_;
	ServicesTodoistAuth* _tmp0_;
	gchar* _tmp1_;
	ObjectsSource* _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (token != NULL);
	_data_ = g_slice_new0 (ServicesTodoistAuthLoginTokenData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, services_todoist_auth_login_token_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = g_strdup (token);
	_g_free0 (_data_->token);
	_data_->token = _tmp1_;
	_tmp2_ = _g_object_ref0 (migrate_source);
	_g_object_unref0 (_data_->migrate_source);
	_data_->migrate_source = _tmp2_;
	services_todoist_auth_login_token_co (_data_);
}

HttpResponse*
services_todoist_auth_login_token_finish (ServicesTodoistAuth* self,
                                          GAsyncResult* _res_)
{
	HttpResponse* result;
	ServicesTodoistAuthLoginTokenData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static void
services_todoist_auth_login_token_ready (GObject* source_object,
                                         GAsyncResult* _res_,
                                         gpointer _user_data_)
{
	ServicesTodoistAuthLoginTokenData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	services_todoist_auth_login_token_co (_data_);
}

static gboolean
services_todoist_auth_login_token_co (ServicesTodoistAuthLoginTokenData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = services_log_service_get_default ();
	_data_->_tmp1_ = _data_->_tmp0_;
	services_log_service_info (_data_->_tmp1_, "Todoist", "Starting login with API token");
	_g_object_unref0 (_data_->_tmp1_);
	_data_->_tmp2_ = http_response_new ();
	_data_->response = _data_->_tmp2_;
	{
		_data_->_state_ = 1;
		services_todoist_auth_add_todoist_account (_data_->self, _data_->token, _data_->response, _data_->migrate_source, services_todoist_auth_login_token_ready, _data_);
		return FALSE;
		_state_1:
		services_todoist_auth_add_todoist_account_finish (_data_->self, _data_->_res_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp3_ = services_log_service_get_default ();
		_data_->_tmp4_ = _data_->_tmp3_;
		_data_->_tmp5_ = _data_->e;
		_data_->_tmp6_ = _data_->_tmp5_->message;
		_data_->_tmp7_ = g_strdup_printf ("Login with token failed: %s", _data_->_tmp6_);
		_data_->_tmp8_ = _data_->_tmp7_;
		services_log_service_error (_data_->_tmp4_, "Todoist", _data_->_tmp8_);
		_g_free0 (_data_->_tmp8_);
		_g_object_unref0 (_data_->_tmp4_);
		_data_->_tmp9_ = _data_->e;
		http_response_set_error_code (_data_->response, _data_->_tmp9_->code);
		_data_->_tmp10_ = _data_->e;
		_data_->_tmp11_ = _data_->_tmp10_->message;
		http_response_set_error (_data_->response, _data_->_tmp11_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_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_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_todoist_auth_add_todoist_account_data_free (gpointer _data)
{
	ServicesTodoistAuthAddTodoistAccountData* _data_;
	_data_ = _data;
	_g_free0 (_data_->token);
	_http_response_unref0 (_data_->response);
	_g_object_unref0 (_data_->migrate_source);
	_g_object_unref0 (_data_->self);
	g_slice_free (ServicesTodoistAuthAddTodoistAccountData, _data_);
}

static gpointer
_http_response_ref0 (gpointer self)
{
	return self ? http_response_ref (self) : NULL;
}

void
services_todoist_auth_add_todoist_account (ServicesTodoistAuth* self,
                                           const gchar* token,
                                           HttpResponse* response,
                                           ObjectsSource* migrate_source,
                                           GAsyncReadyCallback _callback_,
                                           gpointer _user_data_)
{
	ServicesTodoistAuthAddTodoistAccountData* _data_;
	ServicesTodoistAuth* _tmp0_;
	gchar* _tmp1_;
	HttpResponse* _tmp2_;
	ObjectsSource* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (token != NULL);
	g_return_if_fail (response != NULL);
	_data_ = g_slice_new0 (ServicesTodoistAuthAddTodoistAccountData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, services_todoist_auth_add_todoist_account_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = g_strdup (token);
	_g_free0 (_data_->token);
	_data_->token = _tmp1_;
	_tmp2_ = _http_response_ref0 (response);
	_http_response_unref0 (_data_->response);
	_data_->response = _tmp2_;
	_tmp3_ = _g_object_ref0 (migrate_source);
	_g_object_unref0 (_data_->migrate_source);
	_data_->migrate_source = _tmp3_;
	services_todoist_auth_add_todoist_account_co (_data_);
}

void
services_todoist_auth_add_todoist_account_finish (ServicesTodoistAuth* self,
                                                  GAsyncResult* _res_)
{
	ServicesTodoistAuthAddTodoistAccountData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), 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 void
services_todoist_auth_add_todoist_account_ready (GObject* source_object,
                                                 GAsyncResult* _res_,
                                                 gpointer _user_data_)
{
	ServicesTodoistAuthAddTodoistAccountData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	services_todoist_auth_add_todoist_account_co (_data_);
}

static gchar*
g_date_time_to_string (GDateTime* self)
{
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_date_time_format (self, "%FT%H:%M:%S%z");
	result = _tmp0_;
	return result;
}

static gboolean
services_todoist_auth_add_todoist_account_co (ServicesTodoistAuthAddTodoistAccountData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = services_log_service_get_default ();
	_data_->_tmp1_ = _data_->_tmp0_;
	services_log_service_info (_data_->_tmp1_, "Todoist", "Adding Todoist account");
	_g_object_unref0 (_data_->_tmp1_);
	_data_->_tmp2_ = soup_message_new ("POST", SERVICES_TODOIST_AUTH_TODOIST_SYNC_URL);
	_data_->message = _data_->_tmp2_;
	_data_->_tmp3_ = _data_->message;
	_data_->_tmp4_ = soup_message_get_request_headers (_data_->_tmp3_);
	_data_->_tmp5_ = _data_->_tmp4_;
	_data_->_tmp6_ = g_strdup_printf ("Bearer %s", _data_->token);
	_data_->_tmp7_ = _data_->_tmp6_;
	soup_message_headers_append (_data_->_tmp5_, "Authorization", _data_->_tmp7_);
	_g_free0 (_data_->_tmp7_);
	_data_->_tmp8_ = g_strdup_printf ("sync_token=*&resource_types=%s", SERVICES_TODOIST_AUTH_RESOURCE_TYPES);
	_data_->form_data = _data_->_tmp8_;
	_data_->_tmp9_ = _data_->message;
	_data_->_tmp10_ = _data_->form_data;
	_data_->_tmp12_ = 0;
	_data_->_tmp11_ = string_get_data (_data_->_tmp10_, &_data_->_tmp12_);
	_data_->_tmp11__length1 = _data_->_tmp12_;
	_data_->_tmp13_ = _data_->_tmp11_;
	_data_->_tmp13__length1 = _data_->_tmp11__length1;
	_data_->_tmp14_ = g_bytes_new (_data_->_tmp13_, _data_->_tmp13__length1);
	_data_->_tmp15_ = _data_->_tmp14_;
	soup_message_set_request_body_from_bytes (_data_->_tmp9_, "application/x-www-form-urlencoded", _data_->_tmp15_);
	_g_bytes_unref0 (_data_->_tmp15_);
	{
		_data_->_tmp16_ = _data_->self->priv->session;
		_data_->_tmp17_ = _data_->message;
		_data_->_state_ = 1;
		soup_session_send_and_read_async (_data_->_tmp16_, _data_->_tmp17_, G_PRIORITY_HIGH, NULL, services_todoist_auth_add_todoist_account_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp18_ = soup_session_send_and_read_finish (_data_->_tmp16_, _data_->_res_, &_data_->_inner_error0_);
		_data_->stream = _data_->_tmp18_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp19_ = json_parser_new ();
		_data_->parser = _data_->_tmp19_;
		_data_->_tmp20_ = _data_->message;
		g_object_get (_data_->_tmp20_, "status-code", &_data_->_tmp21_, NULL);
		_data_->_tmp22_ = _data_->_tmp21_;
		if (_data_->_tmp22_ != ((guint) 200)) {
			_data_->_tmp23_ = services_log_service_get_default ();
			_data_->_tmp24_ = _data_->_tmp23_;
			_data_->_tmp25_ = _data_->message;
			g_object_get (_data_->_tmp25_, "status-code", &_data_->_tmp26_, NULL);
			_data_->_tmp27_ = _data_->_tmp26_;
			_data_->_tmp28_ = g_strdup_printf ("Authentication failed: HTTP %u", _data_->_tmp27_);
			_data_->_tmp29_ = _data_->_tmp28_;
			services_log_service_error (_data_->_tmp24_, "Todoist", _data_->_tmp29_);
			_g_free0 (_data_->_tmp29_);
			_g_object_unref0 (_data_->_tmp24_);
			_data_->_tmp30_ = _data_->message;
			g_object_get (_data_->_tmp30_, "status-code", &_data_->_tmp31_, NULL);
			_data_->_tmp32_ = _data_->_tmp31_;
			http_response_set_error_code (_data_->response, (gint) _data_->_tmp32_);
			http_response_set_error (_data_->response, _ ("Invalid API token"));
			http_response_set_status (_data_->response, FALSE);
			_g_object_unref0 (_data_->parser);
			_g_bytes_unref0 (_data_->stream);
			_g_free0 (_data_->form_data);
			_g_object_unref0 (_data_->message);
			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;
		}
		_data_->_tmp33_ = services_log_service_get_default ();
		_data_->_tmp34_ = _data_->_tmp33_;
		services_log_service_info (_data_->_tmp34_, "Todoist", "Token validated successfully");
		_g_object_unref0 (_data_->_tmp34_);
		_data_->_tmp35_ = _data_->parser;
		_data_->_tmp36_ = _data_->stream;
		_data_->_tmp37_ = 0;
		_data_->_tmp38_ = g_bytes_get_data (_data_->_tmp36_, &_data_->_tmp37_);
		json_parser_load_from_data (_data_->_tmp35_, (const gchar*) _data_->_tmp38_, (gssize) -1, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->parser);
			_g_bytes_unref0 (_data_->stream);
			goto __catch0_g_error;
		}
		_data_->_tmp39_ = objects_source_new ();
		_data_->source = _data_->_tmp39_;
		_data_->_tmp40_ = _data_->source;
		_data_->_tmp41_ = util_get_default ();
		_data_->_tmp42_ = _data_->_tmp41_;
		_data_->_tmp43_ = util_generate_id (_data_->_tmp42_, NULL);
		_data_->_tmp44_ = _data_->_tmp43_;
		objects_base_object_set_id ((ObjectsBaseObject*) _data_->_tmp40_, _data_->_tmp44_);
		_g_free0 (_data_->_tmp44_);
		_g_object_unref0 (_data_->_tmp42_);
		_data_->_tmp45_ = _data_->source;
		objects_source_set_source_type (_data_->_tmp45_, SOURCE_TYPE_TODOIST);
		_data_->_tmp46_ = objects_source_todoist_data_new ();
		_data_->todoist_data = _data_->_tmp46_;
		_data_->_tmp47_ = _data_->todoist_data;
		_data_->_tmp48_ = _data_->parser;
		_data_->_tmp49_ = json_parser_get_root (_data_->_tmp48_);
		_data_->_tmp50_ = json_node_get_object (_data_->_tmp49_);
		_data_->_tmp51_ = json_object_get_string_member (_data_->_tmp50_, "sync_token");
		objects_source_todoist_data_set_sync_token (_data_->_tmp47_, _data_->_tmp51_);
		_data_->_tmp52_ = _data_->todoist_data;
		objects_source_todoist_data_set_access_token (_data_->_tmp52_, _data_->token);
		_data_->_tmp53_ = _data_->todoist_data;
		objects_source_todoist_data_set_api_version (_data_->_tmp53_, "v1");
		_data_->_tmp54_ = _data_->source;
		objects_source_set_sync_server (_data_->_tmp54_, TRUE);
		_data_->_tmp55_ = _data_->parser;
		_data_->_tmp56_ = json_parser_get_root (_data_->_tmp55_);
		_data_->_tmp57_ = json_node_get_object (_data_->_tmp56_);
		_data_->_tmp58_ = json_object_get_object_member (_data_->_tmp57_, "user");
		_data_->_tmp59_ = _json_object_ref0 (_data_->_tmp58_);
		_data_->user_object = _data_->_tmp59_;
		_data_->_tmp60_ = _data_->todoist_data;
		_data_->_tmp61_ = _data_->user_object;
		_data_->_tmp62_ = json_object_get_string_member (_data_->_tmp61_, "id");
		objects_source_todoist_data_set_user_id (_data_->_tmp60_, _data_->_tmp62_);
		_data_->_tmp63_ = _data_->user_object;
		if (json_object_get_null_member (_data_->_tmp63_, "image_id") == FALSE) {
			_data_->_tmp64_ = _data_->todoist_data;
			_data_->_tmp65_ = _data_->user_object;
			_data_->_tmp66_ = json_object_get_string_member (_data_->_tmp65_, "image_id");
			objects_source_todoist_data_set_user_image_id (_data_->_tmp64_, _data_->_tmp66_);
			_data_->_tmp67_ = _data_->todoist_data;
			_data_->_tmp68_ = _data_->user_object;
			_data_->_tmp69_ = json_object_get_string_member (_data_->_tmp68_, "avatar_s640");
			objects_source_todoist_data_set_user_avatar (_data_->_tmp67_, _data_->_tmp69_);
		}
		_data_->_tmp70_ = _data_->todoist_data;
		_data_->_tmp71_ = _data_->user_object;
		_data_->_tmp72_ = json_object_get_string_member (_data_->_tmp71_, "full_name");
		objects_source_todoist_data_set_user_name (_data_->_tmp70_, _data_->_tmp72_);
		_data_->_tmp73_ = _data_->todoist_data;
		_data_->_tmp74_ = _data_->user_object;
		_data_->_tmp75_ = json_object_get_string_member (_data_->_tmp74_, "email");
		objects_source_todoist_data_set_user_email (_data_->_tmp73_, _data_->_tmp75_);
		_data_->_tmp76_ = _data_->todoist_data;
		_data_->_tmp77_ = _data_->user_object;
		objects_source_todoist_data_set_user_is_premium (_data_->_tmp76_, json_object_get_boolean_member (_data_->_tmp77_, "is_premium"));
		_data_->_tmp78_ = _data_->source;
		_data_->_tmp79_ = _data_->user_object;
		_data_->_tmp80_ = json_object_get_string_member (_data_->_tmp79_, "email");
		objects_source_set_display_name (_data_->_tmp78_, _data_->_tmp80_);
		_data_->_tmp81_ = _data_->source;
		_data_->_tmp82_ = _data_->todoist_data;
		objects_source_set_data (_data_->_tmp81_, (ObjectsSourceData*) _data_->_tmp82_);
		_data_->_tmp83_ = services_store_instance ();
		_data_->_tmp84_ = _data_->todoist_data;
		_data_->_tmp85_ = objects_source_todoist_data_get_user_email (_data_->_tmp84_);
		_data_->_tmp86_ = _data_->_tmp85_;
		if (services_store_source_todoist_exists (_data_->_tmp83_, _data_->_tmp86_)) {
			if (_data_->migrate_source == NULL) {
				_data_->_tmp87_ = TRUE;
			} else {
				_data_->_tmp88_ = objects_source_get_todoist_data (_data_->migrate_source);
				_data_->_tmp89_ = _data_->_tmp88_;
				_data_->_tmp90_ = objects_source_todoist_data_get_user_email (_data_->_tmp89_);
				_data_->_tmp91_ = _data_->_tmp90_;
				_data_->_tmp92_ = _data_->todoist_data;
				_data_->_tmp93_ = objects_source_todoist_data_get_user_email (_data_->_tmp92_);
				_data_->_tmp94_ = _data_->_tmp93_;
				_data_->_tmp87_ = g_strcmp0 (_data_->_tmp91_, _data_->_tmp94_) != 0;
			}
			if (_data_->_tmp87_) {
				http_response_set_error_code (_data_->response, 409);
				http_response_set_error (_data_->response, "Source already exists");
				http_response_set_status (_data_->response, FALSE);
				_json_object_unref0 (_data_->user_object);
				_g_object_unref0 (_data_->todoist_data);
				_g_object_unref0 (_data_->source);
				_g_object_unref0 (_data_->parser);
				_g_bytes_unref0 (_data_->stream);
				_g_free0 (_data_->form_data);
				_g_object_unref0 (_data_->message);
				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;
			}
		}
		_data_->_tmp95_ = services_store_instance ();
		_data_->_tmp96_ = _data_->source;
		services_store_insert_source (_data_->_tmp95_, _data_->_tmp96_);
		_data_->_tmp97_ = _data_->parser;
		_data_->_tmp98_ = json_parser_get_root (_data_->_tmp97_);
		_data_->_tmp99_ = json_node_get_object (_data_->_tmp98_);
		_data_->_tmp100_ = json_object_get_array_member (_data_->_tmp99_, SERVICES_TODOIST_AUTH_LABELS_COLLECTION);
		_data_->labels = _data_->_tmp100_;
		_data_->_tmp101_ = _data_->labels;
		_data_->_tmp102_ = json_array_get_elements (_data_->_tmp101_);
		{
			_data_->_node_collection = _data_->_tmp102_;
			for (_data_->_node_it = _data_->_node_collection; _data_->_node_it != NULL; _data_->_node_it = _data_->_node_it->next) {
				_data_->_node = (JsonNode*) _data_->_node_it->data;
				{
					_data_->_tmp103_ = _data_->_node;
					_data_->_tmp104_ = objects_label_new_from_json (_data_->_tmp103_);
					_data_->_label = _data_->_tmp104_;
					_data_->_tmp105_ = _data_->_label;
					_data_->_tmp106_ = _data_->source;
					_data_->_tmp107_ = objects_base_object_get_id ((ObjectsBaseObject*) _data_->_tmp106_);
					_data_->_tmp108_ = _data_->_tmp107_;
					objects_label_set_source_id (_data_->_tmp105_, _data_->_tmp108_);
					_data_->_tmp109_ = services_store_instance ();
					_data_->_tmp110_ = _data_->_label;
					services_store_insert_label (_data_->_tmp109_, _data_->_tmp110_);
					_g_object_unref0 (_data_->_label);
				}
			}
			(_data_->_node_collection == NULL) ? NULL : (_data_->_node_collection = (g_list_free (_data_->_node_collection), NULL));
		}
		_data_->_tmp111_ = _data_->parser;
		_data_->_tmp112_ = json_parser_get_root (_data_->_tmp111_);
		_data_->_tmp113_ = json_node_get_object (_data_->_tmp112_);
		_data_->_tmp114_ = json_object_get_array_member (_data_->_tmp113_, SERVICES_TODOIST_AUTH_PROJECTS_COLLECTION);
		_data_->projects = _data_->_tmp114_;
		_data_->_tmp115_ = _data_->projects;
		_data_->_tmp116_ = json_array_get_elements (_data_->_tmp115_);
		{
			_data_->_vala1__node_collection = _data_->_tmp116_;
			for (_data_->_vala1__node_it = _data_->_vala1__node_collection; _data_->_vala1__node_it != NULL; _data_->_vala1__node_it = _data_->_vala1__node_it->next) {
				_data_->_vala1__node = (JsonNode*) _data_->_vala1__node_it->data;
				{
					_data_->_tmp117_ = _data_->_vala1__node;
					_data_->_tmp118_ = objects_project_new_from_json (_data_->_tmp117_);
					_data_->_project = _data_->_tmp118_;
					_data_->_tmp119_ = _data_->_project;
					_data_->_tmp120_ = _data_->source;
					_data_->_tmp121_ = objects_base_object_get_id ((ObjectsBaseObject*) _data_->_tmp120_);
					_data_->_tmp122_ = _data_->_tmp121_;
					objects_project_set_source_id (_data_->_tmp119_, _data_->_tmp122_);
					_data_->_tmp123_ = _data_->_vala1__node;
					_data_->_tmp124_ = json_node_get_object (_data_->_tmp123_);
					if (!json_object_get_null_member (_data_->_tmp124_, "parent_id")) {
						_data_->_tmp125_ = services_store_instance ();
						_data_->_tmp126_ = _data_->_vala1__node;
						_data_->_tmp127_ = json_node_get_object (_data_->_tmp126_);
						_data_->_tmp128_ = json_object_get_string_member (_data_->_tmp127_, "parent_id");
						_data_->_tmp129_ = services_store_get_project (_data_->_tmp125_, _data_->_tmp128_);
						_data_->project = _data_->_tmp129_;
						_data_->_tmp130_ = _data_->project;
						if (_data_->_tmp130_ != NULL) {
							_data_->_tmp131_ = _data_->project;
							_data_->_tmp132_ = _data_->_project;
							_data_->_tmp133_ = objects_project_add_subproject_if_not_exists (_data_->_tmp131_, _data_->_tmp132_);
							_data_->_tmp134_ = _data_->_tmp133_;
							_g_object_unref0 (_data_->_tmp134_);
						}
						_g_object_unref0 (_data_->project);
					} else {
						_data_->_tmp135_ = services_store_instance ();
						_data_->_tmp136_ = _data_->_project;
						services_store_insert_project (_data_->_tmp135_, _data_->_tmp136_);
					}
					_g_object_unref0 (_data_->_project);
				}
			}
			(_data_->_vala1__node_collection == NULL) ? NULL : (_data_->_vala1__node_collection = (g_list_free (_data_->_vala1__node_collection), NULL));
		}
		_data_->_tmp137_ = _data_->parser;
		_data_->_tmp138_ = json_parser_get_root (_data_->_tmp137_);
		_data_->_tmp139_ = json_node_get_object (_data_->_tmp138_);
		_data_->_tmp140_ = json_object_get_array_member (_data_->_tmp139_, SERVICES_TODOIST_AUTH_SECTIONS_COLLECTION);
		_data_->sections = _data_->_tmp140_;
		_data_->_tmp141_ = _data_->sections;
		_data_->_tmp142_ = json_array_get_elements (_data_->_tmp141_);
		{
			_data_->_vala2__node_collection = _data_->_tmp142_;
			for (_data_->_vala2__node_it = _data_->_vala2__node_collection; _data_->_vala2__node_it != NULL; _data_->_vala2__node_it = _data_->_vala2__node_it->next) {
				_data_->_vala2__node = (JsonNode*) _data_->_vala2__node_it->data;
				{
					_data_->_tmp143_ = services_todoist_get_default ();
					_data_->_tmp144_ = _data_->_tmp143_;
					_data_->_tmp145_ = _data_->_vala2__node;
					services_todoist_add_section_if_not_exists (_data_->_tmp144_, _data_->_tmp145_);
					_g_object_unref0 (_data_->_tmp144_);
				}
			}
			(_data_->_vala2__node_collection == NULL) ? NULL : (_data_->_vala2__node_collection = (g_list_free (_data_->_vala2__node_collection), NULL));
		}
		_data_->_tmp146_ = _data_->parser;
		_data_->_tmp147_ = json_parser_get_root (_data_->_tmp146_);
		_data_->_tmp148_ = json_node_get_object (_data_->_tmp147_);
		_data_->_tmp149_ = json_object_get_array_member (_data_->_tmp148_, SERVICES_TODOIST_AUTH_ITEMS_COLLECTION);
		_data_->items = _data_->_tmp149_;
		_data_->_tmp150_ = _data_->items;
		_data_->_tmp151_ = json_array_get_elements (_data_->_tmp150_);
		{
			_data_->_vala3__node_collection = _data_->_tmp151_;
			for (_data_->_vala3__node_it = _data_->_vala3__node_collection; _data_->_vala3__node_it != NULL; _data_->_vala3__node_it = _data_->_vala3__node_it->next) {
				_data_->_vala3__node = (JsonNode*) _data_->_vala3__node_it->data;
				{
					_data_->_tmp152_ = services_todoist_get_default ();
					_data_->_tmp153_ = _data_->_tmp152_;
					_data_->_tmp154_ = _data_->_vala3__node;
					services_todoist_add_item_if_not_exists (_data_->_tmp153_, _data_->_tmp154_);
					_g_object_unref0 (_data_->_tmp153_);
				}
			}
			(_data_->_vala3__node_collection == NULL) ? NULL : (_data_->_vala3__node_collection = (g_list_free (_data_->_vala3__node_collection), NULL));
		}
		_data_->_tmp155_ = _data_->parser;
		_data_->_tmp156_ = json_parser_get_root (_data_->_tmp155_);
		_data_->_tmp157_ = json_node_get_object (_data_->_tmp156_);
		_data_->_tmp158_ = json_object_get_array_member (_data_->_tmp157_, SERVICES_TODOIST_AUTH_REMINDERS_COLLECTION);
		_data_->reminders = _data_->_tmp158_;
		_data_->_tmp159_ = _data_->reminders;
		_data_->_tmp160_ = json_array_get_elements (_data_->_tmp159_);
		{
			_data_->_vala4__node_collection = _data_->_tmp160_;
			for (_data_->_vala4__node_it = _data_->_vala4__node_collection; _data_->_vala4__node_it != NULL; _data_->_vala4__node_it = _data_->_vala4__node_it->next) {
				_data_->_vala4__node = (JsonNode*) _data_->_vala4__node_it->data;
				{
					_data_->_tmp161_ = _data_->_vala4__node;
					_data_->_tmp162_ = objects_reminder_new_from_json (_data_->_tmp161_);
					_data_->reminder = _data_->_tmp162_;
					_data_->_tmp163_ = services_store_instance ();
					_data_->_tmp164_ = _data_->reminder;
					_data_->_tmp165_ = objects_reminder_get_item_id (_data_->_tmp164_);
					_data_->_tmp166_ = _data_->_tmp165_;
					_data_->_tmp167_ = services_store_get_item (_data_->_tmp163_, _data_->_tmp166_);
					_data_->item = _data_->_tmp167_;
					_data_->_tmp168_ = _data_->item;
					if (_data_->_tmp168_ != NULL) {
						_data_->_tmp169_ = _data_->item;
						_data_->_tmp170_ = _data_->reminder;
						_data_->_tmp171_ = objects_item_add_reminder_if_not_exists (_data_->_tmp169_, _data_->_tmp170_, TRUE);
						_data_->_tmp172_ = _data_->_tmp171_;
						_g_object_unref0 (_data_->_tmp172_);
					}
					_g_object_unref0 (_data_->item);
					_g_object_unref0 (_data_->reminder);
				}
			}
			(_data_->_vala4__node_collection == NULL) ? NULL : (_data_->_vala4__node_collection = (g_list_free (_data_->_vala4__node_collection), NULL));
		}
		_data_->_tmp173_ = _data_->user_object;
		if (json_object_get_null_member (_data_->_tmp173_, "image_id") == FALSE) {
			_data_->_tmp174_ = util_get_default ();
			_data_->_tmp175_ = _data_->_tmp174_;
			_data_->_tmp176_ = _data_->source;
			_data_->_tmp177_ = objects_source_get_todoist_data (_data_->_tmp176_);
			_data_->_tmp178_ = _data_->_tmp177_;
			_data_->_tmp179_ = objects_source_todoist_data_get_user_image_id (_data_->_tmp178_);
			_data_->_tmp180_ = _data_->_tmp179_;
			_data_->_tmp181_ = _data_->user_object;
			_data_->_tmp182_ = json_object_get_string_member (_data_->_tmp181_, "avatar_s640");
			util_download_profile_image (_data_->_tmp175_, _data_->_tmp180_, _data_->_tmp182_);
			_g_object_unref0 (_data_->_tmp175_);
		}
		_data_->_tmp183_ = _data_->source;
		_data_->_tmp184_ = g_date_time_new_now_local ();
		_data_->_tmp185_ = _data_->_tmp184_;
		_data_->_tmp186_ = g_date_time_to_string (_data_->_tmp185_);
		_data_->_tmp187_ = _data_->_tmp186_;
		objects_source_set_last_sync (_data_->_tmp183_, _data_->_tmp187_);
		_g_free0 (_data_->_tmp187_);
		_g_date_time_unref0 (_data_->_tmp185_);
		_data_->_tmp188_ = _data_->source;
		objects_source_save (_data_->_tmp188_);
		if (_data_->migrate_source != NULL) {
			services_todoist_auth_check_and_update_inbox_project (_data_->self, _data_->migrate_source);
			objects_source_delete_source (_data_->migrate_source, NULL, NULL);
		}
		http_response_set_status (_data_->response, TRUE);
		_data_->_tmp189_ = services_log_service_get_default ();
		_data_->_tmp190_ = _data_->_tmp189_;
		services_log_service_info (_data_->_tmp190_, "Todoist", "Account added successfully");
		_g_object_unref0 (_data_->_tmp190_);
		_json_object_unref0 (_data_->user_object);
		_g_object_unref0 (_data_->todoist_data);
		_g_object_unref0 (_data_->source);
		_g_object_unref0 (_data_->parser);
		_g_bytes_unref0 (_data_->stream);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp191_ = services_log_service_get_default ();
		_data_->_tmp192_ = _data_->_tmp191_;
		_data_->_tmp193_ = _data_->e;
		_data_->_tmp194_ = _data_->_tmp193_->message;
		_data_->_tmp195_ = g_strdup_printf ("Failed to add account: %s", _data_->_tmp194_);
		_data_->_tmp196_ = _data_->_tmp195_;
		services_log_service_error (_data_->_tmp192_, "Todoist", _data_->_tmp196_);
		_g_free0 (_data_->_tmp196_);
		_g_object_unref0 (_data_->_tmp192_);
		_data_->_tmp197_ = _data_->e;
		http_response_set_error_code (_data_->response, _data_->_tmp197_->code);
		_data_->_tmp198_ = _data_->e;
		_data_->_tmp199_ = _data_->_tmp198_->message;
		http_response_set_error (_data_->response, _data_->_tmp199_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_g_free0 (_data_->form_data);
		_g_object_unref0 (_data_->message);
		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;
	}
	_g_free0 (_data_->form_data);
	_g_object_unref0 (_data_->message);
	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_todoist_auth_check_and_update_inbox_project (ServicesTodoistAuth* self,
                                                      ObjectsSource* migrate_source)
{
	gchar* current_inbox_id = NULL;
	ServicesSettings* _tmp0_;
	ServicesSettings* _tmp1_;
	GSettings* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	ObjectsProject* current_inbox = NULL;
	ServicesStore* _tmp5_;
	const gchar* _tmp6_;
	ObjectsProject* _tmp7_;
	gboolean _tmp8_ = FALSE;
	ObjectsProject* _tmp9_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (migrate_source != NULL);
	_tmp0_ = services_settings_get_default ();
	_tmp1_ = _tmp0_;
	_tmp2_ = _tmp1_->settings;
	_tmp3_ = g_settings_get_string (_tmp2_, "local-inbox-project-id");
	_tmp4_ = _tmp3_;
	_g_object_unref0 (_tmp1_);
	current_inbox_id = _tmp4_;
	_tmp5_ = services_store_instance ();
	_tmp6_ = current_inbox_id;
	_tmp7_ = services_store_get_project (_tmp5_, _tmp6_);
	current_inbox = _tmp7_;
	_tmp9_ = current_inbox;
	if (_tmp9_ != NULL) {
		ObjectsProject* _tmp10_;
		const gchar* _tmp11_;
		const gchar* _tmp12_;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		_tmp10_ = current_inbox;
		_tmp11_ = objects_project_get_source_id (_tmp10_);
		_tmp12_ = _tmp11_;
		_tmp13_ = objects_base_object_get_id ((ObjectsBaseObject*) migrate_source);
		_tmp14_ = _tmp13_;
		_tmp8_ = g_strcmp0 (_tmp12_, _tmp14_) == 0;
	} else {
		_tmp8_ = FALSE;
	}
	if (_tmp8_) {
		ObjectsProject* local_inbox = NULL;
		ObjectsProject* _tmp15_;
		ObjectsProject* _tmp16_;
		_tmp15_ = services_todoist_auth_get_local_inbox_project (self);
		local_inbox = _tmp15_;
		_tmp16_ = local_inbox;
		if (_tmp16_ != NULL) {
			ServicesSettings* _tmp17_;
			ServicesSettings* _tmp18_;
			GSettings* _tmp19_;
			ObjectsProject* _tmp20_;
			const gchar* _tmp21_;
			const gchar* _tmp22_;
			_tmp17_ = services_settings_get_default ();
			_tmp18_ = _tmp17_;
			_tmp19_ = _tmp18_->settings;
			_tmp20_ = local_inbox;
			_tmp21_ = objects_base_object_get_id ((ObjectsBaseObject*) _tmp20_);
			_tmp22_ = _tmp21_;
			g_settings_set_string (_tmp19_, "local-inbox-project-id", _tmp22_);
			_g_object_unref0 (_tmp18_);
		}
		_g_object_unref0 (local_inbox);
	}
	_g_object_unref0 (current_inbox);
	_g_free0 (current_inbox_id);
}

static ObjectsProject*
services_todoist_auth_get_local_inbox_project (ServicesTodoistAuth* self)
{
	ObjectsProject* result;
	g_return_val_if_fail (self != NULL, NULL);
	{
		GeeArrayList* _project_list = NULL;
		ServicesStore* _tmp0_;
		GeeArrayList* _tmp1_;
		GeeArrayList* _tmp2_;
		gint _project_size = 0;
		GeeArrayList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _project_index = 0;
		_tmp0_ = services_store_instance ();
		_tmp1_ = services_store_get_projects (_tmp0_);
		_tmp2_ = _tmp1_;
		_project_list = _tmp2_;
		_tmp3_ = _project_list;
		_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_project_size = _tmp5_;
		_project_index = -1;
		while (TRUE) {
			gint _tmp6_;
			gint _tmp7_;
			ObjectsProject* project = NULL;
			GeeArrayList* _tmp8_;
			gpointer _tmp9_;
			gboolean _tmp10_ = FALSE;
			ObjectsProject* _tmp11_;
			const gchar* _tmp12_;
			const gchar* _tmp13_;
			gchar* _tmp14_;
			gchar* _tmp15_;
			gboolean _tmp16_;
			_project_index = _project_index + 1;
			_tmp6_ = _project_index;
			_tmp7_ = _project_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _project_list;
			_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _project_index);
			project = (ObjectsProject*) _tmp9_;
			_tmp11_ = project;
			_tmp12_ = objects_project_get_source_id (_tmp11_);
			_tmp13_ = _tmp12_;
			_tmp14_ = source_type_to_string (SOURCE_TYPE_LOCAL);
			_tmp15_ = _tmp14_;
			_tmp16_ = g_strcmp0 (_tmp13_, _tmp15_) == 0;
			_g_free0 (_tmp15_);
			if (_tmp16_) {
				ObjectsProject* _tmp17_;
				gboolean _tmp18_;
				gboolean _tmp19_;
				_tmp17_ = project;
				_tmp18_ = objects_project_get_inbox_project (_tmp17_);
				_tmp19_ = _tmp18_;
				_tmp10_ = _tmp19_;
			} else {
				_tmp10_ = FALSE;
			}
			if (_tmp10_) {
				result = project;
				return result;
			}
			_g_object_unref0 (project);
		}
	}
	result = NULL;
	return result;
}

static void
services_todoist_auth_class_init (ServicesTodoistAuthClass * klass,
                                  gpointer klass_data)
{
	services_todoist_auth_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ServicesTodoistAuth_private_offset);
	G_OBJECT_CLASS (klass)->finalize = services_todoist_auth_finalize;
}

static void
services_todoist_auth_instance_init (ServicesTodoistAuth * self,
                                     gpointer klass)
{
	self->priv = services_todoist_auth_get_instance_private (self);
}

static void
services_todoist_auth_finalize (GObject * obj)
{
	ServicesTodoistAuth * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_TODOIST_AUTH, ServicesTodoistAuth);
	_g_object_unref0 (self->priv->session);
	G_OBJECT_CLASS (services_todoist_auth_parent_class)->finalize (obj);
}

static GType
services_todoist_auth_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesTodoistAuthClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_todoist_auth_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesTodoistAuth), 0, (GInstanceInitFunc) services_todoist_auth_instance_init, NULL };
	GType services_todoist_auth_type_id;
	services_todoist_auth_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesTodoistAuth", &g_define_type_info, 0);
	ServicesTodoistAuth_private_offset = g_type_add_instance_private (services_todoist_auth_type_id, sizeof (ServicesTodoistAuthPrivate));
	return services_todoist_auth_type_id;
}

GType
services_todoist_auth_get_type (void)
{
	static volatile gsize services_todoist_auth_type_id__once = 0;
	if (g_once_init_enter (&services_todoist_auth_type_id__once)) {
		GType services_todoist_auth_type_id;
		services_todoist_auth_type_id = services_todoist_auth_get_type_once ();
		g_once_init_leave (&services_todoist_auth_type_id__once, services_todoist_auth_type_id);
	}
	return services_todoist_auth_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

