r1690 - trunk/src/target/OM-2007/applications/openmoko-rssreader/src

zecke at sita.openmoko.org zecke at sita.openmoko.org
Sat Apr 7 18:27:14 CEST 2007


Author: zecke
Date: 2007-04-07 18:27:13 +0200 (Sat, 07 Apr 2007)
New Revision: 1690

Modified:
   trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c
   trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.h
Log:
openmoko-rssreader: Add a simple untested RFC822 scanner to be used by the TreeModel


Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c	2007-04-07 12:52:43 UTC (rev 1689)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c	2007-04-07 16:27:13 UTC (rev 1690)
@@ -26,8 +26,10 @@
  */
 
 #include "rfcdate.h"
+#include <glib/gi18n.h>
 
 #include <malloc.h>
+#include <stdio.h>
 
 G_DEFINE_TYPE(RSSRFCDate, rss_rfc_date, G_TYPE_OBJECT)
 
@@ -41,9 +43,19 @@
 static void
 rss_rfc_date_finalize (GObject *object)
 {
-    if ( RSS_RFC_DATE_GET_PRIVATE(object)->string_cache )
-        free (RSS_RFC_DATE_GET_PRIVATE(object)->string_cache);
+    RSSRFCDatePrivate *private = RSS_RFC_DATE_GET_PRIVATE (object);
+    RSSRFCDate *self = RSS_RFC_DATE (object);
 
+    if ( private->string_cache ) {
+        free (private->string_cache);
+        private->string_cache = NULL;
+    }
+
+    if ( self->date ) {
+        g_date_free (self->date);
+        self->date = NULL;
+    }
+
     G_OBJECT_CLASS(rss_rfc_date_parent_class)->finalize (object);
 }
 
@@ -58,11 +70,77 @@
     G_OBJECT_CLASS(klass)->finalize = rss_rfc_date_finalize;
 }
 
+/*
+ * options
+ *  a) use strcmp
+ *  b) swicth/case
+ */
+static GDateMonth
+rss_month_number( gchar *month_str )
+{
+    switch (month_str[0]) {
+    case 'J':
+        switch (month_str[1]) {
+        case 'a':
+            return G_DATE_JANUARY;
+            break;
+        case 'u':
+            switch (month_str[2] ) {
+            case 'n':
+                return G_DATE_JUNE;
+                break;
+            case 'l':
+                return G_DATE_JULY;
+                break;
+            }
+        }
+        break;
+    case 'F':
+        return G_DATE_FEBRUARY;
+        break;
+    case 'M':
+        switch ( month_str[2] ) {
+        case 'r':
+            return G_DATE_MARCH;
+            break;
+        case 'y':
+            return G_DATE_MAY;
+            break;
+        }
+        break;
+    case 'A':
+        switch (month_str[1]){
+        case 'p':
+            return G_DATE_APRIL;
+            break;
+        case 'u':
+            return G_DATE_AUGUST;
+            break;
+        }
+        break;
+    case 'O':
+        return G_DATE_OCTOBER;
+        break;
+    case 'S':
+        return G_DATE_SEPTEMBER;
+        break;
+    case 'N':
+        return G_DATE_NOVEMBER;
+        break;
+    case 'D':
+        return G_DATE_DECEMBER;
+        break;
+    }
+
+    return G_DATE_BAD_MONTH;
+}
+
 static void
 rss_rfc_date_init(RSSRFCDate *self)
 {
     /* I don't know if memset gets called */
     RSS_RFC_DATE_GET_PRIVATE(self)->string_cache = NULL;
+    self->date = g_date_new ();
 }
 
 GObject*
@@ -71,30 +149,101 @@
     return G_OBJECT(g_object_new(RSS_TYPE_RFC_DATE, NULL));
 }
 
+/**
+ * Clear the internal string representation. This can be used
+ * on the day switch
+ */
 void
-rss_rfc_date_set (RSSRFCDate *self, const gchar* rfc822date)
+rss_rfc_date_clear_cache (RSSRFCDate* self)
 {
-    if ( RSS_RFC_DATE_GET_PRIVATE(self)->string_cache ) {
-        free ( RSS_RFC_DATE_GET_PRIVATE(self)->string_cache );
-        RSS_RFC_DATE_GET_PRIVATE(self)->string_cache = NULL;
+    RSSRFCDatePrivate *private = RSS_RFC_DATE_GET_PRIVATE (self);
+
+    if ( private->string_cache ) {
+        free ( private->string_cache );
+        private->string_cache = NULL;
     }
+}
 
+void
+rss_rfc_date_set (RSSRFCDate *self, const gchar* rfc822date)
+{
+    rss_rfc_date_clear_cache (self);
 
+
     /*
-     * XXX parse the date
+     * %a, %d %b %Y %H:%M:%S %z
+     *
+     * We try to parse this date representation. We can ignore
+     * %a but for the %b we need to look it up properly
      */
+    int day, year, hour, minute, second;
+    gchar month_name[4];
+    sscanf (rfc822date, "%*3s, %d %3s %d %d:%d:%d", &day, month_name, &year, &hour, &minute, &second );
+
+    self->timeval.tv_sec  = hour*60*60 + minute*60 + second;
+    self->timeval.tv_usec = 0;
+    g_date_set_dmy ( self->date, day, rss_month_number (month_name), year);
 }
 
+/*
+ * Start by comparing the dates and only if they are equal compare
+ * the times.
+ */
 gint
 rss_rfc_date_compare (RSSRFCDate *left, RSSRFCDate *right)
 {
-    /* XXX, FIXME do the comparsion */
-    return 0;
+    int date_result = g_date_compare( left->date, right->date );
+    if (  date_result != 0 )
+        return date_result;
+
+    return left->timeval.tv_sec - right->timeval.tv_sec;
 }
 
 gchar*
 rss_rfc_date_as_string (RSSRFCDate *self)
 {
-    /* XXX, FIXME */
-    return "";
+    RSSRFCDatePrivate *private = RSS_RFC_DATE_GET_PRIVATE(self);
+    if ( private->string_cache )
+        return private->string_cache;
+
+    /*
+     * format the date now
+     */
+    GString *date_string;
+    GTimeVal now;
+    g_get_current_time (&now);
+    GDate *date = g_date_new ();
+    g_date_set_time_val (date, &now);
+
+    if ( g_date_compare( date, self->date ) == 0 ) {
+        date_string = g_string_new (_("Today"));
+        goto exit;
+    }
+
+    g_date_subtract_days( date, 1 );
+    if ( g_date_compare( date, self->date ) == 0 ) {
+        date_string = g_string_new (_("Yesterday"));
+        goto exit;
+    }
+
+    /*
+     * copy the date using the current locale. And retry
+     * until the buffer is big enough
+     */
+    date_string = g_string_sized_new( 10 );
+    while ( g_date_strftime( date_string->str, date_string->allocated_len-1, "%a, %d %b %Y", self->date ) == 0 ) {
+        g_string_set_size( date_string, date_string->allocated_len + 10 );
+    }
+
+exit:
+    /*
+     * append the time
+     */
+    g_string_append_printf ( date_string, ", %ld:%ld:%ld",
+                             self->timeval.tv_sec/60/60,
+                             self->timeval.tv_sec/60%60,
+                             self->timeval.tv_sec%60);
+    g_date_free (date);
+    private->string_cache = g_string_free (date_string, FALSE);
+    return private->string_cache;
 }

Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.h
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.h	2007-04-07 12:52:43 UTC (rev 1689)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.h	2007-04-07 16:27:13 UTC (rev 1690)
@@ -2,6 +2,7 @@
 #ifndef OPENMOKO_RSS_RFC_DATE_H
 #define OPENMOKO_RSS_RFC_DATE_H
 
+#include <glib.h>
 #include <glib-object.h>
 
 G_BEGIN_DECLS
@@ -18,7 +19,9 @@
 
 struct _RSSRFCDate {
     GObject parent;
-    gint64  date;
+
+    GDate  *date;
+    GTimeVal timeval;
 };
 
 struct _RSSRFCDateClass {
@@ -30,6 +33,7 @@
 void      rss_rfc_date_set      (RSSRFCDate* self, const gchar* rfc822_date);
 gint      rss_rfc_date_compare  (RSSRFCDate* self, RSSRFCDate *other);
 gchar*    rss_rfc_date_as_string(RSSRFCDate* self);
+void      rss_rfc_date_clear_cache (RSSRFCDate* self);
 
 
 G_END_DECLS





More information about the commitlog mailing list