]> code.citadel.org Git - citadel.git/blobdiff - webcit/blogview_renderer.c
Load pointers to blogposts into an array that we can sort
[citadel.git] / webcit / blogview_renderer.c
index 9c66093c8ac2631a16bdd1d4e00b789c4c063729..d125e2ccd7dee523f86f0eb5f612b92aaadc2a1c 100644 (file)
@@ -44,11 +44,9 @@ void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
 
 
 /*
- * Destructor for 'struct blogpost' which does the rendering first.
- * By rendering from here, we eliminate the need for a separate iterator, although
- * we might run into trouble when we get around to displaying newest-to-oldest...
+ * Render (maybe) a single blog post and (maybe) its comments
  */
-void blogpost_render_and_destroy(struct blogpost *bp) {
+void blogpost_render(struct blogpost *bp) {
        const StrBuf *Mime;
        int p = 0;
        int i;
@@ -87,16 +85,21 @@ void blogpost_render_and_destroy(struct blogpost *bp) {
                }
        }
 
-
-       if (bp->alloc_msgs > 0) {
-               free(bp->msgs);
-       }
-
        /* offer the comment box */
        if (p == bp->top_level_id) {
                do_template("blog_comment_box");
        }
 
+}
+
+
+/*
+ * Destructor for "struct blogpost"
+ */
+void blogpost_destroy(struct blogpost *bp) {
+       if (bp->alloc_msgs > 0) {
+               free(bp->msgs);
+       }
        free(bp);
 }
 
@@ -186,9 +189,8 @@ int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
                bp = malloc(sizeof(struct blogpost));
                if (!bp) return(200);
                memset(bp, 0, sizeof (struct blogpost));
-               bp->top_level_id = b.id;
-               Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
-                                       (DeleteHashDataFunc)blogpost_render_and_destroy);
+               bp->top_level_id = b.id;
+               Put(BLOG, (const char *)&b.id, sizeof(b.id), bp, (DeleteHashDataFunc)blogpost_destroy);
        }
        else {
                GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
@@ -220,7 +222,6 @@ int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
 
 /*
  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
- */
 int blogview_sortfunc(const void *s1, const void *s2) {
        long *l1 = (long *)(s1);
        long *l2 = (long *)(s2);
@@ -229,16 +230,53 @@ int blogview_sortfunc(const void *s1, const void *s2) {
        if (*l1 < *l2) return(+1);
        return(0);
 }
+ */
+
 
 
+/*
+ * All blogpost entries are now in the hash list.
+ * Sort them, (FIXME cull by date range if needed,) and render what we want to see.
+ */
 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
 {
-       /* HashList *BLOG = (HashList *) *ViewSpecific; */
+       HashList *BLOG = (HashList *) *ViewSpecific;
+       HashPos *it;
+       const char *Key;
+       void *Data;
+       long len;
+       struct blogpost *bp;
+       int i;
+       struct blogpost **blogposts = NULL;
+       int num_blogposts = 0;
+       int num_blogposts_alloc = 0;
 
-       /*
-        * No code needed here -- we render during disposition.
-        * Maybe this is the location where we want to handle pretty permalinks.
-        */
+       it = GetNewHashPos(BLOG, 0);
+       while (GetNextHashPos(BLOG, it, &len, &Key, &Data)) {
+               bp = (struct blogpost *) Data;
+
+               if (num_blogposts >= num_blogposts_alloc) {
+                       if (num_blogposts_alloc == 0) {
+                               num_blogposts_alloc = 100;
+                       }
+                       else {
+                               num_blogposts_alloc *= 2;
+                       }
+                       blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
+               }
+               blogposts[num_blogposts++] = bp;
+       }
+
+       DeleteHashPos(&it);
+
+       if (num_blogposts > 0) {
+               for (i=0; i<num_blogposts; ++i) {
+                       wc_printf("Top level ID is %d", blogposts[i]->top_level_id);
+                       wc_printf("; top level msgnum is %ld", blogposts[i]->msgs[0]);
+                       blogpost_render(blogposts[i]);
+               }
+               free(blogposts);
+       }
 
        return(0);
 }