Wt examples  4.0.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 bvba, Kessel-Lo, 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 #include <time.h>
15 
16 #include <Wt/WIconPair.h>
17 #include <Wt/WStringUtil.h>
18 #include <Wt/WText.h>
19 #include <Wt/WAny.h>
20 
21 FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
22 #if BOOST_FILESYSTEM_VERSION < 3
23  : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
24 #else
25  : WTreeTableNode(path.leaf().string(), createIcon(path)),
26 #endif
27  path_(path)
28 {
29  label()->setTextFormat(TextFormat::Plain);
30 
31  if (boost::filesystem::exists(path)) {
32  if (!boost::filesystem::is_directory(path)) {
33  int fsize = (int)boost::filesystem::file_size(path);
34  setColumnWidget(1, cpp14::make_unique<WText>(asString(fsize)));
35  columnWidget(1)->setStyleClass("fsize");
36  } else
37  setSelectable(false);
38 
39  std::time_t t = boost::filesystem::last_write_time(path);
40  struct tm ttm;
41 #if WIN32
42  ttm=*localtime(&t);
43 #else
44  localtime_r(&t, &ttm);
45 #endif
46 
47  char c[100];
48  strftime(c, 100, "%b %d %Y", &ttm);
49 
50  setColumnWidget(2, cpp14::make_unique<WText>(c));
51  columnWidget(2)->setStyleClass("date");
52  }
53 }
54 
55 std::unique_ptr<WIconPair> FileTreeTableNode::createIcon(const boost::filesystem::path& path)
56 {
57  if (boost::filesystem::exists(path)
58  && boost::filesystem::is_directory(path))
59  return cpp14::make_unique<WIconPair>("icons/yellow-folder-closed.png",
60  "icons/yellow-folder-open.png", false);
61  else
62  return cpp14::make_unique<WIconPair>("icons/document.png",
63  "icons/yellow-folder-open.png", false);
64 }
65 
67 {
68  if (boost::filesystem::is_directory(path_)) {
69  std::set<boost::filesystem::path> paths;
70  boost::filesystem::directory_iterator end_itr;
71 
72  for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
73  try {
74  paths.insert(*i);
75  } catch (boost::filesystem::filesystem_error& e) {
76  std::cerr << e.what() << std::endl;
77  }
78 
79  for (std::set<boost::filesystem::path>::iterator i = paths.begin();
80  i != paths.end(); ++i)
81  try {
82  addChildNode(cpp14::make_unique<FileTreeTableNode>(*i));
83  } catch (boost::filesystem::filesystem_error& e) {
84  std::cerr << e.what() << std::endl;
85  }
86  }
87 }
88 
90 {
91  if (!populated()) {
92  return boost::filesystem::is_directory(path_);
93  } else
94  return WTreeTableNode::expandable();
95 }
bool populated() const
WTreeNode * addChildNode(std::unique_ptr< WTreeNode > node)
virtual bool expandable() override
Reimplements WTreeNode::expandable.
boost::filesystem::path path_
The path.
static std::unique_ptr< WIconPair > createIcon(const boost::filesystem::path &path)
Create the iconpair for representing the path.
WString asString(const cpp17::any &v, const WString &formatString=WString())
virtual void populate() override
Reimplements WTreeNode::populate to read files within a directory.
FileTreeTableNode(const boost::filesystem::path &path)
Construct a new node for the given file.

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11