/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library 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 2 of the License, or (at your option)
 * any later version.
 *
 * This library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "purpleaccountmanagernoopbackend.h"

struct _PurpleAccountManagerNoopBackend {
	PurpleAccountManagerBackend parent;
};

/******************************************************************************
 * PurpleAccountManagerBackend Implementation
 *****************************************************************************/
static void
purple_account_manager_noop_backend_delete_account_async(PurpleAccountManagerBackend *backend,
                                                         G_GNUC_UNUSED PurpleAccount *account,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer data)
{
	GTask *task = NULL;

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	g_task_set_source_tag(task,
	                      purple_account_manager_noop_backend_delete_account_async);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);
}

static gboolean
purple_account_manager_noop_backend_delete_account_finish(PurpleAccountManagerBackend *backend,
                                                          GAsyncResult *result,
                                                          GError **error)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_MANAGER_NOOP_BACKEND(backend),
	                     FALSE);
	g_return_val_if_fail(g_task_is_valid(G_TASK(result), backend), FALSE);

	return g_task_propagate_boolean(G_TASK(result), error);
}

static void
purple_account_manager_noop_backend_load_accounts_async(PurpleAccountManagerBackend *backend,
                                                        GCancellable *cancellable,
                                                        GAsyncReadyCallback callback,
                                                        gpointer data)
{
	GTask *task = NULL;
	GListStore *store = NULL;

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);

	store = g_list_store_new(PURPLE_TYPE_ACCOUNT);
	g_task_return_pointer(task, store, g_object_unref);

	g_clear_object(&task);
}

static GListModel *
purple_account_manager_noop_backend_load_accounts_finish(PurpleAccountManagerBackend *backend,
                                                         GAsyncResult *result,
                                                         GError **error)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_MANAGER_NOOP_BACKEND(backend),
	                     FALSE);
	g_return_val_if_fail(g_task_is_valid(G_TASK(result), backend), FALSE);

	return g_task_propagate_pointer(G_TASK(result), error);
}

static void
purple_account_manager_noop_backend_save_account_async(PurpleAccountManagerBackend *backend,
                                                       G_GNUC_UNUSED PurpleAccount *account,
                                                       GCancellable *cancellable,
                                                       GAsyncReadyCallback callback,
                                                       gpointer data)
{
	GTask *task = NULL;

	task = g_task_new(G_OBJECT(backend), cancellable, callback, data);
	g_task_set_source_tag(task,
	                      purple_account_manager_noop_backend_save_account_async);
	g_task_return_boolean(task, TRUE);
	g_clear_object(&task);
}

static gboolean
purple_account_manager_noop_backend_save_account_finish(PurpleAccountManagerBackend *backend,
                                                        GAsyncResult *result,
                                                        GError **error)
{
	g_return_val_if_fail(PURPLE_IS_ACCOUNT_MANAGER_NOOP_BACKEND(backend),
	                     FALSE);
	g_return_val_if_fail(g_task_is_valid(G_TASK(result), backend), FALSE);

	return g_task_propagate_boolean(G_TASK(result), error);
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE(PurpleAccountManagerNoopBackend,
                    purple_account_manager_noop_backend,
                    PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND)

static void
purple_account_manager_noop_backend_init(G_GNUC_UNUSED PurpleAccountManagerNoopBackend *backend)
{
}

static void
purple_account_manager_noop_backend_class_init(PurpleAccountManagerNoopBackendClass *klass)
{
	PurpleAccountManagerBackendClass *backend_class = NULL;

	backend_class = PURPLE_ACCOUNT_MANAGER_BACKEND_CLASS(klass);
	backend_class->delete_account_async =
		purple_account_manager_noop_backend_delete_account_async;
	backend_class->delete_account_finish =
		purple_account_manager_noop_backend_delete_account_finish;
	backend_class->load_accounts_async =
		purple_account_manager_noop_backend_load_accounts_async;
	backend_class->load_accounts_finish =
		purple_account_manager_noop_backend_load_accounts_finish;
	backend_class->save_account_async =
		purple_account_manager_noop_backend_save_account_async;
	backend_class->save_account_finish =
		purple_account_manager_noop_backend_save_account_finish;
}

/******************************************************************************
 * Public API
 *****************************************************************************/
PurpleAccountManagerBackend *
purple_account_manager_noop_backend_new(void) {
	return g_object_new(PURPLE_TYPE_ACCOUNT_MANAGER_NOOP_BACKEND, NULL);
}
