GnuCash  4.8a-176-g88ecf8dd1
Public Member Functions
GncXmlBackend Class Reference
Inheritance diagram for GncXmlBackend:
QofBackend

Public Member Functions

 GncXmlBackend (const GncXmlBackend &)=delete
 
GncXmlBackend operator= (const GncXmlBackend &)=delete
 
 GncXmlBackend (const GncXmlBackend &&)=delete
 
GncXmlBackend operator= (const GncXmlBackend &&)=delete
 
void session_begin (QofSession *session, const char *new_uri, SessionOpenMode mode) override
 Open the file or connect to the server. More...
 
void session_end () override
 
void load (QofBook *book, QofBackendLoadType loadType) override
 Load the minimal set of application data needed for the application to be operable at initial startup. More...
 
void export_coa (QofBook *) override
 Extract the chart of accounts from the current database and create a new database with it. More...
 
void sync (QofBook *book) override
 Synchronizes the engine contents to the backend. More...
 
void safe_sync (QofBook *book) override
 Perform a sync in a way that prevents data loss on a DBI backend.
 
void commit (QofInstance *instance) override
 Commits the changes from the engine to the backend data storage.
 
const char * get_filename ()
 
QofBook * get_book ()
 
- Public Member Functions inherited from QofBackend
 QofBackend (const QofBackend &)=delete
 
 QofBackend (const QofBackend &&)=delete
 
virtual void begin (QofInstance *)
 Called when the engine is about to make a change to a data structure. More...
 
virtual void rollback (QofInstance *)
 Revert changes in the engine and unlock the backend.
 
void set_error (QofBackendError err)
 Set the error value only if there isn't already an error already.
 
QofBackendError get_error ()
 Retrieve the currently-stored error and clear it.
 
bool check_error ()
 Report if there is an error.
 
void set_message (std::string &&)
 Set a descriptive message that can be displayed to the user when there's an error.
 
const std::string && get_message ()
 Retrieve and clear the stored error message.
 
void set_percentage (QofBePercentageFunc pctfn)
 Store and retrieve a backend-specific function for determining the progress in completing a long operation, for use with a progress meter.
 
QofBePercentageFunc get_percentage ()
 
const std::string & get_uri ()
 Retrieve the backend's storage URI.
 

Additional Inherited Members

- Static Public Member Functions inherited from QofBackend
static bool register_backend (const char *, const char *)
 Class methods for dynamically loading the several backends and for freeing them at shutdown.
 
static void release_backends ()
 
- Protected Attributes inherited from QofBackend
QofBePercentageFunc m_percentage
 
std::string m_fullpath
 Each backend resolves a fully-qualified file path. More...
 

Detailed Description

Definition at line 29 of file gnc-xml-backend.hpp.

Member Function Documentation

◆ export_coa()

void GncXmlBackend::export_coa ( QofBook *  )
overridevirtual

Extract the chart of accounts from the current database and create a new database with it.

Implemented only in the XML backend at present.

Reimplemented from QofBackend.

Definition at line 337 of file gnc-xml-backend.cpp.

338 {
339  auto out = fopen(m_fullpath.c_str(), "w");
340  if (out == NULL)
341  {
343  set_message(strerror(errno));
344  return;
345  }
346  gnc_book_write_accounts_to_xml_filehandle_v2(this, book, out);
347  fclose(out);
348 }
void set_message(std::string &&)
Set a descriptive message that can be displayed to the user when there's an error.
Definition: qof-backend.cpp:82
couldn't write to the file
Definition: qofbackend.h:97
std::string m_fullpath
Each backend resolves a fully-qualified file path.
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
Definition: qof-backend.cpp:59

◆ load()

void GncXmlBackend::load ( QofBook *  ,
QofBackendLoadType   
)
overridevirtual

Load the minimal set of application data needed for the application to be operable at initial startup.

It is assumed that the application will perform a 'run_query()' to obtain any additional data that it needs. For file-based backends, it is acceptable for the backend to return all data at load time; for SQL-based backends, it is acceptable for the backend to return no data.

Thus, for example, the old GnuCash postgres backend returned the account tree, all currencies, and the pricedb, as these were needed at startup. It did not have to return any transactions whatsoever, as these were obtained at a later stage when a user opened a register, resulting in a query being sent to the backend. The current DBI backend on the other hand loads the entire database into memory.

(Its OK to send over entities at this point, but one should be careful of the network load; also, its possible that whatever is sent is not what the user wanted anyway, which is why its better to wait for the query).

Implements QofBackend.

Definition at line 225 of file gnc-xml-backend.cpp.

226 {
227 
228  QofBackendError error;
229 
230  if (loadType != LOAD_TYPE_INITIAL_LOAD) return;
231 
232  error = ERR_BACKEND_NO_ERR;
233  m_book = book;
234 
235  int rc;
236  switch (determine_file_type (m_fullpath))
237  {
238  case GNC_BOOK_XML2_FILE:
239  rc = qof_session_load_from_xml_file_v2 (this, book,
240  GNC_BOOK_XML2_FILE);
241  if (rc == FALSE)
242  {
243  PWARN ("Syntax error in Xml File %s", m_fullpath.c_str());
244  error = ERR_FILEIO_PARSE_ERROR;
245  }
246  break;
247 
248  case GNC_BOOK_XML2_FILE_NO_ENCODING:
249  error = ERR_FILEIO_NO_ENCODING;
250  PWARN ("No character encoding in Xml File %s", m_fullpath.c_str());
251  break;
252  case GNC_BOOK_XML1_FILE:
253  rc = qof_session_load_from_xml_file (book, m_fullpath.c_str());
254  if (rc == FALSE)
255  {
256  PWARN ("Syntax error in Xml File %s", m_fullpath.c_str());
257  error = ERR_FILEIO_PARSE_ERROR;
258  }
259  break;
260  case GNC_BOOK_POST_XML2_0_0_FILE:
261  error = ERR_BACKEND_TOO_NEW;
262  PWARN ("Version of Xml file %s is newer than what we can read",
263  m_fullpath.c_str());
264  break;
265  default:
266  /* If file type wasn't known, check errno again to give the
267  user some more useful feedback for some particular error
268  conditions. */
269  switch (errno)
270  {
271  case EACCES: /* No read permission */
272  PWARN ("No read permission to file");
273  error = ERR_FILEIO_FILE_EACCES;
274  break;
275  case EISDIR: /* File is a directory - but on this error we don't arrive here */
276  PWARN ("Filename is a directory");
278  break;
279  default:
280  PWARN ("File not any known type");
282  break;
283  }
284  break;
285  }
286 
287  if (error != ERR_BACKEND_NO_ERR)
288  {
289  set_error(error);
290  }
291 
292  /* We just got done loading, it can't possibly be dirty !! */
294 }
No read access permission for the given file.
Definition: qofbackend.h:100
couldn't parse the data in the file
Definition: qofbackend.h:95
not found / no such file
Definition: qofbackend.h:92
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
didn't recognize the file type
Definition: qofbackend.h:94
file does not specify encoding
Definition: qofbackend.h:99
void qof_book_mark_session_saved(QofBook *book)
The qof_book_mark_saved() routine marks the book as having been saved (to a file, to a database)...
Definition: qofbook.cpp:467
file/db version newer than what we can read
Definition: qofbackend.h:69
std::string m_fullpath
Each backend resolves a fully-qualified file path.
gboolean qof_session_load_from_xml_file(QofBook *, const char *filename)
Read in an account group from a file.
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
Definition: qof-backend.cpp:59

◆ session_begin()

void GncXmlBackend::session_begin ( QofSession *  session,
const char *  new_uri,
SessionOpenMode  mode 
)
overridevirtual

Open the file or connect to the server.

Parameters
sessionThe QofSession that will control the backend.
new_uriThe location of the data store that the backend will use.
modeThe session open mode. See qof_session_begin().

Implements QofBackend.

Definition at line 115 of file gnc-xml-backend.cpp.

117 {
118  /* Make sure the directory is there */
119  m_fullpath = gnc_uri_get_path (new_uri);
120 
121  if (m_fullpath.empty())
122  {
124  set_message("No path specified");
125  return;
126  }
127  if (mode == SESSION_NEW_STORE && save_may_clobber_data())
128  {
130  PWARN ("Might clobber, no force");
131  return;
132  }
133 
134  if (!check_path(m_fullpath.c_str(),
135  mode == SESSION_NEW_STORE || mode == SESSION_NEW_OVERWRITE))
136  return;
137  m_dirname = g_path_get_dirname (m_fullpath.c_str());
138 
139 
140 
141  /* ---------------------------------------------------- */
142  /* We should now have a fully resolved path name.
143  * Let's start logging */
144  xaccLogSetBaseName (m_fullpath.c_str());
145  PINFO ("logpath=%s", m_fullpath.empty() ? "(null)" : m_fullpath.c_str());
146 
147  if (mode == SESSION_READ_ONLY)
148  return; // Read-only, don't care about locks.
149 
150  /* Set the lock file */
151  m_lockfile = m_fullpath + ".LCK";
152  get_file_lock(mode);
153 }
not found / no such file
Definition: qofbackend.h:92
void set_message(std::string &&)
Set a descriptive message that can be displayed to the user when there's an error.
Definition: qof-backend.cpp:82
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
Create a new store at the URI.
Definition: qofsession.h:126
Create a new store at the URI even if a store already exists there.
Definition: qofsession.h:128
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
std::string m_fullpath
Each backend resolves a fully-qualified file path.
File exists, data would be destroyed.
Definition: qofbackend.h:67
void xaccLogSetBaseName(const char *basepath)
The xaccLogSetBaseName() method sets the base filepath and the root part of the journal file name...
Definition: TransLog.c:119
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
Definition: qof-backend.cpp:59
Open will fail if the URI doesn't exist or is locked.
Definition: qofsession.h:124

◆ sync()

void GncXmlBackend::sync ( QofBook *  )
overridevirtual

Synchronizes the engine contents to the backend.

This should done by using version numbers (hack alert – the engine does not currently contain version numbers). If the engine contents are newer than what is in the backend, the data is stored to the backend. If the engine contents are older, then the engine contents are updated.

Note that this sync operation is only meant to apply to the current contents of the engine. This routine is not intended to be used to fetch entity data from the backend.

File based backends tend to use sync as if it was called dump. Data is written out into the backend, overwriting the previous data. Database backends should implement a more intelligent solution.

Implements QofBackend.

Definition at line 297 of file gnc-xml-backend.cpp.

298 {
299  /* We make an important assumption here, that we might want to change
300  * in the future: when the user says 'save', we really save the one,
301  * the only, the current open book, and nothing else. In any case the plans
302  * for multiple books have been removed in the meantime and there is just one
303  * book, no more.
304  */
305  if (m_book == nullptr) m_book = book;
306  if (book != m_book) return;
307 
308  if (qof_book_is_readonly (m_book))
309  {
310  /* Are we read-only? Don't continue in this case. */
312  return;
313  }
314 
315  write_to_file (true);
316  remove_old_files();
317 }
gboolean qof_book_is_readonly(const QofBook *book)
Return whether the book is read only.
Definition: qofbook.cpp:580
cannot write to file/directory
Definition: qofbackend.h:68
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
Definition: qof-backend.cpp:59

The documentation for this class was generated from the following files: