Wt examples  4.3.0
FileTreeTableNode.C
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 
8 #include "FileTreeTableNode.h"
9 
10 #include <boost/filesystem/operations.hpp>
11 #include <boost/filesystem/exception.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <iostream>
14 
15 #include <Wt/WDateTime.h>
16 #include <Wt/WIconPair.h>
17 #include <Wt/WLocalDateTime.h>
18 #include <Wt/WStringUtil.h>
19 #include <Wt/WText.h>
20 #include <Wt/WAny.h>
21 
22 FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
23 #if BOOST_FILESYSTEM_VERSION < 3
24  : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
25 #else
26  : WTreeTableNode(path.leaf().string(), createIcon(path)),
27 #endif
28  path_(path)
29 {
30  label()->setTextFormat(TextFormat::Plain);
31 
32  if (boost::filesystem::exists(path)) {
33  if (!boost::filesystem::is_directory(path)) {
34  int fsize = (int)boost::filesystem::file_size(path);
35  setColumnWidget(1, cpp14::make_unique<WText>(asString(fsize)));
36  columnWidget(1)->setStyleClass("fsize");
37  } else
38  setSelectable(false);
39 
40  std::time_t t = boost::filesystem::last_write_time(path);
42  Wt::WLocalDateTime localDateTime = dateTime.toLocalTime();
43  Wt::WString dateTimeStr = localDateTime.toString(Wt::utf8("MMM dd yyyy"));
44 
45  setColumnWidget(2, cpp14::make_unique<WText>(dateTimeStr));
46  columnWidget(2)->setStyleClass("date");
47  }
48 }
49 
50 std::unique_ptr<WIconPair> FileTreeTableNode::createIcon(const boost::filesystem::path& path)
51 {
52  if (boost::filesystem::exists(path)
53  && boost::filesystem::is_directory(path))
54  return cpp14::make_unique<WIconPair>("icons/yellow-folder-closed.png",
55  "icons/yellow-folder-open.png", false);
56  else
57  return cpp14::make_unique<WIconPair>("icons/document.png",
58  "icons/yellow-folder-open.png", false);
59 }
60 
62 {
63  if (boost::filesystem::is_directory(path_)) {
64  std::set<boost::filesystem::path> paths;
65  boost::filesystem::directory_iterator end_itr;
66 
67  for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
68  try {
69  paths.insert(*i);
70  } catch (boost::filesystem::filesystem_error& e) {
71  std::cerr << e.what() << std::endl;
72  }
73 
74  for (std::set<boost::filesystem::path>::iterator i = paths.begin();
75  i != paths.end(); ++i)
76  try {
77  addChildNode(cpp14::make_unique<FileTreeTableNode>(*i));
78  } catch (boost::filesystem::filesystem_error& e) {
79  std::cerr << e.what() << std::endl;
80  }
81  }
82 }
83 
85 {
86  if (!populated()) {
87  return boost::filesystem::is_directory(path_);
88  } else
89  return WTreeTableNode::expandable();
90 }
Wt::WDateTime
FileTreeTableNode::FileTreeTableNode
FileTreeTableNode(const boost::filesystem::path &path)
Construct a new node for the given file.
Definition: FileTreeTableNode.C:22
Wt::WLocalDateTime::toString
WString toString() const
asString
WString asString(const cpp17::any &v, const WString &formatString=WString())
Wt::WDateTime::toLocalTime
WLocalDateTime toLocalTime(const WLocale &locale=WLocale::currentLocale()) const
FileTreeTableNode::expandable
virtual bool expandable() override
Reimplements WTreeNode::expandable.
Definition: FileTreeTableNode.C:84
Wt::WString
Wt::WTreeNode::addChildNode
WTreeNode * addChildNode(std::unique_ptr< WTreeNode > node)
Wt::WLocalDateTime
FileTreeTableNode::path_
boost::filesystem::path path_
The path.
Definition: FileTreeTableNode.h:39
Wt::WTreeTableNode
FileTreeTableNode::createIcon
static std::unique_ptr< WIconPair > createIcon(const boost::filesystem::path &path)
Create the iconpair for representing the path.
Definition: FileTreeTableNode.C:50
FileTreeTableNode::populate
virtual void populate() override
Reimplements WTreeNode::populate to read files within a directory.
Definition: FileTreeTableNode.C:61
FileTreeTableNode.h
Wt::WTreeNode::populated
bool populated() const
Wt::WDateTime::fromTime_t
static WDateTime fromTime_t(std::time_t seconds)

Generated on Thu Mar 26 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.17