00001
00002
00003
00004
00005
00006
00007
00008 #include "FileTreeTableNode.h"
00009
00010 #include <boost/filesystem/operations.hpp>
00011 #include <boost/filesystem/exception.hpp>
00012 #include <boost/lexical_cast.hpp>
00013 #include <iostream>
00014 #include <time.h>
00015
00016 #include <Wt/WIconPair>
00017 #include <Wt/WStringUtil>
00018 #include <Wt/WText>
00019
00020 using namespace Wt;
00021
00022 FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
00023 : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
00024 path_(path)
00025 {
00026 label()->setTextFormat(PlainText);
00027
00028 if (boost::filesystem::exists(path)) {
00029 if (!boost::filesystem::is_directory(path)) {
00030 int fsize = (int)boost::filesystem::file_size(path);
00031 setColumnWidget(1, new WText(boost::lexical_cast<std::wstring>(fsize)));
00032 columnWidget(1)->setStyleClass("fsize");
00033 } else
00034 setSelectable(false);
00035
00036 std::time_t t = boost::filesystem::last_write_time(path);
00037 struct tm ttm;
00038 #if WIN32
00039 ttm=*localtime(&t);
00040 #else
00041 localtime_r(&t, &ttm);
00042 #endif
00043
00044 char c[100];
00045 strftime(c, 100, "%b %d %Y", &ttm);
00046
00047 setColumnWidget(2, new WText(c));
00048 columnWidget(2)->setStyleClass("date");
00049 }
00050 }
00051
00052 WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
00053 {
00054 if (boost::filesystem::exists(path)
00055 && boost::filesystem::is_directory(path))
00056 return new WIconPair("icons/yellow-folder-closed.png",
00057 "icons/yellow-folder-open.png", false);
00058 else
00059 return new WIconPair("icons/document.png",
00060 "icons/yellow-folder-open.png", false);
00061 }
00062
00063 void FileTreeTableNode::populate()
00064 {
00065 try {
00066 if (boost::filesystem::is_directory(path_)) {
00067 std::set<boost::filesystem::path> paths;
00068 boost::filesystem::directory_iterator end_itr;
00069
00070 for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
00071 paths.insert(*i);
00072
00073 for (std::set<boost::filesystem::path>::iterator i = paths.begin();
00074 i != paths.end(); ++i)
00075 addChildNode(new FileTreeTableNode(*i));
00076 }
00077 } catch (boost::filesystem::filesystem_error& e) {
00078 std::cerr << e.what() << std::endl;
00079 }
00080 }
00081
00082 bool FileTreeTableNode::expandable()
00083 {
00084 if (!populated()) {
00085 return boost::filesystem::is_directory(path_);
00086 } else
00087 return WTreeTableNode::expandable();
00088 }