Wt examples  4.13.1
Loading...
Searching...
No Matches
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 <Wt/WDateTime.h>
11#include <Wt/WIconPair.h>
12#include <Wt/WLocalDateTime.h>
13#include <Wt/WStringUtil.h>
14#include <Wt/WText.h>
15#include <Wt/WAny.h>
16#include <web/FileUtils.h>
17
18#include <Wt/cpp17/filesystem.hpp>
19
20#include <iostream>
21
22FileTreeTableNode::FileTreeTableNode(const Wt::cpp17::filesystem::path& path)
23 : WTreeTableNode(path.filename().string(), createIcon(path)),
24 path_(path)
25{
26 label()->setTextFormat(TextFormat::Plain);
27
28 if (Wt::cpp17::filesystem::exists(path)) {
29 if (!Wt::cpp17::filesystem::is_directory(path)) {
30 int fsize = (int)Wt::cpp17::filesystem::file_size(path);
31 setColumnWidget(1, std::make_unique<WText>(asString(fsize)));
32 columnWidget(1)->setStyleClass("fsize");
33 } else
34 setSelectable(false);
35
36 std::time_t t = std::chrono::system_clock::to_time_t(Wt::FileUtils::lastWriteTime(path.string()));
38 Wt::WString dateTimeStr = dateTime.toString(Wt::utf8("MMM dd yyyy"));
39
40 setColumnWidget(2, std::make_unique<WText>(dateTimeStr));
41 columnWidget(2)->setStyleClass("date");
42 }
43}
44
45std::unique_ptr<WIconPair> FileTreeTableNode::createIcon(const Wt::cpp17::filesystem::path& path)
46{
47 if (Wt::cpp17::filesystem::exists(path)
48 && Wt::cpp17::filesystem::is_directory(path))
49 return std::make_unique<WIconPair>("icons/yellow-folder-closed.png",
50 "icons/yellow-folder-open.png", false);
51 else
52 return std::make_unique<WIconPair>("icons/document.png",
53 "icons/yellow-folder-open.png", false);
54}
55
57{
58 if (Wt::cpp17::filesystem::is_directory(path_)) {
59 std::set<Wt::cpp17::filesystem::path> paths;
60 Wt::cpp17::filesystem::directory_iterator end_itr;
61
62 for (Wt::cpp17::filesystem::directory_iterator i(path_); i != end_itr; ++i)
63 try {
64 paths.insert(*i);
65 } catch (Wt::cpp17::filesystem::filesystem_error& e) {
66 std::cerr << e.what() << std::endl;
67 }
68
69 for (std::set<Wt::cpp17::filesystem::path>::iterator i = paths.begin();
70 i != paths.end(); ++i)
71 try {
72 addChildNode(std::make_unique<FileTreeTableNode>(*i));
73 } catch (Wt::cpp17::filesystem::filesystem_error& e) {
74 std::cerr << e.what() << std::endl;
75 }
76 }
77}
78
80{
81 if (!populated()) {
82 return Wt::cpp17::filesystem::is_directory(path_);
83 } else
84 return WTreeTableNode::expandable();
85}
static std::string filename(const fs::path &p)
static std::unique_ptr< WIconPair > createIcon(const cpp17::filesystem::path &path)
Create the iconpair for representing the path.
virtual bool expandable() override
Reimplements WTreeNode::expandable.
cpp17::filesystem::path path_
The path.
virtual void populate() override
Reimplements WTreeNode::populate to read files within a directory.
FileTreeTableNode(const cpp17::filesystem::path &path)
Construct a new node for the given file.
static WDateTime fromTime_t(std::time_t seconds)
WString toString() const
bool setTextFormat(TextFormat format)
virtual void setSelectable(bool selectable) override
WTreeNode * addChildNode(std::unique_ptr< WTreeNode > node)
bool populated() const
WText * label() const
WWidget * columnWidget(int column)
void setColumnWidget(int column, std::unique_ptr< WWidget > item)
virtual void setStyleClass(const WString &styleClass)=0
WString asString(const cpp17::any &v, const WString &formatString=WString())