Example #1
0
gchar *
qof_book_increment_and_format_counter (QofBook *book, const char *counter_name)
{
    KvpFrame *kvp;
    KvpValue *value;
    gint64 counter;
    gchar* format;
    gchar* result;

    if (!book)
    {
        PWARN ("No book!!!");
        return NULL;
    }

    if (!counter_name || *counter_name == '\0')
    {
        PWARN ("Invalid counter name.");
        return NULL;
    }

    /* Get the current counter value from the KVP in the book. */
    counter = qof_book_get_counter(book, counter_name);

    /* Check if an error occurred */
    if (counter < 0)
        return NULL;

    /* Increment the counter */
    counter++;

    /* Get the KVP from the current book */
    kvp = qof_instance_get_slots (QOF_INSTANCE (book));

    if (!kvp)
    {
        PWARN ("Book has no KVP_Frame");
        return NULL;
    }

    /* Save off the new counter */
    qof_book_begin_edit(book);
    value = new KvpValue(counter);
    delete kvp->set_path({"counters", counter_name}, value);
    qof_instance_set_dirty (QOF_INSTANCE (book));
    qof_book_commit_edit(book);

    format = qof_book_get_counter_format(book, counter_name);

    if (!format)
    {
        PWARN("Cannot get format for counter");
        return NULL;
    }

    /* Generate a string version of the counter */
    result = g_strdup_printf(format, counter);
    g_free (format);
    return result;
}
Example #2
0
gint64
qof_book_get_counter (QofBook *book, const char *counter_name)
{
    QofBackend *be;
    KvpFrame *kvp;
    KvpValue *value;
    gint64 counter;

    if (!book)
    {
        PWARN ("No book!!!");
        return -1;
    }

    if (!counter_name || *counter_name == '\0')
    {
        PWARN ("Invalid counter name.");
        return -1;
    }

    /* If we've got a backend with a counter method, call it */
    be = book->backend;
    if (be && be->counter)
        return ((be->counter)(be, counter_name));

    /* If not, then use the KVP in the book */
    kvp = qof_book_get_slots (book);

    if (!kvp)
    {
        PWARN ("Book has no KVP_Frame");
        return -1;
    }

    value = kvp_frame_get_slot_path (kvp, "counters", counter_name, NULL);
    if (value)
    {
        /* found it */
        counter = kvp_value_get_gint64 (value);
    }
    else
    {
        /* New counter */
        counter = 0;
    }

    /* Counter is now valid; increment it */
    counter++;

    /* Save off the new counter */
    qof_book_begin_edit(book);
    value = kvp_value_new_gint64 (counter);
    kvp_frame_set_slot_path (kvp, value, "counters", counter_name, NULL);
    kvp_value_delete (value);
    qof_book_mark_dirty(book);
    qof_book_commit_edit(book);

    /* and return the value */
    return counter;
}
Example #3
0
void
qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val)
{
    qof_book_begin_edit(book);
    kvp_frame_set_string(qof_book_get_slots(book), opt_name, opt_val);
    qof_book_mark_dirty(book);
    qof_book_commit_edit(book);
}
Example #4
0
void
qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr)
{
    KvpFrame *frame = qof_instance_get_slots (QOF_INSTANCE (book));
    qof_book_begin_edit (book);
    delete frame->set_path({GNC_FEATURES, key}, new KvpValue(descr));
    qof_instance_set_dirty (QOF_INSTANCE (book));
    qof_book_commit_edit (book);
}
Example #5
0
void
qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val)
{
    qof_book_begin_edit(book);
    auto frame = qof_instance_get_slots(QOF_INSTANCE(book));
    delete frame->set(opt_name, new KvpValue(opt_val));
    qof_instance_set_dirty (QOF_INSTANCE (book));
    qof_book_commit_edit(book);
}
Example #6
0
void
qof_book_set_option (QofBook *book, KvpValue *value, GSList *path)
{
    KvpFrame *root = qof_instance_get_slots (QOF_INSTANCE (book));
    Path path_v {KVP_OPTION_PATH};
    for (auto item = path; item != nullptr; item = g_slist_next(item))
        path_v.push_back(static_cast<const char*>(item->data));
    qof_book_begin_edit (book);
    delete root->set_path(path_v, value);
    qof_instance_set_dirty (QOF_INSTANCE (book));
    qof_book_commit_edit (book);
}
Example #7
0
void
qof_book_save_options (QofBook *book, GNCOptionSave save_cb,
		       GNCOptionDB* odb, gboolean clear)
{
    /* Wrap this in begin/commit so that it commits only once instead of doing
     * so for every option. Qof_book_set_option will take care of dirtying the
     * book.
     */
    qof_book_begin_edit (book);
    save_cb (odb, book, clear);
    qof_book_commit_edit (book);
}
Example #8
0
void
qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val)
{
    qof_book_begin_edit(book);
    auto frame = qof_instance_get_slots(QOF_INSTANCE(book));
    if (opt_val && (*opt_val != '\0'))
        delete frame->set(opt_name, new KvpValue(g_strdup(opt_val)));
    else
        delete frame->set(opt_name, nullptr);
    qof_instance_set_dirty (QOF_INSTANCE (book));
    qof_book_commit_edit(book);
}
Example #9
0
/* ================================================================= */
static void
load_single_book (GncSqlBackend* sql_be, GncSqlRow& row)
{
    QofBook* pBook;

    g_return_if_fail (sql_be != NULL);

    gnc_sql_load_guid (sql_be, row);

    pBook = sql_be->book();
    if (pBook == NULL)
    {
        pBook = qof_book_new ();
    }

    qof_book_begin_edit (pBook);
    gnc_sql_load_object (sql_be, row, GNC_ID_BOOK, pBook, col_table);
    gnc_sql_slots_load (sql_be, QOF_INSTANCE (pBook));
    qof_book_commit_edit (pBook);

    qof_instance_mark_clean (QOF_INSTANCE (pBook));
}
Example #10
0
/* Test the gnc_dbi_load logic that forces a newer database to be
 * opened read-only and an older one to be safe-saved. Again, it would
 * be better to do this starting from a fresh file, but instead we're
 * being lazy and using an existing one. */
void
test_dbi_version_control( const gchar* driver,  const gchar* url )
{

    QofSession *sess;
    QofBook *book;
    QofBackend *qbe;
    QofBackendError err;
    gint ourversion = gnc_get_long_version();

    g_test_message ( "Testing safe save %s\n", driver );

    // Load the session data
    sess = qof_session_new();
    qof_session_begin( sess, url, TRUE, FALSE, FALSE );
    if (sess && qof_session_get_error(sess) != ERR_BACKEND_NO_ERR)
    {
        g_warning("Session Error: %d, %s", qof_session_get_error(sess),
                  qof_session_get_error_message(sess));
        do_test( FALSE, "DB Session Creation Failed");
        goto cleanup;
    }
    qof_session_load( sess, NULL );
    qbe = qof_session_get_backend( sess );
    book = qof_session_get_book( sess );
    qof_book_begin_edit( book );
    gnc_sql_set_table_version( (GncSqlBackend*)qbe,
                               "Gnucash", GNUCASH_RESAVE_VERSION - 1 );
    qof_book_commit_edit( book );
    qof_session_end( sess );
    qof_session_destroy( sess );
    sess = qof_session_new();
    qof_session_begin( sess, url, TRUE, FALSE, FALSE );
    qof_session_load( sess, NULL );
    err = qof_session_pop_error( sess );
    do_test( err == ERR_SQL_DB_TOO_OLD, "DB Failed to flag too old" );
    qbe = qof_session_get_backend( sess );
    book = qof_session_get_book( sess );
    qof_book_begin_edit( book );
    gnc_sql_set_table_version( (GncSqlBackend*)qbe,
                               "Gnucash", ourversion );
    gnc_sql_set_table_version( (GncSqlBackend*)qbe,
                               "Gnucash-Resave", ourversion + 1 );
    qof_book_commit_edit( book );
    qof_session_end( sess );
    qof_session_destroy( sess );
    sess = qof_session_new();
    qof_session_begin( sess, url, TRUE, FALSE, FALSE );
    qof_session_load( sess, NULL );
    qof_session_ensure_all_data_loaded( sess );
    err = qof_session_pop_error( sess );
    do_test( err == ERR_SQL_DB_TOO_NEW, "DB Failed to flag too new" );
cleanup:
    qbe = qof_session_get_backend( sess );
    book = qof_session_get_book( sess );
    qof_book_begin_edit( book );
    gnc_sql_set_table_version( (GncSqlBackend*)qbe,
                               "Gnucash-Resave", GNUCASH_RESAVE_VERSION );
    qof_book_commit_edit( book );
    qof_session_end( sess );
    qof_session_destroy( sess );
}
Example #11
0
void qof_book_kvp_changed (QofBook *book)
{
    qof_book_begin_edit(book);
    qof_book_mark_dirty(book);
    qof_book_commit_edit(book);
}