Skip to content

Commit

Permalink
Financials 0.2.0, breaks db API, added css, changed hotkeys window
Browse files Browse the repository at this point in the history
  • Loading branch information
BostonBSD committed Feb 11, 2023
1 parent e652c81 commit 23805dd
Show file tree
Hide file tree
Showing 27 changed files with 3,026 additions and 1,966 deletions.
Binary file modified financials11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 12 additions & 17 deletions src/class/class_bullion.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,32 @@ static void ToStrings(guint8 digits_right) {
BLACK);

/* The change in total investment in bullion. */
if (M->bullion_port_value_chg_f > 0) {
if (M->bullion_port_value_chg_f > 0)
DoubleToFormattedStrPango(&M->bullion_port_value_chg_mrkd_ch,
M->bullion_port_value_chg_f, digits_right,
MON_STR, GREEN);
} else if (M->bullion_port_value_chg_f < 0) {
else if (M->bullion_port_value_chg_f < 0)
DoubleToFormattedStrPango(&M->bullion_port_value_chg_mrkd_ch,
M->bullion_port_value_chg_f, digits_right,
MON_STR, RED);
} else {
else
DoubleToFormattedStrPango(&M->bullion_port_value_chg_mrkd_ch,
M->bullion_port_value_chg_f, digits_right,
MON_STR, BLACK);
}

/* The change in total investment in bullion as a percentage. */
if (M->bullion_port_value_p_chg_f > 0) {
if (M->bullion_port_value_p_chg_f > 0)
DoubleToFormattedStrPango(&M->bullion_port_value_p_chg_mrkd_ch,
M->bullion_port_value_p_chg_f, digits_right,
PER_STR, GREEN);
} else if (M->bullion_port_value_p_chg_f < 0) {
else if (M->bullion_port_value_p_chg_f < 0)
DoubleToFormattedStrPango(&M->bullion_port_value_p_chg_mrkd_ch,
M->bullion_port_value_p_chg_f, digits_right,
PER_STR, RED);
} else {
else
DoubleToFormattedStrPango(&M->bullion_port_value_p_chg_mrkd_ch,
M->bullion_port_value_p_chg_f, digits_right,
PER_STR, BLACK);
}

/* The Gold to Silver Ratio */
DoubleToFormattedStr(&M->gold_silver_ratio_ch, M->gold_silver_ratio_f, 2,
Expand All @@ -180,20 +178,18 @@ static void bullion_calculations(bullion *B) {
there is no bullion, the gain is calculated based off of the current and
prev spot price. These gains are ordinarily
different because of premiums. */
if (prev_total == 0) {
if (prev_total == 0)
B->change_percent_f = CalcGain(B->spot_price_f, B->prev_closing_metal_f);
} else {
else
B->change_percent_f = CalcGain(B->port_value_f, prev_total);
}

/* The raw change in bullion as a percentage. */
/* This if statement prevent's a "nan%" string in the index label. */
if (B->prev_closing_metal_f == 0) {
if (B->prev_closing_metal_f == 0)
B->change_percent_raw_f = 0.0f;
} else {
else
B->change_percent_raw_f =
CalcGain(B->spot_price_f, B->prev_closing_metal_f);
}
}

static void Calculate() {
Expand Down Expand Up @@ -221,12 +217,11 @@ static void Calculate() {

/* The change in total investment in bullion as a percentage. */
gdouble prev_total = M->bullion_port_value_f - M->bullion_port_value_chg_f;
if (prev_total == 0.0f) {
if (prev_total == 0.0f)
M->bullion_port_value_p_chg_f = 0.0f;
} else {
else
M->bullion_port_value_p_chg_f =
CalcGain(M->bullion_port_value_f, prev_total);
}

/* The Gold to Silver Ratio */
if (M->Silver->spot_price_f > 0)
Expand Down
20 changes: 8 additions & 12 deletions src/class/class_equity.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@ static void convert_equity_to_strings(stock *S, guint8 digits_right) {
S->prev_closing_stock_f, digits_right, MON_STR,
BLACK);

if (S->change_value_f > 0) {
if (S->change_value_f > 0)
DoubleToFormattedStrPango(&S->change_value_mrkd_ch, S->change_value_f,
digits_right, MON_STR, GREEN);
} else if (S->change_value_f < 0) {
else if (S->change_value_f < 0)
DoubleToFormattedStrPango(&S->change_value_mrkd_ch, S->change_value_f,
digits_right, MON_STR, RED);
} else {
else
DoubleToFormattedStrPango(&S->change_value_mrkd_ch, S->change_value_f,
digits_right, MON_STR, BLACK);
}

DoubleToFormattedStrPango(&S->num_shares_stock_mrkd_ch,
(double)S->num_shares_stock_int, 0, NUM_STR, BLACK);
Expand Down Expand Up @@ -156,9 +155,8 @@ static void convert_equity_to_strings(stock *S, guint8 digits_right) {
static void ToStrings(guint8 digits_right) {
equity_folder *F = FolderClassObject;

for (guint8 g = 0; g < F->size; g++) {
for (guint8 g = 0; g < F->size; g++)
convert_equity_to_strings(F->Equity[g], digits_right);
}

/* The total equity portfolio value. */
DoubleToFormattedStrPango(&F->stock_port_value_mrkd_ch, F->stock_port_value_f,
Expand Down Expand Up @@ -195,11 +193,10 @@ static void ToStrings(guint8 digits_right) {

static void equity_calculations(stock *S) {
/* Calculate the stock holdings change. */
if (S->num_shares_stock_int > 0) {
if (S->num_shares_stock_int > 0)
S->change_value_f = S->change_share_f * (gdouble)S->num_shares_stock_int;
} else {
else
S->change_value_f = 0.0f;
}

/* The total current investment in this equity. */
S->current_investment_stock_f =
Expand Down Expand Up @@ -464,13 +461,12 @@ static void SetSecurityNames(portfolio_packet *pkg) {
pkg->GetMetaClass());
remove_dash(security_name);

if (F->Equity[g]->num_shares_stock_int) {
if (F->Equity[g]->num_shares_stock_int)
StringToStrPango(&F->Equity[g]->security_name_mrkd_ch,
security_name ? security_name : "", BLUE);
} else {
else
StringToStrPango(&F->Equity[g]->security_name_mrkd_ch,
security_name ? security_name : "", BLACK_ITALIC);
}

if (security_name) {
g_free(security_name);
Expand Down
7 changes: 4 additions & 3 deletions src/class/class_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,11 @@ meta *ClassInitMeta() {
new_class->font_ch = g_strdup(MAIN_FONT);
SetFont(new_class->font_ch);

/* Set The User's Config Directory */
new_class->config_dir_ch = g_strdup(g_get_user_config_dir());
/* Set The User's Config Directory: ~/.config/financials */
new_class->config_dir_ch =
g_strconcat(g_get_user_config_dir(), CONFIG_DIR, NULL);

/* Append the sqlite db file paths to the end of the config directory path. */
/* Append the sqlite db files to the end of the config directory path. */
new_class->sqlite_db_path_ch =
g_strconcat(new_class->config_dir_ch, DB_FILE, NULL);
new_class->sqlite_symbol_name_db_path_ch =
Expand Down
121 changes: 65 additions & 56 deletions src/class/class_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ static void FreeMainCurlData() {
metal *M = packet->GetMetalClass();
meta *Met = packet->GetMetaClass();

for (guint8 c = 0; c < F->size; c++) {
for (guint8 c = 0; c < F->size; c++)
FreeMemtype(&F->Equity[c]->JSON);
}

FreeMemtype(&M->Gold->CURLDATA);
FreeMemtype(&M->Silver->CURLDATA);
FreeMemtype(&M->Platinum->CURLDATA);
Expand Down Expand Up @@ -173,9 +173,8 @@ static void remove_main_curl_handles(portfolio_packet *pkg)
g_mutex_lock(&mutexes[MULTICURL_PROG_MUTEX]);

/* Equity Multicurl Operation */
for (guint8 i = 0; i < F->size; i++) {
for (guint8 i = 0; i < F->size; i++)
curl_multi_remove_handle(pkg->multicurl_main_hnd, F->Equity[i]->easy_hnd);
}

/* Bullion Multicurl Operation */
curl_multi_remove_handle(pkg->multicurl_main_hnd, M->Gold->YAHOO_hnd);
Expand Down Expand Up @@ -253,10 +252,10 @@ static void SetIndicesDisplayed(gboolean displayed_bool) {
static gboolean IsClosed()
/* Market Closed flag */
{
if (!packet->meta_class->clocks_displayed_bool) {
if (!packet->meta_class->clocks_displayed_bool)
packet->meta_class->market_closed_bool =
GetTimeData(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}

return packet->meta_class->market_closed_bool;
}

Expand Down Expand Up @@ -327,21 +326,18 @@ static void save_sql_data_bul(const bullion *gold, const bullion *silver,
const bullion *palladium) {
meta *D = packet->GetMetaClass();

gchar *gold_ounces_ch = g_markup_printf_escaped("%lf", gold->ounce_f);
gchar *gold_premium_ch = g_markup_printf_escaped("%lf", gold->premium_f);
gchar *silver_ounces_ch = g_markup_printf_escaped("%lf", silver->ounce_f);
gchar *silver_premium_ch = g_markup_printf_escaped("%lf", silver->premium_f);
gchar *platinum_ounces_ch = g_markup_printf_escaped("%lf", platinum->ounce_f);
gchar *platinum_premium_ch =
g_markup_printf_escaped("%lf", platinum->premium_f);
gchar *palladium_ounces_ch =
g_markup_printf_escaped("%lf", palladium->ounce_f);
gchar *palladium_premium_ch =
g_markup_printf_escaped("%lf", palladium->premium_f);
SqliteBullionAdd(
D, "gold", gold_ounces_ch, gold_premium_ch, "silver", silver_ounces_ch,
silver_premium_ch, "platinum", platinum_ounces_ch, platinum_premium_ch,
"palladium", palladium_ounces_ch, palladium_premium_ch, NULL);
gchar *gold_ounces_ch = SnPrint("%lf", gold->ounce_f);
gchar *gold_premium_ch = SnPrint("%lf", gold->premium_f);
gchar *silver_ounces_ch = SnPrint("%lf", silver->ounce_f);
gchar *silver_premium_ch = SnPrint("%lf", silver->premium_f);
gchar *platinum_ounces_ch = SnPrint("%lf", platinum->ounce_f);
gchar *platinum_premium_ch = SnPrint("%lf", platinum->premium_f);
gchar *palladium_ounces_ch = SnPrint("%lf", palladium->ounce_f);
gchar *palladium_premium_ch = SnPrint("%lf", palladium->premium_f);
SqliteBullionAdd(D, "gold", gold_ounces_ch, gold_premium_ch, "silver",
silver_ounces_ch, silver_premium_ch, "platinum",
platinum_ounces_ch, platinum_premium_ch, "palladium",
palladium_ounces_ch, palladium_premium_ch, NULL);
g_free(gold_ounces_ch);
g_free(gold_premium_ch);
g_free(silver_ounces_ch);
Expand All @@ -352,7 +348,10 @@ static void save_sql_data_bul(const bullion *gold, const bullion *silver,
g_free(palladium_premium_ch);
}

static void save_sql_data_pref(meta *D) {
static void save_sql_data_app(portfolio_packet *pkg) {
meta *D = pkg->GetMetaClass();
window_data *W = pkg->GetWindowData();

const gchar *clks_displ_value;
if (D->clocks_displayed_bool) {
clks_displ_value = "TRUE";
Expand All @@ -366,49 +365,59 @@ static void save_sql_data_pref(meta *D) {
indc_displ_value = "FALSE";
}

gchar *dec_pl_value = g_markup_printf_escaped("%d", D->decimal_places_guint8);
gchar *up_per_min_value =
g_markup_printf_escaped("%lf", D->updates_per_min_f);
gchar *up_hours_value = g_markup_printf_escaped("%lf", D->updates_hours_f);

SqliteAPIPrefAdd(
PREF, D, "Main_Font", D->font_ch, "Clocks_Displayed", clks_displ_value,
"Indices_Displayed", indc_displ_value, "Decimal_Places", dec_pl_value,
"Updates_Per_Min", up_per_min_value, "Updates_Hours", up_hours_value,
gchar *dec_pl_value = SnPrint("%d", D->decimal_places_guint8);
gchar *up_per_min_value = SnPrint("%lf", D->updates_per_min_f);
gchar *up_hours_value = SnPrint("%lf", D->updates_hours_f);

gchar *cash_value = SnPrint("%lf", D->cash_f);

gchar *main_wdth_value = SnPrint("%d", W->main_width);
gchar *main_hght_value = SnPrint("%d", W->main_height);
gchar *main_xpos_value = SnPrint("%d", W->main_x_pos);
gchar *main_ypos_value = SnPrint("%d", W->main_y_pos);

gchar *hstry_wdth_value = SnPrint("%d", W->history_width);
gchar *hstry_hght_value = SnPrint("%d", W->history_height);
gchar *hstry_xpos_value = SnPrint("%d", W->history_x_pos);
gchar *hstry_ypos_value = SnPrint("%d", W->history_y_pos);

const gchar *null_str = "null";
/* We want the cash_value to be after dec_pl_value, such that dec_pl_value is
* read before cash_value on a SELECT * statement. */
SqliteAppAdd(
D, "Stock_URL", D->stock_url_ch, null_str, "URL_KEY", D->curl_key_ch,
null_str, "Nasdaq_Symbol_URL", D->Nasdaq_Symbol_url_ch, null_str,
"NYSE_Symbol_URL", D->NYSE_Symbol_url_ch, null_str, "Main_Font",
D->font_ch, null_str, "Clocks_Displayed", clks_displ_value, null_str,
"Indices_Displayed", indc_displ_value, null_str, "Decimal_Places",
dec_pl_value, null_str, "Updates_Per_Min", up_per_min_value, null_str,
"Updates_Hours", up_hours_value, null_str, "Cash", cash_value, null_str,
"Main_Win_Sz", main_wdth_value, main_hght_value, "Main_Win_Pos",
main_xpos_value, main_ypos_value, "Hstry_Win_Sz", hstry_wdth_value,
hstry_hght_value, "Hstry_Win_Pos", hstry_xpos_value, hstry_ypos_value,
NULL);

g_free(dec_pl_value);
g_free(up_per_min_value);
g_free(up_hours_value);
}

static void save_sql_data_api(meta *D) {
SqliteAPIPrefAdd(API, D, "Stock_URL", D->stock_url_ch, "URL_KEY",
D->curl_key_ch, "Nasdaq_Symbol_URL",
D->Nasdaq_Symbol_url_ch, "NYSE_Symbol_URL",
D->NYSE_Symbol_url_ch, NULL);
g_free(cash_value);

g_free(main_wdth_value);
g_free(main_hght_value);
g_free(main_xpos_value);
g_free(main_ypos_value);

g_free(hstry_wdth_value);
g_free(hstry_hght_value);
g_free(hstry_xpos_value);
g_free(hstry_ypos_value);
}

static void SaveSqlData() {
metal *M = packet->GetMetalClass();
meta *D = packet->GetMetaClass();
window_data *W = packet->GetWindowData();

/* Save the Window Size and Location. */
SqliteMainWindowSizeAdd(W->main_width, W->main_height, D);
SqliteMainWindowPosAdd(W->main_x_pos, W->main_y_pos, D);
SqliteHistoryWindowSizeAdd(W->history_width, W->history_height, D);
SqliteHistoryWindowPosAdd(W->history_x_pos, W->history_y_pos, D);

/* Save preference info. */
save_sql_data_pref(D);

/* Save api info. */
save_sql_data_api(D);

/* Save cash info. */
gchar *value = g_markup_printf_escaped("%lf", D->cash_f);
SqliteCashAdd(value, D);
g_free(value);
/* Save application info. */
save_sql_data_app(packet);

/* Save bullion info. */
save_sql_data_bul(M->Gold, M->Silver, M->Platinum, M->Palladium);
Expand Down
11 changes: 2 additions & 9 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,16 @@ gint RemoveConfigFiles(meta *D) {
ret_val++;
if (g_remove(D->sqlite_symbol_name_db_path_ch) < 0)
ret_val++;
gchar *path = g_strconcat(D->config_dir_ch, CONFIG_DIR, NULL);
if (g_remove(path) < 0)
if (g_remove(D->config_dir_ch) < 0)
ret_val++;
g_free(path);
return ret_val;
}

static void config_dir_processing(const gchar *usr_config_dir)
/* Check if the "~/.config" and "~/.config/financials" directories exist. */
{
/* Append the /financials directory to the end of user config directory path.
*/
gchar *path = g_strconcat(usr_config_dir, CONFIG_DIR, NULL);

/* Make dir if it doesn't exist, along with parent dirs. */
g_mkdir_with_parents(path, 0764);
g_free(path);
g_mkdir_with_parents(usr_config_dir, 0764);
}

void ReadConfig(portfolio_packet *pkg) {
Expand Down
Loading

0 comments on commit 23805dd

Please sign in to comment.