/*
 * SPDX-FileCopyrightText: 2003-2026 Sébastien Helleu <flashcode@flashtux.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/* Save/restore xfer plugin data when upgrading WeeChat */

#include <stdlib.h>
#include <string.h>

#include "../weechat-plugin.h"
#include "xfer.h"
#include "xfer-upgrade.h"
#include "xfer-buffer.h"
#include "xfer-chat.h"


/*
 * Save xfers info to upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
xfer_upgrade_save_xfers (struct t_upgrade_file *upgrade_file)
{
    /* TODO: save xfer data */
    (void) upgrade_file;
    return 1;
}

/*
 * Save upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
xfer_upgrade_save (void)
{
    int rc;
    struct t_upgrade_file *upgrade_file;

    upgrade_file = weechat_upgrade_new (XFER_UPGRADE_FILENAME,
                                        NULL, NULL, NULL);
    if (!upgrade_file)
        return 0;

    rc = xfer_upgrade_save_xfers (upgrade_file);

    weechat_upgrade_close (upgrade_file);

    return rc;
}

/*
 * Restore buffers callbacks (input and close) for buffers created by xfer plugin.
 */

void
xfer_upgrade_set_buffer_callbacks (void)
{
    struct t_infolist *infolist;
    struct t_gui_buffer *ptr_buffer;
    const char *type;

    infolist = weechat_infolist_get ("buffer", NULL, NULL);
    if (infolist)
    {
        while (weechat_infolist_next (infolist))
        {
            if (weechat_infolist_pointer (infolist, "plugin") == weechat_xfer_plugin)
            {
                ptr_buffer = weechat_infolist_pointer (infolist, "pointer");
                type = weechat_buffer_get_string (ptr_buffer, "localvar_type");
                if (strcmp (weechat_infolist_string (infolist, "name"),
                            XFER_BUFFER_NAME) == 0)
                {
                    xfer_buffer = ptr_buffer;
                    weechat_buffer_set_pointer (
                        ptr_buffer, "close_callback", &xfer_buffer_close_cb);
                    weechat_buffer_set_pointer (
                        ptr_buffer, "input_callback", &xfer_buffer_input_cb);
                }
                else if (type && (strcmp (type, "private") == 0))
                {
                    weechat_buffer_set_pointer (
                        ptr_buffer, "close_callback", &xfer_chat_buffer_close_cb);
                    weechat_buffer_set_pointer (
                        ptr_buffer, "input_callback", &xfer_chat_buffer_input_cb);
                }
            }
        }
        weechat_infolist_free (infolist);
    }
}

/*
 * Read callback for xfer upgrade file.
 */

int
xfer_upgrade_read_cb (const void *pointer, void *data,
                      struct t_upgrade_file *upgrade_file,
                      int object_id,
                      struct t_infolist *infolist)
{
    /* Make C compiler happy. */
    (void) pointer;
    (void) data;
    (void) upgrade_file;
    (void) object_id;
    (void) infolist;

    /* TODO: write xfer read cb */
    return WEECHAT_RC_OK;
}

/*
 * Load upgrade file.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
xfer_upgrade_load (void)
{
    int rc;
    struct t_upgrade_file *upgrade_file;

    xfer_upgrade_set_buffer_callbacks ();

    upgrade_file = weechat_upgrade_new (XFER_UPGRADE_FILENAME,
                                        &xfer_upgrade_read_cb, NULL, NULL);
    if (!upgrade_file)
        return 0;

    rc = weechat_upgrade_read (upgrade_file);

    weechat_upgrade_close (upgrade_file);

    xfer_buffer_refresh (NULL);

    return rc;
}
