mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-16 18:25:11 -06:00
Factor out common quotes retrieval code into a new file gnucash-commands.cpp
This file will contain implementations of all commands that can be run via gnucash-cli.
This commit is contained in:
parent
09b270c693
commit
b56b87685b
@ -32,11 +32,13 @@ else()
|
||||
endif()
|
||||
|
||||
set(gnucash_noinst_HEADERS
|
||||
gnucash-commands.hpp
|
||||
gnucash-core-app.hpp
|
||||
)
|
||||
|
||||
set (gnucash_SOURCES
|
||||
gnucash.cpp
|
||||
gnucash-commands.cpp
|
||||
gnucash-core-app.cpp
|
||||
gnucash-gresources.c
|
||||
${GNUCASH_RESOURCE_FILE}
|
||||
@ -62,6 +64,7 @@ target_link_libraries (gnucash
|
||||
|
||||
add_executable (gnucash-cli
|
||||
gnucash-cli.cpp
|
||||
gnucash-commands.cpp
|
||||
gnucash-core-app.cpp
|
||||
${GNUCASH_RESOURCE_FILE}
|
||||
${gnucash_noinst_HEADERS}
|
||||
@ -287,7 +290,8 @@ gnc_add_scheme_targets(price-quotes
|
||||
DEPENDS "scm-engine;scm-app-utils;scm-gnome-utils")
|
||||
|
||||
set_local_dist(gnucash_DIST_local CMakeLists.txt environment.in generate-gnc-script
|
||||
gnucash.cpp gnucash-cli.cpp gnucash-core-app.cpp gnucash.rc.in gnucash-valgrind.in gnucash-gresources.xml ${gresource_files}
|
||||
gnucash.cpp gnucash-commands.cpp gnucash-cli.cpp gnucash-core-app.cpp
|
||||
gnucash.rc.in gnucash-valgrind.in gnucash-gresources.xml ${gresource_files}
|
||||
price-quotes.scm ${gnucash_noinst_HEADERS} ${gnucash_EXTRA_DIST})
|
||||
|
||||
set (gnucash_DIST ${gnucash_DIST_local} ${gnome_DIST} ${gnome_search_DIST} ${gnome_utils_DIST}
|
||||
|
@ -22,21 +22,18 @@
|
||||
*/
|
||||
#include <config.h>
|
||||
|
||||
#include <libguile.h>
|
||||
#include <guile-mappings.h>
|
||||
#ifdef __MINGW32__
|
||||
#include <Windows.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include "gnucash-commands.hpp"
|
||||
#include "gnucash-core-app.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <gnc-engine-guile.h>
|
||||
#include <glib/gi18n.h>
|
||||
#include <gnc-engine.h>
|
||||
#include <gnc-prefs.h>
|
||||
#include <gnc-prefs-utils.h>
|
||||
#include <gnc-gnome-utils.h>
|
||||
#include <gnc-session.h>
|
||||
}
|
||||
|
||||
#include <boost/locale.hpp>
|
||||
@ -47,67 +44,6 @@ namespace bl = boost::locale;
|
||||
/* This static indicates the debugging module that this .o belongs to. */
|
||||
static QofLogModule log_module = GNC_MOD_GUI;
|
||||
|
||||
static void
|
||||
inner_main_add_price_quotes(void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv)
|
||||
{
|
||||
const char* add_quotes_file = static_cast<const char*>(data);
|
||||
SCM mod, add_quotes, scm_book, scm_result = SCM_BOOL_F;
|
||||
QofSession *session = NULL;
|
||||
|
||||
scm_c_eval_string("(debug-set! stack 200000)");
|
||||
|
||||
mod = scm_c_resolve_module("gnucash price-quotes");
|
||||
scm_set_current_module(mod);
|
||||
|
||||
gnc_prefs_init ();
|
||||
qof_event_suspend();
|
||||
scm_c_eval_string("(gnc:price-quotes-install-sources)");
|
||||
|
||||
if (!gnc_quote_source_fq_installed())
|
||||
{
|
||||
std::cerr << bl::translate ("No quotes retrieved. Finance::Quote isn't "
|
||||
"installed properly.") << "\n";
|
||||
goto fail;
|
||||
}
|
||||
|
||||
add_quotes = scm_c_eval_string("gnc:book-add-quotes");
|
||||
session = gnc_get_current_session();
|
||||
if (!session) goto fail;
|
||||
|
||||
qof_session_begin(session, add_quotes_file, FALSE, FALSE, FALSE);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_load(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
scm_book = gnc_book_to_scm(qof_session_get_book(session));
|
||||
scm_result = scm_call_2(add_quotes, SCM_BOOL_F, scm_book);
|
||||
|
||||
qof_session_save(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_destroy(session);
|
||||
if (!scm_is_true(scm_result))
|
||||
{
|
||||
g_warning("Failed to add quotes to %s.", add_quotes_file);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
qof_event_resume();
|
||||
gnc_shutdown(0);
|
||||
return;
|
||||
fail:
|
||||
if (session)
|
||||
{
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR)
|
||||
g_warning("Session Error: %s",
|
||||
qof_session_get_error_message(session));
|
||||
qof_session_destroy(session);
|
||||
}
|
||||
qof_event_resume();
|
||||
gnc_shutdown(1);
|
||||
}
|
||||
|
||||
namespace Gnucash {
|
||||
|
||||
class GnucashCli : public CoreApp
|
||||
@ -115,7 +51,7 @@ namespace Gnucash {
|
||||
public:
|
||||
GnucashCli (const char* app_name);
|
||||
void parse_command_line (int argc, char **argv);
|
||||
void start (int argc, char **argv);
|
||||
int start (int argc, char **argv);
|
||||
private:
|
||||
void configure_program_options (void);
|
||||
|
||||
@ -156,14 +92,15 @@ Gnucash::GnucashCli::configure_program_options (void)
|
||||
m_opt_desc->add (quotes_options);
|
||||
}
|
||||
|
||||
void
|
||||
Gnucash::GnucashCli::start (int argc, char **argv)
|
||||
int
|
||||
Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
|
||||
{
|
||||
Gnucash::CoreApp::start();
|
||||
|
||||
if (not m_quotes_file.empty())
|
||||
scm_boot_guile (argc, argv, inner_main_add_price_quotes, (void *)m_quotes_file.c_str());
|
||||
if (m_quotes_file.empty())
|
||||
return 1;
|
||||
|
||||
return Gnucash::add_quotes (m_quotes_file);
|
||||
}
|
||||
|
||||
int
|
||||
|
118
gnucash/gnucash-commands.cpp
Normal file
118
gnucash/gnucash-commands.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* gnucash-cli.cpp -- The command line entry point for GnuCash
|
||||
*
|
||||
* Copyright (C) 2020 Geert Janssens <geert@kobaltwit.be>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
#include <config.h>
|
||||
|
||||
#include <libguile.h>
|
||||
#include <guile-mappings.h>
|
||||
#ifdef __MINGW32__
|
||||
#include <Windows.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include "gnucash-commands.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <gnc-engine-guile.h>
|
||||
#include <gnc-prefs.h>
|
||||
#include <gnc-prefs-utils.h>
|
||||
#include <gnc-gnome-utils.h>
|
||||
#include <gnc-session.h>
|
||||
}
|
||||
|
||||
#include <boost/locale.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace bl = boost::locale;
|
||||
|
||||
/* This static indicates the debugging module that this .o belongs to. */
|
||||
static QofLogModule log_module = GNC_MOD_GUI;
|
||||
|
||||
static void
|
||||
scm_add_quotes(void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv)
|
||||
{
|
||||
const char* add_quotes_file = static_cast<const char*>(data);
|
||||
SCM mod, add_quotes, scm_book, scm_result = SCM_BOOL_F;
|
||||
QofSession *session = NULL;
|
||||
|
||||
scm_c_eval_string("(debug-set! stack 200000)");
|
||||
|
||||
mod = scm_c_resolve_module("gnucash price-quotes");
|
||||
scm_set_current_module(mod);
|
||||
|
||||
gnc_prefs_init ();
|
||||
qof_event_suspend();
|
||||
scm_c_eval_string("(gnc:price-quotes-install-sources)");
|
||||
|
||||
if (!gnc_quote_source_fq_installed())
|
||||
{
|
||||
std::cerr << bl::translate ("No quotes retrieved. Finance::Quote isn't "
|
||||
"installed properly.") << "\n";
|
||||
goto fail;
|
||||
}
|
||||
|
||||
add_quotes = scm_c_eval_string("gnc:book-add-quotes");
|
||||
session = gnc_get_current_session();
|
||||
if (!session) goto fail;
|
||||
|
||||
qof_session_begin(session, add_quotes_file, FALSE, FALSE, FALSE);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_load(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
scm_book = gnc_book_to_scm(qof_session_get_book(session));
|
||||
scm_result = scm_call_2(add_quotes, SCM_BOOL_F, scm_book);
|
||||
|
||||
qof_session_save(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_destroy(session);
|
||||
if (!scm_is_true(scm_result))
|
||||
{
|
||||
g_warning("Failed to add quotes to %s.", add_quotes_file);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
qof_event_resume();
|
||||
gnc_shutdown(0);
|
||||
return;
|
||||
fail:
|
||||
if (session)
|
||||
{
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR)
|
||||
g_warning("Session Error: %s",
|
||||
qof_session_get_error_message(session));
|
||||
qof_session_destroy(session);
|
||||
}
|
||||
qof_event_resume();
|
||||
gnc_shutdown(1);
|
||||
}
|
||||
|
||||
int
|
||||
Gnucash::add_quotes (std::string &uri)
|
||||
{
|
||||
if (not uri.empty())
|
||||
scm_boot_guile (0, nullptr, scm_add_quotes, (void *)uri.c_str());
|
||||
|
||||
return 0;
|
||||
}
|
35
gnucash/gnucash-commands.hpp
Normal file
35
gnucash/gnucash-commands.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* gnucash-commands.hpp -- Implementation of all the commands that
|
||||
* can be invoked via gnucash-cli
|
||||
*
|
||||
* Copyright (C) 2020 Geert Janssens <geert@kobaltwit.be>
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef GNUCASH_COMMANDS_HPP
|
||||
#define GNUCASH_COMMANDS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Gnucash {
|
||||
|
||||
int add_quotes (std::string &uri);
|
||||
|
||||
}
|
||||
#endif
|
@ -29,6 +29,7 @@
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include "gnucash-commands.hpp"
|
||||
#include "gnucash-core-app.hpp"
|
||||
|
||||
extern "C" {
|
||||
@ -183,67 +184,6 @@ load_gnucash_modules()
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inner_main_add_price_quotes(void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv)
|
||||
{
|
||||
const char* add_quotes_file = static_cast<const char*>(data);
|
||||
SCM mod, add_quotes, scm_book, scm_result = SCM_BOOL_F;
|
||||
QofSession *session = NULL;
|
||||
|
||||
scm_c_eval_string("(debug-set! stack 200000)");
|
||||
|
||||
mod = scm_c_resolve_module("gnucash price-quotes");
|
||||
scm_set_current_module(mod);
|
||||
|
||||
gnc_prefs_init ();
|
||||
qof_event_suspend();
|
||||
scm_c_eval_string("(gnc:price-quotes-install-sources)");
|
||||
|
||||
if (!gnc_quote_source_fq_installed())
|
||||
{
|
||||
std::cerr << bl::translate ("No quotes retrieved. Finance::Quote isn't "
|
||||
"installed properly.") << "\n";
|
||||
goto fail;
|
||||
}
|
||||
|
||||
add_quotes = scm_c_eval_string("gnc:book-add-quotes");
|
||||
session = gnc_get_current_session();
|
||||
if (!session) goto fail;
|
||||
|
||||
qof_session_begin(session, add_quotes_file, FALSE, FALSE, FALSE);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_load(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
scm_book = gnc_book_to_scm(qof_session_get_book(session));
|
||||
scm_result = scm_call_2(add_quotes, SCM_BOOL_F, scm_book);
|
||||
|
||||
qof_session_save(session, NULL);
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR) goto fail;
|
||||
|
||||
qof_session_destroy(session);
|
||||
if (!scm_is_true(scm_result))
|
||||
{
|
||||
g_warning("Failed to add quotes to %s.", add_quotes_file);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
qof_event_resume();
|
||||
gnc_shutdown(0);
|
||||
return;
|
||||
fail:
|
||||
if (session)
|
||||
{
|
||||
if (qof_session_get_error(session) != ERR_BACKEND_NO_ERR)
|
||||
g_warning("Session Error: %s",
|
||||
qof_session_get_error_message(session));
|
||||
qof_session_destroy(session);
|
||||
}
|
||||
qof_event_resume();
|
||||
gnc_shutdown(1);
|
||||
}
|
||||
|
||||
static char *
|
||||
get_file_to_load (const char* file_to_load)
|
||||
{
|
||||
@ -452,10 +392,7 @@ main(int argc, char ** argv)
|
||||
/* If asked via a command line parameter, fetch quotes only */
|
||||
auto quotes_file = application.get_quotes_file ();
|
||||
if (!quotes_file.empty())
|
||||
{
|
||||
scm_boot_guile (argc, argv, inner_main_add_price_quotes, (void*) quotes_file.c_str());
|
||||
exit (0); /* never reached */
|
||||
}
|
||||
return Gnucash::add_quotes (quotes_file);
|
||||
|
||||
/* Now the module files are looked up, which might cause some library
|
||||
initialization to be run, hence gtk must be initialized beforehand. */
|
||||
|
@ -211,6 +211,7 @@ gnucash/gnome-utils/search-param.c
|
||||
gnucash/gnome-utils/tree-view-utils.c
|
||||
gnucash/gnome-utils/window-main-summarybar.c
|
||||
gnucash/gnucash-cli.cpp
|
||||
gnucash/gnucash-commands.cpp
|
||||
gnucash/gnucash-core-app.cpp
|
||||
gnucash/gnucash.cpp
|
||||
gnucash/gschemas/org.gnucash.dialogs.business.gschema.xml.in
|
||||
|
Loading…
Reference in New Issue
Block a user