Wt examples: /home/koen/project/wt/public-git/wt/examples/filetreetable/FileTreeTableNode.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/filetreetable/FileTreeTableNode.C
Go to the documentation of this file.

    00001 // This may look like C code, but it's really -*- C++ -*-
    00002 /*
    00003  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
    00004  *
    00005  * See the LICENSE file for terms of use.
    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 #if BOOST_FILESYSTEM_VERSION < 3
    00024 #ifndef WT_NO_STD_WSTRING
    00025   : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
    00026 #else
    00027   : WTreeTableNode(path.leaf(), createIcon(path)),
    00028 #endif
    00029 #else
    00030   : WTreeTableNode(path.leaf().string(), createIcon(path)),
    00031 #endif
    00032     path_(path)
    00033 {
    00034   label()->setTextFormat(PlainText);
    00035
    00036   if (boost::filesystem::exists(path)) {
    00037     if (!boost::filesystem::is_directory(path)) {
    00038       int fsize = (int)boost::filesystem::file_size(path);
    00039       setColumnWidget(1, new WText(boost::lexical_cast<std::string>(fsize)));
    00040       columnWidget(1)->setStyleClass("fsize");
    00041     } else
    00042       setSelectable(false);
    00043
    00044     std::time_t t = boost::filesystem::last_write_time(path);
    00045     struct tm ttm;
    00046 #if WIN32
    00047     ttm=*localtime(&t);
    00048 #else
    00049     localtime_r(&t, &ttm);
    00050 #endif
    00051
    00052     char c[100];
    00053     strftime(c, 100, "%b %d %Y", &ttm);
    00054
    00055     setColumnWidget(2, new WText(c));
    00056     columnWidget(2)->setStyleClass("date");
    00057   }
    00058 }
    00059
    00060 WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
    00061 {
    00062   if (boost::filesystem::exists(path)
    00063       && boost::filesystem::is_directory(path))
    00064     return new WIconPair("icons/yellow-folder-closed.png",
    00065                          "icons/yellow-folder-open.png", false);
    00066   else
    00067     return new WIconPair("icons/document.png",
    00068                          "icons/yellow-folder-open.png", false);
    00069 }
    00070
    00071 void FileTreeTableNode::populate()
    00072 {
    00073   try {
    00074     if (boost::filesystem::is_directory(path_)) {
    00075       std::set<boost::filesystem::path> paths;
    00076       boost::filesystem::directory_iterator end_itr;
    00077
    00078       for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
    00079         paths.insert(*i);
    00080
    00081       for (std::set<boost::filesystem::path>::iterator i = paths.begin();
    00082            i != paths.end(); ++i)
    00083         addChildNode(new FileTreeTableNode(*i));
    00084     }
    00085   } catch (boost::filesystem::filesystem_error& e) {
    00086     std::cerr << e.what() << std::endl;
    00087   }
    00088 }
    00089
    00090 bool FileTreeTableNode::expandable()
    00091 {
    00092   if (!populated()) {
    00093     return boost::filesystem::is_directory(path_);
    00094   } else
    00095     return WTreeTableNode::expandable();
    00096 }