GnuCash  5.6-150-g038405b370+
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 26 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 344 of file gnc-xml-backend.cpp.

345 {
346  auto out = g_fopen(m_fullpath.c_str(), "w");
347  if (out == NULL)
348  {
350  set_message(strerror(errno));
351  return;
352  }
353  gnc_book_write_accounts_to_xml_filehandle_v2(this, book, out);
354  fclose(out);
355 }
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:79
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:56

◆ 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 229 of file gnc-xml-backend.cpp.

230 {
231 
232  QofBackendError error;
233 
234  if (loadType != LOAD_TYPE_INITIAL_LOAD) return;
235 
236  error = ERR_BACKEND_NO_ERR;
237  if (m_book)
238  g_object_unref(m_book);
239  m_book = QOF_BOOK(g_object_ref(book));
240 
241  int rc;
242  switch (determine_file_type (m_fullpath))
243  {
244  case GNC_BOOK_XML2_FILE:
245  rc = qof_session_load_from_xml_file_v2 (this, book,
246  GNC_BOOK_XML2_FILE);
247  if (rc == FALSE)
248  {
249  PWARN ("Syntax error in Xml File %s", m_fullpath.c_str());
250  error = ERR_FILEIO_PARSE_ERROR;
251  }
252  break;
253 
254  case GNC_BOOK_XML2_FILE_NO_ENCODING:
255  error = ERR_FILEIO_NO_ENCODING;
256  PWARN ("No character encoding in Xml File %s", m_fullpath.c_str());
257  break;
258  case GNC_BOOK_XML1_FILE:
259  rc = qof_session_load_from_xml_file (book, m_fullpath.c_str());
260  if (rc == FALSE)
261  {
262  PWARN ("Syntax error in Xml File %s", m_fullpath.c_str());
263  error = ERR_FILEIO_PARSE_ERROR;
264  }
265  break;
266  case GNC_BOOK_POST_XML2_0_0_FILE:
267  error = ERR_BACKEND_TOO_NEW;
268  PWARN ("Version of Xml file %s is newer than what we can read",
269  m_fullpath.c_str());
270  break;
271  default:
272  /* If file type wasn't known, check errno again to give the
273  user some more useful feedback for some particular error
274  conditions. */
275  switch (errno)
276  {
277  case EACCES: /* No read permission */
278  PWARN ("No read permission to file");
279  error = ERR_FILEIO_FILE_EACCES;
280  break;
281  case EISDIR: /* File is a directory - but on this error we don't arrive here */
282  PWARN ("Filename is a directory");
284  break;
285  default:
286  PWARN ("File not any known type");
288  break;
289  }
290  break;
291  }
292 
293  if (error != ERR_BACKEND_NO_ERR)
294  {
295  set_error(error);
296  }
297 
298  /* We just got done loading, it can't possibly be dirty !! */
300 }
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:383
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:56

◆ 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 114 of file gnc-xml-backend.cpp.

116 {
117  /* Make sure the directory is there */
118  auto path_str = gnc_uri_get_path (new_uri);
119  m_fullpath = path_str;
120  g_free (path_str);
121 
122  if (m_fullpath.empty())
123  {
125  set_message("No path specified");
126  return;
127  }
128  if (mode == SESSION_NEW_STORE && save_may_clobber_data())
129  {
131  PWARN ("Might clobber, no force");
132  return;
133  }
134 
135  if (!check_path(m_fullpath.c_str(),
136  mode == SESSION_NEW_STORE || mode == SESSION_NEW_OVERWRITE))
137  return;
138 
139  auto dirname = g_path_get_dirname (m_fullpath.c_str());
140  m_dirname = dirname;
141  g_free (dirname);
142 
143 
144 
145  /* ---------------------------------------------------- */
146  /* We should now have a fully resolved path name.
147  * Let's start logging */
148  xaccLogSetBaseName (m_fullpath.c_str());
149  PINFO ("logpath=%s", m_fullpath.empty() ? "(null)" : m_fullpath.c_str());
150 
151  if (mode == SESSION_READ_ONLY)
152  return; // Read-only, don't care about locks.
153 
154  /* Set the lock file */
155  m_lockfile = m_fullpath + ".LCK";
156  get_file_lock(mode);
157 }
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:79
#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.cpp:119
void set_error(QofBackendError err)
Set the error value only if there isn't already an error already.
Definition: qof-backend.cpp:56
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 303 of file gnc-xml-backend.cpp.

304 {
305  /* We make an important assumption here, that we might want to change
306  * in the future: when the user says 'save', we really save the one,
307  * the only, the current open book, and nothing else. In any case the plans
308  * for multiple books have been removed in the meantime and there is just one
309  * book, no more.
310  */
311  if (m_book == nullptr)
312  m_book = QOF_BOOK(g_object_ref(book));
313  if (book != m_book) return;
314 
315  if (qof_book_is_readonly (m_book))
316  {
317  /* Are we read-only? Don't continue in this case. */
319  return;
320  }
321 
322  write_to_file (true);
323  remove_old_files();
324 }
gboolean qof_book_is_readonly(const QofBook *book)
Return whether the book is read only.
Definition: qofbook.cpp:497
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:56

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