GnuCash  5.6-150-g038405b370+
Public Member Functions | Data Fields | Protected Member Functions
CsvTransImpSettings Struct Reference
Inheritance diagram for CsvTransImpSettings:
CsvImportSettings

Public Member Functions

bool save (void)
 Save the gathered widget properties to a key File. More...
 
bool load (void)
 Load the widget properties from a key File. More...
 
void remove (void)
 Remove the preset from the state file.
 
- Public Member Functions inherited from CsvImportSettings
bool save (void)
 Save the gathered widget properties to a key File. More...
 
bool load (void)
 Load the widget properties from a key File. More...
 
void remove (void)
 Remove the preset from the state file.
 

Data Fields

Accountm_base_account
 
bool m_multi_split
 
std::vector< GncTransPropType > m_column_types
 
- Data Fields inherited from CsvImportSettings
std::string m_name
 
GncImpFileFormat m_file_format
 
std::string m_encoding
 
int m_date_format
 
int m_currency_format
 
uint32_t m_skip_start_lines
 
uint32_t m_skip_end_lines
 
bool m_skip_alt_lines
 
std::string m_separators
 
bool m_load_error
 
std::vector< uint32_t > m_column_widths
 

Protected Member Functions

const char * get_group_prefix (void) override
 

Detailed Description

Definition at line 41 of file gnc-imp-settings-csv-tx.hpp.

Member Function Documentation

◆ load()

bool CsvTransImpSettings::load ( void  )

Load the widget properties from a key File.

Returns
true if there was a problem.

Definition at line 193 of file gnc-imp-settings-csv-tx.cpp.

194 {
195  if (preset_is_reserved_name (m_name))
196  return true;
197 
198  GError *key_error = nullptr;
199  m_load_error = false;
200  auto keyfile = gnc_state_get_current ();
201  auto group = get_group_prefix() + m_name;
202 
203  // Start Loading the settings
204  m_load_error = CsvImportSettings::load(); // load the common settings
205 
206  m_multi_split = g_key_file_get_boolean (keyfile, group.c_str(), CSV_MULTI_SPLIT, &key_error);
207  m_load_error |= handle_load_error (&key_error, group);
208 
209  gchar *key_char = g_key_file_get_string (keyfile, group.c_str(), CSV_ACCOUNT_GUID, &key_error);
210  if (key_char && *key_char != '\0')
211  {
212  QofBook* book = gnc_get_current_book ();
213  GncGUID guid;
214 
215  if (string_to_guid (key_char, &guid)) // find account by guid first
216  m_base_account = xaccAccountLookup (&guid, book);
217  }
218  m_load_error |= handle_load_error (&key_error, group);
219  if (key_char)
220  g_free (key_char);
221 
222  key_char = g_key_file_get_string (keyfile, group.c_str(), CSV_ACCOUNT, &key_error);
223  if (key_char && *key_char != '\0')
224  {
225  if (m_base_account == nullptr)
226  {
227  m_base_account = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), key_char);
228 
229  if (m_base_account) // save the account as guid, introduced in version 4.5
230  {
231  gchar acct_guid[GUID_ENCODING_LENGTH + 1];
232  guid_to_string_buff (xaccAccountGetGUID (m_base_account), acct_guid);
233  g_key_file_set_string (keyfile, group.c_str(), CSV_ACCOUNT_GUID, acct_guid);
234  }
235  }
236  else // check to see if saved full name is the same and save if not
237  {
238  gchar *full_name = gnc_account_get_full_name (m_base_account);
239 
240  if (g_strcmp0 (key_char, full_name) != 0)
241  g_key_file_set_string (keyfile, group.c_str(), CSV_ACCOUNT, full_name);
242  g_free (full_name);
243  }
244  }
245  m_load_error |= handle_load_error (&key_error, group);
246  if (key_char)
247  g_free (key_char);
248 
249  gsize list_len;
250  m_column_types.clear();
251  gchar** col_types_str = g_key_file_get_string_list (keyfile, group.c_str(), CSV_COL_TYPES,
252  &list_len, &key_error);
253  for (uint32_t i = 0; i < list_len; i++)
254  {
255  /* Special case a few legacy column names */
256  const char *col_type_str = col_types_str[i];
257  if (!g_strcmp0(col_type_str, "Deposit")) // -> "Amount"
258  col_type_str = gnc_csv_col_type_strs[GncTransPropType::AMOUNT];
259  if (!g_strcmp0(col_type_str, "Withdrawal")) // -> "Amount (Negated)"
260  col_type_str = gnc_csv_col_type_strs[GncTransPropType::AMOUNT_NEG];
261  if (!g_strcmp0(col_type_str, "Num")) // -> "Number"
262  col_type_str = gnc_csv_col_type_strs[GncTransPropType::NUM];
263  auto col_types_it = std::find_if (gnc_csv_col_type_strs.begin(),
264  gnc_csv_col_type_strs.end(), test_prop_type_str (col_type_str));
265  auto prop = GncTransPropType::NONE;
266  if (col_types_it != gnc_csv_col_type_strs.end())
267  {
268  /* Found a valid column type. Now check whether it is allowed
269  * in the selected mode (two-split vs multi-split) */
270  prop = sanitize_trans_prop (col_types_it->first, m_multi_split);
271  if (prop != col_types_it->first)
272  PWARN("Found column type '%s', but this is blacklisted when multi-split mode is %s. "
273  "Inserting column type 'NONE' instead'.",
274  col_types_it->second, m_multi_split ? "enabled" : "disabled");
275  }
276  else
277  PWARN("Found invalid column type '%s'. Inserting column type 'NONE' instead'.",
278  col_types_str[i]);
279  m_column_types.push_back(prop);
280  }
281  if (col_types_str)
282  g_strfreev (col_types_str);
283 
284  return m_load_error;
285 }
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Given a string, replace the given guid with the parsed one unless the given value is null...
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition: guid.cpp:173
GKeyFile * gnc_state_get_current(void)
Returns a pointer to the most recently loaded state.
Definition: gnc-state.c:248
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
#define xaccAccountGetGUID(X)
Definition: Account.h:252
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition: Account.cpp:3275
bool preset_is_reserved_name(const std::string &name)
Check whether name can be used as a preset name.
Functor to check if the above map has an element of which the value equals name.
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition: guid.h:84
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name, but uses fully-qualified names using the given separator.
Definition: Account.cpp:3133
bool load(void)
Load the widget properties from a key File.
The type used to store guids in C.
Definition: guid.h:75
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
The xaccAccountLookup() subroutine will return the account associated with the given id...
Definition: Account.cpp:2052

◆ save()

bool CsvTransImpSettings::save ( void  )

Save the gathered widget properties to a key File.

Returns
true if there was a problem in saving.

Definition at line 293 of file gnc-imp-settings-csv-tx.cpp.

294 {
295  if (preset_is_reserved_name (m_name))
296  {
297  PWARN ("Ignoring attempt to save to reserved name '%s'", m_name.c_str());
298  return true;
299  }
300 
301  if ((m_name.find('[') != std::string::npos))
302  {
303  PWARN ("Name '%s' contains invalid characters '[]'. Refusing to save", m_name.c_str());
304  return true;
305  }
306 
307  auto keyfile = gnc_state_get_current ();
308  auto group = get_group_prefix() + m_name;
309 
310  // Drop previous saved settings with this name
311  g_key_file_remove_group (keyfile, group.c_str(), nullptr);
312 
313  // Start Saving the settings
314  bool error = CsvImportSettings::save(); // save the common settings
315 
316  if (error)
317  return error;
318 
319  g_key_file_set_boolean (keyfile, group.c_str(), CSV_MULTI_SPLIT, m_multi_split);
320 
321  if (m_base_account) // also save account guid introduced in version 4.5
322  {
323  gchar acct_guid[GUID_ENCODING_LENGTH + 1];
324  guid_to_string_buff (xaccAccountGetGUID (m_base_account), acct_guid);
325  g_key_file_set_string (keyfile, group.c_str(), CSV_ACCOUNT_GUID, acct_guid);
326 
327  gchar *full_name = gnc_account_get_full_name (m_base_account);
328  g_key_file_set_string (keyfile, group.c_str(), CSV_ACCOUNT, full_name);
329  g_free (full_name);
330  }
331 
332  std::vector<const char*> col_types_str;
333  for (auto col_type : m_column_types)
334  col_types_str.push_back(gnc_csv_col_type_strs[col_type]);
335 
336  if (!col_types_str.empty())
337  g_key_file_set_string_list (keyfile, group.c_str(), CSV_COL_TYPES,
338  col_types_str.data(), col_types_str.size());
339 
340  return error;
341 }
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition: guid.cpp:173
GKeyFile * gnc_state_get_current(void)
Returns a pointer to the most recently loaded state.
Definition: gnc-state.c:248
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
#define xaccAccountGetGUID(X)
Definition: Account.h:252
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition: Account.cpp:3275
bool preset_is_reserved_name(const std::string &name)
Check whether name can be used as a preset name.
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition: guid.h:84
bool save(void)
Save the gathered widget properties to a key File.

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