Wt examples  3.2.1
Home.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 
7 #include <fstream>
8 #include <iostream>
9 
10 #include <boost/lexical_cast.hpp>
11 #include <boost/tokenizer.hpp>
12 #include <boost/algorithm/string.hpp>
13 
14 #include <Wt/WAnchor>
15 #include <Wt/WApplication>
16 #include <Wt/WEnvironment>
17 #include <Wt/WLogger>
18 #include <Wt/WMenu>
19 #include <Wt/WPushButton>
20 #include <Wt/WStackedWidget>
21 #include <Wt/WTabWidget>
22 #include <Wt/WTable>
23 #include <Wt/WTableCell>
24 #include <Wt/WTemplate>
25 #include <Wt/WText>
26 #include <Wt/WViewWidget>
27 #include <Wt/WVBoxLayout>
28 
29 #include "Home.h"
30 #include "view/BlogView.h"
31 
32 static const std::string SRC_INTERNAL_PATH = "src";
33 
35 {
36 }
37 
38 Home::Home(const WEnvironment& env, const std::string& title,
39  const std::string& resourceBundle, const std::string& cssPath)
40  : WApplication(env),
41  releases_(0),
42  homePage_(0),
43  sourceViewer_(0)
44 {
45  messageResourceBundle().use(appRoot() + resourceBundle, false);
46 
47  useStyleSheet(cssPath + "/wt.css");
48  useStyleSheet(cssPath + "/wt_ie.css", "lt IE 7");
49  useStyleSheet("css/home.css");
50  useStyleSheet("css/sourceview.css");
51  setTitle(title);
52 
53  setLocale("");
54  language_ = 0;
55 }
56 
57 void Home::init()
58 {
59  internalPathChanged().connect(this, &Home::setup);
62 
63  setup();
64 
66 }
67 
69 {
70  /*
71  * This function switches between the two major components of the homepage,
72  * depending on the internal path:
73  * /src -> source viewer
74  * /... -> homepage
75  *
76  * FIXME: we should take into account language /cn/src ...
77  */
78  std::string base = internalPathNextPart("/");
79 
80  if (base == SRC_INTERNAL_PATH) {
81  if (!sourceViewer_) {
82  delete homePage_;
83  homePage_ = 0;
84 
85  root()->clear();
86 
88  WVBoxLayout *layout = new WVBoxLayout();
89  layout->setContentsMargins(0, 0, 0, 0);
90  layout->addWidget(sourceViewer_);
91  root()->setLayout(layout);
92  }
93  } else {
94  if (!homePage_) {
95  delete sourceViewer_;
96  sourceViewer_ = 0;
97 
98  root()->clear();
99 
100  createHome();
102 
104  }
105  }
106 }
107 
109 {
110  WTemplate *result = new WTemplate(tr("template"), root());
111  homePage_ = result;
112 
113  WContainerWidget *languagesDiv = new WContainerWidget();
114  languagesDiv->setId("top_languages");
115 
116  for (unsigned i = 0; i < languages.size(); ++i) {
117  if (i != 0)
118  new WText("- ", languagesDiv);
119 
120  const Lang& l = languages[i];
121 
122  new WAnchor(WLink(WLink::InternalPath, l.path_),
123  WString::fromUTF8(l.longDescription_), languagesDiv);
124  }
125 
126  WStackedWidget *contents = new WStackedWidget();
127  WAnimation fade(WAnimation::Fade, WAnimation::Linear, 250);
128  contents->setTransitionAnimation(fade);
129  contents->setId("main_page");
130 
131  mainMenu_ = new WMenu(contents, Vertical);
132  mainMenu_->setRenderAsList(true);
133 
135  (tr("introduction"), introduction())->setPathComponent("");
136 
138  (tr("blog"), deferCreate(boost::bind(&Home::blog, this)));
139 
141  (tr("features"), wrapView(&Home::features), WMenuItem::PreLoading);
142 
144  (tr("documentation"), wrapView(&Home::documentation),
145  WMenuItem::PreLoading);
146 
148  (tr("examples"), examples(),
149  WMenuItem::PreLoading)->setPathComponent("examples/");
150 
152  (tr("download"), deferCreate(boost::bind(&Home::download, this)),
153  WMenuItem::PreLoading);
154 
156  (tr("community"), wrapView(&Home::community), WMenuItem::PreLoading);
157 
159  (tr("other-language"), wrapView(&Home::otherLanguage),
160  WMenuItem::PreLoading);
161 
163 
165 
166  // Make the menu be internal-path aware.
168 
170 
171  result->bindWidget("languages", languagesDiv);
172  result->bindWidget("menu", mainMenu_);
173  result->bindWidget("contents", contents);
174  result->bindWidget("sidebar", sideBarContent_);
175 }
176 
177 void Home::setLanguage(int index)
178 {
179  if (homePage_) {
180  const Lang& l = languages[index];
181 
182  setLocale(l.code_);
183 
184  std::string langPath = l.path_;
185  mainMenu_->setInternalBasePath(langPath);
186  examplesMenu_->setInternalBasePath(langPath + "examples");
187  BlogView *blog = dynamic_cast<BlogView *>(findWidget("blog"));
188  if (blog)
189  blog->setInternalBasePath(langPath + "blog/");
190  updateTitle();
191 
192  language_ = index;
193  }
194 }
195 
196 WWidget *Home::linkSourceBrowser(const std::string& example)
197 {
198  /*
199  * Instead of using a WAnchor, which will not progress properly because
200  * it is wrapped with wrapView() (-- should we not fix that?), we use
201  * a WText which contains an anchor, and enable internal path encoding.
202  */
203  std::string path = "#/" + SRC_INTERNAL_PATH + "/" + example;
204  WText *a = new WText(tr("source-browser-link").arg(path));
205  a->setInternalPathEncoding(true);
206  return a;
207 }
208 
210 {
211  std::string langPath = internalPathNextPart("/");
212 
213  if (langPath.empty())
214  langPath = '/';
215  else
216  langPath = '/' + langPath + '/';
217 
218  int newLanguage = 0;
219 
220  for (unsigned i = 0; i < languages.size(); ++i) {
221  if (languages[i].path_ == langPath) {
222  newLanguage = i;
223  break;
224  }
225  }
226 
227  if (newLanguage != language_)
228  setLanguage(newLanguage);
229 }
230 
232 {
233  if (mainMenu_->currentItem()) {
234  setTitle(tr("wt") + " - " + mainMenu_->currentItem()->text());
235  }
236 }
237 
238 void Home::logInternalPath(const std::string& path)
239 {
240  // simulate an access log for the interal paths
241  log("path") << path;
242 
243  // If this goes to /src, we need to invoke google analytics method too
244  if (path.size() >= 4 && path.substr(0, 4) == "/src") {
246  }
247 }
248 
250 {
251  return new WText(tr("home.intro"));
252 }
253 
255 {
256  const Lang& l = languages[language_];
257  std::string langPath = l.path_;
258  BlogView *blog = new BlogView(langPath + "blog/",
259  appRoot() + "blog.db", "/wt/blog/feed/");
260  blog->setObjectName("blog");
261 
262  if (!blog->user().empty())
263  chatSetUser(blog->user());
264 
265  blog->userChanged().connect(this, &Home::chatSetUser);
266 
267  return blog;
268 }
269 
270 void Home::chatSetUser(const WString& userName)
271 {
273  ("if (window.chat) "
274  "try {"
275  """window.chat.emit(window.chat, 'login', "
276  "" "" + userName.jsStringLiteral() + "); "
277  "} catch (e) {"
278  """window.chatUser = " + userName.jsStringLiteral() + ";"
279  "}"
280  "else "
281  """window.chatUser = " + userName.jsStringLiteral() + ";");
282 }
283 
285 {
286  return new WText(tr("home.status"));
287 }
288 
290 {
291  return new WText(tr("home.features"));
292 }
293 
295 {
296  WText *result = new WText(tr("home.documentation"));
297  result->setInternalPathEncoding(true);
298  return result;
299 }
300 
302 {
303  return new WText(tr("home.other-language"));
304 }
305 
307 {
308  return makeStaticModel(boost::bind(createWidget, this));
309 }
310 
311 std::string Home::href(const std::string& url, const std::string& description)
312 {
313  return "<a href=\"" + url + "\" target=\"_blank\">" + description + "</a>";
314 }
315 
317 {
318  return new WText(tr("home.community"));
319 }
320 
321 void Home::readReleases(WTable *releaseTable)
322 {
323  std::ifstream f((filePrefix() + "releases.txt").c_str());
324 
325  releaseTable->clear();
326 
327  releaseTable->elementAt(0, 0)
328  ->addWidget(new WText(tr("home.download.version")));
329  releaseTable->elementAt(0, 1)
330  ->addWidget(new WText(tr("home.download.date")));
331  releaseTable->elementAt(0, 2)
332  ->addWidget(new WText(tr("home.download.description")));
333 
334  releaseTable->elementAt(0, 0)->resize(WLength(15, WLength::FontEx),
335  WLength::Auto);
336  releaseTable->elementAt(0, 1)->resize(WLength(15, WLength::FontEx),
337  WLength::Auto);
338 
339  int row = 1;
340 
341  while (f) {
342  std::string line;
343  getline(f, line);
344 
345  if (f) {
346  typedef boost::tokenizer<boost::escaped_list_separator<char> >
347  CsvTokenizer;
348  CsvTokenizer tok(line);
349 
350  CsvTokenizer::iterator i=tok.begin();
351 
352  std::string fileName = *i;
353  std::string description = *(++i);
354  releaseTable->elementAt(row, 0)->addWidget
355  (new WText(href("http://prdownloads.sourceforge.net/witty/"
356  + fileName + "?download", description)));
357  releaseTable->elementAt(row, 1)->addWidget(new WText(*(++i)));
358  releaseTable->elementAt(row, 2)->addWidget(new WText(*(++i)));
359 
360  ++row;
361  }
362  }
363 }
364 
365 #ifdef WT_EMWEB_BUILD
367 {
368  WContainerWidget *result = new WContainerWidget();
369  result->setStyleClass("quote");
370 
371  WTemplate *requestTemplate = new WTemplate(tr("quote.request"), result);
372 
373  WPushButton *quoteButton = new WPushButton(tr("quote.requestbutton"));
374  requestTemplate->bindWidget("button", quoteButton);
375 
377  result->addWidget(quoteForm);
378 
379  quoteButton->clicked().connect(quoteForm, &WWidget::show);
380  quoteButton->clicked().connect(requestTemplate, &WWidget::hide);
381 
382  quoteForm->hide();
383 
384  return result;
385 }
386 #endif // WT_EMWEB_BUILD
387 
389 {
390  WContainerWidget *result = new WContainerWidget();
391  result->addWidget(new WText(tr("home.download")));
392 
393  result->addWidget(new WText(tr("home.download.license")));
394 
395 #ifdef WT_EMWEB_BUILD
396  result->addWidget(quoteForm());
397 #endif // WT_EMWEB_BUILD
398 
399  result->addWidget(new WText(tr("home.download.packages")));
400 
401  releases_ = new WTable();
403  result->addWidget(releases_);
404 
405  result->addWidget(new WText(tr("home.download.other")));
406 
407  return result;
408 }
409 
410 
411 WString Home::tr(const char *key)
412 {
413  return WString::tr(key);
414 }
415 
417 {
418  std::string googleCmd =
419  "if (window.pageTracker) {"
420  """try {"
421  "" "setTimeout(function() {"
422  "" "window.pageTracker._trackPageview(\""
423  + environment().deploymentPath() + internalPath() + "\");"
424  "" "}, 1000);"
425  """} catch (e) { }"
426  "}";
427 
428  doJavaScript(googleCmd);
429 }
430 

Generated on Thu Jun 14 2012 for the C++ Web Toolkit (Wt) by doxygen 1.8.1