GnuCash c935c2f+
Loading...
Searching...
No Matches
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.
 
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.
 
void export_coa (QofBook *) override
 Extract the chart of accounts from the current database and create a new database with it.
 
void sync (QofBook *book) override
 Synchronizes the engine contents to the backend.
 
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 ()
 
QofBookget_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.
 
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.
 

Detailed Description

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

Constructor & Destructor Documentation

◆ ~GncXmlBackend()

GncXmlBackend::~GncXmlBackend ( )

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

52{
53 session_end();
54};

Member Function Documentation

◆ commit()

void GncXmlBackend::commit ( QofInstance instance)
overridevirtual

Commits the changes from the engine to the backend data storage.

Reimplemented from QofBackend.

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

328{
329 if (qof_instance_is_dirty(instance))
330 qof_instance_mark_clean(instance);
331}
#define qof_instance_is_dirty
Return value of is_dirty flag.

◆ 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}
@ ERR_FILEIO_WRITE_ERROR
couldn't write to the file
Definition qofbackend.h:97
void set_message(std::string &&)
Set a descriptive message that can be displayed to the user when there's an error.
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.

◆ get_book()

QofBook * GncXmlBackend::get_book ( )
inline

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

45{ return m_book; }

◆ get_filename()

const char * GncXmlBackend::get_filename ( )
inline

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

44{ return m_fullpath.c_str(); }

◆ 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());
251 }
252 break;
253
254 case GNC_BOOK_XML2_FILE_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());
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");
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}
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition qofbackend.h:58
@ ERR_FILEIO_FILE_EACCES
No read access permission for the given file.
Definition qofbackend.h:100
@ ERR_FILEIO_NO_ENCODING
file does not specify encoding
Definition qofbackend.h:99
@ ERR_FILEIO_UNKNOWN_FILE_TYPE
didn't recognize the file type
Definition qofbackend.h:94
@ ERR_BACKEND_TOO_NEW
file/db version newer than what we can read
Definition qofbackend.h:69
@ ERR_FILEIO_PARSE_ERROR
couldn't parse the data in the file
Definition qofbackend.h:95
@ ERR_FILEIO_FILE_NOT_FOUND
not found / no such file
Definition qofbackend.h:92
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
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ safe_sync()

void GncXmlBackend::safe_sync ( QofBook )
inlineoverridevirtual

Perform a sync in a way that prevents data loss on a DBI backend.

Implements QofBackend.

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

42{ sync(book); } // XML sync is inherently safe.
void sync(QofBook *book) override
Synchronizes the engine contents to the backend.

◆ 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 {
124 set_error(ERR_FILEIO_FILE_NOT_FOUND);
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 */
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}
@ ERR_BACKEND_STORE_EXISTS
File exists, data would be destroyed.
Definition qofbackend.h:67
@ SESSION_READ_ONLY
Create a new store at the URI even if a store already exists there.
Definition qofsession.h:128
@ SESSION_NEW_STORE
Open will fail if the URI doesn't exist or is locked.
Definition qofsession.h:124
@ SESSION_NEW_OVERWRITE
Create a new store at the URI.
Definition qofsession.h:126
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256
void xaccLogSetBaseName(const char *basepath)
The xaccLogSetBaseName() method sets the base filepath and the root part of the journal file name.
Definition TransLog.cpp:120
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.

◆ session_end()

void GncXmlBackend::session_end ( )
overridevirtual

Implements QofBackend.

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

161{
162 if (m_book && qof_book_is_readonly (m_book))
163 {
165 return;
166 }
167
168 if (!m_linkfile.empty())
169 g_unlink (m_linkfile.c_str());
170
171 if (m_lockfd != -1)
172 {
173 close (m_lockfd);
174 m_lockfd = -1;
175 }
176
177 if (!m_lockfile.empty())
178 {
179 int rv;
180#ifdef G_OS_WIN32
181 /* On windows, we need to allow write-access before
182 g_unlink() can succeed */
183 rv = g_chmod (m_lockfile.c_str(), S_IWRITE | S_IREAD);
184#endif
185 rv = g_unlink (m_lockfile.c_str());
186 if (rv)
187 {
188 PWARN ("Error on g_unlink(%s): %d: %s", m_lockfile.c_str(),
189 errno, g_strerror (errno) ? g_strerror (errno) : "");
190 }
191 }
192
193 m_dirname.clear();
194 m_fullpath.clear();
195 m_lockfile.clear();
196 m_linkfile.clear();
197}
@ ERR_BACKEND_READONLY
cannot write to file/directory
Definition qofbackend.h:68
gboolean qof_book_is_readonly(const QofBook *book)
Return whether the book is read only.
Definition qofbook.cpp:497

◆ 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}

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