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) 2007 Koen Deforche, 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(boost::lexical_cast<std::wstring>(fsize)));
00032       columnWidget(1)->setStyleClass("fsize");
00033     }
00034 
00035     std::time_t t = boost::filesystem::last_write_time(path);
00036     struct tm ttm;
00037 #if WIN32
00038     ttm=*localtime(&t);
00039 #else
00040     localtime_r(&t, &ttm);
00041 #endif
00042 
00043     char c[100];
00044     strftime(c, 100, "%b %d %Y", &ttm);
00045 
00046     setColumnWidget(2, new WText(Wt::widen(c)));
00047     columnWidget(2)->setStyleClass("date");
00048   }
00049 }
00050 
00051 WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
00052 {
00053   if (boost::filesystem::exists(path)
00054       && boost::filesystem::is_directory(path))
00055     return new WIconPair("icons/yellow-folder-closed.png",
00056                          "icons/yellow-folder-open.png", false);
00057   else
00058     return new WIconPair("icons/document.png",
00059                          "icons/yellow-folder-open.png", false);
00060 }
00061 
00062 void FileTreeTableNode::populate()
00063 {
00064   try {
00065     if (boost::filesystem::is_directory(path_)) {
00066       std::set<boost::filesystem::path> paths;
00067       boost::filesystem::directory_iterator end_itr;
00068 
00069       for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
00070         paths.insert(*i);
00071 
00072       for (std::set<boost::filesystem::path>::iterator i = paths.begin();
00073            i != paths.end(); ++i)
00074         addChildNode(new FileTreeTableNode(*i));
00075     }
00076   } catch (boost::filesystem::filesystem_error& e) {
00077     std::cerr << e.what() << std::endl;
00078   }
00079 }

Generated on Sun Jul 1 19:37:23 2007 for Wt by doxygen 1.4.7