about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-09-29 23:30:23 +0100
committerJames Booth <boothj5@gmail.com>2015-09-29 23:30:23 +0100
commit99fc70bd9283280bdb6056634c759196f549a4c6 (patch)
treecf08ac6c09eed76ff1350efa3c739268667de73e
parentac1164a3fa6f4c388399af06612e16eade16e46b (diff)
downloadprofani-tty-99fc70bd9283280bdb6056634c759196f549a4c6.tar.gz
Added last activity time format option
-rw-r--r--src/command/command.c21
-rw-r--r--src/command/commands.c19
-rw-r--r--src/config/preferences.c5
-rw-r--r--src/config/preferences.h1
-rw-r--r--src/config/theme.c1
-rw-r--r--src/event/server_events.c36
-rw-r--r--src/ui/console.c4
-rw-r--r--src/ui/window.c21
-rw-r--r--themes/boothj51
9 files changed, 81 insertions, 28 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 603b0f12..2203b398 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -950,7 +950,8 @@ static struct cmd_t command_defs[] =
             "/time main set <format>",
             "/time main off",
             "/time statusbar set <format>",
-            "/time statusbar off")
+            "/time statusbar off",
+            "/time lastactivity set <format>")
         CMD_DESC(
             "Configure time display preferences. "
             "Time formats are strings supported by g_date_time_format. "
@@ -958,14 +959,16 @@ static struct cmd_t command_defs[] =
             "Setting the format to an unsupported string, will display the string. "
             "If the format contains spaces, it must be surrounded with double quotes.")
         CMD_ARGS(
-            { "main set <format>", "Change time format in main window." },
-            { "main off", "Do not show time in main window." },
-            { "statusbar set <format>", "Change time format in statusbar." },
-            { "statusbar off", "Change time format in status bar." })
+            { "main set <format>",         "Change time format in main window." },
+            { "main off",                  "Do not show time in main window." },
+            { "statusbar set <format>",    "Change time format in statusbar." },
+            { "statusbar off",             "Do not show time in status bar." },
+            { "lastactivity set <format>", "Change time format for last activity." })
         CMD_EXAMPLES(
             "/time main set \"%d-%m-%y %H:%M\"",
             "/time main off",
-            "/time statusbar set %H:%M")
+            "/time statusbar set %H:%M",
+            "/time lastactivity set \"%d-%m-%y %H:%M\"")
     },
 
     { "/inpblock",
@@ -2098,6 +2101,7 @@ cmd_init(void)
     time_ac = autocomplete_new();
     autocomplete_add(time_ac, "main");
     autocomplete_add(time_ac, "statusbar");
+    autocomplete_add(time_ac, "lastactivity");
 
     time_format_ac = autocomplete_new();
     autocomplete_add(time_format_ac, "set");
@@ -3367,6 +3371,11 @@ _time_autocomplete(ProfWin *window, const char * const input)
         return found;
     }
 
+    found = autocomplete_param_with_ac(input, "/time lastactivity", time_format_ac, TRUE);
+    if (found) {
+        return found;
+    }
+
     found = autocomplete_param_with_ac(input, "/time main", time_format_ac, TRUE);
     if (found) {
         return found;
diff --git a/src/command/commands.c b/src/command/commands.c
index 6b301057..2e4332b2 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -3569,7 +3569,24 @@ cmd_wrap(ProfWin *window, const char * const command, gchar **args)
 gboolean
 cmd_time(ProfWin *window, const char * const command, gchar **args)
 {
-    if (g_strcmp0(args[0], "statusbar") == 0) {
+    if (g_strcmp0(args[0], "lastactivity") == 0) {
+        if (args[1] == NULL) {
+            cons_show("Current last activity time format is '%s'.", prefs_get_string(PREF_TIME_LASTACTIVITY));
+            return TRUE;
+        } else if (g_strcmp0(args[1], "set") == 0 && args[2] != NULL) {
+            prefs_set_string(PREF_TIME_LASTACTIVITY, args[2]);
+            cons_show("Last activity time format set to '%s'.", args[2]);
+            ui_redraw();
+            return TRUE;
+        } else if (g_strcmp0(args[1], "off") == 0) {
+            cons_show("Last activity time cannot be disabled.");
+            ui_redraw();
+            return TRUE;
+        } else {
+            cons_bad_cmd_usage(command);
+            return TRUE;
+        }
+    } else if (g_strcmp0(args[0], "statusbar") == 0) {
         if (args[1] == NULL) {
             cons_show("Current status bar time format is '%s'.", prefs_get_string(PREF_TIME_STATUSBAR));
             return TRUE;
diff --git a/src/config/preferences.c b/src/config/preferences.c
index e594e9f6..c53c09bc 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -602,6 +602,7 @@ _get_group(preference_t pref)
         case PREF_WINS_AUTO_TIDY:
         case PREF_TIME:
         case PREF_TIME_STATUSBAR:
+        case PREF_TIME_LASTACTIVITY:
         case PREF_ROSTER:
         case PREF_ROSTER_OFFLINE:
         case PREF_ROSTER_RESOURCE:
@@ -756,6 +757,8 @@ _get_key(preference_t pref)
             return "time";
         case PREF_TIME_STATUSBAR:
             return "time.statusbar";
+        case PREF_TIME_LASTACTIVITY:
+            return "time.lastactivity";
         case PREF_ROSTER:
             return "roster";
         case PREF_ROSTER_OFFLINE:
@@ -845,6 +848,8 @@ _get_default_string(preference_t pref)
             return "%H:%M:%S";
         case PREF_TIME_STATUSBAR:
             return "%H:%M";
+        case PREF_TIME_LASTACTIVITY:
+            return "%d-%m-%y %H:%M:%S";
         case PREF_PGP_LOG:
             return "redact";
         default:
diff --git a/src/config/preferences.h b/src/config/preferences.h
index 1879c7d3..875db253 100644
--- a/src/config/preferences.h
+++ b/src/config/preferences.h
@@ -72,6 +72,7 @@ typedef enum {
     PREF_WINS_AUTO_TIDY,
     PREF_TIME,
     PREF_TIME_STATUSBAR,
+    PREF_TIME_LASTACTIVITY,
     PREF_STATUSES,
     PREF_STATUSES_CONSOLE,
     PREF_STATUSES_CHAT,
diff --git a/src/config/theme.c b/src/config/theme.c
index c4b25c73..588a3a42 100644
--- a/src/config/theme.c
+++ b/src/config/theme.c
@@ -434,6 +434,7 @@ _load_preferences(void)
     _set_boolean_preference("wins.autotidy", PREF_WINS_AUTO_TIDY);
     _set_string_preference("time", PREF_TIME);
     _set_string_preference("time.statusbar", PREF_TIME_STATUSBAR);
+    _set_string_preference("time.lastactivity", PREF_TIME_LASTACTIVITY);
 
     _set_boolean_preference("resource.title", PREF_RESOURCE_TITLE);
     _set_boolean_preference("resource.message", PREF_RESOURCE_MESSAGE);
diff --git a/src/event/server_events.c b/src/event/server_events.c
index 74e5cf75..91e1ca7b 100644
--- a/src/event/server_events.c
+++ b/src/event/server_events.c
@@ -34,6 +34,7 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <assert.h>
 
 #include "config.h"
 
@@ -740,19 +741,28 @@ sv_ev_lastactivity_response(const char * const from, const int seconds, const ch
         return;
     }
 
+    GDateTime *now = g_date_time_new_now_local();
+    GDateTime *active = g_date_time_add_seconds(now, 0 - seconds);
+
+    gchar *date_fmt = NULL;
+    char *time_pref = prefs_get_string(PREF_TIME_LASTACTIVITY);
+    date_fmt = g_date_time_format(active, time_pref);
+    prefs_free_string(time_pref);
+    assert(date_fmt != NULL);
+
     // full jid - last activity
     if (jidp->resourcepart) {
         if (seconds == 0) {
             if (msg) {
                 cons_show("%s currently active, status: %s", from, msg);
             } else {
-                cons_show("%s currently active.", from);
+                cons_show("%s currently active", from);
             }
         } else {
             if (msg) {
-                cons_show("%s last active %d seconds ago, status: %s", from, seconds, msg);
+                cons_show("%s last active %s, status: %s", from, date_fmt, msg);
             } else {
-                cons_show("%s last active %d seconds ago.", from, seconds);
+                cons_show("%s last active %s", from, date_fmt);
             }
         }
 
@@ -762,20 +772,32 @@ sv_ev_lastactivity_response(const char * const from, const int seconds, const ch
             if (msg) {
                 cons_show("%s currently logged in, status: %s", from, msg);
             } else {
-                cons_show("%s currently loggrd in.", from);
+                cons_show("%s currently loggrd in", from);
             }
         } else {
             if (msg) {
-                cons_show("%s last logged in %d seconds ago, status: %s", from, seconds, msg);
+                cons_show("%s last logged in %s, status: %s", from, date_fmt, msg);
             } else {
-                cons_show("%s last logged in %d seconds ago.", from, seconds);
+                cons_show("%s last logged in %s", from, date_fmt);
             }
         }
 
     // domain only - uptime
     } else {
-        cons_show("%s uptime %d seconds", from, seconds);
+        int left = seconds;
+        int days = seconds / 86400;
+        left = left - days * 86400;
+        int hours = left / 3600;
+        left = left - hours * 3600;
+        int minutes = left / 60;
+        left = left - minutes * 60;
+        int seconds = left;
+
+        cons_show("%s up since %s, uptime %d days, %d hrs, %d mins, %d secs", from, date_fmt, days, hours, minutes, seconds);
     }
 
+    g_date_time_unref(now);
+    g_date_time_unref(active);
+    g_free(date_fmt);
     jid_destroy(jidp);
 }
diff --git a/src/ui/console.c b/src/ui/console.c
index 666f7015..1bbaac38 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -1010,6 +1010,10 @@ cons_time_setting(void)
     else
         cons_show("Time statusbar (/time)        : %s", pref_time_statusbar);
     prefs_free_string(pref_time_statusbar);
+
+    char *pref_time_lastactivity = prefs_get_string(PREF_TIME_LASTACTIVITY);
+    cons_show("Time last activity (/time)    : %s", pref_time_lastactivity);
+    prefs_free_string(pref_time_lastactivity);
 }
 
 void
diff --git a/src/ui/window.c b/src/ui/window.c
index a78b0e15..5a2196f2 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -852,22 +852,15 @@ win_show_status_string(ProfWin *window, const char * const from,
         win_vprint(window, '-', 0, NULL, NO_DATE | NO_EOL, presence_colour, "", " is %s", default_show);
 
     if (last_activity) {
-        GDateTime *now = g_date_time_new_now_local();
-        GTimeSpan span = g_date_time_difference(now, last_activity);
-        g_date_time_unref(now);
+        gchar *date_fmt = NULL;
+        char *time_pref = prefs_get_string(PREF_TIME_LASTACTIVITY);
+        date_fmt = g_date_time_format(last_activity, time_pref);
+        prefs_free_string(time_pref);
+        assert(date_fmt != NULL);
 
-        int hours = span / G_TIME_SPAN_HOUR;
-        span = span - hours * G_TIME_SPAN_HOUR;
-        int minutes = span / G_TIME_SPAN_MINUTE;
-        span = span - minutes * G_TIME_SPAN_MINUTE;
-        int seconds = span / G_TIME_SPAN_SECOND;
+        win_vprint(window, '-', 0, NULL, NO_DATE | NO_EOL, presence_colour, "", ", last activity: %s", date_fmt);
 
-        if (hours > 0) {
-          win_vprint(window, '-', 0, NULL, NO_DATE | NO_EOL, presence_colour, "", ", idle %dh%dm%ds", hours, minutes, seconds);
-        }
-        else {
-          win_vprint(window, '-', 0, NULL, NO_DATE | NO_EOL, presence_colour, "", ", idle %dm%ds", minutes, seconds);
-        }
+        g_free(date_fmt);
     }
 
     if (status)
diff --git a/themes/boothj5 b/themes/boothj5
index a408d3b5..dfbb0192 100644
--- a/themes/boothj5
+++ b/themes/boothj5
@@ -56,6 +56,7 @@ splash=true
 wrap=true
 time=%d-%m-%y %H:%M
 time.statusbar=%H:%M:%S
+time.lastactivity=%d/%m/%y %H:%M:%S
 privileges=true
 presence=true
 intype=true