Wt examples  3.7.1
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>
16 #include <Wt/WIconPair>
17 #include <Wt/WLocalDateTime>
18 #include <Wt/WStringUtil>
19 #include <Wt/WText>
20 
21 using namespace Wt;
22 
23 FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
24 #if BOOST_FILESYSTEM_VERSION < 3
25 #ifndef WT_NO_STD_WSTRING
26  : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
27 #else
28  : WTreeTableNode(path.leaf(), createIcon(path)),
29 #endif
30 #else
31  : WTreeTableNode(path.leaf().string(), createIcon(path)),
32 #endif
33  path_(path)
34 {
35  label()->setTextFormat(PlainText);
36 
37  if (boost::filesystem::exists(path)) {
38  if (!boost::filesystem::is_directory(path)) {
39  int fsize = (int)boost::filesystem::file_size(path);
40  setColumnWidget(1, new WText(boost::lexical_cast<std::string>(fsize)));
41  columnWidget(1)->setStyleClass("fsize");
42  } else
43  setSelectable(false);
44 
45  std::time_t t = boost::filesystem::last_write_time(path);
47  Wt::WLocalDateTime localDateTime = dateTime.toLocalTime();
48  Wt::WString dateTimeStr = localDateTime.toString(Wt::utf8("MMM dd yyyy"));
49 
50  setColumnWidget(2, new WText(dateTimeStr));
51  columnWidget(2)->setStyleClass("date");
52  }
53 }
54 
55 WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
56 {
57  if (boost::filesystem::exists(path)
58  && boost::filesystem::is_directory(path))
59  return new WIconPair("icons/yellow-folder-closed.png",
60  "icons/yellow-folder-open.png", false);
61  else
62  return new 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(new 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
95 }
static Wt::WIconPair * createIcon(const boost::filesystem::path &path)
Create the iconpair for representing the path.
virtual void populate()
Reimplements WTreeNode::populate to read files within a directory.
WString toString() const
WLocalDateTime toLocalTime(const WLocale &locale=WLocale::currentLocale()) const
virtual bool expandable()
static WDateTime fromTime_t(std::time_t seconds)
virtual bool expandable()
Reimplements WTreeNode::expandable.
FileTreeTableNode(const boost::filesystem::path &path)
Construct a new node for the given file.
A single node in a file tree table.

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13