From 4d342d6fddf1ec52e188d1e4e3ff72056a6c6041 Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Fri, 11 Jul 2014 14:28:04 -0400
Subject: [PATCH] gnome: add years to timer and reuse for call history display

Refs #47119

Change-Id: I9a7f0e5f6633feec4e9322ad9f38a2c6039b012a
---
 gnome/src/callable_obj.c      | 37 +++++++++++++++++++++++++++++++----
 gnome/src/callable_obj.h      |  2 ++
 gnome/src/contacts/calltree.c | 28 +-------------------------
 3 files changed, 36 insertions(+), 31 deletions(-)

diff --git a/gnome/src/callable_obj.c b/gnome/src/callable_obj.c
index 7a1508fdb6..68f5512725 100644
--- a/gnome/src/callable_obj.c
+++ b/gnome/src/callable_obj.c
@@ -41,6 +41,7 @@
 #include "contacts/calltree.h"
 #include "dbus.h"
 #include <unistd.h>
+#include <stdint.h>
 
 gint get_state_callstruct(gconstpointer a, gconstpointer b)
 {
@@ -193,12 +194,40 @@ gchar* get_peer_info(const gchar* const number, const gchar* const name)
 
 gchar* get_call_duration(callable_obj_t *obj)
 {
-    long duration = difftime(obj->_time_stop, obj->_time_start);
+    char time_str[32];
+    format_duration(obj, obj->_time_stop, time_str, sizeof time_str);
 
-    if (duration < 0)
-        duration = 0;
+    return g_strdup_printf("<small>Duration:</small> %s", time_str);
+}
 
-    return g_strdup_printf("<small>Duration:</small> %.2ld:%.2ld" , duration/60 , duration%60);
+void
+format_duration(callable_obj_t *obj, time_t end, char *timestr, size_t timestr_sz)
+{
+    const gdouble diff = difftime(end, obj->_time_start);
+    guint32 seconds = CLAMP(diff, 0.0f, UINT32_MAX);
+
+    enum {HOURS_PER_DAY = 24, DAYS_PER_YEAR = 365, SECONDS_PER_HOUR = 3600,
+          SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY,
+          SECONDS_PER_YEAR = DAYS_PER_YEAR * SECONDS_PER_DAY};
+
+    const guint32 years =  seconds / SECONDS_PER_YEAR;
+    const guint32 days =  (seconds / SECONDS_PER_DAY) % DAYS_PER_YEAR;
+    const guint32 hours = (seconds / SECONDS_PER_HOUR) % HOURS_PER_DAY;
+    const guint32 minutes = (seconds / 60) % 60;
+    seconds %= 60;
+
+    if (years)
+        g_snprintf(timestr, timestr_sz, _("%uy %ud %02uh %02umn %02us"),
+                years, days, hours, minutes, seconds);
+    else if (days)
+        g_snprintf(timestr, timestr_sz, _("%ud %02uh %02umn %02us"),
+                days, hours, minutes, seconds);
+    else if (hours)
+        g_snprintf(timestr, timestr_sz, "%u:%02u:%02u",
+                hours, minutes, seconds);
+    else
+        g_snprintf(timestr, timestr_sz, "%02u:%02u",
+                minutes, seconds);
 }
 
 static
diff --git a/gnome/src/callable_obj.h b/gnome/src/callable_obj.h
index 16c5474ee1..f2891ef9f0 100644
--- a/gnome/src/callable_obj.h
+++ b/gnome/src/callable_obj.h
@@ -197,6 +197,8 @@ gchar* get_call_duration(callable_obj_t *obj);
 
 gchar* get_formatted_start_timestamp(time_t);
 
+void format_duration(callable_obj_t *obj, time_t end, char *timestr, size_t timestr_sz);
+
 gboolean call_was_outgoing(callable_obj_t * obj);
 
 void restore_call(const gchar *id);
diff --git a/gnome/src/contacts/calltree.c b/gnome/src/contacts/calltree.c
index e332c07213..b93b15c96c 100644
--- a/gnome/src/contacts/calltree.c
+++ b/gnome/src/contacts/calltree.c
@@ -1170,29 +1170,6 @@ void calltree_display(calltab_t *tab, SFLPhoneClient *client)
     update_actions(client);
 }
 
-static void
-format_duration(guint32 seconds, char *timestr, size_t timestr_sz)
-{
-    enum {HOURS_PER_DAY = 24, SECONDS_PER_HOUR = 3600,
-          SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY};
-
-    const guint32 days =  seconds / SECONDS_PER_DAY;
-    const guint32 hours = (seconds / SECONDS_PER_HOUR) % HOURS_PER_DAY;
-    const guint32 minutes = (seconds / 60) % 60;
-    seconds %= 60;
-
-    if (days)
-        g_snprintf(timestr, timestr_sz, _("%ud %02uh %02umn %02us"),
-                   days, hours, minutes, seconds);
-    else
-        if (hours)
-            g_snprintf(timestr, timestr_sz, "%u:%02u:%02u",
-                   hours, minutes, seconds);
-        else
-            g_snprintf(timestr, timestr_sz, "%02u:%02u",
-                   minutes, seconds);
-}
-
 gboolean calltree_update_clock(G_GNUC_UNUSED gpointer data)
 {
     if (calllist_empty(current_calls_tab))
@@ -1200,7 +1177,6 @@ gboolean calltree_update_clock(G_GNUC_UNUSED gpointer data)
 
     char timestr[32];
     const gchar *msg = "";
-    double duration;
     callable_obj_t *call = calltab_get_selected_call(current_calls_tab);
 
     if (call) {
@@ -1213,9 +1189,7 @@ gboolean calltree_update_clock(G_GNUC_UNUSED gpointer data)
             case CALL_STATE_BUSY:
                 break;
             default:
-                duration = difftime(time(NULL), call->_time_start);
-                format_duration(CLAMP(duration, 0.0f, UINT32_MAX), timestr,
-                                sizeof(timestr));
+                format_duration(call, time(NULL), timestr, sizeof(timestr));
                 msg = timestr;
                 break;
         }
-- 
GitLab