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 250 of file qofsession.cpp.

251 {
252 
253 
254  ENTER (" sess=%p mode=%d, URI=%s", this, mode, new_uri);
255  clear_error ();
256  /* Check to see if this session is already open */
257  if (m_uri.size ())
258  {
259  if (ERR_BACKEND_NO_ERR != get_error ())
260  push_error (ERR_BACKEND_LOCKED, {});
261  LEAVE("push error book is already open ");
262  return;
263  }
264 
265  /* seriously invalid */
266  if (!new_uri)
267  {
268  if (ERR_BACKEND_NO_ERR != get_error ())
269  push_error (ERR_BACKEND_BAD_URL, {});
270  LEAVE("push error missing new_uri");
271  return;
272  }
273 
274  char * scheme {g_uri_parse_scheme (new_uri)};
275  char * filename {nullptr};
276  if (g_strcmp0 (scheme, "file") == 0)
277  filename = g_filename_from_uri (new_uri, nullptr, nullptr);
278  else if (!scheme)
279  filename = g_strdup (new_uri);
280 
281  if (filename && g_file_test (filename, G_FILE_TEST_IS_DIR))
282  {
283  if (ERR_BACKEND_NO_ERR == get_error ())
284  push_error (ERR_BACKEND_BAD_URL, {});
285  g_free (filename);
286  g_free (scheme);
287  LEAVE("Can't open a directory");
288  return;
289  }
290  /* destroy the old backend */
291  destroy_backend ();
292  /* Store the session URL */
293  m_uri = new_uri;
294  m_creating = mode == SESSION_NEW_STORE || mode == SESSION_NEW_OVERWRITE;
295  if (filename)
296  load_backend ("file");
297  else /* access method found, load appropriate backend */
298  load_backend (scheme);
299  g_free (filename);
300  g_free (scheme);
301 
302  /* No backend was found. That's bad. */
303  if (m_backend == nullptr)
304  {
305  m_uri = {};
306  if (ERR_BACKEND_NO_ERR == get_error ())
307  push_error (ERR_BACKEND_BAD_URL, {});
308  LEAVE (" BAD: no backend: sess=%p book-id=%s",
309  this, new_uri);
310  return;
311  }
312 
313  /* If there's a begin method, call that. */
314  m_backend->session_begin(this, m_uri.c_str(), mode);
315  PINFO ("Done running session_begin on backend");
316  QofBackendError const err {m_backend->get_error()};
317  auto msg (m_backend->get_message());
318  if (err != ERR_BACKEND_NO_ERR)
319  {
320  m_uri = {};
321  push_error (err, msg);
322  LEAVE (" backend error %d %s", err, msg.empty() ? "(null)" : msg.c_str());
323  return;
324  }
325  if (!msg.empty())
326  {
327  PWARN("%s", msg.c_str());
328  }
329 
330  LEAVE (" sess=%p book-id=%s", this, new_uri);
331 }
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
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:371
#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 371 of file qofsession.cpp.

372 {
373  /* if we have a local error, return that. */
374  if (m_last_err != ERR_BACKEND_NO_ERR)
375  return m_last_err;
376  auto qof_be = qof_book_get_backend (m_book);
377  if (qof_be == nullptr) return ERR_BACKEND_NO_ERR;
378 
379  m_last_err = qof_be->get_error();
380  return m_last_err;
381 }
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: