Data save and removal of the progress bar

The function io_save_cal() saves apts, todos, configuration data and key
bindings. The configuration and key files do not belong with the two data
files, but the progress bar function assumes that all four files are saved in
a fixed sequence. Since it is used nowhere else and contains unused parts,
the function has been removed.

A return code for file access error is introduced, and the EXIT macro moved to
the command level in calcurse.c.

Save of configuration and key data were already moved to the configuration menu
in commit 0124618, A save refinement: no action if everything is unchanged.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2018-08-28 21:11:06 +02:00 committed by Lukas Fleischer
parent 8b39637a62
commit 39ab4665e6
3 changed files with 27 additions and 128 deletions

View File

@ -274,7 +274,7 @@ static inline void key_generic_save(void)
char *msg = NULL; char *msg = NULL;
int ret; int ret;
ret = io_save_cal(IO_SAVE_DISPLAY_BAR); ret = io_save_cal();
if (ret == IO_SAVE_RELOAD) { if (ret == IO_SAVE_RELOAD) {
ui_todo_load_items(); ui_todo_load_items();
@ -297,6 +297,8 @@ static inline void key_generic_save(void)
case IO_SAVE_NOOP: case IO_SAVE_NOOP:
msg = _("Data were already saved"); msg = _("Data were already saved");
break; break;
case IO_SAVE_ERROR:
EXIT(_("Cannot open data file"));
} }
status_mesg(msg, ""); status_mesg(msg, "");
} }
@ -503,7 +505,7 @@ static inline void key_generic_scroll_down(void)
static inline void key_generic_quit(void) static inline void key_generic_quit(void)
{ {
if (conf.auto_save) if (conf.auto_save)
io_save_cal(IO_SAVE_DISPLAY_BAR); io_save_cal();
if (conf.auto_gc) if (conf.auto_gc)
note_gc(); note_gc();
@ -538,7 +540,7 @@ static inline void key_generic_cmd(void)
if (!strcmp(cmd_name, "write") || !strcmp(cmd_name, "w") || if (!strcmp(cmd_name, "write") || !strcmp(cmd_name, "w") ||
!strcmp(cmd_name, "wq")) { !strcmp(cmd_name, "wq")) {
io_save_cal(IO_SAVE_DISPLAY_BAR); io_save_cal();
valid = 1; valid = 1;
} }
if (!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "q") || if (!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "q") ||

View File

@ -659,7 +659,8 @@ enum {
IO_SAVE_CTINUE, IO_SAVE_CTINUE,
IO_SAVE_RELOAD, IO_SAVE_RELOAD,
IO_SAVE_CANCEL, IO_SAVE_CANCEL,
IO_SAVE_NOOP IO_SAVE_NOOP,
IO_SAVE_ERROR
}; };
/* Return codes for the io_reload_data() function. */ /* Return codes for the io_reload_data() function. */
@ -727,12 +728,6 @@ enum export_type {
IO_EXPORT_NBTYPES IO_EXPORT_NBTYPES
}; };
/* To customize the display when saving data. */
enum save_display {
IO_SAVE_DISPLAY_BAR,
IO_SAVE_DISPLAY_NONE
};
/* apoint.c */ /* apoint.c */
extern llist_ts_t alist_p; extern llist_ts_t alist_p;
void apoint_free_bkp(void); void apoint_free_bkp(void);
@ -867,7 +862,7 @@ unsigned io_save_apts(const char *);
void io_dump_todo(const char *); void io_dump_todo(const char *);
unsigned io_save_todo(const char *); unsigned io_save_todo(const char *);
unsigned io_save_keys(void); unsigned io_save_keys(void);
int io_save_cal(enum save_display); int io_save_cal(void);
void io_load_app(struct item_filter *); void io_load_app(struct item_filter *);
void io_load_todo(struct item_filter *); void io_load_todo(struct item_filter *);
int io_load_data(struct item_filter *, int); int io_load_data(struct item_filter *, int);

136
src/io.c
View File

@ -48,25 +48,6 @@
#include "calcurse.h" #include "calcurse.h"
#include "sha1.h" #include "sha1.h"
typedef enum {
PROGRESS_BAR_SAVE,
PROGRESS_BAR_LOAD,
PROGRESS_BAR_EXPORT
} progress_bar_t;
enum {
PROGRESS_BAR_CONF,
PROGRESS_BAR_TODO,
PROGRESS_BAR_APTS,
PROGRESS_BAR_KEYS
};
enum {
PROGRESS_BAR_EXPORT_EVENTS,
PROGRESS_BAR_EXPORT_APOINTS,
PROGRESS_BAR_EXPORT_TODO
};
struct ht_keybindings_s { struct ht_keybindings_s {
const char *label; const char *label;
enum key key; enum key key;
@ -88,74 +69,6 @@ static int modified = 0;
static char apts_sha1[SHA1_DIGESTLEN * 2 + 1]; static char apts_sha1[SHA1_DIGESTLEN * 2 + 1];
static char todo_sha1[SHA1_DIGESTLEN * 2 + 1]; static char todo_sha1[SHA1_DIGESTLEN * 2 + 1];
/* Draw a progress bar while saving, loading or exporting data. */
static void progress_bar(progress_bar_t type, int progress)
{
#define NBFILES 4
#define NBEXPORTED 3
#define LABELENGTH 15
int i, step, steps;
const char *mesg_sav = _("Saving...");
const char *mesg_load = _("Loading...");
const char *mesg_export = _("Exporting...");
const char *error_msg =
_("Internal error while displaying progress bar");
const char *barchar = "|";
const char *file[NBFILES] = {
"[ conf ]",
"[ todo ]",
"[ apts ]",
"[ keys ]"
};
const char *data[NBEXPORTED] = {
"[ events ]",
"[appointments]",
"[ todo ]"
};
int ipos = LABELENGTH + 2;
int epos[NBFILES];
/* progress bar length init. */
ipos = LABELENGTH + 2;
steps = (type == PROGRESS_BAR_EXPORT) ? NBEXPORTED : NBFILES;
step = floor(col / (steps + 1));
for (i = 0; i < steps - 1; i++)
epos[i] = (i + 2) * step;
epos[steps - 1] = col - 2;
switch (type) {
case PROGRESS_BAR_SAVE:
EXIT_IF(progress < 0
|| progress > PROGRESS_BAR_KEYS, "%s", error_msg);
status_mesg(mesg_sav, file[progress]);
break;
case PROGRESS_BAR_LOAD:
EXIT_IF(progress < 0
|| progress > PROGRESS_BAR_KEYS, "%s", error_msg);
status_mesg(mesg_load, file[progress]);
break;
case PROGRESS_BAR_EXPORT:
EXIT_IF(progress < 0
|| progress > PROGRESS_BAR_EXPORT_TODO, "%s",
error_msg);
status_mesg(mesg_export, data[progress]);
break;
}
/* Draw the progress bar. */
mvwaddstr(win[STA].p, 1, ipos, barchar);
mvwaddstr(win[STA].p, 1, epos[steps - 1], barchar);
custom_apply_attr(win[STA].p, ATTR_HIGHEST);
for (i = ipos + 1; i < epos[progress]; i++)
mvwaddch(win[STA].p, 1, i, ' ' | A_REVERSE);
custom_remove_attr(win[STA].p, ATTR_HIGHEST);
wmove(win[STA].p, 0, 0);
wins_wrefresh(win[STA].p);
#undef NBFILES
#undef NBEXPORTED
#undef LABELENGTH
}
/* Ask user for a file name to export data to. */ /* Ask user for a file name to export data to. */
static FILE *get_export_stream(enum export_type type) static FILE *get_export_stream(enum export_type type)
{ {
@ -575,55 +488,44 @@ cleanup:
* IO_SAVE_RELOAD: cancel save operation (data files changed and reloaded) * IO_SAVE_RELOAD: cancel save operation (data files changed and reloaded)
* IO_SAVE_CANCEL: cancel save operation (user's decision, keep data files, no reload) * IO_SAVE_CANCEL: cancel save operation (user's decision, keep data files, no reload)
* IO_SAVE_NOOP: cancel save operation (nothing has changed) * IO_SAVE_NOOP: cancel save operation (nothing has changed)
* IO_SAVE_ERROR: cannot access data
*/ */
int io_save_cal(enum save_display display) int io_save_cal(void)
{ {
const char *access_pb = _("Problems accessing data file ..."); int ret;
int show_bar, ret = IO_SAVE_CTINUE;
if (read_only) if (read_only)
return IO_SAVE_CANCEL; return IO_SAVE_CANCEL;
if (new_data()) { if ((ret = new_data()) == NOKNOW) {
return IO_SAVE_ERROR;
} else if (ret) { /* New data */
if ((ret = resolve_save_conflict())) if ((ret = resolve_save_conflict()))
return ret; return ret;
} else } else /* No new data */
if (!io_get_modified()) if (!io_get_modified())
return IO_SAVE_NOOP; return IO_SAVE_NOOP;
ret = IO_SAVE_CTINUE;
run_hook("pre-save"); run_hook("pre-save");
io_mutex_lock(); io_mutex_lock();
show_bar = 0; if (!io_save_todo(path_todo)) {
if (ui_mode == UI_CURSES && display == IO_SAVE_DISPLAY_BAR ret = IO_SAVE_ERROR;
&& conf.progress_bar) goto cleanup;
show_bar = 1; }
if (!io_save_apts(path_apts)) {
if (show_bar) ret = IO_SAVE_ERROR;
progress_bar(PROGRESS_BAR_SAVE, PROGRESS_BAR_CONF); goto cleanup;
if (!config_save()) }
ERROR_MSG("%s", access_pb);
if (show_bar)
progress_bar(PROGRESS_BAR_SAVE, PROGRESS_BAR_TODO);
if (!io_save_todo(path_todo))
ERROR_MSG("%s", access_pb);
if (show_bar)
progress_bar(PROGRESS_BAR_SAVE, PROGRESS_BAR_APTS);
if (!io_save_apts(path_apts))
ERROR_MSG("%s", access_pb);
if (show_bar)
progress_bar(PROGRESS_BAR_SAVE, PROGRESS_BAR_KEYS);
if (!io_save_keys())
ERROR_MSG("%s", access_pb);
io_unset_modified(); io_unset_modified();
io_compute_hash(path_apts, apts_sha1); io_compute_hash(path_apts, apts_sha1);
io_compute_hash(path_todo, todo_sha1); io_compute_hash(path_todo, todo_sha1);
cleanup:
io_mutex_unlock(); io_mutex_unlock();
run_hook("post-save"); run_hook("post-save");
return ret; return ret;
@ -1520,7 +1422,7 @@ static void *io_psave_thread(void *arg)
for (;;) { for (;;) {
sleep(delay * MININSEC); sleep(delay * MININSEC);
io_save_cal(IO_SAVE_DISPLAY_NONE); io_save_cal();
} }
} }