/home/koen/project/wt/cvs/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 <WIconPair>
00017 #include <WStringUtil>
00018 #include <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()->setFormatting(WText::PlainFormatting);
00027 
00028   if (boost::filesystem::exists(path)) {
00029     if (!boost::filesystem::is_directory(path)) {
00030       int fsize = boost::filesystem::file_size(path);
00031       setColumnWidget(1, new WText(false,
00032                                    boost::lexical_cast<std::wstring>(fsize)));
00033       columnWidget(1)->setStyleClass("fsize");
00034     } else
00035       setSelectable(false);
00036 
00037     std::time_t t = boost::filesystem::last_write_time(path);
00038     struct tm ttm;
00039 #if WIN32
00040     ttm=*localtime(&t);
00041 #else
00042     localtime_r(&t, &ttm);
00043 #endif
00044 
00045     char c[100];
00046     strftime(c, 100, "%b %d %Y", &ttm);
00047 
00048     setColumnWidget(2, new WText(c));
00049     columnWidget(2)->setStyleClass("date");
00050   }
00051 }
00052 
00053 WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
00054 {
00055   if (boost::filesystem::exists(path)
00056       && boost::filesystem::is_directory(path))
00057     return new WIconPair("icons/yellow-folder-closed.png",
00058                          "icons/yellow-folder-open.png", false);
00059   else
00060     return new WIconPair("icons/document.png",
00061                          "icons/yellow-folder-open.png", false);
00062 }
00063 
00064 void FileTreeTableNode::populate()
00065 {
00066   try {
00067     if (boost::filesystem::is_directory(path_)) {
00068       std::set<boost::filesystem::path> paths;
00069       boost::filesystem::directory_iterator end_itr;
00070 
00071       for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
00072         paths.insert(*i);
00073 
00074       for (std::set<boost::filesystem::path>::iterator i = paths.begin();
00075            i != paths.end(); ++i)
00076         addChildNode(new FileTreeTableNode(*i));
00077     }
00078   } catch (boost::filesystem::filesystem_error& e) {
00079     std::cerr << e.what() << std::endl;
00080   }
00081 }
00082 
00083 bool FileTreeTableNode::expandable()
00084 {
00085   if (!populated()) {
00086     return boost::filesystem::is_directory(path_);
00087   } else
00088     return WTreeTableNode::expandable();
00089 }

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3