/*
 * 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_PROTOCOL_H
#define PURPLE_PROTOCOL_H

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

#include <gio/gio.h>

#include <birb.h>

#include "purpleaccountsettings.h"
#include "purpletags.h"
#include "purpleversion.h"

#define PURPLE_PROTOCOL_ERROR purple_protocol_error_quark()

/**
 * PurpleProtocolError:
 * @PURPLE_PROTOCOL_ERROR_VFUNC_NOT_IMPLEMENTED: A virtual function is not
 *                                               implemented.
 *
 * Error codes returned by protocols.
 *
 * Since: 3.0
 */
typedef enum {
    PURPLE_PROTOCOL_ERROR_VFUNC_NOT_IMPLEMENTED PURPLE_AVAILABLE_ENUMERATOR_IN_3_0,
} PurpleProtocolError;

/**
 * purple_protocol_error_quark:
 *
 * The error domain to identify errors with protocols.
 *
 * Returns: The error domain.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
GQuark purple_protocol_error_quark(void);

#define PURPLE_TYPE_PROTOCOL (purple_protocol_get_type())

/**
 * PurpleProtocol:
 *
 * #PurpleProtocol is the base type for all protocols in libpurple.
 *
 * Since: 3.0
 */

PURPLE_AVAILABLE_IN_3_0
G_DECLARE_DERIVABLE_TYPE(PurpleProtocol, purple_protocol, PURPLE, PROTOCOL,
                         GObject)

#include "purpleaccount.h"
#include "purpleconnection.h"

struct _PurpleProtocolClass {
	GObjectClass parent_class;

	char *(*generate_account_name)(PurpleProtocol *protocol, PurpleAccount *account, guint n_accounts);

	PurpleAccountSettings *(*get_default_account_settings)(PurpleProtocol *protocol);
	gboolean (*validate_account)(PurpleProtocol *protocol, PurpleAccount *account, GError **error);
	void (*delete_account)(PurpleProtocol *protocol, PurpleAccount *account);

	void (*can_connect_async)(PurpleProtocol *protocol, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);
	gboolean (*can_connect_finish)(PurpleProtocol *protocol, GAsyncResult *result, GError **error);

	PurpleConnection *(*create_connection)(PurpleProtocol *protocol, PurpleAccount *account, GError **error);

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

G_BEGIN_DECLS

/**
 * purple_protocol_delete_account:
 * @account: (transfer none): the account to delete
 *
 * Called from [method@Account.delete] to do any protocol specific clean up.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
void purple_protocol_delete_account(PurpleProtocol *protocol, PurpleAccount *account);

/**
 * purple_protocol_generate_account_name:
 * @account: the account whose name to generate
 * @n_accounts: the number of accounts
 *
 * Generates a name for an account.
 *
 * This is used to attempt to generate a simple name for an account. Something
 * like "IRC 1".
 *
 * The @n_accounts parameter is the number of existing accounts with the same
 * protocol.
 *
 * This may be called multiple times for the same account if a duplicate
 * account name is found.
 *
 * Returns: (transfer full): The new account name.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
char *purple_protocol_generate_account_name(PurpleProtocol *protocol, PurpleAccount *account, guint n_accounts);

/**
 * purple_protocol_get_default_account_settings:
 *
 * Gets the default account settings.
 *
 * Returns: (transfer full) (nullable): The default settings.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
PurpleAccountSettings *purple_protocol_get_default_account_settings(PurpleProtocol *protocol);

/**
 * purple_protocol_get_id:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the ID of a protocol.
 *
 * Returns: The ID of the protocol.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_id(PurpleProtocol *protocol);

/**
 * purple_protocol_get_name:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the translated name of a protocol.
 *
 * Returns: The translated name of the protocol.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_name(PurpleProtocol *protocol);

/**
 * purple_protocol_get_description:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the description of a protocol.
 *
 * Returns: The Description of the protocol.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_description(PurpleProtocol *protocol);

/**
 * purple_protocol_get_tags:
 *
 * Gets the tags object from the protocol.
 *
 * Returns: (transfer none): The tags instance.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
PurpleTags *purple_protocol_get_tags(PurpleProtocol *protocol);

/**
 * purple_protocol_can_connect_async:
 * @protocol: The instance.
 * @account: The [class@Purple.Account] instance.
 * @cancellable: (nullable): The [class@Gio.Cancellable] instance.
 * @callback: (scope async): The [callback@Gio.AsyncReadyCallback] to call.
 * @data: (nullable): User data to pass to @callback.
 *
 * Asks @protocol if it can determine if @account can be connected.
 *
 * Most protocol plugins will call [method@Gio.NetworkMonitor.can_reach_async]
 * to determine if a connection is possible.
 *
 * If [vfunc@Protocol.can_connect_async] is not implemented, this function will
 * create a [type@Gio.Task] that returns %TRUE.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
void purple_protocol_can_connect_async(PurpleProtocol *protocol, PurpleAccount *account, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data);

/**
 * purple_protocol_can_connect_finish:
 * @protocol: The instance.
 * @result: The [iface@Gio.AsyncResult] of the operation.
 * @error: Return address for a #GError, or %NULL.
 *
 * This should be called from the callback of
 * [method@Purple.Protocol.can_connect_async] to get the result of the call.
 *
 * Returns: %TRUE on success, otherwise %FALSE with @error optionally set.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_protocol_can_connect_finish(PurpleProtocol *protocol, GAsyncResult *result, GError **error);

/**
 * purple_protocol_create_connection:
 * @protocol: The instance.
 * @account: The [class@Purple.Account] for the connection.
 * @error: A return address for a [type@GLib.GError].
 *
 * Creates a [class@PurpleConnection] for @account.
 *
 * A protocol may indicate an error by setting @error and returning %NULL.
 *
 * Returns: (transfer full): The new connection or %NULL with @error possibly
 *          set on error.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
PurpleConnection *purple_protocol_create_connection(PurpleProtocol *protocol, PurpleAccount *account, GError **error);

/**
 * purple_protocol_get_icon_name:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the name of the icon that the protocol made available via either
 * purple_protocol_get_icon_search_path() or
 * purple_protocol_get_resource_path().
 *
 * Returns: The name of the icon for @protocol.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_icon_name(PurpleProtocol *protocol);

/**
 * purple_protocol_get_icon_search_path:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the icon search path for @protocol. This is used to allow protocol
 * plugins to install their icons in any XDG icon theme compliant directory.
 * The returned value should be the path of where the icons are on disk. See
 * gtk_icon_theme_add_search_path() for additional information.
 *
 * User interfaces will look for icons in this path named
 * %chat-&lt;protocol-name&gt; where protocol-name is the name property of @protocol.
 *
 * Returns: The file system path where the icons can be found.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_icon_search_path(PurpleProtocol *protocol);

/**
 * purple_protocol_get_icon_resource_path:
 * @protocol: The #PurpleProtocol instance.
 *
 * Gets the icon resource path for @protocol. This is used to make icons that
 * have been embedded into a plugin available to libpurple. The returned value
 * should be the path of where the icons are in the resource. See
 * gtk_icon_theme_add_resource_path() for additional information.
 *
 * User interfaces will look for icons in this path named
 * %chat-&lt;protocol-name&gt; where protocol-name is the name property of @protocol.
 *
 * Returns: The gresource path where the icons can be found.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
const gchar *purple_protocol_get_icon_resource_path(PurpleProtocol *protocol);

/**
 * purple_protocol_validate_account:
 * @account: (transfer none): the account to validate
 * @error: (out) (nullable): a return address for a #GError
 *
 * Validates that the given account and its settings are valid.
 *
 * There is a default implementation of this that will always return true.
 *
 * Returns: true if the account is valid.
 *
 * Since: 3.0
 */
PURPLE_AVAILABLE_IN_3_0
gboolean purple_protocol_validate_account(PurpleProtocol *protocol, PurpleAccount *account, GError **error);

G_END_DECLS

#endif /* PURPLE_PROTOCOL_H */
