GnuCash  4.9-165-gc1c198c9f
Files | Data Structures | Macros | Typedefs | Enumerations | Functions
Utility functions for the GnuCash GUI

Files

file  gnc-ui-util.h
 utility functions for the GnuCash UI
 

Data Structures

struct  GNCPrintAmountInfo
 

Macros

#define GNC_PREFS_GROUP_OPEN_SAVE   "dialogs.open-save"
 
#define GNC_PREFS_GROUP_EXPORT   "dialogs.export-accounts"
 
#define GNC_PREFS_GROUP_REPORT   "dialogs.report"
 
#define GNC_PREF_AUTO_DECIMAL_POINT   "auto-decimal-point"
 
#define GNC_PREF_AUTO_DECIMAL_PLACES   "auto-decimal-places"
 
#define WLINK   'w'
 
#define FLINK   'f'
 

Typedefs

typedef QofSession *(* QofSessionCB) (void)
 

Enumerations

enum  GNCEquityType { EQUITY_OPENING_BALANCE, EQUITY_RETAINED_EARNINGS, NUM_EQUITY_TYPES }
 

Functions

gchar * gnc_normalize_account_separator (const gchar *separator)
 
gboolean gnc_reverse_balance (const Account *account)
 
void gnc_set_use_equity_type_opening_balance_account (QofBook *book)
 
gboolean gnc_using_equity_type_opening_balance_account (QofBook *book)
 
gchar * gnc_get_default_directory (const gchar *section)
 
void gnc_set_default_directory (const gchar *section, const gchar *directory)
 
QofBook * gnc_get_current_book (void)
 
gboolean gnc_is_new_book (void)
 
void gnc_set_current_book_tax_name_type (gboolean name_changed, const gchar *tax_name, gboolean type_changed, const gchar *tax_type)
 
const gchar * gnc_get_current_book_tax_name (void)
 
const gchar * gnc_get_current_book_tax_type (void)
 
void gnc_book_option_num_field_source_change_cb (gboolean num_action)
 Calls gnc_book_option_num_field_source_change to initiate registered callbacks when num_field_source book option changes so that registers/reports can update themselves; sets feature flag.
 
void gnc_book_option_book_currency_selected_cb (gboolean use_book_currency)
 Calls gnc_book_option_book_currency_selected to initiate registered callbacks when currency accounting book option changes to book-currency so that registers/reports can update themselves; sets feature flag.
 
gboolean gnc_book_use_book_currency (QofBook *book)
 Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading accounts are not used. More...
 
const gchar * gnc_book_get_book_currency_name (QofBook *book)
 Returns pointer to Book Currency name for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply. More...
 
gnc_commodity * gnc_book_get_book_currency (QofBook *book)
 Returns pointer to Book Currency for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply. More...
 
const gchar * gnc_book_get_default_gains_policy (QofBook *book)
 Returns pointer to default gain/loss policy for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply. More...
 
Accountgnc_book_get_default_gain_loss_acct (QofBook *book)
 Returns pointer to default gain/loss account for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply. More...
 
Accountgnc_get_current_root_account (void)
 
gnc_commodity_table * gnc_get_current_commodities (void)
 
gchar * gnc_get_account_name_for_register (const Account *account)
 Get either the full name of the account or the simple name, depending on the configuration parameter general/register/show_leaf_account_names. More...
 
Accountgnc_account_lookup_for_register (const Account *base_account, const gchar *name)
 Retrieve the account matching the given name starting from the descendants of base_account. More...
 
gchar * gnc_get_account_name_for_split_register (const Account *account, gboolean show_leaf_accounts)
 Get either the full name of the account or the simple name, depending on the show_leaf_accounts. More...
 
char * gnc_ui_account_get_tax_info_string (const Account *account)
 
char * gnc_ui_account_get_tax_info_sub_acct_string (const Account *account)
 
const char * gnc_get_reconcile_str (char reconciled_flag)
 
const char * gnc_get_reconcile_valid_flags (void)
 
const char * gnc_get_reconcile_flag_order (void)
 
const char * gnc_get_doclink_valid_flags (void)
 Get a string containing documentation link valid flags. More...
 
const char * gnc_get_doclink_flag_order (void)
 Get a string containing document link flag order. More...
 
const char * gnc_get_doclink_str (char link_flag)
 Get a string representing the document link type. More...
 
Accountgnc_find_or_create_equity_account (Account *root, GNCEquityType equity_type, gnc_commodity *currency)
 
gboolean gnc_account_create_opening_balance (Account *account, gnc_numeric balance, time64 date, QofBook *book)
 
gnc_commodity * gnc_locale_default_currency_nodefault (void)
 Returns the default currency of the current locale, or NULL if no sensible currency could be identified from the locale. More...
 
gnc_commodity * gnc_locale_default_currency (void)
 Returns the default currency of the current locale. More...
 
gnc_commodity * gnc_default_currency (void)
 Return the default currency set by the user. More...
 
gnc_commodity * gnc_account_or_default_currency (const Account *account, gboolean *currency_from_account_found)
 Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency. More...
 
gnc_commodity * gnc_default_report_currency (void)
 Return the default currency for use in reports, as set by the user. More...
 
GNCPrintAmountInfo gnc_default_print_info (gboolean use_symbol)
 
GNCPrintAmountInfo gnc_commodity_print_info (const gnc_commodity *commodity, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_account_print_info (const Account *account, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_split_amount_print_info (Split *split, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_price_print_info (const gnc_commodity *curr, gboolean use_symbol)
 
GNCPrintAmountInfo gnc_share_print_info_places (int decplaces)
 
GNCPrintAmountInfo gnc_default_share_print_info (void)
 
GNCPrintAmountInfo gnc_default_price_print_info (const gnc_commodity *curr)
 
GNCPrintAmountInfo gnc_integral_print_info (void)
 
const char * xaccPrintAmount (gnc_numeric val, GNCPrintAmountInfo info)
 Make a string representation of a gnc_numeric. More...
 
int xaccSPrintAmount (char *buf, gnc_numeric val, GNCPrintAmountInfo info)
 Make a string representation of a gnc_numeric. More...
 
const gchar * printable_value (gdouble val, gint denom)
 
gchar * number_to_words (gdouble val, gint64 denom)
 
gchar * numeric_to_words (gnc_numeric val)
 
gboolean xaccParseAmount (const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr)
 Parses in_str to obtain a numeric result. More...
 
gboolean xaccParseAmountPosSign (const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr, gboolean skip)
 Parses in_str to a gnc_numeric, with a flag to indicate whether the locale's positive sign (or in absence the '+') character is ignored. More...
 
gboolean xaccParseAmountExtended (const char *in_str, gboolean monetary, gunichar negative_sign, gunichar decimal_point, gunichar group_separator, const char *ignore_list, gnc_numeric *result, char **endstr)
 Converts a string to a gnc_numeric. More...
 
void gnc_ui_util_init (void)
 
void gnc_ui_util_remove_registered_prefs (void)
 
gchar * gnc_filter_text_for_control_chars (const gchar *incoming_text)
 Returns the incoming text removed of control characters. More...
 
void gnc_filter_text_set_cursor_position (const gchar *incoming_text, const gchar *symbol, gint *cursor_position)
 Updates cursor_position after removal of currency symbols. More...
 
gchar * gnc_filter_text_for_currency_symbol (const gchar *incoming_text, const gchar *symbol)
 Returns the incoming text removed of a currency symbol. More...
 
gchar * gnc_filter_text_for_currency_commodity (const gnc_commodity *comm, const gchar *incoming_text, const gchar **symbol)
 Returns the incoming text removed of currency symbol. More...
 

Detailed Description

Function Documentation

◆ gnc_account_lookup_for_register()

Account* gnc_account_lookup_for_register ( const Account base_account,
const gchar *  name 
)

Retrieve the account matching the given name starting from the descendants of base_account.

name is either considered to be the name of the leaf in the account tree or to be the full account path, depending on the configuration parameter general.register/show_leaf_account_names.

Parameters
base_accountThe account to start the search at.
nameThe name to search for.
Returns
A pointer to the account, or NULL if the account was not found.

◆ gnc_account_or_default_currency()

gnc_commodity* gnc_account_or_default_currency ( const Account account,
gboolean *  currency_from_account_found 
)

Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.

The gnc_commodity is taken either from the current account, or from the next parent account that has a gnc_commodity that is a currency, or from gnc_default_currency().

If the given account or any of its parent account have a commodity that is a currency, it is returned and the gboolean currency_from_account_found is set to TRUE (if non-NULL). If neither this account nor any of its parent accounts have such a commodity, gnc_default_currency() is returned and the gboolean currency_from_account_found is set to FALSE (if non-NULL). This can be used to show an appropriate warning message.

If account is NULL, gnc_default_currency() is returned and currency_from_account_found is set to FALSE.

Parameters
accountThe account where the currency should be looked up. May be NULL.
currency_from_account_foundA gboolean pointer that takes the output argument of whether the returned currency was found in the account. May be NULL.
Returns
A currency pointer (and never NULL).

Definition at line 1182 of file gnc-ui-util.c.

1183 {
1184  gnc_commodity *currency;
1185  if (!account)
1186  {
1187  if (currency_from_account_found)
1188  *currency_from_account_found = FALSE;
1189  return gnc_default_currency();
1190  }
1191 
1192  currency = gnc_account_get_currency_or_parent(account);
1193  if (currency)
1194  {
1195  if (currency_from_account_found)
1196  *currency_from_account_found = TRUE;
1197  }
1198  else
1199  {
1200  if (currency_from_account_found)
1201  *currency_from_account_found = FALSE;
1202  currency = gnc_default_currency();
1203  }
1204  return currency;
1205 }
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
Definition: gnc-ui-util.c:1177
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.
Definition: Account.cpp:3430

◆ gnc_book_get_book_currency()

gnc_commodity* gnc_book_get_book_currency ( QofBook *  book)

Returns pointer to Book Currency for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply.

Definition at line 477 of file gnc-ui-util.c.

478 {
479  if (!book) return NULL;
480 
481  if (gnc_book_use_book_currency (book))
482  return gnc_commodity_table_lookup
484  GNC_COMMODITY_NS_CURRENCY,
486 
487  return NULL;
488 }
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gboolean gnc_book_use_book_currency(QofBook *book)
Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading ...
Definition: gnc-ui-util.c:427
const gchar * qof_book_get_book_currency_name(QofBook *book)
Returns pointer to book-currency name for book, if one exists in the KVP, or NULL; does not validate ...
Definition: qofbook.cpp:991

◆ gnc_book_get_book_currency_name()

const gchar* gnc_book_get_book_currency_name ( QofBook *  book)

Returns pointer to Book Currency name for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply.

Definition at line 462 of file gnc-ui-util.c.

463 {
464  if (!book) return NULL;
465 
466  if (gnc_book_use_book_currency (book))
467  return qof_book_get_book_currency_name (book);
468 
469  return NULL;
470 }
gboolean gnc_book_use_book_currency(QofBook *book)
Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading ...
Definition: gnc-ui-util.c:427
const gchar * qof_book_get_book_currency_name(QofBook *book)
Returns pointer to book-currency name for book, if one exists in the KVP, or NULL; does not validate ...
Definition: qofbook.cpp:991

◆ gnc_book_get_default_gain_loss_acct()

Account* gnc_book_get_default_gain_loss_acct ( QofBook *  book)

Returns pointer to default gain/loss account for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply.

Also, account must not be hidden or a placeholder, and must be of same currency as book-currency and income or expense type

Definition at line 511 of file gnc-ui-util.c.

512 {
513  Account *gains_account = NULL;
514 
515  if (!book) return NULL;
516 
517  if (gnc_book_use_book_currency (book))
518  {
520  gains_account = xaccAccountLookup (guid, book);
521  guid_free (guid);
522  }
523 
524  if (gains_account &&
525  !xaccAccountGetPlaceholder(gains_account) &&
526  !xaccAccountGetHidden(gains_account) &&
528  gnc_book_get_book_currency(book))) &&
529  ((xaccAccountGetType(gains_account) == ACCT_TYPE_INCOME) ||
530  (xaccAccountGetType(gains_account) == ACCT_TYPE_EXPENSE)))
531  {
532  return gains_account;
533  }
534  else
535  {
536  return NULL;
537  }
538 }
gboolean gnc_book_use_book_currency(QofBook *book)
Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading ...
Definition: gnc-ui-util.c:427
Expense accounts are used to denote expenses.
Definition: Account.h:146
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
Definition: Account.cpp:3254
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equal.
Income accounts are used to denote income.
Definition: Account.h:143
gnc_commodity * gnc_book_get_book_currency(QofBook *book)
Returns pointer to Book Currency for book or NULL; determines that both book-currency and default gai...
Definition: gnc-ui-util.c:477
gboolean xaccAccountGetHidden(const Account *acc)
Get the "hidden" flag for an account.
Definition: Account.cpp:4305
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
Definition: Account.cpp:3423
GncGUID * qof_book_get_default_gain_loss_acct_guid(QofBook *book)
Returns pointer to default gain/loss account GUID for book, if one exists in the KVP, or NULL; does not validate contents nor determine if there is a valid book-currency, both of which are required, for the 'book-currency' currency accounting method to apply.
Definition: qofbook.cpp:1021
gboolean xaccAccountGetPlaceholder(const Account *acc)
Get the "placeholder" flag for an account.
Definition: Account.cpp:4216
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:2049

◆ gnc_book_get_default_gains_policy()

const gchar* gnc_book_get_default_gains_policy ( QofBook *  book)

Returns pointer to default gain/loss policy for book or NULL; determines that both book-currency and default gain/loss policy KVPs exist and that both are valid, a requirement for the 'book-currency' currency accounting method to apply.

Definition at line 495 of file gnc-ui-util.c.

496 {
497  if (!book) return NULL;
498 
499  if (gnc_book_use_book_currency (book))
500  return qof_book_get_default_gains_policy (book);
501 
502  return NULL;
503 }
gboolean gnc_book_use_book_currency(QofBook *book)
Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading ...
Definition: gnc-ui-util.c:427
const gchar * qof_book_get_default_gains_policy(QofBook *book)
Returns pointer to default gain/loss policy for book, if one exists in the KVP, or NULL; does not val...
Definition: qofbook.cpp:1006

◆ gnc_book_use_book_currency()

gboolean gnc_book_use_book_currency ( QofBook *  book)

Returns TRUE if both book-currency and default gain/loss policy KVPs exist and are valid and trading accounts are not used.

Definition at line 427 of file gnc-ui-util.c.

428 {
429  const gchar *policy;
430  const gchar *currency;
431 
432  if (!book) return FALSE;
433 
434  policy = qof_book_get_default_gains_policy (book);
435  currency = qof_book_get_book_currency_name (book);
436 
437  /* If either a default gain/loss policy or a book-currency does not exist,
438  book-currency accounting method not valid */
439  if (!policy || !currency)
440  return FALSE;
441 
442  /* If both exist, both must be valid */
443  if (!gnc_valid_policy_name (policy) || !gnc_commodity_table_lookup
445  (gnc_get_current_book()),
446  GNC_COMMODITY_NS_CURRENCY,
447  currency))
448  return FALSE;
449 
450  /* If both exist and are valid, there must be no trading accounts flag */
452  return FALSE;
453 
454  return TRUE;
455 }
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
const gchar * qof_book_get_default_gains_policy(QofBook *book)
Returns pointer to default gain/loss policy for book, if one exists in the KVP, or NULL; does not val...
Definition: qofbook.cpp:1006
const gchar * qof_book_get_book_currency_name(QofBook *book)
Returns pointer to book-currency name for book, if one exists in the KVP, or NULL; does not validate ...
Definition: qofbook.cpp:991
gboolean gnc_valid_policy_name(const gchar *policy_name)
Valid Policy Name Uses the Valid Policy List to determine if a policy name is valid.
Definition: policy.c:80
gboolean qof_book_use_trading_accounts(const QofBook *book)
Returns flag indicating whether this book uses trading accounts.
Definition: qofbook.cpp:1033

◆ gnc_default_currency()

gnc_commodity* gnc_default_currency ( void  )

Return the default currency set by the user.

If the user's preference is invalid, then this routine will return the default currency for the user's locale.

Returns
A pointer to a currency.

Definition at line 1177 of file gnc-ui-util.c.

1178 {
1179  return gnc_default_currency_common (user_default_currency, GNC_PREFS_GROUP_GENERAL);
1180 }

◆ gnc_default_report_currency()

gnc_commodity* gnc_default_report_currency ( void  )

Return the default currency for use in reports, as set by the user.

If the user's preference is invalid, then this routine will return the default currency for the user's locale.

Returns
A pointer to a currency.

Definition at line 1210 of file gnc-ui-util.c.

1211 {
1212  return gnc_default_currency_common (user_report_currency, GNC_PREFS_GROUP_GENERAL_REPORT);
1213 }

◆ gnc_filter_text_for_control_chars()

gchar* gnc_filter_text_for_control_chars ( const gchar *  incoming_text)

Returns the incoming text removed of control characters.

Parameters
incoming_textThe text to filter
Returns
The incoming text filtered of control characters to be freed by the caller.

Definition at line 2525 of file gnc-ui-util.c.

2526 {
2527  gchar *normal_text, *nt;
2528  GString *filtered;
2529  gboolean cntrl = FALSE;
2530  gboolean text_found = FALSE;
2531 
2532  if (!text)
2533  return NULL;
2534 
2535  if (!g_utf8_validate (text, -1, NULL))
2536  return NULL;
2537 
2538  normal_text = g_utf8_normalize (text, -1, G_NORMALIZE_ALL_COMPOSE);
2539 
2540  filtered = g_string_sized_new (strlen (normal_text) + 1);
2541 
2542  nt = normal_text;
2543 
2544  while (*nt)
2545  {
2546  gunichar uc = g_utf8_get_char (nt);
2547 
2548  // check for starting with control characters
2549  if (unichar_is_cntrl (uc) && !text_found)
2550  {
2551  nt = g_utf8_next_char (nt);
2552  continue;
2553  }
2554  // check for alpha, num and punctuation
2555  if (!unichar_is_cntrl (uc))
2556  {
2557  filtered = g_string_append_unichar (filtered, uc);
2558  text_found = TRUE;
2559  }
2560  // check for control characters after text
2561  if (unichar_is_cntrl (uc))
2562  cntrl = TRUE;
2563 
2564  nt = g_utf8_next_char (nt);
2565 
2566  if (cntrl) // if control characters in text replace with space
2567  {
2568  gunichar uc2 = g_utf8_get_char (nt);
2569 
2570  if (!unichar_is_cntrl (uc2))
2571  filtered = g_string_append_unichar (filtered, ' ');
2572  }
2573  cntrl = FALSE;
2574  }
2575  g_free (normal_text);
2576  return g_string_free (filtered, FALSE);
2577 }

◆ gnc_filter_text_for_currency_commodity()

gchar* gnc_filter_text_for_currency_commodity ( const gnc_commodity *  comm,
const gchar *  incoming_text,
const gchar **  symbol 
)

Returns the incoming text removed of currency symbol.

Parameters
commcommodity of entry if known
incoming_textThe text to filter
symbolreturn the symbol used
Returns
The incoming text with symbol removed to be freed by the caller

Definition at line 2636 of file gnc-ui-util.c.

2639 {
2640  if (!incoming_text)
2641  {
2642  *symbol = NULL;
2643  return NULL;
2644  }
2645 
2646  if (!gnc_commodity_is_currency (comm))
2647  {
2648  *symbol = NULL;
2649  return g_strdup (incoming_text);
2650  }
2651 
2652  if (comm)
2653  *symbol = gnc_commodity_get_nice_symbol (comm);
2654  else
2656 
2657  return gnc_filter_text_for_currency_symbol (incoming_text, *symbol);
2658 }
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
Definition: gnc-ui-util.c:1177
const char * gnc_commodity_get_nice_symbol(const gnc_commodity *cm)
Retrieve a symbol for the specified commodity, suitable for display to the user.
gchar * gnc_filter_text_for_currency_symbol(const gchar *incoming_text, const gchar *symbol)
Returns the incoming text removed of a currency symbol.
Definition: gnc-ui-util.c:2612

◆ gnc_filter_text_for_currency_symbol()

gchar* gnc_filter_text_for_currency_symbol ( const gchar *  incoming_text,
const gchar *  symbol 
)

Returns the incoming text removed of a currency symbol.

Parameters
incoming_textThe text to filter
symbolto remove
cursor_positionthe posistion of cursor in the incoming text
Returns
The incoming text with symbol removed to be freed by the caller

Definition at line 2612 of file gnc-ui-util.c.

2614 {
2615  gchar *ret_text = NULL;
2616  gchar **split;
2617 
2618  if (!incoming_text)
2619  return NULL;
2620 
2621  if (!symbol)
2622  return g_strdup (incoming_text);
2623 
2624  if (g_strrstr (incoming_text, symbol) == NULL)
2625  return g_strdup (incoming_text);
2626 
2627  split = g_strsplit (incoming_text, symbol, -1);
2628 
2629  ret_text = g_strjoinv (NULL, split);
2630 
2631  g_strfreev (split);
2632  return ret_text;
2633 }

◆ gnc_filter_text_set_cursor_position()

void gnc_filter_text_set_cursor_position ( const gchar *  incoming_text,
const gchar *  symbol,
gint *  cursor_position 
)

Updates cursor_position after removal of currency symbols.

Parameters
incoming_textThe text to filter
symbolto remove
cursor_positionthe posistion of cursor in the incoming text
Returns
nothing

Definition at line 2580 of file gnc-ui-util.c.

2583 {
2584  gint text_len;
2585  gint num = 0;
2586 
2587  if (*cursor_position == 0)
2588  return;
2589 
2590  if (!incoming_text || !symbol)
2591  return;
2592 
2593  if (g_strrstr (incoming_text, symbol) == NULL)
2594  return;
2595 
2596  text_len = g_utf8_strlen (incoming_text, -1);
2597 
2598  for (gint x = 0; x < text_len; x++)
2599  {
2600  gchar *temp = g_utf8_offset_to_pointer (incoming_text, x);
2601 
2602  if (g_str_has_prefix (temp, symbol))
2603  num++;
2604 
2605  if (g_strrstr (temp, symbol) == NULL)
2606  break;
2607  }
2608  *cursor_position = *cursor_position - (num * g_utf8_strlen (symbol, -1));
2609 }

◆ gnc_get_account_name_for_register()

gchar* gnc_get_account_name_for_register ( const Account account)

Get either the full name of the account or the simple name, depending on the configuration parameter general/register/show_leaf_account_names.

Parameters
accountThe account to retrieve the name for.
Returns
A newly allocated string.

Definition at line 562 of file gnc-ui-util.c.

563 {
564  gboolean show_leaf_accounts;
565  show_leaf_accounts = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
566  GNC_PREF_SHOW_LEAF_ACCT_NAMES);
567 
568  return gnc_get_account_name_for_split_register(account, show_leaf_accounts);
569 }
gchar * gnc_get_account_name_for_split_register(const Account *account, gboolean show_leaf_accounts)
Get either the full name of the account or the simple name, depending on the show_leaf_accounts.
Definition: gnc-ui-util.c:553
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.

◆ gnc_get_account_name_for_split_register()

gchar* gnc_get_account_name_for_split_register ( const Account account,
gboolean  show_leaf_accounts 
)

Get either the full name of the account or the simple name, depending on the show_leaf_accounts.

Parameters
accountThe account to retrieve the name for.
show_leaf_accountsWhether the full name will be returned.
Returns
A newly allocated string.

Definition at line 553 of file gnc-ui-util.c.

554 {
555  if (show_leaf_accounts)
556  return g_strdup (xaccAccountGetName (account));
557  else
558  return gnc_account_get_full_name (account);
559 }
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:3283
const char * xaccAccountGetName(const Account *acc)
Get the account&#39;s name.
Definition: Account.cpp:3276

◆ gnc_get_doclink_flag_order()

const char* gnc_get_doclink_flag_order ( void  )

Get a string containing document link flag order.

Returns
a string containing the document link flag change order

Definition at line 921 of file gnc-ui-util.c.

922 {
923  static const char flags[] = { FLINK, WLINK, ' ', 0 };
924  return flags;
925 }

◆ gnc_get_doclink_str()

const char* gnc_get_doclink_str ( char  link_flag)

Get a string representing the document link type.

Parameters
link_flagThe flag to convert into a string
Returns
the i18n'd doclink string

Definition at line 897 of file gnc-ui-util.c.

898 {
899  switch (link_flag)
900  {
901  case WLINK:
902  return C_("Document Link flag for 'web'", "w");
903  case FLINK:
904  return C_("Document Link flag for 'file'", "f");
905  case ' ':
906  return " ";
907  default:
908  PERR("Bad link flag");
909  return NULL;
910  }
911 }
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ gnc_get_doclink_valid_flags()

const char* gnc_get_doclink_valid_flags ( void  )

Get a string containing documentation link valid flags.

Returns
a string containing the list of valid link_flags

Definition at line 914 of file gnc-ui-util.c.

915 {
916  static const char flags[] = { FLINK, WLINK, ' ', 0 };
917  return flags;
918 }

◆ gnc_locale_default_currency()

gnc_commodity* gnc_locale_default_currency ( void  )

Returns the default currency of the current locale.

WATCH OUT: If no currency could be identified from the locale, this one returns "USD", but this will have nothing to do with the actual locale.

Definition at line 1132 of file gnc-ui-util.c.

1133 {
1134  gnc_commodity * currency = gnc_locale_default_currency_nodefault ();
1135 
1136  return (currency ? currency :
1137  gnc_commodity_table_lookup (gnc_get_current_commodities (),
1138  GNC_COMMODITY_NS_CURRENCY, "USD"));
1139 }
gnc_commodity * gnc_locale_default_currency_nodefault(void)
Returns the default currency of the current locale, or NULL if no sensible currency could be identifi...
Definition: gnc-ui-util.c:1117

◆ gnc_locale_default_currency_nodefault()

gnc_commodity* gnc_locale_default_currency_nodefault ( void  )

Returns the default currency of the current locale, or NULL if no sensible currency could be identified from the locale.

Definition at line 1117 of file gnc-ui-util.c.

1118 {
1119  gnc_commodity * currency;
1120  gnc_commodity_table *table;
1121  const char *code;
1122 
1123  table = gnc_get_current_commodities ();
1124  code = gnc_locale_default_iso_currency_code ();
1125 
1126  currency = gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, code);
1127 
1128  return (currency ? currency : NULL);
1129 }

◆ xaccParseAmount()

gboolean xaccParseAmount ( const char *  in_str,
gboolean  monetary,
gnc_numeric *  result,
char **  endstr 
)

Parses in_str to obtain a numeric result.

The routine will parse as much of in_str as it can to obtain a single number. The number is parsed using the current locale information and the 'monetary' flag. The routine will return TRUE if it successfully parsed a number and FALSE otherwise. If TRUE is returned and result is non-NULL, the value of the parsed number is stored in *result. If FALSE is returned, *result is unchanged. If TRUE is returned and endstr is non-NULL, the location of the first character in in_str not used by the parser will be returned in *endstr. If FALSE is returned and endstr is non-NULL, *endstr will point to in_str.

Definition at line 2083 of file gnc-ui-util.c.

2085 {
2086  return xaccParseAmountPosSign (in_str, monetary, result, endstr, FALSE);
2087 }
gboolean xaccParseAmountPosSign(const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr, gboolean skip)
Parses in_str to a gnc_numeric, with a flag to indicate whether the locale&#39;s positive sign (or in abs...
Definition: gnc-ui-util.c:2090

◆ xaccParseAmountExtended()

gboolean xaccParseAmountExtended ( const char *  in_str,
gboolean  monetary,
gunichar  negative_sign,
gunichar  decimal_point,
gunichar  group_separator,
const char *  ignore_list,
gnc_numeric *  result,
char **  endstr 
)

Converts a string to a gnc_numeric.

The caller must provide all the locale-specific information.

ignore_list is a list of characters that are completely ignored while processing the input string. If ignore_list is NULL, nothing is ignored.

Definition at line 2129 of file gnc-ui-util.c.

2133 {
2134  gboolean is_negative;
2135  gboolean got_decimal;
2136  gboolean need_paren;
2137  long long int numer;
2138  long long int denom;
2139  int count;
2140 
2141  ParseState state;
2142 
2143  const gchar *in;
2144  gunichar uc;
2145  gchar *out_str;
2146  gchar *out;
2147 
2148  /* Initialize *endstr to in_str */
2149  if (endstr != NULL)
2150  *endstr = (char *) in_str;
2151 
2152  if (in_str == NULL)
2153  return FALSE;
2154 
2155  if (!g_utf8_validate(in_str, -1, &in))
2156  {
2157  printf("Invalid utf8 string '%s'. Bad character at position %ld.\n",
2158  in_str, g_utf8_pointer_to_offset (in_str, in));
2159  return FALSE;
2160  }
2161 
2162  /* 'out_str' will be used to store digits for numeric conversion.
2163  * 'out' will be used to traverse out_str. */
2164  out = out_str = g_new(gchar, strlen(in_str) + 128);
2165 
2166  /* 'in' is used to traverse 'in_str'. */
2167  in = in_str;
2168 
2169  is_negative = FALSE;
2170  got_decimal = FALSE;
2171  need_paren = FALSE;
2172  numer = 0;
2173  denom = 1;
2174 
2175  /* Initialize the state machine */
2176  state = START_ST;
2177 
2178  /* This while loop implements a state machine for parsing numbers. */
2179  while (TRUE)
2180  {
2181  ParseState next_state = state;
2182 
2183  uc = g_utf8_get_char(in);
2184 
2185  /* Ignore anything in the 'ignore list' */
2186  if (ignore_list && uc && g_utf8_strchr(ignore_list, -1, uc) != NULL)
2187  {
2188  in = g_utf8_next_char(in);
2189  continue;
2190  }
2191 
2192  /* Note we never need to check for the end of 'in_str' explicitly.
2193  * The 'else' clauses on all the state transitions will handle that. */
2194  switch (state)
2195  {
2196  /* START_ST means we have parsed 0 or more whitespace characters */
2197  case START_ST:
2198  if (g_unichar_isdigit(uc))
2199  {
2200  count = g_unichar_to_utf8(uc, out);
2201  out += count; /* we record the digits themselves in out_str
2202  * for later conversion by libc routines */
2203  next_state = NUMER_ST;
2204  }
2205  else if (uc == decimal_point)
2206  {
2207  next_state = FRAC_ST;
2208  }
2209  else if (g_unichar_isspace(uc))
2210  {
2211  }
2212  else if (uc == negative_sign)
2213  {
2214  is_negative = TRUE;
2215  next_state = NEG_ST;
2216  }
2217  else if (uc == '(')
2218  {
2219  is_negative = TRUE;
2220  need_paren = TRUE;
2221  next_state = NEG_ST;
2222  }
2223  else
2224  {
2225  next_state = NO_NUM_ST;
2226  }
2227 
2228  break;
2229 
2230  /* NEG_ST means we have just parsed a negative sign. For now,
2231  * we only recognize formats where the negative sign comes first. */
2232  case NEG_ST:
2233  if (g_unichar_isdigit(uc))
2234  {
2235  count = g_unichar_to_utf8(uc, out);
2236  out += count;
2237  next_state = NUMER_ST;
2238  }
2239  else if (uc == decimal_point)
2240  {
2241  next_state = FRAC_ST;
2242  }
2243  else if (g_unichar_isspace(uc))
2244  {
2245  }
2246  else
2247  {
2248  next_state = NO_NUM_ST;
2249  }
2250 
2251  break;
2252 
2253  /* NUMER_ST means we have started parsing the number, but
2254  * have not encountered a decimal separator. */
2255  case NUMER_ST:
2256  if (g_unichar_isdigit(uc))
2257  {
2258  count = g_unichar_to_utf8(uc, out);
2259  out += count;
2260  }
2261  else if (uc == decimal_point)
2262  {
2263  next_state = FRAC_ST;
2264  }
2265  else if (uc == group_separator)
2266  {
2267  ; //ignore it
2268  }
2269  else if (uc == ')' && need_paren)
2270  {
2271  next_state = DONE_ST;
2272  need_paren = FALSE;
2273  }
2274  else
2275  {
2276  next_state = DONE_ST;
2277  }
2278 
2279  break;
2280 
2281  /* FRAC_ST means we are now parsing fractional digits. */
2282  case FRAC_ST:
2283  if (g_unichar_isdigit(uc))
2284  {
2285  count = g_unichar_to_utf8(uc, out);
2286  out += count;
2287  }
2288  else if (uc == decimal_point)
2289  {
2290  /* If a subsequent decimal point is also whitespace,
2291  * assume it was intended as such and stop parsing.
2292  * Otherwise, there is a problem. */
2293  if (g_unichar_isspace(decimal_point))
2294  next_state = DONE_ST;
2295  else
2296  next_state = NO_NUM_ST;
2297  }
2298  else if (uc == group_separator)
2299  {
2300  /* If a subsequent group separator is also whitespace,
2301  * assume it was intended as such and stop parsing.
2302  * Otherwise ignore it. */
2303  if (g_unichar_isspace(group_separator))
2304  next_state = DONE_ST;
2305  }
2306  else if (uc == ')' && need_paren)
2307  {
2308  next_state = DONE_ST;
2309  need_paren = FALSE;
2310  }
2311  else
2312  {
2313  next_state = DONE_ST;
2314  }
2315 
2316  break;
2317 
2318  default:
2319  PERR("bad state");
2320  g_assert_not_reached();
2321  break;
2322  }
2323 
2324  /* If we're moving into the FRAC_ST or out of the machine
2325  * without going through FRAC_ST, record the integral value. */
2326  if (((next_state == FRAC_ST) && (state != FRAC_ST)) ||
2327  ((next_state == DONE_ST) && !got_decimal))
2328  {
2329  *out = '\0';
2330 
2331  if (*out_str != '\0' && sscanf(out_str, QOF_SCANF_LLD, &numer) < 1)
2332  {
2333  next_state = NO_NUM_ST;
2334  }
2335  else if (next_state == FRAC_ST)
2336  {
2337  /* reset the out pointer to record the fraction */
2338  out = out_str;
2339  *out = '\0';
2340 
2341  got_decimal = TRUE;
2342  }
2343  }
2344 
2345  state = next_state;
2346  if (done_state (state))
2347  break;
2348 
2349  in = g_utf8_next_char(in);
2350  }
2351 
2352  /* If there was an error, just quit */
2353  if (need_paren || (state == NO_NUM_ST))
2354  {
2355  g_free(out_str);
2356  return FALSE;
2357  }
2358 
2359  /* Cap the end of the fraction string, if any */
2360  *out = '\0';
2361 
2362  /* Add in fractional value */
2363  if (got_decimal && (*out_str != '\0'))
2364  {
2365  size_t len;
2366  long long int fraction;
2367 
2368  len = strlen(out_str);
2369 
2370  if (len > 12)
2371  {
2372  out_str[12] = '\0';
2373  len = 12;
2374  }
2375 
2376  if (sscanf (out_str, QOF_SCANF_LLD, &fraction) < 1)
2377  {
2378  g_free(out_str);
2379  return FALSE;
2380  }
2381 
2382  denom = multiplier(len);
2383  numer *= denom;
2384  numer += fraction;
2385  }
2386  else if (monetary && auto_decimal_enabled && !got_decimal)
2387  {
2388  if ((auto_decimal_places > 0) && (auto_decimal_places <= 12))
2389  {
2390  denom = multiplier(auto_decimal_places);
2391 
2392  /* No need to multiply numer by denom at this point,
2393  * since by specifying the auto decimal places the
2394  * user has effectively determined the scaling factor
2395  * for the numerator they entered.
2396  */
2397  }
2398  }
2399 
2400  if (result != NULL)
2401  {
2402  *result = gnc_numeric_create (numer, denom);
2403  if (is_negative)
2404  *result = gnc_numeric_neg (*result);
2405  }
2406 
2407  if (endstr != NULL)
2408  *endstr = (char *) in;
2409 
2410  g_free (out_str);
2411 
2412  return TRUE;
2413 }
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value...
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244

◆ xaccParseAmountPosSign()

gboolean xaccParseAmountPosSign ( const char *  in_str,
gboolean  monetary,
gnc_numeric *  result,
char **  endstr,
gboolean  skip 
)

Parses in_str to a gnc_numeric, with a flag to indicate whether the locale's positive sign (or in absence the '+') character is ignored.

Setting skip to TRUE will cause the function to ignore any positive sign. Setting it to FALSE, and positive signs will be treated as unrecognized characters. xaccParseAmount will run as if skip is FALSE for compatibility reasons (gnc-expression-parser depends on this behaviour).

Definition at line 2090 of file gnc-ui-util.c.

2092 {
2093  struct lconv *lc = gnc_localeconv();
2094 
2095  gunichar negative_sign;
2096  gunichar decimal_point;
2097  gunichar group_separator;
2098  gchar *ignore = NULL;
2099  char *plus_sign = "+";
2100 
2101  negative_sign = g_utf8_get_char(lc->negative_sign);
2102  if (monetary)
2103  {
2104  group_separator = g_utf8_get_char(lc->mon_thousands_sep);
2105  decimal_point = g_utf8_get_char(lc->mon_decimal_point);
2106  }
2107  else
2108  {
2109  group_separator = g_utf8_get_char(lc->thousands_sep);
2110  decimal_point = g_utf8_get_char(lc->decimal_point);
2111  }
2112 
2113  if (skip)
2114  {
2115  /* We want the locale's positive sign to be ignored.
2116  * If the locale doesn't specify one, we assume "+" as
2117  * an optional positive sign and ignore that */
2118  ignore = lc->positive_sign;
2119  if (!ignore || !*ignore)
2120  ignore = plus_sign;
2121  }
2122 
2123  return xaccParseAmountExtended(in_str, monetary, negative_sign,
2124  decimal_point, group_separator,
2125  ignore, result, endstr);
2126 }
gboolean xaccParseAmountExtended(const char *in_str, gboolean monetary, gunichar negative_sign, gunichar decimal_point, gunichar group_separator, const char *ignore_list, gnc_numeric *result, char **endstr)
Converts a string to a gnc_numeric.
Definition: gnc-ui-util.c:2129

◆ xaccPrintAmount()

const char* xaccPrintAmount ( gnc_numeric  val,
GNCPrintAmountInfo  info 
)

Make a string representation of a gnc_numeric.

Warning, the gnc_numeric is not checked for validity and the returned char* may point to random garbage.

Definition at line 1837 of file gnc-ui-util.c.

1838 {
1839  /* hack alert -- this is not thread safe ... */
1840  static char buf[1024];
1841 
1842  if (!xaccSPrintAmount (buf, val, info))
1843  buf[0] = '\0';
1844 
1845  /* its OK to return buf, since we declared it static */
1846  return buf;
1847 }
int xaccSPrintAmount(char *bufp, gnc_numeric val, GNCPrintAmountInfo info)
Make a string representation of a gnc_numeric.
Definition: gnc-ui-util.c:1708

◆ xaccSPrintAmount()

int xaccSPrintAmount ( char *  bufp,
gnc_numeric  val,
GNCPrintAmountInfo  info 
)

Make a string representation of a gnc_numeric.

Warning, the gnc_numeric is not checked for validity and the contents of the buffer will be unchanged. It is up to the calling function to ensure that buf is large enough for the results.

Parameters
bufpShould be at least 64 chars.

Definition at line 1708 of file gnc-ui-util.c.

1709 {
1710  struct lconv *lc;
1711 
1712  char *orig_bufp = bufp;
1713  const char *currency_symbol;
1714  const char *sign;
1715 
1716  char cs_precedes;
1717  char sep_by_space;
1718  char sign_posn;
1719 
1720  gboolean print_sign = TRUE;
1721  gboolean print_absolute = FALSE;
1722 
1723  if (!bufp)
1724  return 0;
1725 
1726  lc = gnc_localeconv();
1727  if (info.use_locale)
1728  if (gnc_numeric_negative_p (val))
1729  {
1730  cs_precedes = lc->n_cs_precedes;
1731  sep_by_space = lc->n_sep_by_space;
1732  }
1733  else
1734  {
1735  cs_precedes = lc->p_cs_precedes;
1736  sep_by_space = lc->p_sep_by_space;
1737  }
1738  else
1739  {
1740  cs_precedes = TRUE;
1741  sep_by_space = TRUE;
1742  }
1743 
1744  if (info.commodity && info.use_symbol)
1745  {
1746  currency_symbol = gnc_commodity_get_nice_symbol (info.commodity);
1747  if (!gnc_commodity_is_iso (info.commodity))
1748  {
1749  cs_precedes = FALSE;
1750  sep_by_space = TRUE;
1751  }
1752  }
1753  else /* !info.use_symbol || !info.commodity */
1754  currency_symbol = "";
1755 
1756  if (gnc_numeric_negative_p (val))
1757  {
1758  sign = lc->negative_sign;
1759  sign_posn = lc->n_sign_posn;
1760  }
1761  else
1762  {
1763  sign = lc->positive_sign;
1764  sign_posn = lc->p_sign_posn;
1765  }
1766 
1767  if (gnc_numeric_zero_p (val) || (sign == NULL) || (sign[0] == 0))
1768  print_sign = FALSE;
1769 
1770  /* See if we print sign now */
1771  if (print_sign && (sign_posn == 1))
1772  bufp = g_stpcpy(bufp, sign);
1773 
1774  /* Now see if we print currency */
1775  if (cs_precedes)
1776  {
1777  /* See if we print sign now */
1778  if (print_sign && (sign_posn == 3))
1779  bufp = g_stpcpy(bufp, sign);
1780 
1781  if (info.use_symbol)
1782  {
1783  bufp = g_stpcpy(bufp, currency_symbol);
1784  if (sep_by_space)
1785  bufp = g_stpcpy(bufp, " ");
1786  }
1787 
1788  /* See if we print sign now */
1789  if (print_sign && (sign_posn == 4))
1790  bufp = g_stpcpy(bufp, sign);
1791  }
1792 
1793  /* Now see if we print parentheses */
1794  if (print_sign && (sign_posn == 0))
1795  {
1796  bufp = g_stpcpy(bufp, "(");
1797  print_absolute = TRUE;
1798  }
1799 
1800  /* Now print the value */
1801  bufp += PrintAmountInternal(bufp,
1802  print_absolute ? gnc_numeric_abs(val) : val,
1803  &info);
1804 
1805  /* Now see if we print parentheses */
1806  if (print_sign && (sign_posn == 0))
1807  bufp = g_stpcpy(bufp, ")");
1808 
1809  /* Now see if we print currency */
1810  if (!cs_precedes)
1811  {
1812  /* See if we print sign now */
1813  if (print_sign && (sign_posn == 3))
1814  bufp = g_stpcpy(bufp, sign);
1815 
1816  if (info.use_symbol)
1817  {
1818  if (sep_by_space)
1819  bufp = g_stpcpy(bufp, " ");
1820  bufp = g_stpcpy(bufp, currency_symbol);
1821  }
1822 
1823  /* See if we print sign now */
1824  if (print_sign && (sign_posn == 4))
1825  bufp = g_stpcpy(bufp, sign);
1826  }
1827 
1828  /* See if we print sign now */
1829  if (print_sign && (sign_posn == 2))
1830  bufp = g_stpcpy(bufp, sign);
1831 
1832  /* return length of printed string */
1833  return (bufp - orig_bufp);
1834 }
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
const char * gnc_commodity_get_nice_symbol(const gnc_commodity *cm)
Retrieve a symbol for the specified commodity, suitable for display to the user.
gnc_numeric gnc_numeric_abs(gnc_numeric a)
Returns a newly created gnc_numeric that is the absolute value of the given gnc_numeric value...
gboolean gnc_commodity_is_iso(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency.