From 80d746f15ae9e223b02a7240c46612d9e371716f Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 6 Jan 2009 21:09:51 -0200 Subject: [PATCH] Added support for category element, which is required 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 | 45 ++++++++++++++++++++++++++++++++++++++++- include/atompub/entry.h | 3 +++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/atom/entry.c b/atom/entry.c index 31994c3..6189447 100644 --- a/atom/entry.c +++ b/atom/entry.c @@ -22,17 +22,21 @@ #include #include #include +#include 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, >v); + 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 (>v); +} + 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++) diff --git a/include/atompub/entry.h b/include/atompub/entry.h index 4e0869e..af95156 100644 --- a/include/atompub/entry.h +++ b/include/atompub/entry.h @@ -21,6 +21,7 @@ #define ATOMPUB_ENTRY_H #include +#include #include #include #include @@ -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 *); -- 2.20.1