]> 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 c0dafd9ea2c230026d2b9c89167fe5cc87361a16..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);
 }
 
@@ -187,8 +190,7 @@ int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
                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);
+               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);
@@ -233,8 +235,8 @@ int blogview_sortfunc(const void *s1, const void *s2) {
 
 
 /*
- * We have to move the render code into this function because it needs to be sorted,
- * and possibly culled to a specific number of messages or date range...
+ * 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)
 {
@@ -244,18 +246,38 @@ int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
        void *Data;
        long len;
        struct blogpost *bp;
+       int i;
+       struct blogpost **blogposts = NULL;
+       int num_blogposts = 0;
+       int num_blogposts_alloc = 0;
 
        it = GetNewHashPos(BLOG, 0);
        while (GetNextHashPos(BLOG, it, &len, &Key, &Data)) {
                bp = (struct blogpost *) Data;
-               wc_printf("Top level ID is %d\n", bp->top_level_id);
-               if (bp->num_msgs > 0) {
-                       wc_printf("; top level msgnum is %ld", bp->msgs[0]);
+
+               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 *)));
                }
-               wc_printf("<br>\n");
+               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);
 }