[gnote] Replace custom TimeSpan with Glib::TimeSpan



commit 5ad391639aa001aaecd7779b6129767b1ec61c93
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Mon Jan 13 17:47:41 2020 +0200

    Replace custom TimeSpan with Glib::TimeSpan

 src/sharp/datetime.cpp                       |  10 +-
 src/sharp/datetime.hpp                       |   7 +-
 src/sharp/timespan.cpp                       | 174 ++++++++-------------------
 src/sharp/timespan.hpp                       |  59 ++-------
 src/synchronization/filesystemsyncserver.cpp |  14 +--
 src/synchronization/isyncmanager.cpp         |   6 +-
 src/synchronization/isyncmanager.hpp         |   4 +-
 src/synchronization/syncmanager.cpp          |  13 +-
 8 files changed, 85 insertions(+), 202 deletions(-)
---
diff --git a/src/sharp/datetime.cpp b/src/sharp/datetime.cpp
index 9e2449cb..a484c2f4 100644
--- a/src/sharp/datetime.cpp
+++ b/src/sharp/datetime.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012,2017 Aurimas Cernius
+ * Copyright (C) 2012,2017,2020 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -178,7 +178,7 @@ namespace sharp {
       && (m_date.tv_usec == dt.m_date.tv_usec);
   }
 
-  TimeSpan DateTime::operator-(const DateTime & dt) const
+  Glib::TimeSpan DateTime::operator-(const DateTime & dt) const
   {
     int secs = m_date.tv_sec - dt.m_date.tv_sec;
     int usecs = m_date.tv_usec - dt.m_date.tv_usec;
@@ -188,13 +188,13 @@ namespace sharp {
     mins %= 60;
     int days = hrs / 24;
     hrs %= 24;
-    return TimeSpan(days, hrs, mins, secs, usecs);
+    return sharp::time_span(days, hrs, mins, secs, usecs);
   }
 
-  DateTime DateTime::operator-(const TimeSpan & ts) const
+  DateTime DateTime::operator-(const Glib::TimeSpan & ts) const
   {
     Glib::TimeVal timeval(m_date);
-    timeval.add_milliseconds(ts.total_milliseconds());
+    timeval.add_milliseconds(time_span_total_milliseconds(ts));
     return DateTime(timeval);
   }
 
diff --git a/src/sharp/datetime.hpp b/src/sharp/datetime.hpp
index 45b51710..013ffa22 100644
--- a/src/sharp/datetime.hpp
+++ b/src/sharp/datetime.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012,2017 Aurimas Cernius
+ * Copyright (C) 2012,2017,2020 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -31,6 +31,7 @@
 
 #include <time.h>
 
+#include <glibmm/datetime.h>
 #include <glibmm/ustring.h>
 #include <glibmm/timeval.h>
 
@@ -87,8 +88,8 @@ public:
     {
       return (*this < dt) || (*this == dt);
     }
-  TimeSpan operator-(const DateTime & dt) const;
-  DateTime operator-(const TimeSpan & ts) const;
+  Glib::TimeSpan operator-(const DateTime & dt) const;
+  DateTime operator-(const Glib::TimeSpan & ts) const;
 
   glong sec() const
     {
diff --git a/src/sharp/timespan.cpp b/src/sharp/timespan.cpp
index 088373a5..33816106 100644
--- a/src/sharp/timespan.cpp
+++ b/src/sharp/timespan.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013,2017 Aurimas Cernius
+ * Copyright (C) 2012-2013,2017,2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,138 +25,62 @@
 
 namespace sharp {
 
-  TimeSpan::TimeSpan(int hrs, int mins, int secs)
-    : m_days(0)
-    , m_hours(hrs)
-    , m_minutes(mins)
-    , m_seconds(secs)
-    , m_usecs(0)
-  {}
-
-
-  TimeSpan::TimeSpan(int d, int hrs, int mins, int secs)
-    : m_days(d)
-    , m_hours(hrs)
-    , m_minutes(mins)
-    , m_seconds(secs)
-    , m_usecs(0)
-  {}
-
-
-  TimeSpan::TimeSpan(int d, int hrs, int mins, int secs, int usecs)
-    : m_days(d)
-    , m_hours(hrs)
-    , m_minutes(mins)
-    , m_seconds(secs)
-    , m_usecs(usecs)
-  {}
-
-
-  double TimeSpan::total_days() const
-  {
-    return m_days + _remaining_hours() / 24.0;
-  }
-
-
-  double TimeSpan::total_hours() const
-  {
-    return _total_hours() + _remaining_minutes() / 60.0;
-  }
-
-
-  double TimeSpan::total_minutes() const
-  {
-    return _total_minutes() + _remaining_seconds() / 60.0;
-  }
-
-
-  double TimeSpan::total_seconds() const
-  {
-    return _total_seconds() + m_usecs / 1000000.0;
-  }
-
-
-  double TimeSpan::total_milliseconds() const
-  {
-    return _total_seconds() * 60.0 + m_usecs / 1000.0;
-  }
-
-
-  Glib::ustring TimeSpan::string() const
-  {
-    return Glib::ustring::compose("%1:%2:%3:%4:%5", m_days, m_hours, m_minutes, m_seconds, m_usecs);
-  }
-
-
-  TimeSpan TimeSpan::operator-(const TimeSpan & ts)
-  {
-    double result = total_milliseconds() - ts.total_milliseconds();
-    int secs = int(result / 1000);
-    int usecs = int(result - (secs * 1000));
-    int mins = secs / 60;
-    secs %= 60;
-    int hrs = mins / 60;
-    mins %= 60;
-    int ds = hrs / 24;
-    hrs %= 24;
-    return TimeSpan(ds, hrs, mins, secs, usecs);
-  }
-
-
-  TimeSpan TimeSpan::parse(const Glib::ustring & s)
-  {
-    std::vector<Glib::ustring> tokens;
-    sharp::string_split(tokens, s, ":");
-    if(tokens.size() != 5) {
-      return TimeSpan(0, 0, 0, 0, 0);
-    }
-    int days = STRING_TO_INT(tokens[0]);
-    int hours = STRING_TO_INT(tokens[1]);
-    int mins = STRING_TO_INT(tokens[2]);
-    int secs = STRING_TO_INT(tokens[3]);
-    int usecs = STRING_TO_INT(tokens[4]);
-    Glib::ustring fmt = Glib::ustring::compose("%1:%2:%3:%4:%5", days, hours, mins, secs, usecs);
-    if(fmt != s) {
-      return TimeSpan(0, 0, 0, 0, 0);
-    }
-
-    return TimeSpan(days, hours, mins, secs, usecs);
-  }
-
-
-  int TimeSpan::_total_hours() const
-  {
-    return m_days * 24 + m_hours;
-  }
+Glib::TimeSpan time_span(int hrs, int mins, int secs)
+{
+  return hrs * G_TIME_SPAN_HOUR + mins * G_TIME_SPAN_MINUTE + secs * G_TIME_SPAN_SECOND;
+}
 
+Glib::TimeSpan time_span(int days, int hrs, int mins, int secs, int usecs)
+{
+  return days * G_TIME_SPAN_DAY + hrs * G_TIME_SPAN_HOUR + mins * G_TIME_SPAN_MINUTE + secs * 
G_TIME_SPAN_SECOND + usecs;
+}
 
-  int TimeSpan::_total_minutes() const
-  {
-    return _total_hours() * 60 + m_minutes;
+Glib::TimeSpan time_span_parse(const Glib::ustring & s)
+{
+  std::vector<Glib::ustring> tokens;
+  sharp::string_split(tokens, s, ":");
+  if(tokens.size() != 5) {
+    return time_span(0, 0, 0, 0, 0);
   }
-
-
-  int TimeSpan::_total_seconds() const
-  {
-    return _total_minutes() * 60 + m_seconds;
+  int days = STRING_TO_INT(tokens[0]);
+  int hours = STRING_TO_INT(tokens[1]);
+  int mins = STRING_TO_INT(tokens[2]);
+  int secs = STRING_TO_INT(tokens[3]);
+  int usecs = STRING_TO_INT(tokens[4]);
+  Glib::ustring fmt = Glib::ustring::compose("%1:%2:%3:%4:%5", days, hours, mins, secs, usecs);
+  if(fmt != s) {
+    return time_span(0, 0, 0, 0, 0);
   }
 
+  return time_span(days, hours, mins, secs, usecs);
+}
 
-  double TimeSpan::_remaining_hours() const
-  {
-    return m_hours + _remaining_minutes() / 60.0;
-  }
-
+double time_span_total_minutes(Glib::TimeSpan ts)
+{
+  return double(ts) / G_TIME_SPAN_MINUTE;
+}
 
-  double TimeSpan::_remaining_minutes() const
-  {
-    return m_minutes + _remaining_seconds() / 60.0;
-  }
+double time_span_total_seconds(Glib::TimeSpan ts)
+{
+  return double(ts) / G_TIME_SPAN_SECOND;
+}
 
+double time_span_total_milliseconds(Glib::TimeSpan ts)
+{
+  return double(ts) / G_TIME_SPAN_MILLISECOND;
+}
 
-  double TimeSpan::_remaining_seconds() const
-  {
-    return m_seconds + m_usecs / 1000000.0;
-  }
+Glib::ustring time_span_string(Glib::TimeSpan ts)
+{
+  unsigned days = ts / G_TIME_SPAN_DAY;
+  ts = ts % G_TIME_SPAN_DAY;
+  unsigned hours = ts / G_TIME_SPAN_HOUR;
+  ts = ts % G_TIME_SPAN_HOUR;
+  unsigned minutes = ts / G_TIME_SPAN_MINUTE;
+  ts = ts % G_TIME_SPAN_MINUTE;
+  unsigned seconds = ts / G_TIME_SPAN_SECOND;
+  unsigned usecs = ts % G_TIME_SPAN_SECOND;
+  return Glib::ustring::compose("%1:%2:%3:%4:%5", days, hours, minutes, seconds, usecs);
+}
 
 }
diff --git a/src/sharp/timespan.hpp b/src/sharp/timespan.hpp
index c31462d0..25865068 100644
--- a/src/sharp/timespan.hpp
+++ b/src/sharp/timespan.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012,2017 Aurimas Cernius
+ * Copyright (C) 2012,2017,2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,60 +23,19 @@
 #define _SHARP_TIMESPAN_HPP_
 
 
+#include <glibmm/datetime.h>
 #include <glibmm/ustring.h>
 
 
 namespace sharp {
 
-  class TimeSpan
-  {
-  public:
-    TimeSpan(int hrs, int mins, int secs);
-    TimeSpan(int days, int hrs, int mins, int secs);
-    TimeSpan(int days, int hrs, int mins, int secs, int usecs);
-    int days() const
-      {
-        return m_days;
-      }
-    int hours() const
-      {
-        return m_hours;
-      }
-    int minutes() const
-      {
-        return m_minutes;
-      }
-    int seconds() const
-      {
-        return m_seconds;
-      }
-    int microseconds() const
-      {
-        return m_usecs;
-      }
-    double total_days() const;
-    double total_hours() const;
-    double total_minutes() const;
-    double total_seconds() const;
-    double total_milliseconds() const;
-    Glib::ustring string() const;
-    TimeSpan operator-(const TimeSpan & ts);
-
-    static TimeSpan parse(const Glib::ustring & s);
-  private:
-    int _total_hours() const;
-    int _total_minutes() const;
-    int _total_seconds() const;
-    double _remaining_hours() const;
-    double _remaining_minutes() const;
-    double _remaining_seconds() const;
-
-    int m_days;
-    int m_hours;
-    int m_minutes;
-    int m_seconds;
-    int m_usecs;
-  };
+Glib::TimeSpan time_span(int hrs, int mins, int secs);
+Glib::TimeSpan time_span(int days, int hrs, int mins, int secs, int usecs);
+Glib::TimeSpan time_span_parse(const Glib::ustring & s);
+double time_span_total_minutes(Glib::TimeSpan ts);
+double time_span_total_seconds(Glib::TimeSpan ts);
+double time_span_total_milliseconds(Glib::TimeSpan ts);
+Glib::ustring time_span_string(Glib::TimeSpan ts);
 
 }
 
diff --git a/src/synchronization/filesystemsyncserver.cpp b/src/synchronization/filesystemsyncserver.cpp
index cf67e7ba..7f171576 100644
--- a/src/synchronization/filesystemsyncserver.cpp
+++ b/src/synchronization/filesystemsyncserver.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013,2017-2019 Aurimas Cernius
+ * Copyright (C) 2012-2013,2017-2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -195,7 +195,7 @@ bool FileSystemSyncServer::begin_sync_transaction()
   if(m_lock_path->query_exists()) {
     SyncLockInfo currentSyncLock = current_sync_lock();
     if(m_initial_sync_attempt == sharp::DateTime()) {
-      DBG_OUT("Sync: Discovered a sync lock file, wait at least %s before trying again.", 
currentSyncLock.duration.string().c_str());
+      DBG_OUT("Sync: Discovered a sync lock file, wait at least %s before trying again.", 
sharp::time_span_string(currentSyncLock.duration).c_str());
       // This is our initial attempt to sync and we've detected
       // a sync file, so we're gonna have to wait.
       m_initial_sync_attempt = sharp::DateTime::now();
@@ -203,7 +203,7 @@ bool FileSystemSyncServer::begin_sync_transaction()
       return false;
     }
     else if(m_last_sync_lock_hash != currentSyncLock.hash_string()) {
-      DBG_OUT("Sync: Updated sync lock file discovered, wait at least %s before trying again.", 
currentSyncLock.duration.string().c_str());
+      DBG_OUT("Sync: Updated sync lock file discovered, wait at least %s before trying again.", 
sharp::time_span_string(currentSyncLock.duration).c_str());
       // The sync lock has been updated and is still a valid lock
       m_initial_sync_attempt = sharp::DateTime::now();
       m_last_sync_lock_hash = currentSyncLock.hash_string();
@@ -237,7 +237,7 @@ bool FileSystemSyncServer::begin_sync_transaction()
   // TODO: Verify that the lockTimeout is actually working or figure
   // out some other way to automatically update the lock file.
   // Reset the timer to 20 seconds sooner than the sync lock duration
-  m_lock_timeout.reset(m_sync_lock.duration.total_milliseconds() - 20000);
+  m_lock_timeout.reset(sharp::time_span_total_milliseconds(m_sync_lock.duration) - 20000);
 
   m_updated_notes.clear();
   m_deleted_notes.clear();
@@ -486,7 +486,7 @@ SyncLockInfo FileSystemSyncServer::current_sync_lock()
     node = sharp::xml_node_xpath_find_single_node(root_node, "lock-expiration-duration/text ()");
     if(node != NULL) {
       Glib::ustring span_txt = sharp::xml_node_content(node);
-      syncLockInfo.duration = sharp::TimeSpan::parse(span_txt);
+      syncLockInfo.duration = sharp::time_span_parse(span_txt);
     }
 
     node = sharp::xml_node_xpath_find_single_node(root_node, "revision/text ()");
@@ -552,7 +552,7 @@ void FileSystemSyncServer::update_lock_file(const SyncLockInfo & syncLockInfo)
     xml.write_end_element();
 
     xml.write_start_element("", "lock-expiration-duration", "");
-    xml.write_string(syncLockInfo.duration.string());
+    xml.write_string(sharp::time_span_string(syncLockInfo.duration));
     xml.write_end_element();
 
     xml.write_start_element("", "revision", "");
@@ -648,7 +648,7 @@ void FileSystemSyncServer::lock_timeout()
   m_sync_lock.renew_count++;
   update_lock_file(m_sync_lock);
   // Reset the timer to 20 seconds sooner than the sync lock duration
-  m_lock_timeout.reset(m_sync_lock.duration.total_milliseconds() - 20000);
+  m_lock_timeout.reset(sharp::time_span_total_milliseconds(m_sync_lock.duration) - 20000);
 }
 
 
diff --git a/src/synchronization/isyncmanager.cpp b/src/synchronization/isyncmanager.cpp
index 2e88fcf8..df0fcf2d 100644
--- a/src/synchronization/isyncmanager.cpp
+++ b/src/synchronization/isyncmanager.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2013,2017,2019 Aurimas Cernius
+ * Copyright (C) 2012-2013,2017,2019-2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -32,14 +32,14 @@ SyncLockInfo::SyncLockInfo(const Glib::ustring & client)
   : client_id(client)
   , transaction_id(sharp::uuid().string())
   , renew_count(0)
-  , duration(0, 2, 0) // default of 2 minutes
+  , duration(sharp::time_span(0, 2, 0)) // default of 2 minutes
   , revision(0)
 {
 }
 
 Glib::ustring SyncLockInfo::hash_string()
 {
-  return Glib::ustring::compose("%1-%2-%3-%4-%5", transaction_id, client_id, renew_count, duration.string(), 
revision);
+  return Glib::ustring::compose("%1-%2-%3-%4-%5", transaction_id, client_id, renew_count, 
sharp::time_span_string(duration), revision);
 }
 
 
diff --git a/src/synchronization/isyncmanager.hpp b/src/synchronization/isyncmanager.hpp
index 09d8fd2a..373df76f 100644
--- a/src/synchronization/isyncmanager.hpp
+++ b/src/synchronization/isyncmanager.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2014,2017,2019 Aurimas Cernius
+ * Copyright (C) 2012-2014,2017,2019-2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,7 +34,7 @@ public:
   Glib::ustring client_id;
   Glib::ustring transaction_id;
   int renew_count;
-  sharp::TimeSpan duration;
+  Glib::TimeSpan duration;
   int revision;
 
   explicit SyncLockInfo(const Glib::ustring & client);
diff --git a/src/synchronization/syncmanager.cpp b/src/synchronization/syncmanager.cpp
index 37e60353..52c53be9 100644
--- a/src/synchronization/syncmanager.cpp
+++ b/src/synchronization/syncmanager.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2012-2014,2017,2019 Aurimas Cernius
+ * Copyright (C) 2012-2014,2017,2019-2020 Aurimas Cernius
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -395,8 +395,8 @@ namespace sync {
     // timer to avoid interupting the user (we want to
     // make sure not to sync more often than the user's pref)
     if(m_sync_thread == NULL) {
-      sharp::TimeSpan time_since_last_check = sharp::DateTime::now() - m_last_background_check;
-      if(time_since_last_check.total_minutes() > m_autosync_timeout_pref_minutes - 1) {
+      Glib::TimeSpan time_since_last_check = sharp::DateTime::now() - m_last_background_check;
+      if(sharp::time_span_total_minutes(time_since_last_check) > m_autosync_timeout_pref_minutes - 1) {
         DBG_OUT("Note edited...killing autosync timer until next save or delete event");
         m_autosync_timer.cancel();
       }
@@ -436,10 +436,9 @@ namespace sync {
   void SyncManager::handle_note_saved_or_deleted(const NoteBase::Ptr &)
   {
     if(m_sync_thread == NULL && m_autosync_timeout_pref_minutes > 0) {
-      sharp::TimeSpan time_since_last_check(sharp::DateTime::now() - m_last_background_check);
-      sharp::TimeSpan time_until_next_check(
-        sharp::TimeSpan(0, m_current_autosync_timeout_minutes, 0) - time_since_last_check);
-      if(time_until_next_check.total_minutes() < 1) {
+      Glib::TimeSpan time_since_last_check(sharp::DateTime::now() - m_last_background_check);
+      Glib::TimeSpan time_until_next_check = sharp::time_span(0, m_current_autosync_timeout_minutes, 0) - 
time_since_last_check;
+      if(sharp::time_span_total_minutes(time_until_next_check) < 1) {
         DBG_OUT("Note saved or deleted within a minute of next autosync...resetting sync timer");
         m_current_autosync_timeout_minutes = 1;
         m_autosync_timer.reset(m_current_autosync_timeout_minutes * 60000);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]