Skip to content

Commit

Permalink
GncDateFormat -> locales
Browse files Browse the repository at this point in the history
part2: save/restore: date-format -> locales
  • Loading branch information
christopherlam committed Sep 1, 2024
1 parent dc84e4d commit dcde316
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 204 deletions.
19 changes: 14 additions & 5 deletions gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <glib/gi18n.h>
#include <stdlib.h>

#include "gnc-locale-utils.hpp"
#include "gnc-ui.h"
#include "gnc-uri-utils.h"
#include "gnc-ui-util.h"
Expand Down Expand Up @@ -659,8 +660,8 @@ CsvImpPriceAssist::CsvImpPriceAssist ()

/* Add in the date format combo box and hook it up to an event handler. */
date_format_combo = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
for (auto& date_fmt : GncDate::c_formats)
gtk_combo_box_text_append_text (date_format_combo, _(date_fmt.m_fmt.c_str()));
for (auto locale : gnc_get_available_locales())
gtk_combo_box_text_append_text (date_format_combo, _(locale.c_str()));
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo), 0);
g_signal_connect (G_OBJECT(date_format_combo), "changed",
G_CALLBACK(csv_price_imp_preview_date_fmt_sel_cb), this);
Expand Down Expand Up @@ -1159,7 +1160,11 @@ CsvImpPriceAssist::preview_update_encoding (const char* encoding)
void
CsvImpPriceAssist::preview_update_date_format ()
{
price_imp->date_format (gtk_combo_box_get_active (GTK_COMBO_BOX(date_format_combo)));
if (char *text = gtk_combo_box_text_get_active_text(date_format_combo))
{
price_imp->date_locale (text);
g_free (text);
}
preview_refresh_table ();
}

Expand Down Expand Up @@ -1764,8 +1769,12 @@ CsvImpPriceAssist::preview_refresh ()
(price_imp->file_format() != GncImpFileFormat::CSV));

// This section deals with the combo's and character encoding
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo),
price_imp->date_format());
auto locales = gnc_get_available_locales();
auto locale_it = std::find (locales.begin(), locales.end(), price_imp->date_locale());
if (locale_it != locales.end())
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo),
std::distance (locales.begin(), locale_it));

gtk_combo_box_set_active (GTK_COMBO_BOX(currency_format_combo),
price_imp->currency_format());
go_charmap_sel_set_encoding (encselector, price_imp->encoding().c_str());
Expand Down
20 changes: 15 additions & 5 deletions gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "gnc-ui-util.h"
#include "dialog-utils.h"

#include "gnc-locale-utils.hpp"
#include "gnc-component-manager.h"

#include "gnc-state.h"
Expand Down Expand Up @@ -602,8 +603,9 @@ CsvImpTransAssist::CsvImpTransAssist ()

/* Add in the date format combo box and hook it up to an event handler. */
date_format_combo = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
for (auto& date_fmt : GncDate::c_formats)
gtk_combo_box_text_append_text (date_format_combo, _(date_fmt.m_fmt.c_str()));
for (auto locale : gnc_get_available_locales())
gtk_combo_box_text_append_text (date_format_combo, _(locale.c_str()));

gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo), 0);
g_signal_connect (G_OBJECT(date_format_combo), "changed",
G_CALLBACK(csv_tximp_preview_date_fmt_sel_cb), this);
Expand Down Expand Up @@ -1137,7 +1139,11 @@ CsvImpTransAssist::preview_update_encoding (const char* encoding)
void
CsvImpTransAssist::preview_update_date_format ()
{
tx_imp->date_format (gtk_combo_box_get_active (GTK_COMBO_BOX(date_format_combo)));
if (char *text = gtk_combo_box_text_get_active_text(date_format_combo))
{
tx_imp->date_locale (text);
g_free (text);
}
preview_refresh_table ();
}

Expand Down Expand Up @@ -1690,8 +1696,12 @@ CsvImpTransAssist::preview_refresh ()
(tx_imp->file_format() != GncImpFileFormat::CSV));

// Set Date & Currency Format and Character encoding
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo),
tx_imp->date_format());
auto locales = gnc_get_available_locales();
auto locale_it = std::find (locales.begin(), locales.end(), tx_imp->date_locale());
if (locale_it != locales.end())
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo),
std::distance (locales.begin(), locale_it));

gtk_combo_box_set_active (GTK_COMBO_BOX(currency_format_combo),
tx_imp->currency_format());
go_charmap_sel_set_encoding (encselector, tx_imp->encoding().c_str());
Expand Down
2 changes: 1 addition & 1 deletion gnucash/import-export/csv-imp/gnc-imp-props-price.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void GncImportPrice::set (GncPricePropType prop_type, const std::string& value,
{
case GncPricePropType::DATE:
m_date.reset();
m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
m_date = GncDate(value, m_date_locale); // Throws if parsing fails
break;

case GncPricePropType::AMOUNT:
Expand Down
6 changes: 3 additions & 3 deletions gnucash/import-export/csv-imp/gnc-imp-props-price.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ GncNumeric parse_amount_price (const std::string &str, int currency_format);
struct GncImportPrice
{
public:
GncImportPrice (int date_format, int currency_format) : m_date_format{date_format},
GncImportPrice (std::string date_locale, int currency_format) : m_date_locale{date_locale},
m_currency_format{currency_format}{};

void set (GncPricePropType prop_type, const std::string& value, bool enable_test_empty);
void set_date_format (int date_format) { m_date_format = date_format ;}
void set_date_locale (std::string date_locale) { m_date_locale = date_locale ;}
void set_currency_format (int currency_format) { m_currency_format = currency_format ;}
void reset (GncPricePropType prop_type);
std::string verify_essentials (void);
Expand All @@ -102,7 +102,7 @@ struct GncImportPrice
std::string errors();

private:
int m_date_format;
std::string m_date_locale;
int m_currency_format;
std::optional<GncDate> m_date;
std::optional<GncNumeric> m_amount;
Expand Down
8 changes: 3 additions & 5 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
case GncTransPropType::DATE:
m_date.reset();
if (!value.empty())
m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
m_date = GncDate(value, m_date_locale);
else if (!m_multi_split)
throw std::invalid_argument (
(bl::format (std::string{_("Date field can not be empty if 'Multi-split' option is unset.\n")}) %
Expand Down Expand Up @@ -527,15 +527,13 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
case GncTransPropType::REC_DATE:
m_rec_date.reset();
if (!value.empty())
m_rec_date = GncDate (value,
GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
m_rec_date = GncDate (value, m_date_locale); // Throws if parsing fails
break;

case GncTransPropType::TREC_DATE:
m_trec_date.reset();
if (!value.empty())
m_trec_date = GncDate (value,
GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
m_trec_date = GncDate (value, m_date_locale); // Throws if parsing fails
break;

default:
Expand Down
14 changes: 7 additions & 7 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ struct DraftTransaction
class GncPreTrans
{
public:
GncPreTrans(int date_format, bool multi_split)
: m_date_format{date_format}, m_multi_split{multi_split}, m_currency{nullptr} {};
GncPreTrans(const std::string date_locale, bool multi_split)
: m_date_locale{date_locale}, m_multi_split{multi_split}, m_currency{nullptr} {};

void set (GncTransPropType prop_type, const std::string& value);
void set_date_format (int date_format) { m_date_format = date_format ;}
void set_date_locale (const std::string date_locale) { m_date_locale = date_locale ;}
void set_multi_split (bool multi_split) { m_multi_split = multi_split ;}
void reset (GncTransPropType prop_type);
StrVec verify_essentials (void);
Expand Down Expand Up @@ -190,7 +190,7 @@ class GncPreTrans


private:
int m_date_format;
std::string m_date_locale;
bool m_multi_split;
std::optional<std::string> m_differ;
std::optional<GncDate> m_date;
Expand Down Expand Up @@ -221,12 +221,12 @@ class GncPreTrans
class GncPreSplit
{
public:
GncPreSplit (int date_format, int currency_format) : m_date_format{date_format},
GncPreSplit (const std::string date_locale, int currency_format) : m_date_locale{date_locale},
m_currency_format{currency_format} {};
void set (GncTransPropType prop_type, const std::string& value);
void reset (GncTransPropType prop_type);
void add (GncTransPropType prop_type, const std::string& value);
void set_date_format (int date_format) { m_date_format = date_format ;}
void set_date_locale (const std::string date_locale) { m_date_locale = date_locale ;}
void set_currency_format (int currency_format) { m_currency_format = currency_format; }
void set_pre_trans (std::shared_ptr<GncPreTrans> pre_trans) { m_pre_trans = pre_trans; }
std::shared_ptr<GncPreTrans> get_pre_trans (void) { return m_pre_trans; }
Expand All @@ -241,7 +241,7 @@ class GncPreSplit
void UpdateCrossSplitCounters ();

std::shared_ptr<GncPreTrans> m_pre_trans;
int m_date_format;
std::string m_date_locale;
int m_currency_format;
std::optional<std::string> m_action;
std::optional<Account*> m_account;
Expand Down
10 changes: 5 additions & 5 deletions gnucash/import-export/csv-imp/gnc-imp-settings-csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "gnc-locale-utils.hpp"
#include "Account.h"
#include "gnc-state.h"
#include "gnc-ui-util.h"
Expand Down Expand Up @@ -150,7 +151,7 @@ CsvImportSettings::load (void)
if (key_char)
g_free (key_char);

m_date_format = g_key_file_get_integer (keyfile, group.c_str(), CSV_DATE, &key_error);
m_date_locale = g_key_file_get_string (keyfile, group.c_str(), CSV_DATE, &key_error);
m_load_error |= handle_load_error (&key_error, group);

m_currency_format = g_key_file_get_integer (keyfile, group.c_str(), CSV_CURRENCY, &key_error);
Expand Down Expand Up @@ -203,13 +204,12 @@ CsvImportSettings::save (void)
(m_file_format == GncImpFileFormat::CSV) ? true : false);

g_key_file_set_string (keyfile, group.c_str(), CSV_SEP, m_separators.c_str());
g_key_file_set_integer (keyfile, group.c_str(), CSV_DATE, m_date_format);
g_key_file_set_string (keyfile, group.c_str(), CSV_DATE, m_date_locale.c_str());
std::ostringstream cmt_ss;
cmt_ss << "Supported date formats: ";
int fmt_num = 0;
std::for_each (GncDate::c_formats.cbegin(), GncDate::c_formats.cend(),
[&cmt_ss, &fmt_num](const GncDateFormat& fmt)
{ cmt_ss << fmt_num++ << ": '" << fmt.m_fmt << "', "; });
for (auto loc : gnc_get_available_locales())
cmt_ss << fmt_num++ << ": '" << loc << "', ";
auto cmt = cmt_ss.str().substr(0, static_cast<long>(cmt_ss.tellp()) - 2);
g_key_file_set_comment (keyfile, group.c_str(), CSV_DATE, cmt.c_str(), nullptr);
g_key_file_set_integer (keyfile, group.c_str(), CSV_CURRENCY, m_currency_format);
Expand Down
4 changes: 2 additions & 2 deletions gnucash/import-export/csv-imp/gnc-imp-settings-csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ enum SETTINGS_COL {SET_GROUP, SET_NAME};
struct CsvImportSettings
{
CsvImportSettings() : m_file_format (GncImpFileFormat::CSV), m_encoding {"UTF-8"},
m_date_format {0}, m_currency_format {0},
m_date_locale {"en_AU"}, m_currency_format {0},
m_skip_start_lines{0}, m_skip_end_lines{0}, m_skip_alt_lines (false),
m_separators {","}, m_load_error {false} { }
virtual ~CsvImportSettings() = default;
Expand All @@ -75,7 +75,7 @@ void remove (void);
std::string m_name; // Name given to this preset by the user
GncImpFileFormat m_file_format; // CSV import Format
std::string m_encoding; // File encoding
int m_date_format; // Date Active id
std::string m_date_locale; // Date Active id
int m_currency_format; // Currency Active id
uint32_t m_skip_start_lines; // Number of header rows to skip
uint32_t m_skip_end_lines; // Number of footer rows to skip
Expand Down
10 changes: 5 additions & 5 deletions gnucash/import-export/csv-imp/gnc-import-price.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ void GncPriceImport::currency_format (int currency_format)
}
int GncPriceImport::currency_format () { return m_settings.m_currency_format; }

void GncPriceImport::date_format (int date_format)
void GncPriceImport::date_locale (std::string date_locale)
{
m_settings.m_date_format = date_format;
m_settings.m_date_locale = date_locale;

/* Reparse all date related columns */
std::vector<GncPricePropType> dates = { GncPricePropType::DATE };
reset_formatted_column (dates);
}
int GncPriceImport::date_format () { return m_settings.m_date_format; }
std::string GncPriceImport::date_locale () { return m_settings.m_date_locale; }

/** Converts raw file data using a new encoding. This function must be
* called after load_file only if load_file guessed
Expand Down Expand Up @@ -385,7 +385,7 @@ void GncPriceImport::tokenize (bool guessColTypes)
auto length = tokenized_line.size();
if (length > 0)
m_parsed_lines.push_back (std::make_tuple (tokenized_line, std::string(),
std::make_shared<GncImportPrice>(date_format(), currency_format()),
std::make_shared<GncImportPrice>(date_locale(), currency_format()),
false));
if (length > max_cols)
max_cols = length;
Expand Down Expand Up @@ -750,7 +750,7 @@ GncPriceImport::set_column_type_price (uint32_t position, GncPricePropType type,
/* Reset date and currency formats for each price props object
* to ensure column updates use the most recent one
*/
std::get<PL_PREPRICE>(*parsed_lines_it)->set_date_format (m_settings.m_date_format);
std::get<PL_PREPRICE>(*parsed_lines_it)->set_date_locale (m_settings.m_date_locale);
std::get<PL_PREPRICE>(*parsed_lines_it)->set_currency_format (m_settings.m_currency_format);

uint32_t row = parsed_lines_it - m_parsed_lines.begin();
Expand Down
4 changes: 2 additions & 2 deletions gnucash/import-export/csv-imp/gnc-import-price.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class GncPriceImport
void currency_format (int currency_format);
int currency_format ();

void date_format (int date_format);
int date_format ();
void date_locale (std::string date_locale);
std::string date_locale ();

void encoding (const std::string& encoding);
std::string encoding ();
Expand Down
14 changes: 7 additions & 7 deletions gnucash/import-export/csv-imp/gnc-import-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,17 @@ void GncTxImport::currency_format (int currency_format)
}
int GncTxImport::currency_format () { return m_settings.m_currency_format; }

void GncTxImport::date_format (int date_format)
void GncTxImport::date_locale (std::string date_locale)
{
m_settings.m_date_format = date_format;
m_settings.m_date_locale = date_locale;

/* Reparse all date related columns */
std::vector<GncTransPropType> dates = { GncTransPropType::DATE,
GncTransPropType::REC_DATE,
GncTransPropType::TREC_DATE};
reset_formatted_column (dates);
}
int GncTxImport::date_format () { return m_settings.m_date_format; }
std::string GncTxImport::date_locale () { return m_settings.m_date_locale; }

/** Converts raw file data using a new encoding. This function must be
* called after load_file only if load_file guessed
Expand Down Expand Up @@ -407,8 +407,8 @@ void GncTxImport::tokenize (bool guessColTypes)
auto length = tokenized_line.size();
if (length > 0)
{
auto pretrans = std::make_shared<GncPreTrans>(date_format(), m_settings.m_multi_split);
auto presplit = std::make_shared<GncPreSplit>(date_format(), currency_format());
auto pretrans = std::make_shared<GncPreTrans>(date_locale(), m_settings.m_multi_split);
auto presplit = std::make_shared<GncPreSplit>(date_locale(), currency_format());
presplit->set_pre_trans (std::move (pretrans));
m_parsed_lines.push_back (std::make_tuple (tokenized_line, ErrMap(),
presplit->get_pre_trans(), std::move (presplit), false));
Expand Down Expand Up @@ -781,7 +781,7 @@ void GncTxImport::update_pre_trans_props (parse_line_t& parsed_line, uint32_t co

/* Reset date format for each trans props object
* to ensure column updates use the most recent one */
trans_props->set_date_format (m_settings.m_date_format);
trans_props->set_date_locale (m_settings.m_date_locale);
trans_props->set_multi_split (m_settings.m_multi_split);

if ((old_type > GncTransPropType::NONE) && (old_type <= GncTransPropType::TRANS_PROPS))
Expand Down Expand Up @@ -820,7 +820,7 @@ void GncTxImport::update_pre_split_props (parse_line_t& parsed_line, uint32_t co
auto trans_props = std::get<PL_PRETRANS> (parsed_line);
/* Reset date format for each split props object
* to ensure column updates use the most recent one */
split_props->set_date_format (m_settings.m_date_format);
split_props->set_date_locale (m_settings.m_date_locale);
if (m_settings.m_multi_split && trans_props->is_part_of( m_parent))
split_props->set_pre_trans (m_parent);
else
Expand Down
4 changes: 2 additions & 2 deletions gnucash/import-export/csv-imp/gnc-import-tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class GncTxImport
void currency_format (int currency_format);
int currency_format ();

void date_format (int date_format);
int date_format ();
void date_locale (std::string date_locale);
std::string date_locale ();

void encoding (const std::string& encoding);
std::string encoding ();
Expand Down
1 change: 1 addition & 0 deletions libgnucash/core-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ target_link_libraries(gnc-core-utils
PkgConfig::GLIB2
PRIVATE
${Boost_LIBRARIES}
${ICU4C_I18N_LDFLAGS}
${GOBJECT_LDFLAGS}
${GTK_MAC_LDFLAGS}
"$<$<BOOL:${MAC_INTEGRATION}>:${OSX_EXTRA_LIBRARIES}>")
Expand Down
12 changes: 12 additions & 0 deletions libgnucash/core-utils/gnc-locale-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <glib.h>
#include <clocale>
#include <boost/locale.hpp>
#include <unicode/uloc.h>
#include "gnc-locale-utils.hpp"
#include <config.h>

Expand Down Expand Up @@ -115,3 +116,14 @@ gnc_get_boost_locale()
}


std::vector<std::string>
gnc_get_available_locales ()
{
std::vector<std::string> rv;
auto num_locales{uloc_countAvailable()};
rv.reserve (num_locales);
for (int32_t i = 0; i < num_locales; ++i)
if (auto localeID = uloc_getAvailable (i))
rv.push_back (localeID);
return rv;
}
Loading

0 comments on commit dcde316

Please sign in to comment.