GnuCash  5.6-150-g038405b370+
Files | Macros | Functions

Files

file  gnc-state.h
 Functions to load, save and get gui state.
 

Macros

#define STATE_FILE_TOP   "Top"
 
#define STATE_FILE_BOOK_GUID   "BookGuid"
 
#define STATE_FILE_EXT   ".gcm"
 

Functions

GKeyFile * gnc_state_load (const QofSession *session)
 Load the state from a state file on disk for the given session. More...
 
void gnc_state_save (const QofSession *session)
 Save the state to a state file on disk for the given session. More...
 
GKeyFile * gnc_state_get_current (void)
 Returns a pointer to the most recently loaded state. More...
 
gint gnc_state_drop_sections_for (const gchar *partial_name)
 Drop all sections from the state file whose name contains partial_name. More...
 

Detailed Description

Function Documentation

◆ gnc_state_drop_sections_for()

gint gnc_state_drop_sections_for ( const gchar *  partial_name)

Drop all sections from the state file whose name contains partial_name.

This function is meant to be called when an object is deleted for which state is kept. For example, when an account is deleted from GnuCash, all state sections that refer to it should get removed. In that case you can call this function with the account's guid as parameter.

Parameters
partial_namea string to match in the section names for most objects in GnuCash that maintain state, this will be the object's guid
Returns
The number of successfully dropped sections.

Definition at line 260 of file gnc-state.c.

261 {
262  gchar **groups;
263  gint found_count = 0, dropped_count = 0;
264  gsize i, num_groups;
265  GError *error = NULL;
266 
267  if (!state_file)
268  {
269  PWARN ("No pre-existing state found, ignoring drop request");
270  return 0;
271  }
272 
273  ENTER("");
274 
275  groups = g_key_file_get_groups (state_file, &num_groups);
276  for (i = 0; i < num_groups; i++)
277  {
278  if (g_strstr_len (groups[i], -1, partial_name))
279  {
280  DEBUG ("Section \"%s\" matches \"%s\", removing", groups[i], partial_name);
281  found_count++;
282  if (!g_key_file_remove_group (state_file, groups[i], &error))
283  {
284  PWARN ("Warning: unable to remove section %s.\n %s",
285  groups[i],
286  error->message);
287  g_error_free (error);
288  }
289  else
290  dropped_count++;
291 
292  }
293  }
294  g_strfreev (groups);
295 
296  LEAVE("Found %i sections matching \"%s\", successfully removed %i",
297  found_count, partial_name, dropped_count);
298  return dropped_count;
299 
300 }
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282

◆ gnc_state_get_current()

GKeyFile* gnc_state_get_current ( void  )

Returns a pointer to the most recently loaded state.

Returns
A pointer to a GKeyFile that holds the current state information. If there is no current state, an empty state is returned (not NULL!). This pointer should never be freed, except when gnucash is exiting !

Definition at line 248 of file gnc-state.c.

249 {
250  if (!state_file)
251  {
252  PINFO ("No pre-existing state found, creating new one");
253  state_file = g_key_file_new ();
254  }
255 
256  return state_file;
257 
258 }
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256

◆ gnc_state_load()

GKeyFile* gnc_state_load ( const QofSession *  session)

Load the state from a state file on disk for the given session.

Parameters
sessionThe session to load the state for.
Returns
A pointer to a GKeyFile that holds the state information for the given session. If no state file was found an empty state is returned (not NULL!). This pointer should never be freed, except when gnucash is exiting !

Definition at line 202 of file gnc-state.c.

203 {
204  /* Drop possible previous state_file first */
205  if (state_file)
206  {
207  g_key_file_free (state_file);
208  state_file = NULL;
209  }
210 
211  gnc_state_set_base (session);
212 
213  if (state_file_name_pre_241)
214  state_file = gnc_key_file_load_from_file (state_file_name_pre_241,
215  TRUE, TRUE, NULL);
216  else if (state_file_name)
217  state_file = gnc_key_file_load_from_file (state_file_name,
218  TRUE, TRUE, NULL);
219 
220  return gnc_state_get_current ();
221 }
GKeyFile * gnc_state_get_current(void)
Returns a pointer to the most recently loaded state.
Definition: gnc-state.c:248
GKeyFile * gnc_key_file_load_from_file(const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
Open and read a key/value file from disk into memory.

◆ gnc_state_save()

void gnc_state_save ( const QofSession *  session)

Save the state to a state file on disk for the given session.

Parameters
sessionThe session to save the state for.

Definition at line 223 of file gnc-state.c.

224 {
225  GError *error = NULL;
226 
227  if (!strlen (qof_session_get_url(session)))
228  {
229  DEBUG("No file associated with session - skip state saving");
230  return;
231  }
232 
233  gnc_state_set_base (session);
234 
235  /* Write it all out to disk */
236  if (state_file_name)
237  gnc_key_file_save_to_file(state_file_name, state_file, &error);
238  else
239  PWARN ("No state file name set, can't save state");
240 
241  if (error)
242  {
243  PERR ("Error: Cannot open state file %s", error->message);
244  g_error_free (error);
245  }
246 }
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
gboolean gnc_key_file_save_to_file(const gchar *filename, GKeyFile *key_file, GError **error)
Write a key/value file from memory to disk.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250