From 5b9fd70f308484d164e0969584fa6fb500b0e2a4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 8 Jul 2014 14:46:50 -0400 Subject: [PATCH] autosave every 60 seconds --- src/misc_tools.c | 12 ++++++------ src/toxic.c | 12 +++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/misc_tools.c b/src/misc_tools.c index cd359a0..13c7fb8 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -46,6 +46,12 @@ uint64_t get_unix_time(void) return current_unix_time; } +/* Returns 1 if connection has timed out, 0 otherwise */ +int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout) +{ + return timestamp + timeout <= curtime; +} + /* Get the current local time */ struct tm *get_time(void) { @@ -135,12 +141,6 @@ int wcs_to_mbs_buf(char *buf, const wchar_t *string, size_t n) return len; } -/* Returns 1 if connection has timed out, 0 otherwise */ -int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout) -{ - return timestamp + timeout <= curtime; -} - /* Colours the window tab according to type. Beeps if is_beep is true */ void alert_window(ToxWindow *self, int type, bool is_beep) { diff --git a/src/toxic.c b/src/toxic.c index 22bf2fb..d1be879 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -67,6 +67,8 @@ ToxAv *av; char *DATA_FILE = NULL; ToxWindow *prompt = NULL; +#define AUTOSAVE_FREQ 60 + struct arg_opts { int ignore_data_file; int use_ipv4; @@ -640,13 +642,21 @@ int main(int argc, char *argv[]) line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } - sort_friendlist_index(); prompt_init_statusbar(prompt, m); + uint64_t last_save = get_unix_time(); + while (true) { update_unix_time(); do_toxic(m, prompt); + uint64_t cur_time = get_unix_time(); + + if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) { + store_data(m, DATA_FILE); + last_save = cur_time; + } + usleep(40000); }