File: BlogView.C

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (382 lines) | stat: -rw-r--r-- 10,375 bytes parent folder | download
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include "PostView.h"
#include "BlogView.h"

#include "../BlogSession.h"

#include "../model/Comment.h"
#include "../model/Post.h"
#include "../model/Tag.h"
#include "../model/User.h"

#include <Wt/WAnchor>
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WTemplate>
#include <Wt/WText>

#include <Wt/Dbo/backend/Sqlite3>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>

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

class BlogImpl : public WContainerWidget
{
public:
  BlogImpl(const std::string& basePath, const std::string& sqliteDb,
	   const std::string& rssFeedUrl)
    : basePath_(basePath),
      session_(sqliteDb),
      rssFeedUrl_(rssFeedUrl)
  {
    WApplication *app = wApp;

    app->messageResourceBundle().use("blog");
    app->useStyleSheet("css/blog.css");
    app->useStyleSheet("css/asciidoc.css");
    app->internalPathChanged().connect(SLOT(this, BlogImpl::handlePathChange));

    login_ = new WTemplate(this);
    register_ = 0;
    items_ = new WContainerWidget(this);

    logout();
  }

  ~BlogImpl() {
    clear();
  }

private:
  std::string basePath_, rssFeedUrl_;
  BlogSession session_;

  WTemplate *login_;
  WTemplate *register_;
  WContainerWidget *items_;

  void logout() {
    session_.setUser(dbo::ptr<User>());
    refresh();

    login_->clear();
    login_->setTemplateText(tr("blog-login"));

    WLineEdit *name = new WLineEdit();
    WLineEdit *passwd = new WLineEdit();
    WPushButton *loginButton = new WPushButton(tr("login"));

    passwd->setEchoMode(WLineEdit::Password);
    passwd->enterPressed().connect(SLOT(this, BlogImpl::login));
    loginButton->clicked().connect(SLOT(this, BlogImpl::login));

    name->hide();
    passwd->hide();
    loginButton->hide();

    WText *loginLink = new WText(tr("login"));
    loginLink->setStyleClass("link");

    loginLink->clicked().connect(SLOT(name, WWidget::show));
    loginLink->clicked().connect(SLOT(passwd, WWidget::show));
    loginLink->clicked().connect(SLOT(loginButton, WWidget::show));
    loginLink->clicked().connect(SLOT(loginLink, WWidget::hide));
    loginLink->clicked().connect(SLOT(name, WFormWidget::setFocus));

    WText *registerLink = new WText(tr("register"));
    registerLink->setStyleClass("link");

    registerLink->clicked().connect(SLOT(this, BlogImpl::newUser));

    login_->bindWidget("name", name);
    login_->bindWidget("passwd", passwd);
    login_->bindWidget("login-button", loginButton);
    login_->bindWidget("login-link", loginLink);
    login_->bindWidget("register-link", registerLink);
    login_->bindString("feed-url", rssFeedUrl_);
  }

  void login() {
    WLineEdit *name = login_->resolve<WLineEdit *>("name");
    WLineEdit *passwd = login_->resolve<WLineEdit *>("passwd");

    dbo::Transaction t(session_);

    dbo::ptr<User> user
      = session_.find<User>("where name = ?").bind(name->text());

    if (user) {
      if (user->authenticate(passwd->text().toUTF8())) {
	loginAs(user);
      } else {
	name->setStyleClass("");
	passwd->setStyleClass("invalid");
      }
    } else
      name->setStyleClass("invalid");

    t.commit();
  }

  void loginAs(dbo::ptr<User> user) {
    session_.setUser(user);

    if (user->role == User::Admin) {
      wApp->setInternalPath(basePath_ + "author/" + user->name, true);
    } else
      refresh();

    login_->clear();
    login_->setTemplateText(tr("blog-logout"));

    cancelRegister();

    WText *logoutLink = new WText(tr("logout"));
    logoutLink->setStyleClass("link");
    logoutLink->clicked().connect(SLOT(this, BlogImpl::logout));

    login_->bindString("feed-url", rssFeedUrl_);
    login_->bindString("user", user->name);
    login_->bindWidget("logout-link", logoutLink);
  }

  void newUser() {
    if (!register_) {
      register_ = new WTemplate();
      insertWidget(1, register_);
      register_->setTemplateText(tr("blog-register"));

      WLineEdit *name = new WLineEdit();
      WLineEdit *passwd = new WLineEdit();
      WLineEdit *passwd2 = new WLineEdit();
      WPushButton *okButton = new WPushButton(tr("register"));
      WPushButton *cancelButton = new WPushButton(tr("cancel"));
      WText *error = new WText();

      passwd->setEchoMode(WLineEdit::Password);
      passwd2->setEchoMode(WLineEdit::Password);

      okButton->clicked().connect(SLOT(this, BlogImpl::doRegister));
      cancelButton->clicked().connect(SLOT(this, BlogImpl::cancelRegister));

      register_->bindWidget("name", name);
      register_->bindWidget("passwd", passwd);
      register_->bindWidget("passwd2", passwd);
      register_->bindWidget("ok-button", okButton);
      register_->bindWidget("cancel-button", cancelButton);
      register_->bindWidget("error", error);
    }
  }

  void doRegister() {
    WLineEdit *name = register_->resolve<WLineEdit *>("name");
    WLineEdit *passwd = register_->resolve<WLineEdit *>("passwd");
    WLineEdit *passwd2 = register_->resolve<WLineEdit *>("passwd2");
    WText *error = register_->resolve<WText *>("error");

    if (passwd->text() != passwd2->text()) {
      error->setText(tr("passwd-mismatch"));
      return;
    }

    dbo::Transaction t(session_);

    dbo::ptr<User> user
      = session_.find<User>("where name = ?").bind(name->text());

    if (user) {
      error->setText(tr("user-exists").arg(name->text()));
      t.commit();
      return;
    } else {
      std::string n = name->text().toUTF8();
      boost::trim(n);
      if (n.length() < 4) {
	error->setText(tr("login-tooshort").arg(4));
	t.commit();
	return;
      }

      user = session_.add(new User());

      user.modify()->name = n;
      user.modify()->role = User::Visitor;
      user.modify()->setPassword(passwd->text().toUTF8());

      loginAs(user);
    }

    t.commit();
  }

  void cancelRegister() {
    delete register_;
    register_ = 0;
  }

  void refresh() {
    handlePathChange(wApp->internalPath());
  }

  void handlePathChange(const std::string& path) {
    WApplication *app = wApp;

    if (app->internalPathMatches(basePath_)) {
      dbo::Transaction t(session_);

      std::string path = app->internalPathNextPart(basePath_);

      items_->clear();

      if (path.empty())
	showPosts(session_.find<Post>
		  ("where state = ? "
		   "order by date desc "
		   "limit 10").bind(Post::Published), items_);

      else if (path == "author") {
	std::string author = app->internalPathNextPart(basePath_ + path + '/');
	dbo::ptr<User> user = findUser(author);

	if (user)
	  showPosts(user);
	else
	  showError(tr("blog-no-author").arg(author));
      } else {
	std::string remainder = app->internalPath().substr(basePath_.length());
	showPostsByDateTopic(remainder, items_);
      }

      t.commit();
    }
  }

  dbo::ptr<User> findUser(const std::string& name) {
    return session_.find<User>("where name = ?").bind(name);
  }

  void showPostsByDateTopic(const std::string& path,
			    WContainerWidget *parent) {
    std::vector<std::string> parts;
    boost::split(parts, path, boost::is_any_of("/"));

    WDate lower, upper;
    int year = boost::lexical_cast<int>(parts[0]);

    if (parts.size() > 1) {
      int month = boost::lexical_cast<int>(parts[1]);

      if (parts.size() > 2) {
	int day = boost::lexical_cast<int>(parts[2]);

	lower.setDate(year, month, day);
	upper = lower.addDays(1);
      } else {
	lower.setDate(year, month, 1);
	upper = lower.addMonths(1);
      }
    } else {
      lower.setDate(year, 1, 1);
      upper = lower.addYears(1);
    }

    Posts posts = session_.find<Post>
      ("where date >= ? "
       "and date < ? "
       "and (state = ? or author_id = ?)")
      .bind(WDateTime(lower))
      .bind(WDateTime(upper))
      .bind(Post::Published)
      .bind(session_.user().id());

    if (parts.size() > 3) {
      std::string title = parts[3];

      for (Posts::const_iterator i = posts.begin(); i != posts.end(); ++i)
	if ((*i)->titleToUrl() == title) {
	  showPost(*i, PostView::Detail, parent);
	  return;
	}

      showError(tr("blog-no-post"));
    } else {
      showPosts(posts, parent);
    }
  }

  void showPosts(dbo::ptr<User> user) {
    if (user == session_.user()) {
      WTemplate *authorPanel = new WTemplate(tr("blog-author-panel"), items_);

      WPushButton *newPost = new WPushButton(tr("new-post"));
      newPost->clicked().connect(SLOT(this, BlogImpl::newPost));

      WContainerWidget *unpublishedPosts = new WContainerWidget();
      showPosts(user->allPosts(Post::Unpublished), unpublishedPosts);

      authorPanel->bindString("user", user->name);
      authorPanel->bindInt("unpublished-count",
			   user->allPosts(Post::Unpublished).size());
      authorPanel->bindInt("published-count",
			   user->allPosts(Post::Published).size());
      authorPanel->bindWidget("new-post", newPost);
      authorPanel->bindWidget("unpublished-posts", unpublishedPosts);
    }

    showPosts(user->latestPosts(), items_);
  }

  void newPost() {
    dbo::Transaction t(session_);

    WTemplate *panel = dynamic_cast<WTemplate *>(items_->widget(0));
    WContainerWidget *unpublishedPosts
      = panel->resolve<WContainerWidget *>("unpublished-posts");

    dbo::ptr<Post> post(new Post);

    Post *p = post.modify();
    p->state = Post::Unpublished;
    p->author = session_.user();
    p->title = "Title";
    p->briefSrc = "Brief ...";
    p->bodySrc = "Body ...";

    showPost(post, PostView::Edit, unpublishedPosts);

    t.commit();
  }

  void showPosts(const Posts& posts, WContainerWidget *parent) {
    for (Posts::const_iterator i = posts.begin(); i != posts.end(); ++i)
      showPost(*i, PostView::Brief, parent);
  }

  void showPost(const dbo::ptr<Post> post, PostView::RenderType type,
		WContainerWidget *parent) {
    parent->addWidget(new PostView(session_, basePath_, post, type));
  }

  void showError(const WString& msg) {
    items_->addWidget(new WText(msg));
  }
};

BlogView::BlogView(const std::string& basePath, const std::string& sqliteDb,
		   const std::string& rssFeedUrl, WContainerWidget *parent)
  : WCompositeWidget(parent)
{
  impl_ = new BlogImpl(basePath, sqliteDb, rssFeedUrl);
  setImplementation(impl_);
}