Modified webpage: now tinymail repository is in gitorious.
[modest] / src / modest-datetime-formatter.c
index d20bf24..b04ec34 100644 (file)
 #include <modest-datetime-formatter.h>
 #ifdef MODEST_TOOLKIT_HILDON2
 #include <gconf/gconf-client.h>
 #include <modest-datetime-formatter.h>
 #ifdef MODEST_TOOLKIT_HILDON2
 #include <gconf/gconf-client.h>
-#include <glib/gi18n.h>
-#include <modest-text-utils.h>
 #include <gtk/gtkmarshal.h>
 #endif
 #include <gtk/gtkmarshal.h>
 #endif
+#include <glib/gi18n.h>
+#include "modest-text-utils.h"
+#ifdef MODEST_USE_LIBTIME
+#include <clockd/libtime.h>
+#include <libosso.h>
+#include <modest-platform.h>
+#endif
 
 typedef enum {
        DATETIME_FORMAT_12H,
 
 typedef enum {
        DATETIME_FORMAT_12H,
@@ -58,6 +63,14 @@ struct _ModestDatetimeFormatterPrivate {
 #endif
 };
 
 #endif
 };
 
+/* We need this very nasty stuff because the call to
+   osso_time_set_notification_cb cannot be reverted and thus the
+   handler will be called always even though we try to set another
+   one */
+#ifdef MODEST_USE_LIBTIME
+static ModestDatetimeFormatter *global_self = NULL;
+#endif
+
 #define MODEST_DATETIME_FORMATTER_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
                                                                                   MODEST_TYPE_DATETIME_FORMATTER, \
                                                                                   ModestDatetimeFormatterPrivate))
 #define MODEST_DATETIME_FORMATTER_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
                                                                                   MODEST_TYPE_DATETIME_FORMATTER, \
                                                                                   ModestDatetimeFormatterPrivate))
@@ -116,7 +129,7 @@ modest_datetime_formatter_class_init (ModestDatetimeFormatterClass *klass)
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET (ModestDatetimeFormatterClass, format_changed),
                              NULL, NULL,
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET (ModestDatetimeFormatterClass, format_changed),
                              NULL, NULL,
-                             gtk_marshal_VOID__VOID,
+                             g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
 }
 
                              G_TYPE_NONE, 0);
 }
 
@@ -156,6 +169,17 @@ clock_format_changed (GConfClient *gconf,
 }
 #endif
 
 }
 #endif
 
+#ifdef MODEST_USE_LIBTIME
+static void
+time_changed_cb (gpointer userdata)
+{
+       if (global_self) {
+               time_get_synced ();
+               g_signal_emit (global_self, signals[FORMAT_CHANGED_SIGNAL], 0);
+       }
+}
+#endif
+
 static void
 init_format (ModestDatetimeFormatter *obj)
 {
 static void
 init_format (ModestDatetimeFormatter *obj)
 {
@@ -181,17 +205,23 @@ init_format (ModestDatetimeFormatter *obj)
                g_warning ("Error listening to time format in gconf %s", err->message);
                g_error_free (err);
        }
                g_warning ("Error listening to time format in gconf %s", err->message);
                g_error_free (err);
        }
+
        update_format (obj);
 #endif
        update_format (obj);
 #endif
+
+#ifdef MODEST_USE_LIBTIME
+       if (OSSO_OK == osso_time_set_notification_cb (modest_platform_get_osso_context (),
+                                                     time_changed_cb,
+                                                     NULL))
+               global_self = obj;
+       time_get_synced ();
+#endif
+
 }
 
 static void
 modest_datetime_formatter_instance_init (ModestDatetimeFormatter *obj)
 {
 }
 
 static void
 modest_datetime_formatter_instance_init (ModestDatetimeFormatter *obj)
 {
-       ModestDatetimeFormatterPrivate *priv;
-
-       priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (obj);
-
        init_format (obj);
 }
 
        init_format (obj);
 }
 
@@ -209,6 +239,13 @@ modest_datetime_formatter_finalize   (GObject *obj)
        priv->gconf_handler = 0;
        gconf_client_remove_dir (gconf, HILDON2_GCONF_FORMAT_DIR,
                                 NULL);
        priv->gconf_handler = 0;
        gconf_client_remove_dir (gconf, HILDON2_GCONF_FORMAT_DIR,
                                 NULL);
+
+       /* Disconnect notification */
+#ifdef MODEST_USE_LIBTIME
+       if (OSSO_OK != osso_time_set_notification_cb (modest_platform_get_osso_context (),
+                                                     NULL, NULL))
+               global_self = NULL;
+#endif
 #endif
        G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 #endif
        G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
@@ -235,7 +272,7 @@ modest_datetime_formatter_format_date (ModestDatetimeFormatter *self,
        switch (priv->current_format) {
        case DATETIME_FORMAT_12H:
        case DATETIME_FORMAT_24H:
        switch (priv->current_format) {
        case DATETIME_FORMAT_12H:
        case DATETIME_FORMAT_24H:
-               format_string = _HL("wdgt_va_date");
+               format_string = _HL_DATE;
                break;
        case DATETIME_FORMAT_LOCALE:
                format_string = "%x";
                break;
        case DATETIME_FORMAT_LOCALE:
                format_string = "%x";
@@ -256,18 +293,25 @@ modest_datetime_formatter_format_time (ModestDatetimeFormatter *self,
        ModestDatetimeFormatterPrivate *priv;
        const gchar *format_string = NULL;
        gboolean is_pm;
        ModestDatetimeFormatterPrivate *priv;
        const gchar *format_string = NULL;
        gboolean is_pm;
+       struct tm localtime_tm = {0, };
 
        g_return_val_if_fail (MODEST_IS_DATETIME_FORMATTER (self), NULL);
        priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (self);
 
        g_return_val_if_fail (MODEST_IS_DATETIME_FORMATTER (self), NULL);
        priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (self);
-
-       is_pm = (date / (60 * 60 * 12)) % 2;
+#ifdef MODEST_USE_LIBTIME
+       time_get_local_ex (date, &localtime_tm);
+#else
+       time_t date_copy;
+       date_copy = date;
+       localtime_r (&date_copy, &localtime_tm);
+#endif
+       is_pm = (localtime_tm.tm_hour/12) % 2;
 
        switch (priv->current_format) {
        case DATETIME_FORMAT_12H:
 
        switch (priv->current_format) {
        case DATETIME_FORMAT_12H:
-               format_string = is_pm?_HL("wdgt_12h_time_pm"):_HL("wdgt_12h_time_am");
+               format_string = is_pm?_HL_12H_TIME_PM:_HL_12H_TIME_AM;
                break;
        case DATETIME_FORMAT_24H:
                break;
        case DATETIME_FORMAT_24H:
-               format_string = _HL("wdgt_va_24h_time");
+               format_string = _HL_24H_TIME;
                break;
        case DATETIME_FORMAT_LOCALE:
                format_string = "%X";
                break;
        case DATETIME_FORMAT_LOCALE:
                format_string = "%X";
@@ -279,14 +323,45 @@ modest_datetime_formatter_format_time (ModestDatetimeFormatter *self,
 }
 
 const gchar *
 }
 
 const gchar *
+modest_datetime_formatter_display_long_datetime (ModestDatetimeFormatter *self,
+                                                time_t date)
+{
+
+#define DATE_BUF_DOUBLE_SIZE 128 
+
+       static gchar date_buf[DATE_BUF_DOUBLE_SIZE];
+       
+       snprintf (date_buf, DATE_BUF_DOUBLE_SIZE, 
+                 "%s %s", modest_datetime_formatter_format_date (self, date), 
+                 modest_datetime_formatter_format_time (self, date));
+
+       return date_buf;
+}
+
+const gchar *
 modest_datetime_formatter_display_datetime (ModestDatetimeFormatter *self,
                                            time_t date)
 {
 
 modest_datetime_formatter_display_datetime (ModestDatetimeFormatter *self,
                                            time_t date)
 {
 
-       int day = time (NULL) / (24*60*60);
-       int date_day = date / (24*60*60);
+       struct tm today_localtime_tm = {0, };
+       struct tm date_localtime_tm = {0, };
+       time_t today;
+
+       today = time (NULL);
+#ifdef MODEST_USE_LIBTIME
+       time_get_local_ex (today, &today_localtime_tm);
+       time_get_local_ex (date, &date_localtime_tm);
+#else
+       time_t date_copy;
+       date_copy = today;
+       localtime_r (&date_copy, &today_localtime_tm);
+       date_copy = date;
+       localtime_r (&date_copy, &date_localtime_tm);
+#endif
 
 
-       if (day == date_day)
+       if (today_localtime_tm.tm_mday == date_localtime_tm.tm_mday &&
+           today_localtime_tm.tm_mon == date_localtime_tm.tm_mon &&
+           today_localtime_tm.tm_year == date_localtime_tm.tm_year)
                return modest_datetime_formatter_format_time (self, date);
        else
                return modest_datetime_formatter_format_date (self, date);
                return modest_datetime_formatter_format_time (self, date);
        else
                return modest_datetime_formatter_format_date (self, date);