GnuCash  5.6-150-g038405b370+
Public Member Functions | Friends
QofSession Struct Reference

Public Member Functions

 QofSessionImpl (QofBook *book=nullptr) noexcept
 
void begin (const char *new_uri, SessionOpenMode mode) noexcept
 Begin this session. More...
 
void swap_books (QofSessionImpl &) noexcept
 Swap books with another session.
 
void ensure_all_data_loaded () noexcept
 
void load (QofPercentageFunc) noexcept
 
void save (QofPercentageFunc) noexcept
 
void safe_save (QofPercentageFunc) noexcept
 
bool save_in_progress () const noexcept
 
bool export_session (QofSessionImpl &real_session, QofPercentageFunc) noexcept
 
bool events_pending () const noexcept
 
bool process_events () const noexcept
 
void clear_error () noexcept
 
QofBackendError pop_error () noexcept
 
std::string const & get_uri () const noexcept
 We return by reference so that a pointer to the data of the string lives long enough to make it back to C code.
 
QofBackendError get_error () noexcept
 Returns and clears the local cached error. More...
 
const std::string & get_error_message () const noexcept
 
QofBook * get_book () const noexcept
 
QofBackendget_backend () const noexcept
 
const std::string & get_file_path () const noexcept
 
bool is_saving () const noexcept
 
void end () noexcept
 Terminates the current backend.
 
void destroy_backend () noexcept
 

Friends

void qof_session_load_backend (QofSession *, const char *)
 
char const * qof_session_get_uri (QofSession *)
 
void qof_session_set_uri (QofSession *, char const *)
 

Detailed Description

Definition at line 37 of file qofsession.hpp.

Member Function Documentation

◆ begin()

void QofSession::begin ( const char *  new_uri,
SessionOpenMode  mode 
)
noexcept

Begin this session.

Definition at line 251 of file qofsession.cpp.

252 {
253 
254 
255  ENTER (" sess=%p mode=%d, URI=%s", this, mode, new_uri);
256  clear_error ();
257  /* Check to see if this session is already open */
258  if (m_uri.size ())
259  {
260  if (ERR_BACKEND_NO_ERR != get_error ())
261  push_error (ERR_BACKEND_LOCKED, {});
262  LEAVE("push error book is already open ");
263  return;
264  }
265 
266  /* seriously invalid */
267  if (!new_uri)
268  {
269  if (ERR_BACKEND_NO_ERR != get_error ())
270  push_error (ERR_BACKEND_BAD_URL, {});
271  LEAVE("push error missing new_uri");
272  return;
273  }
274 
275  char * scheme {g_uri_parse_scheme (new_uri)};
276  char * filename {nullptr};
277  if (g_strcmp0 (scheme, "file") == 0)
278  filename = gnc_uri_get_path(new_uri);
279  else if (!scheme)
280  filename = g_strdup (new_uri);
281 
282  if (filename && g_file_test (filename, G_FILE_TEST_IS_DIR))
283  {
284  if (ERR_BACKEND_NO_ERR == get_error ())
285  push_error (ERR_BACKEND_BAD_URL, {});
286  g_free (filename);
287  g_free (scheme);
288  LEAVE("Can't open a directory");
289  return;
290  }
291  /* destroy the old backend */
292  destroy_backend ();
293  /* Store the session URL */
294  m_uri = new_uri;
295  m_creating = mode == SESSION_NEW_STORE || mode == SESSION_NEW_OVERWRITE;
296  if (filename)
297  load_backend ("file");
298  else /* access method found, load appropriate backend */
299  load_backend (scheme);
300  g_free (filename);
301  g_free (scheme);
302 
303  /* No backend was found. That's bad. */
304  if (m_backend == nullptr)
305  {
306  m_uri = {};
307  if (ERR_BACKEND_NO_ERR == get_error ())
308  push_error (ERR_BACKEND_BAD_URL, {});
309  LEAVE (" BAD: no backend: sess=%p book-id=%s",
310  this, new_uri);
311  return;
312  }
313 
314  /* If there's a begin method, call that. */
315  m_backend->session_begin(this, m_uri.c_str(), mode);
316  PINFO ("Done running session_begin on backend");
317  QofBackendError const err {m_backend->get_error()};
318  auto msg (m_backend->get_message());
319  if (err != ERR_BACKEND_NO_ERR)
320  {
321  m_uri = {};
322  push_error (err, msg);
323  LEAVE (" backend error %d %s", err, msg.empty() ? "(null)" : msg.c_str());
324  return;
325  }
326  if (!msg.empty())
327  {
328  PWARN("%s", msg.c_str());
329  }
330 
331  LEAVE (" sess=%p book-id=%s", this, new_uri);
332 }
const std::string && get_message()
Retrieve and clear the stored error message.
Definition: qof-backend.cpp:85
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:57
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
Can't parse url.
Definition: qofbackend.h:62
in use by another user (ETXTBSY)
Definition: qofbackend.h:66
Create a new store at the URI.
Definition: qofsession.h:126
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
virtual void session_begin(QofSession *session, const char *new_uri, SessionOpenMode mode)=0
Open the file or connect to the server.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
QofBackendError get_error() noexcept
Returns and clears the local cached error.
Definition: qofsession.cpp:372
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
Open will fail if the URI doesn't exist or is locked.
Definition: qofsession.h:124
QofBackendError get_error()
Retrieve the currently-stored error and clear it.
Definition: qof-backend.cpp:64

◆ get_error()

QofBackendError QofSession::get_error ( )
noexcept

Returns and clears the local cached error.

If there is no local error, we check for an error in the backend.

Definition at line 372 of file qofsession.cpp.

373 {
374  /* if we have a local error, return that. */
375  if (m_last_err != ERR_BACKEND_NO_ERR)
376  return m_last_err;
377  auto qof_be = qof_book_get_backend (m_book);
378  if (qof_be == nullptr) return ERR_BACKEND_NO_ERR;
379 
380  m_last_err = qof_be->get_error();
381  return m_last_err;
382 }
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
Definition: qofbook.cpp:440

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