[gnote] Respect gnome clock-format setting



commit 71e94ebbcf1aa97bc3fec9811bf5c9042b770b9a
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Wed Jul 10 23:50:13 2013 +0300

    Respect gnome clock-format setting
    
    Fixes Bug 700432.

 src/preferences.cpp       |    1 +
 src/preferences.hpp       |    1 +
 src/searchnoteswidget.cpp |    5 +++++
 src/test/dttest.cpp       |   34 ++++++++++++++++++++++++++++++++++
 src/utils.cpp             |   36 +++++++++++++++++++++++++++++-------
 src/utils.hpp             |    1 +
 6 files changed, 71 insertions(+), 7 deletions(-)
---
diff --git a/src/preferences.cpp b/src/preferences.cpp
index c40366b..67c97b3 100644
--- a/src/preferences.cpp
+++ b/src/preferences.cpp
@@ -73,6 +73,7 @@ namespace gnote {
   const char * Preferences::SYNC_FUSE_WDFS_URL = "url";
   const char * Preferences::SYNC_FUSE_WDFS_USERNAME = "username";
 
+  const char * Preferences::DESKTOP_GNOME_CLOCK_FORMAT = "clock-format";
   const char * Preferences::DESKTOP_GNOME_FONT = "document-font-name";
   const char * Preferences::DESKTOP_GNOME_KEY_THEME = "gtk-key-theme";
 
diff --git a/src/preferences.hpp b/src/preferences.hpp
index d2e9b89..a1abb2b 100644
--- a/src/preferences.hpp
+++ b/src/preferences.hpp
@@ -83,6 +83,7 @@ namespace gnote {
     static const char *SYNC_FUSE_WDFS_URL;
     static const char *SYNC_FUSE_WDFS_USERNAME;
 
+    static const char *DESKTOP_GNOME_CLOCK_FORMAT;
     static const char *DESKTOP_GNOME_FONT;
     static const char *DESKTOP_GNOME_KEY_THEME;
 
diff --git a/src/searchnoteswidget.cpp b/src/searchnoteswidget.cpp
index b28b463..d871977 100644
--- a/src/searchnoteswidget.cpp
+++ b/src/searchnoteswidget.cpp
@@ -106,6 +106,8 @@ SearchNotesWidget::SearchNotesWidget(NoteManager & m)
   Glib::RefPtr<Gio::Settings> settings = Preferences::obj().get_schema_settings(Preferences::SCHEMA_GNOTE);
   settings->signal_changed().connect(sigc::mem_fun(*this, &SearchNotesWidget::on_settings_changed));
   parse_sorting_setting(settings->get_string(Preferences::SEARCH_SORTING));
+  settings = Preferences::obj().get_schema_settings(Preferences::SCHEMA_DESKTOP_GNOME_INTERFACE);
+  settings->signal_changed().connect(sigc::mem_fun(*this, &SearchNotesWidget::on_settings_changed));
 }
 
 SearchNotesWidget::~SearchNotesWidget()
@@ -1427,6 +1429,9 @@ void SearchNotesWidget::on_settings_changed(const Glib::ustring & key)
       m_note_list_context_menu = NULL;
     }
   }
+  else if(key == Preferences::DESKTOP_GNOME_CLOCK_FORMAT) {
+    update_results();
+  }
 }
 
 void SearchNotesWidget::on_sorting_changed()
diff --git a/src/test/dttest.cpp b/src/test/dttest.cpp
index f204b36..b7c47ce 100644
--- a/src/test/dttest.cpp
+++ b/src/test/dttest.cpp
@@ -23,9 +23,15 @@
 #include <boost/test/minimal.hpp>
 
 #include "sharp/xmlconvert.cpp"
+#include "utils.hpp"
 
 
 
+bool string_ends_with(const std::string & s, const std::string & other)
+{
+  return (s.length() - s.rfind(other)) == other.length();
+}
+
 int test_main(int /*argc*/, char ** /*argv*/)
 {
   std::string format = "%Y-%m-%dT%T  7f@%z";
@@ -48,6 +54,34 @@ int test_main(int /*argc*/, char ** /*argv*/)
   date_string = sharp::XmlConvert::to_string(d3);
   BOOST_CHECK(date_string == "2009-03-24T07:34:35.000000Z");
 
+  sharp::DateTime d4 = sharp::DateTime::now();
+  date_string = gnote::utils::get_pretty_print_date(d4, false, false);
+  BOOST_CHECK(date_string == "Today");
+
+  d4.add_days(1);
+  date_string = gnote::utils::get_pretty_print_date(d4, false, false);
+  BOOST_CHECK(date_string == "Tomorrow");
+
+  d4.add_days(2);
+  date_string = gnote::utils::get_pretty_print_date(d4, false, false);
+  BOOST_CHECK(date_string == "In 3 days");
+
+  sharp::DateTime d5 = sharp::DateTime::now();
+  d5.add_days(-1);
+  date_string = gnote::utils::get_pretty_print_date(d5, false, false);
+  BOOST_CHECK(date_string == "Yesterday");
+
+  d5.add_days(-3);
+  date_string = gnote::utils::get_pretty_print_date(d5, false, false);
+  BOOST_CHECK(date_string == "4 days ago");
+
+  sharp::DateTime d6 = sharp::DateTime::from_iso8601("2009-03-24T13:34:35.2914680-04:00");
+  date_string = gnote::utils::get_pretty_print_date(d6, true, false);
+  BOOST_CHECK(string_ends_with(date_string, "19:34"));
+
+  date_string = gnote::utils::get_pretty_print_date(d6, true, true);
+  BOOST_CHECK(string_ends_with(date_string, "7:34 PM"));
+
   return 0;
 }
 
diff --git a/src/utils.cpp b/src/utils.cpp
index 76b39ff..b31d1fc 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -46,6 +46,7 @@
 #include "sharp/string.hpp"
 #include "sharp/uri.hpp"
 #include "sharp/datetime.hpp"
+#include "preferences.hpp"
 #include "note.hpp"
 #include "utils.hpp"
 #include "debug.hpp"
@@ -185,9 +186,24 @@ namespace gnote {
 
     std::string get_pretty_print_date(const sharp::DateTime & date, bool show_time)
     {
+      bool use_12h = false;
+      if(show_time) {
+        use_12h = Preferences::obj().get_schema_settings(
+          Preferences::SCHEMA_DESKTOP_GNOME_INTERFACE)->get_string(
+            Preferences::DESKTOP_GNOME_CLOCK_FORMAT) == "12h";
+      }
+      return get_pretty_print_date(date, show_time, use_12h);
+    }
+
+    std::string get_pretty_print_date(const sharp::DateTime & date, bool show_time, bool use_12h)
+    {
       std::string pretty_str;
       sharp::DateTime now = sharp::DateTime::now();
-      std::string short_time = date.to_short_time_string ();
+      std::string short_time = use_12h
+        /* TRANSLATORS: time in 12h format. */
+        ? date.to_string("%I:%M %p")
+        /* TRANSLATORS: time in 24h format. */
+        : date.to_string("%H:%M");
 
       if (date.year() == now.year()) {
         if (date.day_of_year() == now.day_of_year()) {
@@ -245,18 +261,24 @@ namespace gnote {
           }
         }
         else {
-          pretty_str = show_time ?
-            date.to_string ("%B %d, %H:%M %p") : // "MMMM d, h:mm tt"
-            date.to_string ("%B %d");            // "MMMM d"
+          /* TRANSLATORS: date in current year. */
+          pretty_str = date.to_string(_("%B %d")); // "MMMM d"
+          if(show_time) {
+            /* TRANSLATORS: first argument is date, second is time. */
+            pretty_str = str(boost::format(_("%1%, %2%")) % pretty_str % short_time);
+          }
         }
       } 
       else if (!date.is_valid()) {
         pretty_str = _("No Date");
       }
       else {
-        pretty_str = show_time ?
-          date.to_string ("%B %d %Y, %H:%M %p") : // "MMMM d yyyy, h:mm tt"
-          date.to_string ("%B %d %Y");            // "MMMM d yyyy"
+        /* TRANSLATORS: date in other than current year. */
+        pretty_str = date.to_string(_("%B %d %Y")); // "MMMM d yyyy"
+        if(show_time) {
+          /* TRANSLATORS: first argument is date, second is time. */
+          pretty_str = str(boost::format(_("%1%, %2%")) % pretty_str % short_time);
+        }
       }
 
       return pretty_str;
diff --git a/src/utils.hpp b/src/utils.hpp
index 3f4a253..216d9a5 100644
--- a/src/utils.hpp
+++ b/src/utils.hpp
@@ -61,6 +61,7 @@ namespace gnote {
                                      const std::string & url, 
                                      const std::string & error);
     std::string get_pretty_print_date(const sharp::DateTime &, bool show_time);
+    std::string get_pretty_print_date(const sharp::DateTime &, bool show_time, bool use_12h);
 
     void main_context_invoke(const sigc::slot<void> & slot);
     void main_context_call(const sigc::slot<void> & slot);


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