GnuCash  4.8a-132-gcdaeb421d+
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 254 of file qofsession.cpp.

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

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

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

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