/********************************************************************\
 * 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 2 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, contact:                        *
 *                                                                  *
 * Free Software Foundation           Voice:  +1-617-542-5942       *
 * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
 * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
\********************************************************************/
/** @addtogroup Import_Export
    @{ */
/** @internal
    @file import-utilities.c
    @brief Utility functions for writing import modules.
    @author Copyright (C) 2002 Benoit Grégoire <bock@step.polymtl.ca>
*/
#include "config.h"


#include <glib.h>

#include <stdlib.h>
#include "import-utilities.h"
#include "qof.h"
#include "Account.h"
#include "Transaction.h"


/********************************************************************\
 * Setter and getter functions for the online_id kvp frame in
 * Account, Transaction and Split
\********************************************************************/

const gchar * gnc_import_get_acc_online_id(Account * account)
{
    kvp_frame * frame;
    frame = xaccAccountGetSlots(account);
    return kvp_frame_get_string(frame, "online_id");
}

/* Used in the midst of editing a transaction; make it save the
 * account data. */
void gnc_import_set_acc_online_id(Account * account,
                                  const gchar * string_value)
{
    kvp_frame * frame;
    g_return_if_fail (account != NULL);
    frame = xaccAccountGetSlots(account);
    xaccAccountBeginEdit (account);
    kvp_frame_set_str(frame, "online_id", string_value);
    qof_instance_set_dirty (QOF_INSTANCE (account));
    xaccAccountCommitEdit (account);
}

const gchar * gnc_import_get_trans_online_id(Transaction * transaction)
{
    kvp_frame * frame;
    frame = xaccTransGetSlots(transaction);
    return kvp_frame_get_string(frame, "online_id");
}
/* Not actually used */
void gnc_import_set_trans_online_id(Transaction * transaction,
                                    const gchar * string_value)
{
    kvp_frame * frame;
    xaccTransBeginEdit (transaction);
    frame = xaccTransGetSlots(transaction);
    kvp_frame_set_str (frame, "online_id", string_value);
    qof_instance_set_dirty (QOF_INSTANCE (transaction));
    xaccTransCommitEdit (transaction);
}

gboolean gnc_import_trans_has_online_id(Transaction * transaction)
{
    const gchar * online_id;
    online_id = gnc_import_get_trans_online_id(transaction);
    return (online_id != NULL && strlen(online_id) > 0);
}

const gchar * gnc_import_get_split_online_id(Split * split)
{
    kvp_frame * frame;
    frame = xaccSplitGetSlots(split);
    return kvp_frame_get_string(frame, "online_id");
}
/* Used several places in a transaction edit where many other
 * parameters are also being set, so individual commits wouldn't be
 * appropriate. */
void gnc_import_set_split_online_id(Split * split,
                                    const gchar * string_value)
{
    kvp_frame * frame;
    xaccTransBeginEdit (xaccSplitGetParent (split));
    frame = xaccSplitGetSlots(split);
    kvp_frame_set_str (frame, "online_id", string_value);
    qof_instance_set_dirty (QOF_INSTANCE (split));
    xaccTransCommitEdit (xaccSplitGetParent (split));
}

gboolean gnc_import_split_has_online_id(Split * split)
{
    const gchar * online_id;
    online_id = gnc_import_get_split_online_id(split);
    return (online_id != NULL && strlen(online_id) > 0);
}

/* @} */
