Wt examples  4.11.3
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/exception.hpp>
11 
12 #include <boost/version.hpp>
13 #if BOOST_VERSION < 108500
14 #include <boost/filesystem/convenience.hpp>
15 #else
16 #include <boost/filesystem/directory.hpp>
17 #endif
18 #include <boost/filesystem/operations.hpp>
19 
20 #include <boost/lexical_cast.hpp>
21 
22 #include <Wt/WDateTime.h>
23 #include <Wt/WIconPair.h>
24 #include <Wt/WLocalDateTime.h>
25 #include <Wt/WStringUtil.h>
26 #include <Wt/WText.h>
27 #include <Wt/WAny.h>
28 
29 #include <iostream>
30 
31 FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
32 #if BOOST_FILESYSTEM_VERSION < 3
33  : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
34 #else
35  : WTreeTableNode(path.filename().string(), createIcon(path)),
36 #endif
37  path_(path)
38 {
39  label()->setTextFormat(TextFormat::Plain);
40 
41  if (boost::filesystem::exists(path)) {
42  if (!boost::filesystem::is_directory(path)) {
43  int fsize = (int)boost::filesystem::file_size(path);
44  setColumnWidget(1, std::make_unique<WText>(asString(fsize)));
45  columnWidget(1)->setStyleClass("fsize");
46  } else
47  setSelectable(false);
48 
49  std::time_t t = boost::filesystem::last_write_time(path);
51  Wt::WString dateTimeStr = dateTime.toString(Wt::utf8("MMM dd yyyy"));
52 
53  setColumnWidget(2, std::make_unique<WText>(dateTimeStr));
54  columnWidget(2)->setStyleClass("date");
55  }
56 }
57 
58 std::unique_ptr<WIconPair> FileTreeTableNode::createIcon(const boost::filesystem::path& path)
59 {
60  if (boost::filesystem::exists(path)
61  && boost::filesystem::is_directory(path))
62  return std::make_unique<WIconPair>("icons/yellow-folder-closed.png",
63  "icons/yellow-folder-open.png", false);
64  else
65  return std::make_unique<WIconPair>("icons/document.png",
66  "icons/yellow-folder-open.png", false);
67 }
68 
70 {
71  if (boost::filesystem::is_directory(path_)) {
72  std::set<boost::filesystem::path> paths;
73  boost::filesystem::directory_iterator end_itr;
74 
75  for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
76  try {
77  paths.insert(*i);
78  } catch (boost::filesystem::filesystem_error& e) {
79  std::cerr << e.what() << std::endl;
80  }
81 
82  for (std::set<boost::filesystem::path>::iterator i = paths.begin();
83  i != paths.end(); ++i)
84  try {
85  addChildNode(std::make_unique<FileTreeTableNode>(*i));
86  } catch (boost::filesystem::filesystem_error& e) {
87  std::cerr << e.what() << std::endl;
88  }
89  }
90 }
91 
93 {
94  if (!populated()) {
95  return boost::filesystem::is_directory(path_);
96  } else
97  return WTreeTableNode::expandable();
98 }
boost::filesystem::path path_
The path.
static std::unique_ptr< WIconPair > createIcon(const boost::filesystem::path &path)
Create the iconpair for representing the path.
virtual bool expandable() override
Reimplements WTreeNode::expandable.
FileTreeTableNode(const boost::filesystem::path &path)
Construct a new node for the given file.
virtual void populate() override
Reimplements WTreeNode::populate to read files within a directory.
static WDateTime fromTime_t(std::time_t seconds)
WString toString() const
WTreeNode * addChildNode(std::unique_ptr< WTreeNode > node)
bool populated() const
WString asString(const cpp17::any &v, const WString &formatString=WString())