GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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)
 
void gnc_state_save (const QofSession *session)
 
GKeyFile * gnc_state_get_current (void)
 
gint gnc_state_drop_sections_for (const gchar *partial_name)
 

Detailed Description

Function Documentation

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 264 of file gnc-state.c.

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

253 {
254  if (!state_file)
255  {
256  PINFO ("No pre-existing state found, creating new one");
257  state_file = g_key_file_new ();
258  }
259 
260  return state_file;
261 
262 }
#define PINFO(format, args...)
Definition: qoflog.h:249
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 200 of file gnc-state.c.

201 {
202 
203  GKeyFile *keyfile = NULL;
204  GError *error = NULL;
205 
206  /* Drop possible previous state_file first */
207  if (state_file)
208  {
209  g_key_file_free (state_file);
210  state_file = NULL;
211  }
212 
213  gnc_state_set_base (session);
214 
215  if (state_file_name_pre_241)
216  state_file = gnc_key_file_load_from_file (state_file_name_pre_241,
217  TRUE, TRUE, NULL);
218  else if (state_file_name)
219  state_file = gnc_key_file_load_from_file (state_file_name,
220  TRUE, TRUE, NULL);
221 
222  return gnc_state_get_current ();
223 
224 }
GKeyFile * gnc_state_get_current(void)
Definition: gnc-state.c:252
GKeyFile * gnc_key_file_load_from_file(const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
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 226 of file gnc-state.c.

227 {
228  GError *error = NULL;
229 
230  if (!qof_session_get_url(session))
231  {
232  DEBUG("No file associated with session - skip state saving");
233  return;
234  }
235 
236  gnc_state_set_base (session);
237 
238  /* Write it all out to disk */
239  if (state_file_name)
240  gnc_key_file_save_to_file(state_file_name, state_file, &error);
241  else
242  PWARN ("No state file name set, can't save state");
243 
244  if (error)
245  {
246  PERR ("Error: Failure saving state file.\n %s",
247  error->message);
248  g_error_free (error);
249  }
250 }
#define DEBUG(format, args...)
Definition: qoflog.h:255
#define PERR(format, args...)
Definition: qoflog.h:237
#define PWARN(format, args...)
Definition: qoflog.h:243
gboolean gnc_key_file_save_to_file(const gchar *filename, GKeyFile *key_file, GError **error)