/*
 * 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/>.
 */

#if !defined(PURPLE_GLOBAL_HEADER_INSIDE) && !defined(PURPLE_COMPILATION)
# error "only <purple.h> may be included directly"
#endif

#ifndef PURPLE_ACCOUNT_MANAGER_BACKEND_H
#define PURPLE_ACCOUNT_MANAGER_BACKEND_H

#include <glib.h>
#include <glib-object.h>

#include <gio/gio.h>

#include "purpleaccount.h"
#include "purpleversion.h"

G_BEGIN_DECLS

#define PURPLE_ACCOUNT_MANAGER_BACKEND_ERROR purple_account_manager_backend_error_quark()

/**
 * PurpleAccountManagerBackendError:
 * @PURPLE_ACCOUNT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED: An expected
 *  virtual function was not implemented.
 *
 * Error codes returned by [class@AccountManagerBackend].
 *
 * Since: 3.0
 */
typedef enum {
    PURPLE_ACCOUNT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED PURPLE_AVAILABLE_ENUMERATOR_IN_3_0,
} PurpleAccountManagerBackendError;

/**
 * purple_account_manager_backend_error_quark:
 *
 * The error domain to identify errors from [class@AccountManagerBackend].
 *
 * Returns: The error domain.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
GQuark purple_account_manager_backend_error_quark(void);

#define PURPLE_TYPE_ACCOUNT_MANAGER_BACKEND (purple_account_manager_backend_get_type())

PURPLE_AVAILABLE_IN_3_0
G_DECLARE_DERIVABLE_TYPE(PurpleAccountManagerBackend,
                         purple_account_manager_backend,
                         PURPLE, ACCOUNT_MANAGER_BACKEND, GObject)

/**
 * PurpleAccountManagerBackend:
 *
 * A backend for [class@Purple.AccountManager] to store its data.
 *
 * Since: 3.0
 */

struct _PurpleAccountManagerBackendClass {
    /*< private >*/
    GObjectClass parent;

    /*< public >*/
    void (*delete_account_async)(PurpleAccountManagerBackend *backend, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
    gboolean (*delete_account_finish)(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

    void (*load_accounts_async)(PurpleAccountManagerBackend *backend, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
    GListModel *(*load_accounts_finish)(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

    void (*save_account_async)(PurpleAccountManagerBackend *backend, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
    gboolean (*save_account_finish)(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

    /*< private >*/
    gpointer reserved[8];
};

/**
 * purple_account_manager_backend_delete_account_async:
 * @account: (transfer none): the account to delete
 * @cancellable: (nullable): an optional #GCancellable object
 * @callback: (scope async) (nullable): a function to call when results are
 *            available
 * @data: the user data to pass to the callback
 *
 * Deletes the given account from the backend.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
void purple_account_manager_backend_delete_account_async(PurpleAccountManagerBackend *backend, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);

/**
 * purple_account_manager_backend_delete_account_finish:
 * @result: the result
 * @error: (out) (nullable): a return address for a #GError
 *
 * Finishes a previous call to
 * [method@AccountManagerBackend.delete_account_async].
 *
 * Returns: true if the account was deleted successfully.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_account_manager_backend_delete_account_finish(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

/**
 * purple_account_manager_backend_load_accounts_async:
 * @cancellable: (nullable): an optional #GCancellable object
 * @callback: (scope async) (nullable): a function to call when results are
 *            available
 * @data: the user data to pass to the callback
 *
 * Loads accounts from the backend.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
void purple_account_manager_backend_load_accounts_async(PurpleAccountManagerBackend *backend, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);

/**
 * purple_account_manager_backend_load_accounts_finish:
 * @result: the result
 * @error: (out) (nullable): a return address for a #GError
 *
 * Finishes a previous call to
 * [method@AccountManagerBackend.load_accounts_async].
 *
 * Returns: (transfer full) (nullable): A list of accounts that were loaded, or
 *          %NULL with error set.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
GListModel *purple_account_manager_backend_load_accounts_finish(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

/**
 * purple_account_manager_backend_save_account_async:
 * @account: (transfer none): the account to save
 * @cancellable: (nullable): an optional #GCancellable object
 * @callback: (scope async) (nullable): a function to call when results are
 *            available
 * @data: the user data to pass to the callback
 *
 * Saves the given account to the backend.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
void purple_account_manager_backend_save_account_async(PurpleAccountManagerBackend *backend, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);

/**
 * purple_account_manager_backend_save_account_finish:
 * @result: the result
 * @error: (out) (nullable): a return address for a #GError
 *
 * Finishes a previous call to
 * [method@AccountManagerBackend.save_account_async].
 *
 * Returns: true if the account was saved successfully.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_account_manager_backend_save_account_finish(PurpleAccountManagerBackend *backend, GAsyncResult *result, GError **error);

G_END_DECLS

#endif /* PURPLE_ACCOUNT_MANAGER_BACKEND_H */
