GnuCash  5.6-150-g038405b370+
gnucash.cpp
1 /*
2  * gnucash.cpp -- The program entry point for GnuCash
3  *
4  * Copyright (C) 2006 Chris Shoemaker <c.shoemaker@cox.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA gnu@gnu.org
22  */
23 #include <config.h>
24 
25 #include <libguile.h>
26 #include <guile-mappings.h>
27 #ifdef __MINGW32__
28 #include <Windows.h>
29 #include <fcntl.h>
30 #endif
31 
32 #include "gnucash-commands.hpp"
33 #include "gnucash-core-app.hpp"
34 
35 #include <glib/gi18n.h>
36 #include <dialog-new-user.h>
37 #include <gfec.h>
38 #include <gnc-engine.h> // For define GNC_MOD_GUI
39 #include <gnc-file.h>
40 #include <gnc-filepath-utils.h>
41 #include <gnc-gnome-utils.h>
42 #include <gnc-hooks.h>
43 #include <gnc-module.h>
44 #include <gnc-path.h>
45 #include <gnc-plugin-bi-import.h>
46 #include <gnc-plugin-csv-export.h>
47 #include <gnc-plugin-csv-import.h>
50 #include <gnc-plugin-log-replay.h>
51 #include <gnc-plugin-qif-import.h>
52 #include <gnc-plugin-report-system.h>
53 #include <gnc-prefs.h>
54 #include <gnc-prefs-utils.h>
55 #include <gnc-session.h>
56 #include <gnc-splash.h>
57 #include <gnucash-register.h>
58 #include <search-core-type.h>
59 #include <top-level.h>
60 
61 #include <boost/locale.hpp>
62 #include <boost/optional.hpp>
63 #ifdef __MINGW32__
64 #include <boost/nowide/args.hpp>
65 #endif
66 #include <iostream>
67 #include <gnc-report.h>
68 #include <gnc-locale-utils.hpp>
69 #include <gnc-quotes.hpp>
70 
71 namespace bl = boost::locale;
72 
73 /* This static indicates the debugging module that this .o belongs to. */
74 static QofLogModule log_module = GNC_MOD_GUI;
75 
76 static void
77 load_gnucash_plugins()
78 {
80  gnc_plugin_csv_export_create_plugin ();
81  gnc_plugin_csv_import_create_plugin();
83  gnc_plugin_qif_import_create_plugin ();
84  gnc_plugin_log_replay_create_plugin ();
85 }
86 
87 static void
88 load_gnucash_modules()
89 {
90  struct
91  {
92  const gchar * name;
93  int version;
94  gboolean optional;
95  } modules[] =
96  {
97  { "gnucash/import-export/ofx", 0, TRUE },
98  { "gnucash/import-export/aqbanking", 0, TRUE },
99  { "gnucash/python", 0, TRUE },
100  };
101 
102  /* module initializations go here */
103  int len = sizeof(modules) / sizeof(*modules);
104  for (int i = 0; i < len; i++)
105  {
106  DEBUG("Loading module %s started", modules[i].name);
107  gnc_update_splash_screen(modules[i].name, GNC_SPLASH_PERCENTAGE_UNKNOWN);
108  if (modules[i].optional)
109  gnc_module_load_optional(modules[i].name, modules[i].version);
110  else
111  gnc_module_load(modules[i].name, modules[i].version);
112  DEBUG("Loading module %s finished", modules[i].name);
113  }
114 }
115 
116 static char *
117 get_file_to_load (const char* file_to_load)
118 {
119  if (file_to_load && *file_to_load != '\0')
120  return g_strdup(file_to_load);
121  else
122  /* Note history will always return a valid (possibly empty) string */
123  return gnc_history_get_last();
124 }
125 
126 extern SCM scm_init_sw_gnome_module(void);
127 
128 struct t_file_spec {
129  int nofile;
130  const char *file_to_load;
131 };
132 
133 static void
134 scm_run_gnucash (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv)
135 {
136  auto user_file_spec = static_cast<t_file_spec*>(data);
137 
138  scm_c_eval_string("(debug-set! stack 200000)");
139 
140  auto main_mod = scm_c_resolve_module("gnucash utilities");
141  scm_set_current_module(main_mod);
142  scm_c_use_module("gnucash app-utils");
143 
145  gnc_search_core_initialize ();
146  gnc_hook_add_dangler(HOOK_UI_SHUTDOWN, (GFunc)gnc_search_core_finalize, NULL, NULL);
148  gnc_report_init ();
149 
150  load_gnucash_plugins();
151  load_gnucash_modules();
152 
153  /* Load the scm config files before starting up the gui. This ensures that
154  * custom reports have been read into memory before the Reports
155  * menu is created. */
156  Gnucash::gnc_load_scm_config ([](const gchar *msg)
157  {
158  gnc_update_splash_screen (msg, GNC_SPLASH_PERCENTAGE_UNKNOWN);
159  PINFO ("%s", msg);
160  });
161 
162  /* Setting-up the report menu must come after the module
163  loading but before the gui initialization. */
164  gnc_plugin_report_system_new();
165 
166  /* TODO: After some more guile-extraction, this should happen even
167  before booting guile. */
168  gnc_main_gui_init();
169 
170  gnc_hook_add_dangler(HOOK_UI_SHUTDOWN, (GFunc)gnc_file_quit, NULL, NULL);
171 
172  /* Install Price Quote Sources */
173 
174  try
175  {
176  const auto checking = _("Checking Finance::Quote…");
177  gnc_update_splash_screen (checking, GNC_SPLASH_PERCENTAGE_UNKNOWN);
178  GncQuotes quotes;
179  auto found = (bl::format (std::string{_("Found Finance::Quote version {1}.")}) % quotes.version()).str();
180  auto quote_sources = quotes.sources();
181  gnc_quote_source_set_fq_installed (quotes.version().c_str(), quote_sources);
182  gnc_update_splash_screen (found.c_str(), GNC_SPLASH_PERCENTAGE_UNKNOWN);
183  }
184  catch (const GncQuoteException& err)
185  {
186  auto msg = _("Unable to load Finance::Quote.");
187  PINFO ("Attempt to load Finance::Quote returned this error message:\n");
188  PINFO ("%s", err.what());
189  gnc_update_splash_screen (msg, GNC_SPLASH_PERCENTAGE_UNKNOWN);
190  }
191 
192  gnc_hook_run(HOOK_STARTUP, NULL);
193 
194  char* fn = nullptr;
195  if (!user_file_spec->nofile && (fn = get_file_to_load (user_file_spec->file_to_load)) && *fn )
196  {
197  auto msg = _("Loading data…");
198  gnc_update_splash_screen (msg, GNC_SPLASH_PERCENTAGE_UNKNOWN);
199  gnc_file_open_file(gnc_get_splash_screen(), fn, /*open_readonly*/ FALSE);
200  g_free(fn);
201  }
202  else if (gnc_prefs_get_bool(GNC_PREFS_GROUP_NEW_USER, GNC_PREF_FIRST_STARTUP))
203  {
204  g_free(fn); /* fn could be an empty string ("") */
205  gnc_destroy_splash_screen();
206  gnc_ui_new_user_dialog();
207  }
208 
209  /* Ensure temporary preferences are temporary */
210  gnc_prefs_reset_group (GNC_PREFS_GROUP_WARNINGS_TEMP);
211 
212  gnc_destroy_splash_screen();
214 
215  gnc_hook_run(HOOK_UI_POST_STARTUP, NULL);
216  gnc_ui_start_event_loop();
217  gnc_hook_remove_dangler(HOOK_UI_SHUTDOWN, (GFunc)gnc_file_quit);
218 
219  gnc_shutdown(0);
220  return;
221 }
222 
223 namespace Gnucash {
224 
225  class Gnucash : public CoreApp
226  {
227  public:
228  Gnucash (const char* app_name);
229  void parse_command_line (int argc, char **argv);
230  int start (int argc, char **argv);
231 
232  private:
233  void configure_program_options (void);
234 
235  bool m_nofile = false;
236  };
237 
238 }
239 
240 Gnucash::Gnucash::Gnucash (const char *app_name) : Gnucash::CoreApp (app_name)
241 {
242  configure_program_options();
243 }
244 
245 
246 void
247 Gnucash::Gnucash::parse_command_line (int argc, char **argv)
248 {
249  Gnucash::CoreApp::parse_command_line (argc, argv);
250 }
251 
252 // Define command line options specific to gnucash.
253 void
254 Gnucash::Gnucash::configure_program_options (void)
255 {
256 
257  bpo::options_description app_options(_("Application Options"));
258  app_options.add_options()
259  ("nofile", bpo::bool_switch (&m_nofile),
260  _("Do not load the last file opened"));
261 
262  m_opt_desc_display->add (app_options);
263  m_opt_desc_all.add (app_options);
264 }
265 
266 int
267 Gnucash::Gnucash::start ([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
268 {
269  Gnucash::CoreApp::start();
270 
271  /* Now the module files are looked up, which might cause some library
272  initialization to be run, hence gtk must be initialized beforehand. */
273  gnc_module_system_init();
274 
275  gnc_gui_init();
276 
277  auto user_file_spec = t_file_spec {
278  m_nofile,
279  m_file_to_load ? m_file_to_load->c_str() : ""};
280  scm_boot_guile (argc, argv, scm_run_gnucash, &user_file_spec);
281 
282  return 0;
283 }
284 
285 int
286 main(int argc, char ** argv)
287 {
288  Gnucash::Gnucash application (PROJECT_NAME);
289 #ifdef __MINGW32__
290  boost::nowide::args a(argc, argv); // Fix arguments - make them UTF-8
291 #endif
292  /* We need to initialize gtk before looking up all modules */
293  if(!gtk_init_check (&argc, &argv))
294  {
295  std::cerr << bl::format (std::string{("Run '{1} --help' to see a full list of available command line options.")}) % *argv[0]
296  << "\n"
297  // Translators: Do not translate $DISPLAY! It is an environment variable for X11
298  << _("Error: could not initialize graphical user interface and option add-price-quotes was not set.\n"
299  "Perhaps you need to set the $DISPLAY environment variable?")
300  << "\n";
301  return 1;
302  }
303 
304  application.parse_command_line (argc, argv);
305  return application.start (argc, argv);
306 }
void gnc_prefs_reset_group(const gchar *group)
Reset all preferences in a group to their default values in the preferences backend.
Definition: gnc-prefs.c:369
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
void gnc_plugin_bi_import_create_plugin(void)
Create a new GncPluginbi_import object and register it.
void gnc_shutdown(int exit_status)
Shutdown gnucash.
void gnc_main_window_show_all_windows(void)
Shows all main windows.
char * gnc_history_get_last(void)
Retrieve the name of the file most recently accessed.
Functions providing the file history menu.
Preferences initialization function.
Gnome specific utility functions.
void gnc_quote_source_set_fq_installed(const char *version_string, const std::vector< std::string > &sources_list)
Update gnucash internal tables based on what Finance::Quote sources are installed.
All type declarations for the whole Gnucash engine.
void gnc_gnome_utils_init(void)
Initialize the gnome-utils library Should be run once before using any gnome-utils features...
Generic api to store and retrieve preferences.
Plugin registration of the bi-import module.
void gnc_plugin_customer_import_create_plugin(void)
Create a new GncPlugincustomer_import object and register it.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
GncMainWindow * gnc_gui_init(void)
Initialize the gnucash gui.
void gnucash_register_add_cell_types(void)
Implementation.
File path resolution utility functions.
const std::string & version() noexcept
Get the installed Finance::Quote version.
const QuoteSources & sources() noexcept
Get the available Finance::Quote sources as a std::vector.
Plugin registration of the customer_import module.