Csv imp settings - internalize prefix handling

This information is not relevant outside of the settings code
The way it's implemented now each settings module defines its
own unique prefix and the generic code can just use it when
needed.
This commit is contained in:
Geert Janssens 2018-03-08 17:59:48 +01:00
parent b577a7daf1
commit 563d716161
6 changed files with 32 additions and 23 deletions

View File

@ -42,7 +42,7 @@ extern "C"
#include "gnc-ui-util.h"
}
const std::string settings_type{"PRICE"};
constexpr auto group_prefix = "Import csv,price - ";
#define CSV_COL_TYPES "ColumnTypes"
@ -57,7 +57,6 @@ static std::shared_ptr<CsvPriceImpSettings> create_int_no_preset(void)
{
auto preset = std::make_shared<CsvPriceImpSettings>();
preset->m_name = get_no_settings();
preset->m_settings_type = settings_type;
return preset;
}
@ -101,7 +100,7 @@ const preset_vec_price& get_import_presets_price (void)
for (gsize i=0; i < grouplength; i++)
{
auto group = std::string(groups[i]);
auto gp = get_prefix() + settings_type + " - ";
auto gp = std::string {group_prefix};
auto pos = group.find(gp);
if (pos == std::string::npos)
continue;
@ -125,7 +124,6 @@ const preset_vec_price& get_import_presets_price (void)
for (auto preset_name : preset_names)
{
auto preset = std::make_shared<CsvPriceImpSettings>();
preset->m_settings_type = settings_type;
preset->m_name = preset_name;
preset->load();
presets_price.push_back(preset);
@ -147,7 +145,7 @@ CsvPriceImpSettings::load (void)
GError *key_error = nullptr;
m_load_error = false;
auto keyfile = gnc_state_get_current ();
auto group = get_prefix() + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
// Start Loading the settings
m_load_error = CsvImportSettings::load(); // load the common settings
@ -210,7 +208,7 @@ CsvPriceImpSettings::save (void)
}
auto keyfile = gnc_state_get_current ();
auto group = get_prefix() + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
// Drop previous saved settings with this name
g_key_file_remove_group (keyfile, group.c_str(), nullptr);
@ -256,3 +254,9 @@ CsvPriceImpSettings::remove (void)
CsvImportSettings::remove();
}
const char*
CsvPriceImpSettings::get_group_prefix (void)
{
return group_prefix;
}

View File

@ -64,6 +64,9 @@ void remove (void);
gnc_commodity *m_from_commodity; // Price From Commodity
gnc_commodity *m_to_currency; // Price To Currency
std::vector<GncPricePropType> m_column_types_price; // The Price Column types in order
protected:
const char* get_group_prefix (void) override;
};
using preset_vec_price = std::vector<std::shared_ptr<CsvPriceImpSettings>>;

View File

@ -42,7 +42,7 @@ extern "C"
#include "gnc-ui-util.h"
}
const std::string settings_type{"TRANS"};
constexpr auto group_prefix = "Import csv,transaction - ";
#define CSV_COL_TYPES "ColumnTypes"
@ -57,7 +57,6 @@ static std::shared_ptr<CsvTransImpSettings> create_int_no_preset(void)
{
auto preset = std::make_shared<CsvTransImpSettings>();
preset->m_name = get_no_settings();
preset->m_settings_type = settings_type;
return preset;
}
@ -114,7 +113,7 @@ const preset_vec_trans& get_import_presets_trans (void)
for (gsize i=0; i < grouplength; i++)
{
auto group = std::string(groups[i]);
auto gp = get_prefix() + settings_type + " - ";
auto gp = std::string {group_prefix};
auto pos = group.find(gp);
if (pos == std::string::npos)
continue;
@ -138,7 +137,6 @@ const preset_vec_trans& get_import_presets_trans (void)
for (auto preset_name : preset_names)
{
auto preset = std::make_shared<CsvTransImpSettings>();
preset->m_settings_type = settings_type;
preset->m_name = preset_name;
preset->load();
presets_trans.push_back(preset);
@ -160,7 +158,7 @@ CsvTransImpSettings::load (void)
GError *key_error = nullptr;
m_load_error = false;
auto keyfile = gnc_state_get_current ();
auto group = get_prefix() + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
// Start Loading the settings
m_load_error = CsvImportSettings::load(); // load the common settings
@ -225,7 +223,7 @@ CsvTransImpSettings::save (void)
}
auto keyfile = gnc_state_get_current ();
auto group = get_prefix() + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
// Drop previous saved settings with this name
g_key_file_remove_group (keyfile, group.c_str(), nullptr);
@ -260,3 +258,9 @@ CsvTransImpSettings::remove (void)
CsvImportSettings::remove();
}
const char*
CsvTransImpSettings::get_group_prefix (void)
{
return group_prefix;
}

View File

@ -64,6 +64,9 @@ void remove (void);
Account *m_base_account; // Base account
bool m_multi_split; // Assume multiple lines per transaction
std::vector<GncTransPropType> m_column_types; // The Column types in order
protected:
const char* get_group_prefix (void) override;
};
using preset_vec_trans = std::vector<std::shared_ptr<CsvTransImpSettings>>;

View File

@ -106,11 +106,6 @@ std::string get_gnc_exp (void)
return gnc_exp;
}
std::string get_prefix (void)
{
return csv_group_prefix;
}
/**************************************************
* load_common
*
@ -121,7 +116,7 @@ CsvImportSettings::load (void)
{
GError *key_error = nullptr;
m_load_error = false;
auto group = csv_group_prefix + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
auto keyfile = gnc_state_get_current ();
m_skip_start_lines = g_key_file_get_integer (keyfile, group.c_str(), CSV_SKIP_START, &key_error);
@ -189,7 +184,7 @@ bool
CsvImportSettings::save (void)
{
auto keyfile = gnc_state_get_current ();
auto group = csv_group_prefix + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
// Start Saving the Common settings
g_key_file_set_string (keyfile, group.c_str(), CSV_NAME, m_name.c_str());
@ -243,6 +238,6 @@ void
CsvImportSettings::remove (void)
{
auto keyfile = gnc_state_get_current ();
auto group = csv_group_prefix + m_settings_type + " - " + m_name;
auto group = get_group_prefix() + m_name;
g_key_file_remove_group (keyfile, group.c_str(), nullptr);
}

View File

@ -72,8 +72,6 @@ bool load (void);
*/
void remove (void);
std::string m_settings_type; // Settings Type, TRANS, PRICE etc.
// Common Settings
std::string m_name; // Name given to this preset by the user
GncImpFileFormat m_file_format; // CSV import Format
@ -86,11 +84,13 @@ bool m_skip_alt_lines; // Skip alternate rows
std::string m_separators; // Separators for csv format
bool m_load_error; // Was there an error while parsing the state file ?
std::vector<uint32_t> m_column_widths; // The Column widths
protected:
virtual const char* get_group_prefix (void) = 0;
};
std::string get_no_settings (void);
std::string get_gnc_exp (void);
std::string get_prefix (void);
/** Check whether name can be used as a preset name.
* The names of the internal presets are considered reserved.