Added support for category element, which is required
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 6 Jan 2009 23:09:51 +0000 (21:09 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 6 Jan 2009 23:18:46 +0000 (21:18 -0200)
The encoding and decoding of ISO8601 format is left to GLib. The public
interface is time_t, which, besides its lack of subsecond precision,
seems to be the most portable and standard for now. POSIX 2008 obsoletes
struct timeval and gettimeofday and struct timespec and clock_gettime
are too new to be used.

atom/entry.c
include/atompub/entry.h

index 31994c3..6189447 100644 (file)
 #include <glib.h>
 #include <libxml/tree.h>
 #include <libxml/parser.h>
+#include <time.h>
 
 struct _atom_entry
 {
   xmlDocPtr doc;
   AtomID *id;
   char *title;
+  time_t updated;
   GPtrArray *authors;
   GPtrArray *categories;
   char *summary;
 };
 
+static void atom_entry_updated_set_from_iso8601 (AtomEntry *, char *);
+
 void atom_entry_author_add (AtomEntry *, AtomPerson *);
 static void atom_entry_authors_delete (AtomEntry *);
 
@@ -47,6 +51,7 @@ atom_entry_new (char *id, char *title, AtomPerson *author)
   entry->doc = NULL;
   entry->id = atom_id_new (id);
   entry->title = g_strdup (title);
+  entry->updated = time (0);
   entry->authors = NULL;
   entry->categories = NULL;
   atom_entry_author_add (entry, author);
@@ -83,6 +88,8 @@ atom_entry_new_data_len (char *data, size_t len)
         entry->id = atom_id_new (content);
       else if (!xmlStrcmp (child->name, "title"))
         entry->title = g_strdup (content);
+      else if (!xmlStrcmp (child->name, "updated"))
+        atom_entry_updated_set_from_iso8601 (entry, content);
       else if (!xmlStrcmp (child->name, "summary"))
         entry->summary = g_strdup (content);
       else if (!xmlStrcmp (child->name, "author"))
@@ -92,7 +99,8 @@ atom_entry_new_data_len (char *data, size_t len)
       else
         xmlFree (content);
     }
-  if (entry->id == NULL || entry->title == NULL || entry->authors == NULL)
+  if (entry->id == NULL || entry->title == NULL ||
+      entry->updated == 0 || entry->authors == NULL)
     {
       atom_entry_delete (entry);
       return NULL;
@@ -150,6 +158,35 @@ atom_entry_title_set (AtomEntry *entry, char *title)
   entry->title = g_strdup (title);
 }
 
+time_t
+atom_entry_updated (AtomEntry *entry)
+{
+  return entry->updated;
+}
+
+void
+atom_entry_updated_set (AtomEntry *entry, time_t updated)
+{
+  entry->updated = updated;
+}
+
+static void
+atom_entry_updated_set_from_iso8601 (AtomEntry *entry, char *updated)
+{
+  GTimeVal gtv;
+  g_time_val_from_iso8601 (updated, &gtv);
+  entry->updated = gtv.tv_sec;
+}
+
+static char *
+atom_entry_updated_to_iso8601 (AtomEntry *entry)
+{
+  GTimeVal gtv;
+  gtv.tv_sec = entry->updated;
+  gtv.tv_usec = 0;
+  return g_time_val_to_iso8601 (&gtv);
+}
+
 void
 atom_entry_authors (AtomEntry *entry, AtomPerson *** authors, size_t *len)
 {
@@ -230,9 +267,11 @@ atom_entry_update_xmlnode (AtomEntry *entry)
   xmlNodePtr root;
   xmlNodePtr id;
   xmlNodePtr title;
+  xmlNodePtr updated;
   xmlNodePtr summary;
   xmlNodePtr author;
   xmlNodePtr cat;
+  char *updatedstr;
   int i;
   if (entry->doc == NULL)
     {
@@ -252,6 +291,7 @@ atom_entry_update_xmlnode (AtomEntry *entry)
           next = child->next;
           if (!xmlStrcmp (child->name, "id") ||
               !xmlStrcmp (child->name, "title") ||
+              !xmlStrcmp (child->name, "updated") ||
               !xmlStrcmp (child->name, "summary") ||
               !xmlStrcmp (child->name, "author") ||
               !xmlStrcmp (child->name, "category"))
@@ -264,6 +304,9 @@ atom_entry_update_xmlnode (AtomEntry *entry)
     }
   id = xmlNewTextChild (root, NULL, "id", atom_id_string (entry->id));
   title = xmlNewTextChild (root, NULL, "title", entry->title);
+  updatedstr = atom_entry_updated_to_iso8601 (entry);
+  updated = xmlNewTextChild (root, NULL, "updated", updatedstr);
+  g_free (updatedstr);
   if (entry->summary)
     summary = xmlNewTextChild (root, NULL, "summary", entry->summary);
   for (i = 0; i < entry->authors->len; i++)
index 4e0869e..af95156 100644 (file)
@@ -21,6 +21,7 @@
 #define ATOMPUB_ENTRY_H
 
 #include <sys/types.h>
+#include <time.h>
 #include <atompub/person.h>
 #include <atompub/category.h>
 #include <atompub/id.h>
@@ -34,6 +35,8 @@ AtomID * atom_entry_id (AtomEntry *);
 void atom_entry_id_set (AtomEntry *, AtomID *);
 char * atom_entry_title (AtomEntry *);
 void atom_entry_title_set (AtomEntry *, char *);
+time_t atom_entry_updated (AtomEntry *);
+void atom_entry_updated_set (AtomEntry *, time_t);
 void atom_entry_authors (AtomEntry *, AtomPerson ***, size_t *);
 void atom_entry_author_add (AtomEntry *, AtomPerson *);
 void atom_entry_categories (AtomEntry *, AtomCategory ***, size_t *);