Add a dialog to warn users that date is out of range

When the date is out of range present a dialog advising this and also
advise that date will be reset to this year.
This commit is contained in:
Robert Fewell
2018-06-10 14:24:34 +01:00
parent a73f9123f9
commit 2384af6068
3 changed files with 44 additions and 14 deletions

View File

@@ -39,6 +39,7 @@
#include "gnc-path.h"
#include "gnc-engine.h"
#include "gnc-euro.h"
#include "gnc-ui.h"
#include "gnc-ui-util.h"
#include "gnc-prefs.h"
#include "gnc-combott.h"
@@ -325,16 +326,16 @@ gnc_draw_arrow_cb (GtkWidget *widget, cairo_t *cr, gpointer direction)
}
static gboolean
gnc_gdate_in_valid_range (GDate *test_date)
gboolean
gnc_gdate_in_valid_range (GDate *test_date, gboolean warn)
{
gboolean use_autoreadonly = qof_book_uses_autoreadonly(gnc_get_current_book());
gboolean use_autoreadonly = qof_book_uses_autoreadonly (gnc_get_current_book());
GDate *max_date = g_date_new_dmy (1,1,10000);
GDate *min_date;
gboolean ret = FALSE;
if (use_autoreadonly)
min_date = qof_book_get_autoreadonly_gdate(gnc_get_current_book());
min_date = qof_book_get_autoreadonly_gdate (gnc_get_current_book());
else
min_date = g_date_new_dmy (1,1,1400);
@@ -342,6 +343,21 @@ gnc_gdate_in_valid_range (GDate *test_date)
(g_date_compare (min_date, test_date) <= 0))
ret = TRUE;
if (warn && !ret)
{
gchar *dialog_msg = _("The entered date is out of the range "
"01/01/1400 - 31/12/9999, resetting to this year");
gchar *dialog_title = _("Date out of range");
GtkWidget *dialog = gtk_message_dialog_new (gnc_ui_get_main_window (NULL),
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", dialog_title);
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG(dialog),
"%s", dialog_msg);
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
}
g_date_free (max_date);
g_date_free (min_date);
return ret;
@@ -392,7 +408,7 @@ gnc_handle_date_accelerator (GdkEventKey *event,
else
g_date_add_days (&gdate, 1);
if (gnc_gdate_in_valid_range (&gdate))
if (gnc_gdate_in_valid_range (&gdate, FALSE))
g_date_to_struct_tm (&gdate, tm);
return TRUE;
@@ -428,7 +444,7 @@ gnc_handle_date_accelerator (GdkEventKey *event,
else
g_date_subtract_days (&gdate, 1);
if (gnc_gdate_in_valid_range (&gdate))
if (gnc_gdate_in_valid_range (&gdate, FALSE))
g_date_to_struct_tm (&gdate, tm);
return TRUE;
@@ -499,7 +515,7 @@ gnc_handle_date_accelerator (GdkEventKey *event,
default:
return FALSE;
}
if (gnc_gdate_in_valid_range (&gdate))
if (gnc_gdate_in_valid_range (&gdate, FALSE))
g_date_to_struct_tm (&gdate, tm);
return TRUE;

View File

@@ -98,6 +98,8 @@ void gnc_widget_set_style_context (GtkWidget *widget, const char *gnc_class);
\********************************************************************/
gboolean gnc_draw_arrow_cb (GtkWidget *widget, cairo_t *cr, gpointer direction);
gboolean gnc_gdate_in_valid_range (GDate *test_date, gboolean warn);
gboolean gnc_handle_date_accelerator (GdkEventKey *event,
struct tm *tm,
const char *date_str);

View File

@@ -126,10 +126,11 @@ check_readonly_threshold (const gchar *datestr, GDate *d)
}
static void
gnc_parse_date (struct tm *parsed, const char * datestr)
gnc_parse_date (struct tm *parsed, const char * datestr, gboolean warn)
{
int day, month, year;
gboolean use_autoreadonly = qof_book_uses_autoreadonly(gnc_get_current_book());
GDate *test_date;
if (!parsed) return;
if (!datestr) return;
@@ -146,6 +147,16 @@ gnc_parse_date (struct tm *parsed, const char * datestr)
year = tm_today.tm_year + 1900;
}
test_date = g_date_new_dmy (day, month, year);
if (!gnc_gdate_in_valid_range (test_date, warn))
{
struct tm tm_today;
memset (&tm_today, 0, sizeof (struct tm));
gnc_tm_get_today_start (&tm_today);
year = tm_today.tm_year + 1900;
}
// If we have an auto-read-only threshold, do not accept a date that is
// older than the threshold.
if (use_autoreadonly)
@@ -159,6 +170,7 @@ gnc_parse_date (struct tm *parsed, const char * datestr)
}
g_date_free (d);
}
g_date_free (test_date);
parsed->tm_mday = day;
parsed->tm_mon = month - 1;
@@ -452,7 +464,7 @@ gnc_date_cell_commit (DateCell *cell)
if (!cell)
return;
gnc_parse_date (&(box->date), cell->cell.value);
gnc_parse_date (&(box->date), cell->cell.value, FALSE);
qof_print_date_dmy_buff (buff, MAX_DATE_LENGTH,
box->date.tm_mday,
@@ -581,9 +593,8 @@ gnc_date_cell_modify_verify (BasicCell *_cell,
/* keep a copy of the new value */
if (accept)
{
gnc_basic_cell_set_value_internal (&cell->cell, newval);
gnc_parse_date (&(box->date), newval);
gnc_parse_date (&(box->date), newval, FALSE);
if (!box->date_picker)
return;
@@ -710,7 +721,7 @@ gnc_date_cell_get_date_gdate (DateCell *cell, GDate *date)
if (!cell || !date)
return;
gnc_parse_date (&(box->date), cell->cell.value);
gnc_parse_date (&(box->date), cell->cell.value, FALSE);
g_date_set_dmy(date,
box->date.tm_mday,
@@ -724,7 +735,8 @@ gnc_date_cell_get_date (DateCell *cell, time64 *time)
PopBox *box = cell->cell.gui_private;
if (!cell || !time)
return;
gnc_parse_date (&(box->date), cell->cell.value);
gnc_parse_date (&(box->date), cell->cell.value, TRUE);
*time = gnc_mktime (&box->date);
}
@@ -735,7 +747,7 @@ gnc_date_cell_set_value_internal (BasicCell *_cell, const char *str)
PopBox *box = cell->cell.gui_private;
char buff[DATE_BUF];
gnc_parse_date (&(box->date), str);
gnc_parse_date (&(box->date), str, FALSE);
qof_print_date_dmy_buff (buff, MAX_DATE_LENGTH,
box->date.tm_mday,