File: PostView.C

package info (click to toggle)
witty 3.3.3%2Bdfsg-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,228 kB
  • ctags: 26,694
  • sloc: cpp: 147,809; ansic: 77,999; xml: 16,331; sh: 1,303; makefile: 198; java: 86; sql: 14
file content (263 lines) | stat: -rw-r--r-- 6,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include "CommentView.h"
#include "PostView.h"
#include "../asciidoc/asciidoc.h"

#include "../model/BlogSession.h"
#include "../model/Comment.h"
#include "../model/Tag.h"
#include "../model/Token.h"
#include "../model/User.h"

#include <Wt/WAnchor>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WTextArea>

using namespace Wt;
namespace dbo = Wt::Dbo;

PostView::PostView(BlogSession& session, const std::string& basePath,
		   dbo::ptr<Post> post, RenderType type)
  : session_(session),
    basePath_(basePath),
    post_(post)
{
  viewType_ = Brief;
  render(type);
}

void PostView::renderTemplate(std::ostream& result)
{
  dbo::Transaction t(session_);

  WTemplate::renderTemplate(result);

  post_.purge();

  t.commit();
}

void PostView::resolveString(const std::string& varName,
			     const std::vector<WString>& args,
			     std::ostream& result)
{
  if (varName == "title")
    format(result, post_->title);
  else if (varName == "date")
    format(result, post_->date.toString("dddd, MMMM d, yyyy @ HH:mm"));
  else if (varName == "brief") {
    if (!post_->briefSrc.empty())
      format(result, post_->briefHtml, XHTMLText);
    else
      format(result, post_->bodyHtml, XHTMLText);
  } else if (varName == "brief+body") {
    format(result, "<div>" + post_->briefHtml + "</div>"
	   "<div id=\"" + basePath_ + post_->permaLink() + "/more\">"
	   "<div>" + post_->bodyHtml + "</div></div>", XHTMLUnsafeText);
  } else
    WTemplate::resolveString(varName, args, result);
}

void PostView::render(RenderType type)
{
  if (type != Edit)
    viewType_ = type;

  clear();

  switch (type) {
  case Detail: {
    setTemplateText(tr("blog-post"));

    commentCount_ = new WText(post_->commentCount());

    CommentView *comments = new CommentView(session_, post_->rootComment());
    session_.commentsChanged().connect(this, &PostView::updateCommentCount);

    bindWidget("comment-count", commentCount_);
    bindWidget("comments", comments);
    bindString("anchor", basePath_ + post_->permaLink());

    break;
  }
  case Brief: {
    setTemplateText(tr("blog-post-brief"));

    WAnchor *titleAnchor
      = new WAnchor(WLink(WLink::InternalPath, basePath_ + post_->permaLink()),
		    post_->title);
    bindWidget("title", titleAnchor);

    if (!post_->briefSrc.empty()) {
      WAnchor *moreAnchor 
	= new WAnchor(WLink(WLink::InternalPath,
			    basePath_ + post_->permaLink() + "/more"),
		      tr("blog-read-more"));
      bindWidget("read-more", moreAnchor);
    } else {
      bindString("read-more", WString::Empty);
    }

    commentCount_ = new WText("(" + post_->commentCount() + ")");

    WAnchor *commentsAnchor
      = new WAnchor(WLink(WLink::InternalPath,
			  basePath_ + post_->permaLink() + "/comments"));
    commentsAnchor->addWidget(commentCount_);
    bindWidget("comment-count", commentsAnchor);

    break; }
  case Edit: {
    setTemplateText(tr("blog-post-edit"));

    bindWidget("title-edit", titleEdit_ = new WLineEdit(post_->title));
    bindWidget("brief-edit", briefEdit_ = new WTextArea(post_->briefSrc));
    bindWidget("body-edit", bodyEdit_ = new WTextArea(post_->bodySrc));

    WPushButton *saveButton = new WPushButton(tr("save"));
    WPushButton *cancelButton = new WPushButton(tr("cancel"));
    bindWidget("save", saveButton);
    bindWidget("cancel", cancelButton);

    saveButton->clicked().connect(this, &PostView::saveEdit);
    cancelButton->clicked().connect(this, &PostView::showView);

    break; }
  }

  if (type == Detail || type == Brief) {
    if (session_.user() == post_->author) {
      WPushButton *publishButton;
      if (post_->state != Post::Published) {
	publishButton = new WPushButton(tr("publish"));
	publishButton->clicked().connect(this, &PostView::publish);
      } else {
	publishButton = new WPushButton(tr("retract"));
	publishButton->clicked().connect(this, &PostView::retract);
      }
      bindWidget("publish", publishButton);

      WPushButton *editButton = new WPushButton(tr("edit"));
      editButton->clicked().connect(this, &PostView::showEdit);
      bindWidget("edit", editButton);

      WPushButton *deleteButton = new WPushButton(tr("delete"));
      deleteButton->clicked().connect(this, &PostView::rm);
      bindWidget("delete", deleteButton);
    } else {
      bindString("publish", WString::Empty);
      bindString("edit", WString::Empty);
      bindString("delete", WString::Empty);
    }
  }

  WAnchor *postAnchor
    = new WAnchor(WLink(WLink::InternalPath,
			basePath_ + "author/" + post_->author->name.toUTF8()),
		  post_->author->name);
  bindWidget("author", postAnchor);
}

void PostView::saveEdit()
{
  dbo::Transaction t(session_);

  bool newPost = post_.id() == -1;

  Post *post = post_.modify();

  post->title = titleEdit_->text();
  post->briefSrc = briefEdit_->text();
  post->bodySrc = bodyEdit_->text();

  post->briefHtml = asciidoc(post->briefSrc);
  post->bodyHtml = asciidoc(post->bodySrc);

  if (newPost) {
    session_.add(post_);

    post->date = Wt::WDateTime::currentDateTime();
    post->state = Post::Unpublished;
    post->author = session_.user();

    dbo::ptr<Comment> rootComment = session_.add(new Comment);
    rootComment.modify()->post = post_;
  }

  session_.flush();

  render(viewType_);

  t.commit();
}

void PostView::showView()
{
  if (post_.id() == -1)
    delete this;
  else {
    dbo::Transaction t(session_);
    render(viewType_);
    t.commit();
  }
}

void PostView::publish()
{
  setState(Post::Published);
}

void PostView::retract()
{
  setState(Post::Unpublished);
}

void PostView::setState(Post::State state)
{
  dbo::Transaction t(session_);

  post_.modify()->state = state;
  if (state == Post::Published)
    post_.modify()->date = Wt::WDateTime::currentDateTime();

  render(viewType_);

  t.commit();
}

void PostView::showEdit()
{
  dbo::Transaction t(session_);

  render(Edit);

  t.commit();
}

void PostView::rm()
{
  dbo::Transaction t(session_);
  post_.remove();
  t.commit();

  delete this;
}

void PostView::updateCommentCount(dbo::ptr<Comment> comment)
{
  if (comment->post == post_) {
    std::string count = comment->post->commentCount();

    if (commentCount_->text().toUTF8()[0] == '(')
      commentCount_->setText("(" + count + ")");
    else
      commentCount_->setText(count);
  }
}